Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created May 18, 2024 17:13
Show Gist options
  • Save christopherbauer/0b168f9a94776a8977cab4e86c4cee46 to your computer and use it in GitHub Desktop.
Save christopherbauer/0b168f9a94776a8977cab4e86c4cee46 to your computer and use it in GitHub Desktop.
export class ChatGptConversationService {
/* ... */
startConversation = async (
id: string,
systemPersona: string,
persona: string,
message: string
) => {
const startConversationTime = new Date();
const startMessages: ChatCompletionMessageParam[] = [
{ role: "system", content: systemPersona },
{ role: "user", content: message },
];
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: startMessages,
});
ConversationStorage[id] = {
persona: persona,
messages: startMessages
.map((m) => ({ ...m, sent: startConversationTime }))
.concat({
role: "assistant",
sent: new Date(),
content: response.choices[0].message.content,
}),
};
return response.choices[0].message.content;
};
/* ... */
}
export class InMemoryConversationService implements ConversationService {
/* ... */
createConversation = async (
id: string,
persona: string,
initial: Message
) => {
const startingPrompt = personas[persona].prompt;
const response = await this.chatgpt.startConversation(
id,
startingPrompt,
persona,
initial.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