-
Notifications
You must be signed in to change notification settings - Fork 139
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 all 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,106 @@ | ||
/** | ||
* 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 { MessageData } from '@genkit-ai/ai/model'; | ||
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'; | ||
import { sampleMenuHistory } from './menuHistory'; | ||
|
||
configureGenkit({ | ||
plugins: [googleAI()], | ||
logLevel: 'debug', | ||
enableTracingAndMetrics: true, | ||
}); | ||
|
||
export const menuSuggestionFlowStreaming = defineFlow( | ||
{ | ||
name: 'menuSuggestionFlowStreaming', | ||
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 |
||
} | ||
} | ||
|
||
console.log((await response()).text()); | ||
} | ||
); | ||
|
||
let historyMap: MessageData[] = sampleMenuHistory; | ||
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. nit: Drop the |
||
export const menuHistoryFlow = defineFlow( | ||
{ | ||
name: 'menuHistoryFlow', | ||
inputSchema: z.string(), | ||
outputSchema: z.string(), | ||
}, | ||
async (subject) => { | ||
let history = historyMap; | ||
let response = await generate({ | ||
prompt: `Suggest a menu item description for a ${subject} themed restaurant`, | ||
model: geminiPro, | ||
history, | ||
}); | ||
historyMap = response.toHistory(); | ||
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,52 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import { MessageData } from '@genkit-ai/ai/model'; | ||
|
||
export const sampleMenuHistory: MessageData[] = [ | ||
{ | ||
role: 'user', | ||
content: [ | ||
{ | ||
text: 'Suggest a single menu item title of a truck themed restaurant', | ||
}, | ||
], | ||
}, | ||
{ | ||
role: 'model', | ||
content: [ | ||
{ | ||
text: '"Blazing Burnout Burger"', | ||
}, | ||
], | ||
}, | ||
{ | ||
role: 'user', | ||
content: [ | ||
{ | ||
text: "That's pretty good, but suggest another one that's spicier", | ||
}, | ||
], | ||
}, | ||
{ | ||
role: 'model', | ||
content: [ | ||
{ | ||
text: '"Redline Reaper Wrap"', | ||
}, | ||
], | ||
}, | ||
]; |
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?"