openai-node
110 строк · 4.3 Кб
1#!/usr/bin/env -S npm run tsn -T
2
3import OpenAI from 'openai';4
5// gets API Key from environment variable OPENAI_API_KEY
6const openai = new OpenAI();7
8const functions = [9{10name: 'list',11description: 'list queries books by genre, and returns a list of names of books',12parameters: {13type: 'object',14properties: {15genre: { type: 'string', enum: ['mystery', 'nonfiction', 'memoir', 'romance', 'historical'] },16},17},18function: list,19parse: JSON.parse,20},21{22name: 'search',23description: 'search queries books by their name and returns a list of book names and their ids',24parameters: {25type: 'object',26properties: {27name: { type: 'string' },28},29},30function: search,31parse: JSON.parse,32},33{34name: 'get',35description:36"get returns a book's detailed information based on the id of the book. Note that this does not accept names, and only IDs, which you can get by using search.",37parameters: {38type: 'object',39properties: {40id: { type: 'string' },41},42},43function: get,44parse: JSON.parse,45},46];47
48async function main() {49const runner = await openai.beta.chat.completions50.runFunctions({51model: 'gpt-3.5-turbo',52messages: [53{54role: 'system',55content:56'Please use our book database, which you can access using functions to answer the following questions.',57},58{59role: 'user',60content:61'I really enjoyed reading To Kill a Mockingbird, could you recommend me a book that is similar and tell me why?',62},63],64functions,65})66.on('message', (msg) => console.log(msg))67.on('content', (diff) => process.stdout.write(diff));68
69const result = await runner.finalChatCompletion();70console.log(result);71
72console.log();73console.log(runner.messages);74}
75
76const db = [77{78id: 'a1',79name: 'To Kill a Mockingbird',80genre: 'historical',81description: `Compassionate, dramatic, and deeply moving, "To Kill A Mockingbird" takes readers to the roots of human behavior - to innocence and experience, kindness and cruelty, love and hatred, humor and pathos. Now with over 18 million copies in print and translated into forty languages, this regional story by a young Alabama woman claims universal appeal. Harper Lee always considered her book to be a simple love story. Today it is regarded as a masterpiece of American literature.`,82},83{84id: 'a2',85name: 'All the Light We Cannot See',86genre: 'historical',87description: `In a mining town in Germany, Werner Pfennig, an orphan, grows up with his younger sister, enchanted by a crude radio they find that brings them news and stories from places they have never seen or imagined. Werner becomes an expert at building and fixing these crucial new instruments and is enlisted to use his talent to track down the resistance. Deftly interweaving the lives of Marie-Laure and Werner, Doerr illuminates the ways, against all odds, people try to be good to one another.`,88},89{90id: 'a3',91name: 'Where the Crawdads Sing',92genre: 'historical',93description: `For years, rumors of the “Marsh Girl” haunted Barkley Cove, a quiet fishing village. Kya Clark is barefoot and wild; unfit for polite society. So in late 1969, when the popular Chase Andrews is found dead, locals immediately suspect her.94But Kya is not what they say. A born naturalist with just one day of school, she takes life's lessons from the land, learning the real ways of the world from the dishonest signals of fireflies. But while she has the skills to live in solitude forever, the time comes when she yearns to be touched and loved. Drawn to two young men from town, who are each intrigued by her wild beauty, Kya opens herself to a new and startling world—until the unthinkable happens.`,95},96];97
98async function list({ genre }: { genre: string }) {99return db.filter((item) => item.genre === genre).map((item) => ({ name: item.name, id: item.id }));100}
101
102async function search({ name }: { name: string }) {103return db.filter((item) => item.name.includes(name)).map((item) => ({ name: item.name, id: item.id }));104}
105
106async function get({ id }: { id: string }) {107return db.find((item) => item.id === id)!;108}
109
110main();111