s2v-general commited on
Commit
0e8b740
·
1 Parent(s): 9c6bd2c

Added currency conversion tool to agent

Browse files
Files changed (2) hide show
  1. app.py +38 -1
  2. prompts.yaml +10 -0
app.py CHANGED
@@ -34,6 +34,43 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  final_answer = FinalAnswerTool()
38
 
39
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
@@ -55,7 +92,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[get_current_time_in_timezone,final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
37
+ @tool
38
+ def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
39
+ """Convert an amount from one currency to another using exchangerate.host.
40
+ Args:
41
+ amount: Numeric amount to convert.
42
+ from_currency: Source currency code (e.g., 'USD').
43
+ to_currency: Target currency code (e.g., 'EUR').
44
+ """
45
+ try:
46
+ response = requests.get(
47
+ "https://api.exchangerate.host/convert",
48
+ params={"from": from_currency.upper(), "to": to_currency.upper(), "amount": amount},
49
+ timeout=10,
50
+ )
51
+ response.raise_for_status()
52
+ data = response.json()
53
+
54
+ if not data.get("success", False):
55
+ return f"Currency conversion failed: {data.get('error', 'Unknown error')}"
56
+
57
+ converted = data.get("result")
58
+ rate = data.get("info", {}).get("rate")
59
+
60
+ if converted is None or rate is None:
61
+ return "Currency conversion failed: incomplete response from service."
62
+
63
+ return (
64
+ f"{amount:.2f} {from_currency.upper()} = {converted:.2f} {to_currency.upper()}\n"
65
+ f"Exchange rate: 1 {from_currency.upper()} = {rate:.4f} {to_currency.upper()}"
66
+ )
67
+
68
+ except requests.RequestException as req_err:
69
+ return f"Currency conversion failed: network error ({req_err})."
70
+ except Exception as e:
71
+ return f"Currency conversion failed: {e}"
72
+
73
+
74
  final_answer = FinalAnswerTool()
75
 
76
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
 
92
 
93
  agent = CodeAgent(
94
  model=model,
95
+ tools=[get_current_time_in_timezone, convert_currency, final_answer], ## add your tools here (don't remove final answer)
96
  max_steps=6,
97
  verbosity_level=1,
98
  grammar=None,
prompts.yaml CHANGED
@@ -38,6 +38,16 @@
38
  final_answer(result)
39
  ```<end_code>
40
 
 
 
 
 
 
 
 
 
 
 
41
  ---
42
  Task:
43
  "Answer the question in the variable `question` about the image stored in the variable `image`. The question is in French.
 
38
  final_answer(result)
39
  ```<end_code>
40
 
41
+ ---
42
+ Task: "Convert 250 USD to EUR."
43
+
44
+ Thought: I will call the `convert_currency` tool to handle the conversion and then present the result using `final_answer`.
45
+ Code:
46
+ ```py
47
+ conversion = convert_currency(amount=250, from_currency="USD", to_currency="EUR")
48
+ final_answer(conversion)
49
+ ```<end_code>
50
+
51
  ---
52
  Task:
53
  "Answer the question in the variable `question` about the image stored in the variable `image`. The question is in French.