返回提交历史
Modified
.gitignore
+1
-0
Modified
g4f/Provider/DeepInfra.py
+55
-57
Modified
g4f/Provider/base_provider.py
+1
-2
Added
ptest.py
+57
-0
XFEstudio/gpt4free
~
fix DeepInfra
10a38324
代码差异
4 个文件
+114
-59
@@ -44,5 +44,6 @@ lab/*
44
44
lab
45
45
tstt.py
46
46
providerstest.py
47
prv.py
47
48
# Emacs crap
48
49
*~
@@ -1,64 +1,62 @@
1
1
from __future__ import annotations
2
2
3
import json
4
from aiohttp import ClientSession
3
import requests, json
4
from ..typing import CreateResult, Messages
5
from .base_provider import BaseProvider
5
6
6
from ..typing import AsyncResult, Messages
7
from .base_provider import AsyncGeneratorProvider
7
class DeepInfra(BaseProvider):
8
url: str = "https://deepinfra.com"
9
working: bool = True
10
supports_stream: bool = True
11
supports_message_history: bool = True
8
12
9
10
class DeepInfra(AsyncGeneratorProvider):
11
url = "https://deepinfra.com"
12
supports_message_history = True
13
working = True
14
15
@classmethod
16
async def create_async_generator(
17
cls,
18
model: str,
19
messages: Messages,
20
proxy: str = None,
21
**kwargs
22
) -> AsyncResult:
23
if not model:
24
model = "meta-llama/Llama-2-70b-chat-hf"
13
@staticmethod
14
def create_completion(model: str,
15
messages: Messages,
16
stream: bool,
17
**kwargs) -> CreateResult:
18
25
19
headers = {
26
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
27
"Accept": "text/event-stream",
28
"Accept-Language": "de,en-US;q=0.7,en;q=0.3",
29
"Accept-Encoding": "gzip, deflate, br",
30
"Referer": f"{cls.url}/",
31
"Content-Type": "application/json",
32
"X-Deepinfra-Source": "web-page",
33
"Origin": cls.url,
34
"Connection": "keep-alive",
35
"Sec-Fetch-Dest": "empty",
36
"Sec-Fetch-Mode": "cors",
37
"Sec-Fetch-Site": "same-site",
38
"Pragma": "no-cache",
39
"Cache-Control": "no-cache",
20
'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',
21
'Cache-Control': 'no-cache',
22
'Connection': 'keep-alive',
23
'Content-Type': 'application/json',
24
'Origin': 'https://deepinfra.com',
25
'Pragma': 'no-cache',
26
'Referer': 'https://deepinfra.com/',
27
'Sec-Fetch-Dest': 'empty',
28
'Sec-Fetch-Mode': 'cors',
29
'Sec-Fetch-Site': 'same-site',
30
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
31
'X-Deepinfra-Source': 'web-embed',
32
'accept': 'text/event-stream',
33
'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
34
'sec-ch-ua-mobile': '?0',
35
'sec-ch-ua-platform': '"macOS"',
40
36
}
41
async with ClientSession(headers=headers) as session:
42
data = {
43
"model": model,
44
"messages": messages,
45
"stream": True,
46
}
47
async with session.post(
48
"https://api.deepinfra.com/v1/openai/chat/completions",
49
json=data,
50
proxy=proxy
51
) as response:
52
response.raise_for_status()
53
first = True
54
async for line in response.content:
55
if line.startswith(b"data: [DONE]"):
56
break
57
elif line.startswith(b"data: "):
58
chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
37
38
json_data = json.dumps({
39
'model' : 'meta-llama/Llama-2-70b-chat-hf',
40
'messages': messages,
41
'stream' : True}, separators=(',', ':'))
42
43
response = requests.post('https://api.deepinfra.com/v1/openai/chat/completions',
44
headers=headers, data=json_data, stream=True)
45
46
response.raise_for_status()
47
first = True
48
49
for line in response.iter_content(chunk_size=1024):
50
if line.startswith(b"data: [DONE]"):
51
break
52
53
elif line.startswith(b"data: "):
54
chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
55
56
if chunk:
57
if first:
58
chunk = chunk.lstrip()
59
59
if chunk:
60
if first:
61
chunk = chunk.lstrip()
62
if chunk:
63
first = False
64
yield chunk
60
first = False
61
62
yield (chunk)
@@ -8,7 +8,6 @@ from inspect import signature, Parameter
8
8
from .helper import get_event_loop, get_cookies, format_prompt
9
9
from ..typing import CreateResult, AsyncResult, Messages
10
10
11
12
11
if sys.version_info < (3, 10):
13
12
NoneType = type(None)
14
13
else:
@@ -76,7 +75,7 @@ class BaseProvider(ABC):
76
75
annotation = "None"
77
76
return str(annotation)
78
77
79
args = "";
78
args = ""
80
79
for name, param in sig.parameters.items():
81
80
if name in ("self", "kwargs"):
82
81
continue
@@ -0,0 +1,57 @@
1
import requests, json
2
3
4
headers = {
5
'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',
6
'Cache-Control': 'no-cache',
7
'Connection': 'keep-alive',
8
'Content-Type': 'application/json',
9
'Origin': 'https://deepinfra.com',
10
'Pragma': 'no-cache',
11
'Referer': 'https://deepinfra.com/',
12
'Sec-Fetch-Dest': 'empty',
13
'Sec-Fetch-Mode': 'cors',
14
'Sec-Fetch-Site': 'same-site',
15
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
16
'X-Deepinfra-Source': 'web-embed',
17
'accept': 'text/event-stream',
18
'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
19
'sec-ch-ua-mobile': '?0',
20
'sec-ch-ua-platform': '"macOS"',
21
}
22
23
json_data = json.dumps({
24
'model': 'meta-llama/Llama-2-70b-chat-hf',
25
'messages': [
26
{
27
'role': 'user',
28
'content': 'what is the meaning of life ?',
29
},
30
],
31
'stream': True}, separators=(',', ':'))
32
33
response = requests.post('https://api.deepinfra.com/v1/openai/chat/completions',
34
headers=headers, data=json_data, stream=True)
35
36
response.raise_for_status()
37
first = True
38
39
for line in response.iter_content(chunk_size=1024):
40
if line.startswith(b"data: [DONE]"):
41
break
42
43
elif line.startswith(b"data: "):
44
chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
45
46
if chunk:
47
if first:
48
chunk = chunk.lstrip()
49
if chunk:
50
first = False
51
52
print(chunk)
53
54
# Note: json_data will not be serialized by requests
55
# exactly as it was in the original request.
56
#data = '{"model":"meta-llama/Llama-2-70b-chat-hf","messages":[{"role":"user","content":"what is the meaning of life ?"},{"role":"assistant","content":" The meaning of life is a question that has puzzled philosophers, religious leaders, scientists, and many others for centuries. There are many different perspectives on this question, and there is no one definitive answer. However, here are some possible approaches to understanding the meaning of life:\\n\\n1. Religious or spiritual perspective: Many people believe that the meaning of life is to fulfill a divine or spiritual purpose. According to this view, our lives have a higher purpose, which is to serve a deity or follow a set of moral principles. The meaning of life is then found in fulfilling this purpose, whether it be through prayer, service, or good works.\\n2. Personal fulfillment: Some people believe that the meaning of life is to find personal fulfillment and happiness. According to this view, we should pursue our passions and interests, cultivate strong relationships, and strive to live a life that is rich in experience and personal growth.\\n3. Social or cultural perspective: Others argue that the meaning of life is tied to our social and cultural contexts. From this perspective, the meaning of life is to contribute to the greater good of society, to uphold certain values or traditions, or to leave a lasting legacy.\\n4. Existentialism: Existentialists believe that the meaning of life is not predetermined or inherent, but rather something that each individual must create for themselves. According to this view, life has no inherent meaning, but we can give it meaning through our choices, actions, and experiences.\\n5. Scientific perspective: Some people believe that the meaning of life can be understood through scientific inquiry. From this perspective, the meaning of life might be seen as a product of evolution, a byproduct of the natural processes that gave rise to consciousness and intelligence.\\n6. Humanistic perspective: Humanists believe that the meaning of life is to promote human flourishing and well-being. According to this view, we should strive to create a world where individuals can live fulfilling lives, pursue their goals and dreams, and reach their full potential.\\n7. Nihilistic perspective: A minority perspective is that life has no inherent meaning and that any attempt to impose meaning on it is ultimately futile. This view is often associated with nihilism, which argues that traditional beliefs and values are baseless and that morality is an illusion.\\n8. Mystical perspective: Finally, some people believe that the meaning of life cannot be expressed in words or concepts, but can only be experienced directly through mystical practices such as meditation or psychedelic experiences.\\n\\nUltimately, the meaning of life may be something that each person must determine for themselves based on their own unique experiences, values, and beliefs. It could also be said that life has multiple meanings, and that these meanings can change throughout our lives as we grow, learn, and evolve as individuals."},{"role":"user","content":"welll well well"},{"role":"assistant","content":" I\'m glad you\'re feeling enthusiastic! \\"Well\\" can be used in various ways to express excitement or surprise, such as:\\n\\n* \\"Well, well, well!\\" - This phrase is often used to express surprise or intrigue, especially when something unexpected happens or when you\'re presented with new information.\\n* \\"Look at you, well done!\\" - Here, \\"well\\" is used to convey approval or admiration for someone\'s achievement or effort.\\n* \\"You think you\'re so smart, don\'t you?\\" - In this sentence, \\"well\\" is used to express sarcasm or skepticism towards someone who thinks they know everything.\\n\\nHowever, I\'d like to point out that using \\"well\\" too frequently can come across as insincere or filler words, especially in formal writing or professional settings. It\'s important to use language that accurately conveys your intended message without relying on unnecessary filler words. Is there anything else you\'d like me to help you with?"},{"role":"user","content":"nice"}],"stream":true}'
57
#response = requests.post('https://api.deepinfra.com/v1/openai/chat/completions', headers=headers, data=data)