-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatBot.py
39 lines (28 loc) · 1.29 KB
/
chatBot.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
from langchain.vectorstores import Pinecone
from langchain.chat_models import ChatOpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chains.question_answering import load_qa_chain
import pinecone
import os
openai_key = os.getenv("OPENAI_API_KEY")
pinecone_key = os.getenv("PINECONE_API_KEY")
pinecoen_env = os.getenv("PINECONE_ENVIRONMENT")
def init_pinecone(pinecone_key, pinecoen_env):
# init pinecone
pinecone.init(
api_key=pinecone_key,
environment=pinecoen_env,
)
def get_answer(question):
query = f"You are a chatbot personalized for answering questions, now answer the\nquestion: {question} \n Do not go out of context in any circumstance or hallucinate or fabricate information."
# select the chat model and temperature
llm = ChatOpenAI(temperature=0.6, openai_api_key=openai_key)
embeddings = OpenAIEmbeddings(openai_api_key=openai_key)
# index_name -> index vector name in Pinecone
index_name = "woman-safety-embeddings"
# question and answer chain
qa_chain = load_qa_chain(llm, chain_type="stuff")
docsearch = Pinecone.from_existing_index(index_name, embeddings)
docs = docsearch.similarity_search(query)
answer = qa_chain.run(input_documents=docs, question=query, max_tokens=150)
return answer