structured-json-agent 1.0.1 → 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 +102 -56
- package/dist/agent/index.d.ts +35 -3
- package/dist/agent/index.js +91 -31
- 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 +14 -0
- package/dist/llm/adapters/anthropic.js +57 -0
- package/dist/llm/adapters/anthropic.js.map +1 -0
- 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 +14 -0
- package/dist/llm/adapters/google.js +67 -0
- package/dist/llm/adapters/google.js.map +1 -0
- package/dist/llm/adapters/openai.d.ts +14 -0
- package/dist/llm/adapters/openai.js +53 -0
- package/dist/llm/adapters/openai.js.map +1 -0
- package/dist/llm/factory.d.ts +12 -0
- package/dist/llm/factory.js +56 -0
- package/dist/llm/factory.js.map +1 -0
- package/dist/llm/index.d.ts +4 -1
- package/dist/llm/index.js +4 -1
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/types.d.ts +18 -1
- package/dist/schemas/validator.d.ts +6 -9
- package/dist/schemas/validator.js +13 -20
- package/dist/schemas/validator.js.map +1 -1
- package/dist/types/index.d.ts +29 -11
- package/dist/types/index.js +7 -1
- package/dist/types/index.js.map +1 -1
- package/package.json +8 -4
- package/dist/llm/openai.d.ts +0 -7
- package/dist/llm/openai.js +0 -33
- package/dist/llm/openai.js.map +0 -1
package/README.md
CHANGED
|
@@ -2,73 +2,93 @@
|
|
|
2
2
|
|
|
3
3
|
A typed and extensible TypeScript library for creating and running Iterative AI Agents that guarantee structured JSON output.
|
|
4
4
|
|
|
5
|
-
This library orchestrates a **Generator ↔ Reviewer** cycle to ensure that the output from Large Language Models (LLMs) strictly adheres to a defined
|
|
5
|
+
This library orchestrates a **Generator ↔ Reviewer** cycle to ensure that the output from Large Language Models (LLMs) strictly adheres to a defined Zod Schema.
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
* **Guaranteed JSON Output**: Enforces strict adherence to
|
|
9
|
+
* **Guaranteed JSON Output**: Enforces strict adherence to Zod Schemas.
|
|
10
|
+
* **Multi-Provider Support**: Built-in adapters for **OpenAI**, **Google GenAI (Gemini)**, **Anthropic (Claude)**, and **DeepSeek**.
|
|
11
|
+
* **Structured Outputs**: Leverages native structured output capabilities of providers (e.g., OpenAI Structured Outputs, Anthropic Beta) when available.
|
|
10
12
|
* **Iterative Self-Correction**: Automatically detects validation errors and feeds them back to a "Reviewer" model to fix the output.
|
|
11
|
-
* **Type-Safe**: Built with TypeScript for full type inference and safety.
|
|
12
|
-
* **
|
|
13
|
-
* **Production Ready**: Includes typed errors, extensive validation, and a clean API.
|
|
13
|
+
* **Type-Safe**: Built with TypeScript and Zod for full type inference and safety.
|
|
14
|
+
* **Flexible Configuration**: Mix and match different providers for generation and review (e.g., generate with GPT-4o, review with Claude 3.5 Sonnet).
|
|
14
15
|
|
|
15
16
|
## Installation
|
|
16
17
|
|
|
17
18
|
```bash
|
|
18
|
-
npm install structured-json-agent
|
|
19
|
+
npm install structured-json-agent zod openai @anthropic-ai/sdk @google/genai
|
|
19
20
|
```
|
|
20
21
|
|
|
22
|
+
> Note: Install the SDKs for the providers you intend to use.
|
|
23
|
+
|
|
21
24
|
## Usage
|
|
22
25
|
|
|
23
26
|
### 1. Import and Configure
|
|
24
27
|
|
|
25
28
|
```typescript
|
|
26
29
|
import { StructuredAgent } from "structured-json-agent";
|
|
30
|
+
import { z } from "zod";
|
|
31
|
+
import OpenAI from "openai";
|
|
32
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
33
|
+
|
|
34
|
+
// 1. Define your Schemas using Zod
|
|
35
|
+
const inputSchema = z.object({
|
|
36
|
+
topic: z.string(),
|
|
37
|
+
depth: z.enum(["basic", "advanced"])
|
|
38
|
+
});
|
|
27
39
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
depth: { type: "string", enum: ["basic", "advanced"] }
|
|
34
|
-
},
|
|
35
|
-
required: ["topic", "depth"]
|
|
36
|
-
};
|
|
40
|
+
const outputSchema = z.object({
|
|
41
|
+
title: z.string(),
|
|
42
|
+
keyPoints: z.array(z.string()),
|
|
43
|
+
summary: z.string()
|
|
44
|
+
});
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
};
|
|
46
|
+
// 2. Initialize Provider Instances
|
|
47
|
+
const openAiInstance = new OpenAI({
|
|
48
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const anthropicInstance = new Anthropic({
|
|
52
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
53
|
+
});
|
|
47
54
|
|
|
48
|
-
// Initialize the Agent
|
|
55
|
+
// 3. Initialize the Agent, you can use the same instance for generator and reviewer
|
|
49
56
|
const agent = new StructuredAgent({
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
// Generator Configuration
|
|
58
|
+
generator: {
|
|
59
|
+
llmService: openAiInstance, // Inject the instance directly
|
|
60
|
+
model: "gpt-5-nano", // Specify the model
|
|
61
|
+
},
|
|
62
|
+
// Reviewer Configuration (Optional but recommended)
|
|
63
|
+
reviewer: {
|
|
64
|
+
llmService: anthropicInstance,
|
|
65
|
+
model: "claude-sonnet-4-5",
|
|
66
|
+
},
|
|
67
|
+
// Schemas & Prompt
|
|
53
68
|
inputSchema,
|
|
54
69
|
outputSchema,
|
|
55
70
|
systemPrompt: "You are an expert summarizer. Create a structured summary based on the topic.",
|
|
56
|
-
maxIterations: 3 // Optional: Max correction attempts (default: 5)
|
|
57
71
|
});
|
|
58
72
|
```
|
|
59
73
|
|
|
60
74
|
### 2. Run the Agent
|
|
61
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
|
+
|
|
62
78
|
```typescript
|
|
63
79
|
async function main() {
|
|
64
80
|
try {
|
|
65
81
|
const result = await agent.run({
|
|
66
82
|
topic: "Clean Architecture",
|
|
67
83
|
depth: "advanced"
|
|
68
|
-
});
|
|
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);
|
|
69
90
|
|
|
70
|
-
|
|
71
|
-
// Output is guaranteed to match outputSchema
|
|
91
|
+
// Result is typed as inferred from outputSchema
|
|
72
92
|
} catch (error) {
|
|
73
93
|
console.error("Agent failed:", error);
|
|
74
94
|
}
|
|
@@ -77,49 +97,75 @@ async function main() {
|
|
|
77
97
|
main();
|
|
78
98
|
```
|
|
79
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
|
+
|
|
80
129
|
## How It Works
|
|
81
130
|
|
|
82
|
-
1. **Validation**: The input JSON is validated against the `inputSchema
|
|
83
|
-
2. **Generation**: The `
|
|
131
|
+
1. **Validation**: The input JSON is validated against the `inputSchema` (Zod).
|
|
132
|
+
2. **Generation**: The `generator` model creates an initial response based on the system prompt and input.
|
|
133
|
+
* If the provider supports native Structured Outputs (like OpenAI or Anthropic), it is used to maximize reliability.
|
|
84
134
|
3. **Verification Loop**:
|
|
85
135
|
* The response is parsed and validated against `outputSchema`.
|
|
86
136
|
* **If Valid**: The result is returned immediately.
|
|
87
|
-
* **If Invalid**: The `
|
|
137
|
+
* **If Invalid**: The `reviewer` model (or generator if no reviewer is set) is invoked with the invalid JSON and specific validation errors. It attempts to fix the output.
|
|
88
138
|
4. **Convergence**: This cycle repeats until a valid JSON is produced or `maxIterations` is reached.
|
|
89
139
|
|
|
90
140
|
## API Reference
|
|
91
141
|
|
|
92
|
-
### `
|
|
142
|
+
### `AgentConfig`
|
|
143
|
+
|
|
144
|
+
Configuration object passed to `new StructuredAgent(config)`.
|
|
93
145
|
|
|
94
146
|
| Property | Type | Description |
|
|
95
|
-
|
|
|
96
|
-
| `
|
|
97
|
-
| `
|
|
98
|
-
| `
|
|
99
|
-
| `
|
|
100
|
-
| `outputSchema` | `object` | JSON Schema for the expected output. |
|
|
147
|
+
| :--- | :--- | :--- |
|
|
148
|
+
| `generator` | `LLMConfig` | Configuration for the generation model. |
|
|
149
|
+
| `reviewer` | `LLMConfig?` | Configuration for the reviewer model (optional). |
|
|
150
|
+
| `inputSchema` | `ZodSchema` | Zod Schema for validating the input. |
|
|
151
|
+
| `outputSchema` | `ZodSchema` | Zod Schema for the expected output. |
|
|
101
152
|
| `systemPrompt` | `string` | Core instructions for the agent. |
|
|
102
153
|
| `maxIterations` | `number?` | Max retries for correction. Default: 5. |
|
|
103
|
-
| `modelConfig` | `ModelConfig?` | Optional parameters (temperature, etc.). |
|
|
104
|
-
| `llmService` | `ILLMService?` | Optional custom LLM service implementation. |
|
|
105
154
|
|
|
106
|
-
###
|
|
155
|
+
### `LLMConfig`
|
|
107
156
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
* `MaxIterationsExceededError`: The agent could not produce valid JSON within the limit.
|
|
114
|
-
* `LLMExecutionError`: Failure in communicating with the LLM provider.
|
|
157
|
+
| Property | Type | Description |
|
|
158
|
+
| :--- | :--- | :--- |
|
|
159
|
+
| `llmService` | `OpenAI \| GoogleGenAI \| Anthropic \| ILLMService` | The provider instance or custom service. Supports DeepSeek via OpenAI SDK. |
|
|
160
|
+
| `model` | `string` | Model ID (e.g., `gpt-4o`, `claude-3-5-sonnet`). |
|
|
161
|
+
| `config` | `ModelConfig?` | Optional parameters (temperature, max_tokens, etc.). |
|
|
115
162
|
|
|
116
163
|
## Architecture
|
|
117
164
|
|
|
118
165
|
The project is structured by domain:
|
|
119
166
|
|
|
120
|
-
* `src/agent`: Core orchestration logic.
|
|
121
|
-
* `src/schemas`: Validation logic using
|
|
122
|
-
* `src/llm`:
|
|
167
|
+
* `src/agent`: Core orchestration logic (`StructuredAgent`).
|
|
168
|
+
* `src/schemas`: Validation logic using **Zod**.
|
|
169
|
+
* `src/llm`: Adapters and Factory for LLM providers (`OpenAI`, `Google`, `Anthropic`).
|
|
123
170
|
* `src/errors`: Custom error definitions.
|
|
124
171
|
* `src/types`: Shared interfaces.
|
|
125
|
-
|
package/dist/agent/index.d.ts
CHANGED
|
@@ -1,12 +1,44 @@
|
|
|
1
|
-
import { AgentConfig } from "../types/index.js";
|
|
1
|
+
import { AgentConfig, AgentResult } from "../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* StructuredAgent is a class that implements a structured agent for generating and reviewing JSON outputs.
|
|
4
|
+
* It uses a language model to generate initial responses and, optionally, a reviewer model to validate and improve those responses.
|
|
5
|
+
*
|
|
6
|
+
* @param config - The configuration object for the agent.
|
|
7
|
+
*/
|
|
2
8
|
export declare class StructuredAgent {
|
|
3
9
|
private schemaValidator;
|
|
4
|
-
private llmService;
|
|
5
10
|
private inputValidator;
|
|
6
11
|
private outputValidator;
|
|
7
12
|
private config;
|
|
13
|
+
private generatorService;
|
|
14
|
+
private reviewerService?;
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of StructuredAgent.
|
|
17
|
+
*
|
|
18
|
+
* @param config - The configuration object for the agent.
|
|
19
|
+
*/
|
|
8
20
|
constructor(config: AgentConfig);
|
|
9
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Runs the agent with the given input JSON.
|
|
23
|
+
*
|
|
24
|
+
* @param inputJson - The input JSON to process.
|
|
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
|
|
40
|
+
*/
|
|
41
|
+
run<TOutput = unknown>(inputJson: unknown, ref?: string | number): Promise<AgentResult<TOutput>>;
|
|
10
42
|
private generateInitialResponse;
|
|
11
43
|
private reviewResponse;
|
|
12
44
|
private validateOutput;
|
package/dist/agent/index.js
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
1
2
|
import { SchemaValidator } from "../schemas/validator.js";
|
|
2
|
-
import {
|
|
3
|
+
import { LLMFactory } from "../llm/index.js";
|
|
3
4
|
import { InvalidInputSchemaError, InvalidOutputSchemaError, SchemaValidationError, MaxIterationsExceededError, LLMExecutionError, } from "../errors/index.js";
|
|
5
|
+
/**
|
|
6
|
+
* StructuredAgent is a class that implements a structured agent for generating and reviewing JSON outputs.
|
|
7
|
+
* It uses a language model to generate initial responses and, optionally, a reviewer model to validate and improve those responses.
|
|
8
|
+
*
|
|
9
|
+
* @param config - The configuration object for the agent.
|
|
10
|
+
*/
|
|
4
11
|
export class StructuredAgent {
|
|
5
12
|
schemaValidator;
|
|
6
|
-
llmService;
|
|
7
13
|
inputValidator;
|
|
8
14
|
outputValidator;
|
|
9
15
|
config;
|
|
16
|
+
generatorService;
|
|
17
|
+
reviewerService;
|
|
18
|
+
/**
|
|
19
|
+
* Creates an instance of StructuredAgent.
|
|
20
|
+
*
|
|
21
|
+
* @param config - The configuration object for the agent.
|
|
22
|
+
*/
|
|
10
23
|
constructor(config) {
|
|
11
24
|
this.config = config;
|
|
12
25
|
this.schemaValidator = new SchemaValidator();
|
|
13
|
-
// Use provided LLM service or default to OpenAI
|
|
14
|
-
this.llmService = config.llmService || new OpenAILLMService(config.openAiApiKey);
|
|
15
26
|
try {
|
|
16
27
|
this.inputValidator = this.schemaValidator.compile(config.inputSchema);
|
|
17
28
|
}
|
|
@@ -24,50 +35,84 @@ export class StructuredAgent {
|
|
|
24
35
|
catch (e) {
|
|
25
36
|
throw new InvalidOutputSchemaError("Failed to compile output schema", e);
|
|
26
37
|
}
|
|
38
|
+
this.generatorService = LLMFactory.create(config.generator.llmService);
|
|
39
|
+
if (config.reviewer) {
|
|
40
|
+
this.reviewerService = LLMFactory.create(config.reviewer.llmService);
|
|
41
|
+
}
|
|
27
42
|
}
|
|
28
|
-
|
|
29
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Runs the agent with the given input JSON.
|
|
45
|
+
*
|
|
46
|
+
* @param inputJson - The input JSON to process.
|
|
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
|
|
62
|
+
*/
|
|
63
|
+
async run(inputJson, ref) {
|
|
30
64
|
try {
|
|
31
65
|
this.schemaValidator.validate(this.inputValidator, inputJson);
|
|
32
66
|
}
|
|
33
67
|
catch (error) {
|
|
34
68
|
if (error instanceof SchemaValidationError) {
|
|
35
|
-
// Enhance error message if needed, but the original is good
|
|
36
69
|
throw error;
|
|
37
70
|
}
|
|
38
71
|
throw error;
|
|
39
72
|
}
|
|
40
|
-
const maxIterations = this.config.maxIterations ??
|
|
73
|
+
const maxIterations = this.config.maxIterations ?? 1;
|
|
41
74
|
const history = [];
|
|
42
|
-
|
|
43
|
-
let
|
|
75
|
+
const metadata = [];
|
|
76
|
+
let initalResponse = await this.generateInitialResponse(inputJson);
|
|
77
|
+
let currentJson = this.parseJson(initalResponse.data);
|
|
44
78
|
history.push({ step: "generation", result: currentJson });
|
|
45
79
|
let validationResult = this.validateOutput(currentJson);
|
|
80
|
+
metadata.push({
|
|
81
|
+
...initalResponse.meta,
|
|
82
|
+
step: "generation",
|
|
83
|
+
validation: validationResult,
|
|
84
|
+
});
|
|
46
85
|
if (validationResult.valid) {
|
|
47
|
-
return
|
|
86
|
+
return {
|
|
87
|
+
output: currentJson,
|
|
88
|
+
metadata,
|
|
89
|
+
ref,
|
|
90
|
+
};
|
|
48
91
|
}
|
|
49
|
-
// 3. Review Loop
|
|
50
92
|
for (let i = 0; i < maxIterations; i++) {
|
|
51
93
|
try {
|
|
52
|
-
|
|
94
|
+
const response = await this.reviewResponse(currentJson, validationResult.errors || [], inputJson // Context might be needed
|
|
53
95
|
);
|
|
96
|
+
currentJson = this.parseJson(response.data);
|
|
54
97
|
history.push({ step: `review-${i + 1}`, result: currentJson });
|
|
55
98
|
validationResult = this.validateOutput(currentJson);
|
|
99
|
+
metadata.push({
|
|
100
|
+
...response.meta,
|
|
101
|
+
step: `review-${i + 1}`,
|
|
102
|
+
validation: validationResult,
|
|
103
|
+
});
|
|
56
104
|
if (validationResult.valid) {
|
|
57
|
-
return
|
|
105
|
+
return {
|
|
106
|
+
output: currentJson,
|
|
107
|
+
metadata,
|
|
108
|
+
ref,
|
|
109
|
+
};
|
|
58
110
|
}
|
|
59
111
|
}
|
|
60
112
|
catch (error) {
|
|
61
|
-
// If LLM fails or parsing fails during review, we record it and continue?
|
|
62
|
-
// Or throw? The requirement says "return exclusively a valid JSON".
|
|
63
|
-
// If we can't continue, we should probably throw or let the loop hit max iterations.
|
|
64
|
-
// For now, let's treat execution errors as fatal or part of the attempt?
|
|
65
|
-
// If it's a parsing error, it's a validation error.
|
|
66
|
-
// If it's an API error, it's an LLMExecutionError.
|
|
67
113
|
if (error instanceof LLMExecutionError) {
|
|
68
114
|
throw error;
|
|
69
115
|
}
|
|
70
|
-
// If JSON parse error in reviewResponse, it might be caught there.
|
|
71
116
|
}
|
|
72
117
|
}
|
|
73
118
|
throw new MaxIterationsExceededError(`Failed to generate valid JSON after ${maxIterations} review iterations`, history);
|
|
@@ -83,10 +128,20 @@ export class StructuredAgent {
|
|
|
83
128
|
content: JSON.stringify(input),
|
|
84
129
|
},
|
|
85
130
|
];
|
|
86
|
-
const
|
|
87
|
-
|
|
131
|
+
const response = await this.generatorService.complete({
|
|
132
|
+
messages,
|
|
133
|
+
model: this.config.generator.model,
|
|
134
|
+
config: this.config.generator.config,
|
|
135
|
+
outputFormat: this.outputValidator
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
data: response.data,
|
|
139
|
+
meta: response.meta,
|
|
140
|
+
};
|
|
88
141
|
}
|
|
89
142
|
async reviewResponse(invalidJson, errors, originalInput) {
|
|
143
|
+
const reviewerService = this.reviewerService || this.generatorService;
|
|
144
|
+
const reviewerConfig = this.config.reviewer || this.config.generator;
|
|
90
145
|
const messages = [
|
|
91
146
|
{
|
|
92
147
|
role: "system",
|
|
@@ -105,13 +160,21 @@ Validation Errors:
|
|
|
105
160
|
${errors.join("\n")}
|
|
106
161
|
|
|
107
162
|
Expected Output Schema:
|
|
108
|
-
${JSON.stringify(this.config.outputSchema)}
|
|
163
|
+
${JSON.stringify(zodToJsonSchema(this.config.outputSchema))}
|
|
109
164
|
|
|
110
165
|
Please correct the JSON.`,
|
|
111
166
|
},
|
|
112
167
|
];
|
|
113
|
-
const
|
|
114
|
-
|
|
168
|
+
const response = await reviewerService.complete({
|
|
169
|
+
messages,
|
|
170
|
+
model: reviewerConfig.model,
|
|
171
|
+
config: reviewerConfig.config,
|
|
172
|
+
outputFormat: this.outputValidator
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
data: response.data,
|
|
176
|
+
meta: response.meta,
|
|
177
|
+
};
|
|
115
178
|
}
|
|
116
179
|
validateOutput(data) {
|
|
117
180
|
try {
|
|
@@ -131,19 +194,16 @@ Please correct the JSON.`,
|
|
|
131
194
|
return JSON.parse(text);
|
|
132
195
|
}
|
|
133
196
|
catch (e) {
|
|
134
|
-
// If parsing fails, we return the text (as string) or null?
|
|
135
|
-
// But the flow expects unknown (object).
|
|
136
|
-
// If we return the text, the schema validation will likely fail (unless schema allows string).
|
|
137
|
-
// This allows the reviewer to see the malformed JSON text.
|
|
138
197
|
return text;
|
|
139
198
|
}
|
|
140
199
|
}
|
|
141
200
|
buildSystemPrompt() {
|
|
201
|
+
const jsonSchema = zodToJsonSchema(this.config.outputSchema);
|
|
142
202
|
return `${this.config.systemPrompt}
|
|
143
203
|
|
|
144
204
|
IMPORTANT: You must output strict JSON only.
|
|
145
205
|
The output must adhere to the following JSON Schema:
|
|
146
|
-
${JSON.stringify(
|
|
206
|
+
${JSON.stringify(jsonSchema)}
|
|
147
207
|
`;
|
|
148
208
|
}
|
|
149
209
|
}
|
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,yBAAyB,CAAC;AAC1D,OAAO,
|
|
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;AAMzF,OAAO,EACL,uBAAuB,EACvB,wBAAwB,EACxB,qBAAqB,EACrB,0BAA0B,EAC1B,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAE5B;;;;;GAKG;AACH,MAAM,OAAO,eAAe;IAClB,eAAe,CAAkB;IACjC,cAAc,CAAY;IAC1B,eAAe,CAAY;IAC3B,MAAM,CAAc;IACpB,gBAAgB,CAAc;IAC9B,eAAe,CAAe;IAEtC;;;;OAIG;IACH,YAAY,MAAmB;QAC7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;QAE7C,IAAI,CAAC;YACH,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC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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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
import { ZodSchema } from "zod";
|
|
3
|
+
import { ILLMService, ChatMessage, ResponseComplete } from "../types.js";
|
|
4
|
+
import { ModelConfig } from "../../types/index.js";
|
|
5
|
+
export declare class AnthropicAdapter implements ILLMService {
|
|
6
|
+
private client;
|
|
7
|
+
constructor(client: Anthropic);
|
|
8
|
+
complete(params: {
|
|
9
|
+
messages: ChatMessage[];
|
|
10
|
+
model: string;
|
|
11
|
+
config?: ModelConfig;
|
|
12
|
+
outputFormat?: ZodSchema;
|
|
13
|
+
}): Promise<ResponseComplete>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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 AnthropicAdapter {
|
|
5
|
+
client;
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
async complete(params) {
|
|
10
|
+
try {
|
|
11
|
+
const systemMessage = params.messages.find((m) => m.role === "system");
|
|
12
|
+
const conversationMessages = params.messages
|
|
13
|
+
.filter((m) => m.role !== "system")
|
|
14
|
+
.map((m) => ({
|
|
15
|
+
role: m.role,
|
|
16
|
+
content: m.content,
|
|
17
|
+
}));
|
|
18
|
+
const requestOptions = {
|
|
19
|
+
model: params.model,
|
|
20
|
+
messages: conversationMessages,
|
|
21
|
+
system: systemMessage?.content,
|
|
22
|
+
max_tokens: params.config?.max_tokens ?? 1024,
|
|
23
|
+
temperature: params.config?.temperature ?? 0.7,
|
|
24
|
+
top_p: params.config?.top_p,
|
|
25
|
+
};
|
|
26
|
+
const headers = {};
|
|
27
|
+
if (params.outputFormat) {
|
|
28
|
+
requestOptions.output_format = {
|
|
29
|
+
type: "json_schema",
|
|
30
|
+
schema: zodToJsonSchema(params.outputFormat)
|
|
31
|
+
};
|
|
32
|
+
// Enable beta feature
|
|
33
|
+
headers["anthropic-beta"] = "structured-outputs-2025-11-13";
|
|
34
|
+
}
|
|
35
|
+
const response = await this.client.messages.create(requestOptions, { headers });
|
|
36
|
+
const content = response.content[0];
|
|
37
|
+
if (content.type !== "text") {
|
|
38
|
+
throw new LLMExecutionError("Received non-text response from Anthropic");
|
|
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
|
+
};
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
throw new LLMExecutionError("Failed to execute Anthropic request", error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=anthropic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../src/llm/adapters/anthropic.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;AAE1D,MAAM,OAAO,gBAAgB;IACnB,MAAM,CAAY;IAE1B,YAAY,MAAiB;QAC3B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,QAAQ,CAAC,MAKrB;QACC,IAAI,CAAC;YACH,MAAM,aAAa,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;YACvE,MAAM,oBAAoB,GAAG,MAAM,CAAC,QAAQ;iBACzC,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,IAA4B;gBACpC,OAAO,EAAE,CAAC,CAAC,OAAO;aACnB,CAAC,CAAC,CAAC;YAEN,MAAM,cAAc,GAAQ;gBAC1B,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,QAAQ,EAAE,oBAAoB;gBAC9B,MAAM,EAAE,aAAa,EAAE,OAAO;gBAC9B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,IAAI,IAAI;gBAC7C,WAAW,EAAE,MAAM,CAA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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;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genai";
|
|
2
|
+
import { ZodSchema } from "zod";
|
|
3
|
+
import { ILLMService, ChatMessage, ResponseComplete } from "../types.js";
|
|
4
|
+
import { ModelConfig } from "../../types/index.js";
|
|
5
|
+
export declare class GoogleGenAIAdapter implements ILLMService {
|
|
6
|
+
private client;
|
|
7
|
+
constructor(client: GoogleGenAI);
|
|
8
|
+
complete(params: {
|
|
9
|
+
messages: ChatMessage[];
|
|
10
|
+
model: string;
|
|
11
|
+
config?: ModelConfig;
|
|
12
|
+
outputFormat?: ZodSchema;
|
|
13
|
+
}): Promise<ResponseComplete>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { LLMProvider } from "../../types/index.js";
|
|
2
|
+
import { LLMExecutionError } from "../../errors/index.js";
|
|
3
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
|
+
export class GoogleGenAIAdapter {
|
|
5
|
+
client;
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
async complete(params) {
|
|
10
|
+
try {
|
|
11
|
+
// Extract system instruction if present
|
|
12
|
+
const systemMessage = params.messages.find((m) => m.role === "system");
|
|
13
|
+
// Filter out system message for the history
|
|
14
|
+
const history = params.messages
|
|
15
|
+
.filter((m) => m.role !== "system")
|
|
16
|
+
.map((m) => ({
|
|
17
|
+
role: m.role === "assistant" ? "model" : "user",
|
|
18
|
+
parts: [{ text: m.content }],
|
|
19
|
+
}));
|
|
20
|
+
// New SDK uses client.models.generateContent
|
|
21
|
+
// System instruction is part of the config
|
|
22
|
+
const generationConfig = {
|
|
23
|
+
temperature: params.config?.temperature,
|
|
24
|
+
topP: params.config?.top_p,
|
|
25
|
+
maxOutputTokens: params.config?.max_tokens,
|
|
26
|
+
responseMimeType: "application/json",
|
|
27
|
+
};
|
|
28
|
+
const result = await this.client.models.generateContent({
|
|
29
|
+
model: params.model,
|
|
30
|
+
contents: history,
|
|
31
|
+
config: {
|
|
32
|
+
...generationConfig,
|
|
33
|
+
responseJsonSchema: params.outputFormat ? zodToJsonSchema(params.outputFormat) : undefined,
|
|
34
|
+
systemInstruction: systemMessage ? { parts: [{ text: systemMessage.content }] } : undefined
|
|
35
|
+
}
|
|
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
|
+
};
|
|
45
|
+
if (!text) {
|
|
46
|
+
if (result.candidates && result.candidates.length > 0) {
|
|
47
|
+
const part = result.candidates[0].content?.parts?.[0];
|
|
48
|
+
if (part && 'text' in part) {
|
|
49
|
+
return {
|
|
50
|
+
data: part.text,
|
|
51
|
+
meta,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
throw new LLMExecutionError("Received empty response from Google GenAI");
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
data: text,
|
|
59
|
+
meta,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
throw new LLMExecutionError("Failed to execute Google GenAI request", error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=google.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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;QAC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,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 OpenAIAdapter 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,53 @@
|
|
|
1
|
+
import { zodResponseFormat } from "openai/helpers/zod";
|
|
2
|
+
import { LLMProvider } from "../../types/index.js";
|
|
3
|
+
import { LLMExecutionError } from "../../errors/index.js";
|
|
4
|
+
export class OpenAIAdapter {
|
|
5
|
+
client;
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
async complete(params) {
|
|
10
|
+
try {
|
|
11
|
+
const createParams = {
|
|
12
|
+
messages: params.messages,
|
|
13
|
+
model: params.model,
|
|
14
|
+
response_format: { type: "json_object" },
|
|
15
|
+
temperature: params.config?.temperature ?? 1,
|
|
16
|
+
top_p: params.config?.top_p,
|
|
17
|
+
max_tokens: params.config?.max_tokens,
|
|
18
|
+
presence_penalty: params.config?.presence_penalty,
|
|
19
|
+
frequency_penalty: params.config?.frequency_penalty,
|
|
20
|
+
};
|
|
21
|
+
if (params.outputFormat) {
|
|
22
|
+
createParams.response_format = zodResponseFormat(params.outputFormat, "response");
|
|
23
|
+
}
|
|
24
|
+
const response = await this.client.chat.completions.create(createParams);
|
|
25
|
+
const message = response.choices[0]?.message;
|
|
26
|
+
const content = message?.content;
|
|
27
|
+
if (message?.refusal) {
|
|
28
|
+
throw new LLMExecutionError(`Model refused to generate response: ${message.refusal}`);
|
|
29
|
+
}
|
|
30
|
+
if (!content) {
|
|
31
|
+
throw new LLMExecutionError("Received empty response from LLM");
|
|
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
|
+
};
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error instanceof LLMExecutionError) {
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
throw new LLMExecutionError("Failed to execute OpenAI request", error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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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M,IAAI,iBAAiB,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAC;QACzE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { GoogleGenAI } from "@google/genai";
|
|
3
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
4
|
+
import { ILLMService } from "./types.js";
|
|
5
|
+
export type LLMInstance = OpenAI | GoogleGenAI | Anthropic | ILLMService;
|
|
6
|
+
export declare class LLMFactory {
|
|
7
|
+
static create(instance: LLMInstance): ILLMService;
|
|
8
|
+
private static isILLMService;
|
|
9
|
+
private static isOpenAI;
|
|
10
|
+
private static isAnthropic;
|
|
11
|
+
private static isGoogleGenAI;
|
|
12
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { GoogleGenAI } from "@google/genai";
|
|
3
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
4
|
+
import { OpenAIAdapter } from "./adapters/openai.js";
|
|
5
|
+
import { GoogleGenAIAdapter } from "./adapters/google.js";
|
|
6
|
+
import { AnthropicAdapter } from "./adapters/anthropic.js";
|
|
7
|
+
import { DeepSeekAdapter } from "./adapters/deepseek.js";
|
|
8
|
+
export class LLMFactory {
|
|
9
|
+
static create(instance) {
|
|
10
|
+
if (this.isILLMService(instance)) {
|
|
11
|
+
return instance;
|
|
12
|
+
}
|
|
13
|
+
if (instance instanceof OpenAI) {
|
|
14
|
+
if (instance.baseURL.includes("deepseek")) {
|
|
15
|
+
return new DeepSeekAdapter(instance);
|
|
16
|
+
}
|
|
17
|
+
return new OpenAIAdapter(instance);
|
|
18
|
+
}
|
|
19
|
+
if (instance instanceof Anthropic) {
|
|
20
|
+
return new AnthropicAdapter(instance);
|
|
21
|
+
}
|
|
22
|
+
if (instance instanceof GoogleGenAI) {
|
|
23
|
+
return new GoogleGenAIAdapter(instance);
|
|
24
|
+
}
|
|
25
|
+
// Fallback check based on properties if instanceof fails
|
|
26
|
+
if (this.isOpenAI(instance)) {
|
|
27
|
+
if (instance.baseURL?.includes("deepseek")) {
|
|
28
|
+
return new DeepSeekAdapter(instance);
|
|
29
|
+
}
|
|
30
|
+
return new OpenAIAdapter(instance);
|
|
31
|
+
}
|
|
32
|
+
if (this.isAnthropic(instance)) {
|
|
33
|
+
return new AnthropicAdapter(instance);
|
|
34
|
+
}
|
|
35
|
+
if (this.isGoogleGenAI(instance)) {
|
|
36
|
+
return new GoogleGenAIAdapter(instance);
|
|
37
|
+
}
|
|
38
|
+
throw new Error("Unknown LLM instance type provided.");
|
|
39
|
+
}
|
|
40
|
+
static isILLMService(instance) {
|
|
41
|
+
return typeof instance.complete === "function";
|
|
42
|
+
}
|
|
43
|
+
static isOpenAI(instance) {
|
|
44
|
+
return instance?.chat?.completions?.create !== undefined;
|
|
45
|
+
}
|
|
46
|
+
static isAnthropic(instance) {
|
|
47
|
+
return instance?.messages?.create !== undefined;
|
|
48
|
+
}
|
|
49
|
+
static isGoogleGenAI(instance) {
|
|
50
|
+
// New SDK check
|
|
51
|
+
return instance?.models?.generateContent !== undefined ||
|
|
52
|
+
// Old SDK check just in case
|
|
53
|
+
instance?.getGenerativeModel !== undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +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,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,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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/index.d.ts
CHANGED
package/dist/llm/index.js
CHANGED
package/dist/llm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC"}
|
package/dist/llm/types.d.ts
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
|
+
import { ZodSchema } from "zod";
|
|
1
2
|
import { ModelConfig } from "../types/index.js";
|
|
2
3
|
export interface ChatMessage {
|
|
3
4
|
role: "system" | "user" | "assistant";
|
|
4
5
|
content: string;
|
|
5
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
|
+
}
|
|
6
18
|
export interface ILLMService {
|
|
7
|
-
complete(
|
|
19
|
+
complete(params: {
|
|
20
|
+
messages: ChatMessage[];
|
|
21
|
+
model: string;
|
|
22
|
+
config?: ModelConfig;
|
|
23
|
+
outputFormat?: ZodSchema;
|
|
24
|
+
}): Promise<ResponseComplete>;
|
|
8
25
|
}
|
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ZodSchema } from "zod";
|
|
2
2
|
export declare class SchemaValidator {
|
|
3
|
-
private ajv;
|
|
4
|
-
constructor();
|
|
5
3
|
/**
|
|
6
4
|
* Compiles a schema and returns a validation function.
|
|
7
|
-
*
|
|
5
|
+
* For Zod, the schema itself is the validator.
|
|
8
6
|
*/
|
|
9
|
-
compile(schema:
|
|
7
|
+
compile(schema: ZodSchema): ZodSchema;
|
|
10
8
|
/**
|
|
11
9
|
* Validates data against a compiled schema validator.
|
|
12
|
-
* Returns true if valid.
|
|
13
10
|
* Throws SchemaValidationError if invalid.
|
|
14
11
|
*/
|
|
15
|
-
validate(validator:
|
|
12
|
+
validate(validator: ZodSchema, data: unknown): void;
|
|
16
13
|
/**
|
|
17
|
-
* Helper to format
|
|
14
|
+
* Helper to format Zod errors into a readable string
|
|
18
15
|
*/
|
|
19
|
-
formatErrors(errors:
|
|
16
|
+
formatErrors(errors: unknown[]): string;
|
|
20
17
|
}
|
|
@@ -1,39 +1,32 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { SchemaValidationError, InvalidInputSchemaError } from "../errors/index.js";
|
|
1
|
+
import { SchemaValidationError } from "../errors/index.js";
|
|
3
2
|
export class SchemaValidator {
|
|
4
|
-
ajv;
|
|
5
|
-
constructor() {
|
|
6
|
-
// @ts-ignore: Ajv import structure compatibility
|
|
7
|
-
this.ajv = new Ajv({ allErrors: true, strict: false });
|
|
8
|
-
}
|
|
9
3
|
/**
|
|
10
4
|
* Compiles a schema and returns a validation function.
|
|
11
|
-
*
|
|
5
|
+
* For Zod, the schema itself is the validator.
|
|
12
6
|
*/
|
|
13
7
|
compile(schema) {
|
|
14
|
-
|
|
15
|
-
return this.ajv.compile(schema);
|
|
16
|
-
}
|
|
17
|
-
catch (error) {
|
|
18
|
-
throw new InvalidInputSchemaError("Invalid JSON Schema provided", error);
|
|
19
|
-
}
|
|
8
|
+
return schema;
|
|
20
9
|
}
|
|
21
10
|
/**
|
|
22
11
|
* Validates data against a compiled schema validator.
|
|
23
|
-
* Returns true if valid.
|
|
24
12
|
* Throws SchemaValidationError if invalid.
|
|
25
13
|
*/
|
|
26
14
|
validate(validator, data) {
|
|
27
|
-
const
|
|
28
|
-
if (!
|
|
29
|
-
throw new SchemaValidationError("Data validation failed",
|
|
15
|
+
const result = validator.safeParse(data);
|
|
16
|
+
if (!result.success) {
|
|
17
|
+
throw new SchemaValidationError("Data validation failed", result.error.errors);
|
|
30
18
|
}
|
|
31
19
|
}
|
|
32
20
|
/**
|
|
33
|
-
* Helper to format
|
|
21
|
+
* Helper to format Zod errors into a readable string
|
|
34
22
|
*/
|
|
35
23
|
formatErrors(errors) {
|
|
36
|
-
return
|
|
24
|
+
return errors
|
|
25
|
+
.map((e) => {
|
|
26
|
+
const path = e.path.join(".");
|
|
27
|
+
return path ? `${path}: ${e.message}` : e.message;
|
|
28
|
+
})
|
|
29
|
+
.join("; ");
|
|
37
30
|
}
|
|
38
31
|
}
|
|
39
32
|
//# sourceMappingURL=validator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/schemas/validator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/schemas/validator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE3D,MAAM,OAAO,eAAe;IAC1B;;;OAGG;IACI,OAAO,CAAC,MAAiB;QAC9B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;OAGG;IACI,QAAQ,CAAC,SAAoB,EAAE,IAAa;QACjD,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,MAAM,IAAI,qBAAqB,CAC7B,wBAAwB,EACxB,MAAM,CAAC,KAAK,CAAC,MAAM,CACpB,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;OAEG;IACI,YAAY,CAAC,MAAiB;QACnC,OAAQ,MAAgB;aACrB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;YACT,MAAM,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC9B,OAAO,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;QACpD,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;CACF"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { ILLMService, MetaResponseComplete } from "../llm/types.js";
|
|
3
|
+
import { GoogleGenAI } from "@google/genai";
|
|
4
|
+
import { ZodSchema } from "zod";
|
|
5
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
2
6
|
export interface ModelConfig {
|
|
3
7
|
temperature?: number;
|
|
4
8
|
top_p?: number;
|
|
@@ -6,21 +10,35 @@ export interface ModelConfig {
|
|
|
6
10
|
presence_penalty?: number;
|
|
7
11
|
frequency_penalty?: number;
|
|
8
12
|
}
|
|
13
|
+
export interface LLMInputInstance {
|
|
14
|
+
llmService: OpenAI | GoogleGenAI | Anthropic | ILLMService;
|
|
15
|
+
model: string;
|
|
16
|
+
config?: ModelConfig;
|
|
17
|
+
}
|
|
9
18
|
export interface AgentConfig {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
outputSchema: object;
|
|
19
|
+
generator: LLMInputInstance;
|
|
20
|
+
reviewer?: LLMInputInstance;
|
|
21
|
+
inputSchema: ZodSchema;
|
|
22
|
+
outputSchema: ZodSchema;
|
|
15
23
|
systemPrompt: string;
|
|
16
|
-
modelConfig?: ModelConfig;
|
|
17
24
|
maxIterations?: number;
|
|
18
|
-
llmService?: ILLMService;
|
|
19
|
-
}
|
|
20
|
-
export interface AgentRunOptions {
|
|
21
|
-
input: unknown;
|
|
22
25
|
}
|
|
23
26
|
export interface ValidationResult {
|
|
24
27
|
valid: boolean;
|
|
25
28
|
errors?: string[];
|
|
26
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "structured-json-agent",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A typed and extensible library for creating and running Iterative AI Agents that generate structured JSON output.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,19 +29,23 @@
|
|
|
29
29
|
"bugs": {
|
|
30
30
|
"url": "https://github.com/thiagoaramizo/structured-json-agent/issues"
|
|
31
31
|
},
|
|
32
|
-
"homepage": "https://github.
|
|
32
|
+
"homepage": "https://thiagoaramizo.github.io/structured-json-agent",
|
|
33
33
|
"author": "",
|
|
34
34
|
"license": "ISC",
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/jest": "^30.0.0",
|
|
37
37
|
"@types/node": "^20.0.0",
|
|
38
|
+
"dotenv": "^17.2.3",
|
|
38
39
|
"jest": "^30.2.0",
|
|
39
40
|
"ts-jest": "^29.4.6",
|
|
40
41
|
"typescript": "^5.0.0"
|
|
41
42
|
},
|
|
42
43
|
"dependencies": {
|
|
43
|
-
"
|
|
44
|
-
"
|
|
44
|
+
"@anthropic-ai/sdk": "^0.71.2",
|
|
45
|
+
"@google/genai": "^1.38.0",
|
|
46
|
+
"openai": "^4.0.0",
|
|
47
|
+
"zod": "^3.25.76",
|
|
48
|
+
"zod-to-json-schema": "^3.25.1"
|
|
45
49
|
},
|
|
46
50
|
"engines": {
|
|
47
51
|
"node": ">=18.0.0"
|
package/dist/llm/openai.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ILLMService, ChatMessage } from "./types.js";
|
|
2
|
-
import { ModelConfig } from "../types/index.js";
|
|
3
|
-
export declare class OpenAILLMService implements ILLMService {
|
|
4
|
-
private client;
|
|
5
|
-
constructor(apiKey: string);
|
|
6
|
-
complete(messages: ChatMessage[], model: string, config?: ModelConfig): Promise<string>;
|
|
7
|
-
}
|
package/dist/llm/openai.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import OpenAI from "openai";
|
|
2
|
-
import { LLMExecutionError } from "../errors/index.js";
|
|
3
|
-
export class OpenAILLMService {
|
|
4
|
-
client;
|
|
5
|
-
constructor(apiKey) {
|
|
6
|
-
this.client = new OpenAI({
|
|
7
|
-
apiKey: apiKey,
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
async complete(messages, model, config) {
|
|
11
|
-
try {
|
|
12
|
-
const response = await this.client.chat.completions.create({
|
|
13
|
-
messages: messages,
|
|
14
|
-
model: model,
|
|
15
|
-
response_format: { type: "json_object" }, // Force JSON mode
|
|
16
|
-
temperature: config?.temperature ?? 0.7,
|
|
17
|
-
top_p: config?.top_p,
|
|
18
|
-
max_tokens: config?.max_tokens,
|
|
19
|
-
presence_penalty: config?.presence_penalty,
|
|
20
|
-
frequency_penalty: config?.frequency_penalty,
|
|
21
|
-
});
|
|
22
|
-
const content = response.choices[0]?.message?.content;
|
|
23
|
-
if (!content) {
|
|
24
|
-
throw new LLMExecutionError("Received empty response from LLM");
|
|
25
|
-
}
|
|
26
|
-
return content;
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
throw new LLMExecutionError("Failed to execute LLM request", error);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=openai.js.map
|
package/dist/llm/openai.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAEvD,MAAM,OAAO,gBAAgB;IACnB,MAAM,CAAS;IAEvB,YAAY,MAAc;QACxB,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC;YACvB,MAAM,EAAE,MAAM;SACf,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,QAAQ,CACnB,QAAuB,EACvB,KAAa,EACb,MAAoB;QAEpB,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;gBACzD,QAAQ,EAAE,QAAQ;gBAClB,KAAK,EAAE,KAAK;gBACZ,eAAe,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,kBAAkB;gBAC5D,WAAW,EAAE,MAAM,EAAE,WAAW,IAAI,GAAG;gBACvC,KAAK,EAAE,MAAM,EAAE,KAAK;gBACpB,UAAU,EAAE,MAAM,EAAE,UAAU;gBAC9B,gBAAgB,EAAE,MAAM,EAAE,gBAAgB;gBAC1C,iBAAiB,EAAE,MAAM,EAAE,iBAAiB;aAC7C,CAAC,CAAC;YAEH,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC;YACtD,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,MAAM,IAAI,iBAAiB,CAAC,kCAAkC,CAAC,CAAC;YAClE,CAAC;YAED,OAAO,OAAO,CAAC;QACjB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,iBAAi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