延續Kevin 的GPT3 Linebot 加上用DynamoDB 儲存conversation 的功能,以及換上新推出的ChatGPT API~
- AWS CLI
- npm
- conda
- OpenAI API account with payment set (or still in free trial)
- LineBot
Prepair your:
OpenAI API key
Linebot: Channel_access_token and Channel_secret
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
We are using ChatCompletion model (gpt-3.5-turbo), which is the same model behind ChatGPT. See Documentation
Ex:
# 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?"}
]
)
There are no session managed by the api now. We can maintain session by storing the conversation and feed it to the model again and again.
Ex:
import openai
openai.api_key = "XXXX" # supply your API key however you choose
message = {"role":"user", "content": input("This is the beginning of your chat with AI. [To exit, send \"###\".]\n\nYou:")};
conversation = [{"role": "system", "content": "DIRECTIVE_FOR_gpt-3.5-turbo"}]
while(message["content"]!="###"):
conversation.append(message)
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=conversation)
message["content"] = input(f"Assistant: {completion.choices[0].message.content} \nYou:")
print()
conversation.append(completion.choices[0].message)
gpt-3.5-turbo: $0.002 / 1K tokens
conda create -n chatgpt_linebot python=3.9
conda activate chatgpt_linebot
pip install line-bot-sdk==1.12.1 openai boto3
export AWS_ACCESS_KEY_ID=**********
export AWS_SECRET_ACCESS_KEY=***********
npm install serverless -g
serverless create --template aws-python --path aws-line-ChatGPT-bot
Paste your Channel_accesss_token
, Channel_secret
, openAI_API_token
in serverless.yml
: provider.environment
sls deploy
maybe need to use sudo
, I'm not sure how to fix
DynamoDB -> Tables -> chatgpt-conversation-table overview -> additional info copy the Amazon Resource Name (ARN)
uncomment these lines and paster your ARN:
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:*
Resource:
- "YOUR_ARN"
Try it out. Use CloudWatch to check logs and debug if needed.
Logs are automatically stored in S3.