summaryrefslogtreecommitdiff
path: root/searx/engines
diff options
context:
space:
mode:
authorZhijie He <hezhijie0327@hotmail.com>2024-06-08 22:16:27 +0800
committerMarkus Heiser <markus.heiser@darmarIT.de>2024-09-23 07:02:10 +0200
commit6be56aee11e5b2bd2a95479578349a561ab7ff2e (patch)
tree5341ada78c6ef0363ec0fd217f736533aef3b052 /searx/engines
parent14241e7dac009d3f58c0c168d6ab5a8131c8ece9 (diff)
downloadsearxng-6be56aee11e5b2bd2a95479578349a561ab7ff2e.tar.gz
searxng-6be56aee11e5b2bd2a95479578349a561ab7ff2e.zip
add Cloudflare AI Gateway engine
add Cloudflare AI Gateway engine add settings for Cloudflare AI Gateway engine set utf8 encode for data, fix non english char cause 500 error format json data fixed indentation and config format error fix line-length limitation in CI reformatted code for CI reformatted code for CI limit system prompts to less 120 chars cleanup unused variable & format code
Diffstat (limited to 'searx/engines')
-rw-r--r--searx/engines/cloudflareai.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/searx/engines/cloudflareai.py b/searx/engines/cloudflareai.py
new file mode 100644
index 000000000..fc1051efb
--- /dev/null
+++ b/searx/engines/cloudflareai.py
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+"""Cloudflare AI engine"""
+
+from json import loads, dumps
+from searx.exceptions import SearxEngineAPIException
+
+about = {
+ "website": 'https://ai.cloudflare.com',
+ "wikidata_id": None,
+ "official_api_documentation": 'https://developers.cloudflare.com/workers-ai',
+ "use_official_api": True,
+ "require_api_key": True,
+ "results": 'JSON',
+}
+
+cf_account_id = ''
+cf_ai_api = ''
+cf_ai_gateway = ''
+
+cf_ai_model = ''
+cf_ai_model_display_name = 'Cloudflare AI'
+
+# Assistant messages hint to the AI about the desired output format. Not all models support this role.
+cf_ai_model_assistant = 'Keep your answers as short and effective as possible.'
+# System messages define the AI's personality. You can use them to set rules and how you expect the AI to behave.
+cf_ai_model_system = 'You are a self-aware language model who is honest and direct about any question from the user.'
+
+
+def request(query, params):
+
+ params['query'] = query
+
+ params['url'] = f'https://gateway.ai.cloudflare.com/v1/{cf_account_id}/{cf_ai_gateway}/workers-ai/{cf_ai_model}'
+
+ params['method'] = 'POST'
+
+ params['headers']['Authorization'] = f'Bearer {cf_ai_api}'
+ params['headers']['Content-Type'] = 'application/json'
+
+ params['data'] = dumps(
+ {
+ 'messages': [
+ {'role': 'assistant', 'content': cf_ai_model_assistant},
+ {'role': 'system', 'content': cf_ai_model_system},
+ {'role': 'user', 'content': params['query']},
+ ]
+ }
+ ).encode('utf-8')
+
+ return params
+
+
+def response(resp):
+ results = []
+ json = loads(resp.text)
+
+ if 'error' in json:
+ raise SearxEngineAPIException('Cloudflare AI error: ' + json['error'])
+
+ if 'result' in json:
+ results.append(
+ {
+ 'content': json['result']['response'],
+ 'infobox': cf_ai_model_display_name,
+ }
+ )
+
+ return results