delrickOliveira
commited on
Commit
•
aa0f66b
1
Parent(s):
2759b74
Inserting format of response and prompt template to request to llm
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
|
3 |
from pinecone import Pinecone
|
4 |
from langchain.chains import RetrievalQAWithSourcesChain
|
5 |
from langchain_community.vectorstores import Pinecone as PineconeVec
|
|
|
6 |
import os
|
7 |
|
8 |
openai_key = os.environ['openai_key']
|
@@ -32,15 +33,39 @@ llm = ChatOpenAI(
|
|
32 |
temperature=0.0
|
33 |
)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
qa_with_sources = RetrievalQAWithSourcesChain.from_chain_type(
|
36 |
llm=llm,
|
37 |
chain_type="stuff",
|
38 |
-
retriever=vectorstore.as_retriever()
|
|
|
|
|
39 |
)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def answer_question(query):
|
42 |
-
|
43 |
-
return
|
44 |
|
45 |
|
46 |
iface = gr.Interface(
|
|
|
3 |
from pinecone import Pinecone
|
4 |
from langchain.chains import RetrievalQAWithSourcesChain
|
5 |
from langchain_community.vectorstores import Pinecone as PineconeVec
|
6 |
+
from langchain.prompts import PromptTemplate
|
7 |
import os
|
8 |
|
9 |
openai_key = os.environ['openai_key']
|
|
|
33 |
temperature=0.0
|
34 |
)
|
35 |
|
36 |
+
#Prompt
|
37 |
+
|
38 |
+
PROMPT_TEMPLATE = """
|
39 |
+
You are called UEAid a first aid assistant helping a normal person to give first aid to some person.
|
40 |
+
Give clear instructions step by step for the {question} strictly based on this context {summaries}.
|
41 |
+
Always place in the end: "instructions given by UEAid".
|
42 |
+
"""
|
43 |
+
|
44 |
+
PROMPT = PromptTemplate(
|
45 |
+
template=PROMPT_TEMPLATE, input_variables=["summaries ", "question"]
|
46 |
+
)
|
47 |
+
|
48 |
+
chain_type_kwargs = {"prompt": PROMPT}
|
49 |
+
|
50 |
qa_with_sources = RetrievalQAWithSourcesChain.from_chain_type(
|
51 |
llm=llm,
|
52 |
chain_type="stuff",
|
53 |
+
retriever=vectorstore.as_retriever(),
|
54 |
+
return_source_documents=True,
|
55 |
+
chain_type_kwargs=chain_type_kwargs
|
56 |
)
|
57 |
|
58 |
+
def format_response(response):
|
59 |
+
sources_info = ''
|
60 |
+
documents = response['source_documents']
|
61 |
+
for document in documents:
|
62 |
+
sources_info += "document: {}, page: {}\n".format(document.metadata['source'].rsplit("/")[-1], int(document.metadata['page']))
|
63 |
+
formated_response = response['answer'] + " Sources:\n" + sources_info
|
64 |
+
return formated_response
|
65 |
+
|
66 |
def answer_question(query):
|
67 |
+
response = qa_with_sources.invoke(query)
|
68 |
+
return format_response(response)
|
69 |
|
70 |
|
71 |
iface = gr.Interface(
|