openai.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import openai
  2. import requests
  3. import json
  4. from .logging import Logger
  5. from typing import Dict, List, Tuple, Generator, Optional
  6. class OpenAI:
  7. api_key: str
  8. chat_model: str = "gpt-3.5-turbo"
  9. logger: Logger
  10. api_code: str = "openai"
  11. @property
  12. def chat_api(self) -> str:
  13. return self.chat_model
  14. classification_api = chat_api
  15. image_api: str = "dalle"
  16. operator: str = "OpenAI ([https://openai.com](https://openai.com))"
  17. def __init__(self, api_key, chat_model=None, logger=None):
  18. self.api_key = api_key
  19. self.chat_model = chat_model or self.chat_model
  20. self.logger = logger or Logger()
  21. def generate_chat_response(self, messages: List[Dict[str, str]], user: Optional[str] = None) -> Tuple[str, int]:
  22. """Generate a response to a chat message.
  23. Args:
  24. messages (List[Dict[str, str]]): A list of messages to use as context.
  25. Returns:
  26. Tuple[str, int]: The response text and the number of tokens used.
  27. """
  28. self.logger.log(f"Generating response to {len(messages)} messages using {self.chat_model}...")
  29. response = openai.ChatCompletion.create(
  30. model=self.chat_model,
  31. messages=messages,
  32. api_key=self.api_key,
  33. user = user
  34. )
  35. result_text = response.choices[0].message['content']
  36. tokens_used = response.usage["total_tokens"]
  37. self.logger.log(f"Generated response with {tokens_used} tokens.")
  38. return result_text, tokens_used
  39. def classify_message(self, query: str, user: Optional[str] = None) -> Tuple[Dict[str, str], int]:
  40. system_message = """You are a classifier for different types of messages. You decide whether an incoming message is meant to be a prompt for an AI chat model, or meant for a different API. You respond with a JSON object like this:
  41. { "type": event_type, "prompt": prompt }
  42. - If the message you received is meant for the AI chat model, the event_type is "chat", and the prompt is the literal content of the message you received. This is also the default if none of the other options apply.
  43. - If it is a prompt for a calculation that can be answered better by WolframAlpha than an AI chat bot, the event_type is "calculate". Optimize the message you received for input to WolframAlpha, and return it as the prompt attribute.
  44. - If it is a prompt for an AI image generation, the event_type is "imagine". Optimize the message you received for use with DALL-E, and return it as the prompt attribute.
  45. - If the user is asking you to create a new room, the event_type is "newroom", and the prompt is the name of the room, if one is given, else an empty string.
  46. - If the user is asking you to throw a coin, the event_type is "coin". The prompt is an empty string.
  47. - If the user is asking you to roll a dice, the event_type is "dice". The prompt is an string containing an optional number of sides, if one is given, else an empty string.
  48. - If for any reason you are unable to classify the message (for example, if it infringes on your terms of service), the event_type is "error", and the prompt is a message explaining why you are unable to process the message.
  49. Only the event_types mentioned above are allowed, you must not respond in any other way."""
  50. messages = [
  51. {
  52. "role": "system",
  53. "content": system_message
  54. },
  55. {
  56. "role": "user",
  57. "content": query
  58. }
  59. ]
  60. self.logger.log(f"Classifying message '{query}'...")
  61. response = openai.ChatCompletion.create(
  62. model=self.chat_model,
  63. messages=messages,
  64. api_key=self.api_key,
  65. user = user
  66. )
  67. try:
  68. result = json.loads(response.choices[0].message['content'])
  69. except:
  70. result = {"type": "chat", "prompt": query}
  71. tokens_used = response.usage["total_tokens"]
  72. self.logger.log(f"Classified message as {result['type']} with {tokens_used} tokens.")
  73. return result, tokens_used
  74. def generate_image(self, prompt: str, user: Optional[str] = None) -> Generator[bytes, None, None]:
  75. """Generate an image from a prompt.
  76. Args:
  77. prompt (str): The prompt to use.
  78. Yields:
  79. bytes: The image data.
  80. """
  81. self.logger.log(f"Generating image from prompt '{prompt}'...")
  82. response = openai.Image.create(
  83. prompt=prompt,
  84. n=1,
  85. api_key=self.api_key,
  86. size="1024x1024",
  87. user = user
  88. )
  89. images = []
  90. for image in response.data:
  91. image = requests.get(image.url).content
  92. images.append(image)
  93. return images, len(images)