siddh4rth commited on
Commit
11ac44e
·
1 Parent(s): ca155d3

Add app file

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import openai
4
+ import pinecone
5
+
6
+ openai.api_key = os.environ["OPENAI-API-KEY"]
7
+
8
+ pinecone.init(
9
+ api_key=os.environ["PINECONE-API-KEY"],
10
+ environment="us-east1-gcp",
11
+ )
12
+
13
+ limit = 5000
14
+ # 3750
15
+
16
+ embed_model = "text-embedding-ada-002"
17
+
18
+ index_name = 'extractive-qa'
19
+ index = pinecone.Index(index_name)
20
+
21
+ # retrieve relevant answers
22
+ def retrieve(query):
23
+ res = openai.Embedding.create(
24
+ input=[query],
25
+ engine=embed_model,
26
+ )
27
+
28
+ # retrieve from Pinecone
29
+ xq = res['data'][0]['embedding']
30
+
31
+ # get relevant contexts
32
+ res = index.query(xq, top_k=3, include_metadata=True)
33
+ contexts = [
34
+ x['metadata']['text'] for x in res['matches']
35
+ ]
36
+
37
+ # build our prompt with the retrieved contexts included
38
+ prompt_start = (
39
+ "Answer the question based on the context below.\n\n"+
40
+ "Context:\n"
41
+ )
42
+ prompt_end = (
43
+ f"\n\nQuestion: {query}\nAnswer:"
44
+ )
45
+
46
+ # append contexts until hitting limit
47
+ for i in range(1, len(contexts)):
48
+ if len("\n\n---\n\n".join(contexts[:i])) >= limit:
49
+ prompt = (
50
+ prompt_start +
51
+ "\n\n---\n\n".join(contexts[:i-1]) +
52
+ prompt_end
53
+ )
54
+ break
55
+ elif i == len(contexts)-1:
56
+ prompt = (
57
+ prompt_start +
58
+ "\n\n---\n\n".join(contexts) +
59
+ prompt_end
60
+ )
61
+ return prompt
62
+
63
+ # then we complete the context-infused query
64
+ def complete(prompt):
65
+ # query text-davinci-003
66
+ response = openai.ChatCompletion.create(
67
+ model="gpt-4",
68
+ messages=[
69
+ {
70
+ "role": "system",
71
+ "content": "Answer the question based on the context below."
72
+ },
73
+ {
74
+ "role": "user",
75
+ "content": prompt
76
+ }
77
+ ],
78
+ temperature=0.5,
79
+ max_tokens=128,
80
+ top_p=1.0,
81
+ frequency_penalty=0.0,
82
+ presence_penalty=0.0
83
+ )
84
+ return response.choices[0].message.content.strip()
85
+
86
+ def greet(query):
87
+ # first we retrieve relevant items from Pinecone
88
+ query_with_contexts = retrieve(query)
89
+ # return only the main answer
90
+ result = complete(query_with_contexts)
91
+ return result
92
+
93
+ iface = gr.Interface(fn=greet, inputs="text", outputs="text")
94
+ iface.launch()
95
+