Spaces:
Running
Running
Update chatbot_handler.py
Browse files- chatbot_handler.py +101 -44
chatbot_handler.py
CHANGED
@@ -1,84 +1,141 @@
|
|
1 |
# chatbot_handler.py
|
2 |
import logging
|
3 |
import json
|
4 |
-
from google import genai
|
5 |
import os
|
|
|
6 |
|
7 |
# Gemini API key configuration
|
8 |
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY', '')
|
9 |
|
10 |
client = None
|
11 |
-
model_name = "gemini-2.0-flash"
|
|
|
12 |
safety_settings = []
|
13 |
|
14 |
-
generation_config = genai.types.GenerationConfig(
|
15 |
-
temperature=0.7,
|
16 |
-
top_k=1,
|
17 |
-
top_p=1,
|
18 |
-
max_output_tokens=2048,
|
19 |
-
)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
logging.error("Gemini API Key is not set.")
|
36 |
except Exception as e:
|
37 |
-
logging.error(f"Failed to initialize Gemini client: {e}", exc_info=True)
|
38 |
|
39 |
|
40 |
def format_history_for_gemini(gradio_chat_history: list) -> list:
|
|
|
41 |
gemini_contents = []
|
42 |
for msg in gradio_chat_history:
|
43 |
-
role = "user" if msg
|
44 |
content = msg.get("content")
|
45 |
if isinstance(content, str):
|
46 |
gemini_contents.append({"role": role, "parts": [{"text": content}]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
else:
|
48 |
-
logging.warning(f"Skipping non-string content in chat history: {content}")
|
49 |
return gemini_contents
|
50 |
|
51 |
|
52 |
async def generate_llm_response(user_message: str, plot_id: str, plot_label: str, chat_history_for_plot: list, plot_data_summary: str = None):
|
53 |
if not client:
|
54 |
-
logging.error("Gemini client not initialized.")
|
55 |
return "The AI model is not available. Configuration error."
|
56 |
|
57 |
gemini_formatted_history = format_history_for_gemini(chat_history_for_plot)
|
58 |
|
59 |
if not gemini_formatted_history:
|
60 |
-
|
61 |
-
|
|
|
62 |
|
63 |
try:
|
64 |
-
response =
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
except Exception as e:
|
83 |
logging.error(f"Error generating response for plot '{plot_label}': {e}", exc_info=True)
|
84 |
-
|
|
|
|
|
|
1 |
# chatbot_handler.py
|
2 |
import logging
|
3 |
import json
|
4 |
+
from google import genai # Assuming this is the correct SDK
|
5 |
import os
|
6 |
+
import asyncio # Added for asyncio.to_thread
|
7 |
|
8 |
# Gemini API key configuration
|
9 |
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY', '')
|
10 |
|
11 |
client = None
|
12 |
+
# model_name = "gemini-1.0-pro" # Or your preferred model like "gemini-2.0-flash"
|
13 |
+
model_name = "gemini-1.5-flash-latest" # Using a more recent Flash model
|
14 |
safety_settings = []
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
generation_config = { # New SDK style
|
18 |
+
"temperature": 0.7,
|
19 |
+
"top_p": 1,
|
20 |
+
"top_k": 1,
|
21 |
+
"max_output_tokens": 2048,
|
22 |
+
}
|
23 |
|
24 |
+
# Define safety settings list to be used by both client types
|
25 |
+
common_safety_settings = [
|
26 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
27 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
28 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
29 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
30 |
+
]
|
31 |
|
32 |
+
try:
|
33 |
+
if GEMINI_API_KEY:
|
34 |
+
if hasattr(genai, 'Client'): # Check for older SDK structure
|
35 |
+
client = genai.Client(api_key=GEMINI_API_KEY)
|
36 |
+
logging.info(f"Gemini client (genai.Client) initialized with model '{model_name}' for older SDK structure.")
|
37 |
+
else: # Fallback to current recommended practice (genai.GenerativeModel)
|
38 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
39 |
+
client = genai.GenerativeModel(
|
40 |
+
model_name=model_name,
|
41 |
+
safety_settings=common_safety_settings,
|
42 |
+
generation_config=generation_config
|
43 |
+
)
|
44 |
+
logging.info(f"Gemini client (genai.GenerativeModel) initialized with model '{model_name}'")
|
45 |
else:
|
46 |
logging.error("Gemini API Key is not set.")
|
47 |
except Exception as e:
|
48 |
+
logging.error(f"Failed to initialize Gemini client/model: {e}", exc_info=True)
|
49 |
|
50 |
|
51 |
def format_history_for_gemini(gradio_chat_history: list) -> list:
|
52 |
+
"""Converts Gradio chat history to Gemini content format."""
|
53 |
gemini_contents = []
|
54 |
for msg in gradio_chat_history:
|
55 |
+
role = "user" if msg.get("role") == "user" else "model"
|
56 |
content = msg.get("content")
|
57 |
if isinstance(content, str):
|
58 |
gemini_contents.append({"role": role, "parts": [{"text": content}]})
|
59 |
+
elif isinstance(content, list) and len(content) > 0 and isinstance(content[0], dict) and "type" in content[0]:
|
60 |
+
parts = []
|
61 |
+
for part_item in content:
|
62 |
+
if part_item.get("type") == "text":
|
63 |
+
parts.append({"text": part_item.get("text", "")})
|
64 |
+
if parts:
|
65 |
+
gemini_contents.append({"role": role, "parts": parts})
|
66 |
+
else:
|
67 |
+
logging.warning(f"Skipping complex but empty content part in chat history: {content}")
|
68 |
else:
|
69 |
+
logging.warning(f"Skipping non-string/non-standard content in chat history: {content}")
|
70 |
return gemini_contents
|
71 |
|
72 |
|
73 |
async def generate_llm_response(user_message: str, plot_id: str, plot_label: str, chat_history_for_plot: list, plot_data_summary: str = None):
|
74 |
if not client:
|
75 |
+
logging.error("Gemini client/model not initialized.")
|
76 |
return "The AI model is not available. Configuration error."
|
77 |
|
78 |
gemini_formatted_history = format_history_for_gemini(chat_history_for_plot)
|
79 |
|
80 |
if not gemini_formatted_history:
|
81 |
+
if not any(part.get("text", "").strip() for message in gemini_formatted_history for part in message.get("parts",[])):
|
82 |
+
logging.error("Formatted history for Gemini is empty or contains no text.")
|
83 |
+
return "There was an issue processing the conversation history for the AI model (empty text)."
|
84 |
|
85 |
try:
|
86 |
+
response = None
|
87 |
+
if isinstance(client, genai.GenerativeModel):
|
88 |
+
logging.debug("Using genai.GenerativeModel.generate_content_async")
|
89 |
+
response = await client.generate_content_async(
|
90 |
+
contents=gemini_formatted_history
|
91 |
+
)
|
92 |
+
elif hasattr(client, 'models') and hasattr(client.models, 'generate_content'): # Check for the synchronous method
|
93 |
+
logging.debug("Using genai.Client.models.generate_content (synchronous via asyncio.to_thread)")
|
94 |
+
qualified_model_name = model_name if model_name.startswith("models/") else f"models/{model_name}"
|
95 |
+
|
96 |
+
# Ensure safety_settings and generation_config are passed correctly
|
97 |
+
# to the synchronous method if it's part of this older client structure.
|
98 |
+
# The `client.models.generate_content` might take these as direct args.
|
99 |
+
response = await asyncio.to_thread(
|
100 |
+
client.models.generate_content, # The synchronous function
|
101 |
+
model=qualified_model_name,
|
102 |
+
contents=gemini_formatted_history,
|
103 |
+
generation_config=generation_config, # Pass the dict directly
|
104 |
+
safety_settings=common_safety_settings # Pass the list of dicts
|
105 |
+
)
|
106 |
+
else:
|
107 |
+
logging.error(f"Gemini client is not a recognized type for generating content. Type: {type(client)}")
|
108 |
+
return "AI model interaction error (client type)."
|
109 |
+
|
110 |
+
if hasattr(response, 'prompt_feedback') and response.prompt_feedback and response.prompt_feedback.block_reason:
|
111 |
+
reason = response.prompt_feedback.block_reason
|
112 |
+
reason_name = getattr(reason, 'name', str(reason))
|
113 |
+
logging.warning(f"Blocked by prompt feedback: {reason_name}")
|
114 |
+
return f"Blocked due to content policy: {reason_name}."
|
115 |
+
|
116 |
+
if response.candidates and response.candidates[0].content and response.candidates[0].content.parts:
|
117 |
+
return "".join(part.text for part in response.candidates[0].content.parts if hasattr(part, 'text'))
|
118 |
+
|
119 |
+
finish_reason = "UNKNOWN"
|
120 |
+
if response.candidates and response.candidates[0].finish_reason:
|
121 |
+
finish_reason_val = response.candidates[0].finish_reason
|
122 |
+
finish_reason = getattr(finish_reason_val, 'name', str(finish_reason_val))
|
123 |
+
|
124 |
+
if not (response.candidates and response.candidates[0].content and response.candidates[0].content.parts):
|
125 |
+
logging.warning(f"No content parts in response. Finish reason: {finish_reason}")
|
126 |
+
if finish_reason == "SAFETY":
|
127 |
+
return f"Response generation stopped due to safety reasons. Finish reason: {finish_reason}."
|
128 |
+
return f"The AI model returned an empty response. Finish reason: {finish_reason}."
|
129 |
+
|
130 |
+
return f"Unexpected response structure from AI model. Finish reason: {finish_reason}."
|
131 |
+
|
132 |
+
except AttributeError as ae:
|
133 |
+
logging.error(f"AttributeError during Gemini call for plot '{plot_label}': {ae}", exc_info=True)
|
134 |
+
if "generate_content_async" in str(ae) or "generate_content" in str(ae):
|
135 |
+
return f"AI model error: SDK method not found or mismatch. Details: {ae}"
|
136 |
+
return f"AI model error (Attribute): {type(ae).__name__} - {ae}."
|
137 |
except Exception as e:
|
138 |
logging.error(f"Error generating response for plot '{plot_label}': {e}", exc_info=True)
|
139 |
+
if "API key not valid" in str(e):
|
140 |
+
return "AI model error: API key is not valid. Please check configuration."
|
141 |
+
return f"An unexpected error occurred while contacting the AI model: {type(e).__name__}."
|