Update script.py

This commit is contained in:
catalpaaa 2023-04-20 17:26:11 -07:00
parent 8ccb213d27
commit 4373f5466d

View File

@ -13,7 +13,7 @@ import torch
torch._C._jit_set_profiling_mode(False) torch._C._jit_set_profiling_mode(False)
params = { # These can all be set in the settings.yml file searx_integration_params = { # These can all be set in the settings.yml file
'enable_search': True, 'enable_search': True,
'include_first_result_content': True, 'include_first_result_content': True,
'include_result_summary': False, 'include_result_summary': False,
@ -45,9 +45,9 @@ def url_to_text(url):
for tag in soup.find_all(html_element_blacklist): for tag in soup.find_all(html_element_blacklist):
tag.decompose() tag.decompose()
text = soup.get_text(strip=True) text = soup.get_text(strip=True)
for string in params['removal_list']: for string in searx_integration_params['removal_list']:
text.replace(string, " ") text.replace(string, " ")
return text[0:params['max_characters_per_page']] return text[0:searx_integration_params['max_characters_per_page']]
def get_search_term(string): # This checks if you say "search ... about" something, and if the "Activate Searx integration" checkbox is ticked will search about that def get_search_term(string): # This checks if you say "search ... about" something, and if the "Activate Searx integration" checkbox is ticked will search about that
@ -64,8 +64,8 @@ def get_search_term(string): # This checks if you say "search ... about" somethi
def search_string(search_term): # This is the main logic that sends the API request to Searx and returns the text to add to the context def search_string(search_term): # This is the main logic that sends the API request to Searx and returns the text to add to the context
print("Searching about" + search_term + "...") print("Searching about" + search_term + "...")
query = f"{search_term} {params['extra_query_information']}" query = f"{search_term} {searx_integration_params['extra_query_information']}"
r = requests.get(params['searx_instance'], params={'q': query,'format': 'json','pageno': '1'}) r = requests.get(searx_integration_params['searx_instance'], params={'q': query,'format': 'json','pageno': '1'})
try: try:
searchdata = r.json() searchdata = r.json()
searchdata = searchdata['results'] searchdata = searchdata['results']
@ -73,22 +73,22 @@ def search_string(search_term): # This is the main logic that sends the API requ
new_context = "Tell me that you could not find the results I asked for." new_context = "Tell me that you could not find the results I asked for."
else: else:
new_context = f"This is new information from after your knowledge cutoff date about {search_term} :\n" new_context = f"This is new information from after your knowledge cutoff date about {search_term} :\n"
if params['include_first_result_content']: if searx_integration_params['include_first_result_content']:
for i in range(params['number_of_results']): for i in range(searx_integration_params['number_of_results']):
new_context += url_to_text(searchdata[i]['url']) + "\n" new_context += url_to_text(searchdata[i]['url']) + "\n"
if params['include_result_summary']: if searx_integration_params['include_result_summary']:
for result in searchdata: for result in searchdata:
if 'content' in result: if 'content' in result:
new_context += result['content'] + "\n" new_context += result['content'] + "\n"
new_context = new_context[0:params['max_total_characters']] new_context = new_context[0:searx_integration_params['max_total_characters']]
finally: finally:
if params['console_log']: if searx_integration_params['console_log']:
print(new_context) print(new_context)
return new_context return new_context
def input_modifier(string): def input_modifier(string):
if params['enable_search'] and params['searx_instance']: if searx_integration_params['enable_search'] and searx_integration_params['searx_instance']:
if get_search_term(string): if get_search_term(string):
search_result = search_string(string) search_result = search_string(string)
if search_result == "Tell me that you could not find the results I asked for.": # If it failed to get a result, ask the LLM to tell user it did if search_result == "Tell me that you could not find the results I asked for.": # If it failed to get a result, ask the LLM to tell user it did
@ -102,23 +102,23 @@ def ui():
with gr.Accordion("Searx Integration", open=True): with gr.Accordion("Searx Integration", open=True):
with gr.Row(): with gr.Row():
with gr.Column(): with gr.Column():
enable_search = gr.Checkbox(value=params['enable_search'], label='Activate Searx integration') enable_search = gr.Checkbox(value=searx_integration_params['enable_search'], label='Activate Searx integration')
console_log = gr.Checkbox(value=params['console_log'], label='Display search results on console') console_log = gr.Checkbox(value=searx_integration_params['console_log'], label='Display search results on console')
include_first_result_content = gr.Checkbox(value=params['include_first_result_content'], label='Include content from the first result') include_first_result_content = gr.Checkbox(value=searx_integration_params['include_first_result_content'], label='Include content from the first result')
number_of_results = gr.Slider(1,10,value=params['number_of_results'],step=1,label='Number of results to fetch') number_of_results = gr.Slider(1,10,value=searx_integration_params['number_of_results'],step=1,label='Number of results to fetch')
with gr.Column(): with gr.Column():
searx_instance = gr.Textbox(placeholder=params['searx_instance'], value=params['searx_instance'], label='Searx instance address') searx_instance = gr.Textbox(placeholder=searx_integration_params['searx_instance'], value=searx_integration_params['searx_instance'], label='Searx instance address')
extra_query_information = gr.Textbox(placeholder=params['extra_query_information'], value=params['extra_query_information'], label='Extra info to pass in Searx query') extra_query_information = gr.Textbox(placeholder=searx_integration_params['extra_query_information'], value=searx_integration_params['extra_query_information'], label='Extra info to pass in Searx query')
include_result_summary = gr.Checkbox(value=params['include_result_summary'], label='Include summary from each search result') include_result_summary = gr.Checkbox(value=searx_integration_params['include_result_summary'], label='Include summary from each search result')
max_characters_per_page = gr.Slider(256,16384,value=params['max_characters_per_page'],step=64,label='Maximum characters per fetched pages') max_characters_per_page = gr.Slider(256,16384,value=searx_integration_params['max_characters_per_page'],step=64,label='Maximum characters per fetched pages')
max_total_characters = gr.Slider(256,16384,value=params['max_total_characters'],step=64,label='Total max characters') max_total_characters = gr.Slider(256,16384,value=searx_integration_params['max_total_characters'],step=64,label='Total max characters')
enable_search.change(lambda x: params.update({"enable_search": x}), enable_search, None) enable_search.change(lambda x: searx_integration_params.update({"enable_search": x}), enable_search, None)
console_log.change(lambda x: params.update({"console_display": x}), console_log, None) console_log.change(lambda x: searx_integration_params.update({"console_display": x}), console_log, None)
include_first_result_content.change(lambda x: params.update({"include_first_result_content": x}), include_first_result_content, None) include_first_result_content.change(lambda x: searx_integration_params.update({"include_first_result_content": x}), include_first_result_content, None)
include_result_summary.change(lambda x: params.update({"include_result_summary": x}), include_result_summary, None) include_result_summary.change(lambda x: searx_integration_params.update({"include_result_summary": x}), include_result_summary, None)
number_of_results.change(lambda x: params.update({"number_of_results": x}), number_of_results, None) number_of_results.change(lambda x: searx_integration_params.update({"number_of_results": x}), number_of_results, None)
max_characters_per_page.change(lambda x: params.update({"max_characters_per_page": x}), max_characters_per_page, None) max_characters_per_page.change(lambda x: searx_integration_params.update({"max_characters_per_page": x}), max_characters_per_page, None)
searx_instance.change(lambda x: params.update({"searx_instance": x}), searx_instance, None) searx_instance.change(lambda x: searx_integration_params.update({"searx_instance": x}), searx_instance, None)
extra_query_information.change(lambda x: params.update({"extra_query_information": x}), extra_query_information, None) extra_query_information.change(lambda x: searx_integration_params.update({"extra_query_information": x}), extra_query_information, None)
max_total_characters.change(lambda x: params.update({"max_total_characters": x}), max_total_characters, None) max_total_characters.change(lambda x: searx_integration_params.update({"max_total_characters": x}), max_total_characters, None)