File size: 4,934 Bytes
62138c4
 
c2c5723
 
8991905
 
c2c5723
 
 
 
 
51a7d9e
62138c4
e32f016
6386510
c2c5723
 
6386510
62138c4
 
 
 
 
 
 
c2c5723
 
b70c257
c2c5723
 
62138c4
 
b4fc3f1
c2c5723
652620b
a1ebba4
 
62138c4
 
 
 
 
 
 
 
7cb9567
c2c5723
 
 
 
 
 
 
 
b70c257
c2c5723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b70c257
8991905
62138c4
8991905
 
25e599b
62138c4
3365a48
 
62138c4
25e599b
ee642b9
8991905
 
1eaecfb
b70c257
c2c5723
 
f3bc24e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2c5723
 
f77fb99
c2c5723
 
b70c257
f3bc24e
c2c5723
561ff95
c2c5723
51a7d9e
c2c5723
 
 
 
 
 
 
 
 
62138c4
c2c5723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51a7d9e
c2c5723
51a7d9e
25e599b
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
from dotenv import load_dotenv
from langchain_community.vectorstores import Qdrant
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.llms import HuggingFacePipeline
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
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
import torch
import spaces

# Load environment variables
load_dotenv()

# Verify environment variables
qdrant_url = os.getenv("QDRANT_URL")
qdrant_api_key = os.getenv("QDRANT_API_KEY")

print(f"QDRANT_URL: {qdrant_url}")
print(f"QDRANT_API_KEY: {qdrant_api_key}")

# HuggingFace Embeddings
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en-v1.5")

# Qdrant Client Setup
client = QdrantClient(
    url=qdrant_url,
    api_key=qdrant_api_key,
    #prefer_grpc=True
)

collection_name="mawared"

# Check if the connection is successful
try:
    client.get_collection(collection_name)
    print(f"Successfully connected to Qdrant collection: {collection_name}")
except Exception as e:
    print(f"Failed to connect to Qdrant: {e}")
    raise e


# Try to create collection, handle if it already exists
try:
    client.create_collection(
        collection_name=collection_name,
        vectors_config=models.VectorParams(
            size=768,  # GTE-large embedding size
            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

# 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}
)

# Load Hugging Face Model
model_name = "NousResearch/Hermes-3-Llama-3.2-3B"  # Replace with your desired model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", trust_remote_code=True)

# Ensure the model is on the GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Create Hugging Face Pipeline with the specified model and tokenizer
hf_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)

# LangChain LLM using Hugging Face Pipeline
llm = HuggingFacePipeline(pipeline=hf_pipeline)

# Create prompt template
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)

# Create the RAG chain
rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

# Define the Gradio function
@spaces.GPU()
def ask_question_gradio(question):
    result = ""
    for chunk in rag_chain.stream(question):
        result += chunk
    return result

# Create the Gradio interface
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",
)

# Launch Gradio app
if __name__ == "__main__":
    interface.launch()