Luigi D'Addona commited on
Commit
bd03a3e
·
1 Parent(s): 260eec5

aggiunti tool: wikipedia_search_3, download_taskid_file

Browse files
Files changed (3) hide show
  1. agent.py +13 -3
  2. app.py +6 -0
  3. tools.py +41 -0
agent.py CHANGED
@@ -13,7 +13,7 @@ from langgraph.graph import StateGraph, END
13
  from langchain_google_genai import ChatGoogleGenerativeAI
14
 
15
  # Local imports
16
- from tools import get_search_tool, get_wikipedia_tool, wikipedia_search, execute_python_code_from_file
17
 
18
  # Nota: per i test in locale si usa il .env
19
  # su HuggingFace invece si usano le variabili definite in Settings/"Variables and secrets"
@@ -25,13 +25,23 @@ GEMINI_TEMPERATURE = float(os.environ.get("GEMINI_TEMPERATURE"))
25
 
26
  TOOLS_CALL_DELAY = 1.5
27
 
28
- GENERAL_AGENT_INSTRUCTIONS = """You are a general AI assistant. I will ask you a question.
 
29
  Your answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
30
  If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
31
  If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
32
  If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
33
  Provide only the answer, without notes, explanations or comments."""
34
 
 
 
 
 
 
 
 
 
 
35
  #
36
  # Inizializza il modello e gli associa i tool
37
  #
@@ -47,7 +57,7 @@ chat = ChatGoogleGenerativeAI(
47
  search_tool = get_search_tool()
48
  #wikipedia_tool = get_wikipedia_tool()
49
 
50
- tools = [search_tool, wikipedia_search, execute_python_code_from_file]
51
 
52
  # Bind tools to the model
53
  chat_with_tools = chat.bind_tools(tools)
 
13
  from langchain_google_genai import ChatGoogleGenerativeAI
14
 
15
  # Local imports
16
+ from tools import get_search_tool, get_wikipedia_tool, wikipedia_search, wikipedia_search_3, execute_python_code_from_file, download_taskid_file
17
 
18
  # Nota: per i test in locale si usa il .env
19
  # su HuggingFace invece si usano le variabili definite in Settings/"Variables and secrets"
 
25
 
26
  TOOLS_CALL_DELAY = 1.5
27
 
28
+ GENERAL_AGENT_INSTRUCTIONS = """You are a helpful assistant tasked with answering questions using a set of tools.
29
+ Now, I will ask you a question. Report your thoughts, and provide your answer.
30
  Your answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
31
  If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
32
  If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
33
  If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
34
  Provide only the answer, without notes, explanations or comments."""
35
 
36
+ # GENERAL_AGENT_INSTRUCTIONS = """
37
+ # You are a general AI assistant. Your purpose is to answer questions and complete tasks accurately and concisely.
38
+ # You have access to various tools to help you gather information and perform actions.
39
+ # Always prioritize using your tools to find factual information if a question requires it.
40
+ # If a question can be answered directly from your knowledge, do so.
41
+ # If you use a tool, provide only the direct result or answer based on the tool's output.
42
+ # Do not include any conversational filler, explanations of your thought process, or pleasantries unless specifically asked.
43
+ # """
44
+
45
  #
46
  # Inizializza il modello e gli associa i tool
47
  #
 
57
  search_tool = get_search_tool()
58
  #wikipedia_tool = get_wikipedia_tool()
59
 
60
+ tools = [search_tool, wikipedia_search_3, execute_python_code_from_file, download_taskid_file]
61
 
62
  # Bind tools to the model
63
  chat_with_tools = chat.bind_tools(tools)
app.py CHANGED
@@ -85,6 +85,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
85
  if not task_id or question_text is None:
86
  print(f"Skipping item with missing task_id or question: {item}")
87
  continue
 
 
 
 
 
 
88
  try:
89
  # submitted_answer = agent(question_text)
90
  messages = [HumanMessage(content = question_text)]
 
85
  if not task_id or question_text is None:
86
  print(f"Skipping item with missing task_id or question: {item}")
87
  continue
88
+
89
+ # Se c'e' un file associato, agginge alla question file e task_id e suggerisce il tool da utilizzare per scaricarlo
90
+ file_name = item.get("file_name")
91
+ if file_name is not None:
92
+ question_text += " file_name: {}, task:id: {} (use tool 'download_taskid_file' to get it)".format(file_name, task_id)
93
+
94
  try:
95
  # submitted_answer = agent(question_text)
96
  messages = [HumanMessage(content = question_text)]
tools.py CHANGED
@@ -1,9 +1,11 @@
1
  import os, sys
2
  from dotenv import load_dotenv
 
3
 
4
  from langchain_community.tools import DuckDuckGoSearchRun
5
  from langchain_community.utilities import WikipediaAPIWrapper
6
  from langchain_community.tools import WikipediaQueryRun
 
7
  import wikipedia
8
  from langchain_core.tools import tool
9
  from langchain.tools import Tool
@@ -12,6 +14,8 @@ from langchain.tools import Tool
12
  import subprocess
13
 
14
 
 
 
15
  load_dotenv()
16
  WIKIPEDIA_TOP_K_RESULTS = int(os.environ.get("WIKIPEDIA_TOP_K_RESULTS"))
17
  WIKIPEDIA_DOC_CONTENT_CHARS_MAX = int(os.environ.get("WIKIPEDIA_DOC_CONTENT_CHARS_MAX"))
@@ -24,6 +28,8 @@ def get_search_tool():
24
  return search_tool
25
 
26
 
 
 
27
  def get_wikipedia_tool():
28
 
29
  #print("WIKIPEDIA_TOP_K_RESULTS:{}, WIKIPEDIA_DOC_CONTENT_CHARS_MAX:{}".format(WIKIPEDIA_TOP_K_RESULTS, WIKIPEDIA_DOC_CONTENT_CHARS_MAX))
@@ -37,6 +43,7 @@ def get_wikipedia_tool():
37
  return wikipedia_tool
38
 
39
 
 
40
  @tool
41
  def wikipedia_search(query: str) -> str:
42
  """
@@ -64,6 +71,25 @@ def wikipedia_search(query: str) -> str:
64
  return f"Error occurred: {str(e)}"
65
 
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
 
69
  @tool
@@ -108,3 +134,18 @@ def execute_python_code_from_file(file_path: str) -> str:
108
 
109
  except Exception as e:
110
  return f"An unexpected error occurred during code execution: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os, sys
2
  from dotenv import load_dotenv
3
+ import requests
4
 
5
  from langchain_community.tools import DuckDuckGoSearchRun
6
  from langchain_community.utilities import WikipediaAPIWrapper
7
  from langchain_community.tools import WikipediaQueryRun
8
+ from langchain_community.document_loaders import WikipediaLoader
9
  import wikipedia
10
  from langchain_core.tools import tool
11
  from langchain.tools import Tool
 
14
  import subprocess
15
 
16
 
17
+ DATASET_API_URL = 'https://agents-course-unit4-scoring.hf.space'
18
+
19
  load_dotenv()
20
  WIKIPEDIA_TOP_K_RESULTS = int(os.environ.get("WIKIPEDIA_TOP_K_RESULTS"))
21
  WIKIPEDIA_DOC_CONTENT_CHARS_MAX = int(os.environ.get("WIKIPEDIA_DOC_CONTENT_CHARS_MAX"))
 
28
  return search_tool
29
 
30
 
31
+ # Wikipedia tool 1: usa WikipediaQueryRun dal package 'langchain_community.tools'
32
+ # problema: sembra ottenere solo i summary
33
  def get_wikipedia_tool():
34
 
35
  #print("WIKIPEDIA_TOP_K_RESULTS:{}, WIKIPEDIA_DOC_CONTENT_CHARS_MAX:{}".format(WIKIPEDIA_TOP_K_RESULTS, WIKIPEDIA_DOC_CONTENT_CHARS_MAX))
 
43
  return wikipedia_tool
44
 
45
 
46
+ # Wikipedia tool 2: utilizza direttamente il package 'wikipedia'
47
  @tool
48
  def wikipedia_search(query: str) -> str:
49
  """
 
71
  return f"Error occurred: {str(e)}"
72
 
73
 
74
+ # Wikipedia tool 3: utilizza WikipediaLoader dla package 'langchain_community.document_loaders'
75
+ @tool
76
+ def wikipedia_search_3(query: str) -> str:
77
+ """
78
+ Search Wikipedia and return the full content of the most relevant articles.
79
+ Args:
80
+ query: The search query.
81
+ """
82
+ search_docs = WikipediaLoader(query=query,
83
+ load_max_docs=WIKIPEDIA_TOP_K_RESULTS,
84
+ doc_content_chars_max=WIKIPEDIA_DOC_CONTENT_CHARS_MAX,
85
+ load_all_available_meta=True).load()
86
+ formatted_search_docs = "\n\n---\n\n".join(
87
+ [
88
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
89
+ for doc in search_docs
90
+ ])
91
+ return {"wiki_results": formatted_search_docs}
92
+
93
 
94
 
95
  @tool
 
134
 
135
  except Exception as e:
136
  return f"An unexpected error occurred during code execution: {e}"
137
+
138
+
139
+ @tool
140
+ def download_taskid_file(task_id: str, file_name: str) -> str:
141
+ """
142
+ Downloads the file associated with the given task_id (if any). Returns the absolute path of the file
143
+ """
144
+ try:
145
+ response = requests.get(f"{DATASET_API_URL}/files/{task_id}", timeout=20)
146
+ response.raise_for_status()
147
+ with open(file_name, 'wb') as file:
148
+ file.write(response.content)
149
+ return os.path.abspath(file_name)
150
+ except Exception as e:
151
+ return "Error occurred: {}".format(e)