Update agent.py
Browse files
agent.py
CHANGED
|
@@ -110,90 +110,68 @@ def build_graph():
|
|
| 110 |
Build and return a StateGraph using a Hugging Face chat LLM with tools.
|
| 111 |
"""
|
| 112 |
try:
|
| 113 |
-
# For HuggingFace Spaces, use the Inference API approach
|
| 114 |
hf_token = os.getenv("HUGGINGFACE_TOKEN") or os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN")
|
| 115 |
|
| 116 |
if hf_token:
|
| 117 |
print("Using HuggingFace Inference API...")
|
| 118 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 119 |
|
| 120 |
-
# Use HF Inference API instead of local models
|
| 121 |
llm = HuggingFaceEndpoint(
|
| 122 |
repo_id="microsoft/DialoGPT-medium",
|
| 123 |
huggingfacehub_api_token=hf_token,
|
| 124 |
model_kwargs={"temperature": 0.1, "max_new_tokens": 512}
|
| 125 |
)
|
| 126 |
|
| 127 |
-
# Wrap for chat interface
|
| 128 |
llm = ChatHuggingFace(llm=llm)
|
| 129 |
print("✓ Successfully initialized HF Inference API")
|
| 130 |
|
| 131 |
else:
|
| 132 |
-
print("No HF token found, creating mock LLM for demo
|
| 133 |
-
# Create a simple mock LLM for demonstration
|
| 134 |
class SimpleMockLLM:
|
| 135 |
def bind_tools(self, tools):
|
| 136 |
return self
|
| 137 |
|
| 138 |
def invoke(self, messages):
|
| 139 |
from langchain_core.messages import AIMessage
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
if hasattr(messages, '__iter__') and messages:
|
| 143 |
-
last_msg = messages[-1]
|
| 144 |
-
if hasattr(last_msg, 'content'):
|
| 145 |
-
content = last_msg.content.lower()
|
| 146 |
-
else:
|
| 147 |
-
content = str(last_msg).lower()
|
| 148 |
-
else:
|
| 149 |
-
content = str(messages).lower()
|
| 150 |
-
|
| 151 |
-
# Simple rule-based responses
|
| 152 |
if any(word in content for word in ['math', 'calculate', 'add', 'multiply']):
|
| 153 |
return AIMessage(content="I can help with math! Try asking me to add, multiply, subtract, or divide numbers.")
|
| 154 |
elif any(word in content for word in ['search', 'find', 'look up']):
|
| 155 |
return AIMessage(content="I can search Wikipedia, Arxiv, or the web for information. What would you like me to search for?")
|
| 156 |
else:
|
| 157 |
-
return AIMessage(content=f"Hello! I'm a demo assistant.
|
| 158 |
|
| 159 |
llm = SimpleMockLLM()
|
| 160 |
print("✓ Created demo LLM")
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
print(f"Error initializing LLM: {e}")
|
| 164 |
-
# Fallback to basic mock
|
| 165 |
class BasicMockLLM:
|
| 166 |
def bind_tools(self, tools):
|
| 167 |
return self
|
| 168 |
|
| 169 |
def invoke(self, messages):
|
| 170 |
from langchain_core.messages import AIMessage
|
| 171 |
-
return AIMessage(content="Demo mode:
|
| 172 |
|
| 173 |
llm = BasicMockLLM()
|
| 174 |
print("✓ Using basic fallback LLM")
|
| 175 |
|
| 176 |
llm_with_tools = llm.bind_tools(tools)
|
| 177 |
|
| 178 |
-
# retriever node: use vector store if available
|
| 179 |
def retriever(state: MessagesState):
|
| 180 |
if supabase:
|
| 181 |
query = state["messages"][-1].content
|
| 182 |
doc = vector_store.similarity_search(query, k=1)[0]
|
| 183 |
content = doc.page_content
|
| 184 |
-
if "Final answer :" in content
|
| 185 |
-
answer = content.split("Final answer :")[-1].strip()
|
| 186 |
-
else:
|
| 187 |
-
answer = content.strip()
|
| 188 |
return {"messages": [AIMessage(content=answer)]}
|
| 189 |
-
# no supabase: pass through
|
| 190 |
return {"messages": state["messages"]}
|
| 191 |
|
| 192 |
-
# assistant node: invoke LLM and tools
|
| 193 |
def assistant(state: MessagesState):
|
| 194 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 195 |
|
| 196 |
-
# assemble graph
|
| 197 |
g = StateGraph(MessagesState)
|
| 198 |
g.add_node("retriever", retriever)
|
| 199 |
g.add_node("assistant", assistant)
|
|
@@ -202,7 +180,6 @@ def build_graph():
|
|
| 202 |
g.add_conditional_edges("assistant", tools_condition)
|
| 203 |
g.add_node("tools", ToolNode(tools))
|
| 204 |
g.add_edge("tools", "assistant")
|
| 205 |
-
|
| 206 |
g.set_entry_point("retriever")
|
| 207 |
g.set_finish_point("assistant")
|
| 208 |
-
return g.compile()
|
|
|
|
| 110 |
Build and return a StateGraph using a Hugging Face chat LLM with tools.
|
| 111 |
"""
|
| 112 |
try:
|
|
|
|
| 113 |
hf_token = os.getenv("HUGGINGFACE_TOKEN") or os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN")
|
| 114 |
|
| 115 |
if hf_token:
|
| 116 |
print("Using HuggingFace Inference API...")
|
| 117 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 118 |
|
|
|
|
| 119 |
llm = HuggingFaceEndpoint(
|
| 120 |
repo_id="microsoft/DialoGPT-medium",
|
| 121 |
huggingfacehub_api_token=hf_token,
|
| 122 |
model_kwargs={"temperature": 0.1, "max_new_tokens": 512}
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
llm = ChatHuggingFace(llm=llm)
|
| 126 |
print("✓ Successfully initialized HF Inference API")
|
| 127 |
|
| 128 |
else:
|
| 129 |
+
print("No HF token found, creating mock LLM for demo…")
|
|
|
|
| 130 |
class SimpleMockLLM:
|
| 131 |
def bind_tools(self, tools):
|
| 132 |
return self
|
| 133 |
|
| 134 |
def invoke(self, messages):
|
| 135 |
from langchain_core.messages import AIMessage
|
| 136 |
+
last_msg = messages[-1] if messages else None
|
| 137 |
+
content = getattr(last_msg, 'content', str(last_msg)).lower() if last_msg else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
if any(word in content for word in ['math', 'calculate', 'add', 'multiply']):
|
| 139 |
return AIMessage(content="I can help with math! Try asking me to add, multiply, subtract, or divide numbers.")
|
| 140 |
elif any(word in content for word in ['search', 'find', 'look up']):
|
| 141 |
return AIMessage(content="I can search Wikipedia, Arxiv, or the web for information. What would you like me to search for?")
|
| 142 |
else:
|
| 143 |
+
return AIMessage(content=f"Hello! I'm a demo assistant. You said: {content[:100]}...")
|
| 144 |
|
| 145 |
llm = SimpleMockLLM()
|
| 146 |
print("✓ Created demo LLM")
|
| 147 |
|
| 148 |
except Exception as e:
|
| 149 |
print(f"Error initializing LLM: {e}")
|
|
|
|
| 150 |
class BasicMockLLM:
|
| 151 |
def bind_tools(self, tools):
|
| 152 |
return self
|
| 153 |
|
| 154 |
def invoke(self, messages):
|
| 155 |
from langchain_core.messages import AIMessage
|
| 156 |
+
return AIMessage(content="Demo mode: Please configure a token for full functionality.")
|
| 157 |
|
| 158 |
llm = BasicMockLLM()
|
| 159 |
print("✓ Using basic fallback LLM")
|
| 160 |
|
| 161 |
llm_with_tools = llm.bind_tools(tools)
|
| 162 |
|
|
|
|
| 163 |
def retriever(state: MessagesState):
|
| 164 |
if supabase:
|
| 165 |
query = state["messages"][-1].content
|
| 166 |
doc = vector_store.similarity_search(query, k=1)[0]
|
| 167 |
content = doc.page_content
|
| 168 |
+
answer = content.split("Final answer :")[-1].strip() if "Final answer :" in content else content.strip()
|
|
|
|
|
|
|
|
|
|
| 169 |
return {"messages": [AIMessage(content=answer)]}
|
|
|
|
| 170 |
return {"messages": state["messages"]}
|
| 171 |
|
|
|
|
| 172 |
def assistant(state: MessagesState):
|
| 173 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 174 |
|
|
|
|
| 175 |
g = StateGraph(MessagesState)
|
| 176 |
g.add_node("retriever", retriever)
|
| 177 |
g.add_node("assistant", assistant)
|
|
|
|
| 180 |
g.add_conditional_edges("assistant", tools_condition)
|
| 181 |
g.add_node("tools", ToolNode(tools))
|
| 182 |
g.add_edge("tools", "assistant")
|
|
|
|
| 183 |
g.set_entry_point("retriever")
|
| 184 |
g.set_finish_point("assistant")
|
| 185 |
+
return g.compile()
|