-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path_llm.py
executable file
·386 lines (326 loc) · 12.2 KB
/
_llm.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import numpy as np
from openai import AsyncOpenAI, AsyncAzureOpenAI, APIConnectionError, RateLimitError
from ollama import AsyncClient
from dataclasses import asdict, dataclass, field
from tenacity import (
retry,
stop_after_attempt,
wait_exponential,
retry_if_exception_type,
)
import os
from ._utils import compute_args_hash, wrap_embedding_func_with_attrs
from .base import BaseKVStorage
from ._utils import EmbeddingFunc
global_openai_async_client = None
global_azure_openai_async_client = None
global_ollama_client = None
def get_openai_async_client_instance():
global global_openai_async_client
if global_openai_async_client is None:
global_openai_async_client = AsyncOpenAI()
return global_openai_async_client
def get_azure_openai_async_client_instance():
global global_azure_openai_async_client
if global_azure_openai_async_client is None:
global_azure_openai_async_client = AsyncAzureOpenAI()
return global_azure_openai_async_client
def get_ollama_async_client_instance():
global global_ollama_client
if global_ollama_client is None:
# set OLLAMA_HOST or pass in host="http://127.0.0.1:11434"
global_ollama_client = AsyncClient() # Adjust base URL if necessary
return global_ollama_client
# Setup LLM Configuration.
@dataclass
class LLMConfig:
# To be set
embedding_func_raw: callable
embedding_model_name: str
embedding_dim: int
embedding_max_token_size: int
embedding_batch_num: int
embedding_func_max_async: int
query_better_than_threshold: float
best_model_func_raw: callable
best_model_name: str
best_model_max_token_size: int
best_model_max_async: int
cheap_model_func_raw: callable
cheap_model_name: str
cheap_model_max_token_size: int
cheap_model_max_async: int
# Assigned in post init
embedding_func: EmbeddingFunc = None
best_model_func: callable = None
cheap_model_func: callable = None
def __post_init__(self):
embedding_wrapper = wrap_embedding_func_with_attrs(
embedding_dim = self.embedding_dim,
max_token_size = self.embedding_max_token_size,
model_name = self.embedding_model_name)
self.embedding_func = embedding_wrapper(self.embedding_func_raw)
self.best_model_func = lambda prompt, *args, **kwargs: self.best_model_func_raw(
self.best_model_name, prompt, *args, **kwargs
)
self.cheap_model_func = lambda prompt, *args, **kwargs: self.cheap_model_func_raw(
self.cheap_model_name, prompt, *args, **kwargs
)
##### OpenAI Configuration
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((RateLimitError, APIConnectionError)),
)
async def openai_complete_if_cache(
model, prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
openai_async_client = get_openai_async_client_instance()
hashing_kv: BaseKVStorage = kwargs.pop("hashing_kv", None)
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.extend(history_messages)
messages.append({"role": "user", "content": prompt})
if hashing_kv is not None:
args_hash = compute_args_hash(model, messages)
if_cache_return = await hashing_kv.get_by_id(args_hash)
if if_cache_return is not None:
return if_cache_return["return"]
response = await openai_async_client.chat.completions.create(
model=model, messages=messages, **kwargs
)
if hashing_kv is not None:
await hashing_kv.upsert(
{args_hash: {"return": response.choices[0].message.content, "model": model}}
)
await hashing_kv.index_done_callback()
return response.choices[0].message.content
async def gpt_4o_complete(
model_name, prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
return await openai_complete_if_cache(
model_name,
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
**kwargs,
)
async def gpt_4o_mini_complete(
model_name, prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
return await openai_complete_if_cache(
model_name,
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
**kwargs,
)
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((RateLimitError, APIConnectionError)),
)
async def openai_embedding(model_name: str, texts: list[str]) -> np.ndarray:
openai_async_client = get_openai_async_client_instance()
response = await openai_async_client.embeddings.create(
model=model_name, input=texts, encoding_format="float"
)
return np.array([dp.embedding for dp in response.data])
openai_config = LLMConfig(
embedding_func_raw = openai_embedding,
embedding_model_name = "text-embedding-3-small",
embedding_dim = 1536,
embedding_max_token_size = 8192,
embedding_batch_num = 32,
embedding_func_max_async = 16,
query_better_than_threshold = 0.2,
# LLM
best_model_func_raw = gpt_4o_complete,
best_model_name = "gpt-4o",
best_model_max_token_size = 32768,
best_model_max_async = 16,
cheap_model_func_raw = gpt_4o_mini_complete,
cheap_model_name = "gpt-4o-mini",
cheap_model_max_token_size = 32768,
cheap_model_max_async = 16
)
openai_4o_mini_config = LLMConfig(
embedding_func_raw = openai_embedding,
embedding_model_name = "text-embedding-3-small",
embedding_dim = 1536,
embedding_max_token_size = 8192,
embedding_batch_num = 32,
embedding_func_max_async = 16,
query_better_than_threshold = 0.2,
# LLM
best_model_func_raw = gpt_4o_mini_complete,
best_model_name = "gpt-4o-mini",
best_model_max_token_size = 32768,
best_model_max_async = 16,
cheap_model_func_raw = gpt_4o_mini_complete,
cheap_model_name = "gpt-4o-mini",
cheap_model_max_token_size = 32768,
cheap_model_max_async = 16
)
###### Azure OpenAI Configuration
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((RateLimitError, APIConnectionError)),
)
async def azure_openai_complete_if_cache(
deployment_name, prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
azure_openai_client = get_azure_openai_async_client_instance()
hashing_kv: BaseKVStorage = kwargs.pop("hashing_kv", None)
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.extend(history_messages)
messages.append({"role": "user", "content": prompt})
if hashing_kv is not None:
args_hash = compute_args_hash(deployment_name, messages)
if_cache_return = await hashing_kv.get_by_id(args_hash)
if if_cache_return is not None:
return if_cache_return["return"]
response = await azure_openai_client.chat.completions.create(
model=deployment_name, messages=messages, **kwargs
)
if hashing_kv is not None:
await hashing_kv.upsert(
{
args_hash: {
"return": response.choices[0].message.content,
"model": deployment_name,
}
}
)
await hashing_kv.index_done_callback()
return response.choices[0].message.content
async def azure_gpt_4o_complete(
model_name, prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
return await azure_openai_complete_if_cache(
model_name,
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
**kwargs,
)
async def azure_gpt_4o_mini_complete(
model_name, prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
return await azure_openai_complete_if_cache(
model_name,
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
**kwargs,
)
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((RateLimitError, APIConnectionError)),
)
async def azure_openai_embedding(model_name: str, texts: list[str]) -> np.ndarray:
azure_openai_client = get_azure_openai_async_client_instance()
response = await azure_openai_client.embeddings.create(
model=model_name, input=texts, encoding_format="float"
)
return np.array([dp.embedding for dp in response.data])
azure_openai_config = LLMConfig(
embedding_func_raw = azure_openai_embedding,
embedding_model_name = "text-embedding-3-small",
embedding_dim = 1536,
embedding_max_token_size = 8192,
embedding_batch_num = 32,
embedding_func_max_async = 16,
query_better_than_threshold = 0.2,
best_model_func_raw = azure_gpt_4o_complete,
best_model_name = "gpt-4o",
best_model_max_token_size = 32768,
best_model_max_async = 16,
cheap_model_func_raw = azure_gpt_4o_mini_complete,
cheap_model_name = "gpt-4o-mini",
cheap_model_max_token_size = 32768,
cheap_model_max_async = 16
)
###### Ollama configuration
async def ollama_complete_if_cache(
model, prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
# Initialize the Ollama client
ollama_client = get_ollama_async_client_instance()
hashing_kv: BaseKVStorage = kwargs.pop("hashing_kv", None)
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.extend(history_messages)
messages.append({"role": "user", "content": prompt})
if hashing_kv is not None:
args_hash = compute_args_hash(model, messages)
if_cache_return = await hashing_kv.get_by_id(args_hash)
if if_cache_return is not None:
return if_cache_return["return"]
# Send the request to Ollama
response = await ollama_client.chat(
model=model,
messages=messages
)
# print(messages)
# print(response['message']['content'])
if hashing_kv is not None:
await hashing_kv.upsert(
{args_hash: {"return": response['message']['content'], "model": model}}
)
await hashing_kv.index_done_callback()
return response['message']['content']
async def ollama_complete(model_name, prompt, system_prompt=None, history_messages=[], **kwargs) -> str:
return await ollama_complete_if_cache(
model_name,
prompt,
system_prompt=system_prompt,
history_messages=history_messages
)
async def ollama_mini_complete(model_name, prompt, system_prompt=None, history_messages=[], **kwargs) -> str:
return await ollama_complete_if_cache(
# "deepseek-r1:latest", # For now select your model
model_name,
prompt,
system_prompt=system_prompt,
history_messages=history_messages
)
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((RateLimitError, APIConnectionError)),
)
async def ollama_embedding(model_name: str, texts: list[str]) -> np.ndarray:
# Initialize the Ollama client
ollama_client = get_ollama_async_client_instance()
# Send the request to Ollama for embeddings
response = await ollama_client.embed(
model=model_name,
input=texts
)
# Extract embeddings from the response
embeddings = response['embeddings']
return np.array(embeddings)
ollama_config = LLMConfig(
embedding_func_raw = ollama_embedding,
embedding_model_name = "nomic-embed-text",
embedding_dim = 768,
embedding_max_token_size=8192,
embedding_batch_num = 1,
embedding_func_max_async = 1,
query_better_than_threshold = 0.2,
best_model_func_raw = ollama_complete ,
best_model_name = "gemma2:latest", # need to be a solid instruct model
best_model_max_token_size = 32768,
best_model_max_async = 1,
cheap_model_func_raw = ollama_mini_complete,
cheap_model_name = "olmo2",
cheap_model_max_token_size = 32768,
cheap_model_max_async = 1
)