Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple srcmd from node libs and AI #140

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions packages/api/ai/srcbook-generator.mts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { generateText, GenerateTextResult } from 'ai';
import { type CodeLanguageType, type CellType } from '@srcbook/shared';
import {
type CodeLanguageType,
type CellType,
randomid,
type CellWithPlaceholderType,
} from '@srcbook/shared';
import { type SessionType } from '../types.mjs';
import { readFileSync } from 'node:fs';
import Path from 'node:path';
import { createOpenAI } from '@ai-sdk/openai';
import { PROMPTS_DIR } from '../constants.mjs';
import { encode, decodePartial } from '../srcmd.mjs';
import { encode, decodeCells } from '../srcmd.mjs';
import { getConfig } from '../config.mjs';

const OPENAI_MODEL = 'gpt-4o';
Expand All @@ -19,9 +24,17 @@ const makeGenerateCellSystemPrompt = (language: CodeLanguageType) => {
};

const makeGenerateCellUserPrompt = (session: SessionType, insertIdx: number, query: string) => {
const inlineSrcbookWithPlaceholder = encode(session.cells, session.metadata, {
// Make sure we copy cells so we don't mutate the session
const cellsWithPlaceholder: CellWithPlaceholderType[] = [...session.cells];

cellsWithPlaceholder.splice(insertIdx, 0, {
Copy link
Contributor Author

@benjreinhart benjreinhart Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since CellWithPlaceholderType is a superset of the CellType, any existing calls or cell representations will succeed just fine, but here, in the one place we need to do something different, we can leverage this superset type and pass in the cell instead of relying on the encoder to know what to do for AI.

id: randomid(),
type: 'placeholder',
text: '==== INTRODUCE CELL HERE ====',
});

const inlineSrcbookWithPlaceholder = encode(cellsWithPlaceholder, session.metadata, {
inline: true,
insertCellIdx: insertIdx,
});

const prompt = `==== BEGIN SRCBOOK ====
Expand Down Expand Up @@ -107,7 +120,7 @@ export async function generateCell(

// TODO figure out logging here. It's incredibly valuable to see the data going to and from the LLM
// for debugging, but there are considerations around privacy and log size to think about.
const decodeResult = decodePartial(text);
const decodeResult = decodeCells(text);

if (decodeResult.error) {
return { error: true, errors: decodeResult.errors };
Expand Down
Loading
Loading