-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlp_module.py
66 lines (48 loc) · 1.91 KB
/
nlp_module.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
import os
import time
import openai
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.prompts import PromptTemplate
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ["OPENAI_API_KEY"]
llm = ChatOpenAI(temperature=0, model='gpt-3.5-turbo')
template = """/
{context}: You are the Meta-Mart Assistant and your name is 'Hayathi'. No extra words or explanation just give the product name and quantity value. if not found just say `False`
Question: {question}
Answer:
"""
PROMPT = PromptTemplate(template=template, input_variables=["context", 'question'])
# Text Splitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
embeddings = OpenAIEmbeddings()
def extract_tp_sl_values(file):
loader = TextLoader(file)
documents = loader.load()
# print(documents)
# Text Splitter
docs = text_splitter.split_documents(documents)
# Vectorstore: https://python.langchain.com/en/latest/modules/indexes/vectorstores.html
db = FAISS.from_documents(docs, embeddings)
retriever = db.as_retriever()
qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff",
retriever=retriever,
chain_type_kwargs={"prompt": PROMPT})
query = "i am sanaullah. I need 5kg meet. can you help me with this."
# Get the answer from the chain
start = time.time()
res = qa_chain(query)
answer = res['result']
end = time.time()
# Print the result
print("\n\n> Question:")
print(query)
print(f"\n> Answer (took {round(end - start, 2)} s.):")
print(answer)
return answer
extract_tp_sl_values("message.txt")