返回提交历史
Modified
etc/unittest/__main__.py
+1
-0
Added
etc/unittest/async_client.py
+56
-0
Modified
etc/unittest/integration.py
+9
-1
Modified
g4f/Provider/Vercel.py
+1
-1
Modified
g4f/client/async_client.py
+7
-0
Modified
g4f/gui/client/static/js/chat.v1.js
+4
-7
Modified
g4f/requests/curl_cffi.py
+4
-6
XFEstudio/gpt4free
Add unittests for async client (#1830)
* Add unittests for async client * Add pollyfill for anext * Update integration tests
0b712c2b
代码差异
7 个文件
+82
-15
@@ -4,6 +4,7 @@ from .backend import *
4
4
from .main import *
5
5
from .model import *
6
6
from .client import *
7
from .async_client import *
7
8
from .include import *
8
9
from .integration import *
9
10
@@ -0,0 +1,56 @@
1
import unittest
2
3
from g4f.client import AsyncClient, ChatCompletion, ChatCompletionChunk
4
from .mocks import AsyncGeneratorProviderMock, ModelProviderMock, YieldProviderMock
5
6
DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
7
8
class AsyncTestPassModel(unittest.IsolatedAsyncioTestCase):
9
10
async def test_response(self):
11
client = AsyncClient(provider=AsyncGeneratorProviderMock)
12
response = await client.chat.completions.create(DEFAULT_MESSAGES, "")
13
self.assertIsInstance(response, ChatCompletion)
14
self.assertEqual("Mock", response.choices[0].message.content)
15
16
async def test_pass_model(self):
17
client = AsyncClient(provider=ModelProviderMock)
18
response = await client.chat.completions.create(DEFAULT_MESSAGES, "Hello")
19
self.assertIsInstance(response, ChatCompletion)
20
self.assertEqual("Hello", response.choices[0].message.content)
21
22
async def test_max_tokens(self):
23
client = AsyncClient(provider=YieldProviderMock)
24
messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
25
response = await client.chat.completions.create(messages, "Hello", max_tokens=1)
26
self.assertIsInstance(response, ChatCompletion)
27
self.assertEqual("How ", response.choices[0].message.content)
28
response = await client.chat.completions.create(messages, "Hello", max_tokens=2)
29
self.assertIsInstance(response, ChatCompletion)
30
self.assertEqual("How are ", response.choices[0].message.content)
31
32
async def test_max_stream(self):
33
client = AsyncClient(provider=YieldProviderMock)
34
messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
35
response = client.chat.completions.create(messages, "Hello", stream=True)
36
async for chunk in response:
37
self.assertIsInstance(chunk, ChatCompletionChunk)
38
if chunk.choices[0].delta.content is not None:
39
self.assertIsInstance(chunk.choices[0].delta.content, str)
40
messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]]
41
response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2)
42
response = [chunk async for chunk in response]
43
self.assertEqual(len(response), 3)
44
for chunk in response:
45
if chunk.choices[0].delta.content is not None:
46
self.assertEqual(chunk.choices[0].delta.content, "You ")
47
48
async def test_stop(self):
49
client = AsyncClient(provider=YieldProviderMock)
50
messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
51
response = await client.chat.completions.create(messages, "Hello", stop=["and"])
52
self.assertIsInstance(response, ChatCompletion)
53
self.assertEqual("How are you?", response.choices[0].message.content)
54
55
if __name__ == '__main__':
56
unittest.main()
@@ -8,7 +8,7 @@ except ImportError:
8
8
has_nest_asyncio = False
9
9
10
10
from g4f.client import Client, ChatCompletion
11
from g4f.Provider import Bing, OpenaiChat
11
from g4f.Provider import Bing, OpenaiChat, DuckDuckGo
12
12
13
13
DEFAULT_MESSAGES = [{"role": "system", "content": 'Response in json, Example: {"success: true"}'},
14
14
{"role": "user", "content": "Say success true in json"}]
@@ -19,11 +19,19 @@ class TestProviderIntegration(unittest.TestCase):
19
19
self.skipTest("nest_asyncio is not installed")
20
20
21
21
def test_bing(self):
22
self.skipTest("Not stable")
22
23
client = Client(provider=Bing)
23
24
response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
24
25
self.assertIsInstance(response, ChatCompletion)
25
26
self.assertIn("success", json.loads(response.choices[0].message.content))
26
27
28
def test_duckduckgo(self):
29
self.skipTest("Not working")
30
client = Client(provider=DuckDuckGo)
31
response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
32
self.assertIsInstance(response, ChatCompletion)
33
self.assertIn("success", json.loads(response.choices[0].message.content))
34
27
35
def test_openai(self):
28
36
client = Client(provider=OpenaiChat)
29
37
response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
@@ -11,7 +11,7 @@ except ImportError:
11
11
from ..typing import Messages, CreateResult
12
12
from .base_provider import AbstractProvider
13
13
from ..requests import raise_for_status
14
from ..errors import MissingRequirementsError, RateLimitError, ResponseStatusError
14
from ..errors import MissingRequirementsError
15
15
16
16
class Vercel(AbstractProvider):
17
17
url = 'https://chat.vercel.ai'
@@ -16,6 +16,13 @@ from ..errors import NoImageResponseError
16
16
from ..image import ImageResponse as ImageProviderResponse
17
17
from ..providers.base_provider import AsyncGeneratorProvider
18
18
19
try:
20
anext
21
except NameError:
22
async def anext(iter):
23
async for chunk in iter:
24
return chunk
25
19
26
async def iter_response(
20
27
response: AsyncIterator[str],
21
28
stream: bool,
@@ -1262,7 +1262,7 @@ if (SpeechRecognition) {
1262
1262
1263
1263
function may_stop() {
1264
1264
if (microLabel.classList.contains("recognition")) {
1265
//recognition.stop();
1265
recognition.stop();
1266
1266
}
1267
1267
}
1268
1268
@@ -1272,15 +1272,12 @@ if (SpeechRecognition) {
1272
1272
recognition.onstart = function() {
1273
1273
microLabel.classList.add("recognition");
1274
1274
startValue = messageInput.value;
1275
messageInput.placeholder = "";
1276
1275
lastDebounceTranscript = "";
1277
1276
timeoutHandle = window.setTimeout(may_stop, 10000);
1278
1277
};
1279
1278
recognition.onend = function() {
1280
1279
microLabel.classList.remove("recognition");
1281
messageInput.value = messageInput.placeholder;
1282
messageInput.placeholder = "Ask a question";
1283
//messageInput.focus();
1280
messageInput.focus();
1284
1281
};
1285
1282
recognition.onresult = function(event) {
1286
1283
if (!event.results) {
@@ -1298,9 +1295,9 @@ if (SpeechRecognition) {
1298
1295
lastDebounceTranscript = transcript;
1299
1296
}
1300
1297
if (transcript) {
1301
messageInput.placeholder = `${startValue ? startValue+"\n" : ""}${transcript.trim()}`;
1298
messageInput.value = `${startValue ? startValue+"\n" : ""}${transcript.trim()}`;
1302
1299
if (isFinal) {
1303
startValue = messageInput.placeholder;
1300
startValue = messageInput.value;
1304
1301
}
1305
1302
messageInput.style.height = messageInput.scrollHeight + "px";
1306
1303
messageInput.scrollTop = messageInput.scrollHeight;
@@ -34,15 +34,13 @@ class StreamResponse:
34
34
"""Asynchronously parse the JSON response content."""
35
35
return json.loads(await self.inner.acontent(), **kwargs)
36
36
37
async def iter_lines(self) -> AsyncGenerator[bytes, None]:
37
def iter_lines(self) -> AsyncGenerator[bytes, None]:
38
38
"""Asynchronously iterate over the lines of the response."""
39
async for line in self.inner.aiter_lines():
40
yield line
39
return self.inner.aiter_lines()
41
40
42
async def iter_content(self) -> AsyncGenerator[bytes, None]:
41
def iter_content(self) -> AsyncGenerator[bytes, None]:
43
42
"""Asynchronously iterate over the response content."""
44
async for chunk in self.inner.aiter_content():
45
yield chunk
43
return self.inner.aiter_content()
46
44
47
45
async def __aenter__(self):
48
46
"""Asynchronously enter the runtime context for the response object."""