typing.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. class OraResponse:
  2. class Completion:
  3. class Choices:
  4. def __init__(self, choice: dict) -> None:
  5. self.text = choice['text']
  6. self.content = self.text.encode()
  7. self.index = choice['index']
  8. self.logprobs = choice['logprobs']
  9. self.finish_reason = choice['finish_reason']
  10. def __repr__(self) -> str:
  11. return f'''<__main__.APIResponse.Completion.Choices(\n text = {self.text.encode()},\n index = {self.index},\n logprobs = {self.logprobs},\n finish_reason = {self.finish_reason})object at 0x1337>'''
  12. def __init__(self, choices: dict) -> None:
  13. self.choices = [self.Choices(choice) for choice in choices]
  14. class Usage:
  15. def __init__(self, usage_dict: dict) -> None:
  16. self.prompt_tokens = usage_dict['prompt_tokens']
  17. self.completion_tokens = usage_dict['completion_tokens']
  18. self.total_tokens = usage_dict['total_tokens']
  19. def __repr__(self):
  20. return f'''<__main__.APIResponse.Usage(\n prompt_tokens = {self.prompt_tokens},\n completion_tokens = {self.completion_tokens},\n total_tokens = {self.total_tokens})object at 0x1337>'''
  21. def __init__(self, response_dict: dict) -> None:
  22. self.response_dict = response_dict
  23. self.id = response_dict['id']
  24. self.object = response_dict['object']
  25. self.created = response_dict['created']
  26. self.model = response_dict['model']
  27. self.completion = self.Completion(response_dict['choices'])
  28. self.usage = self.Usage(response_dict['usage'])
  29. def json(self) -> dict:
  30. return self.response_dict
粤ICP备19079148号