fix: update temperature parameter to 0.6 across multiple providers and add debugging output
This commit is contained in:
@@ -124,37 +124,46 @@ def get_content(response: GenerateContentResponse | dict[str, Any]) -> str:
|
||||
The concatenated text content, or an error message string.
|
||||
"""
|
||||
try:
|
||||
# Handle error dictionary case
|
||||
# Check if it's an error dictionary passed from upstream (e.g., completion helper)
|
||||
if isinstance(response, dict) and "error" in response:
|
||||
logger.error(f"Cannot get content from error response: {response['error']}")
|
||||
logger.error(f"Cannot get content from error dict: {response['error']}")
|
||||
return f"[Error: {response['error']}]"
|
||||
|
||||
# Handle successful GenerateContentResponse object
|
||||
if hasattr(response, "text"):
|
||||
# The `.text` attribute usually provides the concatenated text content directly
|
||||
# Ensure it's a GenerateContentResponse object before accessing attributes
|
||||
if not isinstance(response, GenerateContentResponse):
|
||||
logger.error(f"Cannot get content: Expected GenerateContentResponse or error dict, got {type(response)}")
|
||||
return f"[Error: Unexpected response type {type(response)}]"
|
||||
|
||||
# --- Access GenerateContentResponse attributes ---
|
||||
# Prioritize response.text if available and not empty
|
||||
if hasattr(response, "text") and response.text:
|
||||
content = response.text
|
||||
logger.debug(f"Extracted content (length {len(content)}) from response.text.")
|
||||
return content
|
||||
elif hasattr(response, "candidates") and response.candidates:
|
||||
# Fallback: manually concatenate text from parts if .text is missing
|
||||
|
||||
# Fallback: manually concatenate text from parts if .text is missing/empty
|
||||
if hasattr(response, "candidates") and response.candidates:
|
||||
first_candidate = response.candidates[0]
|
||||
if hasattr(first_candidate, "content") and hasattr(first_candidate.content, "parts"):
|
||||
text_parts = []
|
||||
for part in first_candidate.content.parts:
|
||||
if hasattr(part, "text"):
|
||||
text_parts.append(part.text)
|
||||
# We are only interested in text content here, ignore function calls etc.
|
||||
content = "".join(text_parts)
|
||||
logger.debug(f"Extracted content (length {len(content)}) from response candidates' parts.")
|
||||
return content
|
||||
# Check candidate content and parts carefully
|
||||
if hasattr(first_candidate, "content") and first_candidate.content and hasattr(first_candidate.content, "parts") and first_candidate.content.parts:
|
||||
text_parts = [part.text for part in first_candidate.content.parts if hasattr(part, "text")]
|
||||
if text_parts:
|
||||
content = "".join(text_parts)
|
||||
logger.debug(f"Extracted content (length {len(content)}) from response candidate parts.")
|
||||
return content
|
||||
else:
|
||||
logger.warning("Google response candidate parts contained no text.")
|
||||
return "" # Return empty if parts exist but have no text
|
||||
else:
|
||||
logger.warning("Google response candidate has no content or parts.")
|
||||
return "" # Return empty string if no text found
|
||||
logger.warning("Google response candidate has no valid content or parts.")
|
||||
return "" # Return empty string if no valid content/parts
|
||||
else:
|
||||
logger.warning(f"Could not extract content from Google response: No 'text' or valid 'candidates'. Response type: {type(response)}")
|
||||
# If neither .text nor valid candidates are found
|
||||
logger.warning(f"Could not extract content from Google response: No .text or valid candidates found. Response: {response}")
|
||||
return "" # Return empty string if no text found
|
||||
|
||||
except AttributeError as ae:
|
||||
logger.error(f"Attribute error extracting content from Google response: {ae}. Response object: {response}", exc_info=True)
|
||||
logger.error(f"Attribute error extracting content from Google response: {ae}. Response type: {type(response)}", exc_info=True)
|
||||
return f"[Error extracting content: Attribute missing - {str(ae)}]"
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error extracting content from Google response: {e}", exc_info=True)
|
||||
@@ -173,32 +182,34 @@ def get_usage(response: GenerateContentResponse | dict[str, Any]) -> dict[str, i
|
||||
usage information is unavailable or an error occurred.
|
||||
"""
|
||||
try:
|
||||
# Handle error dictionary case
|
||||
# Check if it's an error dictionary passed from upstream
|
||||
if isinstance(response, dict) and "error" in response:
|
||||
logger.warning("Cannot get usage from error response.")
|
||||
logger.warning(f"Cannot get usage from error dict: {response['error']}")
|
||||
return None
|
||||
|
||||
# Check for usage metadata in the response object
|
||||
if hasattr(response, "usage_metadata"):
|
||||
metadata = response.usage_metadata
|
||||
# Ensure it's a GenerateContentResponse object before accessing attributes
|
||||
if not isinstance(response, GenerateContentResponse):
|
||||
logger.warning(f"Cannot get usage: Expected GenerateContentResponse or error dict, got {type(response)}")
|
||||
return None
|
||||
|
||||
# Safely access usage metadata
|
||||
metadata = getattr(response, "usage_metadata", None)
|
||||
if metadata:
|
||||
# Google uses prompt_token_count and candidates_token_count
|
||||
prompt_tokens = getattr(metadata, "prompt_token_count", 0)
|
||||
completion_tokens = getattr(metadata, "candidates_token_count", 0)
|
||||
usage = {
|
||||
"prompt_tokens": getattr(metadata, "prompt_token_count", 0),
|
||||
"completion_tokens": getattr(metadata, "candidates_token_count", 0),
|
||||
# Google also provides total_token_count, could be added if needed
|
||||
# "total_tokens": getattr(metadata, "total_token_count", 0),
|
||||
"prompt_tokens": int(prompt_tokens),
|
||||
"completion_tokens": int(completion_tokens),
|
||||
}
|
||||
# Ensure values are integers
|
||||
usage = {k: int(v) for k, v in usage.items()}
|
||||
logger.debug(f"Extracted usage from Google response metadata: {usage}")
|
||||
return usage
|
||||
else:
|
||||
# Log a warning only if it's not clearly an error dict already handled
|
||||
if not (isinstance(response, dict) and "error" in response):
|
||||
logger.warning(f"Could not extract usage from Google response object of type {type(response)}. No 'usage_metadata' attribute found.")
|
||||
logger.warning(f"Could not extract usage from Google response object: No 'usage_metadata' attribute found. Response: {response}")
|
||||
return None
|
||||
|
||||
except AttributeError as ae:
|
||||
logger.error(f"Attribute error extracting usage from Google response: {ae}. Response object: {response}", exc_info=True)
|
||||
logger.error(f"Attribute error extracting usage from Google response: {ae}. Response type: {type(response)}", exc_info=True)
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error extracting usage from Google response: {e}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user