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

XFEstudio/gpt4free

Update async.py

6e3f350f
H Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +18 -30
Modified g4f/client/async.py +18 -30
@@ -6,49 +6,38 @@ import time
6 6 import random
7 7 import string
8 8
9 from .types import BaseProvider, ProviderType, FinishReason
9 10 from .stubs import ChatCompletion, ChatCompletionChunk, Image, ImagesResponse
10 from .typing import Union, Iterator, Messages, ImageType
11 from .providers.types import BaseProvider, ProviderType, FinishReason
12 from .image import ImageResponse as ImageProviderResponse
13 from .errors import NoImageResponseError, RateLimitError, MissingAuthError
14 from . import get_model_and_provider, get_last_provider
11 from ..typing import Union, Iterator, Messages, ImageType, AsyncIerator
12 from ..image import ImageResponse as ImageProviderResponse
13 from ..errors import NoImageResponseError, RateLimitError, MissingAuthError
14 from .. import get_model_and_provider, get_last_provider
15 from .helper import read_json
15 16
16 17 from .Provider.BingCreateImages import BingCreateImages
17 18 from .Provider.needs_auth import Gemini, OpenaiChat
18 from .Provider.You import You
19 from .helper import read_json
19 from ..Provider.You import You
20 20
21 def iter_response(
22 response: iter[str],
21 async def iter_response(
22 response: AsyncIerator[str],
23 23 stream: bool,
24 24 response_format: dict = None,
25 25 max_tokens: int = None,
26 26 stop: list = None
27 ) -> IterResponse:
27 ) -> AsyncIterResponse:
28 28 content = ""
29 29 finish_reason = None
30 30 completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28))
31 for idx, chunk in enumerate(response):
31 count: int = 0
32 async for idx, chunk in response:
32 33 if isinstance(chunk, FinishReason):
33 34 finish_reason = chunk.reason
34 35 break
35 36 content += str(chunk)
36 if max_tokens is not None and idx + 1 >= max_tokens:
37 count += 1
38 if max_tokens is not None and count >= max_tokens:
37 39 finish_reason = "length"
38 first = -1
39 word = None
40 if stop is not None:
41 for word in list(stop):
42 first = content.find(word)
43 if first != -1:
44 content = content[:first]
45 break
46 if stream and first != -1:
47 first = chunk.find(word)
48 if first != -1:
49 chunk = chunk[:first]
50 else:
51 first = 0
40 first, content, chunk = find_stop(stop, content, chunk)
52 41 if first != -1:
53 42 finish_reason = "stop"
54 43 if stream:
@@ -64,16 +53,15 @@ def iter_response(
64 53 content = read_json(content)
65 54 yield ChatCompletion(content, finish_reason, completion_id, int(time.time()))
66 55
67 def iter_append_model_and_provider(response: IterResponse) -> IterResponse:
56 async def iter_append_model_and_provider(response: AsyncIterResponse) -> IterResponse:
68 57 last_provider = None
69 for chunk in response:
58 async for chunk in response:
70 59 last_provider = get_last_provider(True) if last_provider is None else last_provider
71 60 chunk.model = last_provider.get("model")
72 61 chunk.provider = last_provider.get("name")
73 62 yield chunk
74 63
75 64 class Client():
76
77 65 def __init__(
78 66 self,
79 67 api_key: str = None,
@@ -222,4 +210,4 @@ class Images():
222 210 result = ImagesResponse([Image(image)for image in result])
223 211 if result is None:
224 212 raise NoImageResponseError()
225 return result
213 return result