LoginSignup
0
0

More than 1 year has passed since last update.

ChatGPT 翻訳 + stream print (like official site) コラボ環境

Posted at
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)

このコードで、翻訳が便利になります。streamingさせたのでみやすいと思います。
結果図스크린샷 2023-04-04 오후 2.09.42.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0