EtienneB commited on
Commit
ffa4ee4
·
1 Parent(s): 7da4369
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
__pycache__/agent.cpython-313.pyc ADDED
Binary file (2.39 kB). View file
 
__pycache__/retriever.cpython-313.pyc ADDED
Binary file (195 Bytes). View file
 
__pycache__/tools.cpython-313.pyc ADDED
Binary file (7.74 kB). View file
 
agent.py CHANGED
@@ -1,14 +1,17 @@
1
  from typing import Annotated, TypedDict
2
 
 
 
3
  from langchain_core.messages import AIMessage, AnyMessage, HumanMessage
4
- from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
5
  from langgraph.graph import START, StateGraph
6
  from langgraph.graph.message import add_messages
7
  from langgraph.prebuilt import ToolNode, tools_condition
8
 
9
  from retriever import guest_info_tool
10
- from tools import (absolute, add, divide, exponential, floor_divide, logarithm,
11
- modulus, multiply, power, square_root, subtract, web_search)
 
 
12
 
13
  # Generate the chat interface, including the tools
14
  llm = HuggingFaceEndpoint(
@@ -17,7 +20,6 @@ llm = HuggingFaceEndpoint(
17
  )
18
 
19
  chat = ChatHuggingFace(llm=llm, verbose=True)
20
- tools = [, weather_info_tool, hub_stats_tool]
21
  tools = [
22
  multiply,
23
  add,
@@ -31,6 +33,8 @@ tools = [
31
  logarithm,
32
  exponential,
33
  web_search,
 
 
34
  ]
35
 
36
  chat_with_tools = chat.bind_tools(tools)
 
1
  from typing import Annotated, TypedDict
2
 
3
+ from langchain_community.chat_models import ChatHuggingFace
4
+ from langchain_community.llms import HuggingFaceEndpoint
5
  from langchain_core.messages import AIMessage, AnyMessage, HumanMessage
 
6
  from langgraph.graph import START, StateGraph
7
  from langgraph.graph.message import add_messages
8
  from langgraph.prebuilt import ToolNode, tools_condition
9
 
10
  from retriever import guest_info_tool
11
+ from tools import (absolute, add, divide, exponential, floor_divide,
12
+ get_current_time_in_timezone, logarithm, modulus, multiply,
13
+ power, roman_calculator_converter, square_root, subtract,
14
+ web_search)
15
 
16
  # Generate the chat interface, including the tools
17
  llm = HuggingFaceEndpoint(
 
20
  )
21
 
22
  chat = ChatHuggingFace(llm=llm, verbose=True)
 
23
  tools = [
24
  multiply,
25
  add,
 
33
  logarithm,
34
  exponential,
35
  web_search,
36
+ roman_calculator_converter,
37
+ get_current_time_in_timezone,
38
  ]
39
 
40
  chat_with_tools = chat.bind_tools(tools)
app.py CHANGED
@@ -4,24 +4,58 @@ import os
4
  import gradio as gr
5
  import pandas as pd
6
  import requests
7
- from agent import build_graph
 
 
8
  from langchain_core.messages import HumanMessage
9
 
 
 
 
 
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
 
 
13
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
  class BasicAgent:
17
  def __init__(self):
18
  print("BasicAgent initialized.")
19
- self.graph = build_graph()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def __call__(self, question: str) -> str:
21
  print(f"Agent received question (first 50 chars): {question[:50]}...")
22
- fixed_answer = "This is a default answer."
 
23
  messages = [HumanMessage(content=question)]
24
- messages = self.graph.invoke({"messages": messages})
25
  answer = messages['messages'][-1].content
26
  return answer[14:]
27
 
 
4
  import gradio as gr
5
  import pandas as pd
6
  import requests
7
+ from dotenv import load_dotenv
8
+ from langchain_community.chat_models import ChatHuggingFace
9
+ from langchain_community.llms import HuggingFaceEndpoint
10
  from langchain_core.messages import HumanMessage
11
 
12
+ from tools import (absolute, add, divide, exponential, floor_divide,
13
+ get_current_time_in_timezone, logarithm, modulus, multiply,
14
+ power, roman_calculator_converter, square_root, subtract,
15
+ web_search)
16
+
17
  # (Keep Constants as is)
18
  # --- Constants ---
19
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
20
+ MAX_AGENT_ITERATIONS = 15
21
+
22
+
23
+ load_dotenv()
24
+ HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
25
 
26
  # --- Basic Agent Definition ---
27
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
28
  class BasicAgent:
29
  def __init__(self):
30
  print("BasicAgent initialized.")
31
+ self.llm = HuggingFaceEndpoint(repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,)
32
+ self.chat = ChatHuggingFace(llm=self.llm, verbose=True)
33
+ self.tools = [
34
+ multiply,
35
+ add,
36
+ subtract,
37
+ power,
38
+ divide,
39
+ modulus,
40
+ square_root,
41
+ floor_divide,
42
+ absolute,
43
+ logarithm,
44
+ exponential,
45
+ web_search,
46
+ roman_calculator_converter,
47
+ get_current_time_in_timezone,
48
+ ]
49
+ self.chat_with_tools = self.chat.bind_tools(tools)
50
+ print(f"Total tools available: {len(self.tools)}")
51
+
52
+
53
  def __call__(self, question: str) -> str:
54
  print(f"Agent received question (first 50 chars): {question[:50]}...")
55
+
56
+ # Question in a HumanMessage from langchain_core
57
  messages = [HumanMessage(content=question)]
58
+ messages = self.chat_with_tools.invoke({"messages": messages})
59
  answer = messages['messages'][-1].content
60
  return answer[14:]
61
 
requirements.txt CHANGED
@@ -2,8 +2,13 @@ gradio
2
  requests
3
 
4
  # LangChain dependencies
 
5
  langchain-core
6
-
 
 
7
 
8
  #other
9
  # inspect
 
 
 
2
  requests
3
 
4
  # LangChain dependencies
5
+ langchain
6
  langchain-core
7
+ langchain-community
8
+ langgraph
9
+ huggingface_hub
10
 
11
  #other
12
  # inspect
13
+ python-dotenv
14
+ gradio[oauth]
tools.py CHANGED
@@ -180,6 +180,7 @@ def exponential(x: float) -> float:
180
  """
181
  return math.exp(x)
182
 
 
183
  @tool
184
  def web_search(query: str) -> str:
185
  """Performs a DuckDuckGo search for the given query and returns the results.
@@ -193,3 +194,53 @@ def web_search(query: str) -> str:
193
  search_tool = DuckDuckGoSearchRun()
194
  return search_tool.invoke(query)
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  """
181
  return math.exp(x)
182
 
183
+
184
  @tool
185
  def web_search(query: str) -> str:
186
  """Performs a DuckDuckGo search for the given query and returns the results.
 
194
  search_tool = DuckDuckGoSearchRun()
195
  return search_tool.invoke(query)
196
 
197
+
198
+ @tool
199
+ def roman_calculator_converter(value1:int, value2:int, oper:str) -> str:
200
+ """A tool that performs an operator on 2 numbers to calculate the result
201
+ Args:
202
+ value1: the first value
203
+ value2: the second value
204
+ oper: operator for the calculation, like "add", "substract", "multiply", "divide"
205
+ """
206
+ roman_numerals = {
207
+ 1000: "M", 900: "CM", 500: "D", 400: "CD",
208
+ 100: "C", 90: "XC", 50: "L", 40: "XL",
209
+ 10: "X", 9: "IX", 5: "V", 4: "IV", 1: "I"
210
+ }
211
+ roman_string = ""
212
+
213
+ if oper == "add":
214
+ result = value1 + value2
215
+ elif oper == "subtract":
216
+ result = value2 - value1
217
+ elif oper == "divide":
218
+ result = value1 / value2
219
+ elif oper == "multiply":
220
+ result = value1 * value2
221
+
222
+ else:
223
+ return "Unsupported operation. Please use 'add' or 'subtract'."
224
+
225
+ for value, numeral in roman_numerals.items():
226
+ while result >= value:
227
+ roman_string += numeral
228
+ result -= value
229
+ return f"The result of {oper} on the values {value1} and {value2} is the Roman numeral: {roman_string}"
230
+
231
+
232
+ @tool
233
+ def get_current_time_in_timezone(timezone: str) -> str:
234
+ """A tool that fetches the current local time in a specified timezone.
235
+ Args:
236
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
237
+ """
238
+ try:
239
+ # Create timezone object
240
+ tz = pytz.timezone(timezone)
241
+ # Get current time in that timezone
242
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
243
+ return f"The current local time in {timezone} is: {local_time}"
244
+ except Exception as e:
245
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
246
+