返回提交历史
Modified
g4f/Provider/DeepInfra.py
+48
-40
XFEstudio/gpt4free
Fix DeepInfra Provider
ae46cf72
代码差异
1 个文件
+48
-40
@@ -1,30 +1,34 @@
1
1
from __future__ import annotations
2
2
3
import requests, json
4
from ..typing import CreateResult, Messages
5
from .base_provider import AbstractProvider
3
import json
4
from ..typing import AsyncResult, Messages
5
from .base_provider import AsyncGeneratorProvider
6
from ..requests import StreamSession
6
7
7
class DeepInfra(AbstractProvider):
8
url: str = "https://deepinfra.com"
9
working: bool = True
10
supports_stream: bool = True
11
supports_message_history: bool = True
8
class DeepInfra(AsyncGeneratorProvider):
9
url = "https://deepinfra.com"
10
working = True
11
supports_stream = True
12
supports_message_history = True
12
13
13
14
@staticmethod
14
def create_completion(model: str,
15
messages: Messages,
16
stream: bool,
17
auth: str = None,
18
**kwargs) -> CreateResult:
15
async def create_async_generator(
16
model: str,
17
messages: Messages,
18
stream: bool,
19
proxy: str = None,
20
timeout: int = 120,
21
auth: str = None,
22
**kwargs
23
) -> AsyncResult:
19
24
if not model:
20
25
model = 'meta-llama/Llama-2-70b-chat-hf'
21
26
headers = {
22
'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',
23
'Cache-Control': 'no-cache',
27
'Accept-Encoding': 'gzip, deflate, br',
28
'Accept-Language': 'en-US',
24
29
'Connection': 'keep-alive',
25
30
'Content-Type': 'application/json',
26
31
'Origin': 'https://deepinfra.com',
27
'Pragma': 'no-cache',
28
32
'Referer': 'https://deepinfra.com/',
29
33
'Sec-Fetch-Dest': 'empty',
30
34
'Sec-Fetch-Mode': 'cors',
@@ -38,28 +42,32 @@ class DeepInfra(AbstractProvider):
38
42
}
39
43
if auth:
40
44
headers['Authorization'] = f"bearer {auth}"
41
42
json_data = json.dumps({
43
'model' : model,
44
'messages': messages,
45
'stream' : True}, separators=(',', ':'))
46
47
response = requests.post('https://api.deepinfra.com/v1/openai/chat/completions',
48
headers=headers, data=json_data, stream=True)
49
50
response.raise_for_status()
51
first = True
52
for line in response.content:
53
if line.startswith(b"data: [DONE]"):
54
break
55
elif line.startswith(b"data: "):
56
try:
57
chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
58
except Exception:
59
raise RuntimeError(f"Response: {line}")
60
if chunk:
61
if first:
62
chunk = chunk.lstrip()
45
46
async with StreamSession(headers=headers,
47
timeout=timeout,
48
proxies={"https": proxy},
49
impersonate="chrome110"
50
) as session:
51
json_data = {
52
'model' : model,
53
'messages': messages,
54
'stream' : True
55
}
56
async with session.post('https://api.deepinfra.com/v1/openai/chat/completions',
57
json=json_data) as response:
58
response.raise_for_status()
59
first = True
60
async for line in response.iter_lines():
61
try:
62
if line.startswith(b"data: [DONE]"):
63
break
64
elif line.startswith(b"data: "):
65
chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
63
66
if chunk:
64
first = False
65
yield chunk
67
if first:
68
chunk = chunk.lstrip()
69
if chunk:
70
first = False
71
yield chunk
72
except Exception:
73
raise RuntimeError(f"Response: {line}")