Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,6 +35,19 @@ from langchain.schema import HumanMessage, AIMessage, SystemMessage
|
|
35 |
from langchain.prompts import ChatPromptTemplate
|
36 |
from langgraph.graph import StateGraph, END
|
37 |
from langchain_community.llms import HuggingFacePipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Corrected Tool import: Use 'tool' (lowercase)
|
40 |
from langchain_core.tools import BaseTool, tool
|
@@ -325,42 +338,55 @@ def reasoning_node(state: AgentState) -> AgentState:
|
|
325 |
print("WARNING: No vector store available for RAG")
|
326 |
rag_context = "\n\n[Relevant Knowledge] No knowledge base available."
|
327 |
|
328 |
-
#
|
329 |
-
|
330 |
"You are an expert problem solver, designed to provide concise and accurate answers. "
|
331 |
"Your process involves analyzing the question, intelligently selecting and using tools, "
|
332 |
"and synthesizing information.\n\n"
|
333 |
"**Available Tools:**\n"
|
334 |
f"{tool_descriptions}\n\n"
|
335 |
"**Tool Usage Guidelines:**\n"
|
336 |
-
"- Use **duckduckgo_search** for current events, general facts, or quick lookups. Provide a concise search query
|
337 |
-
"- Use **wikipedia_search** for encyclopedic information, historical context, or detailed topics. Provide a concise search term
|
338 |
-
"- Use **arxiv_search** for scientific papers, research, or cutting-edge technical information. Provide a concise search query
|
339 |
-
"- Use **document_qa** when the question explicitly refers to a specific document or when you have content to query. Input format: 'document_text||question'
|
340 |
-
"- Use **python_execution** for complex calculations, data manipulation, or logical operations that cannot be done with simple reasoning. Always provide the full Python code, ensuring it's valid and executable, and assign the final result to a variable named '_result_value'
|
341 |
-
"- Use **transcript_video** for any question involving video or audio content (e.g., YouTube). Provide the full YouTube URL or video ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
"**Retrieved Context:**\n{rag_context}\n\n"
|
343 |
-
"**Current Context:**\n{context}\n\n"
|
344 |
"**Previous Reasoning Steps:**\n{reasoning}\n\n"
|
345 |
"**Current Task:** {current_task}\n"
|
346 |
"**Current Thoughts:** {current_thoughts}\n\n"
|
347 |
-
"**
|
|
|
348 |
"```json\n"
|
349 |
"{\n"
|
350 |
-
" \"Reasoning\": \"Your
|
351 |
-
" \"Action\": \"
|
352 |
-
" \"Action Input\": \"
|
353 |
"}\n"
|
354 |
"```\n"
|
355 |
-
"
|
356 |
-
"If you cannot determine a suitable tool or a conclusive final answer after exhausting options, return Action: 'Final Answer' with a message like 'I cannot answer this question with the available tools.' or 'More information is needed.'\n"
|
357 |
-
"Ensure 'Action Input' is always the complete, valid input for the chosen 'Action'. If 'Action' is 'Final Answer', provide the complete, concise answer."
|
358 |
)
|
359 |
|
360 |
-
# Create prompt
|
361 |
prompt = ChatPromptTemplate.from_messages([
|
362 |
-
|
363 |
-
|
|
|
|
|
364 |
])
|
365 |
|
366 |
# Format messages safely
|
|
|
35 |
from langchain.prompts import ChatPromptTemplate
|
36 |
from langgraph.graph import StateGraph, END
|
37 |
from langchain_community.llms import HuggingFacePipeline
|
38 |
+
from typing import List, Union, Dict, Any, TypedDict # Ensure all types are imported
|
39 |
+
|
40 |
+
import torch
|
41 |
+
from langchain_core.messages import AIMessage, HumanMessage # Corrected import for message types
|
42 |
+
from langchain_core.tools import BaseTool
|
43 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
44 |
+
from langchain_community.vectorstores import FAISS
|
45 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
46 |
+
from langchain_core.documents import Document
|
47 |
+
from langchain_community.llms import HuggingFacePipeline
|
48 |
+
from langchain.prompts import ChatPromptTemplate # SystemMessage moved to langchain_core.messages
|
49 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
50 |
+
from langgraph.graph import END, StateGraph
|
51 |
|
52 |
# Corrected Tool import: Use 'tool' (lowercase)
|
53 |
from langchain_core.tools import BaseTool, tool
|
|
|
338 |
print("WARNING: No vector store available for RAG")
|
339 |
rag_context = "\n\n[Relevant Knowledge] No knowledge base available."
|
340 |
|
341 |
+
# Renamed the variable to emphasize it's a template string, not a SystemMessage object
|
342 |
+
system_prompt_template_str = (
|
343 |
"You are an expert problem solver, designed to provide concise and accurate answers. "
|
344 |
"Your process involves analyzing the question, intelligently selecting and using tools, "
|
345 |
"and synthesizing information.\n\n"
|
346 |
"**Available Tools:**\n"
|
347 |
f"{tool_descriptions}\n\n"
|
348 |
"**Tool Usage Guidelines:**\n"
|
349 |
+
"- Use **duckduckgo_search** for current events, general facts, or quick lookups. Provide a concise search query. Example: `What is the population of New York?`\n"
|
350 |
+
"- Use **wikipedia_search** for encyclopedic information, historical context, or detailed topics. Provide a concise search term. Example: `Eiffel Tower history`\n"
|
351 |
+
"- Use **arxiv_search** for scientific papers, research, or cutting-edge technical information. Provide a concise search query. Example: `Large Language Models recent advances`\n"
|
352 |
+
"- Use **document_qa** when the question explicitly refers to a specific document or when you have content to query. Input format: 'document_text||question'. Example: `The capital of France is Paris.||What is the capital of France?`\n"
|
353 |
+
"- Use **python_execution** for complex calculations, data manipulation, or logical operations that cannot be done with simple reasoning. Always provide the full Python code, ensuring it's valid and executable, and assign the final result to a variable named '_result_value'. Example: `_result_value = 1 + 1`\n"
|
354 |
+
"- Use **transcript_video** for any question involving video or audio content (e.g., YouTube). Provide the full YouTube URL or video ID. Example: `youtube.com`\n\n"
|
355 |
+
"**Crucial Instructions:**\n"
|
356 |
+
"1. **Always aim to provide a definitive answer.** If you have enough information, use the 'final answer' action.\n"
|
357 |
+
"2. **To provide a final answer, use the Action 'final answer' with the complete answer in 'Action Input'.** This is how you tell me you're done. Example:\n"
|
358 |
+
" ```json\n"
|
359 |
+
" {\n"
|
360 |
+
" \"Reasoning\": \"I have found the capital of France.\",\n"
|
361 |
+
" \"Action\": \"final answer\",\n"
|
362 |
+
" \"Action Input\": \"The capital of France is Paris.\"\n"
|
363 |
+
" }\n"
|
364 |
+
" ```\n"
|
365 |
+
"3. **If you need more information or cannot answer yet, select an appropriate tool and provide a clear, concise query.**\n"
|
366 |
+
"4. **Think step-by-step.** Reflect on previous tool outputs and the question.\n"
|
367 |
+
"5. **Do NOT repeat actions or search queries unless the previous attempt yielded an error.**\n\n"
|
368 |
"**Retrieved Context:**\n{rag_context}\n\n"
|
369 |
+
"**Current Context (Tool Outputs/Intermediate Info):**\n{context}\n\n"
|
370 |
"**Previous Reasoning Steps:**\n{reasoning}\n\n"
|
371 |
"**Current Task:** {current_task}\n"
|
372 |
"**Current Thoughts:** {current_thoughts}\n\n"
|
373 |
+
"**Question:** {question}\n\n"
|
374 |
+
"**Expected JSON Output Format:**\n"
|
375 |
"```json\n"
|
376 |
"{\n"
|
377 |
+
" \"Reasoning\": \"Your reasoning process to decide the next step, including why a tool is chosen or how an answer is derived.\",\n"
|
378 |
+
" \"Action\": \"The name of the tool to use (e.g., duckduckgo_search, final answer, No Action), if no tool is needed yet, use 'No Action'.\",\n"
|
379 |
+
" \"Action Input\": \"The input for the tool (e.g., 'What is the capital of France?', 'The final answer is Paris.').\"\n"
|
380 |
"}\n"
|
381 |
"```\n"
|
382 |
+
"Ensure your response is ONLY valid JSON and strictly follows this format. Begin your response with ````json`."
|
|
|
|
|
383 |
)
|
384 |
|
|
|
385 |
prompt = ChatPromptTemplate.from_messages([
|
386 |
+
# --- CHANGE THIS LINE ---
|
387 |
+
# Pass the system message as a tuple (role, content_template_string)
|
388 |
+
("system", system_prompt_template_str),
|
389 |
+
*state["history"]
|
390 |
])
|
391 |
|
392 |
# Format messages safely
|