|
from langchain_community.vectorstores import Qdrant |
|
from langchain_huggingface import HuggingFaceEmbeddings |
|
from langchain.llms import HuggingFacePipeline |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
import os |
|
from dotenv import load_dotenv |
|
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_qdrant import Qdrant |
|
import gradio as gr |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en-v1.5") |
|
|
|
|
|
client = QdrantClient( |
|
url=os.getenv("QDRANT_URL"), |
|
api_key=os.getenv("QDRANT_API_KEY"), |
|
prefer_grpc=True |
|
) |
|
|
|
collection_name = "mawared" |
|
|
|
|
|
try: |
|
client.create_collection( |
|
collection_name=collection_name, |
|
vectors_config=models.VectorParams( |
|
size=768, |
|
distance=models.Distance.COSINE |
|
), |
|
) |
|
print(f"Created new collection: {collection_name}") |
|
except Exception as e: |
|
if "already exists" in str(e): |
|
print(f"Collection {collection_name} already exists, continuing...") |
|
else: |
|
raise e |
|
|
|
|
|
db = Qdrant( |
|
client=client, |
|
collection_name=collection_name, |
|
embeddings=embeddings, |
|
) |
|
|
|
|
|
retriever = db.as_retriever( |
|
search_type="similarity", |
|
search_kwargs={"k": 5} |
|
) |
|
|
|
|
|
model_name = "Daemontatox/CogitoZ14" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", trust_remote_code=True) |
|
hf_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
|
|
|
llm = HuggingFacePipeline(pipeline=hf_pipeline) |
|
|
|
|
|
template = """ |
|
You are an expert assistant specializing in the Mawared HR System. Your task is to answer the user's question strictly based on the provided context. If the context lacks sufficient information, ask focused clarifying questions to gather additional details. |
|
|
|
To improve your responses, follow these steps: |
|
|
|
Chain-of-Thought (COT): Break down complex queries into logical steps. Use tags like [Step 1], [Step 2], etc., to label each part of the reasoning process. This helps structure your thinking and ensure clarity. For example: |
|
|
|
[Step 1] Identify the key details in the context relevant to the question. |
|
[Step 2] Break down any assumptions or information gaps. |
|
[Step 3] Combine all pieces to form the final, well-reasoned response. |
|
Reasoning: Demonstrate a clear logical connection between the context and your answer at each step. If information is missing or unclear, indicate the gap using tags like [Missing Information] and ask relevant follow-up questions to fill that gap. |
|
|
|
Clarity and Precision: Provide direct, concise answers focused only on the context. Avoid including speculative or unrelated information. |
|
|
|
Follow-up Questions: If the context is insufficient, focus on asking specific, relevant questions. Label them as [Clarifying Question] to indicate they are needed to complete the response. For example: |
|
|
|
[Clarifying Question] Could you specify which employee section you're referring to? |
|
Context: |
|
{context} |
|
|
|
Question: |
|
{question} |
|
|
|
Answer |
|
""" |
|
|
|
prompt = ChatPromptTemplate.from_template(template) |
|
|
|
|
|
rag_chain = ( |
|
{"context": retriever, "question": RunnablePassthrough()} |
|
| prompt |
|
| llm |
|
| StrOutputParser() |
|
) |
|
|
|
|
|
def ask_question_gradio(question): |
|
result = "" |
|
for chunk in rag_chain.stream(question): |
|
result += chunk |
|
return result |
|
|
|
|
|
interface = gr.Interface( |
|
fn=ask_question_gradio, |
|
inputs="text", |
|
outputs="text", |
|
title="Mawared Expert Assistant", |
|
description="Ask questions about the Mawared HR System or any related topic using Chain-of-Thought (CoT) and RAG principles.", |
|
theme="compact", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|