megaMUR_LG/chatbot/chat/chatgpt.py

26 lines
684 B
Python
Raw Normal View History

import os
import openai
def chat(message, openai_key, shortAnswer=True):
print("###################")
openai.api_key = openai_key
if shortAnswer:
message = message + " - eine kurze Antwort bitte!"
print(f"Asking ChatGPT for an answer to: \n {message} \n ...")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": message}
]
)
first_answer = completion.choices[0].message.content
print(f"Got answer from ChatGPT:\n {first_answer}")
return first_answer
if __name__ == "__main__":
answer = chat("Erzähle mir was über Murnau")
print(answer)