|
10 | 10 | from langchain_core.prompts import ChatPromptTemplate |
11 | 11 |
|
12 | 12 | import logging |
13 | | - |
14 | | -logger = logging.getLogger(__name__) |
15 | | - |
| 13 | +from google import genai |
| 14 | +from django.conf import settings |
| 15 | +... |
16 | 16 | def list_gemini_models(): |
17 | 17 | """ |
18 | | - Lists available Gemini models that support content generation. |
| 18 | + Lists available Gemini models that support content generation using the new google-genai SDK. |
19 | 19 | """ |
20 | 20 | api_key = getattr(settings, 'GOOGLE_API_KEY', None) |
21 | 21 | if not api_key: |
22 | 22 | print("DEBUG: No GOOGLE_API_KEY found in settings.") |
23 | 23 | return [] |
24 | | - |
| 24 | + |
25 | 25 | try: |
26 | | - genai.configure(api_key=api_key) |
| 26 | + client = genai.Client(api_key=api_key) |
27 | 27 | models = [] |
28 | | - print("DEBUG: Fetching available Gemini models...") |
29 | | - for m in genai.list_models(): |
30 | | - # Print all models found for debugging |
31 | | - print(f"DEBUG: Found model: {m.name} (DisplayName: {m.display_name}, Methods: {m.supported_generation_methods})") |
32 | | - |
33 | | - if 'generateContent' in m.supported_generation_methods: |
34 | | - display_name = m.display_name |
35 | | - model_name = m.name |
| 28 | + print("DEBUG: Fetching available Gemini models with NEW SDK...") |
| 29 | + # The new SDK has a different way to list models |
| 30 | + for m in client.models.list(): |
| 31 | + print(f"DEBUG: Found model: {m.name} (Methods: {m.supported_generation_methods})") |
| 32 | + # We filter for models that support generating content |
| 33 | + # In the new SDK, supported_generation_methods might be represented differently |
| 34 | + # but usually it's still ['generateContent'] |
| 35 | + if 'generateContent' in m.supported_generation_methods or 'generate_content' in m.supported_generation_methods: |
36 | 36 | models.append({ |
37 | | - 'name': model_name, |
38 | | - 'display_name': display_name |
| 37 | + 'name': m.name, |
| 38 | + 'display_name': m.display_name or m.name |
39 | 39 | }) |
40 | | - |
| 40 | + |
41 | 41 | print(f"DEBUG: Returning {len(models)} eligible models.") |
42 | 42 | return models |
43 | 43 | except Exception as e: |
44 | | - print(f"DEBUG: Error listing Gemini models: {str(e)}") |
45 | | - return [] |
| 44 | + print(f"DEBUG: Error listing Gemini models with NEW SDK: {str(e)}") |
| 45 | + # Fallback to a hardcoded list if the listing fails |
| 46 | + return [ |
| 47 | + {'name': 'models/gemini-1.5-flash', 'display_name': 'Gemini 1.5 Flash'}, |
| 48 | + {'name': 'models/gemini-1.5-pro', 'display_name': 'Gemini 1.5 Pro'}, |
| 49 | + ] |
46 | 50 |
|
47 | 51 | def process_pdf(file_path, vector_store_path): |
48 | 52 | """ |
|
0 commit comments