|
1 | 1 | import os |
| 2 | +import time |
2 | 3 | import litellm |
3 | 4 | from litellm import completion |
| 5 | +from litellm.utils import get_valid_models |
4 | 6 | from typing import List, Dict, Any, Optional |
5 | 7 |
|
| 8 | +# Configure litellm to drop unsupported parameters |
| 9 | +litellm.drop_params = True |
| 10 | + |
6 | 11 | SAFETY_SETTINGS = [ |
7 | 12 | {"category": cat, "threshold": "BLOCK_NONE"} |
8 | 13 | for cat in [ |
@@ -36,16 +41,36 @@ def create(model: str, messages: List[Dict[str, str]], **kwargs): |
36 | 41 | class Models: |
37 | 42 | @staticmethod |
38 | 43 | def list(): |
39 | | - # Since LiteLLM doesn't have a direct method to list models, |
40 | | - # we'll return a predefined list of supported models. |
41 | | - # This list can be expanded as needed. |
42 | | - return { |
43 | | - "data": [ |
44 | | - {"id": "gpt-4o-mini"}, |
45 | | - {"id": "gpt-4o"}, |
46 | | - {"id": "command-nightly"}, |
47 | | - # Add more models as needed |
48 | | - ] |
49 | | - } |
50 | | - |
| 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 | + } |
51 | 76 | models = Models() |
0 commit comments