Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ import os
|
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 5 |
-
#
|
| 6 |
-
from langchain.agents import
|
| 7 |
from langchain_core.tools import Tool
|
| 8 |
from langchain_core.prompts import PromptTemplate
|
| 9 |
from duckduckgo_search import DDGS
|
|
@@ -11,15 +11,14 @@ from duckduckgo_search import DDGS
|
|
| 11 |
app = Flask(__name__)
|
| 12 |
CORS(app)
|
| 13 |
|
| 14 |
-
# 1. Setup LLM
|
| 15 |
sec_token = os.getenv("HF_TOKEN")
|
| 16 |
llm = HuggingFaceEndpoint(
|
| 17 |
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 18 |
huggingfacehub_api_token=sec_token,
|
| 19 |
-
timeout=300
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# 2.
|
| 23 |
def manual_search(query: str):
|
| 24 |
try:
|
| 25 |
with DDGS() as ddgs:
|
|
@@ -32,22 +31,20 @@ tools = [
|
|
| 32 |
Tool(
|
| 33 |
name="Search",
|
| 34 |
func=manual_search,
|
| 35 |
-
description="
|
| 36 |
)
|
| 37 |
]
|
| 38 |
|
| 39 |
-
# 3.
|
| 40 |
-
template = """Answer the following questions
|
| 41 |
{tools}
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
Thought: you should always think about what to do
|
| 46 |
Action: the action to take, should be one of [{tool_names}]
|
| 47 |
Action Input: the input to the action
|
| 48 |
Observation: the result of the action
|
| 49 |
-
... (
|
| 50 |
-
Thought: I now know the final answer
|
| 51 |
Final Answer: the final answer
|
| 52 |
|
| 53 |
Begin!
|
|
@@ -56,14 +53,9 @@ Thought: {agent_scratchpad}"""
|
|
| 56 |
|
| 57 |
prompt = PromptTemplate.from_template(template)
|
| 58 |
|
| 59 |
-
# 4.
|
| 60 |
agent = create_react_agent(llm, tools, prompt)
|
| 61 |
-
agent_executor = AgentExecutor(
|
| 62 |
-
agent=agent,
|
| 63 |
-
tools=tools,
|
| 64 |
-
verbose=True,
|
| 65 |
-
handle_parsing_errors=True
|
| 66 |
-
)
|
| 67 |
|
| 68 |
@app.route('/')
|
| 69 |
def index():
|
|
@@ -73,9 +65,9 @@ def index():
|
|
| 73 |
def ask():
|
| 74 |
try:
|
| 75 |
data = request.get_json()
|
| 76 |
-
|
| 77 |
# Run agent
|
| 78 |
-
result = agent_executor.invoke({"input":
|
| 79 |
return jsonify({"answer": result["output"]})
|
| 80 |
except Exception as e:
|
| 81 |
return jsonify({"answer": f"System Error: {str(e)}"}), 500
|
|
|
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 5 |
+
# THE STABLE IMPORTS - NO MORE MISSING NAMES
|
| 6 |
+
from langchain.agents import AgentExecutor, create_react_agent
|
| 7 |
from langchain_core.tools import Tool
|
| 8 |
from langchain_core.prompts import PromptTemplate
|
| 9 |
from duckduckgo_search import DDGS
|
|
|
|
| 11 |
app = Flask(__name__)
|
| 12 |
CORS(app)
|
| 13 |
|
| 14 |
+
# 1. Setup LLM
|
| 15 |
sec_token = os.getenv("HF_TOKEN")
|
| 16 |
llm = HuggingFaceEndpoint(
|
| 17 |
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 18 |
huggingfacehub_api_token=sec_token,
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# 2. Manual Search
|
| 22 |
def manual_search(query: str):
|
| 23 |
try:
|
| 24 |
with DDGS() as ddgs:
|
|
|
|
| 31 |
Tool(
|
| 32 |
name="Search",
|
| 33 |
func=manual_search,
|
| 34 |
+
description="Useful for finding current info on the web."
|
| 35 |
)
|
| 36 |
]
|
| 37 |
|
| 38 |
+
# 3. Explicit Prompt Template (This is required for create_react_agent)
|
| 39 |
+
template = """Answer the following questions:
|
| 40 |
{tools}
|
| 41 |
+
Format:
|
| 42 |
+
Question: {input}
|
| 43 |
+
Thought: {agent_scratchpad}
|
|
|
|
| 44 |
Action: the action to take, should be one of [{tool_names}]
|
| 45 |
Action Input: the input to the action
|
| 46 |
Observation: the result of the action
|
| 47 |
+
... (repeat)
|
|
|
|
| 48 |
Final Answer: the final answer
|
| 49 |
|
| 50 |
Begin!
|
|
|
|
| 53 |
|
| 54 |
prompt = PromptTemplate.from_template(template)
|
| 55 |
|
| 56 |
+
# 4. Initialize Agent
|
| 57 |
agent = create_react_agent(llm, tools, prompt)
|
| 58 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, handle_parsing_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
@app.route('/')
|
| 61 |
def index():
|
|
|
|
| 65 |
def ask():
|
| 66 |
try:
|
| 67 |
data = request.get_json()
|
| 68 |
+
query = data.get("query")
|
| 69 |
# Run agent
|
| 70 |
+
result = agent_executor.invoke({"input": query})
|
| 71 |
return jsonify({"answer": result["output"]})
|
| 72 |
except Exception as e:
|
| 73 |
return jsonify({"answer": f"System Error: {str(e)}"}), 500
|