Skip to content

Commit 80462dc

Browse files
Dev NirwalDev Nirwal
authored andcommitted
Feature: switch to new google-genai SDK and add model listing fallback
1 parent 6a84eb4 commit 80462dc

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

chat/utils.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,43 @@
1010
from langchain_core.prompts import ChatPromptTemplate
1111

1212
import logging
13-
14-
logger = logging.getLogger(__name__)
15-
13+
from google import genai
14+
from django.conf import settings
15+
...
1616
def list_gemini_models():
1717
"""
18-
Lists available Gemini models that support content generation.
18+
Lists available Gemini models that support content generation using the new google-genai SDK.
1919
"""
2020
api_key = getattr(settings, 'GOOGLE_API_KEY', None)
2121
if not api_key:
2222
print("DEBUG: No GOOGLE_API_KEY found in settings.")
2323
return []
24-
24+
2525
try:
26-
genai.configure(api_key=api_key)
26+
client = genai.Client(api_key=api_key)
2727
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:
3636
models.append({
37-
'name': model_name,
38-
'display_name': display_name
37+
'name': m.name,
38+
'display_name': m.display_name or m.name
3939
})
40-
40+
4141
print(f"DEBUG: Returning {len(models)} eligible models.")
4242
return models
4343
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+
]
4650

4751
def process_pdf(file_path, vector_store_path):
4852
"""

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ django-environ
99
gunicorn
1010
psycopg2-binary
1111
google-generativeai
12+
google-genai

0 commit comments

Comments
 (0)