jybae commited on
Commit
c630198
1 Parent(s): 52f5a76

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def get_response_from_query(db, query, k=3):
4
+
5
+
6
+ docs = db.similarity_search(query, k=k)
7
+
8
+ docs_page_content = " ".join([d.page_content for d in docs])
9
+
10
+ # llm = BardLLM()
11
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k",temperature=0)
12
+
13
+ prompt = PromptTemplate(
14
+ input_variables=["question", "docs"],
15
+ template="""
16
+ 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.
17
+ Answer the following question: {question}
18
+ By searching the following articles: {docs}
19
+
20
+ Only use the factual information from the documents. Make sure to mention key phrases from the articles.
21
+
22
+ If you feel like you don't have enough information to answer the question, say "I don't know".
23
+
24
+ """,
25
+ )
26
+
27
+ chain = LLMChain(llm=llm, prompt=prompt)
28
+ response = chain.run(question=query, docs=docs_page_content,return_source_documents=True)
29
+ r_text = str(response)
30
+
31
+ ##evaluation part
32
+
33
+ prompt_eval = PromptTemplate(
34
+ input_variables=["answer", "docs"],
35
+ template="""
36
+ You job is to evaluate if the response to a given context is faithful.
37
+
38
+ for the following: {answer}
39
+ By searching the following article: {docs}
40
+
41
+ Give a reason why they are similar or not, start with a Yes or a No.
42
+ """,
43
+ )
44
+
45
+ chain_part_2 = LLMChain(llm=llm, prompt=prompt_eval)
46
+
47
+
48
+ evals = chain_part_2.run(answer=r_text, docs=docs_page_content)
49
+
50
+ return response,docs,evals
51
+
52
+
53
+
54
+ def greet(query):
55
+
56
+ answer,sources,evals = get_response_from_query(db,query,2)
57
+ return answer,sources,evals
58
+ examples = [
59
+ ["How to be happy"],
60
+ ["Climate Change Challenges in Europe"],
61
+ ["Philosophy in the world of Minimalism"],
62
+ ["Hate Speech vs Freedom of Speech"],
63
+ ["Articles by Noam Chomsky on US Politics"],
64
+ ["The importance of values and reflection"]
65
+ ]
66
+ demo = gr.Interface(fn=greet, title="cicero-semantic-search", inputs="text",
67
+ outputs=[gr.components.Textbox(lines=3, label="Response"),
68
+ gr.components.Textbox(lines=3, label="Source"),
69
+ gr.components.Textbox(lines=3, label="Evaluation")],
70
+ examples=examples)
71
+
72
+ demo.launch(share=True)