stubs.py 3.7 KB

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