@drax/ai-back 3.31.0 → 3.33.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/dist/config/GoogleAiConfig.js +9 -0
- package/dist/factory/AiProviderFactory.js +8 -3
- package/dist/factory/GoogleAiProviderFactory.js +14 -0
- package/dist/index.js +4 -1
- package/dist/providers/GoogleAiProvider.js +367 -0
- package/dist/tools/BuilderTool.js +9 -0
- package/package.json +5 -3
- package/src/config/GoogleAiConfig.ts +14 -0
- package/src/factory/AiProviderFactory.ts +8 -5
- package/src/factory/GoogleAiProviderFactory.ts +26 -0
- package/src/index.ts +6 -0
- package/src/providers/GoogleAiProvider.ts +489 -0
- package/src/tools/BuilderTool.ts +12 -0
- package/test/GoogleAiProvider.test.ts +211 -0
- package/test/ToolBuilder.test.ts +48 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/types/config/GoogleAiConfig.d.ts +9 -0
- package/types/config/GoogleAiConfig.d.ts.map +1 -0
- package/types/factory/AiProviderFactory.d.ts.map +1 -1
- package/types/factory/GoogleAiProviderFactory.d.ts +8 -0
- package/types/factory/GoogleAiProviderFactory.d.ts.map +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/providers/GoogleAiProvider.d.ts +63 -0
- package/types/providers/GoogleAiProvider.d.ts.map +1 -0
- package/types/tools/BuilderTool.d.ts.map +1 -1
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import {describe, expect, test} from "vitest";
|
|
2
|
+
import {AiProviderFactory, GoogleAiProvider} from "../src";
|
|
3
|
+
import {IPromptTool} from "../src/interfaces/IAIProvider";
|
|
4
|
+
|
|
5
|
+
describe("GoogleAiProvider Test", () => {
|
|
6
|
+
|
|
7
|
+
test("GoogleAi prompt supports image inputs and vision model fallback", async () => {
|
|
8
|
+
let request: any
|
|
9
|
+
|
|
10
|
+
class MockedGoogleAiProvider extends GoogleAiProvider {
|
|
11
|
+
constructor() {
|
|
12
|
+
super("test-key", "gemini-2.5-flash", "gemini-2.5-flash")
|
|
13
|
+
this._client = {
|
|
14
|
+
models: {
|
|
15
|
+
generateContent: async (payload: any) => {
|
|
16
|
+
request = payload
|
|
17
|
+
return {
|
|
18
|
+
text: "{\"name\":\"invoice\"}",
|
|
19
|
+
usageMetadata: {
|
|
20
|
+
totalTokenCount: 10,
|
|
21
|
+
promptTokenCount: 7,
|
|
22
|
+
candidatesTokenCount: 3,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
} as any
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const googleAi = new MockedGoogleAiProvider()
|
|
32
|
+
|
|
33
|
+
const r = await googleAi.prompt({
|
|
34
|
+
systemPrompt: "Extract invoice data",
|
|
35
|
+
userInput: "Read this invoice",
|
|
36
|
+
userImages: [{url: "data:image/png;base64,abc123", detail: "high"}]
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
expect(r.output).toBe("{\"name\":\"invoice\"}")
|
|
40
|
+
expect(request.model).toBe("gemini-2.5-flash")
|
|
41
|
+
expect(request.contents[0]).toEqual({
|
|
42
|
+
role: "user",
|
|
43
|
+
parts: [
|
|
44
|
+
{text: "Read this invoice"},
|
|
45
|
+
{
|
|
46
|
+
inlineData: {
|
|
47
|
+
mimeType: "image/png",
|
|
48
|
+
data: "abc123",
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test("GoogleAi prompt maps OpenAI jsonSchema format to Gemini responseJsonSchema", async () => {
|
|
56
|
+
let request: any
|
|
57
|
+
|
|
58
|
+
class MockedGoogleAiProvider extends GoogleAiProvider {
|
|
59
|
+
constructor() {
|
|
60
|
+
super("test-key", "gemini-2.5-flash")
|
|
61
|
+
this._client = {
|
|
62
|
+
models: {
|
|
63
|
+
generateContent: async (payload: any) => {
|
|
64
|
+
request = payload
|
|
65
|
+
return {
|
|
66
|
+
text: "{\"name\":\"Pikachu\"}",
|
|
67
|
+
usageMetadata: {
|
|
68
|
+
totalTokenCount: 8,
|
|
69
|
+
promptTokenCount: 6,
|
|
70
|
+
candidatesTokenCount: 2,
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} as any
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const jsonSchema = {
|
|
80
|
+
type: "json_schema",
|
|
81
|
+
json_schema: {
|
|
82
|
+
name: "element_description",
|
|
83
|
+
schema: {
|
|
84
|
+
type: "object",
|
|
85
|
+
properties: {
|
|
86
|
+
name: {type: "string"}
|
|
87
|
+
},
|
|
88
|
+
required: ["name"]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const googleAi = new MockedGoogleAiProvider()
|
|
94
|
+
await googleAi.prompt({
|
|
95
|
+
systemPrompt: "You are an AI assistant.",
|
|
96
|
+
userInput: "What is the most famous pokemon",
|
|
97
|
+
jsonSchema,
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
expect(request.config.responseMimeType).toBe("application/json")
|
|
101
|
+
expect(request.config.responseJsonSchema).toEqual(jsonSchema.json_schema.schema)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test("GoogleAi prompt executes tools and sends function response back to model", async () => {
|
|
105
|
+
const requests: any[] = []
|
|
106
|
+
const weatherTool: IPromptTool = {
|
|
107
|
+
name: "get_weather",
|
|
108
|
+
description: "Get weather for a city",
|
|
109
|
+
parameters: {
|
|
110
|
+
type: "object",
|
|
111
|
+
properties: {
|
|
112
|
+
city: {type: "string"}
|
|
113
|
+
},
|
|
114
|
+
required: ["city"],
|
|
115
|
+
additionalProperties: false
|
|
116
|
+
},
|
|
117
|
+
execute: async ({city}) => ({city, temperature: 21})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
class MockedGoogleAiProvider extends GoogleAiProvider {
|
|
121
|
+
constructor() {
|
|
122
|
+
super("test-key", "gemini-2.5-flash")
|
|
123
|
+
this._client = {
|
|
124
|
+
models: {
|
|
125
|
+
generateContent: async (payload: any) => {
|
|
126
|
+
requests.push(payload)
|
|
127
|
+
|
|
128
|
+
if(requests.length === 1){
|
|
129
|
+
return {
|
|
130
|
+
functionCalls: [{
|
|
131
|
+
id: "call_123",
|
|
132
|
+
name: "get_weather",
|
|
133
|
+
args: {city: "Buenos Aires"}
|
|
134
|
+
}],
|
|
135
|
+
candidates: [{
|
|
136
|
+
content: {
|
|
137
|
+
role: "model",
|
|
138
|
+
parts: [{
|
|
139
|
+
functionCall: {
|
|
140
|
+
id: "call_123",
|
|
141
|
+
name: "get_weather",
|
|
142
|
+
args: {city: "Buenos Aires"}
|
|
143
|
+
}
|
|
144
|
+
}]
|
|
145
|
+
}
|
|
146
|
+
}],
|
|
147
|
+
usageMetadata: {
|
|
148
|
+
totalTokenCount: 15,
|
|
149
|
+
promptTokenCount: 10,
|
|
150
|
+
candidatesTokenCount: 5,
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
text: "21 grados",
|
|
157
|
+
usageMetadata: {
|
|
158
|
+
totalTokenCount: 9,
|
|
159
|
+
promptTokenCount: 7,
|
|
160
|
+
candidatesTokenCount: 2,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
} as any
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const googleAi = new MockedGoogleAiProvider()
|
|
170
|
+
const r = await googleAi.prompt({
|
|
171
|
+
systemPrompt: "You are an AI assistant.",
|
|
172
|
+
userInput: "How is the weather in Buenos Aires?",
|
|
173
|
+
tools: [weatherTool]
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
expect(r.output).toBe("21 grados")
|
|
177
|
+
expect(r.tokens).toBe(24)
|
|
178
|
+
expect(requests[0].config.tools).toEqual([{
|
|
179
|
+
functionDeclarations: [{
|
|
180
|
+
name: "get_weather",
|
|
181
|
+
description: "Get weather for a city",
|
|
182
|
+
parametersJsonSchema: weatherTool.parameters,
|
|
183
|
+
}]
|
|
184
|
+
}])
|
|
185
|
+
expect(requests[1].contents[2]).toEqual({
|
|
186
|
+
role: "user",
|
|
187
|
+
parts: [{
|
|
188
|
+
functionResponse: {
|
|
189
|
+
id: "call_123",
|
|
190
|
+
name: "get_weather",
|
|
191
|
+
response: {
|
|
192
|
+
output: {
|
|
193
|
+
city: "Buenos Aires",
|
|
194
|
+
temperature: 21,
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}]
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
test("AiProviderFactory supports GoogleAi option", () => {
|
|
203
|
+
process.env.GOOGLE_AI_API_KEY = "test-key"
|
|
204
|
+
process.env.GOOGLE_AI_MODEL = "gemini-2.5-flash"
|
|
205
|
+
process.env.GOOGLE_AI_VISION_MODEL = "gemini-2.5-flash"
|
|
206
|
+
process.env.DRAX_DB_ENGINE = "mongo"
|
|
207
|
+
|
|
208
|
+
const googleAi = AiProviderFactory.instance("GoogleAi")
|
|
209
|
+
expect(googleAi).toBeInstanceOf(GoogleAiProvider)
|
|
210
|
+
})
|
|
211
|
+
})
|
package/test/ToolBuilder.test.ts
CHANGED
|
@@ -77,6 +77,54 @@ describe("ToolBuilder", () => {
|
|
|
77
77
|
expect(section).toContain("Schema JSON de la entidad");
|
|
78
78
|
});
|
|
79
79
|
|
|
80
|
+
test("adapts date schemas to OpenAI-compatible JSON schema", () => {
|
|
81
|
+
const service: any = {
|
|
82
|
+
async create(data: any) {
|
|
83
|
+
return data;
|
|
84
|
+
},
|
|
85
|
+
async updatePartial(_id: string, data: any) {
|
|
86
|
+
return data;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const schema = z.object({
|
|
91
|
+
birthdate: z.coerce.date().nullable().optional(),
|
|
92
|
+
createdAt: z.coerce.date().default(new Date("2024-01-01T00:00:00.000Z")),
|
|
93
|
+
history: z.array(z.object({
|
|
94
|
+
at: z.coerce.date(),
|
|
95
|
+
})),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const builder = new BuilderTool({
|
|
99
|
+
entityName: "person",
|
|
100
|
+
schema,
|
|
101
|
+
service,
|
|
102
|
+
methods: ["create", "updatePartial"],
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const tools = builder.getTools();
|
|
106
|
+
const createParameters: any = tools[0].parameters;
|
|
107
|
+
const dataSchema = createParameters.properties.data;
|
|
108
|
+
|
|
109
|
+
expect(dataSchema.properties.birthdate).toMatchObject({
|
|
110
|
+
anyOf: [
|
|
111
|
+
{type: "string", format: "date-time"},
|
|
112
|
+
{type: "null"},
|
|
113
|
+
],
|
|
114
|
+
});
|
|
115
|
+
expect(dataSchema.properties.createdAt).toMatchObject({
|
|
116
|
+
type: "string",
|
|
117
|
+
format: "date-time",
|
|
118
|
+
default: "2024-01-01T00:00:00.000Z",
|
|
119
|
+
});
|
|
120
|
+
expect(dataSchema.properties.history.items.properties.at).toMatchObject({
|
|
121
|
+
type: "string",
|
|
122
|
+
format: "date-time",
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
expect(() => builder.getSystemPromptSection()).not.toThrow();
|
|
126
|
+
});
|
|
127
|
+
|
|
80
128
|
test("fails when a requested service method is not available", () => {
|
|
81
129
|
const builder = new BuilderTool({
|
|
82
130
|
entityName: "person",
|