Skip to content
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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions js/samples/docs-menu-generation/README.md
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:
Copy link
Member

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?"


```
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.
Binary file added js/samples/docs-menu-generation/menu.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions js/samples/docs-menu-generation/package.json
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"
}
}
106 changes: 106 additions & 0 deletions js/samples/docs-menu-generation/src/index.ts
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`,
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

@tagboola tagboola May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider passing the contents of the chunk to the streamingCallback if it's defined.

}
}

console.log((await response()).text());
}
);

let historyMap: MessageData[] = sampleMenuHistory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Drop the Map since it's an array

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();
52 changes: 52 additions & 0 deletions js/samples/docs-menu-generation/src/menuHistory.ts
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"',
},
],
},
];
14 changes: 14 additions & 0 deletions js/samples/docs-menu-generation/tsconfig.json
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
}
}