Update app.py
Browse files
app.py
CHANGED
|
@@ -23,6 +23,8 @@ 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 |
]
|
| 28 |
|
|
@@ -54,39 +56,40 @@ class CitingSources(BaseModel):
|
|
| 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):
|
| 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):
|
| 65 |
history[-1] = (message, response)
|
| 66 |
yield history
|
| 67 |
-
except gr.
|
| 68 |
yield history
|
| 69 |
except Exception as e:
|
| 70 |
logging.error(f"Unexpected error in chatbot_interface: {str(e)}")
|
| 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):
|
| 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)
|
| 82 |
|
| 83 |
-
def respond(message, history, model, temperature, num_calls, use_embeddings):
|
| 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=num_calls, temperature=temperature, use_embeddings=use_embeddings):
|
| 90 |
response = f"{main_content}\n\n{sources}"
|
| 91 |
first_line = response.split('\n')[0] if response else ''
|
| 92 |
yield response
|
|
@@ -105,7 +108,7 @@ def create_web_search_vectors(search_results):
|
|
| 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):
|
| 109 |
search_results = duckduckgo_search(query)
|
| 110 |
|
| 111 |
if use_embeddings:
|
|
@@ -144,7 +147,7 @@ After writing the document, please provide a list of sources used in your respon
|
|
| 144 |
try:
|
| 145 |
response = client.chat_completion(
|
| 146 |
messages=[
|
| 147 |
-
{"role": "system", "content":
|
| 148 |
{"role": "user", "content": prompt}
|
| 149 |
],
|
| 150 |
max_tokens=max_new_tokens,
|
|
@@ -191,19 +194,19 @@ def initial_conversation():
|
|
| 191 |
return [
|
| 192 |
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
| 193 |
"1. Ask me any question, and I'll search the web for information.\n"
|
| 194 |
-
"2. You can adjust the model, temperature, number of API calls,
|
| 195 |
"3. For any queries, feel free to reach out @[email protected] or discord - shreyas094\n\n"
|
| 196 |
"To get started, ask me a question!")
|
| 197 |
]
|
| 198 |
|
| 199 |
demo = gr.ChatInterface(
|
| 200 |
respond,
|
| 201 |
-
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),
|
| 202 |
additional_inputs=[
|
| 203 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2]),
|
| 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=True),
|
|
|
|
| 207 |
],
|
| 208 |
title="AI-powered Web Search Assistant",
|
| 209 |
description="Ask questions and get answers from web search results.",
|
|
|
|
| 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 |
|
|
|
|
| 56 |
description="List of sources to cite. Should be an URL of the source."
|
| 57 |
)
|
| 58 |
|
| 59 |
+
def chatbot_interface(message, history, model, temperature, num_calls, use_embeddings, system_prompt):
|
| 60 |
if not message.strip():
|
| 61 |
return "", history
|
| 62 |
|
| 63 |
history = history + [(message, "")]
|
| 64 |
|
| 65 |
try:
|
| 66 |
+
for response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):
|
| 67 |
history[-1] = (message, response)
|
| 68 |
yield history
|
| 69 |
+
except gr.CancelledError:
|
| 70 |
yield history
|
| 71 |
except Exception as e:
|
| 72 |
logging.error(f"Unexpected error in chatbot_interface: {str(e)}")
|
| 73 |
history[-1] = (message, f"An unexpected error occurred: {str(e)}")
|
| 74 |
yield history
|
| 75 |
|
| 76 |
+
def retry_last_response(history, model, temperature, num_calls, use_embeddings, system_prompt):
|
| 77 |
if not history:
|
| 78 |
return history
|
| 79 |
|
| 80 |
last_user_msg = history[-1][0]
|
| 81 |
history = history[:-1] # Remove the last response
|
| 82 |
|
| 83 |
+
return chatbot_interface(last_user_msg, history, model, temperature, num_calls, use_embeddings, system_prompt)
|
| 84 |
|
| 85 |
+
def respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):
|
| 86 |
logging.info(f"User Query: {message}")
|
| 87 |
logging.info(f"Model Used: {model}")
|
| 88 |
logging.info(f"Use Embeddings: {use_embeddings}")
|
| 89 |
+
logging.info(f"System Prompt: {system_prompt}")
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
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):
|
| 93 |
response = f"{main_content}\n\n{sources}"
|
| 94 |
first_line = response.split('\n')[0] if response else ''
|
| 95 |
yield response
|
|
|
|
| 108 |
|
| 109 |
return FAISS.from_documents(documents, embed)
|
| 110 |
|
| 111 |
+
def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
| 112 |
search_results = duckduckgo_search(query)
|
| 113 |
|
| 114 |
if use_embeddings:
|
|
|
|
| 147 |
try:
|
| 148 |
response = client.chat_completion(
|
| 149 |
messages=[
|
| 150 |
+
{"role": "system", "content": system_prompt},
|
| 151 |
{"role": "user", "content": prompt}
|
| 152 |
],
|
| 153 |
max_tokens=max_new_tokens,
|
|
|
|
| 194 |
return [
|
| 195 |
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
| 196 |
"1. Ask me any question, and I'll search the web for information.\n"
|
| 197 |
+
"2. You can adjust the model, temperature, number of API calls, whether to use embeddings, and the system prompt for fine-tuned responses.\n"
|
| 198 |
"3. For any queries, feel free to reach out @[email protected] or discord - shreyas094\n\n"
|
| 199 |
"To get started, ask me a question!")
|
| 200 |
]
|
| 201 |
|
| 202 |
demo = gr.ChatInterface(
|
| 203 |
respond,
|
|
|
|
| 204 |
additional_inputs=[
|
| 205 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2]),
|
| 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=True),
|
| 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.",
|