普段から使い慣れた、テキストログ風の段階的出力を試してみます。
実際のところ
from openai import OpenAI from dotenv import dotenv_values import sys def streamingQuestion(prompt, client): stream = yourclient.chat.completions.create( messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user","content": myquestion}, ], model="gpt-4", stream=True, ) for chunk in stream: if chunk.choices[0].delta.content is not None: # 受け取ったフレーズを逐次出力。 # flushをいれないとNULLになるまで表示しないので注意 text = chunk.choices[0].delta.content print(text,end='',flush=True) print("") return True myquestion = "" if len(sys.argv) > 1: myquestion = sys.argv[1] else: myquestion = "We just wanted to say hello. Take your time." config = dotenv_values(".env") yourclient = OpenAI( api_key= config["OPENAI_API_KEY"] ) streamingQuestion(myquestion,yourclient)
使ってみる
$ python3 chatgpttest.py "What is a Kimchi? Japanese food, I guess."
などとやると、以下の様に不正確さを指摘してくれます。
Actually, Kimchi is a traditional Korean dish, not Japanese. It's typically made from salted and fermented vegetables, most commonly napa cabbage and Korean radishes, along with a variety of seasonings including chili pepper, garlic, ginger, and jeotgal (salted seafood). Kimchi is highly nutritious and is known for its bold and spicy flavor.