Spaces:
Sleeping
Sleeping
Jose-Maria Segui commited on
Commit ·
b0f804a
1
Parent(s): e5c13af
Switch to Groq LLM for faster inference, simplify system prompt
Browse files- agent.py +8 -20
- main.py +19 -13
- system_prompt.txt +1 -3
agent.py
CHANGED
|
@@ -25,11 +25,8 @@ from langchain_community.tools import DuckDuckGoSearchRun
|
|
| 25 |
from langchain_community.document_loaders import WikipediaLoader
|
| 26 |
from langchain_community.document_loaders import ArxivLoader
|
| 27 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 28 |
-
from langchain_huggingface import
|
| 29 |
-
|
| 30 |
-
HuggingFaceEndpoint,
|
| 31 |
-
HuggingFaceEmbeddings,
|
| 32 |
-
)
|
| 33 |
from langchain_community.vectorstores import SupabaseVectorStore
|
| 34 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 35 |
from langchain_core.tools import tool, Tool
|
|
@@ -649,21 +646,12 @@ tools = [
|
|
| 649 |
def build_graph():
|
| 650 |
"""Build the graph"""
|
| 651 |
|
| 652 |
-
# Use
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 659 |
-
task="text-generation",
|
| 660 |
-
max_new_tokens=4096,
|
| 661 |
-
do_sample=False,
|
| 662 |
-
repetition_penalty=1.03,
|
| 663 |
-
temperature=0.01, # Low temperature for factual tasks
|
| 664 |
-
huggingfacehub_api_token=hf_token
|
| 665 |
-
),
|
| 666 |
-
verbose=True,
|
| 667 |
)
|
| 668 |
|
| 669 |
# Bind tools to LLM
|
|
|
|
| 25 |
from langchain_community.document_loaders import WikipediaLoader
|
| 26 |
from langchain_community.document_loaders import ArxivLoader
|
| 27 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 28 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 29 |
+
from langchain_groq import ChatGroq
|
|
|
|
|
|
|
|
|
|
| 30 |
from langchain_community.vectorstores import SupabaseVectorStore
|
| 31 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 32 |
from langchain_core.tools import tool, Tool
|
|
|
|
| 646 |
def build_graph():
|
| 647 |
"""Build the graph"""
|
| 648 |
|
| 649 |
+
# Use Groq (fast, reliable, free tier)
|
| 650 |
+
# Model: qwen/qwen3-32b (same as reference implementation)
|
| 651 |
+
llm = ChatGroq(
|
| 652 |
+
model="qwen/qwen3-32b",
|
| 653 |
+
temperature=0,
|
| 654 |
+
api_key=os.environ.get("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 655 |
)
|
| 656 |
|
| 657 |
# Bind tools to LLM
|
main.py
CHANGED
|
@@ -45,26 +45,32 @@ def download_file(task_id):
|
|
| 45 |
return None
|
| 46 |
|
| 47 |
def clean_answer(raw_response):
|
| 48 |
-
"""Strip
|
| 49 |
if not raw_response:
|
| 50 |
return ""
|
| 51 |
clean = str(raw_response)
|
| 52 |
|
| 53 |
# 1. Look for explicit "FINAL ANSWER:" marker (case insensitive)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
# 2. If no marker,
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
return clean
|
| 68 |
|
| 69 |
def run_agent_on_task(prompt):
|
| 70 |
"""Run the graph agent on a single prompt."""
|
|
|
|
| 45 |
return None
|
| 46 |
|
| 47 |
def clean_answer(raw_response):
|
| 48 |
+
"""Strip everything except the final answer."""
|
| 49 |
if not raw_response:
|
| 50 |
return ""
|
| 51 |
clean = str(raw_response)
|
| 52 |
|
| 53 |
# 1. Look for explicit "FINAL ANSWER:" marker (case insensitive)
|
| 54 |
+
import re
|
| 55 |
+
# Match "FINAL ANSWER:" with any casing
|
| 56 |
+
match = re.search(r'FINAL\s*ANSWER\s*:\s*(.+)', clean, re.IGNORECASE | re.DOTALL)
|
| 57 |
+
if match:
|
| 58 |
+
answer = match.group(1).strip()
|
| 59 |
+
# Remove trailing punctuation that might be added
|
| 60 |
+
answer = answer.rstrip('.')
|
| 61 |
+
# If multiline, take just the first line (the actual answer)
|
| 62 |
+
if '\n' in answer:
|
| 63 |
+
answer = answer.split('\n')[0].strip()
|
| 64 |
+
return answer
|
| 65 |
|
| 66 |
+
# 2. If no marker, the model didn't follow instructions - return empty or last line
|
| 67 |
+
# This signals an error to the evaluation
|
| 68 |
+
lines = [l.strip() for l in clean.strip().split('\n') if l.strip()]
|
| 69 |
+
if lines:
|
| 70 |
+
# Return the last non-empty line as a fallback
|
| 71 |
+
return lines[-1]
|
| 72 |
|
| 73 |
+
return clean.strip()
|
|
|
|
| 74 |
|
| 75 |
def run_agent_on_task(prompt):
|
| 76 |
"""Run the graph agent on a single prompt."""
|
system_prompt.txt
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 2 |
-
|
| 3 |
-
If not, use your other tools to solve the problem.
|
| 4 |
-
Report your thoughts, and finish your answer with the following template:
|
| 5 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 6 |
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 rules above for each element (number or string), ensure there is exactly one space after each comma.
|
| 7 |
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
|
|
|
| 1 |
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 2 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
|
|
|
|
|
|
| 3 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 4 |
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 rules above for each element (number or string), ensure there is exactly one space after each comma.
|
| 5 |
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|