Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
6 |
+
from langchain.text_splitter import CharacterTextSplitter
|
7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
+
from langchain.vectorstores import Chroma
|
9 |
+
from langchain.document_loaders import TextLoader
|
10 |
+
from langchain.chat_models import ChatOpenAI
|
11 |
+
from langchain import PromptTemplate
|
12 |
+
from langchain.chains import LLMChain
|
13 |
+
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
|
14 |
+
from langchain.llms import OpenAI
|
15 |
+
from langchain.vectorstores import FAISS
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
db = pd.read_pickle('index.pkl')
|
21 |
+
|
22 |
+
#-----------------------------------------------------------------------------
|
23 |
+
def get_response_from_query(db, query, k=3):
|
24 |
+
|
25 |
+
docs = db.similarity_search(query, k=k)
|
26 |
+
|
27 |
+
docs_page_content = " ".join([d.page_content for d in docs])
|
28 |
+
|
29 |
+
# llm = BardLLM()
|
30 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k",temperature=0)
|
31 |
+
|
32 |
+
prompt = PromptTemplate(
|
33 |
+
input_variables=["question", "docs"],
|
34 |
+
template="""
|
35 |
+
A bot that is open to discussions about different cultural, philosophical and political exchanges. I will use do different analysis to the articles provided to me. Stay truthful and if you weren't provided any resources give your oppinion only.
|
36 |
+
Answer the following question: {question}
|
37 |
+
By searching the following articles: {docs}
|
38 |
+
|
39 |
+
Only use the factual information from the documents. Make sure to mention key phrases from the articles.
|
40 |
+
|
41 |
+
If you feel like you don't have enough information to answer the question, say "I don't know".
|
42 |
+
|
43 |
+
""",
|
44 |
+
)
|
45 |
+
|
46 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
47 |
+
# chain = RetrievalQAWithSourcesChain.from_chain_type(llm=llm, prompt=prompt,
|
48 |
+
# chain_type="stuff", retriever=db.as_retriever(), return_source_documents=True)
|
49 |
+
|
50 |
+
response = chain.run(question=query, docs=docs_page_content,return_source_documents=True)
|
51 |
+
r_text = str(response)
|
52 |
+
|
53 |
+
##evaluation part
|
54 |
+
|
55 |
+
prompt_eval = PromptTemplate(
|
56 |
+
input_variables=["answer", "docs"],
|
57 |
+
template="""
|
58 |
+
You job is to evaluate if the response to a given context is faithful.
|
59 |
+
|
60 |
+
for the following: {answer}
|
61 |
+
By searching the following article: {docs}
|
62 |
+
|
63 |
+
Give a reason why they are similar or not, start with a Yes or a No.
|
64 |
+
""",
|
65 |
+
)
|
66 |
+
|
67 |
+
chain_part_2 = LLMChain(llm=llm, prompt=prompt_eval)
|
68 |
+
|
69 |
+
|
70 |
+
evals = chain_part_2.run(answer=r_text, docs=docs_page_content)
|
71 |
+
|
72 |
+
return response,docs,evals
|
73 |
+
|
74 |
+
def greet(query):
|
75 |
+
|
76 |
+
answer,sources,evals = get_response_from_query(db,query,2)
|
77 |
+
return answer,sources,evals
|
78 |
+
|
79 |
+
demo = gr.Interface(fn=greet, title="cicero-semantic-search", inputs="text",
|
80 |
+
outputs=[gr.components.Textbox(lines=3, label="Response"),
|
81 |
+
gr.components.Textbox(lines=3, label="Source"),
|
82 |
+
gr.components.Textbox(lines=3, label="Evaluation")])
|
83 |
+
|
84 |
+
demo.launch(share=True)
|
85 |
+
|
86 |
+
|
87 |
+
|