Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,40 +10,38 @@ from langchain_huggingface import HuggingFaceEndpoint
|
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
-
# ---
|
| 14 |
class LangChainAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("Initializing LangChainAgent...")
|
| 17 |
|
| 18 |
-
# 1. Set up the LLM
|
| 19 |
self.llm = HuggingFaceEndpoint(
|
| 20 |
-
repo_id="
|
| 21 |
-
task="
|
| 22 |
max_new_tokens=512,
|
| 23 |
-
do_sample=False,
|
| 24 |
)
|
| 25 |
-
print("LLM initialized with
|
| 26 |
|
| 27 |
-
# 2. Define the Tools
|
| 28 |
self.tools = [DuckDuckGoSearchRun()]
|
| 29 |
print("Tools initialized.")
|
| 30 |
|
| 31 |
-
# 3. Get the
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
print("Prompt template pulled: hwchase17/react-chat")
|
| 35 |
|
| 36 |
-
# 4. Create the
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
print("Chat Agent created.")
|
| 40 |
|
| 41 |
-
# 5. Create the Agent Executor
|
| 42 |
self.agent_executor = AgentExecutor(
|
| 43 |
agent=self.agent,
|
| 44 |
tools=self.tools,
|
| 45 |
verbose=True,
|
| 46 |
-
|
|
|
|
| 47 |
max_iterations=5,
|
| 48 |
)
|
| 49 |
print("Agent Executor created. Initialization complete.")
|
|
@@ -55,11 +53,7 @@ class LangChainAgent:
|
|
| 55 |
print(f"Agent received question: {question}")
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
response = self.agent_executor.invoke({
|
| 60 |
-
"input": question,
|
| 61 |
-
"chat_history": []
|
| 62 |
-
})
|
| 63 |
final_answer = response.get("output", "Error: Could not parse final answer.")
|
| 64 |
except Exception as e:
|
| 65 |
print(f"Error during agent execution: {e}")
|
|
|
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
+
# --- Final LangChain Agent Definition ---
|
| 14 |
class LangChainAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("Initializing LangChainAgent...")
|
| 17 |
|
| 18 |
+
# 1. Set up the LLM using the stable Flan-T5 model
|
| 19 |
self.llm = HuggingFaceEndpoint(
|
| 20 |
+
repo_id="google/flan-t5-xxl",
|
| 21 |
+
task="text-generation",
|
| 22 |
max_new_tokens=512,
|
|
|
|
| 23 |
)
|
| 24 |
+
print("LLM initialized with google/flan-t5-xxl.")
|
| 25 |
|
| 26 |
+
# 2. Define the Tools
|
| 27 |
self.tools = [DuckDuckGoSearchRun()]
|
| 28 |
print("Tools initialized.")
|
| 29 |
|
| 30 |
+
# 3. Get the Prompt Template
|
| 31 |
+
self.prompt = hub.pull("hwchase17/react")
|
| 32 |
+
print("Prompt template pulled.")
|
|
|
|
| 33 |
|
| 34 |
+
# 4. Create the Agent
|
| 35 |
+
self.agent = create_react_agent(self.llm, self.tools, self.prompt)
|
| 36 |
+
print("Agent created.")
|
|
|
|
| 37 |
|
| 38 |
+
# 5. Create the Agent Executor with a robust error handler
|
| 39 |
self.agent_executor = AgentExecutor(
|
| 40 |
agent=self.agent,
|
| 41 |
tools=self.tools,
|
| 42 |
verbose=True,
|
| 43 |
+
# This new line gives the agent instructions on how to recover from parsing errors
|
| 44 |
+
handle_parsing_errors="Check your output and make sure it conforms to the format. Your final answer must be just the answer, with no other text.",
|
| 45 |
max_iterations=5,
|
| 46 |
)
|
| 47 |
print("Agent Executor created. Initialization complete.")
|
|
|
|
| 53 |
print(f"Agent received question: {question}")
|
| 54 |
|
| 55 |
try:
|
| 56 |
+
response = self.agent_executor.invoke({"input": question})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
final_answer = response.get("output", "Error: Could not parse final answer.")
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error during agent execution: {e}")
|