__init__.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from ora.model import CompletionModel
  2. from ora.typing import OraResponse
  3. from requests import post
  4. from time import time
  5. from random import randint
  6. class Completion:
  7. def create(
  8. model : CompletionModel,
  9. prompt: str,
  10. includeHistory: bool = True,
  11. conversationId: str or None = None) -> OraResponse:
  12. extra = {
  13. 'conversationId': conversationId} if conversationId else {}
  14. response = post('https://ora.sh/api/conversation',
  15. headers = {
  16. "host" : "ora.sh",
  17. "authorization" : f"Bearer AY0{randint(1111, 9999)}",
  18. "user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
  19. "origin" : "https://ora.sh",
  20. "referer" : "https://ora.sh/chat/",
  21. },
  22. json = extra | {
  23. 'chatbotId': model.id,
  24. 'input' : prompt,
  25. 'userId' : model.createdBy,
  26. 'model' : model.modelName,
  27. 'provider' : 'OPEN_AI',
  28. 'includeHistory': includeHistory}).json()
  29. return OraResponse({
  30. 'id' : response['conversationId'],
  31. 'object' : 'text_completion',
  32. 'created': int(time()),
  33. 'model' : model.slug,
  34. 'choices': [{
  35. 'text' : response['response'],
  36. 'index' : 0,
  37. 'logprobs' : None,
  38. 'finish_reason' : 'stop'
  39. }],
  40. 'usage': {
  41. 'prompt_tokens' : len(prompt),
  42. 'completion_tokens' : len(response['response']),
  43. 'total_tokens' : len(prompt) + len(response['response'])
  44. }
  45. })
粤ICP备19079148号