Delete model.py
Browse files
model.py
DELETED
|
@@ -1,141 +0,0 @@
|
|
| 1 |
-
# ============================
|
| 2 |
-
# model.py
|
| 3 |
-
# ============================
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
-
from langgraph.graph import START, StateGraph, MessagesState
|
| 8 |
-
from langgraph.prebuilt import tools_condition, ToolNode
|
| 9 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 10 |
-
from langchain_groq import ChatGroq
|
| 11 |
-
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 12 |
-
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 13 |
-
from langchain_community.vectorstores import SupabaseVectorStore
|
| 14 |
-
from langchain_core.messages import SystemMessage, HumanMessage
|
| 15 |
-
from langchain_core.tools import tool
|
| 16 |
-
from langchain_tavily import TavilySearch
|
| 17 |
-
from langchain.tools.retriever import create_retriever_tool
|
| 18 |
-
from supabase.client import Client, create_client
|
| 19 |
-
|
| 20 |
-
load_dotenv()
|
| 21 |
-
|
| 22 |
-
# Setup Supabase
|
| 23 |
-
url = os.getenv("SUPABASE_URL")
|
| 24 |
-
key = os.getenv("SUPABASE_KEY")
|
| 25 |
-
supabase: Client = create_client(url, key)
|
| 26 |
-
|
| 27 |
-
# Tools
|
| 28 |
-
@tool
|
| 29 |
-
def multiply(a: int, b: int) -> int:
|
| 30 |
-
"""Multiply two numbers and return the result."""
|
| 31 |
-
return a * b
|
| 32 |
-
|
| 33 |
-
@tool
|
| 34 |
-
def add(a: int, b: int) -> int:
|
| 35 |
-
"""Add two numbers and return the result."""
|
| 36 |
-
return a + b
|
| 37 |
-
|
| 38 |
-
@tool
|
| 39 |
-
def subtract(a: int, b: int) -> int:
|
| 40 |
-
"""Subtract second number from first and return the result."""
|
| 41 |
-
return a - b
|
| 42 |
-
|
| 43 |
-
@tool
|
| 44 |
-
def divide(a: int, b: int) -> float:
|
| 45 |
-
"""Divide first number by second and return the result."""
|
| 46 |
-
if b == 0:
|
| 47 |
-
raise ValueError("Cannot divide by zero.")
|
| 48 |
-
return a / b
|
| 49 |
-
|
| 50 |
-
@tool
|
| 51 |
-
def modulus(a: int, b: int) -> int:
|
| 52 |
-
"""Return the modulus (remainder) of two numbers."""
|
| 53 |
-
return a % b
|
| 54 |
-
|
| 55 |
-
@tool
|
| 56 |
-
def wiki_search(query: str) -> str:
|
| 57 |
-
"""Search Wikipedia and return 2 results."""
|
| 58 |
-
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 59 |
-
return "\n\n---\n\n".join(doc.page_content for doc in docs)
|
| 60 |
-
|
| 61 |
-
@tool
|
| 62 |
-
def web_search(query: str) -> str:
|
| 63 |
-
"""Search the web using Tavily and return 3 results."""
|
| 64 |
-
docs = TavilySearch(max_results=3).invoke(query)
|
| 65 |
-
return "\n\n---\n\n".join(doc.page_content for doc in docs)
|
| 66 |
-
|
| 67 |
-
@tool
|
| 68 |
-
def arvix_search(query: str) -> str:
|
| 69 |
-
"""Search Arxiv for academic papers and return 3 results."""
|
| 70 |
-
docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 71 |
-
return "\n\n---\n\n".join(doc.page_content[:1000] for doc in docs)
|
| 72 |
-
|
| 73 |
-
# Load system prompt
|
| 74 |
-
with open("system_prompt.txt", "r") as f:
|
| 75 |
-
system_prompt = f.read()
|
| 76 |
-
|
| 77 |
-
sys_msg = SystemMessage(content=system_prompt)
|
| 78 |
-
|
| 79 |
-
# Vector search setup
|
| 80 |
-
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
| 81 |
-
vector_store = SupabaseVectorStore(
|
| 82 |
-
client=supabase,
|
| 83 |
-
embedding=embeddings,
|
| 84 |
-
table_name="documents",
|
| 85 |
-
query_name="match_documents_langchain",
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
retriever_tool = create_retriever_tool(
|
| 89 |
-
retriever=vector_store.as_retriever(),
|
| 90 |
-
name="Question Search",
|
| 91 |
-
description="Retrieve similar questions from vector DB.",
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
# Tools list
|
| 95 |
-
tools = [
|
| 96 |
-
multiply, add, subtract, divide, modulus,
|
| 97 |
-
wiki_search, web_search, arvix_search,
|
| 98 |
-
retriever_tool,
|
| 99 |
-
]
|
| 100 |
-
|
| 101 |
-
# Build LangGraph
|
| 102 |
-
|
| 103 |
-
def build_graph(provider: str = "groq"):
|
| 104 |
-
if provider == "google":
|
| 105 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 106 |
-
elif provider == "groq":
|
| 107 |
-
llm = ChatGroq(model="qwen-qwq-32b", temperature=0, api_key=os.getenv("GROQ_API"))
|
| 108 |
-
elif provider == "huggingface":
|
| 109 |
-
llm = ChatHuggingFace(llm=HuggingFaceEndpoint(
|
| 110 |
-
url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
|
| 111 |
-
temperature=0))
|
| 112 |
-
else:
|
| 113 |
-
raise ValueError("Invalid provider")
|
| 114 |
-
|
| 115 |
-
llm_with_tools = llm.bind_tools(tools)
|
| 116 |
-
|
| 117 |
-
def assistant(state: MessagesState):
|
| 118 |
-
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 119 |
-
|
| 120 |
-
def retriever(state: MessagesState):
|
| 121 |
-
docs = vector_store.similarity_search(state["messages"][0].content)
|
| 122 |
-
if not docs:
|
| 123 |
-
return {"messages": [sys_msg] + state["messages"]}
|
| 124 |
-
similar_msg = HumanMessage(content=f"Reference: {docs[0].page_content}")
|
| 125 |
-
return {"messages": [sys_msg] + state["messages"] + [similar_msg]}
|
| 126 |
-
|
| 127 |
-
builder = StateGraph(MessagesState)
|
| 128 |
-
builder.add_node("retriever", retriever)
|
| 129 |
-
builder.add_node("assistant", assistant)
|
| 130 |
-
builder.add_node("tools", ToolNode(tools))
|
| 131 |
-
builder.add_edge(START, "retriever")
|
| 132 |
-
builder.add_edge("retriever", "assistant")
|
| 133 |
-
builder.add_conditional_edges("assistant", tools_condition)
|
| 134 |
-
builder.add_edge("tools", "assistant")
|
| 135 |
-
|
| 136 |
-
return builder.compile()
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
# ============================
|
| 140 |
-
# Save this as model.py and let me know when you want full app.py regenerated to match
|
| 141 |
-
# ============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|