Update app.py
Browse files
app.py
CHANGED
@@ -54,14 +54,14 @@ class CitingSources(BaseModel):
|
|
54 |
description="List of sources to cite. Should be an URL of the source."
|
55 |
)
|
56 |
|
57 |
-
def chatbot_interface(message,
|
58 |
if not message.strip():
|
59 |
return "", history
|
60 |
|
61 |
history = history + [(message, "")]
|
62 |
|
63 |
try:
|
64 |
-
for response in respond(message,
|
65 |
history[-1] = (message, response)
|
66 |
yield history
|
67 |
except gr.CancelledError:
|
@@ -71,34 +71,29 @@ def chatbot_interface(message, system_prompt, history, model, temperature, num_c
|
|
71 |
history[-1] = (message, f"An unexpected error occurred: {str(e)}")
|
72 |
yield history
|
73 |
|
74 |
-
def retry_last_response(
|
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,
|
82 |
|
83 |
-
def respond(message,
|
84 |
logging.info(f"User Query: {message}")
|
85 |
logging.info(f"Model Used: {model}")
|
86 |
logging.info(f"Use Embeddings: {use_embeddings}")
|
87 |
logging.info(f"System Prompt: {system_prompt}")
|
88 |
|
89 |
try:
|
90 |
-
full_response = ""
|
91 |
for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
if sources:
|
96 |
-
full_response += f"\n\nSources:\n{sources}"
|
97 |
-
yield history + [(message, full_response)]
|
98 |
except Exception as e:
|
99 |
logging.error(f"Error with {model}: {str(e)}")
|
100 |
-
|
101 |
-
yield history + [(message, error_message)]
|
102 |
|
103 |
def create_web_search_vectors(search_results):
|
104 |
embed = get_embeddings()
|
@@ -111,9 +106,7 @@ def create_web_search_vectors(search_results):
|
|
111 |
|
112 |
return FAISS.from_documents(documents, embed)
|
113 |
|
114 |
-
def get_response_with_search(query, model,
|
115 |
-
logging.info(f"get_response_with_search - Query: {query}")
|
116 |
-
logging.info(f"get_response_with_search - System Prompt: {system_prompt}")
|
117 |
search_results = duckduckgo_search(query)
|
118 |
|
119 |
if use_embeddings:
|
@@ -196,14 +189,24 @@ css = """
|
|
196 |
/* Fine-tune chatbox size */
|
197 |
"""
|
198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
demo = gr.ChatInterface(
|
200 |
-
|
|
|
201 |
additional_inputs=[
|
202 |
-
gr.Textbox(label="System Prompt", lines=5, value=DEFAULT_SYSTEM_PROMPT),
|
203 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
204 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
205 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
206 |
gr.Checkbox(label="Use Embeddings", value=False),
|
|
|
207 |
],
|
208 |
title="AI-powered Web Search Assistant",
|
209 |
description="Ask questions and get answers from web search results.",
|
@@ -217,11 +220,12 @@ demo = gr.ChatInterface(
|
|
217 |
cache_examples=False,
|
218 |
analytics_enabled=False,
|
219 |
textbox=gr.Textbox(placeholder="Ask a question", container=False, scale=7),
|
220 |
-
chatbot=gr.Chatbot(
|
221 |
show_copy_button=True,
|
222 |
likeable=True,
|
223 |
layout="bubble",
|
224 |
-
height=400
|
|
|
225 |
)
|
226 |
)
|
227 |
|
|
|
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 |
logging.info(f"System Prompt: {system_prompt}")
|
88 |
|
89 |
try:
|
|
|
90 |
for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):
|
91 |
+
response = f"{main_content}\n\n{sources}"
|
92 |
+
first_line = response.split('\n')[0] if response else ''
|
93 |
+
yield response
|
|
|
|
|
|
|
94 |
except Exception as e:
|
95 |
logging.error(f"Error with {model}: {str(e)}")
|
96 |
+
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
|
|
97 |
|
98 |
def create_web_search_vectors(search_results):
|
99 |
embed = get_embeddings()
|
|
|
106 |
|
107 |
return FAISS.from_documents(documents, embed)
|
108 |
|
109 |
+
def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
|
|
|
|
110 |
search_results = duckduckgo_search(query)
|
111 |
|
112 |
if use_embeddings:
|
|
|
189 |
/* Fine-tune chatbox size */
|
190 |
"""
|
191 |
|
192 |
+
def initial_conversation():
|
193 |
+
return [
|
194 |
+
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
195 |
+
"1. Ask me any question, and I'll search the web for information.\n"
|
196 |
+
"2. You can adjust the model, temperature, number of API calls, whether to use embeddings, and the system prompt for fine-tuned responses.\n"
|
197 |
+
"3. For any queries, feel free to reach out @[email protected] or discord - shreyas094\n\n"
|
198 |
+
"To get started, ask me a question!")
|
199 |
+
]
|
200 |
+
|
201 |
demo = gr.ChatInterface(
|
202 |
+
respond,
|
203 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),
|
204 |
additional_inputs=[
|
|
|
205 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
206 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
207 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
208 |
gr.Checkbox(label="Use Embeddings", value=False),
|
209 |
+
gr.Textbox(label="System Prompt", lines=5, value=DEFAULT_SYSTEM_PROMPT),
|
210 |
],
|
211 |
title="AI-powered Web Search Assistant",
|
212 |
description="Ask questions and get answers from web search results.",
|
|
|
220 |
cache_examples=False,
|
221 |
analytics_enabled=False,
|
222 |
textbox=gr.Textbox(placeholder="Ask a question", container=False, scale=7),
|
223 |
+
chatbot = gr.Chatbot(
|
224 |
show_copy_button=True,
|
225 |
likeable=True,
|
226 |
layout="bubble",
|
227 |
+
height=400,
|
228 |
+
value=initial_conversation()
|
229 |
)
|
230 |
)
|
231 |
|