summaryrefslogtreecommitdiff
path: root/searx/engines/cloudflareai.py
blob: fc1051efb0216567ac67bd9499c39f9dbe82b4c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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