Skip to content

Commit

Permalink
Record and play back OpenAI responses in dev server
Browse files Browse the repository at this point in the history
Now we can iterated faster, and save costs.
  • Loading branch information
unstubbable committed Mar 19, 2024
1 parent 165a8e2 commit a270934
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tsconfig.tsbuildinfo
.turbo
cdk.out
cdk.context.json
dev-server/openai-tapes
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ For more information on the Google Custom Search JSON API, visit:

### Dev Server

To start the development server (which watches for file changes), run:
To start the dev server (which watches for file changes), run:

```
npm run dev
```

Recording and replaying OpenAI responses can be enabled by starting the dev
server as follows:

```
OPENAI_BASE_URL=http://localhost:3001/v1/ npm run dev
```

### Browser

Open [http://localhost:3000](http://localhost:3000) in your browser.
19 changes: 19 additions & 0 deletions dev-server/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'path';
import {serve} from '@hono/node-server';
import {serveStatic} from '@hono/node-server/serve-static';
import {Hono} from 'hono';
import {compress} from 'hono/compress';
import talkbackModule from 'talkback';
import {authMiddleware} from './auth-middleware.js';
import './stub-awslambda.js';

Expand All @@ -23,3 +25,20 @@ serve({fetch: app.fetch, port: 3000}, ({address, port}) => {

return console.log(`Started dev server at ${serverUrl}`);
});

if (process.env.OPENAI_BASE_URL?.startsWith(`http://localhost`)) {
const {port} = new URL(process.env.OPENAI_BASE_URL);

// hacky kind of es module interop
const talkback = talkbackModule as unknown as typeof talkbackModule.default;

const talkbackServer = talkback({
host: `https://api.openai.com`,
record: talkback.Options.RecordMode.NEW,
port: parseInt(port, 10),
path: path.join(import.meta.dirname, `openai-tapes`),
summary: false,
});

await talkbackServer.start(() => console.log(`Talkback server started`));
}
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"source-map-loader": "^4.0.1",
"swc-loader": "^0.2.6",
"tailwindcss": "^3.2.7",
"talkback": "^3.0.2",
"tsx": "^4.7.1",
"turbo": "~1.11.3",
"typescript": "^5.4.2",
Expand Down

0 comments on commit a270934

Please sign in to comment.