|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import openAI from '@genkit-ai/compat-oai/openai'; |
| 18 | +import * as fs from 'fs'; |
| 19 | +import { genkit, z } from 'genkit'; |
| 20 | +import wav from 'wav'; |
| 21 | + |
| 22 | +const ai = genkit({ |
| 23 | + plugins: [ |
| 24 | + // Provide the key via the OPENAI_API_KEY environment variable |
| 25 | + openAI(), |
| 26 | + ], |
| 27 | +}); |
| 28 | + |
| 29 | +ai.defineFlow('basic-hi', async () => { |
| 30 | + const { text } = await ai.generate({ |
| 31 | + model: openAI.model('o4-mini'), |
| 32 | + prompt: 'You are a helpful AI assistant named Walt, say hello', |
| 33 | + }); |
| 34 | + |
| 35 | + return text; |
| 36 | +}); |
| 37 | + |
| 38 | +// Multimodal input |
| 39 | +ai.defineFlow('multimodal-input', async () => { |
| 40 | + const photoBase64 = fs.readFileSync('photo.jpg', { encoding: 'base64' }); |
| 41 | + |
| 42 | + const { text } = await ai.generate({ |
| 43 | + model: openAI.model('gpt-4o'), |
| 44 | + prompt: [ |
| 45 | + { text: 'describe this photo' }, |
| 46 | + { |
| 47 | + media: { |
| 48 | + contentType: 'image/jpeg', |
| 49 | + url: `data:image/jpeg;base64,${photoBase64}`, |
| 50 | + }, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }); |
| 54 | + |
| 55 | + return text; |
| 56 | +}); |
| 57 | + |
| 58 | +// Streaming |
| 59 | +ai.defineFlow('streaming', async (_, { sendChunk }) => { |
| 60 | + const { stream } = ai.generateStream({ |
| 61 | + model: openAI.model('gpt-4o'), |
| 62 | + prompt: 'Write a poem about AI.', |
| 63 | + }); |
| 64 | + |
| 65 | + let poem = ''; |
| 66 | + for await (const chunk of stream) { |
| 67 | + poem += chunk.text; |
| 68 | + sendChunk(chunk.text); |
| 69 | + } |
| 70 | + |
| 71 | + return poem; |
| 72 | +}); |
| 73 | + |
| 74 | +// Web search |
| 75 | +ai.defineFlow('web-search', async () => { |
| 76 | + const response = await ai.generate({ |
| 77 | + model: openAI.model('gpt-4o-search-preview'), |
| 78 | + prompt: 'Who is Albert Einstein?', |
| 79 | + config: { |
| 80 | + web_search_options: {}, |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + return { |
| 85 | + text: response.text, |
| 86 | + annotations: (response.raw as any)?.choices?.[0].message.annotations, |
| 87 | + }; |
| 88 | +}); |
| 89 | + |
| 90 | +const getWeather = ai.defineTool( |
| 91 | + { |
| 92 | + name: 'getWeather', |
| 93 | + inputSchema: z.object({ |
| 94 | + location: z |
| 95 | + .string() |
| 96 | + .describe( |
| 97 | + 'Location for which to get the weather, ex: San-Francisco, CA' |
| 98 | + ), |
| 99 | + }), |
| 100 | + description: 'can be used to calculate gablorken value', |
| 101 | + }, |
| 102 | + async (input) => { |
| 103 | + // pretend we call an actual API |
| 104 | + return { |
| 105 | + location: input.location, |
| 106 | + temperature_celcius: 21.5, |
| 107 | + conditions: 'cloudy', |
| 108 | + }; |
| 109 | + } |
| 110 | +); |
| 111 | + |
| 112 | +// Tool calling |
| 113 | +ai.defineFlow( |
| 114 | + { |
| 115 | + name: 'tool-calling', |
| 116 | + inputSchema: z.string().default('Paris, France'), |
| 117 | + outputSchema: z.string(), |
| 118 | + streamSchema: z.any(), |
| 119 | + }, |
| 120 | + async (location, { sendChunk }) => { |
| 121 | + const { response, stream } = ai.generateStream({ |
| 122 | + model: openAI.model('gpt-4o'), |
| 123 | + config: { |
| 124 | + temperature: 1, |
| 125 | + }, |
| 126 | + tools: [getWeather], |
| 127 | + prompt: `tell what's the weather in ${location} (in Fahrenheit)`, |
| 128 | + }); |
| 129 | + |
| 130 | + for await (const chunk of stream) { |
| 131 | + sendChunk(chunk); |
| 132 | + } |
| 133 | + |
| 134 | + return (await response).text; |
| 135 | + } |
| 136 | +); |
| 137 | + |
| 138 | +const RpgCharacterSchema = z.object({ |
| 139 | + name: z.string().describe('name of the character'), |
| 140 | + backstory: z.string().describe("character's backstory, about a paragraph"), |
| 141 | + weapons: z.array(z.string()), |
| 142 | + class: z.enum(['RANGER', 'WIZZARD', 'TANK', 'HEALER', 'ENGINEER']), |
| 143 | +}); |
| 144 | + |
| 145 | +// A simple example of structured output. |
| 146 | +ai.defineFlow( |
| 147 | + { |
| 148 | + name: 'structured-output', |
| 149 | + inputSchema: z.string().default('Glorb'), |
| 150 | + outputSchema: RpgCharacterSchema, |
| 151 | + }, |
| 152 | + async (name, { sendChunk }) => { |
| 153 | + const { response, stream } = ai.generateStream({ |
| 154 | + model: openAI.model('gpt-4o'), |
| 155 | + config: { |
| 156 | + temperature: 1, // we want creativity |
| 157 | + }, |
| 158 | + output: { schema: RpgCharacterSchema }, |
| 159 | + prompt: `Generate an RPC character called ${name}`, |
| 160 | + }); |
| 161 | + |
| 162 | + for await (const chunk of stream) { |
| 163 | + sendChunk(chunk.output); |
| 164 | + } |
| 165 | + |
| 166 | + return (await response).output!; |
| 167 | + } |
| 168 | +); |
| 169 | + |
| 170 | +// Image generation. |
| 171 | +ai.defineFlow('dall-e-image-generation', async (_, { sendChunk }) => { |
| 172 | + const { media } = await ai.generate({ |
| 173 | + model: openAI.model('dall-e-3'), |
| 174 | + prompt: `generate an image of a banana riding bicycle`, |
| 175 | + }); |
| 176 | + |
| 177 | + return media; |
| 178 | +}); |
| 179 | + |
| 180 | +// TTS sample |
| 181 | +ai.defineFlow( |
| 182 | + { |
| 183 | + name: 'tts', |
| 184 | + inputSchema: z.string().default('Genkit is an amazing Gen AI library'), |
| 185 | + outputSchema: z.object({ media: z.string() }), |
| 186 | + }, |
| 187 | + async (query) => { |
| 188 | + const { media } = await ai.generate({ |
| 189 | + model: openAI.model('gpt-4o-mini-tts'), |
| 190 | + config: { |
| 191 | + voice: 'sage', |
| 192 | + }, |
| 193 | + prompt: query, |
| 194 | + }); |
| 195 | + if (!media) { |
| 196 | + throw new Error('no media returned'); |
| 197 | + } |
| 198 | + const audioBuffer = Buffer.from( |
| 199 | + media.url.substring(media.url.indexOf(',') + 1), |
| 200 | + 'base64' |
| 201 | + ); |
| 202 | + return { |
| 203 | + media: 'data:audio/wav;base64,' + (await toWav(audioBuffer)), |
| 204 | + }; |
| 205 | + } |
| 206 | +); |
| 207 | + |
| 208 | +async function toWav( |
| 209 | + pcmData: Buffer, |
| 210 | + channels = 1, |
| 211 | + rate = 24000, |
| 212 | + sampleWidth = 2 |
| 213 | +): Promise<string> { |
| 214 | + return new Promise((resolve, reject) => { |
| 215 | + // This code depends on `wav` npm library. |
| 216 | + const writer = new wav.Writer({ |
| 217 | + channels, |
| 218 | + sampleRate: rate, |
| 219 | + bitDepth: sampleWidth * 8, |
| 220 | + }); |
| 221 | + |
| 222 | + let bufs = [] as any[]; |
| 223 | + writer.on('error', reject); |
| 224 | + writer.on('data', function (d) { |
| 225 | + bufs.push(d); |
| 226 | + }); |
| 227 | + writer.on('end', function () { |
| 228 | + resolve(Buffer.concat(bufs).toString('base64')); |
| 229 | + }); |
| 230 | + |
| 231 | + writer.write(pcmData); |
| 232 | + writer.end(); |
| 233 | + }); |
| 234 | +} |
0 commit comments