Spaces:
Runtime error
Runtime error
Update app.py
Browse filesMade the big change - went for the more detailed answer version
app.py
CHANGED
@@ -20,47 +20,153 @@ def get_combo_index():
|
|
20 |
index = GPTSimpleVectorIndex.load_from_disk('comboindex.json')
|
21 |
return index
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def querying_db(query: str):
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
tools = [
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
]
|
36 |
|
37 |
-
prefix = "Give a detailed answer to the question
|
38 |
-
suffix = """Give answer
|
39 |
|
40 |
Question: {input}
|
41 |
{agent_scratchpad}"""
|
42 |
|
43 |
prompt = ZeroShotAgent.create_prompt(
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
)
|
49 |
|
50 |
-
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
|
51 |
tool_names = [tool.name for tool in tools]
|
52 |
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
|
53 |
|
54 |
|
55 |
def get_answer(query_string):
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
return result
|
60 |
|
61 |
|
62 |
def qa_app(query):
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
|
66 |
inputs = gr.inputs.Textbox(label="Enter your question:")
|
|
|
20 |
index = GPTSimpleVectorIndex.load_from_disk('comboindex.json')
|
21 |
return index
|
22 |
|
23 |
+
def generate_variations(question):
|
24 |
+
def send_message(message_log):
|
25 |
+
# Use OpenAI's ChatCompletion API to get the chatbot's response
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model="gpt-3.5-turbo", # The name of the OpenAI chatbot model to use
|
28 |
+
messages=message_log, # The conversation history up to this point, as a list of dictionaries
|
29 |
+
max_tokens=1000, # The maximum number of tokens (words or subwords) in the generated response
|
30 |
+
stop=None, # The stopping sequence for the generated response, if any (not used here)
|
31 |
+
temperature=0.7, # The "creativity" of the generated response (higher temperature = more creative)
|
32 |
+
)
|
33 |
+
|
34 |
+
# Find the first response from the chatbot that has text in it (some responses may not have text)
|
35 |
+
for choice in response.choices:
|
36 |
+
if "text" in choice:
|
37 |
+
return choice.text
|
38 |
+
|
39 |
+
# If no response with text is found, return the first response's content (which may be empty)
|
40 |
+
return response.choices[0].message.content
|
41 |
+
|
42 |
+
def extract(input):
|
43 |
+
|
44 |
+
message_log = [{"role": "system", "content": input}]
|
45 |
+
user_input = f"Generate more questions from the following question: {input}. Give two more questions only. The questions are intended for knowledgeable doctors"
|
46 |
+
message_log.append({"role": "user", "content": user_input})
|
47 |
+
response = send_message(message_log)
|
48 |
+
message_log.append({"role": "assistant", "content": response})
|
49 |
+
text = str(response)
|
50 |
+
print(response)
|
51 |
+
return response
|
52 |
+
|
53 |
+
input2 = question
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
my_string = question
|
58 |
+
output = extract(input2)
|
59 |
+
output_list = output.split("\n")
|
60 |
+
final_list = [my_string] + output_list
|
61 |
+
print(final_list)
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
return final_list
|
67 |
+
|
68 |
+
def consolidated_answer(question, oginput):
|
69 |
+
def send_message(message_log):
|
70 |
+
# Use OpenAI's ChatCompletion API to get the chatbot's response
|
71 |
+
response = openai.ChatCompletion.create(
|
72 |
+
model="gpt-3.5-turbo", # The name of the OpenAI chatbot model to use
|
73 |
+
messages=message_log, # The conversation history up to this point, as a list of dictionaries
|
74 |
+
max_tokens=1000, # The maximum number of tokens (words or subwords) in the generated response
|
75 |
+
stop=None, # The stopping sequence for the generated response, if any (not used here)
|
76 |
+
temperature=0.7, # The "creativity" of the generated response (higher temperature = more creative)
|
77 |
+
)
|
78 |
+
|
79 |
+
# Find the first response from the chatbot that has text in it (some responses may not have text)
|
80 |
+
for choice in response.choices:
|
81 |
+
if "text" in choice:
|
82 |
+
return choice.text
|
83 |
+
|
84 |
+
# If no response with text is found, return the first response's content (which may be empty)
|
85 |
+
return response.choices[0].message.content
|
86 |
+
|
87 |
+
def extract(input):
|
88 |
+
|
89 |
+
message_log = [{"role": "system", "content": input}]
|
90 |
+
user_input = f"Give a consolidated answer from this: {input}. It should answer the original question {oginput}. The answer is for knowledgeable doctors so use medical terms."
|
91 |
+
message_log.append({"role": "user", "content": user_input})
|
92 |
+
response = send_message(message_log)
|
93 |
+
message_log.append({"role": "assistant", "content": response})
|
94 |
+
text = str(response)
|
95 |
+
print(response)
|
96 |
+
return response
|
97 |
+
|
98 |
+
input2 = question
|
99 |
+
|
100 |
+
|
101 |
+
output = extract(input2)
|
102 |
+
|
103 |
+
print(output)
|
104 |
+
return output
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
|
109 |
def querying_db(query: str):
|
110 |
+
response = index.query(query, response_mode="default")
|
111 |
+
source = response.get_formatted_sources()
|
112 |
+
return response, source
|
113 |
+
|
114 |
|
115 |
tools = [
|
116 |
+
Tool(
|
117 |
+
name="QueryingDB",
|
118 |
+
func=querying_db,
|
119 |
+
description="useful for when you need to answer questions from the database. The answer is for knowledgeable doctors",
|
120 |
+
return_direct=True
|
121 |
+
)
|
122 |
]
|
123 |
|
124 |
+
prefix = "Give a detailed answer to the question"
|
125 |
+
suffix = """Give answer intended for knowledgeable doctors
|
126 |
|
127 |
Question: {input}
|
128 |
{agent_scratchpad}"""
|
129 |
|
130 |
prompt = ZeroShotAgent.create_prompt(
|
131 |
+
tools,
|
132 |
+
prefix=prefix,
|
133 |
+
suffix=suffix,
|
134 |
+
input_variables=["input", "agent_scratchpad"]
|
135 |
)
|
136 |
|
137 |
+
llm_chain = LLMChain(llm=OpenAI(temperature=0.5), prompt=prompt)
|
138 |
tool_names = [tool.name for tool in tools]
|
139 |
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
|
140 |
|
141 |
|
142 |
def get_answer(query_string):
|
143 |
+
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
144 |
+
response, source = agent_executor.run(query_string)
|
145 |
+
result = f"{response}"
|
146 |
+
return result
|
147 |
+
|
148 |
+
|
149 |
+
def get_answer2(list_thing):
|
150 |
+
answers = []
|
151 |
+
for question in list_thing:
|
152 |
+
answer = get_answer(question)
|
153 |
+
answers.append(answer)
|
154 |
+
|
155 |
+
response2 = "\n".join(answers)
|
156 |
+
return response2
|
157 |
+
|
158 |
+
|
159 |
|
|
|
160 |
|
161 |
|
162 |
def qa_app(query):
|
163 |
+
question_list = generate_variations(query)
|
164 |
+
big_answer = get_answer2(question_list)
|
165 |
+
final_answer = consolidated_answer(big_answer, query)
|
166 |
+
return final_answer
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
|
171 |
|
172 |
inputs = gr.inputs.Textbox(label="Enter your question:")
|