-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
78 lines (64 loc) · 2.72 KB
/
bot.py
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
import os
from langchain.chains import (
create_history_aware_retriever,
create_retrieval_chain,
)
from langchain_core.messages import HumanMessage, AIMessage
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_pinecone import PineconeVectorStore
embeddings = OpenAIEmbeddings(openai_api_type=os.environ.get("OPENAI_API_KEY"))
vectorstore = PineconeVectorStore(
index_name=os.environ["INDEX_NAME"], embedding=embeddings
)
retriever = vectorstore.as_retriever()
llm = ChatOpenAI(verbose=True, temperature=0, model_name="gpt-4o-mini")
contextualize_q_system_prompt = (
"Given a chat history and the latest user question "
"which might reference context in the chat history, "
"formulate a standalone question which can be understood "
"without the chat history. Do NOT answer the question, just "
"reformulate it if needed and otherwise return it as is."
)
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
history_aware_retriever = create_history_aware_retriever(
llm=llm, retriever=retriever, prompt=contextualize_q_prompt
)
qa_system_prompt = (
"You are a virtual assistant designed to answer questions specifically about Albert Derevski. "
"Use the following pieces of retrieved context to answer the question, ensuring the response is relevant to Albert's "
"professional experience, skills, education, or portfolio. If you don't know the answer or the question is unrelated to Albert, "
"respond politely with, 'I'm sorry, I can only answer questions about Albert Derevski.' Avoid speculation or fabricated information. "
"Keep the response concise, professional.\n\n"
"{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", qa_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm=llm, prompt=qa_prompt)
rag_chain = create_retrieval_chain(
history_aware_retriever,
question_answer_chain
)
chat_history = []
print("Welcome! Ask me questions about Albert Derevski. Type 'exit' to quit.")
while True:
query = input("You: ")
if query.lower() == "exit":
print("Goodbye!")
break
response = rag_chain.invoke({"input": query, "chat_history": chat_history})
print(f"Assistant: {response['answer']}")
chat_history.append(HumanMessage(content=query))
chat_history.append(AIMessage(content=response['answer']))