Skip to content

Instantly share code, notes, and snippets.

export class ChatGptConversationService {
/* ... */
startConversation = async (
id: string,
systemPersona: string,
persona: string,
message: string
) => {
const startConversationTime = new Date();
const startMessages: ChatCompletionMessageParam[] = [
export class ChatGptConversationService {
/* ... */
getConversation = (id: string) =>
Promise.resolve(
ConversationStorage[id]?.messages
.filter((m) => m.role !== "system")
.map<Message>((m) => ({
text: String(m.content),
sent: m.sent,
source: m.role === "user" ? "user" : "bot",
import { personas } from "../persona";
import { ChatGptConversationService } from "./chatgpt";
import {Conversation, ConversationList, ConversationListEntry, ConversationService, Message } from "./types";
/*
* Should only be instantiated once for this app due to it being used in-memory rather than synced to a store
*/
export class InMemoryConversationService implements ConversationService {
chatgpt: ChatGptConversationService;
constructor() {
import OpenAI from "openai";
import { ChatCompletionMessageParam } from "openai/resources";
import { Message } from "../types";
const openai = new OpenAI({
apiKey: process.env.OPEN_AI_API_KEY,
});
type StorageMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam & {
sent: Date;
/** ... ***/
app.route("/persona").get(async (req, res) => {
const personas = getPersonas();
const personaMap = personas.map((p) => ({
id: p.id,
name: p.name,
imageUri: p.imageUri,
}));
res.status(200).send(personaMap);
export const personas: Record<
string,
{ name: string; prompt: string; role: string; imageUri?: string }
> = {
Builder: {
name: "Craig",
imageUri: "./Craig.png",
role: "Builder",
prompt: "You are a master craftsman, able to build anything you can imagine. When responding, be gruff but helpful. You're always honest when you aren't sure how to do something.",
},
export type Message = { text: string; sent: Date; source: string };
export type Conversation = Message[];
export type ConversationListEntry = {
id: string;
persona: string;
firstMessage: Message;
};
export type ConversationList = ConversationListEntry[];
export interface ConversationService {
getConversationList: () => Promise<ConversationList>;
import express from "express";
const app = express();
app.use(express.json());
app.get("/", (req, res) => {
res.send("Hello World");
});
const run = () => {
services:
api:
build: ./api
ports:
- "3010:3000"
volumes:
- .:/app
environment:
- NODE_ENV=development
- PORT=3000
FROM node:21-alpine
WORKDIR /app/web
COPY . .
RUN npm install --silent --legacy-peer-deps
CMD ["npm", "run", "dev"]