Spaces:
Sleeping
Sleeping
Delete agent.py
Browse files
agent.py
DELETED
|
@@ -1,110 +0,0 @@
|
|
| 1 |
-
import operator
|
| 2 |
-
from typing import TypedDict, Annotated, Sequence
|
| 3 |
-
from langchain_core.messages import BaseMessage, SystemMessage, HumanMessage
|
| 4 |
-
from langgraph.graph import StateGraph, END
|
| 5 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 6 |
-
from langchain_huggingface import ChatHuggingFace
|
| 7 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
-
from langchain_groq import ChatGroq
|
| 9 |
-
from tools import multiply, add, subtract, divide, modulus, wiki_search, web_search, arxiv_search, chess_suggest, youtube_transcript, analyze_image
|
| 10 |
-
|
| 11 |
-
# 1. Define the state schema for the graph
|
| 12 |
-
class AgentState(TypedDict):
|
| 13 |
-
messages: Annotated[Sequence[BaseMessage], operator.add]
|
| 14 |
-
|
| 15 |
-
# Load the system prompt from the file
|
| 16 |
-
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 17 |
-
SYSTEM_PROMPT = f.read()
|
| 18 |
-
|
| 19 |
-
# System message
|
| 20 |
-
sys_msg = SystemMessage(content=SYSTEM_PROMPT)
|
| 21 |
-
|
| 22 |
-
with open('metadata.json', 'r') as jsonl_file:
|
| 23 |
-
json_list = list(jsonl_file)
|
| 24 |
-
|
| 25 |
-
tools = [
|
| 26 |
-
add,
|
| 27 |
-
subtract,
|
| 28 |
-
multiply,
|
| 29 |
-
divide,
|
| 30 |
-
modulus,
|
| 31 |
-
wiki_search,
|
| 32 |
-
web_search,
|
| 33 |
-
arxiv_search,
|
| 34 |
-
youtube_transcript,
|
| 35 |
-
# load_excel,
|
| 36 |
-
analyze_image,
|
| 37 |
-
chess_suggest,
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
-
def build_graph(llm: ChatGoogleGenerativeAI | ChatGroq | ChatHuggingFace):
|
| 41 |
-
# bind the tools so that OpenAI function calls are dispatched automatically
|
| 42 |
-
llm_with_tools = llm.bind_tools(tools)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# planner node
|
| 46 |
-
def planner(state: AgentState):
|
| 47 |
-
ctx = state["messages"][-6:]
|
| 48 |
-
if not isinstance(ctx[0], SystemMessage):
|
| 49 |
-
ctx = [sys_msg] + list(ctx)
|
| 50 |
-
return {"messages": [llm_with_tools.invoke(ctx)]}
|
| 51 |
-
|
| 52 |
-
def retriever(state):
|
| 53 |
-
"""Retriever node that searches metadata.jsonl for similar content"""
|
| 54 |
-
user_question = state["messages"][-1].content
|
| 55 |
-
|
| 56 |
-
# Simple keyword-based search through metadata.jsonl
|
| 57 |
-
import json
|
| 58 |
-
best_match = None
|
| 59 |
-
best_score = 0
|
| 60 |
-
|
| 61 |
-
try:
|
| 62 |
-
for line in json_list:
|
| 63 |
-
if line.strip(): # Skip empty lines
|
| 64 |
-
try:
|
| 65 |
-
data = json.loads(line)
|
| 66 |
-
# Assuming the JSON has 'question' and 'answer' fields
|
| 67 |
-
# Adjust field names based on your actual JSON structure
|
| 68 |
-
content = data.get('Question', '')
|
| 69 |
-
# Simple keyword matching - count common words
|
| 70 |
-
user_words = set(user_question.lower().split())
|
| 71 |
-
content_words = set(content.lower().split())
|
| 72 |
-
common_words = user_words.intersection(content_words)
|
| 73 |
-
score = len(common_words)
|
| 74 |
-
|
| 75 |
-
if score > best_score and score > 0:
|
| 76 |
-
best_score = score
|
| 77 |
-
best_match = data
|
| 78 |
-
|
| 79 |
-
except json.JSONDecodeError:
|
| 80 |
-
continue # Skip malformed JSON lines
|
| 81 |
-
|
| 82 |
-
if best_match:
|
| 83 |
-
# Create a reference message with the similar content
|
| 84 |
-
reference_content = best_match.get('Question', '') or best_match.get('Final answer', '') or str(best_match)
|
| 85 |
-
example_msg = HumanMessage(
|
| 86 |
-
content=f"Here is relevant information from our knowledge base: \n\n{reference_content}. The correct answer is {best_match.get('Final answer', '') }",
|
| 87 |
-
)
|
| 88 |
-
return {"messages": state["messages"] + [example_msg]}
|
| 89 |
-
else:
|
| 90 |
-
return state
|
| 91 |
-
|
| 92 |
-
except FileNotFoundError:
|
| 93 |
-
print("metadata.jsonl file not found")
|
| 94 |
-
return state
|
| 95 |
-
except Exception as e:
|
| 96 |
-
print(f"Error reading metadata.jsonl: {e}")
|
| 97 |
-
return state
|
| 98 |
-
graph = StateGraph(AgentState)
|
| 99 |
-
graph.add_node("planner", planner)
|
| 100 |
-
graph.add_node("retriever", retriever)
|
| 101 |
-
graph.add_node("tools", ToolNode(tools))
|
| 102 |
-
|
| 103 |
-
graph.set_entry_point("retriever")
|
| 104 |
-
graph.add_conditional_edges(
|
| 105 |
-
"planner",
|
| 106 |
-
tools_condition,
|
| 107 |
-
)
|
| 108 |
-
graph.add_edge("tools", "planner")
|
| 109 |
-
graph.add_edge("retriever", "planner")
|
| 110 |
-
return graph.compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|