XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Fix unittests, use Union typing

d733930a
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

4 个文件 +19 -13
Modified etc/unittest/client.py +5 -3
@@ -35,13 +35,15 @@ class TestPassModel(unittest.TestCase):
35 35 response = client.chat.completions.create(messages, "Hello", stream=True)
36 36 for chunk in response:
37 37 self.assertIsInstance(chunk, ChatCompletionChunk)
38 self.assertIsInstance(chunk.choices[0].delta.content, str)
38 if chunk.choices[0].delta.content is not None:
39 self.assertIsInstance(chunk.choices[0].delta.content, str)
39 40 messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]]
40 41 response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2)
41 42 response = list(response)
42 self.assertEqual(len(response), 2)
43 self.assertEqual(len(response), 3)
43 44 for chunk in response:
44 self.assertEqual(chunk.choices[0].delta.content, "You ")
45 if chunk.choices[0].delta.content is not None:
46 self.assertEqual(chunk.choices[0].delta.content, "You ")
45 47
46 48 def test_stop(self):
47 49 client = Client(provider=YieldProviderMock)
Modified g4f/api/__init__.py +5 -5
@@ -6,7 +6,7 @@ import nest_asyncio
6 6 from fastapi import FastAPI, Response, Request
7 7 from fastapi.responses import StreamingResponse, RedirectResponse, HTMLResponse, JSONResponse
8 8 from pydantic import BaseModel
9 from typing import List
9 from typing import List, Union
10 10
11 11 import g4f
12 12 import g4f.debug
@@ -16,12 +16,12 @@ from g4f.typing import Messages
16 16 class ChatCompletionsConfig(BaseModel):
17 17 messages: Messages
18 18 model: str
19 provider: str | None
19 provider: Union[str, None]
20 20 stream: bool = False
21 temperature: float | None
21 temperature: Union[float, None]
22 22 max_tokens: int = None
23 stop: list[str] | str | None
24 access_token: str | None
23 stop: Union[list[str], str, None]
24 access_token: Union[str, None]
25 25
26 26 class Api:
27 27 def __init__(self, engine: g4f, debug: bool = True, sentry: bool = False,
Modified g4f/client.py +2 -2
@@ -17,7 +17,7 @@ from . import get_model_and_provider, get_last_provider
17 17
18 18 ImageProvider = Union[BaseProvider, object]
19 19 Proxies = Union[dict, str]
20 IterResponse = Generator[ChatCompletion | ChatCompletionChunk, None, None]
20 IterResponse = Generator[Union[ChatCompletion, ChatCompletionChunk], None, None]
21 21
22 22 def read_json(text: str) -> dict:
23 23 """
@@ -124,7 +124,7 @@ class Completions():
124 124 stream: bool = False,
125 125 response_format: dict = None,
126 126 max_tokens: int = None,
127 stop: list[str] | str = None,
127 stop: Union[list[str], str] = None,
128 128 **kwargs
129 129 ) -> Union[ChatCompletion, Generator[ChatCompletionChunk]]:
130 130 if max_tokens is not None:
Modified g4f/stubs.py +7 -3
@@ -1,6 +1,8 @@
1 1
2 2 from __future__ import annotations
3 3
4 from typing import Union
5
4 6 class Model():
5 7 ...
6 8
@@ -52,7 +54,7 @@ class ChatCompletionChunk(Model):
52 54 }
53 55
54 56 class ChatCompletionMessage(Model):
55 def __init__(self, content: str | None):
57 def __init__(self, content: Union[str, None]):
56 58 self.role = "assistant"
57 59 self.content = content
58 60
@@ -72,7 +74,9 @@ class ChatCompletionChoice(Model):
72 74 }
73 75
74 76 class ChatCompletionDelta(Model):
75 def __init__(self, content: str | None):
77 content: Union[str, None] = None
78
79 def __init__(self, content: Union[str, None]):
76 80 if content is not None:
77 81 self.content = content
78 82
@@ -80,7 +84,7 @@ class ChatCompletionDelta(Model):
80 84 return self.__dict__
81 85
82 86 class ChatCompletionDeltaChoice(Model):
83 def __init__(self, delta: ChatCompletionDelta, finish_reason: str | None):
87 def __init__(self, delta: ChatCompletionDelta, finish_reason: Union[str, None]):
84 88 self.delta = delta
85 89 self.finish_reason = finish_reason
86 90