Telegram-ChatBot-Serverless/utils/chat.py

22 lines
526 B
Python
Raw Normal View History

2023-07-15 08:00:45 +00:00
import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
2023-08-26 05:49:21 +00:00
def create_conversation(messages: list):
2023-07-15 08:00:45 +00:00
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=1,
)
2023-08-26 05:49:21 +00:00
content = response["choices"][0]["message"]["content"]
model = response["model"]
tokens = [
response["usage"]["total_tokens"],
response["usage"]["prompt_tokens"],
response["usage"]["completion_tokens"],
]
return content, model, tokens