Last active
May 19, 2024 14:13
-
-
Save christopherbauer/bf28cc30046bb57d4ed19fd928419bb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class ChatGptConversationService { | |
/* ... */ | |
private newMessage = ( | |
message: OpenAI.Chat.Completions.ChatCompletionMessageParam | |
) => ({ ...message, sent: new Date() }); | |
sendMessages = async ( | |
id: string, | |
messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] | |
) => { | |
ConversationStorage[id].messages.push(...messages.map(this.newMessage)); | |
const response = await openai.chat.completions.create({ | |
model: "gpt-3.5-turbo", | |
messages: ConversationStorage[id].messages, | |
}); | |
ConversationStorage[id].messages.push( | |
this.newMessage(response.choices[0].message) | |
); | |
return response.choices[0].message.content; | |
}; | |
/* ... */ | |
} | |
export class InMemoryConversationService implements ConversationService { | |
/* ... */ | |
updateConversation = async (id: string, message: Message) => { | |
const response = await this.chatgpt.sendMessages(id, [ | |
{ role: "user", content: message.text }, | |
]); | |
if (!response) { | |
throw new Error("No response from ChatGPT"); | |
} | |
return this.chatgpt.getConversation(id); | |
}; | |
/* ... */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment