ChatGPTAPI過去のトーク内容を踏まえて会話する方法
public.icon
ChatGPTのAPIを用いて、LINEBotなどを実装する時、送られてきたメッセージだけの送信では前提を踏まえてくれない
code:index.js
# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
role
systemでフォーマットを整える
userでユーザーが過去に送ってきたメッセージ
assistantでアシスタントの回答
なので、表題のことを実装するためには、DBから過去の やり取りを取得し、配列に入れて送信することで柔軟な回答を得ることができる