Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,7 +1,17 @@
|
|
1 |
# agent.py
|
2 |
import os
|
|
|
|
|
3 |
from langgraph import Graph, LLM, tool #or other graph library
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# 1) Define tools
|
6 |
|
7 |
@tool
|
@@ -15,7 +25,28 @@ def calculator(expr: str) -> str:
|
|
15 |
# @tool
|
16 |
# def web_search(query:str) -> str:
|
17 |
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
|
|
|
|
|
|
|
|
19 |
# 2) Build your graph
|
20 |
def build_graph(provider: str = "huggingface") -> Graph:
|
21 |
# 2a) Instantiate your LLM endpoint
|
|
|
1 |
# agent.py
|
2 |
import os
|
3 |
+
from supabase import create_client
|
4 |
+
from sentence_transformers import SentenceTransformers
|
5 |
from langgraph import Graph, LLM, tool #or other graph library
|
6 |
|
7 |
+
# ----Supabase setup----
|
8 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
9 |
+
SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_KEY")
|
10 |
+
EMBED_MODEL_ID = os.getenv("HF_EMBEDDING_MODEL")
|
11 |
+
|
12 |
+
sb_client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
13 |
+
embedder = SentenceTransformers(EMBED_MODEL_ID)
|
14 |
+
|
15 |
# 1) Define tools
|
16 |
|
17 |
@tool
|
|
|
25 |
# @tool
|
26 |
# def web_search(query:str) -> str:
|
27 |
# ...
|
28 |
+
@tool
|
29 |
+
def retrieve_docs(query: str, k: int = 3) -> str:
|
30 |
+
"""
|
31 |
+
Fetch tpo-k docs from Supabase vector store.
|
32 |
+
Returns the concatenated text.
|
33 |
+
"""
|
34 |
+
# --- embed the query
|
35 |
+
q_emb = embedder.encode(query).tolist()
|
36 |
+
|
37 |
+
# --- query the embedding table
|
38 |
+
response = (
|
39 |
+
sb_client
|
40 |
+
.rpc("match_documents", {"query_embedding": q_emb, "match_count": k})
|
41 |
+
.execute()
|
42 |
+
)
|
43 |
+
rows = response.data
|
44 |
|
45 |
+
# ---- concatenate the content field
|
46 |
+
docs = [row["content"] for row in rows]
|
47 |
+
return "\n\n---\n\n".join(docs)
|
48 |
+
|
49 |
+
|
50 |
# 2) Build your graph
|
51 |
def build_graph(provider: str = "huggingface") -> Graph:
|
52 |
# 2a) Instantiate your LLM endpoint
|