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

XFEstudio/gpt4free

Update stubs.py

0c4a1d0b
H Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +106 -1
Modified g4f/client/stubs.py +106 -1
@@ -1 +1,106 @@
1 #
1 from __future__ import annotations
2
3 from typing import Union
4
5 class Model():
6 ...
7
8 class ChatCompletion(Model):
9 def __init__(
10 self,
11 content: str,
12 finish_reason: str,
13 completion_id: str = None,
14 created: int = None
15 ):
16 self.id: str = f"chatcmpl-{completion_id}" if completion_id else None
17 self.object: str = "chat.completion"
18 self.created: int = created
19 self.model: str = None
20 self.provider: str = None
21 self.choices = [ChatCompletionChoice(ChatCompletionMessage(content), finish_reason)]
22 self.usage: dict[str, int] = {
23 "prompt_tokens": 0, #prompt_tokens,
24 "completion_tokens": 0, #completion_tokens,
25 "total_tokens": 0, #prompt_tokens + completion_tokens,
26 }
27
28 def to_json(self):
29 return {
30 **self.__dict__,
31 "choices": [choice.to_json() for choice in self.choices]
32 }
33
34 class ChatCompletionChunk(Model):
35 def __init__(
36 self,
37 content: str,
38 finish_reason: str,
39 completion_id: str = None,
40 created: int = None
41 ):
42 self.id: str = f"chatcmpl-{completion_id}" if completion_id else None
43 self.object: str = "chat.completion.chunk"
44 self.created: int = created
45 self.model: str = None
46 self.provider: str = None
47 self.choices = [ChatCompletionDeltaChoice(ChatCompletionDelta(content), finish_reason)]
48
49 def to_json(self):
50 return {
51 **self.__dict__,
52 "choices": [choice.to_json() for choice in self.choices]
53 }
54
55 class ChatCompletionMessage(Model):
56 def __init__(self, content: Union[str, None]):
57 self.role = "assistant"
58 self.content = content
59
60 def to_json(self):
61 return self.__dict__
62
63 class ChatCompletionChoice(Model):
64 def __init__(self, message: ChatCompletionMessage, finish_reason: str):
65 self.index = 0
66 self.message = message
67 self.finish_reason = finish_reason
68
69 def to_json(self):
70 return {
71 **self.__dict__,
72 "message": self.message.to_json()
73 }
74
75 class ChatCompletionDelta(Model):
76 content: Union[str, None] = None
77
78 def __init__(self, content: Union[str, None]):
79 if content is not None:
80 self.content = content
81
82 def to_json(self):
83 return self.__dict__
84
85 class ChatCompletionDeltaChoice(Model):
86 def __init__(self, delta: ChatCompletionDelta, finish_reason: Union[str, None]):
87 self.delta = delta
88 self.finish_reason = finish_reason
89
90 def to_json(self):
91 return {
92 **self.__dict__,
93 "delta": self.delta.to_json()
94 }
95
96 class Image(Model):
97 url: str
98
99 def __init__(self, url: str) -> None:
100 self.url = url
101
102 class ImagesResponse(Model):
103 data: list[Image]
104
105 def __init__(self, data: list) -> None:
106 self.data = data