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 |
|
@@ -56,30 +54,42 @@ class CitingSources(BaseModel):
|
|
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
|
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
|
67 |
history[-1] = (message, response)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
logging.info(f"User Query: {message}")
|
76 |
logging.info(f"Model Used: {model}")
|
77 |
logging.info(f"Use Embeddings: {use_embeddings}")
|
78 |
|
79 |
try:
|
80 |
-
for main_content, sources in get_response_with_search(message, model, num_calls, temperature, use_embeddings
|
81 |
-
|
82 |
-
|
|
|
83 |
except Exception as e:
|
84 |
logging.error(f"Error with {model}: {str(e)}")
|
85 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
@@ -95,16 +105,19 @@ def create_web_search_vectors(search_results):
|
|
95 |
|
96 |
return FAISS.from_documents(documents, embed)
|
97 |
|
98 |
-
def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True
|
99 |
search_results = duckduckgo_search(query)
|
100 |
|
101 |
if use_embeddings:
|
102 |
web_search_database = create_web_search_vectors(search_results)
|
|
|
103 |
if not web_search_database:
|
104 |
yield "No web search results available. Please try again.", ""
|
105 |
return
|
|
|
106 |
retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
|
107 |
relevant_docs = retriever.get_relevant_documents(query)
|
|
|
108 |
context = "\n".join([doc.page_content for doc in relevant_docs])
|
109 |
else:
|
110 |
context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])
|
@@ -131,7 +144,7 @@ After writing the document, please provide a list of sources used in your respon
|
|
131 |
try:
|
132 |
response = client.chat_completion(
|
133 |
messages=[
|
134 |
-
{"role": "system", "content":
|
135 |
{"role": "user", "content": prompt}
|
136 |
],
|
137 |
max_tokens=max_new_tokens,
|
@@ -179,19 +192,18 @@ def initial_conversation():
|
|
179 |
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
180 |
"1. Ask me any question, and I'll search the web for information.\n"
|
181 |
"2. You can adjust the model, temperature, number of API calls, and whether to use embeddings for fine-tuned responses.\n"
|
182 |
-
"3.
|
183 |
-
"4. For any queries, feel free to reach out @[email protected] or discord - shreyas094\n\n"
|
184 |
"To get started, ask me a question!")
|
185 |
]
|
186 |
|
187 |
demo = gr.ChatInterface(
|
188 |
-
|
|
|
189 |
additional_inputs=[
|
190 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2]),
|
191 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
192 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
193 |
gr.Checkbox(label="Use Embeddings", value=True),
|
194 |
-
gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, lines=5),
|
195 |
],
|
196 |
title="AI-powered Web Search Assistant",
|
197 |
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 |
]
|
28 |
|
|
|
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.CanceledError:
|
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
|
93 |
except Exception as e:
|
94 |
logging.error(f"Error with {model}: {str(e)}")
|
95 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
|
|
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:
|
112 |
web_search_database = create_web_search_vectors(search_results)
|
113 |
+
|
114 |
if not web_search_database:
|
115 |
yield "No web search results available. Please try again.", ""
|
116 |
return
|
117 |
+
|
118 |
retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
|
119 |
relevant_docs = retriever.get_relevant_documents(query)
|
120 |
+
|
121 |
context = "\n".join([doc.page_content for doc in relevant_docs])
|
122 |
else:
|
123 |
context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])
|
|
|
144 |
try:
|
145 |
response = client.chat_completion(
|
146 |
messages=[
|
147 |
+
{"role": "system", "content": DEFAULT_SYSTEM_PROMPT},
|
148 |
{"role": "user", "content": prompt}
|
149 |
],
|
150 |
max_tokens=max_new_tokens,
|
|
|
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, and whether to use embeddings for fine-tuned responses.\n"
|
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.",
|