Spaces:
Running
Running
Added Supabase DB functionality
Browse files- .DS_Store +0 -0
- .gitignore +5 -0
- app/rag.py +48 -6
.DS_Store
CHANGED
|
Binary files a/.DS_Store and b/.DS_Store differ
|
|
|
.gitignore
CHANGED
|
@@ -6,3 +6,8 @@ __pycache__/
|
|
| 6 |
models/
|
| 7 |
.cache/
|
| 8 |
.env
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
models/
|
| 7 |
.cache/
|
| 8 |
.env
|
| 9 |
+
*.pdf
|
| 10 |
+
|
| 11 |
+
# macOS
|
| 12 |
+
.DS_Store
|
| 13 |
+
data/.DS_Store
|
app/rag.py
CHANGED
|
@@ -9,25 +9,31 @@ from langchain_qdrant import QdrantVectorStore
|
|
| 9 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 10 |
from langgraph.graph import StateGraph, END
|
| 11 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 12 |
|
|
|
|
| 13 |
import os
|
|
|
|
| 14 |
|
| 15 |
from qdrant_client import QdrantClient
|
| 16 |
|
| 17 |
-
import
|
| 18 |
-
|
| 19 |
load_dotenv()
|
| 20 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 21 |
|
|
|
|
| 22 |
session_histories: dict[str, list] = {}
|
| 23 |
# %%
|
| 24 |
LLM_MODEL = "mistralai/mistral-7b-instruct:free"
|
|
|
|
| 25 |
OPENROUTER_API_KEY = os.getenv('OPENROUTER_API_KEY')
|
| 26 |
COLLECTION_NAME = "wellness_docs"
|
| 27 |
EMBEDDING_MODEL = "intfloat/e5-large-v2"
|
| 28 |
QDRANT_URL = os.getenv('QDRANT_URL')
|
| 29 |
QDRANT_API_KEY = os.getenv('QDRANT_API_KEY')
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
FAQ_COLLECTION = "faqs"
|
| 33 |
BLOGS_COLLECTION = "blogs"
|
|
@@ -35,7 +41,7 @@ TECHNOLOGY_COLLECTION = "technology"
|
|
| 35 |
REVOLUTION_COLLECTION = "revolution"
|
| 36 |
|
| 37 |
# %%
|
| 38 |
-
llm = ChatOpenAI(
|
| 39 |
model_name=LLM_MODEL,
|
| 40 |
openai_api_key=OPENROUTER_API_KEY,
|
| 41 |
base_url="https://openrouter.ai/api/v1",
|
|
@@ -43,8 +49,12 @@ llm = ChatOpenAI(
|
|
| 43 |
"HTTP-Referer": "http://localhost",
|
| 44 |
"X-Title": "Auro Wellness Chatbot",
|
| 45 |
}
|
| 46 |
-
)
|
|
|
|
|
|
|
|
|
|
| 47 |
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
|
|
|
|
| 48 |
|
| 49 |
try:
|
| 50 |
client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)
|
|
@@ -69,7 +79,23 @@ class ToolInput(BaseModel):
|
|
| 69 |
prompt: str
|
| 70 |
iteration: int = 1
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# %%
|
| 74 |
template = """
|
| 75 |
You are Auro-Chat, a wellness assistant for Auro Wellness, here to answer any questions anyone has on wellness using the provided context from Auro Wellness's websites which contained company-approved documents, blogs, and FAQs
|
|
@@ -258,6 +284,22 @@ async def get_response(query: str, config) -> dict:
|
|
| 258 |
"input": query,
|
| 259 |
"history": history
|
| 260 |
}
|
|
|
|
| 261 |
result = await app.ainvoke(input_data, config=config)
|
|
|
|
| 262 |
session_histories[session_id] = result.get("history", [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
return result
|
|
|
|
| 9 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 10 |
from langgraph.graph import StateGraph, END
|
| 11 |
from langchain_openai import ChatOpenAI
|
| 12 |
+
from langchain_ollama.chat_models import ChatOllama
|
| 13 |
|
| 14 |
+
import time
|
| 15 |
import os
|
| 16 |
+
from dotenv import load_dotenv
|
| 17 |
|
| 18 |
from qdrant_client import QdrantClient
|
| 19 |
|
| 20 |
+
from supabase import create_client, Client
|
| 21 |
+
|
| 22 |
load_dotenv()
|
| 23 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 24 |
|
| 25 |
+
|
| 26 |
session_histories: dict[str, list] = {}
|
| 27 |
# %%
|
| 28 |
LLM_MODEL = "mistralai/mistral-7b-instruct:free"
|
| 29 |
+
LLM_MODEL = "mistral:latest"
|
| 30 |
OPENROUTER_API_KEY = os.getenv('OPENROUTER_API_KEY')
|
| 31 |
COLLECTION_NAME = "wellness_docs"
|
| 32 |
EMBEDDING_MODEL = "intfloat/e5-large-v2"
|
| 33 |
QDRANT_URL = os.getenv('QDRANT_URL')
|
| 34 |
QDRANT_API_KEY = os.getenv('QDRANT_API_KEY')
|
| 35 |
+
SUPABASE_URL = os.getenv('SUPABASE_URL')
|
| 36 |
+
SUPABASE_KEY = os.getenv('SUPABASE_KEY')
|
| 37 |
|
| 38 |
FAQ_COLLECTION = "faqs"
|
| 39 |
BLOGS_COLLECTION = "blogs"
|
|
|
|
| 41 |
REVOLUTION_COLLECTION = "revolution"
|
| 42 |
|
| 43 |
# %%
|
| 44 |
+
"""llm = ChatOpenAI(
|
| 45 |
model_name=LLM_MODEL,
|
| 46 |
openai_api_key=OPENROUTER_API_KEY,
|
| 47 |
base_url="https://openrouter.ai/api/v1",
|
|
|
|
| 49 |
"HTTP-Referer": "http://localhost",
|
| 50 |
"X-Title": "Auro Wellness Chatbot",
|
| 51 |
}
|
| 52 |
+
)"""
|
| 53 |
+
|
| 54 |
+
llm = ChatOllama(model=LLM_MODEL)
|
| 55 |
+
|
| 56 |
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
|
| 57 |
+
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 58 |
|
| 59 |
try:
|
| 60 |
client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)
|
|
|
|
| 79 |
prompt: str
|
| 80 |
iteration: int = 1
|
| 81 |
|
| 82 |
+
def log_chat(session_id: str, query: str, answer: str, metadata: dict=None):
|
| 83 |
+
"""
|
| 84 |
+
Logs a chat interaction to the Supabase 'chat_logs' table.
|
| 85 |
+
"""
|
| 86 |
+
data = {
|
| 87 |
+
"session_id": session_id,
|
| 88 |
+
"query": query,
|
| 89 |
+
"answer": answer,
|
| 90 |
+
"metadata": metadata or {}
|
| 91 |
+
}
|
| 92 |
+
try:
|
| 93 |
+
response = supabase.table("chat_logs").insert(data).execute()
|
| 94 |
+
print("Logged chat interaction:", response)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print("Failed to log chat interaction:", e)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
# %%
|
| 100 |
template = """
|
| 101 |
You are Auro-Chat, a wellness assistant for Auro Wellness, here to answer any questions anyone has on wellness using the provided context from Auro Wellness's websites which contained company-approved documents, blogs, and FAQs
|
|
|
|
| 284 |
"input": query,
|
| 285 |
"history": history
|
| 286 |
}
|
| 287 |
+
start_time = time.time()
|
| 288 |
result = await app.ainvoke(input_data, config=config)
|
| 289 |
+
latency_ms = int((time.time() - start_time) * 1000)
|
| 290 |
session_histories[session_id] = result.get("history", [])
|
| 291 |
+
|
| 292 |
+
metadata = {
|
| 293 |
+
"retrieved_docs": result.get("tool_results", {}),
|
| 294 |
+
"model": LLM_MODEL,
|
| 295 |
+
"embedding_model": EMBEDDING_MODEL,
|
| 296 |
+
"latency_ms": latency_ms
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
log_chat(
|
| 300 |
+
session_id=session_id,
|
| 301 |
+
query=query,
|
| 302 |
+
answer=result.get("response", ""),
|
| 303 |
+
metadata=metadata
|
| 304 |
+
)
|
| 305 |
return result
|