Fixes and improvements.

This commit is contained in:
OK
2025-08-09 00:16:37 +02:00
parent 38f0479d1e
commit d742ab86fa
22 changed files with 529 additions and 457 deletions
+74 -58
View File
@@ -1,6 +1,6 @@
import logging
from functools import cache
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional
import requests
@@ -99,33 +99,43 @@ class IGDBQuery(object):
"""
if not query or not query.strip():
return None
try:
# Search for games with fuzzy matching
games = self.generalized_igdb_query(
{"name": query.strip()},
"games",
[
"id", "name", "summary", "storyline", "rating", "aggregated_rating",
"first_release_date", "genres.name", "platforms.name", "developers.name",
"publishers.name", "game_modes.name", "themes.name", "cover.url"
"id",
"name",
"summary",
"storyline",
"rating",
"aggregated_rating",
"first_release_date",
"genres.name",
"platforms.name",
"involved_companies.company.name",
"game_modes.name",
"themes.name",
"cover.url",
],
additional_filters={"category": "= 0"}, # Main games only
limit=limit
limit=limit,
)
if not games:
return None
# Format games for AI consumption
formatted_games = []
for game in games:
formatted_game = self._format_game_for_ai(game)
if formatted_game:
formatted_games.append(formatted_game)
return formatted_games if formatted_games else None
except Exception as e:
logging.error(f"Error searching games for query '{query}': {e}")
return None
@@ -139,22 +149,37 @@ class IGDBQuery(object):
{},
"games",
[
"id", "name", "summary", "storyline", "rating", "aggregated_rating",
"first_release_date", "genres.name", "platforms.name", "developers.name",
"publishers.name", "game_modes.name", "themes.name", "keywords.name",
"similar_games.name", "cover.url", "screenshots.url", "videos.video_id",
"release_dates.date", "release_dates.platform.name", "age_ratings.rating"
"id",
"name",
"summary",
"storyline",
"rating",
"aggregated_rating",
"first_release_date",
"genres.name",
"platforms.name",
"involved_companies.company.name",
"game_modes.name",
"themes.name",
"keywords.name",
"similar_games.name",
"cover.url",
"screenshots.url",
"videos.video_id",
"release_dates.date",
"release_dates.platform.name",
"age_ratings.rating",
],
additional_filters={"id": f"= {game_id}"},
limit=1
limit=1,
)
if games and len(games) > 0:
return self._format_game_for_ai(games[0], detailed=True)
except Exception as e:
logging.error(f"Error getting game details for ID {game_id}: {e}")
return None
def _format_game_for_ai(self, game_data: Dict[str, Any], detailed: bool = False) -> Dict[str, Any]:
@@ -162,60 +187,56 @@ class IGDBQuery(object):
Format game data in a way that's easy for AI to understand and present to users.
"""
try:
formatted = {
"name": game_data.get("name", "Unknown"),
"summary": game_data.get("summary", "No summary available")
}
formatted = {"name": game_data.get("name", "Unknown"), "summary": game_data.get("summary", "No summary available")}
# Add basic info
if "rating" in game_data:
formatted["rating"] = f"{game_data['rating']:.1f}/100"
if "aggregated_rating" in game_data:
formatted["user_rating"] = f"{game_data['aggregated_rating']:.1f}/100"
# Release information
if "first_release_date" in game_data:
import datetime
release_date = datetime.datetime.fromtimestamp(game_data["first_release_date"])
formatted["release_year"] = release_date.year
if detailed:
formatted["release_date"] = release_date.strftime("%Y-%m-%d")
# Platforms
if "platforms" in game_data and game_data["platforms"]:
platforms = [p.get("name", "") for p in game_data["platforms"] if p.get("name")]
formatted["platforms"] = platforms[:5] # Limit to prevent overflow
# Genres
# Genres
if "genres" in game_data and game_data["genres"]:
genres = [g.get("name", "") for g in game_data["genres"] if g.get("name")]
formatted["genres"] = genres
# Developers
if "developers" in game_data and game_data["developers"]:
developers = [d.get("name", "") for d in game_data["developers"] if d.get("name")]
formatted["developers"] = developers[:3] # Limit for readability
# Publishers
if "publishers" in game_data and game_data["publishers"]:
publishers = [p.get("name", "") for p in game_data["publishers"] if p.get("name")]
formatted["publishers"] = publishers[:2]
# Companies (developers/publishers)
if "involved_companies" in game_data and game_data["involved_companies"]:
companies = []
for company_data in game_data["involved_companies"]:
if "company" in company_data and "name" in company_data["company"]:
companies.append(company_data["company"]["name"])
formatted["companies"] = companies[:5] # Limit for readability
if detailed:
# Add more detailed info for specific requests
if "storyline" in game_data and game_data["storyline"]:
formatted["storyline"] = game_data["storyline"]
if "game_modes" in game_data and game_data["game_modes"]:
modes = [m.get("name", "") for m in game_data["game_modes"] if m.get("name")]
formatted["game_modes"] = modes
if "themes" in game_data and game_data["themes"]:
themes = [t.get("name", "") for t in game_data["themes"] if t.get("name")]
formatted["themes"] = themes
return formatted
except Exception as e:
logging.error(f"Error formatting game data: {e}")
return {"name": game_data.get("name", "Unknown"), "summary": "Error retrieving game information"}
@@ -234,30 +255,25 @@ class IGDBQuery(object):
"properties": {
"query": {
"type": "string",
"description": "The game name or search query (e.g., 'Elden Ring', 'Mario', 'Zelda Breath of the Wild')"
"description": "The game name or search query (e.g., 'Elden Ring', 'Mario', 'Zelda Breath of the Wild')",
},
"limit": {
"type": "integer",
"type": "integer",
"description": "Maximum number of games to return (default: 5, max: 10)",
"minimum": 1,
"maximum": 10
}
"maximum": 10,
},
},
"required": ["query"]
}
"required": ["query"],
},
},
{
"name": "get_game_details",
"description": "Get detailed information about a specific game when you have its ID from a previous search.",
"parameters": {
"type": "object",
"properties": {
"game_id": {
"type": "integer",
"description": "The IGDB game ID from a previous search result"
}
},
"required": ["game_id"]
}
}
"properties": {"game_id": {"type": "integer", "description": "The IGDB game ID from a previous search result"}},
"required": ["game_id"],
},
},
]
-2
View File
@@ -68,7 +68,5 @@ class LeonardoAIDrawMixIn(AIResponderBase):
return image_bytes
except Exception as err:
logging.warning(f"Failed to generate image, sleep for {error_sleep}s: {repr(description)}\n{repr(err)}")
else:
logging.warning(f"Failed to generate image, sleep for {error_sleep}s: {repr(description)}")
await asyncio.sleep(error_sleep)
raise RuntimeError(f"Failed to generate image {repr(description)}")
+158 -56
View File
@@ -29,21 +29,25 @@ class OpenAIResponder(AIResponder, LeonardoAIDrawMixIn):
def __init__(self, config: Dict[str, Any], channel: Optional[str] = None) -> None:
super().__init__(config, channel)
self.client = openai.AsyncOpenAI(api_key=self.config.get("openai-token", self.config.get("openai-key", "")))
# Initialize IGDB if enabled
self.igdb = None
if (self.config.get("enable-game-info", False) and
self.config.get("igdb-client-id") and
self.config.get("igdb-access-token")):
logging.info("IGDB Configuration Check:")
logging.info(f" enable-game-info: {self.config.get('enable-game-info', 'NOT SET')}")
logging.info(f" igdb-client-id: {'SET' if self.config.get('igdb-client-id') else 'NOT SET'}")
logging.info(f" igdb-access-token: {'SET' if self.config.get('igdb-access-token') else 'NOT SET'}")
if self.config.get("enable-game-info", False) and self.config.get("igdb-client-id") and self.config.get("igdb-access-token"):
try:
self.igdb = IGDBQuery(
self.config["igdb-client-id"],
self.config["igdb-access-token"]
)
logging.info("IGDB integration enabled for game information")
self.igdb = IGDBQuery(self.config["igdb-client-id"], self.config["igdb-access-token"])
logging.info("✅ IGDB integration SUCCESSFULLY enabled for game information")
logging.info(f" Client ID: {self.config['igdb-client-id'][:8]}...")
logging.info(f" Available functions: {len(self.igdb.get_openai_functions())}")
except Exception as e:
logging.warning(f"Failed to initialize IGDB: {e}")
logging.error(f"Failed to initialize IGDB: {e}")
self.igdb = None
else:
logging.warning("❌ IGDB integration DISABLED - missing configuration or disabled in config")
async def draw_openai(self, description: str) -> BytesIO:
for _ in range(3):
@@ -56,69 +60,146 @@ class OpenAIResponder(AIResponder, LeonardoAIDrawMixIn):
raise RuntimeError(f"Failed to generate image {repr(description)} after multiple retries")
async def chat(self, messages: List[Dict[str, Any]], limit: int) -> Tuple[Optional[Dict[str, Any]], int]:
if isinstance(messages[-1]["content"], str):
model = self.config["model"]
elif "model-vision" in self.config:
model = self.config["model-vision"]
else:
messages[-1]["content"] = messages[-1]["content"][0]["text"]
# Safety check for mock objects in tests
if not isinstance(messages, list) or len(messages) == 0:
logging.warning("Invalid messages format in chat method")
return None, limit
try:
# Clean up any orphaned tool messages from previous conversations
clean_messages = []
for i, msg in enumerate(messages):
if msg.get("role") == "tool":
# Skip tool messages that don't have a corresponding assistant message with tool_calls
if i == 0 or messages[i - 1].get("role") != "assistant" or not messages[i - 1].get("tool_calls"):
logging.debug(f"Removing orphaned tool message at position {i}")
continue
clean_messages.append(msg)
messages = clean_messages
last_message_content = messages[-1]["content"]
if isinstance(last_message_content, str):
model = self.config["model"]
elif "model-vision" in self.config:
model = self.config["model-vision"]
else:
messages[-1]["content"] = messages[-1]["content"][0]["text"]
except (KeyError, IndexError, TypeError) as e:
logging.warning(f"Error accessing message content: {e}")
return None, limit
try:
# Prepare function calls if IGDB is enabled
chat_kwargs = {
"model": model,
"messages": messages,
}
if self.igdb and self.config.get("enable-game-info", False):
chat_kwargs["tools"] = [
{"type": "function", "function": func}
for func in self.igdb.get_openai_functions()
]
chat_kwargs["tool_choice"] = "auto"
try:
igdb_functions = self.igdb.get_openai_functions()
if igdb_functions and isinstance(igdb_functions, list):
chat_kwargs["tools"] = [{"type": "function", "function": func} for func in igdb_functions]
chat_kwargs["tool_choice"] = "auto"
logging.info(f"🎮 IGDB functions available to AI: {[f['name'] for f in igdb_functions]}")
logging.debug(f" Full chat_kwargs with tools: {list(chat_kwargs.keys())}")
except (TypeError, AttributeError) as e:
logging.warning(f"Error setting up IGDB functions: {e}")
else:
logging.debug(
"🎮 IGDB not available for this request (igdb={}, enabled={})".format(
self.igdb is not None, self.config.get("enable-game-info", False)
)
)
result = await openai_chat(self.client, **chat_kwargs)
# Handle function calls if present
message = result.choices[0].message
# Log what we received from OpenAI
logging.debug(f"📨 OpenAI Response: content={bool(message.content)}, has_tool_calls={hasattr(message, 'tool_calls')}")
if hasattr(message, "tool_calls") and message.tool_calls:
tool_names = [tc.function.name for tc in message.tool_calls]
logging.info(f"🔧 OpenAI requested function calls: {tool_names}")
# Check if we have function/tool calls and IGDB is enabled
has_tool_calls = (hasattr(message, 'tool_calls') and message.tool_calls and
self.igdb and self.config.get("enable-game-info", False))
has_tool_calls = (
hasattr(message, "tool_calls") and message.tool_calls and self.igdb and self.config.get("enable-game-info", False)
)
# Clean up any existing tool messages in the history to avoid conflicts
if has_tool_calls:
messages = [msg for msg in messages if msg.get("role") != "tool"]
if has_tool_calls:
logging.info(f"🎮 Processing {len(message.tool_calls)} IGDB function call(s)...")
try:
# Process function calls
messages.append({
"role": "assistant",
"content": message.content or "",
"tool_calls": [tc.dict() if hasattr(tc, 'dict') else tc for tc in message.tool_calls]
})
# Process function calls - serialize tool_calls properly
tool_calls_data = []
for tc in message.tool_calls:
tool_calls_data.append(
{"id": tc.id, "type": "function", "function": {"name": tc.function.name, "arguments": tc.function.arguments}}
)
messages.append({"role": "assistant", "content": message.content or "", "tool_calls": tool_calls_data})
# Execute function calls
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
logging.info(f"🎮 Executing IGDB function: {function_name} with args: {function_args}")
# Execute IGDB function
function_result = await self._execute_igdb_function(function_name, function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(function_result) if function_result else "No results found"
})
# Get final response after function execution
final_result = await openai_chat(self.client, **chat_kwargs)
logging.info(f"🎮 IGDB function result: {type(function_result)} - {str(function_result)[:200]}...")
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(function_result) if function_result else "No results found",
}
)
# Get final response after function execution - remove tools for final call
final_chat_kwargs = {
"model": model,
"messages": messages,
}
logging.debug(f"🔧 Sending final request to OpenAI with {len(messages)} messages (no tools)")
logging.debug(f"🔧 Last few messages: {messages[-3:] if len(messages) > 3 else messages}")
final_result = await openai_chat(self.client, **final_chat_kwargs)
answer_obj = final_result.choices[0].message
logging.debug(
f"🔧 Final OpenAI response: content_length={len(answer_obj.content) if answer_obj.content else 0}, has_tool_calls={hasattr(answer_obj, 'tool_calls') and answer_obj.tool_calls}"
)
if answer_obj.content:
logging.debug(f"🔧 Response preview: {answer_obj.content[:200]}")
else:
logging.warning(f"🔧 OpenAI returned NULL content despite {final_result.usage.completion_tokens} completion tokens")
# If OpenAI returns null content after function calling, use empty string
if not answer_obj.content and function_result:
logging.warning("OpenAI returned null after function calling, using empty string")
answer_obj.content = ""
except Exception as e:
# If function calling fails, fall back to regular response
logging.warning(f"Function calling failed, using regular response: {e}")
answer_obj = message
else:
answer_obj = message
answer = {"content": answer_obj.content, "role": answer_obj.role}
# Handle null content from OpenAI
content = answer_obj.content
if content is None:
logging.warning("OpenAI returned null content, using empty string")
content = ""
answer = {"content": content, "role": answer_obj.role}
self.rate_limit_backoff = exponential_backoff()
logging.info(f"generated response {result.usage}: {repr(answer)}")
return answer, limit
@@ -135,12 +216,20 @@ class OpenAIResponder(AIResponder, LeonardoAIDrawMixIn):
logging.warning(f"got an rate limit error, sleep for {rate_limit_sleep} seconds: {str(err)}")
await asyncio.sleep(rate_limit_sleep)
except Exception as err:
import traceback
logging.warning(f"failed to generate response: {repr(err)}")
logging.debug(f"Full traceback: {traceback.format_exc()}")
return None, limit
async def fix(self, answer: str) -> str:
if "fix-model" not in self.config:
return answer
# Handle null/empty answer
if not answer:
logging.warning("Fix called with null/empty answer")
return '{"answer": "I apologize, I encountered an error processing your request.", "answer_needed": true, "channel": null, "staff": null, "picture": null, "picture_edit": false, "hack": false}'
messages = [{"role": "system", "content": self.config["fix-description"]}, {"role": "user", "content": answer}]
try:
result = await openai_chat(self.client, model=self.config["fix-model"], messages=messages)
@@ -206,37 +295,50 @@ class OpenAIResponder(AIResponder, LeonardoAIDrawMixIn):
"""
Execute IGDB function calls from OpenAI.
"""
logging.info(f"🎮 _execute_igdb_function called: {function_name}")
if not self.igdb:
return None
logging.error("🎮 IGDB function called but self.igdb is None!")
return {"error": "IGDB not available"}
try:
if function_name == "search_games":
query = function_args.get("query", "")
limit = function_args.get("limit", 5)
logging.info(f"🎮 Searching IGDB for: '{query}' (limit: {limit})")
if not query:
logging.warning("🎮 No search query provided to search_games")
return {"error": "No search query provided"}
results = self.igdb.search_games(query, limit)
if results:
logging.info(f"🎮 IGDB search returned: {len(results) if results and isinstance(results, list) else 0} results")
if results and isinstance(results, list) and len(results) > 0:
return {"games": results}
else:
return {"games": [], "message": f"No games found matching '{query}'"}
elif function_name == "get_game_details":
game_id = function_args.get("game_id")
logging.info(f"🎮 Getting IGDB details for game ID: {game_id}")
if not game_id:
logging.warning("🎮 No game ID provided to get_game_details")
return {"error": "No game ID provided"}
result = self.igdb.get_game_details(game_id)
logging.info(f"🎮 IGDB game details returned: {bool(result)}")
if result:
return {"game": result}
else:
return {"error": f"Game with ID {game_id} not found"}
else:
return {"error": f"Unknown function: {function_name}"}
except Exception as e:
logging.error(f"Error executing IGDB function {function_name}: {e}")
return {"error": f"Failed to execute {function_name}: {str(e)}"}