|
| 1 | +from typing import Optional |
| 2 | +from litellm import completion |
| 3 | +from .model import ( |
| 4 | + OutputMethod, |
| 5 | + GenerativeModel, |
| 6 | + GenerativeModelConfig, |
| 7 | + GenerationResponse, |
| 8 | + FinishReason, |
| 9 | + GenerativeModelChatSession, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class LiteLLMGenerativeModel(GenerativeModel): |
| 14 | + """ |
| 15 | + A generative model that interfaces with the LiteLLM for chat completions. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + model_name: str, |
| 21 | + generation_config: Optional[GenerativeModelConfig] = None, |
| 22 | + system_instruction: Optional[str] = None, |
| 23 | + ): |
| 24 | + self.model_name = model_name |
| 25 | + self.generation_config = generation_config or GenerativeModelConfig() |
| 26 | + self.system_instruction = system_instruction |
| 27 | + |
| 28 | + |
| 29 | + def with_system_instruction(self, system_instruction: str) -> "GenerativeModel": |
| 30 | + self.system_instruction = system_instruction |
| 31 | + return self |
| 32 | + |
| 33 | + def start_chat(self, args: Optional[dict] = None) -> GenerativeModelChatSession: |
| 34 | + return LiteLLMChatSession(self, args) |
| 35 | + |
| 36 | + def parse_generate_content_response(self, response: any) -> GenerationResponse: |
| 37 | + return GenerationResponse( |
| 38 | + text=response.choices[0].message.content, |
| 39 | + finish_reason=( |
| 40 | + FinishReason.STOP |
| 41 | + if response.choices[0].finish_reason == "stop" |
| 42 | + else ( |
| 43 | + FinishReason.MAX_TOKENS |
| 44 | + if response.choices[0].finish_reason == "length" |
| 45 | + else FinishReason.OTHER |
| 46 | + ) |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + def to_json(self) -> dict: |
| 51 | + return { |
| 52 | + "model_name": self.model_name, |
| 53 | + "generation_config": self.generation_config.to_json(), |
| 54 | + "system_instruction": self.system_instruction, |
| 55 | + } |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def from_json(json: dict) -> "GenerativeModel": |
| 59 | + return LiteLLMGenerativeModel( |
| 60 | + json["model_name"], |
| 61 | + generation_config=GenerativeModelConfig.from_json( |
| 62 | + json["generation_config"] |
| 63 | + ), |
| 64 | + system_instruction=json["system_instruction"], |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class LiteLLMChatSession(GenerativeModelChatSession): |
| 69 | + |
| 70 | + _history = [] |
| 71 | + |
| 72 | + def __init__(self, model: LiteLLMGenerativeModel, args: Optional[dict] = None): |
| 73 | + self._model = model |
| 74 | + self._args = args |
| 75 | + self._history = ( |
| 76 | + [{"role": "system", "content": self._model.system_instruction}] |
| 77 | + if self._model.system_instruction is not None |
| 78 | + else [] |
| 79 | + ) |
| 80 | + |
| 81 | + def send_message(self, message: str, output_method: OutputMethod = OutputMethod.DEFAULT) -> GenerationResponse: |
| 82 | + generation_config = self._get_generation_config(output_method) |
| 83 | + prompt = [] |
| 84 | + prompt.extend(self._history) |
| 85 | + prompt.append({"role": "user", "content": message[:14385]}) |
| 86 | + response = completion( |
| 87 | + model=self._model.model_name, |
| 88 | + messages=prompt, |
| 89 | + **generation_config |
| 90 | + ) |
| 91 | + content = self._model.parse_generate_content_response(response) |
| 92 | + self._history.append({"role": "user", "content": message}) |
| 93 | + self._history.append({"role": "assistant", "content": content.text}) |
| 94 | + return content |
| 95 | + |
| 96 | + def _get_generation_config(self, output_method: OutputMethod): |
| 97 | + config = self._model.generation_config.to_json() |
| 98 | + if output_method == OutputMethod.JSON: |
| 99 | + config['temperature'] = 0 |
| 100 | + config['response_format'] = { "type": "json_object" } |
| 101 | + |
| 102 | + return config |
| 103 | + |
| 104 | + def delete_last_message(self): |
| 105 | + """ |
| 106 | + Deletes the last message exchange (user message and assistant response) from the chat history. |
| 107 | + Preserves the system message if present. |
| 108 | + |
| 109 | + Example: |
| 110 | + Before: |
| 111 | + [ |
| 112 | + {"role": "system", "content": "System message"}, |
| 113 | + {"role": "user", "content": "User message"}, |
| 114 | + {"role": "assistant", "content": "Assistant response"}, |
| 115 | + ] |
| 116 | + After: |
| 117 | + [ |
| 118 | + {"role": "system", "content": "System message"}, |
| 119 | + ] |
| 120 | +
|
| 121 | + Note: Does nothing if the chat history is empty or contains only a system message. |
| 122 | + """ |
| 123 | + # Keep at least the system message if present |
| 124 | + min_length = 1 if self._model.system_instruction else 0 |
| 125 | + if len(self._history) - 2 >= min_length: |
| 126 | + self._history.pop() |
| 127 | + self._history.pop() |
| 128 | + else: |
| 129 | + # Reset to initial state with just system message if present |
| 130 | + self._history = ( |
| 131 | + [{"role": "system", "content": self._model.system_instruction}] |
| 132 | + if self._model.system_instruction is not None |
| 133 | + else [] |
| 134 | + ) |
0 commit comments