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 8 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"
}
}
117 changes: 117 additions & 0 deletions js/samples/docs-menu-generation/src/index.ts
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`,
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.

}
}

// 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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

@MichaelDoyle MichaelDoyle May 10, 2024

Choose a reason for hiding this comment

The 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();
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
}
}