Spaces:
Sleeping
Sleeping
File size: 5,738 Bytes
0ef0b85 914e091 0ef0b85 fba7621 c9285dc fba7621 de2f2dd 9c9687b de2f2dd 9c9687b de2f2dd 9c9687b de2f2dd 9c9687b de2f2dd 9c9687b de2f2dd 9c9687b de2f2dd 9c9687b de2f2dd 0ef0b85 c9285dc 0ef0b85 c9285dc c3d1e2c c9285dc 0ef0b85 fba7621 0ef0b85 fba7621 0ef0b85 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
import os
from tools import search_tool
from langgraph.graph import StateGraph, START, END, add_messages
from langgraph.prebuilt import ToolNode, tools_condition
from typing import TypedDict, Annotated
from langchain_core.messages import AnyMessage, SystemMessage
from langchain_mistralai import ChatMistralAI
# SYSTEM_PROMPT = (
# "You are a general AI assistant. "
# "I will ask you a question. "
# "Only write your answer, nothing more, following the instructions below. "
# "Your 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."
# )
# SYSTEM_PROMPT = """
# Answer the question with the exact final answer only.
# Never output explanations, reasoning, labels, markdown, quotes, or extra punctuation.
# Allowed outputs only:
# - a single number
# - a single short string
# - a comma-separated list of numbers and/or strings
# Mandatory formatting:
# - If list: use commas with NO spaces: a,b,c
# - If number: no thousands separators, no units, no symbols unless explicitly requested
# - If string: as few words as possible, no articles, no abbreviations unless explicitly requested
# Final verification before output:
# - remove any extra text
# - remove any spaces around commas
# - remove any unrequested units or symbols
# Output only the answer.
# """
SYSTEM_PROMPT = """
You are answering a GAIA evaluation item.
Your task is to produce the final answer in the exact required format.
Output rules:
- Output the answer only.
- Do not add any explanation, reasoning, prefix, suffix, label, or punctuation other than what belongs to the answer itself.
- Do not use markdown.
- Do not use quotes.
- Do not add a trailing period.
- Do not add a newline before or after the answer.
- If the answer is a single number, output only that number.
- If the answer is a single string, output only that string.
- If the answer is a comma-separated list, output the items separated by commas with NO spaces before or after commas.
Formatting constraints:
- Numbers:
- Do not use thousands separators.
- Do not use commas inside numbers.
- Do not add units unless explicitly requested.
- Do not add symbols such as $, %, or ° unless explicitly requested.
- Strings:
- Use as few words as possible.
- Do not use articles.
- Do not use abbreviations unless explicitly requested.
- For city names, country names, person names, etc., write the full form.
- Write digits in words only if explicitly required by the question or instructions.
- Comma-separated lists:
- Use this exact format: item1,item2,item3
- NO spaces anywhere around commas.
- Apply the number/string rules to each item individually.
Priority order:
1. Follow the user’s question.
2. Follow these formatting rules exactly.
3. When in doubt, prefer the shortest valid answer.
Before sending the answer, perform this silent check:
- Is there any extra text? If yes, remove it.
- Are there any spaces around commas? If yes, remove them.
- Are there any articles in a string answer? If yes, remove them.
- Are there any abbreviations in a string answer? If yes, expand them unless explicitly allowed.
- Are there any units or symbols not explicitly requested? If yes, remove them.
Return only the final answer.
"""
def create_agent():
# # Generate the chat interface, including the tools
# llm = HuggingFaceEndpoint(
# # repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
# # repo_id="deepseek-ai/DeepSeek-R1",
# repo_id="Qwen/Qwen3-Coder-480B-A35B-Instruct",
# # repo_id="meta-llama/Llama-3.1-8B-Instruct",
# # provider="auto",
# # provider="together",
# # repo_id="moonshotai/Kimi-K2.5",
# # provider="auto",
# huggingfacehub_api_token=os.getenv("HF_TOKEN"),
# server_kwargs={
# "bill_to": "HornetSecurity"
# },
# )
chat = ChatMistralAI(
model="mistral-large-2512",
api_key=os.environ["MISTRAL_API_KEY"],
temperature=1,
)
# chat = ChatHuggingFace(llm=llm, verbose=True)
tools = [search_tool]
chat_with_tools = chat.bind_tools(tools)
# Generate the AgentState and Agent graph
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
def assistant(state: AgentState):
messages_for_model = [
SystemMessage(content=SYSTEM_PROMPT),
*state["messages"],
]
return {
"messages": [chat_with_tools.invoke(messages_for_model)],
}
## The graph
builder = StateGraph(AgentState)
# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Define edges: these determine how the control flow moves
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message requires a tool, route to tools
# Otherwise, provide a direct response
tools_condition,
)
builder.add_edge("tools", "assistant")
agent = builder.compile()
return agent
|