@ainetwork/adk-provider-model-gemini 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +69 -0
- package/package.json +3 -3
package/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { BaseModel } from "@ainetwork/adk/modules";
|
|
2
2
|
import { ChatRole, type SessionObject } from "@ainetwork/adk/types/memory";
|
|
3
|
+
import type {
|
|
4
|
+
LLMStream,
|
|
5
|
+
StreamChunk,
|
|
6
|
+
ToolCallDelta,
|
|
7
|
+
} from "@ainetwork/adk/types/stream";
|
|
3
8
|
import type {
|
|
4
9
|
FetchResponse,
|
|
5
10
|
IA2ATool,
|
|
@@ -12,6 +17,7 @@ import {
|
|
|
12
17
|
type Content,
|
|
13
18
|
type FunctionCall,
|
|
14
19
|
type FunctionDeclaration,
|
|
20
|
+
type GenerateContentResponse,
|
|
15
21
|
GoogleGenAI,
|
|
16
22
|
} from "@google/genai";
|
|
17
23
|
|
|
@@ -112,6 +118,69 @@ export class GeminiModel extends BaseModel<Content, FunctionDeclaration> {
|
|
|
112
118
|
return await this.fetch(messages);
|
|
113
119
|
}
|
|
114
120
|
|
|
121
|
+
async fetchStreamWithContextMessage(
|
|
122
|
+
messages: Content[],
|
|
123
|
+
functions: FunctionDeclaration[],
|
|
124
|
+
): Promise<LLMStream> {
|
|
125
|
+
const stream = await this.client.models.generateContentStream({
|
|
126
|
+
model: this.modelName,
|
|
127
|
+
contents: messages,
|
|
128
|
+
config: { tools: [{ functionDeclarations: functions }] },
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return await this.createGeminiStreamAdapter(stream);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// NOTE(yoojin): Need to switch API Stream type to LLMStream.
|
|
135
|
+
private createGeminiStreamAdapter(
|
|
136
|
+
geminiStream: AsyncIterable<GenerateContentResponse>,
|
|
137
|
+
): LLMStream {
|
|
138
|
+
const hasName = (
|
|
139
|
+
value: FunctionCall,
|
|
140
|
+
): value is FunctionCall & { name: string } => {
|
|
141
|
+
return value.name !== undefined;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
async *[Symbol.asyncIterator](): AsyncIterator<StreamChunk> {
|
|
146
|
+
for await (const geminiChunk of geminiStream) {
|
|
147
|
+
yield {
|
|
148
|
+
delta: {
|
|
149
|
+
role: geminiChunk.candidates?.[0]?.content?.role,
|
|
150
|
+
content:
|
|
151
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]?.text ||
|
|
152
|
+
undefined,
|
|
153
|
+
tool_calls: hasName(
|
|
154
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
155
|
+
?.functionCall || {},
|
|
156
|
+
)
|
|
157
|
+
? ([
|
|
158
|
+
{
|
|
159
|
+
index: 0,
|
|
160
|
+
id:
|
|
161
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
162
|
+
?.functionCall?.id || "id",
|
|
163
|
+
function: {
|
|
164
|
+
name: geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
165
|
+
?.functionCall?.name,
|
|
166
|
+
arguments:
|
|
167
|
+
geminiChunk.candidates?.[0]?.content?.parts?.[0]
|
|
168
|
+
?.functionCall?.args,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
] as ToolCallDelta[])
|
|
172
|
+
: undefined,
|
|
173
|
+
},
|
|
174
|
+
finish_reason: geminiChunk.candidates?.[0]?.finishReason as any,
|
|
175
|
+
metadata: {
|
|
176
|
+
provider: "gemini",
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
115
184
|
convertToolsToFunctions(tools: IAgentTool[]): FunctionDeclaration[] {
|
|
116
185
|
const functions: FunctionDeclaration[] = [];
|
|
117
186
|
for (const tool of tools) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ainetwork/adk-provider-model-gemini",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"author": "AI Network (https://ainetwork.ai)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"clean": "rm -rf dist"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@ainetwork/adk": "^0.1.
|
|
24
|
+
"@ainetwork/adk": "^0.1.7",
|
|
25
25
|
"@google/genai": "^1.11.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "4098aacea7a18099aa020ef5d331cdce0d59c3f3"
|
|
35
35
|
}
|