今天仍然在等待Training的結果,開始嘗試摸索chatGPT的API,想要讓ChatGPT依據我給的情境幫我做分類任務。
import openai
import speech_recognition as s_r
from pvrecorder import PvRecorder
import pyaudio
from gtts import gTTS
import os
from gpt_config import gpt_config
class ChatGPT(object):
def __init__(self):
openai.api_key = gpt_config["key"]
init_prompt = gpt_config["init_prompt"]
self.messages = []
self.messages.append({"role": "system", "content": "你是一个助手"})
self.messages.append({"role": "user", "content": init_prompt})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
# model = "gpt-4",
messages=self.messages,
#temperature=0.3,
)
# role = response['choices'][0].message.role
# print(role)
returned_text = response["choices"][0].message.content
# print(returned_text)
self.messages.append({"role": "assistant", "content": returned_text})
def __call__(self, query_text):
tmpMessages = self.messages.copy()
tmpMessages.append({"role": "user", "content": query_text})
response = openai.ChatCompletion.create(
model="gpt-4",
messages=tmpMessages,
#temperature=0.3,
)
returned_text = response["choices"][0].message.content
# print("chatgpt 的回应")
# print(returned_text)
return returned_text