import openai
openai.api_key = ""
def chatbot(message):
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=[
{"role": "user", "content": message},
],
temperature=0,
stream=True # again, we set stream=True
)
# create variables to collect the stream of chunks
# iterate through the stream of events
fin = []
for chunk in response:
chunk_message = chunk['choices'][0]['delta'].get('content', '') # extract the message
fin.append(chunk_message)
if len(fin) % 60 == 0 :#60は出力行の長さ
print(chunk_message)
else:
print(chunk_message, end = "")
translation_template = "次の文章を日本語に翻訳して。 : "
inputMessage = input()
chatbot(translation_template + inputMessage)
More than 1 year has passed since last update.
ChatGPT 翻訳 + stream print (like official site) コラボ環境
Posted at
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme