Skip to content

Instantly share code, notes, and snippets.

export const personas: Record<
string,
{ name: string; prompt: string; role: string; imageUri?: string }
> = {
/* ... */
Robot: {
name: "JaSON",
imageUri: "./Robot.png",
role: "Robot",
prompt: "You only respond with JSON code. You're always honest when you aren't sure how to do something.",
import { Conversation, Message } from "./conversation/types";
import { Persona } from "./persona/types";
export const getPersonas: () => Promise<Persona[]> = () => {
return fetch("http://localhost:3010/persona", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
app.route("/conversation").get(async (req, res) => {
const conversations = await inMemoryDB.getConversationList();
res.status(200).send(conversations);
});
export class ChatGptConversationService {
/* ... */
getConversationList = () => Promise.resolve(ConversationStorage);
/* ... */
}
export class InMemoryConversationService implements ConversationService {
/* ... */
getConversationList = async (): Promise<ConversationList> => {
const conversations = await this.chatgpt.getConversationList();
export class ChatGptConversationService {
/* ... */
deleteConversation = (id: string) => {
if (id in ConversationStorage) {
delete ConversationStorage[id];
return Promise.resolve(true);
}
return Promise.resolve(false);
};
/* ... */
.put(async (req, res) => {
//continue an ongoing conversation
const { id } = req.params;
const { message } = req.body;
const response = await inMemoryDB.updateConversation(id, {
text: message,
sent: new Date(),
source: "user",
});
res.status(200).send(response);
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));
.post(async (req, res) => {
//start a new conversation
const { id } = req.params;
const { persona, message } = req.body;
const response = await inMemoryDB.createConversation(id, persona, {
text: message,
sent: new Date(),
source: "user",
});
res.status(200).send(response);
app.route("/conversation/:id")
.get(async (req, res) => {
//get a conversation
const { id } = req.params;
const response = await inMemoryDB.getConversation(id);
res.status(200).send(response);
})
export class ChatGptConversationService {
/* ... */
startConversation = async (
id: string,
systemPersona: string,
persona: string,
message: string
) => {
const startConversationTime = new Date();
const startMessages: ChatCompletionMessageParam[] = [