stubs.py 3.4 KB

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