| 19 | ] |
| 20 | |
| 21 | class LiteLLMWrapper: |
| 22 | def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None): |
| 23 | self.api_key = api_key |
| 24 | self.base_url = base_url |
| 25 | self.chat = self.Chat() |
| 26 | # litellm.set_verbose=True |
| 27 | |
| 28 | class Chat: |
| 29 | class Completions: |
| 30 | @staticmethod |
| 31 | def create(model: str, messages: List[Dict[str, str]], **kwargs): |
| 32 | if model.startswith("gemini"): |
| 33 | response = completion(model=model, messages=messages, **kwargs, safety_settings=SAFETY_SETTINGS) |
| 34 | else: |
| 35 | response = completion(model=model, messages=messages, **kwargs) |
| 36 | # Convert LiteLLM response to match OpenAI response structure |
| 37 | return response |
| 38 | |
| 39 | completions = Completions() |
| 40 | |
| 41 | class Models: |
| 42 | @staticmethod |
| 43 | def list(): |
| 44 | try: |
| 45 | # Get all valid models from LiteLLM |
| 46 | valid_models = get_valid_models() |
| 47 | |
| 48 | # Format the response to match OpenAI's API format |
| 49 | model_list = [] |
| 50 | for model in valid_models: |
| 51 | model_list.append({ |
| 52 | "id": model, |
| 53 | "object": "model", |
| 54 | "created": int(time.time()), |
| 55 | "owned_by": "litellm" |
| 56 | }) |
| 57 | |
| 58 | return { |
| 59 | "object": "list", |
| 60 | "data": model_list |
| 61 | } |
| 62 | except Exception as e: |
| 63 | # Fallback to a basic list if there's an error |
| 64 | print(f"Error fetching LiteLLM models: {str(e)}") |
| 65 | return { |
| 66 | "object": "list", |
| 67 | "data": [ |
| 68 | {"id": "gpt-4o-mini", "object": "model", "created": int(time.time()), "owned_by": "litellm"}, |
| 69 | {"id": "gpt-4o", "object": "model", "created": int(time.time()), "owned_by": "litellm"}, |
| 70 | {"id": "command-nightly", "object": "model", "created": int(time.time()), "owned_by": "litellm"}, |
| 71 | {"id": "claude-3-opus-20240229", "object": "model", "created": int(time.time()), "owned_by": "litellm"}, |
| 72 | {"id": "claude-3-sonnet-20240229", "object": "model", "created": int(time.time()), "owned_by": "litellm"}, |
| 73 | {"id": "gemini-1.5-pro-latest", "object": "model", "created": int(time.time()), "owned_by": "litellm"} |
| 74 | ] |
| 75 | } |
| 76 | models = Models() |