Skip to content
Open
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
20 changes: 11 additions & 9 deletions apps/server/src/ai/open-ai-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import OpenAI from 'openai';
import { zodResponseFormat } from 'openai/helpers/zod';
import { z } from 'zod';

let openAIClient: OpenAI | undefined;
Expand All @@ -12,26 +11,29 @@ if (process.env.OPENAI_API_KEY && process.env.OPENAI_PROJECT_ID && process.env.O
}

const BookInsights = z.object({
genres: z.string().array(),
genres: z.array(z.string()),
summary: z.string(),
});

export async function getBookInsights(bookTitle: string, bookAuthor: string) {
const completion = await openAIClient?.beta.chat.completions.parse({
model: 'gpt-4o',
messages: [
if (!openAIClient) return;

const completion = await openAIClient.responses.parse({
model: 'gpt-4o-mini', // oder: gpt-4o
input: [
{
role: 'system',
content:
'You are an expert librarian. You know everything about every book. Respond with details about the book given the title and author',
'You are an expert librarian. Respond with details about the book given title + author.',
},
{
role: 'user',
content: `What can you tell me about the book ${bookTitle} by ${bookAuthor}`,
content: `What can you tell me about the book "${bookTitle}" by ${bookAuthor}?`,
},
],
response_format: zodResponseFormat(BookInsights, 'book_insights'),
schema: BookInsights,
});

return completion?.choices[0].message.parsed;
return completion.output;
}

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"engines": {
"node": ">=22"
},
"resolutions": {
"zod": "3.23.8"
},
"workspaces": [
"apps/*",
"packages/*"
Expand Down
8 changes: 7 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"scripts": {
"build": "tsc",
"dev": "tsc --watch"
}
},
"peerDependencies": {
"zod": "^3.23.0"
},
"devDependencies": {
"zod": "^3.23.0"
}
}