-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
llm_gemini.py
337 lines (304 loc) · 11.1 KB
/
llm_gemini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import httpx
import ijson
import llm
from pydantic import Field
from typing import Optional
SAFETY_SETTINGS = [
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
]
@llm.hookimpl
def register_models(register):
# Register both sync and async versions of each model
for model_id in [
"gemini-pro",
"gemini-1.5-pro-latest",
"gemini-1.5-flash-latest",
"gemini-1.5-pro-001",
"gemini-1.5-flash-001",
"gemini-1.5-pro-002",
"gemini-1.5-flash-002",
"gemini-1.5-flash-8b-latest",
"gemini-1.5-flash-8b-001",
"gemini-exp-1114",
"gemini-exp-1121",
"gemini-exp-1206",
"gemini-2.0-flash-exp",
]:
register(GeminiPro(model_id), AsyncGeminiPro(model_id))
def resolve_type(attachment):
mime_type = attachment.resolve_type()
# https://github.com/simonw/llm/issues/587#issuecomment-2439785140
if mime_type == "audio/mpeg":
mime_type = "audio/mp3"
return mime_type
class _SharedGemini:
needs_key = "gemini"
key_env_var = "LLM_GEMINI_KEY"
can_stream = True
attachment_types = (
# PDF
"application/pdf",
# Images
"image/png",
"image/jpeg",
"image/webp",
"image/heic",
"image/heif",
# Audio
"audio/wav",
"audio/mp3",
"audio/aiff",
"audio/aac",
"audio/ogg",
"audio/flac",
"audio/mpeg", # Treated as audio/mp3
# Video
"video/mp4",
"video/mpeg",
"video/mov",
"video/avi",
"video/x-flv",
"video/mpg",
"video/webm",
"video/wmv",
"video/3gpp",
"video/quicktime",
)
class Options(llm.Options):
code_execution: Optional[bool] = Field(
description="Enables the model to generate and run Python code",
default=None,
)
temperature: Optional[float] = Field(
description=(
"Controls the randomness of the output. Use higher values for "
"more creative responses, and lower values for more "
"deterministic responses."
),
default=None,
ge=0.0,
le=2.0,
)
max_output_tokens: Optional[int] = Field(
description="Sets the maximum number of tokens to include in a candidate.",
default=None,
)
top_p: Optional[float] = Field(
description=(
"Changes how the model selects tokens for output. Tokens are "
"selected from the most to least probable until the sum of "
"their probabilities equals the topP value."
),
default=None,
ge=0.0,
le=1.0,
)
top_k: Optional[int] = Field(
description=(
"Changes how the model selects tokens for output. A topK of 1 "
"means the selected token is the most probable among all the "
"tokens in the model's vocabulary, while a topK of 3 means "
"that the next token is selected from among the 3 most "
"probable using the temperature."
),
default=None,
ge=1,
)
json_object: Optional[bool] = Field(
description="Output a valid JSON object {...}",
default=None,
)
def __init__(self, model_id):
self.model_id = model_id
def build_messages(self, prompt, conversation):
messages = []
if conversation:
for response in conversation.responses:
parts = []
for attachment in response.attachments:
mime_type = resolve_type(attachment)
parts.append(
{
"inlineData": {
"data": attachment.base64_content(),
"mimeType": mime_type,
}
}
)
if response.prompt.prompt:
parts.append({"text": response.prompt.prompt})
messages.append({"role": "user", "parts": parts})
messages.append({"role": "model", "parts": [{"text": response.text()}]})
parts = []
if prompt.prompt:
parts.append({"text": prompt.prompt})
for attachment in prompt.attachments:
mime_type = resolve_type(attachment)
parts.append(
{
"inlineData": {
"data": attachment.base64_content(),
"mimeType": mime_type,
}
}
)
messages.append({"role": "user", "parts": parts})
return messages
def build_request_body(self, prompt, conversation):
body = {
"contents": self.build_messages(prompt, conversation),
"safetySettings": SAFETY_SETTINGS,
}
if prompt.options and prompt.options.code_execution:
body["tools"] = [{"codeExecution": {}}]
if prompt.system:
body["systemInstruction"] = {"parts": [{"text": prompt.system}]}
config_map = {
"temperature": "temperature",
"max_output_tokens": "maxOutputTokens",
"top_p": "topP",
"top_k": "topK",
}
if prompt.options and prompt.options.json_object:
body["generationConfig"] = {"response_mime_type": "application/json"}
if any(
getattr(prompt.options, key, None) is not None for key in config_map.keys()
):
generation_config = {}
for key, other_key in config_map.items():
config_value = getattr(prompt.options, key, None)
if config_value is not None:
generation_config[other_key] = config_value
body["generationConfig"] = generation_config
return body
def process_part(self, part):
if "text" in part:
return part["text"]
elif "executableCode" in part:
return f'```{part["executableCode"]["language"].lower()}\n{part["executableCode"]["code"].strip()}\n```\n'
elif "codeExecutionResult" in part:
return f'```\n{part["codeExecutionResult"]["output"].strip()}\n```\n'
return ""
def set_usage(self, response):
try:
usage = response.response_json[-1].pop("usageMetadata")
input_tokens = usage.pop("promptTokenCount", None)
output_tokens = usage.pop("candidatesTokenCount", None)
usage.pop("totalTokenCount", None)
if input_tokens is not None:
response.set_usage(
input=input_tokens, output=output_tokens, details=usage or None
)
except (IndexError, KeyError):
pass
class GeminiPro(_SharedGemini, llm.Model):
def execute(self, prompt, stream, response, conversation):
key = self.get_key()
url = f"https://generativelanguage.googleapis.com/v1beta/models/{self.model_id}:streamGenerateContent"
gathered = []
body = self.build_request_body(prompt, conversation)
with httpx.stream(
"POST",
url,
timeout=None,
headers={"x-goog-api-key": key},
json=body,
) as http_response:
events = ijson.sendable_list()
coro = ijson.items_coro(events, "item")
for chunk in http_response.iter_bytes():
coro.send(chunk)
if events:
event = events[0]
if isinstance(event, dict) and "error" in event:
raise llm.ModelError(event["error"]["message"])
try:
part = event["candidates"][0]["content"]["parts"][0]
yield self.process_part(part)
except KeyError:
yield ""
gathered.append(event)
events.clear()
response.response_json = gathered
self.set_usage(response)
class AsyncGeminiPro(_SharedGemini, llm.AsyncModel):
async def execute(self, prompt, stream, response, conversation):
key = self.get_key()
url = f"https://generativelanguage.googleapis.com/v1beta/models/{self.model_id}:streamGenerateContent"
gathered = []
body = self.build_request_body(prompt, conversation)
async with httpx.AsyncClient() as client:
async with client.stream(
"POST",
url,
timeout=None,
headers={"x-goog-api-key": key},
json=body,
) as http_response:
events = ijson.sendable_list()
coro = ijson.items_coro(events, "item")
async for chunk in http_response.aiter_bytes():
coro.send(chunk)
if events:
event = events[0]
if isinstance(event, dict) and "error" in event:
raise llm.ModelError(event["error"]["message"])
try:
part = event["candidates"][0]["content"]["parts"][0]
yield self.process_part(part)
except KeyError:
yield ""
gathered.append(event)
events.clear()
response.response_json = gathered
self.set_usage(response)
@llm.hookimpl
def register_embedding_models(register):
register(
GeminiEmbeddingModel("text-embedding-004", "text-embedding-004"),
)
class GeminiEmbeddingModel(llm.EmbeddingModel):
needs_key = "gemini"
key_env_var = "LLM_GEMINI_KEY"
batch_size = 20
def __init__(self, model_id, gemini_model_id):
self.model_id = model_id
self.gemini_model_id = gemini_model_id
def embed_batch(self, items):
headers = {
"Content-Type": "application/json",
"x-goog-api-key": self.get_key(),
}
data = {
"requests": [
{
"model": "models/" + self.gemini_model_id,
"content": {"parts": [{"text": item}]},
}
for item in items
]
}
with httpx.Client() as client:
response = client.post(
f"https://generativelanguage.googleapis.com/v1beta/models/{self.gemini_model_id}:batchEmbedContents",
headers=headers,
json=data,
timeout=None,
)
response.raise_for_status()
return [item["values"] for item in response.json()["embeddings"]]