Spaces:
Sleeping
Sleeping
File size: 3,173 Bytes
9408cb5 e3e0c81 9408cb5 16a513a 9408cb5 16a513a 9408cb5 e3e0c81 9408cb5 16a513a 9408cb5 e3e0c81 9408cb5 e3e0c81 9408cb5 e3e0c81 9408cb5 16a513a 9408cb5 16a513a 9408cb5 16a513a 9408cb5 16a513a 9408cb5 e3e0c81 16a513a e3e0c81 9408cb5 e3e0c81 9408cb5 e3e0c81 9408cb5 16a513a 9408cb5 16a513a e3e0c81 16a513a e3e0c81 16a513a e3e0c81 16a513a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# app.py
import os
import warnings
from dotenv import load_dotenv
import gradio as gr
from qdrant_search import QdrantSearch
from langchain_groq import ChatGroq
from nomic_embeddings import EmbeddingsModel
# Load environment variables from .env file
load_dotenv()
# Suppress FutureWarnings
warnings.filterwarnings("ignore", category=FutureWarning)
# Disable tokenizers parallelism to avoid potential issues
os.environ["TOKENIZERS_PARALLELISM"] = "FALSE"
# Initialize global variables
collection_names = ["docs_v1_2", "docs_v2_2", "docs_v3_2"]
limit = 5
# Initialize the language model
llm = ChatGroq(model="mixtral-8x7b-32768")
# Initialize the embeddings model
embeddings = EmbeddingsModel()
# Initialize Qdrant search with necessary credentials
search = QdrantSearch(
qdrant_url=os.environ["QDRANT_CLOUD_URL"],
api_key=os.environ["QDRANT_API_KEY"],
embeddings=embeddings
)
def chat_endpoint(question: str):
"""
Handles the chat functionality by processing the user's question,
retrieving relevant documents, generating an answer, and returning sources.
Args:
question (str): The user's question.
Returns:
Tuple[str, str]: The generated answer and the sources used.
"""
query = question.strip()
if not query:
return "❌ **Error:** Query cannot be empty.", "No sources available."
# Step 1: Retrieve relevant documents from Qdrant
retrieved_docs = search.query_multiple_collections(query, collection_names, limit)
if not retrieved_docs:
return "⚠️ **No relevant documents found** for your query.", "No sources available."
# Step 2: Prepare the context from retrieved documents
context = "\n\n".join([doc['text'] for doc in retrieved_docs])
# Step 3: Construct the prompt with context and question
prompt = (
"You are LangAssist, a knowledgeable assistant for the LangChain Python Library. "
"Given the following context from the documentation, provide a helpful answer to the user's question.\n\n"
"### Context:\n{context}\n\n"
"### Question:\n{question}\n\n"
"### Answer:"
).format(context=context, question=query)
# Step 4: Generate an answer using the language model
try:
answer = llm.invoke(prompt)
except Exception as e:
return f"⚠️ **Error generating answer:** {str(e)}", "No sources available."
# Prepare sources
sources_md = "\n\n".join([
f"**Source:** {src['source']}\n**Excerpt:** {src['text']}"
for src in retrieved_docs
])
return answer.content.strip(), sources_md
# Create Gradio Interface
interface = gr.Interface(
fn=chat_endpoint,
inputs=gr.Textbox(
lines=2,
placeholder="Type your question here...",
label="Your Question"
),
outputs=[
gr.Markdown(label="Answer"),
gr.Markdown(label="Sources")
],
title="🗨️ LangAssist Chat",
description="Ask questions about the LangChain Python Library and get answers based on the latest documentation."
)
# If running locally, uncomment the following lines:
# if __name__ == "__main__":
# interface.launch()
|