|
|
@@ -1,3 +1,4 @@
|
|
|
+import json
|
|
|
from typing import List
|
|
|
from fastapi import HTTPException
|
|
|
from openai import OpenAI
|
|
|
@@ -5,12 +6,13 @@ from config.settings import OPENAI_API_KEY
|
|
|
from models.chat import Message
|
|
|
from services.data_service import data_bg_loaded
|
|
|
from logging import getLogger
|
|
|
+from services.openai_service.openai_tools import tools_list, tools
|
|
|
# Initialize OpenAI client
|
|
|
openai_client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
-async def generate_completion(messages_array: List[Message], session_id: str) -> str:
|
|
|
+async def generate_completion(messages_array: List[Message], session_id: str, name: str, email: str) -> str:
|
|
|
"""Generate OpenAI chat completion"""
|
|
|
if not OPENAI_API_KEY:
|
|
|
logger.error("Error: OpenAI API key is not configured.")
|
|
|
@@ -33,6 +35,7 @@ tus responsabilidades son:
|
|
|
- Proporcionar recomendaciones sobre el menú de el bar klein
|
|
|
- Proporcionar información sobre la comida de el bar klein
|
|
|
- No puedes tomar pedidos de clientes, solo informar
|
|
|
+- puedes recibir feedback de los clientes, y usar la herramienta feedback para enviar el feedback
|
|
|
- Debes evadir cualquier pregunta que no sea relacionada con el bar klein
|
|
|
para esto usaras los siguientes datos:
|
|
|
{data_string}
|
|
|
@@ -46,8 +49,25 @@ para esto usaras los siguientes datos:
|
|
|
model="gpt-4o-mini",
|
|
|
messages=processed_messages, # type: ignore (OpenAI lib expects list of specific dicts)
|
|
|
temperature=0.3,
|
|
|
+ tools=tools_list,
|
|
|
+ tool_choice="auto",
|
|
|
)
|
|
|
+ calls = completion.choices[0].message.tool_calls
|
|
|
+ if calls:
|
|
|
+ logger.info(f"Tool calls: {calls}")
|
|
|
+ for call in calls:
|
|
|
+ if call.function.name in tools:
|
|
|
+ tool_function = tools[call.function.name]
|
|
|
+ tool_args = json.loads(call.function.arguments)
|
|
|
+ logger.info(f"Calling tool: {call.function.name} with args: {tool_args}")
|
|
|
+ tool_response = tool_function(name=name, email=email, **tool_args)
|
|
|
+ logger.info(f"Tool response: {tool_response}")
|
|
|
+ completion.choices[0].message.content = tool_response
|
|
|
+ else:
|
|
|
+ logger.warning(f"Tool {call.function.name} not found in tools dictionary.")
|
|
|
+
|
|
|
response_content = completion.choices[0].message.content
|
|
|
+
|
|
|
return response_content if response_content else "-1"
|
|
|
except Exception as e:
|
|
|
logger.error(f"Error calling OpenAI: {e}")
|