structured-json-agent 1.0.1 → 1.1.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 +66 -55
- package/dist/agent/index.d.ts +19 -1
- package/dist/agent/index.js +43 -24
- package/dist/agent/index.js.map +1 -1
- package/dist/llm/adapters/anthropic.d.ts +14 -0
- package/dist/llm/adapters/anthropic.js +46 -0
- package/dist/llm/adapters/anthropic.js.map +1 -0
- package/dist/llm/adapters/google.d.ts +14 -0
- package/dist/llm/adapters/google.js +52 -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 +42 -0
- package/dist/llm/adapters/openai.js.map +1 -0
- package/dist/llm/factory.d.ts +12 -0
- package/dist/llm/factory.js +49 -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 +7 -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 +13 -7
- 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,58 +2,72 @@
|
|
|
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)**, and **Anthropic (Claude)**.
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const outputSchema = {
|
|
39
|
-
type: "object",
|
|
40
|
-
properties: {
|
|
41
|
-
title: { type: "string" },
|
|
42
|
-
keyPoints: { type: "array", items: { type: "string" } },
|
|
43
|
-
summary: { type: "string" }
|
|
44
|
-
},
|
|
45
|
-
required: ["title", "keyPoints", "summary"]
|
|
46
|
-
};
|
|
40
|
+
const outputSchema = z.object({
|
|
41
|
+
title: z.string(),
|
|
42
|
+
keyPoints: z.array(z.string()),
|
|
43
|
+
summary: z.string()
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// 2. Initialize Provider Instances
|
|
47
|
+
const openAiInstance = new OpenAI({
|
|
48
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
49
|
+
});
|
|
47
50
|
|
|
48
|
-
|
|
51
|
+
const anthropicInstance = new Anthropic({
|
|
52
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
53
|
+
});
|
|
54
|
+
|
|
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
|
|
|
@@ -68,7 +82,7 @@ async function main() {
|
|
|
68
82
|
});
|
|
69
83
|
|
|
70
84
|
console.log("Result:", result);
|
|
71
|
-
//
|
|
85
|
+
// Result is typed as inferred from outputSchema
|
|
72
86
|
} catch (error) {
|
|
73
87
|
console.error("Agent failed:", error);
|
|
74
88
|
}
|
|
@@ -79,47 +93,44 @@ main();
|
|
|
79
93
|
|
|
80
94
|
## How It Works
|
|
81
95
|
|
|
82
|
-
1. **Validation**: The input JSON is validated against the `inputSchema
|
|
83
|
-
2. **Generation**: The `
|
|
96
|
+
1. **Validation**: The input JSON is validated against the `inputSchema` (Zod).
|
|
97
|
+
2. **Generation**: The `generator` model creates an initial response based on the system prompt and input.
|
|
98
|
+
* If the provider supports native Structured Outputs (like OpenAI or Anthropic), it is used to maximize reliability.
|
|
84
99
|
3. **Verification Loop**:
|
|
85
100
|
* The response is parsed and validated against `outputSchema`.
|
|
86
101
|
* **If Valid**: The result is returned immediately.
|
|
87
|
-
* **If Invalid**: The `
|
|
102
|
+
* **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
103
|
4. **Convergence**: This cycle repeats until a valid JSON is produced or `maxIterations` is reached.
|
|
89
104
|
|
|
90
105
|
## API Reference
|
|
91
106
|
|
|
92
|
-
### `
|
|
107
|
+
### `AgentConfig`
|
|
108
|
+
|
|
109
|
+
Configuration object passed to `new StructuredAgent(config)`.
|
|
93
110
|
|
|
94
111
|
| Property | Type | Description |
|
|
95
|
-
|
|
|
96
|
-
| `
|
|
97
|
-
| `
|
|
98
|
-
| `
|
|
99
|
-
| `
|
|
100
|
-
| `outputSchema` | `object` | JSON Schema for the expected output. |
|
|
112
|
+
| :--- | :--- | :--- |
|
|
113
|
+
| `generator` | `LLMConfig` | Configuration for the generation model. |
|
|
114
|
+
| `reviewer` | `LLMConfig?` | Configuration for the reviewer model (optional). |
|
|
115
|
+
| `inputSchema` | `ZodSchema` | Zod Schema for validating the input. |
|
|
116
|
+
| `outputSchema` | `ZodSchema` | Zod Schema for the expected output. |
|
|
101
117
|
| `systemPrompt` | `string` | Core instructions for the agent. |
|
|
102
118
|
| `maxIterations` | `number?` | Max retries for correction. Default: 5. |
|
|
103
|
-
| `modelConfig` | `ModelConfig?` | Optional parameters (temperature, etc.). |
|
|
104
|
-
| `llmService` | `ILLMService?` | Optional custom LLM service implementation. |
|
|
105
119
|
|
|
106
|
-
###
|
|
120
|
+
### `LLMConfig`
|
|
107
121
|
|
|
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.
|
|
122
|
+
| Property | Type | Description |
|
|
123
|
+
| :--- | :--- | :--- |
|
|
124
|
+
| `llmService` | `OpenAI \| GoogleGenAI \| Anthropic \| ILLMService` | The provider instance or custom service. |
|
|
125
|
+
| `model` | `string` | Model ID (e.g., `gpt-4o`, `claude-3-5-sonnet`). |
|
|
126
|
+
| `config` | `ModelConfig?` | Optional parameters (temperature, max_tokens, etc.). |
|
|
115
127
|
|
|
116
128
|
## Architecture
|
|
117
129
|
|
|
118
130
|
The project is structured by domain:
|
|
119
131
|
|
|
120
|
-
* `src/agent`: Core orchestration logic.
|
|
121
|
-
* `src/schemas`: Validation logic using
|
|
122
|
-
* `src/llm`:
|
|
132
|
+
* `src/agent`: Core orchestration logic (`StructuredAgent`).
|
|
133
|
+
* `src/schemas`: Validation logic using **Zod**.
|
|
134
|
+
* `src/llm`: Adapters and Factory for LLM providers (`OpenAI`, `Google`, `Anthropic`).
|
|
123
135
|
* `src/errors`: Custom error definitions.
|
|
124
136
|
* `src/types`: Shared interfaces.
|
|
125
|
-
|
package/dist/agent/index.d.ts
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import { AgentConfig } 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);
|
|
21
|
+
/**
|
|
22
|
+
* Runs the agent with the given input JSON.
|
|
23
|
+
*
|
|
24
|
+
* @param inputJson - The input JSON to process.
|
|
25
|
+
* @returns A promise that resolves to the processed JSON.
|
|
26
|
+
*/
|
|
9
27
|
run(inputJson: unknown): Promise<unknown>;
|
|
10
28
|
private generateInitialResponse;
|
|
11
29
|
private reviewResponse;
|
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,29 +35,35 @@ 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
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Runs the agent with the given input JSON.
|
|
45
|
+
*
|
|
46
|
+
* @param inputJson - The input JSON to process.
|
|
47
|
+
* @returns A promise that resolves to the processed JSON.
|
|
48
|
+
*/
|
|
28
49
|
async run(inputJson) {
|
|
29
|
-
// 1. Validate Input
|
|
30
50
|
try {
|
|
31
51
|
this.schemaValidator.validate(this.inputValidator, inputJson);
|
|
32
52
|
}
|
|
33
53
|
catch (error) {
|
|
34
54
|
if (error instanceof SchemaValidationError) {
|
|
35
|
-
// Enhance error message if needed, but the original is good
|
|
36
55
|
throw error;
|
|
37
56
|
}
|
|
38
57
|
throw error;
|
|
39
58
|
}
|
|
40
|
-
const maxIterations = this.config.maxIterations ??
|
|
59
|
+
const maxIterations = this.config.maxIterations ?? 1;
|
|
41
60
|
const history = [];
|
|
42
|
-
// 2. Initial Generation
|
|
43
61
|
let currentJson = await this.generateInitialResponse(inputJson);
|
|
44
62
|
history.push({ step: "generation", result: currentJson });
|
|
45
63
|
let validationResult = this.validateOutput(currentJson);
|
|
46
64
|
if (validationResult.valid) {
|
|
47
65
|
return currentJson;
|
|
48
66
|
}
|
|
49
|
-
// 3. Review Loop
|
|
50
67
|
for (let i = 0; i < maxIterations; i++) {
|
|
51
68
|
try {
|
|
52
69
|
currentJson = await this.reviewResponse(currentJson, validationResult.errors || [], inputJson // Context might be needed
|
|
@@ -58,16 +75,9 @@ export class StructuredAgent {
|
|
|
58
75
|
}
|
|
59
76
|
}
|
|
60
77
|
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
78
|
if (error instanceof LLMExecutionError) {
|
|
68
79
|
throw error;
|
|
69
80
|
}
|
|
70
|
-
// If JSON parse error in reviewResponse, it might be caught there.
|
|
71
81
|
}
|
|
72
82
|
}
|
|
73
83
|
throw new MaxIterationsExceededError(`Failed to generate valid JSON after ${maxIterations} review iterations`, history);
|
|
@@ -83,10 +93,17 @@ export class StructuredAgent {
|
|
|
83
93
|
content: JSON.stringify(input),
|
|
84
94
|
},
|
|
85
95
|
];
|
|
86
|
-
const responseText = await this.
|
|
96
|
+
const responseText = await this.generatorService.complete({
|
|
97
|
+
messages,
|
|
98
|
+
model: this.config.generator.model,
|
|
99
|
+
config: this.config.generator.config,
|
|
100
|
+
outputFormat: this.outputValidator
|
|
101
|
+
});
|
|
87
102
|
return this.parseJson(responseText);
|
|
88
103
|
}
|
|
89
104
|
async reviewResponse(invalidJson, errors, originalInput) {
|
|
105
|
+
const reviewerService = this.reviewerService || this.generatorService;
|
|
106
|
+
const reviewerConfig = this.config.reviewer || this.config.generator;
|
|
90
107
|
const messages = [
|
|
91
108
|
{
|
|
92
109
|
role: "system",
|
|
@@ -105,12 +122,17 @@ Validation Errors:
|
|
|
105
122
|
${errors.join("\n")}
|
|
106
123
|
|
|
107
124
|
Expected Output Schema:
|
|
108
|
-
${JSON.stringify(this.config.outputSchema)}
|
|
125
|
+
${JSON.stringify(zodToJsonSchema(this.config.outputSchema))}
|
|
109
126
|
|
|
110
127
|
Please correct the JSON.`,
|
|
111
128
|
},
|
|
112
129
|
];
|
|
113
|
-
const responseText = await
|
|
130
|
+
const responseText = await reviewerService.complete({
|
|
131
|
+
messages,
|
|
132
|
+
model: reviewerConfig.model,
|
|
133
|
+
config: reviewerConfig.config,
|
|
134
|
+
outputFormat: this.outputValidator
|
|
135
|
+
});
|
|
114
136
|
return this.parseJson(responseText);
|
|
115
137
|
}
|
|
116
138
|
validateOutput(data) {
|
|
@@ -131,19 +153,16 @@ Please correct the JSON.`,
|
|
|
131
153
|
return JSON.parse(text);
|
|
132
154
|
}
|
|
133
155
|
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
156
|
return text;
|
|
139
157
|
}
|
|
140
158
|
}
|
|
141
159
|
buildSystemPrompt() {
|
|
160
|
+
const jsonSchema = zodToJsonSchema(this.config.outputSchema);
|
|
142
161
|
return `${this.config.systemPrompt}
|
|
143
162
|
|
|
144
163
|
IMPORTANT: You must output strict JSON only.
|
|
145
164
|
The output must adhere to the following JSON Schema:
|
|
146
|
-
${JSON.stringify(
|
|
165
|
+
${JSON.stringify(jsonSchema)}
|
|
147
166
|
`;
|
|
148
167
|
}
|
|
149
168
|
}
|
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,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AC,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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
import { ZodSchema } from "zod";
|
|
3
|
+
import { ILLMService, ChatMessage } 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<string>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
import { LLMExecutionError } from "../../errors/index.js";
|
|
3
|
+
export class AnthropicAdapter {
|
|
4
|
+
client;
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
}
|
|
8
|
+
async complete(params) {
|
|
9
|
+
try {
|
|
10
|
+
const systemMessage = params.messages.find((m) => m.role === "system");
|
|
11
|
+
const conversationMessages = params.messages
|
|
12
|
+
.filter((m) => m.role !== "system")
|
|
13
|
+
.map((m) => ({
|
|
14
|
+
role: m.role,
|
|
15
|
+
content: m.content,
|
|
16
|
+
}));
|
|
17
|
+
const requestOptions = {
|
|
18
|
+
model: params.model,
|
|
19
|
+
messages: conversationMessages,
|
|
20
|
+
system: systemMessage?.content,
|
|
21
|
+
max_tokens: params.config?.max_tokens ?? 1024,
|
|
22
|
+
temperature: params.config?.temperature ?? 0.7,
|
|
23
|
+
top_p: params.config?.top_p,
|
|
24
|
+
};
|
|
25
|
+
const headers = {};
|
|
26
|
+
if (params.outputFormat) {
|
|
27
|
+
requestOptions.output_format = {
|
|
28
|
+
type: "json_schema",
|
|
29
|
+
schema: zodToJsonSchema(params.outputFormat)
|
|
30
|
+
};
|
|
31
|
+
// Enable beta feature
|
|
32
|
+
headers["anthropic-beta"] = "structured-outputs-2025-11-13";
|
|
33
|
+
}
|
|
34
|
+
const response = await this.client.messages.create(requestOptions, { headers });
|
|
35
|
+
const content = response.content[0];
|
|
36
|
+
if (content.type !== "text") {
|
|
37
|
+
throw new LLMExecutionError("Received non-text response from Anthropic");
|
|
38
|
+
}
|
|
39
|
+
return content.text;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
throw new LLMExecutionError("Failed to execute Anthropic request", error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# 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;AAGrD,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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OAAO,OAAO,CAAC,IAAI,CAAC;QACtB,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 { GoogleGenAI } from "@google/genai";
|
|
2
|
+
import { ZodSchema } from "zod";
|
|
3
|
+
import { ILLMService, ChatMessage } 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<string>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { LLMExecutionError } from "../../errors/index.js";
|
|
2
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
|
+
export class GoogleGenAIAdapter {
|
|
4
|
+
client;
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
}
|
|
8
|
+
async complete(params) {
|
|
9
|
+
try {
|
|
10
|
+
// Extract system instruction if present
|
|
11
|
+
const systemMessage = params.messages.find((m) => m.role === "system");
|
|
12
|
+
// Filter out system message for the history
|
|
13
|
+
const history = params.messages
|
|
14
|
+
.filter((m) => m.role !== "system")
|
|
15
|
+
.map((m) => ({
|
|
16
|
+
role: m.role === "assistant" ? "model" : "user",
|
|
17
|
+
parts: [{ text: m.content }],
|
|
18
|
+
}));
|
|
19
|
+
// New SDK uses client.models.generateContent
|
|
20
|
+
// System instruction is part of the config
|
|
21
|
+
const generationConfig = {
|
|
22
|
+
temperature: params.config?.temperature,
|
|
23
|
+
topP: params.config?.top_p,
|
|
24
|
+
maxOutputTokens: params.config?.max_tokens,
|
|
25
|
+
responseMimeType: "application/json",
|
|
26
|
+
};
|
|
27
|
+
const result = await this.client.models.generateContent({
|
|
28
|
+
model: params.model,
|
|
29
|
+
contents: history,
|
|
30
|
+
config: {
|
|
31
|
+
...generationConfig,
|
|
32
|
+
responseJsonSchema: params.outputFormat ? zodToJsonSchema(params.outputFormat) : undefined,
|
|
33
|
+
systemInstruction: systemMessage ? { parts: [{ text: systemMessage.content }] } : undefined
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const text = result.text || result.candidates?.[0]?.content?.parts?.[0]?.text;
|
|
37
|
+
if (!text) {
|
|
38
|
+
if (result.candidates && result.candidates.length > 0) {
|
|
39
|
+
const part = result.candidates[0].content?.parts?.[0];
|
|
40
|
+
if (part && 'text' in part)
|
|
41
|
+
return part.text;
|
|
42
|
+
}
|
|
43
|
+
throw new LLMExecutionError("Received empty response from Google GenAI");
|
|
44
|
+
}
|
|
45
|
+
return text;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new LLMExecutionError("Failed to execute Google GenAI request", error);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=google.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../../src/llm/adapters/google.ts"],"names":[],"mappings":"AAIA,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,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,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;YAE9E,IAAI,CAAC,IAAI,EAAE,CAAC;gBACT,IAAI,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrD,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;oBACtD,IAAI,IAAI,IAAI,MAAM,IAAI,IAAI;wBAAE,OAAO,IAAI,CAAC,IAAc,CAAC;gBAC1D,CAAC;gBACD,MAAM,IAAI,iBAAiB,CAAC,2CAA2C,CAAC,CAAC;YAC5E,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,iBAAiB,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;QAC/E,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { ZodSchema } from "zod";
|
|
3
|
+
import { ILLMService, ChatMessage } 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<string>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { zodResponseFormat } from "openai/helpers/zod";
|
|
2
|
+
import { LLMExecutionError } from "../../errors/index.js";
|
|
3
|
+
export class OpenAIAdapter {
|
|
4
|
+
client;
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
}
|
|
8
|
+
async complete(params) {
|
|
9
|
+
try {
|
|
10
|
+
const createParams = {
|
|
11
|
+
messages: params.messages,
|
|
12
|
+
model: params.model,
|
|
13
|
+
response_format: { type: "json_object" },
|
|
14
|
+
temperature: params.config?.temperature ?? 1,
|
|
15
|
+
top_p: params.config?.top_p,
|
|
16
|
+
max_tokens: params.config?.max_tokens,
|
|
17
|
+
presence_penalty: params.config?.presence_penalty,
|
|
18
|
+
frequency_penalty: params.config?.frequency_penalty,
|
|
19
|
+
};
|
|
20
|
+
if (params.outputFormat) {
|
|
21
|
+
createParams.response_format = zodResponseFormat(params.outputFormat, "response");
|
|
22
|
+
}
|
|
23
|
+
const response = await this.client.chat.completions.create(createParams);
|
|
24
|
+
const message = response.choices[0]?.message;
|
|
25
|
+
const content = message?.content;
|
|
26
|
+
if (message?.refusal) {
|
|
27
|
+
throw new LLMExecutionError(`Model refused to generate response: ${message.refusal}`);
|
|
28
|
+
}
|
|
29
|
+
if (!content) {
|
|
30
|
+
throw new LLMExecutionError("Received empty response from LLM");
|
|
31
|
+
}
|
|
32
|
+
return content;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error instanceof LLMExecutionError) {
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
throw new LLMExecutionError("Failed to execute OpenAI request", error);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# 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;AAIvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAG1D,MAAM,OAAO,aAAa;IAChB,MAAM,CAAS;IAEvB,YAAY,MAAc;QAC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AK,YAAY,iBAAiB,EAAE,CAAC;gBACvC,MAAM,KAAK,CAAC;YACd,CAAC;YACD,MAAM,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,49 @@
|
|
|
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
|
+
export class LLMFactory {
|
|
8
|
+
static create(instance) {
|
|
9
|
+
if (this.isILLMService(instance)) {
|
|
10
|
+
return instance;
|
|
11
|
+
}
|
|
12
|
+
if (instance instanceof OpenAI) {
|
|
13
|
+
return new OpenAIAdapter(instance);
|
|
14
|
+
}
|
|
15
|
+
if (instance instanceof Anthropic) {
|
|
16
|
+
return new AnthropicAdapter(instance);
|
|
17
|
+
}
|
|
18
|
+
if (instance instanceof GoogleGenAI) {
|
|
19
|
+
return new GoogleGenAIAdapter(instance);
|
|
20
|
+
}
|
|
21
|
+
// Fallback check based on properties if instanceof fails (e.g. different versions or mocked)
|
|
22
|
+
if (this.isOpenAI(instance)) {
|
|
23
|
+
return new OpenAIAdapter(instance);
|
|
24
|
+
}
|
|
25
|
+
if (this.isAnthropic(instance)) {
|
|
26
|
+
return new AnthropicAdapter(instance);
|
|
27
|
+
}
|
|
28
|
+
if (this.isGoogleGenAI(instance)) {
|
|
29
|
+
return new GoogleGenAIAdapter(instance);
|
|
30
|
+
}
|
|
31
|
+
throw new Error("Unknown LLM instance type provided.");
|
|
32
|
+
}
|
|
33
|
+
static isILLMService(instance) {
|
|
34
|
+
return typeof instance.complete === "function";
|
|
35
|
+
}
|
|
36
|
+
static isOpenAI(instance) {
|
|
37
|
+
return instance?.chat?.completions?.create !== undefined;
|
|
38
|
+
}
|
|
39
|
+
static isAnthropic(instance) {
|
|
40
|
+
return instance?.messages?.create !== undefined;
|
|
41
|
+
}
|
|
42
|
+
static isGoogleGenAI(instance) {
|
|
43
|
+
// New SDK check
|
|
44
|
+
return instance?.models?.generateContent !== undefined ||
|
|
45
|
+
// Old SDK check just in case
|
|
46
|
+
instance?.getGenerativeModel !== undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//# 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;AAI3D,MAAM,OAAO,UAAU;IACd,MAAM,CAAC,MAAM,CAAC,QAAqB;QACxC,IAAI,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;YACjC,OAAO,QAAQ,CA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/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,14 @@
|
|
|
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
|
}
|
|
6
7
|
export interface ILLMService {
|
|
7
|
-
complete(
|
|
8
|
+
complete(params: {
|
|
9
|
+
messages: ChatMessage[];
|
|
10
|
+
model: string;
|
|
11
|
+
config?: ModelConfig;
|
|
12
|
+
outputFormat?: ZodSchema;
|
|
13
|
+
}): Promise<string>;
|
|
8
14
|
}
|
|
@@ -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 OpenAI from "openai";
|
|
1
2
|
import { ILLMService } 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,16 +10,18 @@ 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
25
|
}
|
|
20
26
|
export interface AgentRunOptions {
|
|
21
27
|
input: unknown;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "structured-json-agent",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.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,eA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