-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
50 lines (36 loc) · 1.39 KB
/
Copy pathchat.py
File metadata and controls
50 lines (36 loc) · 1.39 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
from dotenv import load_dotenv
from langchain_openai import OpenAIEmbeddings
from langchain_qdrant import QdrantVectorStore
from openai import OpenAI
from constants import COLLECTION_NAME, EMBEDDING_MODEL, QDRANT_URL
load_dotenv()
openai_client = OpenAI()
# Vector Embeddings
embedding_model = OpenAIEmbeddings(
model=EMBEDDING_MODEL
)
vector_db = QdrantVectorStore.from_existing_collection(
url=QDRANT_URL,
collection_name=COLLECTION_NAME,
embedding=embedding_model,
)
# Take user input
user_query = input("Ask something: ")
# Relevant chunks from the vector db
search_results = vector_db.similarity_search(query=user_query)
context = "\n\n\n".join(
[f"Page Content: {result.page_content}\nPage Number: {result.metadata['page_label']}\nFile Location: {result.metadata['source']}" for result in search_results])
SYSTEM_PROMPT = f"""
You are a helpfull AI Assistant who answeres user query based on the available context retrieved from a 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 = openai_client.chat.completions.create(
model="gpt-5",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_query},
]
)
print(f"🤖: {response.choices[0].message.content}")