openai-node

Форк
0
/
tool-call-helpers-zod.ts 
144 строки · 5.6 Кб
1
#!/usr/bin/env -S npm run tsn -T
2

3
import OpenAI from 'openai';
4
import { RunnableToolFunctionWithParse } from 'openai/lib/RunnableFunction';
5
import { JSONSchema } from 'openai/lib/jsonschema';
6
import { ZodSchema, z } from 'zod';
7
import { zodToJsonSchema } from 'zod-to-json-schema';
8

9
// gets API Key from environment variable OPENAI_API_KEY
10
const openai = new OpenAI();
11

12
// Define your functions, alongside zod schemas.
13

14
const ListParams = z.object({
15
  genre: z.enum(['mystery', 'nonfiction', 'memoir', 'romance', 'historical']),
16
});
17
type ListParams = z.infer<typeof ListParams>;
18
async function listBooks({ genre }: ListParams) {
19
  return db.filter((item) => item.genre === genre).map((item) => ({ name: item.name, id: item.id }));
20
}
21

22
const SearchParams = z.object({
23
  name: z.string(),
24
});
25
type SearchParams = z.infer<typeof SearchParams>;
26
async function searchBooks({ name }: SearchParams) {
27
  return db.filter((item) => item.name.includes(name)).map((item) => ({ name: item.name, id: item.id }));
28
}
29

30
const GetParams = z.object({
31
  id: z.string(),
32
});
33
type GetParams = z.infer<typeof GetParams>;
34
async function getBook({ id }: GetParams) {
35
  return db.find((item) => item.id === id)!;
36
}
37

38
async function main() {
39
  const runner = await openai.beta.chat.completions
40
    .runTools({
41
      model: 'gpt-4-1106-preview',
42
      stream: true,
43
      tools: [
44
        zodFunction({
45
          function: listBooks,
46
          schema: ListParams,
47
          description: 'List queries books by genre, and returns a list of names of books',
48
        }),
49
        zodFunction({
50
          function: searchBooks,
51
          schema: SearchParams,
52
          description: 'Search queries books by their name and returns a list of book names and their ids',
53
        }),
54
        zodFunction({
55
          function: getBook,
56
          schema: GetParams,
57
          description:
58
            "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.",
59
        }),
60
      ],
61
      messages: [
62
        {
63
          role: 'system',
64
          content:
65
            'Please use our book database, which you can access using functions to answer the following questions.',
66
        },
67
        {
68
          role: 'user',
69
          content:
70
            'I really enjoyed reading To Kill a Mockingbird, could you recommend me a book that is similar and tell me why?',
71
        },
72
      ],
73
    })
74
    .on('message', (msg) => console.log('msg', msg))
75
    .on('functionCall', (functionCall) => console.log('functionCall', functionCall))
76
    .on('functionCallResult', (functionCallResult) => console.log('functionCallResult', functionCallResult))
77
    .on('content', (diff) => process.stdout.write(diff));
78

79
  const result = await runner.finalChatCompletion();
80
  console.log();
81
  console.log('messages');
82
  console.log(runner.messages);
83

84
  console.log();
85
  console.log('final chat completion');
86
  console.dir(result, { depth: null });
87
}
88

89
const db = [
90
  {
91
    id: 'a1',
92
    name: 'To Kill a Mockingbird',
93
    genre: 'historical',
94
    description: `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.`,
95
  },
96
  {
97
    id: 'a2',
98
    name: 'All the Light We Cannot See',
99
    genre: 'historical',
100
    description: `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.`,
101
  },
102
  {
103
    id: 'a3',
104
    name: 'Where the Crawdads Sing',
105
    genre: 'historical',
106
    description: `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.
107
But 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.`,
108
  },
109
];
110

111
/**
112
 * A generic utility function that returns a RunnableFunction
113
 * you can pass to `.runTools()`,
114
 * with a fully validated, typesafe parameters schema.
115
 *
116
 * You are encouraged to copy/paste this into your codebase!
117
 */
118
function zodFunction<T extends object>({
119
  function: fn,
120
  schema,
121
  description = '',
122
  name,
123
}: {
124
  function: (args: T) => Promise<object>;
125
  schema: ZodSchema<T>;
126
  description?: string;
127
  name?: string;
128
}): RunnableToolFunctionWithParse<T> {
129
  return {
130
    type: 'function',
131
    function: {
132
      function: fn,
133
      name: name ?? fn.name,
134
      description: description,
135
      parameters: zodToJsonSchema(schema) as JSONSchema,
136
      parse(input: string): T {
137
        const obj = JSON.parse(input);
138
        return schema.parse(obj);
139
      },
140
    },
141
  };
142
}
143

144
main();
145

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.