summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Cohere.py
blob: 4f9fd30acd33bd8c80d9259f50e114128945c42a (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from __future__ import annotations

import json, random, requests, threading
from aiohttp import ClientSession

from ..typing import CreateResult, Messages
from .base_provider import AbstractProvider
from .helper import format_prompt

class Cohere(AbstractProvider):
    url                   = "https://cohereforai-c4ai-command-r-plus.hf.space"
    working               = True
    supports_gpt_35_turbo = False
    supports_gpt_4        = False
    supports_stream       = True
    
    @staticmethod
    def create_completion(
        model: str,
        messages: Messages,
        stream: bool,
        proxy: str = None,
        max_retries: int = 6,
        **kwargs
    ) -> CreateResult:
        
        prompt = format_prompt(messages)
        
        headers = {
            'accept': 'text/event-stream',
            'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
            'cache-control': 'no-cache',
            'pragma': 'no-cache',
            'referer': 'https://cohereforai-c4ai-command-r-plus.hf.space/?__theme=light',
            'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"macOS"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
        }
        
        session_hash = ''.join(random.choices("abcdefghijklmnopqrstuvwxyz0123456789", k=11))

        params = {
            'fn_index': '1',
            'session_hash': session_hash,
        }

        response = requests.get(
            'https://cohereforai-c4ai-command-r-plus.hf.space/queue/join',
            params=params,
            headers=headers,
            stream=True
        )
        
        completion = ''

        for line in response.iter_lines():
            if line:
                json_data = json.loads(line[6:])
                
                if b"send_data" in (line):
                    event_id = json_data["event_id"]
                    
                    threading.Thread(target=send_data, args=[session_hash, event_id, prompt]).start()
                
                if b"process_generating" in line or b"process_completed" in line:
                    token = (json_data['output']['data'][0][0][1])
                    
                    yield (token.replace(completion, ""))
                    completion = token

def send_data(session_hash, event_id, prompt):
    headers = {
        'accept': '*/*',
        'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
        'cache-control': 'no-cache',
        'content-type': 'application/json',
        'origin': 'https://cohereforai-c4ai-command-r-plus.hf.space',
        'pragma': 'no-cache',
        'referer': 'https://cohereforai-c4ai-command-r-plus.hf.space/?__theme=light',
        'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"macOS"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
    }

    json_data = {
        'data': [
            prompt,
            '',
            [],
        ],
        'event_data': None,
        'fn_index': 1,
        'session_hash': session_hash,
        'event_id': event_id
    }
    
    requests.post('https://cohereforai-c4ai-command-r-plus.hf.space/queue/data',
                    json = json_data, headers=headers)