From 08f3e07fb431fa31e9344d1a8e7ea8e9a4bbf275 Mon Sep 17 00:00:00 2001 From: Julian Boilen Date: Mon, 1 Jan 2024 11:09:25 -0500 Subject: [PATCH] Convert from deprecated completions to new chat completions api --- backend/src/business/ai/AiService.ts | 4 +- .../src/business/ai/createStoryTitlePrompt.ts | 71 +++++++++++++------ 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/backend/src/business/ai/AiService.ts b/backend/src/business/ai/AiService.ts index 131c86cb..d2ee44c8 100644 --- a/backend/src/business/ai/AiService.ts +++ b/backend/src/business/ai/AiService.ts @@ -7,9 +7,9 @@ const openai = new OpenAI({ }); export async function suggestStoryTitle(storyContent: string): Promise { - const response = await openai.completions.create( + const response = await openai.chat.completions.create( createStoryTitlePrompt(storyContent) ); - return response.choices[0].text?.trim() ?? ''; + return response.choices[0].message.content?.trim() ?? ''; } diff --git a/backend/src/business/ai/createStoryTitlePrompt.ts b/backend/src/business/ai/createStoryTitlePrompt.ts index bbeace9c..9196267e 100644 --- a/backend/src/business/ai/createStoryTitlePrompt.ts +++ b/backend/src/business/ai/createStoryTitlePrompt.ts @@ -2,30 +2,59 @@ import OpenAI from 'openai'; export default function createStoryTitlePrompt( storyContent: string -): OpenAI.CompletionCreateParamsNonStreaming { +): OpenAI.ChatCompletionCreateParamsNonStreaming { return { - model: 'text-davinci-003', - prompt: `Create a very short caption for each story - -Story: I lived in the building with the peak from my birth in 1948 until I was 13 in 1961, with my parents and siblings. The 3 windows right above the Bohack sign were in my parents bedroom. My fathers parents and my aunts and uncles lived on Grattan St in various apartments over the years. Those are the buildings to the left of my house. Very sad to see it now. -Caption: My childhood home - -Story: The Tepedino's, Michael and Angelina. ITALIAN immigrants from the Salerno area of Italy raised 3 children in this home. -Caption: Italian immigrants raised family - -Story: The plaza hotel -Caption: Plaza Hotel - -Story: That's old man Saso sitting on the stoop of 35 Snediker ave. He sat there every day for years. -Caption: Man sat daily on stoop - -Story: ${storyContent.replaceAll('\n', ' ').trim()} -Caption:`, - temperature: 0.1, - max_tokens: 11, + model: 'gpt-4', + messages: [ + { + role: 'system', + content: + 'You turn stories into captions. User input is a story about an old photo of a building in NYC. You respond with no more than 6 words to caption the story or label the map marker for the place. Personality: charming, curious, catchy, succinct. \nRespond with NO surrounding quotes.', + }, + { + role: 'user', + content: + 'I lived in the building with the peak from my birth in 1948 until I was 13 in 1961, with my parents and siblings. The 3 windows right above the Bohack sign were in my parents bedroom. My fathers parents and my aunts and uncles lived on Grattan St in various apartments over the years. Those are the buildings to the left of my house. Very sad to see it now.', + }, + { + role: 'assistant', + content: 'My childhood home', + }, + { + role: 'user', + content: + "The Tepedino's, Michael and Angelina. ITALIAN immigrants from the Salerno area of Italy raised 3 children in this home.", + }, + { + role: 'assistant', + content: 'Italian immigrants raised family', + }, + { + role: 'user', + content: 'The plaza hotel', + }, + { + role: 'assistant', + content: 'Plaza Hotel', + }, + { + role: 'user', + content: + "That's old man Saso sitting on the stoop of 35 Snediker ave. He sat there every day for years.", + }, + { + role: 'assistant', + content: 'Man sat daily on stoop', + }, + { + role: 'user', + content: storyContent, + }, + ], + temperature: 0.2, + max_tokens: 15, top_p: 1, frequency_penalty: 0, presence_penalty: 0, - stop: ['\n'], }; }