Spaces:
Runtime error
Runtime error
added chat tool
Browse files
app.py
CHANGED
|
@@ -18,6 +18,34 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -55,7 +83,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
+
chat_history = [] # Global variable to store conversation history
|
| 22 |
+
|
| 23 |
+
@tool
|
| 24 |
+
def chat_with_ai(message: str) -> str:
|
| 25 |
+
"""A tool that allows the AI to engage in general conversation with memory.
|
| 26 |
+
Args:
|
| 27 |
+
message: The user's message.
|
| 28 |
+
"""
|
| 29 |
+
global chat_history # Keep track of past messages
|
| 30 |
+
|
| 31 |
+
# Limit conversation history to the last 5 messages
|
| 32 |
+
if len(chat_history) > 5:
|
| 33 |
+
chat_history.pop(0) # Remove the oldest message
|
| 34 |
+
|
| 35 |
+
# Add the new message to history
|
| 36 |
+
chat_history.append({"role": "user", "content": message})
|
| 37 |
+
|
| 38 |
+
# Format history as context for the AI
|
| 39 |
+
formatted_history = "\n".join(f"{msg['role']}: {msg['content']}" for msg in chat_history)
|
| 40 |
+
|
| 41 |
+
# Generate AI response
|
| 42 |
+
response = model(formatted_history) # AI model processes chat history
|
| 43 |
+
|
| 44 |
+
# Add AI response to history
|
| 45 |
+
chat_history.append({"role": "assistant", "content": response})
|
| 46 |
+
|
| 47 |
+
return response
|
| 48 |
+
|
| 49 |
@tool
|
| 50 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 51 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 83 |
|
| 84 |
agent = CodeAgent(
|
| 85 |
model=model,
|
| 86 |
+
tools=[my_custom_tool, chat_with_ai, get_current_time_in_timezone, final_answer], ## add your tools here (don't remove final answer)
|
| 87 |
max_steps=6,
|
| 88 |
verbosity_level=1,
|
| 89 |
grammar=None,
|