Spaces:
Runtime error
Runtime error
Upload agent.py
Browse files
agent.py
CHANGED
|
@@ -10,10 +10,27 @@ from langchain_core.messages import HumanMessage, SystemMessage
|
|
| 10 |
from langgraph.graph import START, StateGraph
|
| 11 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 12 |
from IPython.display import Image, display
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
load_dotenv('../config.env')
|
| 15 |
|
| 16 |
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# load the system prompt from the file
|
| 19 |
with open('system_prompt.txt', 'r') as f:
|
|
@@ -159,24 +176,47 @@ def build_graph():
|
|
| 159 |
|
| 160 |
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
# Graph
|
| 165 |
builder = StateGraph(AgentState)
|
| 166 |
|
| 167 |
# Define nodes: these do the work
|
| 168 |
-
builder.add_node("assistant", assistant)
|
| 169 |
-
builder.add_node("tools", ToolNode(tools))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
# Define edges: these determine how the control flow moves
|
| 172 |
-
builder.add_edge(START, "
|
| 173 |
-
builder.
|
| 174 |
-
|
| 175 |
-
# If the latest message (result) from assistant is a tool call -> tools_condition routes to tools
|
| 176 |
-
# If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END
|
| 177 |
-
tools_condition,
|
| 178 |
-
)
|
| 179 |
-
builder.add_edge("tools", "assistant")
|
| 180 |
react_graph = builder.compile()
|
| 181 |
|
| 182 |
# Show
|
|
|
|
| 10 |
from langgraph.graph import START, StateGraph
|
| 11 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 12 |
from IPython.display import Image, display
|
| 13 |
+
from langchain_core.messages import AIMessage
|
| 14 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 15 |
+
from supabase.client import Client, create_client
|
| 16 |
+
import os
|
| 17 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 18 |
|
| 19 |
load_dotenv('../config.env')
|
| 20 |
|
| 21 |
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
|
| 22 |
+
embedding_model = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
|
| 23 |
+
|
| 24 |
+
supabase_url = os.environ.get("SUPABASE_URL")
|
| 25 |
+
supabase_key = os.environ.get("SUPABASE_SERVICE_KEY")
|
| 26 |
+
supabase: Client = create_client(supabase_url, supabase_key)
|
| 27 |
+
|
| 28 |
+
vector_store = SupabaseVectorStore(
|
| 29 |
+
client=supabase,
|
| 30 |
+
embedding= embedding_model,
|
| 31 |
+
table_name="documents",
|
| 32 |
+
query_name="match_documents_langchain",
|
| 33 |
+
)
|
| 34 |
|
| 35 |
# load the system prompt from the file
|
| 36 |
with open('system_prompt.txt', 'r') as f:
|
|
|
|
| 176 |
|
| 177 |
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
|
| 178 |
|
| 179 |
+
def retriever(state: AgentState):
|
| 180 |
+
query = state["messages"][-1].content
|
| 181 |
+
results = vector_store.similarity_search(query, k=1)
|
| 182 |
+
|
| 183 |
+
if not results:
|
| 184 |
+
# If no documents are found, provide a fallback response.
|
| 185 |
+
answer = "I couldn't find anything relevant in the knowledge base. Please try rephrasing your question."
|
| 186 |
+
else:
|
| 187 |
+
similar_doc = results[0]
|
| 188 |
+
content = similar_doc.page_content
|
| 189 |
+
if "Final answer :" in content:
|
| 190 |
+
answer = content.split("Final answer :")[-1].strip()
|
| 191 |
+
else:
|
| 192 |
+
answer = content.strip()
|
| 193 |
+
|
| 194 |
+
return {"messages": [AIMessage(content=answer)]}
|
| 195 |
+
|
| 196 |
|
| 197 |
|
| 198 |
# Graph
|
| 199 |
builder = StateGraph(AgentState)
|
| 200 |
|
| 201 |
# Define nodes: these do the work
|
| 202 |
+
# builder.add_node("assistant", assistant)
|
| 203 |
+
# builder.add_node("tools", ToolNode(tools))
|
| 204 |
+
|
| 205 |
+
# # Define edges: these determine how the control flow moves
|
| 206 |
+
# builder.add_edge(START, "assistant")
|
| 207 |
+
# builder.add_conditional_edges(
|
| 208 |
+
# "assistant",
|
| 209 |
+
# # If the latest message (result) from assistant is a tool call -> tools_condition routes to tools
|
| 210 |
+
# # If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END
|
| 211 |
+
# tools_condition,
|
| 212 |
+
# )
|
| 213 |
+
# builder.add_edge("tools", "assistant")
|
| 214 |
+
builder.add_node("retriever", retriever)
|
| 215 |
|
| 216 |
# Define edges: these determine how the control flow moves
|
| 217 |
+
builder.add_edge(START, "retriever")
|
| 218 |
+
builder.set_finish_point("retriever")
|
| 219 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
react_graph = builder.compile()
|
| 221 |
|
| 222 |
# Show
|