mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
adds function definition and operations
This commit is contained in:
parent
76df4e6e7a
commit
ac19a79256
47
operations/chatOperations.ts
Normal file
47
operations/chatOperations.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { post } from './fetch';
|
||||
import childrenBookDefinition from './function-definitions/childrenBookDefinition';
|
||||
|
||||
const DEFAULT_SYSTEM_PROMPT = `You are a genius story writer with incredible ability to craft narratives, stories, and children's books that captivate the imagination and entertain. Make the quality of your writing as excellent as possible (think Dr. Seuss, J.K. Rowling, etc). Feel free to include act structure names/chapters.)`;
|
||||
|
||||
const DEFAULT_USER_PROMPT = `The book is about a dude named SOULJABOI programmer who is trying to build a startup and is enchanted by naughty witches.`;
|
||||
|
||||
const generateRequestPayload = (messages: { role: string; content: string }[]) => ({
|
||||
model: 'gpt-3.5-turbo',
|
||||
messages,
|
||||
functions: childrenBookDefinition // Make sure this is correctly imported and structured
|
||||
});
|
||||
|
||||
// TODO(Benson): Type the interface returned from open ai
|
||||
|
||||
function getFunctionCallArguments<T>(response: any) {
|
||||
return JSON.parse(response.text.function_call.arguments);
|
||||
}
|
||||
|
||||
// export interface IStory {
|
||||
// title: string;
|
||||
// topic: string;
|
||||
// introduction: string;
|
||||
// narrativeStructure: string;
|
||||
// archetypes_characters: string;
|
||||
// pages: { text: string }[]
|
||||
// }
|
||||
|
||||
async function createStoryAsync(
|
||||
systemPrompt = DEFAULT_SYSTEM_PROMPT,
|
||||
userPrompt = DEFAULT_USER_PROMPT
|
||||
): Promise<any> {
|
||||
const messages = [
|
||||
{
|
||||
role: 'system',
|
||||
content: systemPrompt
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: userPrompt
|
||||
}
|
||||
];
|
||||
const data = await post('/api/open-ai/chat', generateRequestPayload(messages));
|
||||
return getFunctionCallArguments<any>(data);
|
||||
}
|
||||
|
||||
export default { createStoryAsync };
|
21
operations/fetch.ts
Normal file
21
operations/fetch.ts
Normal file
@ -0,0 +1,21 @@
|
||||
async function post(path: string, payload?: any): Promise<any> {
|
||||
try {
|
||||
const response = await fetch(path, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ payload })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw new Error('HTTP Post Error');
|
||||
}
|
||||
}
|
||||
|
||||
export { post };
|
68
operations/function-definitions/childrenBookDefinition.ts
Normal file
68
operations/function-definitions/childrenBookDefinition.ts
Normal file
@ -0,0 +1,68 @@
|
||||
const childrenBookDefinition = [
|
||||
{
|
||||
name: 'createAChildrensBook',
|
||||
description: `This function creates an entire *short* childrens book along with all of its pages (about ~5). It takes in a topic and a title and returns a childrens book object. These stories need to be profound and meaningful- reminiscent of the world's most important and transcendent children's stories and possesing profoundly archetypal characters rich in meaning and symbolism. It needs to be a powerful and profound narrative structure that is entertaining to read. Make sure that the language is simple and inocent like a childrens book.`,
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
title: {
|
||||
type: 'string',
|
||||
description: 'Title of the book- needs to be creative, powerful, and intruiging'
|
||||
},
|
||||
topic: {
|
||||
type: 'string',
|
||||
description: 'Topic of the book'
|
||||
},
|
||||
introduction: {
|
||||
type: 'string',
|
||||
description:
|
||||
'Front cover introduction to the book. Needs to be as intruiging and mystifying as possible to get the reader hooked'
|
||||
},
|
||||
narrativeStructure: {
|
||||
type: 'string',
|
||||
description: 'strong one sentence description of the narrative structure of the book. '
|
||||
},
|
||||
archetypes_characters: {
|
||||
type: 'string',
|
||||
description:
|
||||
'This is a bulleted list of the characters in the book. It also has a description of the archetypes that they and their struggles represent. The archetypes need to be profound and meaningful. The characters need to be rich in symbolism and meaning.'
|
||||
},
|
||||
pages: {
|
||||
type: 'array',
|
||||
description:
|
||||
'Array of pages in the book- each page should be about ~2-4 sentences. Each page is an item inside of the array with text inside of it. This is the content of the book- return the writing in markdown if need be',
|
||||
items: {
|
||||
principle: {
|
||||
type: 'dictionary',
|
||||
description: 'dictionaries of the 10 pages of the book.',
|
||||
items: {
|
||||
page_number: {
|
||||
type: 'number',
|
||||
description: 'page number'
|
||||
},
|
||||
chapter_name: {
|
||||
type: 'string',
|
||||
description: 'Name that the chapter is called'
|
||||
},
|
||||
page_contents: {
|
||||
type: 'number',
|
||||
description: 'contents of the page of the book'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
required: [
|
||||
'title',
|
||||
'topic',
|
||||
'introduction',
|
||||
'narrativeStructure',
|
||||
'archetypes_characters',
|
||||
'pages'
|
||||
]
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
export default childrenBookDefinition;
|
Loading…
x
Reference in New Issue
Block a user