Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.prompts import PromptTemplate
|
| 2 |
+
from langchain.llms import OpenAI
|
| 3 |
+
from langchain.chains import LLMChain
|
| 4 |
+
import gradio
|
| 5 |
+
|
| 6 |
+
## create the promt template with context & question
|
| 7 |
+
template = """Use the following pieces of context to answer the question at the end.
|
| 8 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 9 |
+
Use three sentences maximum and keep the answer as concise as possible.
|
| 10 |
+
{context}
|
| 11 |
+
Question: {question}
|
| 12 |
+
Helpful Answer:"""
|
| 13 |
+
|
| 14 |
+
QA_CHAIN_PROMPT = PromptTemplate(
|
| 15 |
+
input_variables=["context", "question"],
|
| 16 |
+
template=template,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
## setup LLM
|
| 20 |
+
llm = OpenAI()
|
| 21 |
+
|
| 22 |
+
## Setup the Q&A chain that stiches LLM, similar results from the vectordb & the prompt template
|
| 23 |
+
llm_chain = LLMChain(prompt=QA_CHAIN_PROMPT, llm=llm)
|
| 24 |
+
|
| 25 |
+
def blogbot(question):
|
| 26 |
+
## query similar results
|
| 27 |
+
# question = "are contents biased?"
|
| 28 |
+
retriever = db.as_retriever(search_type="mmr", search_kwargs={'k': 4, 'fetch_k': 5, 'lambda_mult': 0.25})
|
| 29 |
+
relevant_docs = retriever.get_relevant_documents(question)
|
| 30 |
+
## do the prompt against the Q&A chain & display the result
|
| 31 |
+
return str(llm_chain.run({'question': question, 'context': relevant_docs}))
|
| 32 |
+
|
| 33 |
+
gradio.Interface(blogbot, "text", "text").launch(share=True)
|