Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,13 +26,20 @@ if "Mistral" not in os.environ:
|
|
| 26 |
os.environ["Mistral"] = getpass.getpass("Enter your Mistral API key: ")
|
| 27 |
|
| 28 |
print("Loading LLM...")
|
| 29 |
-
|
| 30 |
model="pixtral-large-2411",
|
| 31 |
temperature=0.01,
|
| 32 |
max_retries=6,
|
| 33 |
mistral_api_key = os.getenv("Mistral")
|
| 34 |
)
|
| 35 |
-
print(f"Model {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# (Keep Constants as is)
|
| 38 |
# --- Constants ---
|
|
@@ -133,8 +140,7 @@ run_code_tool = Tool(
|
|
| 133 |
description="Executes a python code."
|
| 134 |
)
|
| 135 |
|
| 136 |
-
|
| 137 |
-
chat_with_tools = chat.bind_tools(tools, parallel_tool_calls=False)
|
| 138 |
|
| 139 |
|
| 140 |
# Generate the AgentState and Agent graph
|
|
@@ -142,59 +148,68 @@ class AgentState(TypedDict):
|
|
| 142 |
messages: Annotated[list[AnyMessage], add_messages]
|
| 143 |
|
| 144 |
|
| 145 |
-
def assistant(state: AgentState):
|
| 146 |
return {
|
| 147 |
"messages": chat.invoke(state["messages"]),
|
| 148 |
}
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
builder
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
builder.
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
# If the latest message requires a tool, route to tools
|
| 163 |
-
# Otherwise, provide a direct response
|
| 164 |
-
tools_condition
|
| 165 |
-
)
|
| 166 |
-
builder.add_edge("tools", "assistant")
|
| 167 |
-
alfred = builder.compile()
|
| 168 |
-
system_prompt = SystemMessage(
|
| 169 |
-
content="You are a general AI assistant. \
|
| 170 |
-
I will ask you a question. Report your thoughts shortly, and finish your answer with the following template: \
|
| 171 |
-
FINAL ANSWER: YOUR FINAL ANSWER \
|
| 172 |
-
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. \
|
| 173 |
-
If you are asked for a number, use only digits in your final answer. Don't use comma nor brackets to write your number neither use units such as $ or percent sign unless specified otherwise. \
|
| 174 |
-
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. \
|
| 175 |
-
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. Wrap the list in [square brackets].\
|
| 176 |
-
If there is a file attached, open the file and read it. \
|
| 177 |
-
If you don't have enough references to answer, use your tools, search the web, run your code or convert data to a data frame, whatever helps. \
|
| 178 |
-
If the question refers to an external content and there is no reference file attached, perform a web search and retrieve relevant information from the internet. \
|
| 179 |
-
If there is a code, execute it. \
|
| 180 |
-
Make sure that each final answer is preceded with 'FINAL ANSWER:' and is short: it should contain a number (without full stop at the end), a string (one or two words only, without full stop at the end) or a comma-separated list (without full stops at the end), nothing else. "
|
| 181 |
)
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
class BasicAgent:
|
| 184 |
def __init__(self):
|
| 185 |
print("BasicAgent initialized.")
|
| 186 |
|
| 187 |
-
def __call__(self, question: str) -> str:
|
|
|
|
| 188 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
message = HumanMessage(content=question)
|
| 190 |
print(message)
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
answer = "".join(re.findall(r'(FINAL ANSWER.*)', answer, flags=re.M))
|
| 193 |
answer = answer.replace('FINAL ANSWER:', '')
|
| 194 |
answer = answer.replace('FINAL ANSWER', '')
|
| 195 |
answer = answer.replace('YOUR ', '')
|
| 196 |
answer = answer.replace('*', '')
|
| 197 |
-
print(f"Agent returning fixed answer: {answer}")
|
|
|
|
| 198 |
return answer
|
| 199 |
|
| 200 |
|
|
|
|
| 26 |
os.environ["Mistral"] = getpass.getpass("Enter your Mistral API key: ")
|
| 27 |
|
| 28 |
print("Loading LLM...")
|
| 29 |
+
chat1 = ChatMistralAI(
|
| 30 |
model="pixtral-large-2411",
|
| 31 |
temperature=0.01,
|
| 32 |
max_retries=6,
|
| 33 |
mistral_api_key = os.getenv("Mistral")
|
| 34 |
)
|
| 35 |
+
print(f"Model {chat1.model} downloaded!")
|
| 36 |
+
chat2 = ChatMistralAI(
|
| 37 |
+
model="mistral-medium-2505",
|
| 38 |
+
temperature=0.01,
|
| 39 |
+
max_retries=6,
|
| 40 |
+
mistral_api_key = os.getenv("Mistral")
|
| 41 |
+
)
|
| 42 |
+
print(f"Model {chat2.model} downloaded!")
|
| 43 |
|
| 44 |
# (Keep Constants as is)
|
| 45 |
# --- Constants ---
|
|
|
|
| 140 |
description="Executes a python code."
|
| 141 |
)
|
| 142 |
|
| 143 |
+
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
# Generate the AgentState and Agent graph
|
|
|
|
| 148 |
messages: Annotated[list[AnyMessage], add_messages]
|
| 149 |
|
| 150 |
|
| 151 |
+
def assistant(state: AgentState, chat):
|
| 152 |
return {
|
| 153 |
"messages": chat.invoke(state["messages"]),
|
| 154 |
}
|
| 155 |
|
| 156 |
+
def build_agent(chat):
|
| 157 |
+
tools = [get_file_path_tool, get_content_tool, search_web_tool, extract_text_tool, play_chess_tool, run_code_tool]
|
| 158 |
+
chat_with_tools = chat.bind_tools(tools, parallel_tool_calls=False)
|
| 159 |
+
# The graph
|
| 160 |
+
builder = StateGraph(AgentState)
|
| 161 |
|
| 162 |
+
# Define nodes: these do the work
|
| 163 |
+
builder.add_node("assistant", assistant)
|
| 164 |
+
builder.add_node("tools", ToolNode([get_file_path_tool]))
|
| 165 |
+
|
| 166 |
+
# Define edges: these determine how the control flow moves
|
| 167 |
+
builder.add_edge(START, "assistant")
|
| 168 |
+
builder.add_conditional_edges(
|
| 169 |
+
"assistant",
|
| 170 |
+
# If the latest message requires a tool, route to tools
|
| 171 |
+
# Otherwise, provide a direct response
|
| 172 |
+
tools_condition
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
)
|
| 174 |
+
builder.add_edge("tools", "assistant")
|
| 175 |
+
alfred = builder.compile()
|
| 176 |
+
return alfred
|
| 177 |
|
| 178 |
class BasicAgent:
|
| 179 |
def __init__(self):
|
| 180 |
print("BasicAgent initialized.")
|
| 181 |
|
| 182 |
+
def __call__(self, question: str) -> str:
|
| 183 |
+
|
| 184 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 185 |
+
system_prompt = SystemMessage(
|
| 186 |
+
content="You are a general AI assistant. \
|
| 187 |
+
I will ask you a question. Report your thoughts shortly, and finish your answer with the following template: \
|
| 188 |
+
FINAL ANSWER: YOUR FINAL ANSWER \
|
| 189 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. \
|
| 190 |
+
If you are asked for a number, use only digits in your final answer. Don't use comma nor brackets to write your number neither use units such as $ or percent sign unless specified otherwise. \
|
| 191 |
+
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. \
|
| 192 |
+
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. Wrap the list in [square brackets].\
|
| 193 |
+
If there is a file attached, open the file and read it. \
|
| 194 |
+
If you don't have enough references to answer, use your tools, search the web, run your code or convert data to a data frame, whatever helps. \
|
| 195 |
+
If the question refers to an external content and there is no reference file attached, perform a web search and retrieve relevant information from the internet. \
|
| 196 |
+
If there is a code, execute it. \
|
| 197 |
+
Make sure that each final answer is preceded with 'FINAL ANSWER:' and is short: it should contain a number (without full stop at the end), a string (one or two words only, without full stop at the end) or a comma-separated list (without full stops at the end), nothing else. "
|
| 198 |
+
)
|
| 199 |
message = HumanMessage(content=question)
|
| 200 |
print(message)
|
| 201 |
+
try:
|
| 202 |
+
alfred = build_agent(chat1)
|
| 203 |
+
answer = alfred.invoke(input={"messages": [system_prompt, message]},config={"recursion_limit": 6})['messages'][-1].content
|
| 204 |
+
except Exception as e:
|
| 205 |
+
alfred = build_agent(chat2)
|
| 206 |
answer = "".join(re.findall(r'(FINAL ANSWER.*)', answer, flags=re.M))
|
| 207 |
answer = answer.replace('FINAL ANSWER:', '')
|
| 208 |
answer = answer.replace('FINAL ANSWER', '')
|
| 209 |
answer = answer.replace('YOUR ', '')
|
| 210 |
answer = answer.replace('*', '')
|
| 211 |
+
print(f"Agent returning fixed answer: {answer}")
|
| 212 |
+
|
| 213 |
return answer
|
| 214 |
|
| 215 |
|