-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
59 lines (46 loc) · 1.42 KB
/
Copy pathchat.py
File metadata and controls
59 lines (46 loc) · 1.42 KB
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
from dotenv import load_dotenv
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_qdrant import QdrantVectorStore
from google import genai
import os
load_dotenv()
client = genai.Client(
api_key = os.getenv("GOOGLE_API_KEY")
)
# vector embedding
embedding_model = GoogleGenerativeAIEmbeddings(
model="models/gemini-embedding-2",
)
vector_db = QdrantVectorStore.from_existing_collection(
embedding=embedding_model,
url="http://localhost:6333",
collection_name="DSA_75"
)
# user input
user_query = input("ask me question:: ")
# relevant chunks from vector db
search_result = vector_db.similarity_search(query=user_query)
context = "\n\n\n".join([
(
f"Page content: {result.page_content}\n"
f"Page Number:{result.metadata['page_label']}\n"
f"File Location: {result.metadata['source']}"
)
for result in search_result
])
SYSTEM_PROMPT = f"""
You are a helpful AI assistant who answers user query based on available
context retrieved from the Pdf file along with page_contents and page number.
You should only ans the user based on the following context and navigate the
user to open the right page number to know more.
Context:
{context}
"""
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=user_query,
config={
"system_instruction": SYSTEM_PROMPT,
}
)
print(response.text)