GuglielmoTor commited on
Commit
24f43be
·
verified ·
1 Parent(s): 09bc280

Update chatbot_handler.py

Browse files
Files changed (1) hide show
  1. chatbot_handler.py +87 -54
chatbot_handler.py CHANGED
@@ -1,27 +1,27 @@
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"},
@@ -31,21 +31,13 @@ common_safety_settings = [
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:
@@ -67,75 +59,116 @@ def format_history_for_gemini(gradio_chat_history: list) -> list:
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__}."
 
1
  # chatbot_handler.py
2
  import logging
3
  import json
4
+ from google import genai
5
+ from google.genai import types as genai_types # Import types for GenerateContentConfig
6
  import os
7
+ import asyncio
8
 
9
  # Gemini API key configuration
10
  GEMINI_API_KEY = os.getenv('GEMINI_API_KEY', '')
11
 
12
  client = None
 
13
  model_name = "gemini-1.5-flash-latest" # Using a more recent Flash model
14
+ # model_name = "gemini-2.0-flash" # As per user's documentation snippet, ensure this model is available with their API key type
15
 
16
+ # This will be used to create genai_types.GenerateContentConfig
17
+ generation_config_params = {
18
  "temperature": 0.7,
19
  "top_p": 1,
20
  "top_k": 1,
21
  "max_output_tokens": 2048,
22
  }
23
 
24
+ # Safety settings list
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"},
 
31
 
32
  try:
33
  if GEMINI_API_KEY:
34
+ # Initialize client using genai.Client as per user's documentation and error
35
+ client = genai.Client(api_key=GEMINI_API_KEY)
36
+ logging.info(f"Gemini client (genai.Client) initialized. Target model for generation: '{model_name}'")
 
 
 
 
 
 
 
 
37
  else:
38
  logging.error("Gemini API Key is not set.")
39
  except Exception as e:
40
+ logging.error(f"Failed to initialize Gemini client (genai.Client): {e}", exc_info=True)
41
 
42
 
43
  def format_history_for_gemini(gradio_chat_history: list) -> list:
 
59
  logging.warning(f"Skipping complex but empty content part in chat history: {content}")
60
  else:
61
  logging.warning(f"Skipping non-string/non-standard content in chat history: {content}")
62
+ # For the older client.models.generate_content, the 'contents' is typically a list of strings or multimodal parts,
63
+ # not a list of role-based dicts. The role-based dicts are for chat history with newer .start_chat().send_message().
64
+ # The user's example shows: contents=["Explain how AI works"]
65
+ # If the history is to be used, it needs to be formatted as a flat list of alternating user/model prompts for some older chat patterns,
66
+ # or the API might only take the latest user message if not using a dedicated chat session object.
67
+ # Given the `client.models.generate_content` structure, we might need to adjust how history is passed.
68
+ # For now, let's assume gemini_formatted_history is what `contents` expects, or it should be just the latest user message.
69
+ # The documentation for client.models.generate_content shows `contents` can be a list of parts.
70
+ # Let's re-evaluate: if chat_history_for_plot is a list of {"role": ..., "parts": ...},
71
+ # client.models.generate_content might expect `contents` to be just the parts of the last user message,
72
+ # or a more complex structure if it supports multi-turn via this method directly.
73
+ # The example `contents=[image, "Tell me about this instrument"]` suggests a list of content parts.
74
+ # Let's assume for now that the `gemini_formatted_history` (which is a list of {"role": ..., "parts": ...})
75
+ # is the correct format for the `contents` argument if the SDK version handles it.
76
+ # If not, this function or its usage in generate_llm_response will need adjustment.
77
+ # For a simple non-chat scenario, contents would be like: `[{"parts": [{"text": user_message}]}]`
78
+ # For a multi-turn conversation, the `contents` parameter for `generate_content`
79
+ # expects a list of `Content` objects (or dicts that can be cast to them).
80
+ # Each `Content` object has 'role' and 'parts'.
81
+ # So, the current `format_history_for_gemini` output *should* be correct.
82
  return gemini_contents
83
 
84
 
85
  async def generate_llm_response(user_message: str, plot_id: str, plot_label: str, chat_history_for_plot: list, plot_data_summary: str = None):
86
  if not client:
87
+ logging.error("Gemini client (genai.Client) not initialized.")
88
  return "The AI model is not available. Configuration error."
89
 
90
+ # gemini_formatted_history will be a list of {"role": ..., "parts": ...} dicts
91
  gemini_formatted_history = format_history_for_gemini(chat_history_for_plot)
92
 
93
+ if not gemini_formatted_history: # Should not happen if chat_history_for_plot has at least one message
94
+ logging.error("Formatted history for Gemini is empty.")
95
+ return "There was an issue processing the conversation history (empty)."
96
+
97
+ # Ensure the last message has text if it's the only one (e.g. initial prompt)
98
+ if not any(part.get("text","").strip() for message in gemini_formatted_history for part in message.get("parts",[])):
99
+ logging.error("Formatted history for Gemini contains no text parts.")
100
+ return "There was an issue processing the conversation history for the AI model (empty text)."
101
 
102
  try:
103
  response = None
104
+ # We are now certain we need to use client.models.generate_content
105
+ if hasattr(client, 'models') and hasattr(client.models, 'generate_content'):
106
+ logging.debug(f"Using genai.Client.models.generate_content for model '{model_name}' (synchronous via asyncio.to_thread)")
107
+
108
+ # The model name for client.models.generate_content should not be prefixed with "models/"
109
+ # if it's like "gemini-1.5-flash-latest" or "gemini-2.0-flash".
110
+ # If your model_name is already "models/gemini-1.5-flash-latest", then it's fine.
111
+ # Let's assume model_name is like "gemini-1.5-flash-latest"
112
+ effective_model_name = model_name
113
+ if not model_name.startswith("models/"): # Ensure it's not like "models/models/gemini..."
114
+ effective_model_name = f"models/{model_name}" # Prepend "models/" if not already there
115
+
116
+ # Create the GenerateContentConfig object from our parameters
117
+ gen_config_obj = genai_types.GenerateContentConfig(**generation_config_params)
118
+
119
+ response = await asyncio.to_thread(
120
+ client.models.generate_content,
121
+ model=effective_model_name, # Pass the model name string
122
+ contents=gemini_formatted_history, # This should be the list of Content dicts
123
+ generation_config=gen_config_obj,
124
+ safety_settings=common_safety_settings
125
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  else:
127
+ logging.error(f"Gemini client (genai.Client) does not have 'models.generate_content' method. Type: {type(client)}")
128
+ return "AI model interaction error (SDK method not found)."
129
 
130
+ # Process response (this part should be largely consistent)
131
  if hasattr(response, 'prompt_feedback') and response.prompt_feedback and response.prompt_feedback.block_reason:
132
  reason = response.prompt_feedback.block_reason
133
+ reason_name = getattr(reason, 'name', str(reason)) # .name might not exist
134
  logging.warning(f"Blocked by prompt feedback: {reason_name}")
135
  return f"Blocked due to content policy: {reason_name}."
136
 
137
+ # The user's documentation example uses `response.text` directly.
138
+ # This implies the response object from `client.models.generate_content` might be simpler.
139
+ # Let's check for `response.text` first.
140
+ if hasattr(response, 'text') and response.text:
141
+ logging.debug("Response has a direct .text attribute.")
142
+ return response.text
143
+
144
+ # Fallback to candidates structure if .text is not available or empty
145
+ logging.debug("Response does not have a direct .text attribute or it's empty, checking candidates.")
146
  if response.candidates and response.candidates[0].content and response.candidates[0].content.parts:
147
  return "".join(part.text for part in response.candidates[0].content.parts if hasattr(part, 'text'))
148
 
149
  finish_reason = "UNKNOWN"
150
  if response.candidates and response.candidates[0].finish_reason:
151
  finish_reason_val = response.candidates[0].finish_reason
152
+ finish_reason = getattr(finish_reason_val, 'name', str(finish_reason_val)) # .name might not exist
153
 
154
+ if not (hasattr(response, 'text') and response.text) and \
155
+ not (response.candidates and response.candidates[0].content and response.candidates[0].content.parts):
156
+ logging.warning(f"No content parts in response and no direct .text. Finish reason: {finish_reason}")
157
+ if finish_reason == "SAFETY": # Or other relevant finish reasons
158
  return f"Response generation stopped due to safety reasons. Finish reason: {finish_reason}."
159
  return f"The AI model returned an empty response. Finish reason: {finish_reason}."
160
 
161
+ # If we reach here, it means .text was empty and candidates structure was also empty/problematic
162
+ return f"Unexpected response structure from AI model (checked .text and .candidates). Finish reason: {finish_reason}."
163
 
164
  except AttributeError as ae:
165
  logging.error(f"AttributeError during Gemini call for plot '{plot_label}': {ae}", exc_info=True)
 
 
166
  return f"AI model error (Attribute): {type(ae).__name__} - {ae}."
167
  except Exception as e:
168
  logging.error(f"Error generating response for plot '{plot_label}': {e}", exc_info=True)
169
+ # Check for specific API errors if possible
170
+ if "API_KEY_INVALID" in str(e) or "API key not valid" in str(e):
171
+ return "AI model error: API key is not valid. Please check configuration."
172
+ if "400" in str(e) and "model" in str(e).lower() and "not found" in str(e).lower(): # Example for model not found
173
+ return f"AI model error: Model '{model_name}' not found or not accessible with your API key."
174
  return f"An unexpected error occurred while contacting the AI model: {type(e).__name__}."