Spaces:
Build error
Build error
porla commited on
Commit ·
f7a42c7
1
Parent(s): 81917a3
Refactor agent logic and enhance data handling; add tools for file processing and update system prompt format
Browse files- .gitignore +28 -0
- app.py +21 -5
- fetch_all_data.py +51 -0
- src/my_app.py +127 -0
- src/state.py +14 -0
- src/tools.py +235 -0
- system_prompt.txt +16 -0
- test.py +18 -0
.gitignore
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
results/*
|
| 22 |
+
*.log
|
| 23 |
+
.env
|
| 24 |
+
test.png
|
| 25 |
+
graph.png
|
| 26 |
+
questions_data.json
|
| 27 |
+
.vscode/
|
| 28 |
+
cache
|
app.py
CHANGED
|
@@ -3,6 +3,11 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -13,11 +18,19 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
-
|
|
|
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -54,6 +67,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 54 |
response = requests.get(questions_url, timeout=15)
|
| 55 |
response.raise_for_status()
|
| 56 |
questions_data = response.json()
|
|
|
|
|
|
|
|
|
|
| 57 |
if not questions_data:
|
| 58 |
print("Fetched questions list is empty.")
|
| 59 |
return "Fetched questions list is empty or invalid format.", None
|
|
@@ -80,7 +96,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
submitted_answer = agent(question_text)
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
from src.my_app import build_graph, get_prompt
|
| 10 |
+
import json
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
|
|
| 18 |
class BasicAgent:
|
| 19 |
def __init__(self):
|
| 20 |
print("BasicAgent initialized.")
|
| 21 |
+
self.graph = build_graph() # Build the state graph for the agent
|
| 22 |
+
def __call__(self, question: str, task_id: str) -> str:
|
| 23 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 24 |
+
system_prompt = SystemMessage(content=get_prompt())
|
| 25 |
+
messages = self.graph.invoke({
|
| 26 |
+
"messages": [
|
| 27 |
+
system_prompt,
|
| 28 |
+
{"role": "user", "content": question}
|
| 29 |
+
],
|
| 30 |
+
"task_id": task_id
|
| 31 |
+
})
|
| 32 |
+
answer = messages['messages'][-1].content
|
| 33 |
+
return answer[14:]
|
| 34 |
|
| 35 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 36 |
"""
|
|
|
|
| 67 |
response = requests.get(questions_url, timeout=15)
|
| 68 |
response.raise_for_status()
|
| 69 |
questions_data = response.json()
|
| 70 |
+
# Save questions_data to a local file for inspection/debugging
|
| 71 |
+
with open("questions_data.json", "w", encoding="utf-8") as f:
|
| 72 |
+
json.dump(questions_data, f, ensure_ascii=False, indent=2)
|
| 73 |
if not questions_data:
|
| 74 |
print("Fetched questions list is empty.")
|
| 75 |
return "Fetched questions list is empty or invalid format.", None
|
|
|
|
| 96 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 97 |
continue
|
| 98 |
try:
|
| 99 |
+
submitted_answer = agent(question_text, task_id)
|
| 100 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 101 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 102 |
except Exception as e:
|
fetch_all_data.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Load task IDs from questions_data.json
|
| 6 |
+
questions_file = "/home/pietro/Courses/HF_Agents/Final_Assignment_Template/questions_data.json"
|
| 7 |
+
with open(questions_file, "r", encoding="utf-8") as f:
|
| 8 |
+
questions_data = json.load(f)
|
| 9 |
+
|
| 10 |
+
task_ids = [question["task_id"] for question in questions_data]
|
| 11 |
+
|
| 12 |
+
# Directory to save the results
|
| 13 |
+
output_dir = "results"
|
| 14 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 15 |
+
|
| 16 |
+
base_url = "https://agents-course-unit4-scoring.hf.space/files/{}"
|
| 17 |
+
headers = {"accept": "application/json"}
|
| 18 |
+
|
| 19 |
+
for task_id in task_ids:
|
| 20 |
+
url = base_url.format(task_id)
|
| 21 |
+
response = requests.get(url, headers=headers)
|
| 22 |
+
|
| 23 |
+
# Extract file name from Content-Disposition header if available
|
| 24 |
+
content_disposition = response.headers.get("content-disposition", "")
|
| 25 |
+
if "filename=" in content_disposition:
|
| 26 |
+
file_name = content_disposition.split("filename=")[-1].strip('"')
|
| 27 |
+
else:
|
| 28 |
+
file_name = f"{task_id}.unknown" # Default file name if not provided
|
| 29 |
+
|
| 30 |
+
# Determine file extension based on Content-Type header
|
| 31 |
+
content_type = response.headers.get("content-type", "")
|
| 32 |
+
if "image/png" in content_type:
|
| 33 |
+
file_extension = ".png"
|
| 34 |
+
elif "application/json" in content_type:
|
| 35 |
+
file_extension = ".json"
|
| 36 |
+
elif "audio/mpeg" in content_type:
|
| 37 |
+
file_extension = ".mp3"
|
| 38 |
+
elif "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" in content_type:
|
| 39 |
+
file_extension = ".xlsx"
|
| 40 |
+
elif "text/plain" in content_type:
|
| 41 |
+
file_extension = ".txt"
|
| 42 |
+
elif "application/x-python-code" in content_type or file_name.endswith(".py"):
|
| 43 |
+
file_extension = ".py"
|
| 44 |
+
else:
|
| 45 |
+
file_extension = "" # Unknown file type
|
| 46 |
+
|
| 47 |
+
# Save the file with the appropriate name and extension
|
| 48 |
+
output_path = os.path.join(output_dir, file_name if file_extension else f"{task_id}{file_extension}")
|
| 49 |
+
with open(output_path, "wb") as f:
|
| 50 |
+
f.write(response.content)
|
| 51 |
+
print(f"Saved: {output_path}")
|
src/my_app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import List, Dict, Any, Optional
|
| 3 |
+
from langgraph.graph import StateGraph, START, END
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
from langchain_core.messages import HumanMessage
|
| 6 |
+
from IPython.display import Image, display
|
| 7 |
+
|
| 8 |
+
from langchain_openai import AzureChatOpenAI
|
| 9 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 10 |
+
from langgraph.prebuilt import tools_condition
|
| 11 |
+
|
| 12 |
+
from .tools import reverse_text, is_question_reversed, route_question, avaiable_tools, CustomToolNode
|
| 13 |
+
from langchain_core.messages import SystemMessage
|
| 14 |
+
from .state import State
|
| 15 |
+
# from langgraph.prebuilt import create_react_agent
|
| 16 |
+
from langchain.agents import create_tool_calling_agent
|
| 17 |
+
from langchain_core.runnables import Runnable
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# llm_with_tools = llm.bind_tools(avaiable_tools, parallel_tool_calls=False)
|
| 21 |
+
def get_prompt() -> str:
|
| 22 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 23 |
+
return f.read()
|
| 24 |
+
# return """You are a helpful assistant tasked with answering questions using a set of tools.
|
| 25 |
+
# Now, I will ask you a question. Report your thoughts, show the task_id and finish your answer with the following template:
|
| 26 |
+
# FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 27 |
+
# YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. 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. 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. 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.
|
| 28 |
+
# Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 29 |
+
# """
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def build_graph():
|
| 33 |
+
"""Builds the state graph for the React agent."""
|
| 34 |
+
# Initialize our LLM
|
| 35 |
+
llm = AzureChatOpenAI(
|
| 36 |
+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), # Corrected variable name
|
| 37 |
+
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
|
| 38 |
+
deployment_name=os.getenv("AZURE_OPENAI_DEPLOYMENT"), # Corrected variable name
|
| 39 |
+
openai_api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
| 40 |
+
temperature=0.0,
|
| 41 |
+
)
|
| 42 |
+
llm_with_tools = llm.bind_tools(avaiable_tools)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def assistant(state: State):
|
| 46 |
+
"""Assistant node"""
|
| 47 |
+
response = llm_with_tools.invoke(state["messages"])
|
| 48 |
+
if response.content == '':
|
| 49 |
+
messages = [response] # tool calling message
|
| 50 |
+
else:
|
| 51 |
+
final_message = response.content
|
| 52 |
+
# final_message += f"\n\nTask ID: {state['task_id']}"
|
| 53 |
+
messages = [final_message]
|
| 54 |
+
return {"messages": messages,
|
| 55 |
+
"task_id": state["task_id"]
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Initialize the state graph
|
| 59 |
+
graph_builder = StateGraph(State)
|
| 60 |
+
|
| 61 |
+
# Add nodes
|
| 62 |
+
graph_builder.add_node("check_question_reversed", is_question_reversed)
|
| 63 |
+
graph_builder.add_node("reverse_text", reverse_text)
|
| 64 |
+
graph_builder.add_node("assistant", assistant)
|
| 65 |
+
tools_dict = {tool.name: tool for tool in avaiable_tools}
|
| 66 |
+
|
| 67 |
+
graph_builder.add_node("tools", CustomToolNode(tools_dict))
|
| 68 |
+
|
| 69 |
+
# graph_builder.add_edge(START, "check_question_reversed")
|
| 70 |
+
# graph_builder.add_conditional_edges(
|
| 71 |
+
# "check_question_reversed",
|
| 72 |
+
# route_question,
|
| 73 |
+
# {
|
| 74 |
+
# "question_reversed": "reverse_text",
|
| 75 |
+
# "question_not_reversed": "assistant"
|
| 76 |
+
# }
|
| 77 |
+
# )
|
| 78 |
+
# graph_builder.add_edge("reverse_text", "assistant")
|
| 79 |
+
graph_builder.add_edge(START, "assistant")
|
| 80 |
+
graph_builder.add_conditional_edges(
|
| 81 |
+
"assistant",
|
| 82 |
+
tools_condition,
|
| 83 |
+
)
|
| 84 |
+
graph_builder.add_edge("tools", "assistant")
|
| 85 |
+
graph_builder.add_edge("assistant", END)
|
| 86 |
+
return graph_builder.compile()
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
# Build the graph
|
| 90 |
+
react_graph = build_graph()
|
| 91 |
+
|
| 92 |
+
# Display the graph visualization
|
| 93 |
+
# graph = react_graph.get_graph(xray=True)
|
| 94 |
+
# display(Image(graph.draw_mermaid_png(output_file_path='graph.png')))
|
| 95 |
+
|
| 96 |
+
# Example question to test the agent
|
| 97 |
+
# question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."
|
| 98 |
+
# question = ".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"
|
| 99 |
+
# question = "Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?"
|
| 100 |
+
#question = """Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.\n\nWhat does Teal'c say in response to the question \"Isn't that hot?\""""
|
| 101 |
+
# question = """Hi, I was out sick from my classes on Friday, so I'm trying to figure out what I need to study for my Calculus mid-term next week. My friend from class sent me an audio recording of Professor Willowbrook giving out the recommended reading for the test, but my headphones are broken :(\n\nCould you please listen to the recording for me and tell me the page numbers I'm supposed to go over? I've attached a file called Homework.mp3 that has the recording. Please provide just the page numbers as a comma-delimited list. And please provide the list in ascending order."""
|
| 102 |
+
# question = """The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places."""
|
| 103 |
+
question = """What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?"""
|
| 104 |
+
task_id = "7bd855d8-463d-4ed5-93ca-5fe35145f733"
|
| 105 |
+
system_prompt = SystemMessage(content=get_prompt())
|
| 106 |
+
messages = react_graph.invoke({
|
| 107 |
+
"messages": [
|
| 108 |
+
system_prompt,
|
| 109 |
+
{"role": "user", "content": question}
|
| 110 |
+
],
|
| 111 |
+
"task_id": task_id
|
| 112 |
+
})
|
| 113 |
+
for m in messages["messages"]:
|
| 114 |
+
m.pretty_print()
|
| 115 |
+
|
| 116 |
+
answer = messages['messages'][-1].content
|
| 117 |
+
print(f"Final Answer: {answer[14:]}")
|
| 118 |
+
# Stream the response from the agent
|
| 119 |
+
# events = react_graph.stream(
|
| 120 |
+
# {"messages": [("user", question)]},
|
| 121 |
+
# config={"configurable": {"return_intermediate_steps": True}},
|
| 122 |
+
# stream_mode="values"
|
| 123 |
+
# )
|
| 124 |
+
|
| 125 |
+
# for event in events:
|
| 126 |
+
# print(event) # Replace `_print_event(event, _printed)` with direct printing
|
| 127 |
+
# print("----\n---")
|
src/state.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langgraph.graph.message import add_messages
|
| 2 |
+
from typing import Annotated
|
| 3 |
+
from typing_extensions import TypedDict
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class State(TypedDict):
|
| 8 |
+
# Messages have the type "list". The `add_messages` function
|
| 9 |
+
# in the annotation defines how this state key should be updated
|
| 10 |
+
# (in this case, it appends messages to the list, rather than overwriting them)
|
| 11 |
+
is_question_reversed: bool
|
| 12 |
+
question: str
|
| 13 |
+
messages: Annotated[list, add_messages]
|
| 14 |
+
task_id: str
|
src/tools.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.tools import WikipediaQueryRun
|
| 2 |
+
from langchain_community.utilities import WikipediaAPIWrapper
|
| 3 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 4 |
+
from langchain_community.tools import ArxivQueryRun
|
| 5 |
+
|
| 6 |
+
from langchain_core.messages import HumanMessage
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from langchain_openai import AzureChatOpenAI
|
| 9 |
+
from langchain_perplexity import ChatPerplexity
|
| 10 |
+
from langchain_core.tools import tool
|
| 11 |
+
from langchain_community.tools.tavily_search.tool import TavilySearchResults
|
| 12 |
+
|
| 13 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 14 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 15 |
+
|
| 16 |
+
from pandasai.llm.openai import OpenAI
|
| 17 |
+
from llama_index.readers.pandas_ai import PandasAIReader
|
| 18 |
+
|
| 19 |
+
import os
|
| 20 |
+
import re
|
| 21 |
+
import whisper
|
| 22 |
+
import pandas as pd
|
| 23 |
+
|
| 24 |
+
from .state import State
|
| 25 |
+
|
| 26 |
+
# wikipedia_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 27 |
+
|
| 28 |
+
load_dotenv()
|
| 29 |
+
|
| 30 |
+
# Set your OpenAI API key here
|
| 31 |
+
# os.environ["OPENAI_API_KEY"] = "sk-xxxxx" # Replace with your actual API key
|
| 32 |
+
|
| 33 |
+
# Initialize our LLM
|
| 34 |
+
llm = AzureChatOpenAI(
|
| 35 |
+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), # Corrected variable name
|
| 36 |
+
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
|
| 37 |
+
deployment_name=os.getenv("AZURE_OPENAI_DEPLOYMENT"), # Corrected variable name
|
| 38 |
+
openai_api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
| 39 |
+
temperature=0.0,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
hf_endpoint = HuggingFaceEndpoint(
|
| 43 |
+
# repo_id="microsoft/Phi-3-mini-4k-instruct",
|
| 44 |
+
repo_id="Qwen/Qwen3-4B",
|
| 45 |
+
task="text-generation",
|
| 46 |
+
max_new_tokens=1000,
|
| 47 |
+
do_sample=False,
|
| 48 |
+
)
|
| 49 |
+
llm_hf = ChatHuggingFace(llm=hf_endpoint, verbose=True)
|
| 50 |
+
|
| 51 |
+
@tool
|
| 52 |
+
def get_youtube_transcript(video_id: str) -> list:
|
| 53 |
+
"""Fetches the transcript of a YouTube video."""
|
| 54 |
+
ytt_api = YouTubeTranscriptApi()
|
| 55 |
+
fetched_transcript = ytt_api.fetch(video_id)
|
| 56 |
+
return fetched_transcript.to_raw_data()
|
| 57 |
+
|
| 58 |
+
@tool
|
| 59 |
+
def transcript_mp3_audio(task_id: str) -> str:
|
| 60 |
+
"""Transcribes an MP3 audio file using a speech-to-text model."""
|
| 61 |
+
file_path = f"{task_id}.mp3" # Assuming the file is named with the task_id
|
| 62 |
+
file_path = os.path.join('results', file_path)
|
| 63 |
+
model = whisper.load_model("base") # puoi usare tiny, base, small, medium, large
|
| 64 |
+
result = model.transcribe(file_path)
|
| 65 |
+
return result["text"]
|
| 66 |
+
|
| 67 |
+
@tool
|
| 68 |
+
def load_and_analyze_excel_file(question: str, task_id: str) -> str:
|
| 69 |
+
"""Loads an Excel file and analyzes it using PandasAI."""
|
| 70 |
+
file_path = f"{task_id}.xlsx" # Assuming the file is named with the task_id
|
| 71 |
+
file_path = os.path.join('results', file_path)
|
| 72 |
+
df = pd.read_excel(file_path)
|
| 73 |
+
reader = PandasAIReader(pandas_llm=llm)
|
| 74 |
+
response = reader.run_pandas_ai(
|
| 75 |
+
df, question, is_conversational_answer=True
|
| 76 |
+
)
|
| 77 |
+
return response
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def reverse_text(state: State) -> State:
|
| 82 |
+
"""Reverses the input text."""
|
| 83 |
+
reversed_text = state["question"]
|
| 84 |
+
print(f"Reversing text: {reversed_text}")
|
| 85 |
+
text = reversed_text[::-1]
|
| 86 |
+
message = [
|
| 87 |
+
{"role": "user", "content": text},
|
| 88 |
+
]
|
| 89 |
+
return {
|
| 90 |
+
"is_question_reversed": False,
|
| 91 |
+
"messages": message,
|
| 92 |
+
"question": text,
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
def is_question_reversed(state: State) -> State:
|
| 96 |
+
|
| 97 |
+
"""Checks if the question is reversed though an LLM."""
|
| 98 |
+
question = state["messages"][-1].content # Get the last user message
|
| 99 |
+
|
| 100 |
+
# Prepare our prompt for the LLM
|
| 101 |
+
prompt = f"""
|
| 102 |
+
You are given a question. Determine whether it is written normally or in reverse (backwards).
|
| 103 |
+
Only answer with 'normal' or 'reversed'.
|
| 104 |
+
|
| 105 |
+
Question: {question}
|
| 106 |
+
Answer:
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
# Call the LLM
|
| 110 |
+
messages = [HumanMessage(content=prompt)]
|
| 111 |
+
response = llm_hf.invoke(messages)
|
| 112 |
+
|
| 113 |
+
# Simple logic to parse the response (in a real app, you'd want more robust parsing)
|
| 114 |
+
response_text = response.content.lower()
|
| 115 |
+
response_text = re.sub(r"<think>.*?</think>", "", response_text, flags=re.DOTALL).strip()
|
| 116 |
+
|
| 117 |
+
is_reversed = "reversed" in response_text and "normal" not in response_text
|
| 118 |
+
|
| 119 |
+
# Update messages for tracking
|
| 120 |
+
if is_reversed:
|
| 121 |
+
new_messages = [
|
| 122 |
+
{"role": "user", "content": prompt},
|
| 123 |
+
{"role": "assistant", "content": response_text}
|
| 124 |
+
]
|
| 125 |
+
else:
|
| 126 |
+
new_messages = state.get("messages", []) + [
|
| 127 |
+
{"role": "user", "content": prompt},
|
| 128 |
+
{"role": "assistant", "content": response_text}
|
| 129 |
+
]
|
| 130 |
+
|
| 131 |
+
# Return state updates
|
| 132 |
+
return {
|
| 133 |
+
"is_question_reversed": is_reversed,
|
| 134 |
+
"messages": new_messages,
|
| 135 |
+
"question": question,
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
def route_question(state: State) -> str:
|
| 139 |
+
"""Determine the next step based on whether the question is reversed or not."""
|
| 140 |
+
if state["is_question_reversed"]:
|
| 141 |
+
return "question_reversed"
|
| 142 |
+
else:
|
| 143 |
+
return "question_not_reversed"
|
| 144 |
+
|
| 145 |
+
# web_search_tool = DuckDuckGoSearchRun()
|
| 146 |
+
web_search_tool = TavilySearchResults()
|
| 147 |
+
arxiv_search_tool = ArxivQueryRun()
|
| 148 |
+
wikipedia_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
class CustomToolNode:
|
| 152 |
+
"""Tool node personalizzato che può accedere allo stato completo"""
|
| 153 |
+
|
| 154 |
+
def __init__(self, tools_dict):
|
| 155 |
+
self.tools_dict = tools_dict
|
| 156 |
+
|
| 157 |
+
def __call__(self, state):
|
| 158 |
+
messages = state["messages"]
|
| 159 |
+
last_message = messages[-1]
|
| 160 |
+
|
| 161 |
+
# Estrai i tool calls dall'ultimo messaggio
|
| 162 |
+
if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
|
| 163 |
+
results = []
|
| 164 |
+
for tool_call in last_message.tool_calls:
|
| 165 |
+
tool_name = tool_call["name"]
|
| 166 |
+
tool_args = tool_call["args"]
|
| 167 |
+
|
| 168 |
+
# Aggiungi task_id agli argomenti del tool
|
| 169 |
+
tool_args_with_state = {
|
| 170 |
+
**tool_args,
|
| 171 |
+
"task_id": state["task_id"],
|
| 172 |
+
"state": state # Opzionale: passa tutto lo stato
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
if tool_name in self.tools_dict:
|
| 176 |
+
try:
|
| 177 |
+
result = self.tools_dict[tool_name].invoke(tool_args_with_state)
|
| 178 |
+
results.append({
|
| 179 |
+
"type": "tool",
|
| 180 |
+
"name": tool_name,
|
| 181 |
+
"tool_call_id": tool_call["id"],
|
| 182 |
+
"content": str(result)
|
| 183 |
+
})
|
| 184 |
+
except Exception as e:
|
| 185 |
+
results.append({
|
| 186 |
+
"type": "tool",
|
| 187 |
+
"name": tool_name,
|
| 188 |
+
"tool_call_id": tool_call["id"],
|
| 189 |
+
"content": f"Error: {str(e)}"
|
| 190 |
+
})
|
| 191 |
+
|
| 192 |
+
return {"messages": results}
|
| 193 |
+
|
| 194 |
+
return {"messages": []}
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
avaiable_tools = [
|
| 199 |
+
wikipedia_tool,
|
| 200 |
+
arxiv_search_tool,
|
| 201 |
+
web_search_tool,
|
| 202 |
+
get_youtube_transcript,
|
| 203 |
+
transcript_mp3_audio,
|
| 204 |
+
load_and_analyze_excel_file
|
| 205 |
+
]
|
| 206 |
+
# response = wikipedia_tool.run("HUNTER X HUNTER")
|
| 207 |
+
# print(response)
|
| 208 |
+
|
| 209 |
+
# 1) tool_for_fetch_wikipedia_data
|
| 210 |
+
|
| 211 |
+
# 2) fetch youtube video data
|
| 212 |
+
|
| 213 |
+
# 3 ) reverse del testo
|
| 214 |
+
|
| 215 |
+
# 4 ) tool/agente che valuta se la domanda è sensata o è scritta al contrario
|
| 216 |
+
|
| 217 |
+
# 5) chess-image-to-dict
|
| 218 |
+
|
| 219 |
+
# 6) chess agent
|
| 220 |
+
|
| 221 |
+
# 7) general python code execution tool
|
| 222 |
+
|
| 223 |
+
# 8) get trascript from youtube video
|
| 224 |
+
|
| 225 |
+
# 9) web-search tool
|
| 226 |
+
|
| 227 |
+
# 10) fetch page content
|
| 228 |
+
|
| 229 |
+
# 11) trascribe mp3 audio file
|
| 230 |
+
|
| 231 |
+
# 12) read mp3 audio file
|
| 232 |
+
|
| 233 |
+
# 13) Research paper MCP
|
| 234 |
+
|
| 235 |
+
# 14) read excel file
|
system_prompt.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 2 |
+
|
| 3 |
+
Your final answer must strictly follow this format:
|
| 4 |
+
FINAL ANSWER: [ANSWER]
|
| 5 |
+
|
| 6 |
+
Only write the answer in that exact format. Do not explain anything. Do not include any other text.
|
| 7 |
+
|
| 8 |
+
If the question refers to attachments or external files, there are specific tools available for this. These tools accept a task_id as an argument, retrieve the corresponding file, and execute the required functionality.
|
| 9 |
+
|
| 10 |
+
Examples:
|
| 11 |
+
- FINAL ANSWER: FunkMonk
|
| 12 |
+
- FINAL ANSWER: Paris
|
| 13 |
+
- FINAL ANSWER: 128
|
| 14 |
+
- FINAL ANSWER: 123, 126, 128, 130
|
| 15 |
+
|
| 16 |
+
If you do not follow this format exactly, your response will be considered incorrect.
|
test.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# def get_youtube_transcript(video_url: str) -> list:
|
| 6 |
+
# """Fetches the transcript of a YouTube video."""
|
| 7 |
+
# ytt_api = YouTubeTranscriptApi()
|
| 8 |
+
# fetched_transcript = ytt_api.fetch(video_url)
|
| 9 |
+
# return fetched_transcript.to_raw_data()
|
| 10 |
+
|
| 11 |
+
# url = '1htKBjuUWec'
|
| 12 |
+
# transcript = get_youtube_transcript(url)
|
| 13 |
+
# print(transcript)
|
| 14 |
+
|
| 15 |
+
# Leggi un file xlsx con pandas
|
| 16 |
+
file_path = 'results/7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx'
|
| 17 |
+
df = pd.read_excel(file_path)
|
| 18 |
+
print(df)
|