-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Menu/generation example #107
Changes from 8 commits
ab6b18b
5142959
02b73a6
0c1f56a
ef1761f
f544ae8
4857781
da2cc0e
fd4401f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Build it | ||
|
||
``` | ||
pnpm build | ||
``` | ||
|
||
or if you need to, build everything: | ||
|
||
``` | ||
cd </path/to/genkit>; pnpm run setup; cd - | ||
``` | ||
|
||
where `</path/to/genkit>` is the top level of the genkit repo. | ||
|
||
## Run the flows via cli | ||
|
||
``` | ||
genkit flow:run menuStreamingFlow '"astronauts"' | ||
genkit flow:run menuHistoryFlow '"cats"' | ||
genkit flow:run menuImageFlow '{"imageUrl": "https://raw.githubusercontent.com/firebase/genkit/main/js/samples/docs-menu-generation/menu.jpeg", "subject": "tiger"}' | ||
``` | ||
|
||
## Run the flow in the Developer UI | ||
|
||
``` | ||
genkit start | ||
``` | ||
|
||
Click on `menuHistoryFlow`, `menuStreamingFlow`, or `menuImageFlow` | ||
in the lefthand navigation panel to run the new flows. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "generation", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node lib/index.js", | ||
"compile": "tsc", | ||
"build": "npm run build:clean && npm run compile", | ||
"build:clean": "rm -rf ./lib", | ||
"build:watch": "tsc --watch" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@genkit-ai/ai": "workspace:*", | ||
"@genkit-ai/core": "workspace:*", | ||
"@genkit-ai/firebase": "workspace:*", | ||
"@genkit-ai/flow": "workspace:*", | ||
"@genkit-ai/googleai": "workspace:*", | ||
"express": "^4.19.2", | ||
"zod": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.3.3" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// This sample is referenced by the genkit docs. Changes should be made to | ||
// both. | ||
import { generate, generateStream } from '@genkit-ai/ai'; | ||
import { configureGenkit } from '@genkit-ai/core'; | ||
import { defineFlow, startFlowsServer } from '@genkit-ai/flow'; | ||
import { geminiPro, geminiProVision, googleAI } from '@genkit-ai/googleai'; | ||
import * as z from 'zod'; | ||
|
||
configureGenkit({ | ||
plugins: [googleAI()], | ||
logLevel: 'debug', | ||
enableTracingAndMetrics: true, | ||
}); | ||
|
||
export const menuStreamingSuggestionFlow = defineFlow( | ||
MichaelDoyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
name: 'menuStreamingSuggestionFlow', | ||
MichaelDoyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inputSchema: z.string(), | ||
outputSchema: z.void(), | ||
}, | ||
async (subject) => { | ||
const { response, stream } = await generateStream({ | ||
prompt: `Suggest many items for the menu of a ${subject} themed restaurant`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe specify exactly how many, or provide a range? |
||
model: geminiPro, | ||
config: { | ||
temperature: 1, | ||
}, | ||
}); | ||
|
||
for await (let chunk of stream()) { | ||
for (let content of chunk.content) { | ||
console.log(content.text); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider passing the contents of the chunk to the |
||
} | ||
} | ||
|
||
// you can also await the full response | ||
console.log((await response()).text()); | ||
} | ||
); | ||
|
||
export const menuHistoryFlow = defineFlow( | ||
{ | ||
name: 'menuHistoryFlow', | ||
inputSchema: z.string(), | ||
outputSchema: z.string(), | ||
}, | ||
async (subject) => { | ||
// lets do few-shot examples: generate a few different generations, keep adding the history | ||
// before generating an item for the menu of a themed restaurant | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is actually going to call the model, it's probably not a good example of a "few-shot" prompt, which would typically have hardcoded examples. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks more like a pre-scripted chat exchange, but will still be non-deterministic. We should probably talk through more what you want to be the outcome here. |
||
let response = await generate({ | ||
prompt: `Create examples of delicious menu entrees`, | ||
model: geminiPro, | ||
}); | ||
let history = response.toHistory(); | ||
|
||
response = await generate({ | ||
prompt: `Chicken is my favorite meat`, | ||
model: geminiPro, | ||
history, | ||
}); | ||
history = response.toHistory(); | ||
|
||
response = await generate({ | ||
prompt: `Suggest an item for the menu of a ${subject} themed restaurant`, | ||
model: geminiPro, | ||
history, | ||
}); | ||
return response.text(); | ||
} | ||
); | ||
|
||
export const menuImageFlow = defineFlow( | ||
{ | ||
name: 'menuImageFlow', | ||
inputSchema: z.object({ imageUrl: z.string(), subject: z.string() }), | ||
outputSchema: z.string(), | ||
}, | ||
async (input) => { | ||
const visionResponse = await generate({ | ||
model: geminiProVision, | ||
prompt: [ | ||
{ | ||
text: `Extract _all_ of the text, in order, | ||
from the following image of a restaurant menu.`, | ||
}, | ||
{ media: { url: input.imageUrl, contentType: 'image/jpeg' } }, | ||
], | ||
}); | ||
const imageDescription = visionResponse.text(); | ||
|
||
const response = await generate({ | ||
model: geminiPro, | ||
prompt: `Here is the text of today's menu: ${imageDescription} | ||
Rename the items to match the restaurant's ${input.subject} theme.`, | ||
}); | ||
|
||
return response.text(); | ||
} | ||
); | ||
|
||
startFlowsServer(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compileOnSave": true, | ||
"include": ["src"], | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"noImplicitReturns": true, | ||
"outDir": "lib", | ||
"sourceMap": true, | ||
"strict": true, | ||
"target": "es2017", | ||
"skipLibCheck": true, | ||
"esModuleInterop": true | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can set this up as a prerequisite step (build the entire repo first) that we note can be skipped if already done. otherwise, how does one answer the question "build if you need to?"