diff --git a/searx/plugins/chat.py b/searx/plugins/chat.py index 2e3507323..e1cca941d 100644 --- a/searx/plugins/chat.py +++ b/searx/plugins/chat.py @@ -1,4 +1,6 @@ from searx.search import SearchWithPlugins +from pathlib import Path +from gpt4all import GPT4All name = "Chat Plugin" @@ -11,5 +13,29 @@ def post_search(request, search: SearchWithPlugins) -> None: search_request = search.search_query container = search.result_container # container.infoboxes.append(container.infoboxes[0]) - container.chat_box = [{'chat_box': 'hello world'}] - container.chat_box[0]['content'] = 'some string that relates to your search query here' + f'\n{search_request.query}' + container.chat_box = {'chat_box': 'GPT4All'} + container.chat_box['content'] = 'Generating response to query: ' + f'\n{search_request.query}' + +def generate_chat_content(query): + model = GPT4All(model_name='gpt4all-falcon-q4_0.gguf', + model_path=(Path.cwd() / 'searx' / 'plugins'), + allow_download=False) + + system_template = """ + ### System Instructions: + 1. Provide concise and directly relevant answers to the specific query in HTML format, emulating the style of an info box on a search engine. + 2. Only use appropriate HTML tags (e.g., `
`, `
{{ chat_box.content | safe }}
\ No newline at end of file diff --git a/searx/webapp.py b/searx/webapp.py index 7707e3ce2..00a7ad955 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -96,7 +96,7 @@ from searx.utils import ( from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH from searx.query import RawTextQuery from searx.plugins import Plugin, plugins, initialize as plugin_initialize -import searx.plugins.chat +import searx.plugins.chat as chat from searx.plugins.oa_doi_rewrite import get_doi_resolver from searx.preferences import ( Preferences, @@ -664,7 +664,7 @@ def search(): search_query, raw_text_query, _, _, selected_locale = get_search_query_from_webapp( request.preferences, request.form ) - if searx.plugins.chat in request.user_plugins: + if chat in request.user_plugins: extremely_bad_global_variable_search_query = raw_text_query.getQuery() search = SearchWithPlugins(search_query, request.user_plugins, request) # pylint: disable=redefined-outer-name result_container = search.search() @@ -1309,43 +1309,13 @@ def config(): } ) -@app.route('/get-chat-content') -def get_chat_content(): - global extremely_bad_global_variable_search_query - # Retrieve chat content from wherever it's stored - # temp - chat_content = None - # retrieve from searx.plugins.chat - if extremely_bad_global_variable_search_query: - # do task - chat_content = {'chat_box': 'hello world', 'content': 'Not Generated Response Yet'} - model = GPT4All(model_name='gpt4all-falcon-q4_0.gguf', - model_path=(Path.cwd() / 'searx' / 'plugins'), - allow_download=False) - - system_template = """ -### System Instructions: -1. Provide concise, accurate, and directly relevant answers to queries. -2. Emulate the style of a Google Answer Box, which includes straightforward and authoritative responses. -3. Where appropriate, use bullet points or structured formatting to clearly present information. -4. Avoid unnecessary elaborations and focus on the key points of the query. -5. Ensure the information is up-to-date and factually correct. -""" - - prompt_template = """ -### Query: -{0} - -### Expected Response: -""" - with model.chat_session(system_template, prompt_template): - response = model.generate(extremely_bad_global_variable_search_query) - chat_content['content'] = str(response) - - extremely_bad_global_variable_search_query = None - - - return jsonify(chat_content) +@app.route('/generate-chat-content', methods=['POST']) +def generate_chat_content_endpoint(): + if request.json is None: + return jsonify({'content': ''}) + query = request.json.get('query') + chat_content = chat.generate_chat_content(query) + return jsonify({'chat_box': 'GPT4ALL', 'content': chat_content}) @app.errorhandler(404)