Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Last active May 19, 2024 14:13
Show Gist options
  • Save christopherbauer/bf28cc30046bb57d4ed19fd928419bb4 to your computer and use it in GitHub Desktop.
Save christopherbauer/bf28cc30046bb57d4ed19fd928419bb4 to your computer and use it in GitHub Desktop.
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