-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathlitellm.py
More file actions
290 lines (243 loc) · 10.1 KB
/
litellm.py
File metadata and controls
290 lines (243 loc) · 10.1 KB
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
import logging
from ollama import Client
from typing import Optional
from litellm import completion, validate_environment, utils as litellm_utils
from .model import (
OutputMethod,
GenerativeModel,
GenerativeModelConfig,
GenerationResponse,
FinishReason,
GenerativeModelChatSession,
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
class LiteModel(GenerativeModel):
"""
A generative model that interfaces with the LiteLLM for chat completions.
"""
def __init__(
self,
model_name: str,
generation_config: Optional[GenerativeModelConfig] = None,
system_instruction: Optional[str] = None,
):
"""
Initialize the LiteModel with the required parameters.
LiteLLM model_name format: <provider>/<model_name>
Examples:
- openai/gpt-4o
- azure/gpt-4o
- gemini/gemini-1.5-pro
- ollama/llama3:8b
Args:
model_name (str): The name and the provider for the LiteLLM client.
generation_config (Optional[GenerativeModelConfig]): Configuration settings for generation.
system_instruction (Optional[str]): Instruction to guide the model.
"""
env_val = validate_environment(model_name)
if not env_val['keys_in_environment']:
raise ValueError(f"Missing {env_val['missing_keys']} in the environment.")
self.model_name, provider, _, _ = litellm_utils.get_llm_provider(model_name)
self.model = model_name
if provider == "ollama":
self.ollama_client = Client()
self.check_and_pull_model()
if not self.check_valid_key(model_name):
raise ValueError(f"Invalid keys for model {model_name}.")
self.generation_config = generation_config or GenerativeModelConfig()
self.system_instruction = system_instruction
def check_valid_key(self, model: str):
"""
Checks if the environment key is valid for a specific model by making a litellm.completion call with max_tokens=10
Args:
model (str): The name of the model to check the key against.
Returns:
bool: True if the key is valid for the model, False otherwise.
"""
messages = [{"role": "user", "content": "Hey, how's it going?"}]
try:
completion(
model=model, messages=messages, max_tokens=10
)
return True
except Exception as e:
return False
def check_and_pull_model(self) -> None:
"""
Checks if the specified model is available locally, and pulls it if not.
Logs:
- Info: If the model is already available or after successfully pulling the model.
- Error: If there is a failure in pulling the model.
Raises:
Exception: If there is an error during the model pull process.
"""
# Get the list of available models
response = self.ollama_client.list() # This returns a dictionary
available_models = [model['name'] for model in response['models']] # Extract model names
# Check if the model is already pulled
if self.model_name in available_models:
logger.info(f"The model '{self.model_name}' is already available.")
else:
logger.info(f"Pulling the model '{self.model_name}'...")
try:
self.ollama_client.pull(self.model_name) # Pull the model
logger.info(f"Model '{self.model_name}' pulled successfully.")
except Exception as e:
logger.error(f"Failed to pull the model '{self.model_name}': {e}")
raise ValueError(f"Failed to pull the model '{self.model_name}': {e}")
def with_system_instruction(self, system_instruction: str) -> "GenerativeModel":
"""
Set or update the system instruction for new model instance.
Args:
system_instruction (str): Instruction for guiding the model's behavior.
Returns:
GenerativeModel: The updated model instance.
"""
self.system_instruction = system_instruction
return self
def start_chat(self, args: Optional[dict] = None) -> GenerativeModelChatSession:
"""
Start a new chat session.
Args:
args (Optional[dict]): Additional arguments for the chat session.
Returns:
GenerativeModelChatSession: A new instance of the chat session.
"""
return LiteModelChatSession(self, args)
def parse_generate_content_response(self, response: any) -> GenerationResponse:
"""
Parse the model's response and extract content for the user.
Args:
response (any): The raw response from the model.
Returns:
GenerationResponse: Parsed response containing the generated text.
"""
return GenerationResponse(
text=response.choices[0].message.content,
finish_reason=(
FinishReason.STOP
if response.choices[0].finish_reason == "stop"
else (
FinishReason.MAX_TOKENS
if response.choices[0].finish_reason == "length"
else FinishReason.OTHER
)
),
)
def to_json(self) -> dict:
"""
Serialize the model's configuration and state to JSON format.
Returns:
dict: The serialized JSON data.
"""
return {
"model_name": self.model_name,
"generation_config": self.generation_config.to_json(),
"system_instruction": self.system_instruction,
}
@staticmethod
def from_json(json: dict) -> "GenerativeModel":
"""
Deserialize a JSON object to create an instance of LiteLLMGenerativeModel.
Args:
json (dict): The serialized JSON data.
Returns:
GenerativeModel: A new instance of the model.
"""
return LiteModel(
json["model_name"],
generation_config=GenerativeModelConfig.from_json(
json["generation_config"]
),
system_instruction=json["system_instruction"],
)
class LiteModelChatSession(GenerativeModelChatSession):
"""
A chat session for interacting with the LiteLLM model, maintaining conversation history.
"""
def __init__(self, model: LiteModel, args: Optional[dict] = None):
"""
Initialize the chat session and set up the conversation history.
Args:
model (LiteLLMGenerativeModel): The model instance for the session.
args (Optional[dict]): Additional arguments for customization.
"""
self._model = model
self._args = args
self._chat_history = (
[{"role": "system", "content": self._model.system_instruction}]
if self._model.system_instruction is not None
else []
)
def get_chat_history(self) -> list[dict]:
"""
Retrieve the conversation history for the current chat session.
Returns:
list[dict]: The chat session's conversation history.
"""
return self._chat_history.copy()
def send_message(self, message: str, output_method: OutputMethod = OutputMethod.DEFAULT) -> GenerationResponse:
"""
Send a message in the chat session and receive the model's response.
Args:
message (str): The message to send.
output_method (OutputMethod): Format for the model's output.
Returns:
GenerationResponse: The generated response.
"""
generation_config = self._adjust_generation_config(output_method)
self._chat_history.append({"role": "user", "content": message})
try:
response = completion(
model=self._model.model,
messages=self._chat_history,
**generation_config
)
except Exception as e:
raise ValueError(f"Error during completion request, please check the credentials - {e}")
content = self._model.parse_generate_content_response(response)
self._chat_history.append({"role": "assistant", "content": content.text})
return content
def _adjust_generation_config(self, output_method: OutputMethod):
"""
Adjust the generation configuration based on the specified output method.
Args:
output_method (OutputMethod): The desired output method (e.g., default or JSON).
Returns:
dict: The adjusted configuration settings for generation.
"""
config = self._model.generation_config.to_json()
if output_method == OutputMethod.JSON:
config['temperature'] = 0
config['response_format'] = { "type": "json_object" }
return config
def delete_last_message(self):
"""
Deletes the last message exchange (user message and assistant response) from the chat history.
Preserves the system message if present.
Example:
Before:
[
{"role": "system", "content": "System message"},
{"role": "user", "content": "User message"},
{"role": "assistant", "content": "Assistant response"},
]
After:
[
{"role": "system", "content": "System message"},
]
Note: Does nothing if the chat history is empty or contains only a system message.
"""
# Keep at least the system message if present
min_length = 1 if self._model.system_instruction else 0
if len(self._chat_history) - 2 >= min_length:
self._chat_history.pop()
self._chat_history.pop()
else:
# Reset to initial state with just system message if present
self._chat_history = (
[{"role": "system", "content": self._model.system_instruction}]
if self._model.system_instruction is not None
else []
)