CoolFace
Apppublic

BettyHsu/speech2text_chatgpt

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
Chat.py27 linesDownload Raw Back to root
1import requests2 3class Chat:4	def __init__(self, api):5		self.API_KEY = api6		self.messages = []7	def send_message(self, msg):8		self.messages.append({9			"role": "user",10			"content": msg,11		})12		response = requests.post(13			'https://api.openai.com/v1/chat/completions',14			headers = {15				'Content-Type': 'application/json',16				'Authorization': f'Bearer {self.API_KEY}',17			},18			json = {19				'model': 'gpt-3.5-turbo',20				'messages' : self.messages,21			})22		response_content = response.json()['choices'][0]['message']['content']23		self.messages.append({24			"role": "assistant",25			"content": response_content,26		})27		return response_content