Skip to content

Commit

Permalink
Dev UI test examples for auth and tools (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDoyle authored Jun 7, 2024
1 parent 5ca283b commit ca99e39
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 8 deletions.
3 changes: 3 additions & 0 deletions js/pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions js/testapps/dev-ui-gallery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@genkit-ai/flow": "workspace:*",
"@genkit-ai/googleai": "workspace:*",
"@genkit-ai/vertexai": "workspace:*",
"firebase-admin": "^12.1.0",
"genkitx-chromadb": "workspace:*",
"genkitx-ollama": "workspace:*",
"genkitx-pinecone": "workspace:*",
Expand Down
4 changes: 4 additions & 0 deletions js/testapps/dev-ui-gallery/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export const HelloFullNameSchema = z.object({
lastName: z.string(),
persona: z.string(),
});

export const WeatherSchema = z.object({
cities: z.array(z.string()),
});
3 changes: 2 additions & 1 deletion js/testapps/dev-ui-gallery/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export default configureGenkit({
],
});

export * from './main/durable-flows.js';
export * from './main/flows-durable.js';
export * from './main/flows-firebase-functions.js';
export * from './main/flows.js';
export * from './main/prompts.js';
export * from './main/tools.js';
75 changes: 75 additions & 0 deletions js/testapps/dev-ui-gallery/src/main/flows-firebase-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* 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 { generate } from '@genkit-ai/ai';
import { firebaseAuth } from '@genkit-ai/firebase/auth';
import { noAuth, onFlow } from '@genkit-ai/firebase/functions';
import { run } from '@genkit-ai/flow';
import { gemini15Flash } from '@genkit-ai/googleai';
import { DecodedIdToken } from 'firebase-admin/auth';
import * as z from 'zod';

export const flowAuth = onFlow(
{
name: 'flowAuth',
inputSchema: z.string(),
outputSchema: z.string(),
httpsOptions: {
cors: '*',
},
authPolicy: firebaseAuth((user: DecodedIdToken) => {
if (!user.email_verified && !user.admin) {
throw new Error('Auth failed - email not verified');
}
}),
},
async (language) => {
const prompt = `Say hello in language ${language}`;

return await run('call-llm', async () => {
const llmResponse = await generate({
model: gemini15Flash,
prompt: prompt,
});

return llmResponse.text();
});
}
);

export const flowAuthNone = onFlow(
{
name: 'flowAuthNone',
inputSchema: z.string(),
outputSchema: z.string(),
httpsOptions: {
cors: '*',
},
authPolicy: noAuth(),
},
async (language) => {
const prompt = `Say hello in language ${language}`;

return await run('call-llm', async () => {
const llmResponse = await generate({
model: gemini15Flash,
prompt: prompt,
});

return llmResponse.text();
});
}
);
6 changes: 3 additions & 3 deletions js/testapps/dev-ui-gallery/src/main/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { defineDotprompt, prompt } from '@genkit-ai/dotprompt';
import { defineFlow } from '@genkit-ai/flow';
import { geminiPro } from '@genkit-ai/googleai';
import { gemini15Flash } from '@genkit-ai/googleai';
import * as z from 'zod';
import { HelloFullNameSchema, HelloSchema } from '../common/types.js';

Expand All @@ -30,7 +30,7 @@ const template = 'Say hello to {{name}} in the voice of a {{persona}}.';
export const codeDefinedPrompt = defineDotprompt(
{
name: promptName,
model: geminiPro,
model: gemini15Flash,
input: {
schema: HelloSchema,
default: {
Expand Down Expand Up @@ -75,7 +75,7 @@ export const codeDefinedPromptVariant = defineDotprompt(
{
name: promptName,
variant: 'jsonOutput',
model: geminiPro,
model: gemini15Flash,
input: {
schema: HelloSchema,
default: {
Expand Down
64 changes: 60 additions & 4 deletions js/testapps/dev-ui-gallery/src/main/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/

import { defineTool } from '@genkit-ai/ai/tool';
import { defineDotprompt } from '@genkit-ai/dotprompt';
import { defineFlow } from '@genkit-ai/flow';
import { gemini15Flash } from '@genkit-ai/googleai';
import * as z from 'zod';
import { WeatherSchema } from '../common/types';

defineTool(
const getWeather = defineTool(
{
name: 'getWeather',
description: 'Get the weather for the given location.',
Expand All @@ -28,14 +32,18 @@ defineTool(
}),
},
async (input) => {
const conditions = ['Sunny', 'Cloudy', 'Partially Cloudy', 'Raining'];
const c = Math.floor(Math.random() * conditions.length);
const temp = Math.floor(Math.random() * (120 - 32) + 32);

return {
temperatureF: 70,
conditions: 'sunny',
temperatureF: temp,
conditions: conditions[c],
};
}
);

defineTool(
const getTime = defineTool(
{
name: 'getTime',
description: 'Get the current time',
Expand All @@ -46,3 +54,51 @@ defineTool(
return { time: Date.now() };
}
);

const template = `
{{role "system"}}
Always try to be as efficient as possible, and request tool calls in batches.
{{role "user"}}
Help me decide which is a better place to visit today based on the weather.
I want to be outside as much as possible. Here are the cities I am
considering:\n\n{{#each cities}}{{this}}\n{{/each}}`;

export const weatherPrompt = defineDotprompt(
{
name: 'weatherPrompt',
model: gemini15Flash,
input: {
schema: WeatherSchema,
default: {
persona: 'Space Pirate',
},
},
output: {
format: 'text',
},
config: {
maxOutputTokens: 2048,
temperature: 0.6,
topK: 16,
topP: 0.95,
},
tools: [getWeather],
},
template
);

defineFlow(
{
name: 'flowWeather',
inputSchema: WeatherSchema,
outputSchema: z.string(),
},
async (input) => {
const response = await weatherPrompt.generate({
input,
});

return response.text();
}
);

0 comments on commit ca99e39

Please sign in to comment.