File size: 3,588 Bytes
8359d12 0217602 c2c5723 0217602 8359d12 0217602 6386510 c2c5723 6386510 0217602 62138c4 c2c5723 b70c257 c2c5723 0217602 652620b 0217602 8359d12 7cb9567 0217602 c2c5723 0217602 b70c257 0217602 c2c5723 0217602 c2c5723 0217602 c2c5723 b70c257 0217602 b70c257 c2c5723 1cdadeb c2c5723 1cdadeb c2c5723 561ff95 c2c5723 51a7d9e 0217602 c2c5723 0217602 8359d12 0217602 51a7d9e 0217602 |
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
import os
from dotenv import load_dotenv
from langchain_community.vectorstores import Qdrant
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
from qdrant_client import QdrantClient, models
from langchain_openai import ChatOpenAI
import gradio as gr
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
# HuggingFace API Token
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
logger.error("HF_TOKEN is not set in the environment variables.")
exit(1)
# HuggingFace Embeddings
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en-v1.5")
# Qdrant Client Setup
try:
client = QdrantClient(
url=os.getenv("QDRANT_URL"),
api_key=os.getenv("QDRANT_API_KEY"),
prefer_grpc=True
)
except Exception as e:
logger.error("Failed to connect to Qdrant. Ensure QDRANT_URL and QDRANT_API_KEY are correctly set.")
exit(1)
# Define collection name
collection_name = "mawared"
# Try to create collection
try:
client.create_collection(
collection_name=collection_name,
vectors_config=models.VectorParams(
size=768, # GTE-large embedding size
distance=models.Distance.COSINE
)
)
logger.info(f"Created new collection: {collection_name}")
except Exception as e:
if "already exists" in str(e):
logger.info(f"Collection {collection_name} already exists, continuing...")
else:
logger.error(f"Error creating collection: {e}")
exit(1)
# Create Qdrant vector store
db = Qdrant(
client=client,
collection_name=collection_name,
embeddings=embeddings,
)
# Create retriever
retriever = db.as_retriever(
search_type="similarity",
search_kwargs={"k": 5}
)
# Set up the LLM
llm = ChatOpenAI(
base_url="https://api-inference.huggingface.co/v1/",
temperature=0,
api_key=HF_TOKEN,
model="meta-llama/Llama-3.3-70B-Instruct"
)
# Create prompt template
template = """
You are an expert assistant specializing in the Mawared HR System. Your task is to provide accurate and contextually relevant answers strictly based on the provided context. If the context lacks sufficient information, ask targeted clarifying questions to gather specific details required for a precise response. Always ensure clarity, brevity, and relevance in your answers.
Context:
{context}
Question:
{question}
Answer:
"""
prompt = ChatPromptTemplate.from_template(template)
# Create the RAG chain
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Gradio Function
def ask_question_gradio(question):
try:
response = ""
for chunk in rag_chain.stream(question):
response += chunk
return response
except Exception as e:
logger.error(f"Error during question processing: {e}")
return "An error occurred. Please try again later."
# Gradio Interface
iface = gr.Interface(
fn=ask_question_gradio,
inputs=gr.Textbox(label="Ask a question about Mawared HR System:"),
outputs=gr.Textbox(label="Answer:"),
title="Mawared HR Assistant",
description="Ask questions about the Mawared HR system, and this assistant will provide answers based on the available context."
)
# Launch the Gradio App
if __name__ == "__main__":
iface.launch() |