Spaces:
Sleeping
Sleeping
Commit
·
e946b39
1
Parent(s):
29dcea0
migrated to langchain from graph
Browse files- app.py +19 -46
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import requests
|
|
| 5 |
import inspect
|
| 6 |
import pandas as pd
|
| 7 |
import datetime
|
| 8 |
-
|
| 9 |
|
| 10 |
from langchain.tools import tool
|
| 11 |
# from langchain_community.tools import get_all_tools
|
|
@@ -13,9 +13,12 @@ from typing import TypedDict, Annotated
|
|
| 13 |
# from langgraph.graph.message import add_messages
|
| 14 |
# from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
| 15 |
from langgraph.prebuilt import ToolNode
|
| 16 |
-
from langgraph.graph import START, StateGraph, END
|
| 17 |
# from langgraph.prebuilt import tools_condition
|
| 18 |
-
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
## # Load environment variables from .env file
|
| 21 |
# --- Constants ---
|
|
@@ -105,12 +108,12 @@ def convert_units(args: str) -> str:
|
|
| 105 |
|
| 106 |
# --- TOOL 5: Date & Time Tool ---
|
| 107 |
@tool
|
| 108 |
-
def get_time(
|
| 109 |
"""Get current UTC time as HH:MM."""
|
| 110 |
return datetime.datetime.utc().strftime("%H:%M")
|
| 111 |
|
| 112 |
@tool
|
| 113 |
-
def get_date(
|
| 114 |
"""Get current date as YYYY-MM-DD."""
|
| 115 |
return datetime.datetime.utc().strftime("%Y-%m-%d")
|
| 116 |
|
|
@@ -238,53 +241,22 @@ Instructions:
|
|
| 238 |
|
| 239 |
## --- Initialize Hugging Face Model ---
|
| 240 |
# Generate the chat interface, including the tools
|
| 241 |
-
llm =
|
| 242 |
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 243 |
huggingfacehub_api_token=HF_ACCESS_KEY,
|
| 244 |
-
model_kwargs={'prompt': system_prompt}
|
| 245 |
-
# system_prompt=system_prompt,
|
| 246 |
)
|
|
|
|
| 247 |
# chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 248 |
# tools = [search_tool, fetch_weather]
|
| 249 |
# chat_with_tools = chat.bind_tools(tools)
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
pass
|
| 258 |
-
|
| 259 |
-
# Define the main node (agent logic)
|
| 260 |
-
def agent_node(state: AgentState) -> AgentState:
|
| 261 |
-
question = state["question"]
|
| 262 |
-
# The LLM will decide which tool to use based on the prompt and tools
|
| 263 |
-
# response = chat_with_tools.invoke(question) # use this if using ChatHuggingFace with binding option to tools
|
| 264 |
-
response = llm.invoke(question, tools=tools_list)
|
| 265 |
-
return AgentState({"question": question, "answer": response})
|
| 266 |
-
|
| 267 |
-
# Build the graph
|
| 268 |
-
graph = StateGraph(AgentState)
|
| 269 |
-
graph.add_node("agent", agent_node)
|
| 270 |
-
# graph.add_node("tools", ToolNode(tools)) #use this when using ChatHuggingFace with binding option to tools
|
| 271 |
-
# graph.add_edge(START, "agent") #alternatively use the below with set_entry_point
|
| 272 |
-
graph.set_entry_point("agent")
|
| 273 |
-
graph.add_edge("agent", END)
|
| 274 |
-
my_agent = graph.compile()
|
| 275 |
-
|
| 276 |
-
# Or try simply with Graph instead of StateGraph
|
| 277 |
-
# from langgraph.graph import Graph
|
| 278 |
-
# graph = Graph(llm=llm, tools=tools_list)
|
| 279 |
-
# def agent(question: str) -> str:
|
| 280 |
-
# return graph.run(question)
|
| 281 |
-
|
| 282 |
-
## --- AGENT CALL FUNCTION ---
|
| 283 |
-
def agent(question: str) -> str:
|
| 284 |
-
state = AgentState({"question": question})
|
| 285 |
-
result = my_agent.invoke(state)
|
| 286 |
-
return result["answer"]
|
| 287 |
-
|
| 288 |
|
| 289 |
|
| 290 |
## --
|
|
@@ -351,7 +323,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 351 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 352 |
continue
|
| 353 |
try:
|
| 354 |
-
|
|
|
|
| 355 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 356 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 357 |
except Exception as e:
|
|
|
|
| 5 |
import inspect
|
| 6 |
import pandas as pd
|
| 7 |
import datetime
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
from langchain.tools import tool
|
| 11 |
# from langchain_community.tools import get_all_tools
|
|
|
|
| 13 |
# from langgraph.graph.message import add_messages
|
| 14 |
# from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
| 15 |
from langgraph.prebuilt import ToolNode
|
| 16 |
+
from langgraph.graph import START, StateGraph, END, Graph
|
| 17 |
# from langgraph.prebuilt import tools_condition
|
| 18 |
+
# from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 19 |
+
from langchain.agents import initialize_agent, AgentType
|
| 20 |
+
from langchain_community.llms import HuggingFaceHub
|
| 21 |
+
from langchain_community.chat_models import ChatHuggingFace
|
| 22 |
|
| 23 |
## # Load environment variables from .env file
|
| 24 |
# --- Constants ---
|
|
|
|
| 108 |
|
| 109 |
# --- TOOL 5: Date & Time Tool ---
|
| 110 |
@tool
|
| 111 |
+
def get_time(input: str) -> str:
|
| 112 |
"""Get current UTC time as HH:MM."""
|
| 113 |
return datetime.datetime.utc().strftime("%H:%M")
|
| 114 |
|
| 115 |
@tool
|
| 116 |
+
def get_date(input: str) -> str:
|
| 117 |
"""Get current date as YYYY-MM-DD."""
|
| 118 |
return datetime.datetime.utc().strftime("%Y-%m-%d")
|
| 119 |
|
|
|
|
| 241 |
|
| 242 |
## --- Initialize Hugging Face Model ---
|
| 243 |
# Generate the chat interface, including the tools
|
| 244 |
+
llm = HuggingFaceHub(
|
| 245 |
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 246 |
huggingfacehub_api_token=HF_ACCESS_KEY,
|
| 247 |
+
# model_kwargs={'prompt': system_prompt}
|
|
|
|
| 248 |
)
|
| 249 |
+
chat_llm = ChatHuggingFace(llm=llm)
|
| 250 |
# chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 251 |
# tools = [search_tool, fetch_weather]
|
| 252 |
# chat_with_tools = chat.bind_tools(tools)
|
| 253 |
|
| 254 |
+
agent = initialize_agent(
|
| 255 |
+
tools=tools_list,
|
| 256 |
+
llm=chat_llm,
|
| 257 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 258 |
+
verbose=True,
|
| 259 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
## --
|
|
|
|
| 323 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 324 |
continue
|
| 325 |
try:
|
| 326 |
+
full_prompt = f"{system_prompt}\n Input Question: {question_text}"
|
| 327 |
+
submitted_answer = agent(full_prompt)
|
| 328 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 329 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 330 |
except Exception as e:
|
requirements.txt
CHANGED
|
@@ -4,4 +4,5 @@ langchain
|
|
| 4 |
langgraph
|
| 5 |
langchainhub
|
| 6 |
huggingface-hub
|
| 7 |
-
langchain-huggingface
|
|
|
|
|
|
| 4 |
langgraph
|
| 5 |
langchainhub
|
| 6 |
huggingface-hub
|
| 7 |
+
langchain-huggingface
|
| 8 |
+
langchain-community
|