@positronic/client-anthropic 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,307 @@
1
+ function _array_like_to_array(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _array_without_holes(arr) {
7
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
8
+ }
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
+ try {
11
+ var info = gen[key](arg);
12
+ var value = info.value;
13
+ } catch (error) {
14
+ reject(error);
15
+ return;
16
+ }
17
+ if (info.done) {
18
+ resolve(value);
19
+ } else {
20
+ Promise.resolve(value).then(_next, _throw);
21
+ }
22
+ }
23
+ function _async_to_generator(fn) {
24
+ return function() {
25
+ var self = this, args = arguments;
26
+ return new Promise(function(resolve, reject) {
27
+ var gen = fn.apply(self, args);
28
+ function _next(value) {
29
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
+ }
31
+ function _throw(err) {
32
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
33
+ }
34
+ _next(undefined);
35
+ });
36
+ };
37
+ }
38
+ function _class_call_check(instance, Constructor) {
39
+ if (!(instance instanceof Constructor)) {
40
+ throw new TypeError("Cannot call a class as a function");
41
+ }
42
+ }
43
+ function _defineProperties(target, props) {
44
+ for(var i = 0; i < props.length; i++){
45
+ var descriptor = props[i];
46
+ descriptor.enumerable = descriptor.enumerable || false;
47
+ descriptor.configurable = true;
48
+ if ("value" in descriptor) descriptor.writable = true;
49
+ Object.defineProperty(target, descriptor.key, descriptor);
50
+ }
51
+ }
52
+ function _create_class(Constructor, protoProps, staticProps) {
53
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
54
+ if (staticProps) _defineProperties(Constructor, staticProps);
55
+ return Constructor;
56
+ }
57
+ function _define_property(obj, key, value) {
58
+ if (key in obj) {
59
+ Object.defineProperty(obj, key, {
60
+ value: value,
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true
64
+ });
65
+ } else {
66
+ obj[key] = value;
67
+ }
68
+ return obj;
69
+ }
70
+ function _iterable_to_array(iter) {
71
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
72
+ }
73
+ function _non_iterable_spread() {
74
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
75
+ }
76
+ function _object_spread(target) {
77
+ for(var i = 1; i < arguments.length; i++){
78
+ var source = arguments[i] != null ? arguments[i] : {};
79
+ var ownKeys = Object.keys(source);
80
+ if (typeof Object.getOwnPropertySymbols === "function") {
81
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
82
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
83
+ }));
84
+ }
85
+ ownKeys.forEach(function(key) {
86
+ _define_property(target, key, source[key]);
87
+ });
88
+ }
89
+ return target;
90
+ }
91
+ function ownKeys(object, enumerableOnly) {
92
+ var keys = Object.keys(object);
93
+ if (Object.getOwnPropertySymbols) {
94
+ var symbols = Object.getOwnPropertySymbols(object);
95
+ if (enumerableOnly) {
96
+ symbols = symbols.filter(function(sym) {
97
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
98
+ });
99
+ }
100
+ keys.push.apply(keys, symbols);
101
+ }
102
+ return keys;
103
+ }
104
+ function _object_spread_props(target, source) {
105
+ source = source != null ? source : {};
106
+ if (Object.getOwnPropertyDescriptors) {
107
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
108
+ } else {
109
+ ownKeys(Object(source)).forEach(function(key) {
110
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
111
+ });
112
+ }
113
+ return target;
114
+ }
115
+ function _to_consumable_array(arr) {
116
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
117
+ }
118
+ function _unsupported_iterable_to_array(o, minLen) {
119
+ if (!o) return;
120
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
121
+ var n = Object.prototype.toString.call(o).slice(8, -1);
122
+ if (n === "Object" && o.constructor) n = o.constructor.name;
123
+ if (n === "Map" || n === "Set") return Array.from(n);
124
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
125
+ }
126
+ function _ts_generator(thisArg, body) {
127
+ var f, y, t, _ = {
128
+ label: 0,
129
+ sent: function() {
130
+ if (t[0] & 1) throw t[1];
131
+ return t[1];
132
+ },
133
+ trys: [],
134
+ ops: []
135
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
136
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
137
+ return this;
138
+ }), g;
139
+ function verb(n) {
140
+ return function(v) {
141
+ return step([
142
+ n,
143
+ v
144
+ ]);
145
+ };
146
+ }
147
+ function step(op) {
148
+ if (f) throw new TypeError("Generator is already executing.");
149
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
150
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
151
+ if (y = 0, t) op = [
152
+ op[0] & 2,
153
+ t.value
154
+ ];
155
+ switch(op[0]){
156
+ case 0:
157
+ case 1:
158
+ t = op;
159
+ break;
160
+ case 4:
161
+ _.label++;
162
+ return {
163
+ value: op[1],
164
+ done: false
165
+ };
166
+ case 5:
167
+ _.label++;
168
+ y = op[1];
169
+ op = [
170
+ 0
171
+ ];
172
+ continue;
173
+ case 7:
174
+ op = _.ops.pop();
175
+ _.trys.pop();
176
+ continue;
177
+ default:
178
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
179
+ _ = 0;
180
+ continue;
181
+ }
182
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
183
+ _.label = op[1];
184
+ break;
185
+ }
186
+ if (op[0] === 6 && _.label < t[1]) {
187
+ _.label = t[1];
188
+ t = op;
189
+ break;
190
+ }
191
+ if (t && _.label < t[2]) {
192
+ _.label = t[2];
193
+ _.ops.push(op);
194
+ break;
195
+ }
196
+ if (t[2]) _.ops.pop();
197
+ _.trys.pop();
198
+ continue;
199
+ }
200
+ op = body.call(thisArg, _);
201
+ } catch (e) {
202
+ op = [
203
+ 6,
204
+ e
205
+ ];
206
+ y = 0;
207
+ } finally{
208
+ f = t = 0;
209
+ }
210
+ if (op[0] & 5) throw op[1];
211
+ return {
212
+ value: op[0] ? op[1] : void 0,
213
+ done: true
214
+ };
215
+ }
216
+ }
217
+ import Instructor from '@instructor-ai/instructor';
218
+ import { createLLMClient } from 'llm-polyglot';
219
+ import { config } from 'dotenv';
220
+ config();
221
+ var anthropic = createLLMClient({
222
+ provider: 'anthropic',
223
+ apiKey: process.env.ANTHROPIC_API_KEY,
224
+ defaultHeaders: {
225
+ 'anthropic-beta': 'output-128k-2025-02-19'
226
+ }
227
+ });
228
+ export var AnthropicClient = /*#__PURE__*/ function() {
229
+ "use strict";
230
+ function AnthropicClient() {
231
+ _class_call_check(this, AnthropicClient);
232
+ _define_property(this, "client", void 0);
233
+ this.client = Instructor({
234
+ client: anthropic,
235
+ mode: 'TOOLS'
236
+ });
237
+ }
238
+ _create_class(AnthropicClient, [
239
+ {
240
+ key: "generateObject",
241
+ value: function generateObject(params) {
242
+ return _async_to_generator(function() {
243
+ var messages, model, max_tokens, temperature, top_p, extra_options, response;
244
+ return _ts_generator(this, function(_state) {
245
+ switch(_state.label){
246
+ case 0:
247
+ // Compose messages array according to the interface contract
248
+ messages = params.messages ? _to_consumable_array(params.messages) : [];
249
+ if (params.system) {
250
+ messages = [
251
+ {
252
+ role: 'system',
253
+ content: params.system
254
+ }
255
+ ].concat(_to_consumable_array(messages));
256
+ }
257
+ if (params.prompt) {
258
+ messages = _to_consumable_array(messages).concat([
259
+ {
260
+ role: 'user',
261
+ content: params.prompt
262
+ }
263
+ ]);
264
+ }
265
+ model = 'claude-3-7-sonnet-latest';
266
+ max_tokens = 64000;
267
+ temperature = 0.5;
268
+ top_p = 1;
269
+ extra_options = {
270
+ thinking: {
271
+ type: 'enabled',
272
+ budget_tokens: max_tokens
273
+ }
274
+ };
275
+ return [
276
+ 4,
277
+ this.client.chat.completions.create(_object_spread_props(_object_spread({
278
+ messages: messages,
279
+ model: model,
280
+ response_model: {
281
+ schema: params.schema,
282
+ name: params.schemaName,
283
+ description: params.schemaDescription
284
+ },
285
+ max_tokens: max_tokens
286
+ }, temperature !== undefined ? {
287
+ temperature: temperature
288
+ } : {}, top_p !== undefined ? {
289
+ top_p: top_p
290
+ } : {}), {
291
+ extra_options: extra_options
292
+ }))
293
+ ];
294
+ case 1:
295
+ response = _state.sent();
296
+ return [
297
+ 2,
298
+ response
299
+ ];
300
+ }
301
+ });
302
+ }).call(this);
303
+ }
304
+ }
305
+ ]);
306
+ return AnthropicClient;
307
+ }();
@@ -0,0 +1,18 @@
1
+ import type { ObjectGenerator } from '@positronic/core';
2
+ import { z } from 'zod';
3
+ export declare class AnthropicClient implements ObjectGenerator {
4
+ private client;
5
+ constructor();
6
+ generateObject<T extends z.AnyZodObject>(params: {
7
+ schema: T;
8
+ schemaName: string;
9
+ schemaDescription?: string;
10
+ prompt?: string;
11
+ messages?: {
12
+ role: 'user' | 'assistant' | 'system';
13
+ content: string;
14
+ }[];
15
+ system?: string;
16
+ }): Promise<z.infer<T>>;
17
+ }
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAGxD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAcxB,qBAAa,eAAgB,YAAW,eAAe;IACrD,OAAO,CAAC,MAAM,CAAqC;;IAS7C,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,YAAY,EAAE,MAAM,EAAE;QACrD,MAAM,EAAE,CAAC,CAAC;QACV,UAAU,EAAE,MAAM,CAAC;QACnB,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE;YAAE,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,QAAQ,CAAC;YAAC,OAAO,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;QACxE,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CAmCxB"}
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@positronic/client-anthropic",
3
+ "version": "0.0.2",
4
+ "description": "Anthropic client for Positronic",
5
+ "type": "module",
6
+ "main": "dist/src/index.js",
7
+ "types": "dist/types/index.ts",
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "tsc": "tsc --project tsconfig.json",
11
+ "swc": "swc src -d dist",
12
+ "build": "npm run tsc && npm run swc",
13
+ "clean": "rm -rf tsconfig.tsbuildinfo dist node_modules"
14
+ },
15
+ "dependencies": {
16
+ "@positronic/core": "^0.0.1",
17
+ "@instructor-ai/instructor": "^1.5.0",
18
+ "llm-polyglot": "^2.4.0",
19
+ "dotenv": "^16.4.7",
20
+ "zod": "^3.24.1"
21
+ }
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1,70 @@
1
+ import type { ObjectGenerator } from '@positronic/core';
2
+ import Instructor from '@instructor-ai/instructor';
3
+ import { createLLMClient } from 'llm-polyglot';
4
+ import { z } from 'zod';
5
+ import { config } from 'dotenv';
6
+ import type { InstructorClient } from '@instructor-ai/instructor';
7
+
8
+ config();
9
+
10
+ const anthropic = createLLMClient({
11
+ provider: 'anthropic',
12
+ apiKey: process.env.ANTHROPIC_API_KEY,
13
+ defaultHeaders: {
14
+ 'anthropic-beta': 'output-128k-2025-02-19',
15
+ },
16
+ });
17
+
18
+ export class AnthropicClient implements ObjectGenerator {
19
+ private client: InstructorClient<typeof anthropic>;
20
+
21
+ constructor() {
22
+ this.client = Instructor({
23
+ client: anthropic,
24
+ mode: 'TOOLS',
25
+ });
26
+ }
27
+
28
+ async generateObject<T extends z.AnyZodObject>(params: {
29
+ schema: T;
30
+ schemaName: string;
31
+ schemaDescription?: string;
32
+ prompt?: string;
33
+ messages?: { role: 'user' | 'assistant' | 'system'; content: string }[];
34
+ system?: string;
35
+ }): Promise<z.infer<T>> {
36
+ // Compose messages array according to the interface contract
37
+ let messages = params.messages ? [...params.messages] : [];
38
+ if (params.system) {
39
+ messages = [{ role: 'system', content: params.system }, ...messages];
40
+ }
41
+ if (params.prompt) {
42
+ messages = [...messages, { role: 'user', content: params.prompt }];
43
+ }
44
+
45
+ const model = 'claude-3-7-sonnet-latest';
46
+ const max_tokens = 64000;
47
+ const temperature = 0.5;
48
+ const top_p = 1;
49
+ const extra_options = {
50
+ thinking: {
51
+ type: 'enabled',
52
+ budget_tokens: max_tokens,
53
+ },
54
+ };
55
+ const response = await this.client.chat.completions.create({
56
+ messages,
57
+ model,
58
+ response_model: {
59
+ schema: params.schema,
60
+ name: params.schemaName,
61
+ description: params.schemaDescription,
62
+ },
63
+ max_tokens,
64
+ ...(temperature !== undefined ? { temperature } : {}),
65
+ ...(top_p !== undefined ? { top_p } : {}),
66
+ extra_options,
67
+ });
68
+ return response;
69
+ }
70
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "emitDeclarationOnly": true,
6
+ "declarationDir": "./dist/types"
7
+ },
8
+ "include": ["**/*"],
9
+ "exclude": ["dist", "node_modules", "**/*.test.ts"]
10
+ }