Spaces:
Sleeping
Sleeping
Add the retriever code
Browse files- .env +3 -1
- app.py +4 -2
- create_embeddings.ipynb +0 -0
- metadata.jsonl +0 -0
- poetry.lock +0 -0
- pyproject.toml +8 -3
- requirements.txt +5 -1
- tools.py +22 -0
.env
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
|
|
|
|
|
| 1 |
SERPAPI_KEY = "046aa74b206478f196088246569b77000619bcae1c5ccda5d0143ce274a932e1"
|
| 2 |
-
OPENAI_API_KEY = "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"
|
|
|
|
| 1 |
+
SUPABASE_URL = "https://jaqnaccpocxhsavlkdpt.supabase.co/"
|
| 2 |
+
SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImphcW5hY2Nwb2N4aHNhdmxrZHB0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTE4MDk2MjYsImV4cCI6MjA2NzM4NTYyNn0.5r37tq8gA_BEumvi614fbovfs6qlb6qxDX0gcB5mo3o"
|
| 3 |
SERPAPI_KEY = "046aa74b206478f196088246569b77000619bcae1c5ccda5d0143ce274a932e1"
|
| 4 |
+
OPENAI_API_KEY = "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"
|
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import inspect
|
|
| 5 |
import pandas as pd
|
| 6 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 7 |
from langchain_core.messages import HumanMessage
|
| 8 |
-
from tools import assistant, AgentState, tools
|
| 9 |
from langgraph.graph import StateGraph, START
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
|
@@ -16,9 +16,11 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
builder = StateGraph(AgentState)
|
|
|
|
| 19 |
builder.add_node("assistant", assistant)
|
| 20 |
builder.add_node("tools", ToolNode(tools))
|
| 21 |
-
builder.add_edge(START, "
|
|
|
|
| 22 |
builder.add_conditional_edges("assistant", tools_condition)
|
| 23 |
builder.add_edge("tools", "assistant")
|
| 24 |
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 7 |
from langchain_core.messages import HumanMessage
|
| 8 |
+
from tools import assistant, AgentState, tools, retriever
|
| 9 |
from langgraph.graph import StateGraph, START
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
|
|
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
builder = StateGraph(AgentState)
|
| 19 |
+
builder.add_node("retriever", retriever)
|
| 20 |
builder.add_node("assistant", assistant)
|
| 21 |
builder.add_node("tools", ToolNode(tools))
|
| 22 |
+
builder.add_edge(START, "retriever")
|
| 23 |
+
builder.add_conditional_edges("retriever", "assistant")
|
| 24 |
builder.add_conditional_edges("assistant", tools_condition)
|
| 25 |
builder.add_edge("tools", "assistant")
|
| 26 |
|
create_embeddings.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
metadata.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
poetry.lock
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pyproject.toml
CHANGED
|
@@ -6,7 +6,7 @@ authors = [
|
|
| 6 |
{name = "sharath"}
|
| 7 |
]
|
| 8 |
readme = "README.md"
|
| 9 |
-
requires-python = ">=3.
|
| 10 |
dependencies = [
|
| 11 |
"gradio",
|
| 12 |
"requests",
|
|
@@ -20,9 +20,14 @@ dependencies = [
|
|
| 20 |
"langchain-openai",
|
| 21 |
"serpapi",
|
| 22 |
"python-dotenv",
|
| 23 |
-
"google-search-results"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
-
|
|
|
|
| 26 |
[build-system]
|
| 27 |
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
| 28 |
build-backend = "poetry.core.masonry.api"
|
|
|
|
| 6 |
{name = "sharath"}
|
| 7 |
]
|
| 8 |
readme = "README.md"
|
| 9 |
+
requires-python = ">=3.10"
|
| 10 |
dependencies = [
|
| 11 |
"gradio",
|
| 12 |
"requests",
|
|
|
|
| 20 |
"langchain-openai",
|
| 21 |
"serpapi",
|
| 22 |
"python-dotenv",
|
| 23 |
+
"google-search-results",
|
| 24 |
+
"langchain-huggingface",
|
| 25 |
+
"langchain-community",
|
| 26 |
+
"supabase",
|
| 27 |
+
"sentence-transformers"
|
| 28 |
]
|
| 29 |
+
[tool.poetry]
|
| 30 |
+
package-mode = false
|
| 31 |
[build-system]
|
| 32 |
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
| 33 |
build-backend = "poetry.core.masonry.api"
|
requirements.txt
CHANGED
|
@@ -10,4 +10,8 @@ langchain
|
|
| 10 |
langchain-openai
|
| 11 |
serpapi
|
| 12 |
python-dotenv
|
| 13 |
-
google-search-results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
langchain-openai
|
| 11 |
serpapi
|
| 12 |
python-dotenv
|
| 13 |
+
google-search-results
|
| 14 |
+
langchain-huggingface
|
| 15 |
+
langchain-community
|
| 16 |
+
supabase
|
| 17 |
+
sentence-transformers
|
tools.py
CHANGED
|
@@ -2,6 +2,9 @@ from typing import List, TypedDict, Annotated, Optional, Dict, Union
|
|
| 2 |
from langchain_openai import ChatOpenAI
|
| 3 |
from langchain_core.messages import SystemMessage, HumanMessage, AnyMessage
|
| 4 |
from langgraph.graph.message import add_messages
|
|
|
|
|
|
|
|
|
|
| 5 |
from serpapi import GoogleSearch
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
|
@@ -47,6 +50,25 @@ llm = ChatOpenAI(model = "gpt-3.5-turbo")
|
|
| 47 |
tools = [add, subtract, divide, web_search]
|
| 48 |
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls = False)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def assistant(state: AgentState) -> Dict:
|
| 51 |
|
| 52 |
system_message = """
|
|
|
|
| 2 |
from langchain_openai import ChatOpenAI
|
| 3 |
from langchain_core.messages import SystemMessage, HumanMessage, AnyMessage
|
| 4 |
from langgraph.graph.message import add_messages
|
| 5 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 6 |
+
from supabase.client import create_client
|
| 7 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 8 |
from serpapi import GoogleSearch
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
import os
|
|
|
|
| 50 |
tools = [add, subtract, divide, web_search]
|
| 51 |
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls = False)
|
| 52 |
|
| 53 |
+
def retriever(query: str) -> str:
|
| 54 |
+
"""
|
| 55 |
+
Retrieve the answer fom vector database instead of searching if we found a user query similar to which is already found in the dataset
|
| 56 |
+
|
| 57 |
+
"""
|
| 58 |
+
supabase_url = os.environ.get("SUPABASE_URL")
|
| 59 |
+
supabase_key = os.environ.get("SUPABASE_KEY")
|
| 60 |
+
supabase = create_client(supabase_url, supabase_key)
|
| 61 |
+
embeddings = HuggingFaceEmbeddings(model_name = "sentence-transformers/all-mpnet-base-v2")
|
| 62 |
+
vector_store = SupabaseVectorStore(
|
| 63 |
+
embedding=embeddings,
|
| 64 |
+
client=supabase,
|
| 65 |
+
table_name="documents",
|
| 66 |
+
query_name="match_documents",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
docs = vector_store.similarity_search(query = query, k = 1)
|
| 70 |
+
return docs[0].page_content.split("Final answer : ")[-1]
|
| 71 |
+
|
| 72 |
def assistant(state: AgentState) -> Dict:
|
| 73 |
|
| 74 |
system_message = """
|