Spaces:
Sleeping
Sleeping
tools
#2
by tonio7 - opened
- README.md +2 -1
- agent.py +0 -153
- app.py +3 -8
- requirements.txt +1 -6
- tools.py +0 -5
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: Final
|
| 3 |
emoji: 🕵🏻♂️
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: indigo
|
|
@@ -8,6 +8,7 @@ sdk_version: 5.25.2
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
hf_oauth: true
|
|
|
|
| 11 |
hf_oauth_expiration_minutes: 480
|
| 12 |
---
|
| 13 |
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Template Final Assignment
|
| 3 |
emoji: 🕵🏻♂️
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: indigo
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
hf_oauth: true
|
| 11 |
+
# optional, default duration is 8 hours/480 minutes. Max duration is 30 days/43200 minutes.
|
| 12 |
hf_oauth_expiration_minutes: 480
|
| 13 |
---
|
| 14 |
|
agent.py
DELETED
|
@@ -1,153 +0,0 @@
|
|
| 1 |
-
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 2 |
-
import os
|
| 3 |
-
from tools import search_tool
|
| 4 |
-
from langgraph.graph import StateGraph, START, END, add_messages
|
| 5 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 6 |
-
from typing import TypedDict, Annotated
|
| 7 |
-
from langchain_core.messages import AnyMessage, SystemMessage
|
| 8 |
-
from langchain_mistralai import ChatMistralAI
|
| 9 |
-
|
| 10 |
-
# SYSTEM_PROMPT = (
|
| 11 |
-
# "You are a general AI assistant. "
|
| 12 |
-
# "I will ask you a question. "
|
| 13 |
-
# "Only write your answer, nothing more, following the instructions below. "
|
| 14 |
-
# "Your answer should be a number OR as few words as possible OR "
|
| 15 |
-
# "a comma separated list of numbers and/or strings. "
|
| 16 |
-
# "If you are asked for a number, don't use comma to write your number "
|
| 17 |
-
# "neither use units such as $ or percent sign unless specified otherwise. "
|
| 18 |
-
# "If you are asked for a string, don't use articles, neither abbreviations "
|
| 19 |
-
# "(e.g. for cities), and write the digits in plain text unless specified "
|
| 20 |
-
# "otherwise. "
|
| 21 |
-
# "If you are asked for a comma separated list, apply the above rules "
|
| 22 |
-
# "depending of whether the element to be put in the list is a number or a "
|
| 23 |
-
# "string."
|
| 24 |
-
# )
|
| 25 |
-
|
| 26 |
-
# SYSTEM_PROMPT = """
|
| 27 |
-
# Answer the question with the exact final answer only.
|
| 28 |
-
|
| 29 |
-
# Never output explanations, reasoning, labels, markdown, quotes, or extra punctuation.
|
| 30 |
-
|
| 31 |
-
# Allowed outputs only:
|
| 32 |
-
# - a single number
|
| 33 |
-
# - a single short string
|
| 34 |
-
# - a comma-separated list of numbers and/or strings
|
| 35 |
-
|
| 36 |
-
# Mandatory formatting:
|
| 37 |
-
# - If list: use commas with NO spaces: a,b,c
|
| 38 |
-
# - If number: no thousands separators, no units, no symbols unless explicitly requested
|
| 39 |
-
# - If string: as few words as possible, no articles, no abbreviations unless explicitly requested
|
| 40 |
-
|
| 41 |
-
# Final verification before output:
|
| 42 |
-
# - remove any extra text
|
| 43 |
-
# - remove any spaces around commas
|
| 44 |
-
# - remove any unrequested units or symbols
|
| 45 |
-
|
| 46 |
-
# Output only the answer.
|
| 47 |
-
# """
|
| 48 |
-
|
| 49 |
-
SYSTEM_PROMPT = """
|
| 50 |
-
You are answering a GAIA evaluation item.
|
| 51 |
-
|
| 52 |
-
Your task is to produce the final answer in the exact required format.
|
| 53 |
-
|
| 54 |
-
Output rules:
|
| 55 |
-
- Output the answer only.
|
| 56 |
-
- Do not add any explanation, reasoning, prefix, suffix, label, or punctuation other than what belongs to the answer itself.
|
| 57 |
-
- Do not use markdown.
|
| 58 |
-
- Do not use quotes.
|
| 59 |
-
- Do not add a trailing period.
|
| 60 |
-
- Do not add a newline before or after the answer.
|
| 61 |
-
- If the answer is a single number, output only that number.
|
| 62 |
-
- If the answer is a single string, output only that string.
|
| 63 |
-
- If the answer is a comma-separated list, output the items separated by commas with NO spaces before or after commas.
|
| 64 |
-
|
| 65 |
-
Formatting constraints:
|
| 66 |
-
- Numbers:
|
| 67 |
-
- Do not use thousands separators.
|
| 68 |
-
- Do not use commas inside numbers.
|
| 69 |
-
- Do not add units unless explicitly requested.
|
| 70 |
-
- Do not add symbols such as $, %, or ° unless explicitly requested.
|
| 71 |
-
- Strings:
|
| 72 |
-
- Use as few words as possible.
|
| 73 |
-
- Do not use articles.
|
| 74 |
-
- Do not use abbreviations unless explicitly requested.
|
| 75 |
-
- For city names, country names, person names, etc., write the full form.
|
| 76 |
-
- Write digits in words only if explicitly required by the question or instructions.
|
| 77 |
-
- Comma-separated lists:
|
| 78 |
-
- Use this exact format: item1,item2,item3
|
| 79 |
-
- NO spaces anywhere around commas.
|
| 80 |
-
- Apply the number/string rules to each item individually.
|
| 81 |
-
|
| 82 |
-
Priority order:
|
| 83 |
-
1. Follow the user’s question.
|
| 84 |
-
2. Follow these formatting rules exactly.
|
| 85 |
-
3. When in doubt, prefer the shortest valid answer.
|
| 86 |
-
|
| 87 |
-
Before sending the answer, perform this silent check:
|
| 88 |
-
- Is there any extra text? If yes, remove it.
|
| 89 |
-
- Are there any spaces around commas? If yes, remove them.
|
| 90 |
-
- Are there any articles in a string answer? If yes, remove them.
|
| 91 |
-
- Are there any abbreviations in a string answer? If yes, expand them unless explicitly allowed.
|
| 92 |
-
- Are there any units or symbols not explicitly requested? If yes, remove them.
|
| 93 |
-
|
| 94 |
-
Return only the final answer.
|
| 95 |
-
"""
|
| 96 |
-
|
| 97 |
-
def create_agent():
|
| 98 |
-
# # Generate the chat interface, including the tools
|
| 99 |
-
# llm = HuggingFaceEndpoint(
|
| 100 |
-
# # repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 101 |
-
# # repo_id="deepseek-ai/DeepSeek-R1",
|
| 102 |
-
# repo_id="Qwen/Qwen3-Coder-480B-A35B-Instruct",
|
| 103 |
-
# # repo_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 104 |
-
# # provider="auto",
|
| 105 |
-
# # provider="together",
|
| 106 |
-
# # repo_id="moonshotai/Kimi-K2.5",
|
| 107 |
-
# # provider="auto",
|
| 108 |
-
# huggingfacehub_api_token=os.getenv("HF_TOKEN"),
|
| 109 |
-
# server_kwargs={
|
| 110 |
-
# "bill_to": "HornetSecurity"
|
| 111 |
-
# },
|
| 112 |
-
# )
|
| 113 |
-
|
| 114 |
-
chat = ChatMistralAI(
|
| 115 |
-
model="mistral-large-2512",
|
| 116 |
-
api_key=os.environ["MISTRAL_API_KEY"],
|
| 117 |
-
temperature=1,
|
| 118 |
-
)
|
| 119 |
-
# chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 120 |
-
tools = [search_tool]
|
| 121 |
-
chat_with_tools = chat.bind_tools(tools)
|
| 122 |
-
|
| 123 |
-
# Generate the AgentState and Agent graph
|
| 124 |
-
class AgentState(TypedDict):
|
| 125 |
-
messages: Annotated[list[AnyMessage], add_messages]
|
| 126 |
-
|
| 127 |
-
def assistant(state: AgentState):
|
| 128 |
-
messages_for_model = [
|
| 129 |
-
SystemMessage(content=SYSTEM_PROMPT),
|
| 130 |
-
*state["messages"],
|
| 131 |
-
]
|
| 132 |
-
return {
|
| 133 |
-
"messages": [chat_with_tools.invoke(messages_for_model)],
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
## The graph
|
| 137 |
-
builder = StateGraph(AgentState)
|
| 138 |
-
|
| 139 |
-
# Define nodes: these do the work
|
| 140 |
-
builder.add_node("assistant", assistant)
|
| 141 |
-
builder.add_node("tools", ToolNode(tools))
|
| 142 |
-
|
| 143 |
-
# Define edges: these determine how the control flow moves
|
| 144 |
-
builder.add_edge(START, "assistant")
|
| 145 |
-
builder.add_conditional_edges(
|
| 146 |
-
"assistant",
|
| 147 |
-
# If the latest message requires a tool, route to tools
|
| 148 |
-
# Other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app.py
CHANGED
|
@@ -3,8 +3,6 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from agent import create_agent
|
| 7 |
-
from langchain_core.messages import HumanMessage
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
@@ -14,15 +12,12 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
-
self.agent = create_agent()
|
| 18 |
print("BasicAgent initialized.")
|
| 19 |
def __call__(self, question: str) -> str:
|
| 20 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
response = self.agent.invoke({"messages": [HumanMessage(content=question)]})
|
| 25 |
-
return response['messages'][-1].content
|
| 26 |
|
| 27 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 28 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
|
|
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
+
fixed_answer = "This is a default answer."
|
| 19 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
+
return fixed_answer
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
requirements.txt
CHANGED
|
@@ -1,7 +1,2 @@
|
|
| 1 |
gradio
|
| 2 |
-
requests
|
| 3 |
-
langchain-community>=0.4.1
|
| 4 |
-
langchain-huggingface>=1.2.1
|
| 5 |
-
langgraph>=1.1.3
|
| 6 |
-
ddgs>=9.12.0
|
| 7 |
-
langchain-mistralai>=1.1.2
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools.py
DELETED
|
@@ -1,5 +0,0 @@
|
|
| 1 |
-
from langchain_community.tools import DuckDuckGoSearchRun
|
| 2 |
-
|
| 3 |
-
search_tool = DuckDuckGoSearchRun()
|
| 4 |
-
# results = search_tool.invoke("Who's the current President of France? Give a short answer.")
|
| 5 |
-
# print(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|