Update app.py
Browse files
app.py
CHANGED
@@ -23,8 +23,6 @@ MODELS = [
|
|
23 |
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
24 |
"mistralai/Mistral-Nemo-Instruct-2407",
|
25 |
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
26 |
-
"meta-llama/Meta-Llama-3.1-70B-Instruct",
|
27 |
-
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
28 |
"meta-llama/Meta-Llama-3.1-70B-Instruct"
|
29 |
]
|
30 |
|
@@ -36,6 +34,12 @@ MODEL_TOKEN_LIMITS = {
|
|
36 |
"meta-llama/Meta-Llama-3.1-70B-Instruct": 8192,
|
37 |
}
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def get_embeddings():
|
40 |
return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")
|
41 |
|
@@ -50,14 +54,14 @@ class CitingSources(BaseModel):
|
|
50 |
description="List of sources to cite. Should be an URL of the source."
|
51 |
)
|
52 |
|
53 |
-
def chatbot_interface(message, history, model, temperature, num_calls):
|
54 |
if not message.strip():
|
55 |
return "", history
|
56 |
|
57 |
history = history + [(message, "")]
|
58 |
|
59 |
try:
|
60 |
-
for response in respond(message, history, model, temperature, num_calls):
|
61 |
history[-1] = (message, response)
|
62 |
yield history
|
63 |
except gr.CancelledError:
|
@@ -67,21 +71,22 @@ def chatbot_interface(message, history, model, temperature, num_calls):
|
|
67 |
history[-1] = (message, f"An unexpected error occurred: {str(e)}")
|
68 |
yield history
|
69 |
|
70 |
-
def retry_last_response(history, model, temperature, num_calls):
|
71 |
if not history:
|
72 |
return history
|
73 |
|
74 |
last_user_msg = history[-1][0]
|
75 |
history = history[:-1] # Remove the last response
|
76 |
|
77 |
-
return chatbot_interface(last_user_msg, history, model, temperature, num_calls)
|
78 |
|
79 |
-
def respond(message, history, model, temperature, num_calls):
|
80 |
logging.info(f"User Query: {message}")
|
81 |
logging.info(f"Model Used: {model}")
|
|
|
82 |
|
83 |
try:
|
84 |
-
for main_content, sources in get_response_with_search(message, model, num_calls
|
85 |
response = f"{main_content}\n\n{sources}"
|
86 |
first_line = response.split('\n')[0] if response else ''
|
87 |
yield response
|
@@ -100,21 +105,20 @@ def create_web_search_vectors(search_results):
|
|
100 |
|
101 |
return FAISS.from_documents(documents, embed)
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
def get_response_with_search(query, model, num_calls=3, temperature=0.2):
|
106 |
search_results = duckduckgo_search(query)
|
107 |
-
web_search_database = create_web_search_vectors(search_results)
|
108 |
-
|
109 |
-
if not web_search_database:
|
110 |
-
yield "No web search results available. Please try again.", ""
|
111 |
-
return
|
112 |
-
|
113 |
-
retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
|
114 |
-
relevant_docs = retriever.get_relevant_documents(query)
|
115 |
-
|
116 |
-
context = "\n".join([doc.page_content for doc in relevant_docs])
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
prompt = f"""Using the following context from web search results:
|
119 |
{context}
|
120 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
@@ -136,7 +140,10 @@ After writing the document, please provide a list of sources used in your respon
|
|
136 |
for i in range(num_calls):
|
137 |
try:
|
138 |
response = client.chat_completion(
|
139 |
-
messages=[
|
|
|
|
|
|
|
140 |
max_tokens=max_new_tokens,
|
141 |
temperature=temperature,
|
142 |
stream=False,
|
@@ -181,55 +188,71 @@ def initial_conversation():
|
|
181 |
return [
|
182 |
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
183 |
"1. Ask me any question, and I'll search the web for information.\n"
|
184 |
-
"2. You can adjust the model, temperature,
|
185 |
-
"3.
|
|
|
186 |
"To get started, ask me a question!")
|
187 |
]
|
188 |
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
border_color_primary_dark="#1b0f0f",
|
212 |
-
background_fill_secondary_dark="#0c0505",
|
213 |
-
color_accent_soft_dark="transparent",
|
214 |
-
code_background_fill_dark="#140b0b"
|
215 |
-
),
|
216 |
-
css=css,
|
217 |
-
examples=[
|
218 |
-
["What are the latest developments in artificial intelligence?"],
|
219 |
-
["Can you explain the basics of quantum computing?"],
|
220 |
-
["What are the current global economic trends?"]
|
221 |
-
],
|
222 |
-
cache_examples=False,
|
223 |
-
analytics_enabled=False,
|
224 |
-
textbox=gr.Textbox(placeholder="Ask a question", container=False, scale=7),
|
225 |
-
chatbot = gr.Chatbot(
|
226 |
show_copy_button=True,
|
227 |
likeable=True,
|
228 |
layout="bubble",
|
229 |
height=400,
|
230 |
value=initial_conversation()
|
231 |
)
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
if __name__ == "__main__":
|
235 |
demo.launch(share=True)
|
|
|
23 |
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
24 |
"mistralai/Mistral-Nemo-Instruct-2407",
|
25 |
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
|
|
|
|
26 |
"meta-llama/Meta-Llama-3.1-70B-Instruct"
|
27 |
]
|
28 |
|
|
|
34 |
"meta-llama/Meta-Llama-3.1-70B-Instruct": 8192,
|
35 |
}
|
36 |
|
37 |
+
DEFAULT_SYSTEM_PROMPT = """You are a world-class financial AI assistant, capable of complex reasoning and reflection.
|
38 |
+
Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags.
|
39 |
+
Providing comprehensive and accurate information based on web search results is essential.
|
40 |
+
Your goal is to synthesize the given context into a coherent and detailed response that directly addresses the user's query.
|
41 |
+
Please ensure that your response is well-structured, factual."""
|
42 |
+
|
43 |
def get_embeddings():
|
44 |
return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")
|
45 |
|
|
|
54 |
description="List of sources to cite. Should be an URL of the source."
|
55 |
)
|
56 |
|
57 |
+
def chatbot_interface(message, history, model, temperature, num_calls, use_embeddings, system_prompt):
|
58 |
if not message.strip():
|
59 |
return "", history
|
60 |
|
61 |
history = history + [(message, "")]
|
62 |
|
63 |
try:
|
64 |
+
for response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):
|
65 |
history[-1] = (message, response)
|
66 |
yield history
|
67 |
except gr.CancelledError:
|
|
|
71 |
history[-1] = (message, f"An unexpected error occurred: {str(e)}")
|
72 |
yield history
|
73 |
|
74 |
+
def retry_last_response(history, model, temperature, num_calls, use_embeddings, system_prompt):
|
75 |
if not history:
|
76 |
return history
|
77 |
|
78 |
last_user_msg = history[-1][0]
|
79 |
history = history[:-1] # Remove the last response
|
80 |
|
81 |
+
return chatbot_interface(last_user_msg, history, model, temperature, num_calls, use_embeddings, system_prompt)
|
82 |
|
83 |
+
def respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):
|
84 |
logging.info(f"User Query: {message}")
|
85 |
logging.info(f"Model Used: {model}")
|
86 |
+
logging.info(f"Use Embeddings: {use_embeddings}")
|
87 |
|
88 |
try:
|
89 |
+
for main_content, sources in get_response_with_search(message, model, num_calls, temperature, use_embeddings, system_prompt):
|
90 |
response = f"{main_content}\n\n{sources}"
|
91 |
first_line = response.split('\n')[0] if response else ''
|
92 |
yield response
|
|
|
105 |
|
106 |
return FAISS.from_documents(documents, embed)
|
107 |
|
108 |
+
def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
|
|
|
|
109 |
search_results = duckduckgo_search(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
+
if use_embeddings:
|
112 |
+
web_search_database = create_web_search_vectors(search_results)
|
113 |
+
if not web_search_database:
|
114 |
+
yield "No web search results available. Please try again.", ""
|
115 |
+
return
|
116 |
+
retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
|
117 |
+
relevant_docs = retriever.get_relevant_documents(query)
|
118 |
+
context = "\n".join([doc.page_content for doc in relevant_docs])
|
119 |
+
else:
|
120 |
+
context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])
|
121 |
+
|
122 |
prompt = f"""Using the following context from web search results:
|
123 |
{context}
|
124 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
|
|
140 |
for i in range(num_calls):
|
141 |
try:
|
142 |
response = client.chat_completion(
|
143 |
+
messages=[
|
144 |
+
{"role": "system", "content": system_prompt},
|
145 |
+
{"role": "user", "content": prompt}
|
146 |
+
],
|
147 |
max_tokens=max_new_tokens,
|
148 |
temperature=temperature,
|
149 |
stream=False,
|
|
|
188 |
return [
|
189 |
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
190 |
"1. Ask me any question, and I'll search the web for information.\n"
|
191 |
+
"2. You can adjust the model, temperature, number of API calls, and whether to use embeddings for fine-tuned responses.\n"
|
192 |
+
"3. You can also customize the system prompt to guide my behavior.\n"
|
193 |
+
"4. For any queries, feel free to reach out @[email protected] or discord - shreyas094\n\n"
|
194 |
"To get started, ask me a question!")
|
195 |
]
|
196 |
|
197 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft(
|
198 |
+
primary_hue="orange",
|
199 |
+
secondary_hue="amber",
|
200 |
+
neutral_hue="gray",
|
201 |
+
font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
|
202 |
+
).set(
|
203 |
+
body_background_fill_dark="#0c0505",
|
204 |
+
block_background_fill_dark="#0c0505",
|
205 |
+
block_border_width="1px",
|
206 |
+
block_title_background_fill_dark="#1b0f0f",
|
207 |
+
input_background_fill_dark="#140b0b",
|
208 |
+
button_secondary_background_fill_dark="#140b0b",
|
209 |
+
border_color_accent_dark="#1b0f0f",
|
210 |
+
border_color_primary_dark="#1b0f0f",
|
211 |
+
background_fill_secondary_dark="#0c0505",
|
212 |
+
color_accent_soft_dark="transparent",
|
213 |
+
code_background_fill_dark="#140b0b"
|
214 |
+
)) as demo:
|
215 |
+
gr.Markdown("# AI-powered Web Search Assistant")
|
216 |
+
gr.Markdown("Ask questions and get answers from web search results.")
|
217 |
+
|
218 |
+
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
show_copy_button=True,
|
220 |
likeable=True,
|
221 |
layout="bubble",
|
222 |
height=400,
|
223 |
value=initial_conversation()
|
224 |
)
|
225 |
+
|
226 |
+
with gr.Row():
|
227 |
+
msg = gr.Textbox(placeholder="Ask a question", container=False, scale=7)
|
228 |
+
submit = gr.Button("Submit")
|
229 |
+
|
230 |
+
with gr.Accordion("Advanced Options", open=False):
|
231 |
+
model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2])
|
232 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")
|
233 |
+
num_calls = gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")
|
234 |
+
use_embeddings = gr.Checkbox(label="Use Embeddings", value=True)
|
235 |
+
system_prompt = gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, lines=5)
|
236 |
+
|
237 |
+
clear = gr.Button("Clear")
|
238 |
+
retry = gr.Button("Retry Last Response")
|
239 |
+
|
240 |
+
# Set up event handlers
|
241 |
+
submit.click(chatbot_interface, [msg, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)
|
242 |
+
msg.submit(chatbot_interface, [msg, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)
|
243 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
244 |
+
retry.click(retry_last_response, [chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)
|
245 |
+
|
246 |
+
chatbot.like(vote, None, None)
|
247 |
+
|
248 |
+
gr.Examples(
|
249 |
+
examples=[
|
250 |
+
["What are the latest developments in artificial intelligence?"],
|
251 |
+
["Can you explain the basics of quantum computing?"],
|
252 |
+
["What are the current global economic trends?"]
|
253 |
+
],
|
254 |
+
inputs=msg
|
255 |
+
)
|
256 |
|
257 |
if __name__ == "__main__":
|
258 |
demo.launch(share=True)
|