返回提交历史
Added
g4f/Provider/Cohere.py
+106
-0
Modified
g4f/Provider/__init__.py
+1
-0
Modified
g4f/models.py
+2
-1
XFEstudio/gpt4free
add cohere provider.
8ec94204
代码差异
3 个文件
+109
-1
@@ -0,0 +1,106 @@
1
from __future__ import annotations
2
3
import json, random, requests, threading
4
from aiohttp import ClientSession
5
6
from ..typing import CreateResult, Messages
7
from .base_provider import AbstractProvider
8
from .helper import format_prompt
9
10
class Cohere(AbstractProvider):
11
url = "https://cohereforai-c4ai-command-r-plus.hf.space"
12
working = True
13
supports_gpt_35_turbo = False
14
supports_gpt_4 = False
15
supports_stream = True
16
17
@staticmethod
18
def create_completion(
19
model: str,
20
messages: Messages,
21
stream: bool,
22
proxy: str = None,
23
max_retries: int = 6,
24
**kwargs
25
) -> CreateResult:
26
27
prompt = format_prompt(messages)
28
29
headers = {
30
'accept': 'text/event-stream',
31
'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',
32
'cache-control': 'no-cache',
33
'pragma': 'no-cache',
34
'referer': 'https://cohereforai-c4ai-command-r-plus.hf.space/?__theme=light',
35
'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
36
'sec-ch-ua-mobile': '?0',
37
'sec-ch-ua-platform': '"macOS"',
38
'sec-fetch-dest': 'empty',
39
'sec-fetch-mode': 'cors',
40
'sec-fetch-site': 'same-origin',
41
'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',
42
}
43
44
session_hash = ''.join(random.choices("abcdefghijklmnopqrstuvwxyz0123456789", k=11))
45
46
params = {
47
'fn_index': '1',
48
'session_hash': session_hash,
49
}
50
51
response = requests.get(
52
'https://cohereforai-c4ai-command-r-plus.hf.space/queue/join',
53
params=params,
54
headers=headers,
55
stream=True
56
)
57
58
completion = ''
59
60
for line in response.iter_lines():
61
if line:
62
json_data = json.loads(line[6:])
63
64
if b"send_data" in (line):
65
event_id = json_data["event_id"]
66
67
threading.Thread(target=send_data, args=[session_hash, event_id, prompt]).start()
68
69
if b"process_generating" in line or b"process_completed" in line:
70
token = (json_data['output']['data'][0][0][1])
71
72
yield (token.replace(completion, ""))
73
completion = token
74
75
def send_data(session_hash, event_id, prompt):
76
headers = {
77
'accept': '*/*',
78
'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',
79
'cache-control': 'no-cache',
80
'content-type': 'application/json',
81
'origin': 'https://cohereforai-c4ai-command-r-plus.hf.space',
82
'pragma': 'no-cache',
83
'referer': 'https://cohereforai-c4ai-command-r-plus.hf.space/?__theme=light',
84
'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
85
'sec-ch-ua-mobile': '?0',
86
'sec-ch-ua-platform': '"macOS"',
87
'sec-fetch-dest': 'empty',
88
'sec-fetch-mode': 'cors',
89
'sec-fetch-site': 'same-origin',
90
'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',
91
}
92
93
json_data = {
94
'data': [
95
prompt,
96
'',
97
[],
98
],
99
'event_data': None,
100
'fn_index': 1,
101
'session_hash': session_hash,
102
'event_id': event_id
103
}
104
105
requests.post('https://cohereforai-c4ai-command-r-plus.hf.space/queue/data',
106
json = json_data, headers=headers)
@@ -46,6 +46,7 @@ from .ReplicateImage import ReplicateImage
46
46
from .Vercel import Vercel
47
47
from .WhiteRabbitNeo import WhiteRabbitNeo
48
48
from .You import You
49
from .Cohere import Cohere
49
50
50
51
import sys
51
52
@@ -20,6 +20,7 @@ from .Provider import (
20
20
Vercel,
21
21
Gemini,
22
22
Koala,
23
Cohere,
23
24
Bing,
24
25
You,
25
26
Pi,
@@ -275,7 +276,7 @@ dbrx_instruct = Model(
275
276
command_r_plus = Model(
276
277
name = 'CohereForAI/c4ai-command-r-plus',
277
278
base_provider = 'mistral',
278
best_provider = HuggingChat
279
best_provider = RetryProvider([HuggingChat, Cohere])
279
280
)
280
281
281
282
class ModelUtils: