structured-json-agent 1.1.0 → 1.2.0
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.
- package/README.md +39 -4
- package/dist/agent/index.d.ts +17 -3
- package/dist/agent/index.js +51 -10
- package/dist/agent/index.js.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/llm/adapters/anthropic.d.ts +2 -2
- package/dist/llm/adapters/anthropic.js +12 -1
- package/dist/llm/adapters/anthropic.js.map +1 -1
- package/dist/llm/adapters/deepseek.d.ts +14 -0
- package/dist/llm/adapters/deepseek.js +69 -0
- package/dist/llm/adapters/deepseek.js.map +1 -0
- package/dist/llm/adapters/google.d.ts +2 -2
- package/dist/llm/adapters/google.js +18 -3
- package/dist/llm/adapters/google.js.map +1 -1
- package/dist/llm/adapters/openai.d.ts +2 -2
- package/dist/llm/adapters/openai.js +12 -1
- package/dist/llm/adapters/openai.js.map +1 -1
- package/dist/llm/factory.js +8 -1
- package/dist/llm/factory.js.map +1 -1
- package/dist/llm/types.d.ts +12 -1
- package/dist/types/index.d.ts +16 -4
- package/dist/types/index.js +7 -1
- package/dist/types/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ This library orchestrates a **Generator ↔ Reviewer** cycle to ensure that the
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
9
|
* **Guaranteed JSON Output**: Enforces strict adherence to Zod Schemas.
|
|
10
|
-
* **Multi-Provider Support**: Built-in adapters for **OpenAI**, **Google GenAI (Gemini)**,
|
|
10
|
+
* **Multi-Provider Support**: Built-in adapters for **OpenAI**, **Google GenAI (Gemini)**, **Anthropic (Claude)**, and **DeepSeek**.
|
|
11
11
|
* **Structured Outputs**: Leverages native structured output capabilities of providers (e.g., OpenAI Structured Outputs, Anthropic Beta) when available.
|
|
12
12
|
* **Iterative Self-Correction**: Automatically detects validation errors and feeds them back to a "Reviewer" model to fix the output.
|
|
13
13
|
* **Type-Safe**: Built with TypeScript and Zod for full type inference and safety.
|
|
@@ -73,15 +73,21 @@ const agent = new StructuredAgent({
|
|
|
73
73
|
|
|
74
74
|
### 2. Run the Agent
|
|
75
75
|
|
|
76
|
+
To run the agent, use the `run` method with your input data. Optionally, provide a reference (string or number) identifier for tracking in the second parameter.
|
|
77
|
+
|
|
76
78
|
```typescript
|
|
77
79
|
async function main() {
|
|
78
80
|
try {
|
|
79
81
|
const result = await agent.run({
|
|
80
82
|
topic: "Clean Architecture",
|
|
81
83
|
depth: "advanced"
|
|
82
|
-
});
|
|
84
|
+
}, "12345"); // Optional reference for tracking
|
|
85
|
+
|
|
86
|
+
console.log("Output:", result.output);
|
|
87
|
+
console.log("Metadata:", result.metadata);
|
|
88
|
+
// Metadata includes provider, model, and iteration count
|
|
89
|
+
console.log("Reference:", result.ref);
|
|
83
90
|
|
|
84
|
-
console.log("Result:", result);
|
|
85
91
|
// Result is typed as inferred from outputSchema
|
|
86
92
|
} catch (error) {
|
|
87
93
|
console.error("Agent failed:", error);
|
|
@@ -91,6 +97,35 @@ async function main() {
|
|
|
91
97
|
main();
|
|
92
98
|
```
|
|
93
99
|
|
|
100
|
+
In TypeScript, you can use the `run<T>` method to get a typed result.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const result = await agent.run<T>({
|
|
104
|
+
topic: "Clean Architecture",
|
|
105
|
+
depth: "advanced"
|
|
106
|
+
}, "12345");
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The result object is of type `AgentResult<T>`, where `T` is the type inferred from `outputSchema`.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
type AgentResult<T> = {
|
|
113
|
+
output: T; // Output as per outputSchema
|
|
114
|
+
metadata: {
|
|
115
|
+
provider: string; // e.g., "openai", "deepseek"
|
|
116
|
+
model: string; // e.g., "gpt-4o", "claude-3-5-sonnet"
|
|
117
|
+
inputTokens: number; // Number of tokens in the input
|
|
118
|
+
outputTokens: number; // Number of tokens in the output
|
|
119
|
+
step: string; // Step description ("generation", "review-1", "review-2", etc.)
|
|
120
|
+
validation: {
|
|
121
|
+
valid: boolean; // Whether the output is valid against outputSchema
|
|
122
|
+
errors?: string[]; // Validation errors if any
|
|
123
|
+
}
|
|
124
|
+
}[];
|
|
125
|
+
ref?: string | number; // Optional reference provided for tracking
|
|
126
|
+
};
|
|
127
|
+
```
|
|
128
|
+
|
|
94
129
|
## How It Works
|
|
95
130
|
|
|
96
131
|
1. **Validation**: The input JSON is validated against the `inputSchema` (Zod).
|
|
@@ -121,7 +156,7 @@ Configuration object passed to `new StructuredAgent(config)`.
|
|
|
121
156
|
|
|
122
157
|
| Property | Type | Description |
|
|
123
158
|
| :--- | :--- | :--- |
|
|
124
|
-
| `llmService` | `OpenAI \| GoogleGenAI \| Anthropic \| ILLMService` | The provider instance or custom service. |
|
|
159
|
+
| `llmService` | `OpenAI \| GoogleGenAI \| Anthropic \| ILLMService` | The provider instance or custom service. Supports DeepSeek via OpenAI SDK. |
|
|
125
160
|
| `model` | `string` | Model ID (e.g., `gpt-4o`, `claude-3-5-sonnet`). |
|
|
126
161
|
| `config` | `ModelConfig?` | Optional parameters (temperature, max_tokens, etc.). |
|
|
127
162
|
|
package/dist/agent/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentConfig } from "../types/index.js";
|
|
1
|
+
import { AgentConfig, AgentResult } from "../types/index.js";
|
|
2
2
|
/**
|
|
3
3
|
* StructuredAgent is a class that implements a structured agent for generating and reviewing JSON outputs.
|
|
4
4
|
* It uses a language model to generate initial responses and, optionally, a reviewer model to validate and improve those responses.
|
|
@@ -22,9 +22,23 @@ export declare class StructuredAgent {
|
|
|
22
22
|
* Runs the agent with the given input JSON.
|
|
23
23
|
*
|
|
24
24
|
* @param inputJson - The input JSON to process.
|
|
25
|
-
* @
|
|
25
|
+
* @param ref - An optional reference identifier for the run.
|
|
26
|
+
*
|
|
27
|
+
* @template TOutput - The type of the output. Defaults to unknown.
|
|
28
|
+
*
|
|
29
|
+
* @returns A promise that resolves to the AgentRunResult object containing the structured output and execution metadata.
|
|
30
|
+
* {
|
|
31
|
+
* output: TOutput,
|
|
32
|
+
* metadata: MetadataAgentResult[],
|
|
33
|
+
* ref?: string | number,
|
|
34
|
+
* }
|
|
35
|
+
* @example
|
|
36
|
+
* const result = await agent.run({ input: "some data" }, "12345");
|
|
37
|
+
* console.log(result.output); // Typed output matching outputSchema
|
|
38
|
+
* console.log(result.metadata); // Execution steps and token usage
|
|
39
|
+
* console.log(result.ref); // Optional reference ID if provided
|
|
26
40
|
*/
|
|
27
|
-
run(inputJson: unknown): Promise<
|
|
41
|
+
run<TOutput = unknown>(inputJson: unknown, ref?: string | number): Promise<AgentResult<TOutput>>;
|
|
28
42
|
private generateInitialResponse;
|
|
29
43
|
private reviewResponse;
|
|
30
44
|
private validateOutput;
|
package/dist/agent/index.js
CHANGED
|
@@ -44,9 +44,23 @@ export class StructuredAgent {
|
|
|
44
44
|
* Runs the agent with the given input JSON.
|
|
45
45
|
*
|
|
46
46
|
* @param inputJson - The input JSON to process.
|
|
47
|
-
* @
|
|
47
|
+
* @param ref - An optional reference identifier for the run.
|
|
48
|
+
*
|
|
49
|
+
* @template TOutput - The type of the output. Defaults to unknown.
|
|
50
|
+
*
|
|
51
|
+
* @returns A promise that resolves to the AgentRunResult object containing the structured output and execution metadata.
|
|
52
|
+
* {
|
|
53
|
+
* output: TOutput,
|
|
54
|
+
* metadata: MetadataAgentResult[],
|
|
55
|
+
* ref?: string | number,
|
|
56
|
+
* }
|
|
57
|
+
* @example
|
|
58
|
+
* const result = await agent.run({ input: "some data" }, "12345");
|
|
59
|
+
* console.log(result.output); // Typed output matching outputSchema
|
|
60
|
+
* console.log(result.metadata); // Execution steps and token usage
|
|
61
|
+
* console.log(result.ref); // Optional reference ID if provided
|
|
48
62
|
*/
|
|
49
|
-
async run(inputJson) {
|
|
63
|
+
async run(inputJson, ref) {
|
|
50
64
|
try {
|
|
51
65
|
this.schemaValidator.validate(this.inputValidator, inputJson);
|
|
52
66
|
}
|
|
@@ -58,20 +72,41 @@ export class StructuredAgent {
|
|
|
58
72
|
}
|
|
59
73
|
const maxIterations = this.config.maxIterations ?? 1;
|
|
60
74
|
const history = [];
|
|
61
|
-
|
|
75
|
+
const metadata = [];
|
|
76
|
+
let initalResponse = await this.generateInitialResponse(inputJson);
|
|
77
|
+
let currentJson = this.parseJson(initalResponse.data);
|
|
62
78
|
history.push({ step: "generation", result: currentJson });
|
|
63
79
|
let validationResult = this.validateOutput(currentJson);
|
|
80
|
+
metadata.push({
|
|
81
|
+
...initalResponse.meta,
|
|
82
|
+
step: "generation",
|
|
83
|
+
validation: validationResult,
|
|
84
|
+
});
|
|
64
85
|
if (validationResult.valid) {
|
|
65
|
-
return
|
|
86
|
+
return {
|
|
87
|
+
output: currentJson,
|
|
88
|
+
metadata,
|
|
89
|
+
ref,
|
|
90
|
+
};
|
|
66
91
|
}
|
|
67
92
|
for (let i = 0; i < maxIterations; i++) {
|
|
68
93
|
try {
|
|
69
|
-
|
|
94
|
+
const response = await this.reviewResponse(currentJson, validationResult.errors || [], inputJson // Context might be needed
|
|
70
95
|
);
|
|
96
|
+
currentJson = this.parseJson(response.data);
|
|
71
97
|
history.push({ step: `review-${i + 1}`, result: currentJson });
|
|
72
98
|
validationResult = this.validateOutput(currentJson);
|
|
99
|
+
metadata.push({
|
|
100
|
+
...response.meta,
|
|
101
|
+
step: `review-${i + 1}`,
|
|
102
|
+
validation: validationResult,
|
|
103
|
+
});
|
|
73
104
|
if (validationResult.valid) {
|
|
74
|
-
return
|
|
105
|
+
return {
|
|
106
|
+
output: currentJson,
|
|
107
|
+
metadata,
|
|
108
|
+
ref,
|
|
109
|
+
};
|
|
75
110
|
}
|
|
76
111
|
}
|
|
77
112
|
catch (error) {
|
|
@@ -93,13 +128,16 @@ export class StructuredAgent {
|
|
|
93
128
|
content: JSON.stringify(input),
|
|
94
129
|
},
|
|
95
130
|
];
|
|
96
|
-
const
|
|
131
|
+
const response = await this.generatorService.complete({
|
|
97
132
|
messages,
|
|
98
133
|
model: this.config.generator.model,
|
|
99
134
|
config: this.config.generator.config,
|
|
100
135
|
outputFormat: this.outputValidator
|
|
101
136
|
});
|
|
102
|
-
return
|
|
137
|
+
return {
|
|
138
|
+
data: response.data,
|
|
139
|
+
meta: response.meta,
|
|
140
|
+
};
|
|
103
141
|
}
|
|
104
142
|
async reviewResponse(invalidJson, errors, originalInput) {
|
|
105
143
|
const reviewerService = this.reviewerService || this.generatorService;
|
|
@@ -127,13 +165,16 @@ ${JSON.stringify(zodToJsonSchema(this.config.outputSchema))}
|
|
|
127
165
|
Please correct the JSON.`,
|
|
128
166
|
},
|
|
129
167
|
];
|
|
130
|
-
const
|
|
168
|
+
const response = await reviewerService.complete({
|
|
131
169
|
messages,
|
|
132
170
|
model: reviewerConfig.model,
|
|
133
171
|
config: reviewerConfig.config,
|
|
134
172
|
outputFormat: this.outputValidator
|
|
135
173
|
});
|
|
136
|
-
return
|
|
174
|
+
return {
|
|
175
|
+
data: response.data,
|
|
176
|
+
meta: response.meta,
|
|
177
|
+
};
|
|
137
178
|
}
|
|
138
179
|
validateOutput(data) {
|
|
139
180
|
try {
|
package/dist/agent/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agent/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAe,UAAU,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agent/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAe,UAAU,EAAiC,MAAM,iBAAiB,CAAC;AAMz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AAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,qBAAqB,EAAE,CAAC;gBAC3C,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC,YAAY,CAAC,KAAK,CAAC,MAAe,CAAC,CAAC;gBACjF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,eAAe,CAAC,EAAE,CAAC;YACrD,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,0BAA0B,CAAC,EAAE,CAAC;QAChE,CAAC;IACH,CAAC;IAEO,SAAS,CAAC,IAAY;QAC5B,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAEO,iBAAiB;QACvB,MAAM,UAAU,GAAG,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QAC7D,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY;;;;EAIpC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC;CAC3B,CAAC;IACA,CAAC;CACF"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
2
|
import { ZodSchema } from "zod";
|
|
3
|
-
import { ILLMService, ChatMessage } from "../types.js";
|
|
3
|
+
import { ILLMService, ChatMessage, ResponseComplete } from "../types.js";
|
|
4
4
|
import { ModelConfig } from "../../types/index.js";
|
|
5
5
|
export declare class AnthropicAdapter implements ILLMService {
|
|
6
6
|
private client;
|
|
@@ -10,5 +10,5 @@ export declare class AnthropicAdapter implements ILLMService {
|
|
|
10
10
|
model: string;
|
|
11
11
|
config?: ModelConfig;
|
|
12
12
|
outputFormat?: ZodSchema;
|
|
13
|
-
}): Promise<
|
|
13
|
+
}): Promise<ResponseComplete>;
|
|
14
14
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
import { LLMProvider } from "../../types/index.js";
|
|
2
3
|
import { LLMExecutionError } from "../../errors/index.js";
|
|
3
4
|
export class AnthropicAdapter {
|
|
4
5
|
client;
|
|
@@ -36,7 +37,17 @@ export class AnthropicAdapter {
|
|
|
36
37
|
if (content.type !== "text") {
|
|
37
38
|
throw new LLMExecutionError("Received non-text response from Anthropic");
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
+
const meta = {
|
|
41
|
+
provider: LLMProvider.Anthropic,
|
|
42
|
+
model: params.model,
|
|
43
|
+
config: params.config,
|
|
44
|
+
inputTokens: response.usage.input_tokens,
|
|
45
|
+
outputTokens: response.usage.output_tokens,
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
data: content.text,
|
|
49
|
+
meta,
|
|
50
|
+
};
|
|
40
51
|
}
|
|
41
52
|
catch (error) {
|
|
42
53
|
throw new LLMExecutionError("Failed to execute Anthropic request", error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../src/llm/adapters/anthropic.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,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3C,IAAI,MAAM,CAAC,YAAY,EAAE,CAAC;gBACxB,cAAc,CAAC,aAAa,GAAG;oBAC7B,IAAI,EAAE,aAAa;oBACnB,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,YAAY,CAAC;iBAC7C,CAAC;gBACF,sBAAsB;gBACtB,OAAO,CAAC,gBAAgB,CAAC,GAAG,+BAA+B,CAAC;YAC9D,CAAC;YAED,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAChD,cAAc,EACd,EAAE,OAAO,EAAE,CACZ,CAAC;YAEF,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YACpC,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC3B,MAAM,IAAI,iBAAiB,CAAC,2CAA2C,CAAC,CAAC;YAC5E,CAAC;YAED,MAAM,IAAI,GAAG;gBACX,QAAQ,EAAE,WAAW,CAAC,SAAS;gBAC/B,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,WAAW,EAAE,QAAQ,CAAC,KAAK,CAAC,YAAY;gBACxC,YAAY,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa;aAC3C,CAAC;YAEF,OAAO;gBACL,IAAI,EAAE,OAAO,CAAC,IAAI;gBAClB,IAAI;aACL,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,iBAAiB,CAAC,qCAAqC,EAAE,KAAK,CAAC,CAAC;QAC5E,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { ZodSchema } from "zod";
|
|
3
|
+
import { ILLMService, ChatMessage, ResponseComplete } from "../types.js";
|
|
4
|
+
import { ModelConfig } from "../../types/index.js";
|
|
5
|
+
export declare class DeepSeekAdapter implements ILLMService {
|
|
6
|
+
private client;
|
|
7
|
+
constructor(client: OpenAI);
|
|
8
|
+
complete(params: {
|
|
9
|
+
messages: ChatMessage[];
|
|
10
|
+
model: string;
|
|
11
|
+
config?: ModelConfig;
|
|
12
|
+
outputFormat?: ZodSchema;
|
|
13
|
+
}): Promise<ResponseComplete>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
import { LLMProvider } from "../../types/index.js";
|
|
3
|
+
import { LLMExecutionError } from "../../errors/index.js";
|
|
4
|
+
export class DeepSeekAdapter {
|
|
5
|
+
client;
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
async complete(params) {
|
|
10
|
+
try {
|
|
11
|
+
let messages = [...params.messages];
|
|
12
|
+
if (params.outputFormat) {
|
|
13
|
+
const jsonSchema = zodToJsonSchema(params.outputFormat, "output");
|
|
14
|
+
const schemaString = JSON.stringify(jsonSchema, null, 2);
|
|
15
|
+
const systemMessageIndex = messages.findIndex(m => m.role === "system");
|
|
16
|
+
const schemaInstruction = `\n\nYou must output a valid JSON object matching this schema:\n${schemaString}`;
|
|
17
|
+
if (systemMessageIndex !== -1) {
|
|
18
|
+
messages[systemMessageIndex] = {
|
|
19
|
+
...messages[systemMessageIndex],
|
|
20
|
+
content: messages[systemMessageIndex].content + schemaInstruction
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
messages.unshift({
|
|
25
|
+
role: "system",
|
|
26
|
+
content: schemaInstruction
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const createParams = {
|
|
31
|
+
messages: messages,
|
|
32
|
+
model: params.model,
|
|
33
|
+
response_format: { type: "json_object" },
|
|
34
|
+
temperature: params.config?.temperature ?? 1,
|
|
35
|
+
top_p: params.config?.top_p,
|
|
36
|
+
max_tokens: params.config?.max_tokens,
|
|
37
|
+
presence_penalty: params.config?.presence_penalty,
|
|
38
|
+
frequency_penalty: params.config?.frequency_penalty,
|
|
39
|
+
};
|
|
40
|
+
const response = await this.client.chat.completions.create(createParams);
|
|
41
|
+
const message = response.choices[0]?.message;
|
|
42
|
+
const content = message?.content;
|
|
43
|
+
if (message?.refusal) {
|
|
44
|
+
throw new LLMExecutionError(`Model refused to generate response: ${message.refusal}`);
|
|
45
|
+
}
|
|
46
|
+
if (!content) {
|
|
47
|
+
throw new LLMExecutionError("Received empty response from DeepSeek");
|
|
48
|
+
}
|
|
49
|
+
const meta = {
|
|
50
|
+
provider: LLMProvider.Deepseek,
|
|
51
|
+
model: params.model,
|
|
52
|
+
config: params.config,
|
|
53
|
+
inputTokens: response.usage?.prompt_tokens,
|
|
54
|
+
outputTokens: response.usage?.completion_tokens,
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
data: content,
|
|
58
|
+
meta,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error instanceof LLMExecutionError) {
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
throw new LLMExecutionError("Failed to execute DeepSeek request", error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=deepseek.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepseek.js","sourceRoot":"","sources":["../../../src/llm/adapters/deepseek.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,EAAE,WAAW,EAAe,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAG1D,MAAM,OAAO,eAAe;IAClB,MAAM,CAAS;IAEvB,YAAY,MAAc;QACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,QAAQ,CAAC,MAKrB;QACC,IA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AC,MAAM,EAAE,iBAAiB;aACpD,CAAA;YAED,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;YAEzE,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC;YAC7C,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,CAAC;YAEjC,IAAI,OAAO,EAAE,OAAO,EAAE,CAAC;gBACrB,MAAM,IAAI,iBAAiB,CAAC,uCAAuC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;YACxF,CAAC;YAED,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,MAAM,IAAI,iBAAiB,CAAC,uCAAuC,CAAC,CAAC;YACvE,CAAC;YAED,MAAM,IAAI,GAAG;gBACX,QAAQ,EAAE,WAAW,CAAC,QAAQ;gBAC9B,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,WAAW,EAAE,QAAQ,CAAC,KAAK,EAAE,aAAa;gBAC1C,YAAY,EAAE,QAAQ,CAAC,KAAK,EAAE,iBAAiB;aAChD,CAAC;YAEF,OAAO;gBACL,IAAI,EAAE,OAAO;gBACb,IAAI;aACL,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,iBAAiB,EAAE,CAAC;gBACvC,MAAM,KAAK,CAAC;YACd,CAAC;YACD,MAAM,IAAI,iBAAiB,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;QAC3E,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { GoogleGenAI } from "@google/genai";
|
|
2
2
|
import { ZodSchema } from "zod";
|
|
3
|
-
import { ILLMService, ChatMessage } from "../types.js";
|
|
3
|
+
import { ILLMService, ChatMessage, ResponseComplete } from "../types.js";
|
|
4
4
|
import { ModelConfig } from "../../types/index.js";
|
|
5
5
|
export declare class GoogleGenAIAdapter implements ILLMService {
|
|
6
6
|
private client;
|
|
@@ -10,5 +10,5 @@ export declare class GoogleGenAIAdapter implements ILLMService {
|
|
|
10
10
|
model: string;
|
|
11
11
|
config?: ModelConfig;
|
|
12
12
|
outputFormat?: ZodSchema;
|
|
13
|
-
}): Promise<
|
|
13
|
+
}): Promise<ResponseComplete>;
|
|
14
14
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LLMProvider } from "../../types/index.js";
|
|
1
2
|
import { LLMExecutionError } from "../../errors/index.js";
|
|
2
3
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
4
|
export class GoogleGenAIAdapter {
|
|
@@ -34,15 +35,29 @@ export class GoogleGenAIAdapter {
|
|
|
34
35
|
}
|
|
35
36
|
});
|
|
36
37
|
const text = result.text || result.candidates?.[0]?.content?.parts?.[0]?.text;
|
|
38
|
+
const meta = {
|
|
39
|
+
provider: LLMProvider.GoogleGenAI,
|
|
40
|
+
model: params.model,
|
|
41
|
+
config: params.config,
|
|
42
|
+
inputTokens: result.usageMetadata?.promptTokenCount,
|
|
43
|
+
outputTokens: result.usageMetadata?.candidatesTokenCount,
|
|
44
|
+
};
|
|
37
45
|
if (!text) {
|
|
38
46
|
if (result.candidates && result.candidates.length > 0) {
|
|
39
47
|
const part = result.candidates[0].content?.parts?.[0];
|
|
40
|
-
if (part && 'text' in part)
|
|
41
|
-
return
|
|
48
|
+
if (part && 'text' in part) {
|
|
49
|
+
return {
|
|
50
|
+
data: part.text,
|
|
51
|
+
meta,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
42
54
|
}
|
|
43
55
|
throw new LLMExecutionError("Received empty response from Google GenAI");
|
|
44
56
|
}
|
|
45
|
-
return
|
|
57
|
+
return {
|
|
58
|
+
data: text,
|
|
59
|
+
meta,
|
|
60
|
+
};
|
|
46
61
|
}
|
|
47
62
|
catch (error) {
|
|
48
63
|
throw new LLMExecutionError("Failed to execute Google GenAI request", error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../../src/llm/adapters/google.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../../src/llm/adapters/google.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAe,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,MAAM,OAAO,kBAAkB;IACrB,MAAM,CAAc;IAE5B,YAAY,MAAmB;QAC7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,QAAQ,CAAC,MAKrB;QACC,IAAI,CAAC;YACH,wCAAwC;YACxC,MAAM,aAAa,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;YAEvE,4CAA4C;YAC5C,MAAM,OAAO,GAAG,MAAM,CAAC,QAAQ;iBAC5B,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC;iBAClC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gBACX,IAAI,EAAE,CAAC,CAAC,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM;gBAC/C,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;aAC7B,CAAC,CAAC,CAAC;YAEN,6CAA6C;YAC7C,2CAA2C;YAE3C,MAAM,gBAAgB,GAAG;gBACvB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW;gBACvC,IAAI,EAAE,MAAM,CAAC,MAAM,E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ACtD,IAAI,IAAI,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;wBAC3B,OAAO;4BACL,IAAI,EAAE,IAAI,CAAC,IAAc;4BACzB,IAAI;yBACL,CAAA;oBACH,CAAC;gBACJ,CAAC;gBACD,MAAM,IAAI,iBAAiB,CAAC,2CAA2C,CAAC,CAAC;YAC5E,CAAC;YAED,OAAO;gBACL,IAAI,EAAE,IAAI;gBACV,IAAI;aACL,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,iBAAiB,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;QAC/E,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import { ZodSchema } from "zod";
|
|
3
|
-
import { ILLMService, ChatMessage } from "../types.js";
|
|
3
|
+
import { ILLMService, ChatMessage, ResponseComplete } from "../types.js";
|
|
4
4
|
import { ModelConfig } from "../../types/index.js";
|
|
5
5
|
export declare class OpenAIAdapter implements ILLMService {
|
|
6
6
|
private client;
|
|
@@ -10,5 +10,5 @@ export declare class OpenAIAdapter implements ILLMService {
|
|
|
10
10
|
model: string;
|
|
11
11
|
config?: ModelConfig;
|
|
12
12
|
outputFormat?: ZodSchema;
|
|
13
|
-
}): Promise<
|
|
13
|
+
}): Promise<ResponseComplete>;
|
|
14
14
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { zodResponseFormat } from "openai/helpers/zod";
|
|
2
|
+
import { LLMProvider } from "../../types/index.js";
|
|
2
3
|
import { LLMExecutionError } from "../../errors/index.js";
|
|
3
4
|
export class OpenAIAdapter {
|
|
4
5
|
client;
|
|
@@ -29,7 +30,17 @@ export class OpenAIAdapter {
|
|
|
29
30
|
if (!content) {
|
|
30
31
|
throw new LLMExecutionError("Received empty response from LLM");
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
+
const meta = {
|
|
34
|
+
provider: LLMProvider.OpenAI,
|
|
35
|
+
model: params.model,
|
|
36
|
+
config: params.config,
|
|
37
|
+
inputTokens: response.usage?.prompt_tokens,
|
|
38
|
+
outputTokens: response.usage?.completion_tokens,
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
data: content,
|
|
42
|
+
meta,
|
|
43
|
+
};
|
|
33
44
|
}
|
|
34
45
|
catch (error) {
|
|
35
46
|
if (error instanceof LLMExecutionError) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../../src/llm/adapters/openai.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../../src/llm/adapters/openai.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAGvD,OAAO,EAAE,WAAW,EAAe,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAG1D,MAAM,OAAO,aAAa;IAChB,MAAM,CAAS;IAEvB,YAAY,MAAc;QACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,QAAQ,CAAC,MAKrB;QACC,IAAI,CAAC;YACH,MAAM,YAAY,GAA+B;gBAC/C,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,eAAe,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE;gBACxC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,IAAI,CAAC;gBAC5C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK;gBAC3B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU;gBACrC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB;gBACjD,iBAAiB,EAAE,MAAM,C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"@anthropic-ai/sdk";
|
|
|
4
4
|
import { OpenAIAdapter } from "./adapters/openai.js";
|
|
5
5
|
import { GoogleGenAIAdapter } from "./adapters/google.js";
|
|
6
6
|
import { AnthropicAdapter } from "./adapters/anthropic.js";
|
|
7
|
+
import { DeepSeekAdapter } from "./adapters/deepseek.js";
|
|
7
8
|
export class LLMFactory {
|
|
8
9
|
static create(instance) {
|
|
9
10
|
if (this.isILLMService(instance)) {
|
|
10
11
|
return instance;
|
|
11
12
|
}
|
|
12
13
|
if (instance instanceof OpenAI) {
|
|
14
|
+
if (instance.baseURL.includes("deepseek")) {
|
|
15
|
+
return new DeepSeekAdapter(instance);
|
|
16
|
+
}
|
|
13
17
|
return new OpenAIAdapter(instance);
|
|
14
18
|
}
|
|
15
19
|
if (instance instanceof Anthropic) {
|
|
@@ -18,8 +22,11 @@ export class LLMFactory {
|
|
|
18
22
|
if (instance instanceof GoogleGenAI) {
|
|
19
23
|
return new GoogleGenAIAdapter(instance);
|
|
20
24
|
}
|
|
21
|
-
// Fallback check based on properties if instanceof fails
|
|
25
|
+
// Fallback check based on properties if instanceof fails
|
|
22
26
|
if (this.isOpenAI(instance)) {
|
|
27
|
+
if (instance.baseURL?.includes("deepseek")) {
|
|
28
|
+
return new DeepSeekAdapter(instance);
|
|
29
|
+
}
|
|
23
30
|
return new OpenAIAdapter(instance);
|
|
24
31
|
}
|
|
25
32
|
if (this.isAnthropic(instance)) {
|
package/dist/llm/factory.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../src/llm/factory.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB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AE,CAAC;gBACpD,OAAO,IAAI,eAAe,CAAC,QAAkB,CAAC,CAAC;YACjD,CAAC;YACD,OAAO,IAAI,aAAa,CAAC,QAAkB,CAAC,CAAC;QAChD,CAAC;QAED,IAAI,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC/B,OAAO,IAAI,gBAAgB,CAAC,QAAqB,CAAC,CAAC;QACrD,CAAC;QAED,IAAI,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;YACjC,OAAO,IAAI,kBAAkB,CAAC,QAAuB,CAAC,CAAC;QACzD,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;IACzD,CAAC;IAEO,MAAM,CAAC,aAAa,CAAC,QAAa;QACxC,OAAO,OAAO,QAAQ,CAAC,QAAQ,KAAK,UAAU,CAAC;IACjD,CAAC;IAEO,MAAM,CAAC,QAAQ,CAAC,QAAa;QACnC,OAAO,QAAQ,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,KAAK,SAAS,CAAC;IAC3D,CAAC;IAEO,MAAM,CAAC,WAAW,CAAC,QAAa;QACtC,OAAO,QAAQ,EAAE,QAAQ,EAAE,MAAM,KAAK,SAAS,CAAC;IAClD,CAAC;IAEO,MAAM,CAAC,aAAa,CAAC,QAAa;QACxC,gBAAgB;QAChB,OAAO,QAAQ,EAAE,MAAM,EAAE,eAAe,KAAK,SAAS;YAC/C,6BAA6B;YAC7B,QAAQ,EAAE,kBAAkB,KAAK,SAAS,CAAC;IACpD,CAAC;CACF"}
|
package/dist/llm/types.d.ts
CHANGED
|
@@ -4,11 +4,22 @@ export interface ChatMessage {
|
|
|
4
4
|
role: "system" | "user" | "assistant";
|
|
5
5
|
content: string;
|
|
6
6
|
}
|
|
7
|
+
export interface MetaResponseComplete {
|
|
8
|
+
provider: string;
|
|
9
|
+
model: string;
|
|
10
|
+
config?: ModelConfig;
|
|
11
|
+
inputTokens?: number;
|
|
12
|
+
outputTokens?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface ResponseComplete {
|
|
15
|
+
data: string;
|
|
16
|
+
meta: MetaResponseComplete;
|
|
17
|
+
}
|
|
7
18
|
export interface ILLMService {
|
|
8
19
|
complete(params: {
|
|
9
20
|
messages: ChatMessage[];
|
|
10
21
|
model: string;
|
|
11
22
|
config?: ModelConfig;
|
|
12
23
|
outputFormat?: ZodSchema;
|
|
13
|
-
}): Promise<
|
|
24
|
+
}): Promise<ResponseComplete>;
|
|
14
25
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
|
-
import { ILLMService } from "../llm/types.js";
|
|
2
|
+
import { ILLMService, MetaResponseComplete } from "../llm/types.js";
|
|
3
3
|
import { GoogleGenAI } from "@google/genai";
|
|
4
4
|
import { ZodSchema } from "zod";
|
|
5
5
|
import Anthropic from "@anthropic-ai/sdk";
|
|
@@ -23,10 +23,22 @@ export interface AgentConfig {
|
|
|
23
23
|
systemPrompt: string;
|
|
24
24
|
maxIterations?: number;
|
|
25
25
|
}
|
|
26
|
-
export interface AgentRunOptions {
|
|
27
|
-
input: unknown;
|
|
28
|
-
}
|
|
29
26
|
export interface ValidationResult {
|
|
30
27
|
valid: boolean;
|
|
31
28
|
errors?: string[];
|
|
32
29
|
}
|
|
30
|
+
export interface MetadataAgentResult extends MetaResponseComplete {
|
|
31
|
+
step: string;
|
|
32
|
+
validation: ValidationResult;
|
|
33
|
+
}
|
|
34
|
+
export interface AgentResult<T = unknown> {
|
|
35
|
+
output: T;
|
|
36
|
+
metadata: MetadataAgentResult[];
|
|
37
|
+
ref?: string | number;
|
|
38
|
+
}
|
|
39
|
+
export declare enum LLMProvider {
|
|
40
|
+
OpenAI = "openai",
|
|
41
|
+
GoogleGenAI = "google-genai",
|
|
42
|
+
Anthropic = "anthropic",
|
|
43
|
+
Deepseek = "deepseek"
|
|
44
|
+
}
|
package/dist/types/index.js
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
export
|
|
1
|
+
export var LLMProvider;
|
|
2
|
+
(function (LLMProvider) {
|
|
3
|
+
LLMProvider["OpenAI"] = "openai";
|
|
4
|
+
LLMProvider["GoogleGenAI"] = "google-genai";
|
|
5
|
+
LLMProvider["Anthropic"] = "anthropic";
|
|
6
|
+
LLMProvider["Deepseek"] = "deepseek";
|
|
7
|
+
})(LLMProvider || (LLMProvider = {}));
|
|
2
8
|
//# sourceMappingURL=index.js.map
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AA6CA,MAAM,CAAN,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,gCAAiB,CAAA;IACjB,2CAA4B,CAAA;IAC5B,sCAAuB,CAAA;IACvB,oCAAqB,CAAA;AACvB,CAAC,EALW,WAAW,KAAX,WAAW,QAKtB"}
|
package/package.json
CHANGED