temperancehong commited on
Commit
9f9e79c
·
1 Parent(s): 69c000c

change llm set, and invoke

Browse files
Files changed (2) hide show
  1. agent/llm_setup.py +13 -16
  2. agent/router_agent.py +4 -6
agent/llm_setup.py CHANGED
@@ -1,13 +1,10 @@
1
- # agent/llm_setup.py
2
  import os
3
  from typing import Optional, List
4
  from langchain.agents import initialize_agent, AgentType
5
  from langchain.tools import Tool
6
-
7
- # ✅ New, non-deprecated wrappers
8
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
9
 
10
- # your tools
11
  from tools.web_search import ddg_search
12
  from tools.web_fetch import fetch_text
13
  from tools.wikipedia_tool import wiki_page_text
@@ -18,18 +15,16 @@ from tools.excel_ops import load_df_from_xlsx, sum_where_not_category
18
  from tools.cayley_props import non_commuting_elements
19
  from tools.text_utils import reverse_text
20
 
21
- # ---------- choose ONE model ----------
22
-
23
- USE_FLAN = True # set to False if you want Mistral instead
24
 
25
  def make_llm():
26
  hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
27
  if not hf_token:
28
- # Fails fast with a readable message instead of cryptic pydantic errors
29
- raise RuntimeError("Missing HF token. Set a Space secret 'HUGGINGFACEHUB_API_TOKEN' or 'HF_TOKEN'.")
30
 
31
  if USE_FLAN:
32
- # FLAN is a seq2seq model -> task MUST be text2text-generation
33
  endpoint = HuggingFaceEndpoint(
34
  repo_id="google/flan-t5-base",
35
  task="text2text-generation",
@@ -38,7 +33,7 @@ def make_llm():
38
  temperature=0.0,
39
  )
40
  else:
41
- # Decoder-only chat/instruct model -> task is text-generation
42
  endpoint = HuggingFaceEndpoint(
43
  repo_id="mistralai/Mistral-7B-Instruct-v0.2",
44
  task="text-generation",
@@ -141,17 +136,19 @@ def make_react_agent(file_name: Optional[str]):
141
  ]
142
 
143
  llm = make_llm()
 
 
144
  prefix = (
145
- "You are a tool-using ReAct agent. "
146
- f"If a file is attached, its path is: {file_name if file_name else '[no file attached]'}. "
147
- "Use tools when helpful; pass explicit file paths to file tools (or empty to use the attached path). "
148
- "Return only the final answer unless asked otherwise."
149
  )
150
 
151
  return initialize_agent(
152
  tools=tools,
153
  llm=llm,
154
- agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
155
  verbose=True,
156
  agent_kwargs={"prefix": prefix},
157
  handle_parsing_errors=True,
 
 
1
  import os
2
  from typing import Optional, List
3
  from langchain.agents import initialize_agent, AgentType
4
  from langchain.tools import Tool
 
 
5
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
6
 
7
+ # ---- your tools ----
8
  from tools.web_search import ddg_search
9
  from tools.web_fetch import fetch_text
10
  from tools.wikipedia_tool import wiki_page_text
 
15
  from tools.cayley_props import non_commuting_elements
16
  from tools.text_utils import reverse_text
17
 
18
+ # Pick ONE model. FLAN is light; Mistral is better but heavier.
19
+ USE_FLAN = True # set False to try Mistral instruct
 
20
 
21
  def make_llm():
22
  hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
23
  if not hf_token:
24
+ raise RuntimeError("Missing HF token: set HUGGINGFACEHUB_API_TOKEN or HF_TOKEN in Space secrets.")
 
25
 
26
  if USE_FLAN:
27
+ # FLAN is seq2seq -> MUST use text2text-generation
28
  endpoint = HuggingFaceEndpoint(
29
  repo_id="google/flan-t5-base",
30
  task="text2text-generation",
 
33
  temperature=0.0,
34
  )
35
  else:
36
+ # Decoder-only instruct -> text-generation
37
  endpoint = HuggingFaceEndpoint(
38
  repo_id="mistralai/Mistral-7B-Instruct-v0.2",
39
  task="text-generation",
 
136
  ]
137
 
138
  llm = make_llm()
139
+
140
+ # STRUCTURED_CHAT agent is more forgiving with non-OpenAI chat models.
141
  prefix = (
142
+ "You are a tool-using agent. If a file is attached for this question, its path is: "
143
+ f"{file_name if file_name else '[no file attached]'}. "
144
+ "Decide which tools to use. When a tool needs a file path, pass the path (or an empty string to use the attached path). "
145
+ "When you have the answer, respond concisely."
146
  )
147
 
148
  return initialize_agent(
149
  tools=tools,
150
  llm=llm,
151
+ agent_type=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
152
  verbose=True,
153
  agent_kwargs={"prefix": prefix},
154
  handle_parsing_errors=True,
agent/router_agent.py CHANGED
@@ -2,11 +2,9 @@ from typing import Optional
2
  from agent.llm_setup import make_react_agent
3
 
4
  class GenericLangChainAgent:
5
- """
6
- LLM-orchestrated ReAct agent. We build it per-call so the 'file_name'
7
- can be injected into the system prefix and used by file tools.
8
- """
9
  def __call__(self, question: str, file_name: Optional[str] = None) -> str:
10
  agent = make_react_agent(file_name=file_name)
11
- # Let the LLM decide which tools to call.
12
- return agent.run(question)
 
 
 
2
  from agent.llm_setup import make_react_agent
3
 
4
  class GenericLangChainAgent:
 
 
 
 
5
  def __call__(self, question: str, file_name: Optional[str] = None) -> str:
6
  agent = make_react_agent(file_name=file_name)
7
+ # IMPORTANT: use invoke() with {"input": ...}
8
+ out = agent.invoke({"input": question})
9
+ # AgentExecutors return a dict with "output"
10
+ return out.get("output", "").strip() or "I don't know."