Spaces:
Sleeping
Sleeping
Commit ·
93d76df
1
Parent(s): b6e93af
Multiple and tool message management in UI
Browse files- agents/factory.py +2 -2
- agents/session.py +23 -8
- app.py +4 -1
- demos/chatbot_with_tools.py +1 -1
- demos/chatbot_with_tools_and_memory.py +1 -1
- main.py +3 -2
agents/factory.py
CHANGED
|
@@ -9,7 +9,7 @@ def build_agent(model_used: ModelEnum):
|
|
| 9 |
if model_used in [ModelEnum.ZEPHYR, ModelEnum.MISTRAL]:
|
| 10 |
return JarvisBasicAgent(model_used)
|
| 11 |
elif model_used == ModelEnum.GPT_3_5:
|
| 12 |
-
|
| 13 |
-
return JarvisMcpMemoryAgent(model_used)
|
| 14 |
else:
|
| 15 |
raise ValueError("Modelo no soportado.")
|
|
|
|
| 9 |
if model_used in [ModelEnum.ZEPHYR, ModelEnum.MISTRAL]:
|
| 10 |
return JarvisBasicAgent(model_used)
|
| 11 |
elif model_used == ModelEnum.GPT_3_5:
|
| 12 |
+
return JarvisMemoryAgent(model_used)
|
| 13 |
+
# return JarvisMcpMemoryAgent(model_used)
|
| 14 |
else:
|
| 15 |
raise ValueError("Modelo no soportado.")
|
agents/session.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 2 |
from enums.core_enums import ModelEnum, IdentificationFailedProtocolEnum
|
| 3 |
from config import DEFAULT_MODEL, IDENTIFICATION_FAILED_PROTOCOL
|
| 4 |
from database.users.users_db import find_user_by_prompt
|
|
@@ -122,11 +122,26 @@ class JarvisSession:
|
|
| 122 |
try:
|
| 123 |
kwargs = self._build_agent_kwargs(messages)
|
| 124 |
response = self.agent.invoke(**kwargs)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
if isinstance(msg,
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
except Exception as e:
|
| 131 |
# Optional: log the error
|
| 132 |
return f"Ha habido un error procesando su petición, señor. Error: {e}"
|
|
@@ -138,10 +153,10 @@ class JarvisSession:
|
|
| 138 |
self._update_chat_state(prompt)
|
| 139 |
|
| 140 |
if self._chat_state == ChatState.NOT_INITIALIZED:
|
| 141 |
-
return AUTOMATIC_RESPONSE_IF_ID_FAILED
|
| 142 |
|
| 143 |
elif self._chat_state == ChatState.JARVIS_WELCOME_MESSAGE:
|
| 144 |
-
return self._get_welcome_message()
|
| 145 |
|
| 146 |
elif self._chat_state == ChatState.STARTING_CHAT:
|
| 147 |
messages = [
|
|
|
|
| 1 |
+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, ToolMessage
|
| 2 |
from enums.core_enums import ModelEnum, IdentificationFailedProtocolEnum
|
| 3 |
from config import DEFAULT_MODEL, IDENTIFICATION_FAILED_PROTOCOL
|
| 4 |
from database.users.users_db import find_user_by_prompt
|
|
|
|
| 122 |
try:
|
| 123 |
kwargs = self._build_agent_kwargs(messages)
|
| 124 |
response = self.agent.invoke(**kwargs)
|
| 125 |
+
response_messages = response.get("messages", [])
|
| 126 |
+
last_human_index = max(
|
| 127 |
+
(i for i, msg in enumerate(response_messages) if isinstance(msg, HumanMessage)),
|
| 128 |
+
default=-1
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
result = []
|
| 134 |
+
for msg in response_messages[last_human_index + 1:]:
|
| 135 |
+
if isinstance(msg, AIMessage) and 'tool_calls' in msg.additional_kwargs:
|
| 136 |
+
for tool_call in msg.additional_kwargs['tool_calls']:
|
| 137 |
+
result.append(f"Llamando a la función: {tool_call['function']['name']}")
|
| 138 |
+
elif isinstance(msg, ToolMessage):
|
| 139 |
+
result.append(f"Resultado de la función {msg.name}: {msg.content}")
|
| 140 |
+
else:
|
| 141 |
+
result.append(msg.content)
|
| 142 |
+
|
| 143 |
+
return result if result else "Lo siento, señor. No tengo respuesta para su petición."
|
| 144 |
+
|
| 145 |
except Exception as e:
|
| 146 |
# Optional: log the error
|
| 147 |
return f"Ha habido un error procesando su petición, señor. Error: {e}"
|
|
|
|
| 153 |
self._update_chat_state(prompt)
|
| 154 |
|
| 155 |
if self._chat_state == ChatState.NOT_INITIALIZED:
|
| 156 |
+
return [AUTOMATIC_RESPONSE_IF_ID_FAILED]
|
| 157 |
|
| 158 |
elif self._chat_state == ChatState.JARVIS_WELCOME_MESSAGE:
|
| 159 |
+
return [self._get_welcome_message()]
|
| 160 |
|
| 161 |
elif self._chat_state == ChatState.STARTING_CHAT:
|
| 162 |
messages = [
|
app.py
CHANGED
|
@@ -15,7 +15,10 @@ def respond(message, chat_history, model_used):
|
|
| 15 |
response = ask_jarvis(message, model_used, thread_id=thread_id)
|
| 16 |
chat_history = chat_history or []
|
| 17 |
chat_history.append({"role": "user", "content": message})
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
return chat_history, ""
|
| 20 |
|
| 21 |
|
|
|
|
| 15 |
response = ask_jarvis(message, model_used, thread_id=thread_id)
|
| 16 |
chat_history = chat_history or []
|
| 17 |
chat_history.append({"role": "user", "content": message})
|
| 18 |
+
|
| 19 |
+
for response_msg in response:
|
| 20 |
+
chat_history.append({"role": "assistant", "content": response_msg})
|
| 21 |
+
|
| 22 |
return chat_history, ""
|
| 23 |
|
| 24 |
|
demos/chatbot_with_tools.py
CHANGED
|
@@ -112,7 +112,7 @@ def ask_jarvis(question: str):
|
|
| 112 |
return "Array de respuestas: Vacío. Lo siento, señor. Actualmente no tengo respuesta para su petición."
|
| 113 |
|
| 114 |
while True:
|
| 115 |
-
query = input("
|
| 116 |
if query.lower() in ["salir", "exit", "quit", "q"] or ("eso es todo" in query.lower() and "jarvis" in query.lower()):
|
| 117 |
break
|
| 118 |
response = ask_jarvis(query)
|
|
|
|
| 112 |
return "Array de respuestas: Vacío. Lo siento, señor. Actualmente no tengo respuesta para su petición."
|
| 113 |
|
| 114 |
while True:
|
| 115 |
+
query = input("Usuario: ")
|
| 116 |
if query.lower() in ["salir", "exit", "quit", "q"] or ("eso es todo" in query.lower() and "jarvis" in query.lower()):
|
| 117 |
break
|
| 118 |
response = ask_jarvis(query)
|
demos/chatbot_with_tools_and_memory.py
CHANGED
|
@@ -60,7 +60,7 @@ def ask_jarvis(question: str):
|
|
| 60 |
return "Array de respuestas: Vacío. Lo siento, señor. Actualmente no tengo respuesta para su petición."
|
| 61 |
|
| 62 |
while True:
|
| 63 |
-
query = input("
|
| 64 |
if query.lower() in ["salir", "exit", "quit", "q"] or ("eso es todo" in query.lower() and "jarvis" in query.lower()):
|
| 65 |
break
|
| 66 |
response = ask_jarvis(query)
|
|
|
|
| 60 |
return "Array de respuestas: Vacío. Lo siento, señor. Actualmente no tengo respuesta para su petición."
|
| 61 |
|
| 62 |
while True:
|
| 63 |
+
query = input("Usuario: ")
|
| 64 |
if query.lower() in ["salir", "exit", "quit", "q"] or ("eso es todo" in query.lower() and "jarvis" in query.lower()):
|
| 65 |
break
|
| 66 |
response = ask_jarvis(query)
|
main.py
CHANGED
|
@@ -5,8 +5,9 @@ model_used = DEFAULT_MODEL
|
|
| 5 |
thread_id = "1" # Parameterize this in the future
|
| 6 |
|
| 7 |
while True:
|
| 8 |
-
question = input("
|
| 9 |
if question.lower() in ["salir", "exit", "quit"] or ("eso es todo" in question.lower() and "jarvis" in question.lower()):
|
| 10 |
break
|
| 11 |
response = ask_jarvis(question, model_used, thread_id=thread_id)
|
| 12 |
-
|
|
|
|
|
|
| 5 |
thread_id = "1" # Parameterize this in the future
|
| 6 |
|
| 7 |
while True:
|
| 8 |
+
question = input("Usuario: ")
|
| 9 |
if question.lower() in ["salir", "exit", "quit"] or ("eso es todo" in question.lower() and "jarvis" in question.lower()):
|
| 10 |
break
|
| 11 |
response = ask_jarvis(question, model_used, thread_id=thread_id)
|
| 12 |
+
for response_msg in response:
|
| 13 |
+
print("Jarvis:", response_msg)
|