Spaces:
Runtime error
Runtime error
add some tools
Browse files
app.py
CHANGED
@@ -18,7 +18,53 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
@tool
|
24 |
def chat_with_ai(message: str) -> str:
|
@@ -26,25 +72,34 @@ def chat_with_ai(message: str) -> str:
|
|
26 |
Args:
|
27 |
message: The user's message.
|
28 |
"""
|
29 |
-
global chat_history
|
30 |
|
31 |
-
#
|
32 |
if len(chat_history) > 5:
|
33 |
-
chat_history.pop(0)
|
34 |
|
35 |
-
# Add
|
36 |
chat_history.append({"role": "user", "content": message})
|
37 |
|
38 |
-
# Format history as
|
39 |
formatted_history = "\n".join(f"{msg['role']}: {msg['content']}" for msg in chat_history)
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
|
|
48 |
|
49 |
@tool
|
50 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -68,7 +123,7 @@ final_answer = FinalAnswerTool()
|
|
68 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
69 |
|
70 |
model = HfApiModel(
|
71 |
-
max_tokens=
|
72 |
temperature=0.5,
|
73 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
74 |
custom_role_conversions=None,
|
@@ -83,15 +138,20 @@ with open("prompts.yaml", 'r') as stream:
|
|
83 |
|
84 |
agent = CodeAgent(
|
85 |
model=model,
|
86 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
max_steps=6,
|
88 |
verbosity_level=1,
|
89 |
-
grammar=None,
|
90 |
-
planning_interval=None,
|
91 |
-
name=None,
|
92 |
-
description=None,
|
93 |
prompt_templates=prompt_templates
|
94 |
)
|
95 |
|
96 |
-
|
97 |
GradioUI(agent).launch()
|
|
|
18 |
"""
|
19 |
return "What magic will you build ?"
|
20 |
|
21 |
+
@tool
|
22 |
+
def search_flights(departure: str, destination: str, date: str) -> str:
|
23 |
+
"""Finds flights from departure to destination on the given date using DuckDuckGo.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
departure: The city or airport code where the flight starts.
|
27 |
+
destination: The city or airport code where the flight ends.
|
28 |
+
date: The departure date in YYYY-MM-DD format.
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
A string containing flight search results.
|
32 |
+
"""
|
33 |
+
query = f"flights from {departure} to {destination} on {date}"
|
34 |
+
search_results = DuckDuckGoSearchTool()(query) # Calls DuckDuckGo search
|
35 |
+
return f"Here are some flight options:\n{search_results}"
|
36 |
+
|
37 |
+
API_KEY = "fca_live_L2xM404CxH5IQWoNotD3X645W8lv7UHOoJdswwgd" # 🔹 Your API key
|
38 |
+
|
39 |
+
@tool
|
40 |
+
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
|
41 |
+
"""Converts currency from one to another using FreeCurrencyAPI.
|
42 |
+
|
43 |
+
Args:
|
44 |
+
amount: The amount to convert.
|
45 |
+
from_currency: The original currency (e.g., "USD").
|
46 |
+
to_currency: The target currency (e.g., "EUR").
|
47 |
+
|
48 |
+
Returns:
|
49 |
+
The converted amount in the target currency.
|
50 |
+
"""
|
51 |
+
try:
|
52 |
+
url = f"https://api.freecurrencyapi.com/v1/latest?apikey={API_KEY}&base_currency={from_currency.upper()}"
|
53 |
+
|
54 |
+
response = requests.get(url).json()
|
55 |
+
|
56 |
+
# ✅ Check if the API returned valid exchange rates
|
57 |
+
if "data" in response and to_currency.upper() in response["data"]:
|
58 |
+
rate = response["data"][to_currency.upper()]
|
59 |
+
converted_amount = amount * rate
|
60 |
+
return f"{amount} {from_currency.upper()} is approximately {converted_amount:.2f} {to_currency.upper()}."
|
61 |
+
|
62 |
+
return f"Error: Could not find exchange rate for {to_currency.upper()}."
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
return f"Error fetching exchange rates: {str(e)}"
|
66 |
+
|
67 |
+
chat_history = [] # Store conversation history
|
68 |
|
69 |
@tool
|
70 |
def chat_with_ai(message: str) -> str:
|
|
|
72 |
Args:
|
73 |
message: The user's message.
|
74 |
"""
|
75 |
+
global chat_history
|
76 |
|
77 |
+
# Keep the last 5 messages for context
|
78 |
if len(chat_history) > 5:
|
79 |
+
chat_history.pop(0)
|
80 |
|
81 |
+
# Add user message to history
|
82 |
chat_history.append({"role": "user", "content": message})
|
83 |
|
84 |
+
# Format the history as input for the AI model
|
85 |
formatted_history = "\n".join(f"{msg['role']}: {msg['content']}" for msg in chat_history)
|
86 |
|
87 |
+
# ✅ Ensure AI response is handled correctly
|
88 |
+
try:
|
89 |
+
response = model(formatted_history) # Call model
|
90 |
+
|
91 |
+
# ✅ Handle both string and dictionary responses
|
92 |
+
if isinstance(response, dict):
|
93 |
+
response_text = response.get("text", str(response)) # Extract text if available
|
94 |
+
else:
|
95 |
+
response_text = str(response) # Convert non-dict responses to string
|
96 |
+
|
97 |
+
# Add AI response to history
|
98 |
+
chat_history.append({"role": "assistant", "content": response_text})
|
99 |
+
return response_text
|
100 |
|
101 |
+
except Exception as e:
|
102 |
+
return f"Error processing chat: {str(e)}"
|
103 |
|
104 |
@tool
|
105 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
123 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
124 |
|
125 |
model = HfApiModel(
|
126 |
+
max_tokens=2096,
|
127 |
temperature=0.5,
|
128 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
129 |
custom_role_conversions=None,
|
|
|
138 |
|
139 |
agent = CodeAgent(
|
140 |
model=model,
|
141 |
+
tools=[
|
142 |
+
final_answer,
|
143 |
+
get_current_time_in_timezone,
|
144 |
+
my_custom_tool,
|
145 |
+
chat_with_ai, # Regular chat tool
|
146 |
+
search_flights,
|
147 |
+
#scrape_webpage,
|
148 |
+
convert_currency,
|
149 |
+
#get_weather,
|
150 |
+
#generate_ai_image
|
151 |
+
],
|
152 |
max_steps=6,
|
153 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
154 |
prompt_templates=prompt_templates
|
155 |
)
|
156 |
|
|
|
157 |
GradioUI(agent).launch()
|