mrhenu commited on
Commit
b13ee08
·
verified ·
1 Parent(s): 3eec996

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
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
- # --- New LangChain Agent Definition ---
14
  class LangChainAgent:
15
  def __init__(self):
16
  print("Initializing LangChainAgent...")
17
 
18
- # 1. Set up the LLM (The "Brain") - This part is correct now
19
  self.llm = HuggingFaceEndpoint(
20
- repo_id="mistralai/Mistral-7B-Instruct-v0.2",
21
- task="conversational",
22
  max_new_tokens=512,
23
- do_sample=False,
24
  )
25
- print("LLM initialized with mistralai/Mistral-7B-Instruct-v0.2.")
26
 
27
- # 2. Define the Tools - This is correct
28
  self.tools = [DuckDuckGoSearchRun()]
29
  print("Tools initialized.")
30
 
31
- # 3. Get the CHAT-SPECIFIC Prompt Template
32
- # We are switching to a prompt designed for chat models
33
- self.prompt = hub.pull("hwchase17/react-chat")
34
- print("Prompt template pulled: hwchase17/react-chat")
35
 
36
- # 4. Create the CHAT Agent
37
- # We use create_react_chat_agent, which is designed for conversational models
38
- self.agent = create_react_chat_agent(self.llm, self.tools, self.prompt)
39
- print("Chat Agent created.")
40
 
41
- # 5. Create the Agent Executor - This is correct
42
  self.agent_executor = AgentExecutor(
43
  agent=self.agent,
44
  tools=self.tools,
45
  verbose=True,
46
- handle_parsing_errors=True,
 
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
- # The input must now be a dictionary with 'input' and 'chat_history'
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}")