Skip to content

Commit

Permalink
chore: fixes dev-ui-gallery sample (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaroneee authored Oct 14, 2024
1 parent 565404f commit 8c5d2dd
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 124 deletions.
6 changes: 6 additions & 0 deletions js/pnpm-lock.yaml

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

6 changes: 5 additions & 1 deletion js/testapps/dev-ui-gallery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"main": "lib/index.js",
"scripts": {
"start": "node lib/index.js",
"dev": "tsx --watch src/index.ts",
"genkit:dev": "cross-env GENKIT_ENV=dev pnpm dev",
"compile": "tsc",
"build": "pnpm build:clean && pnpm compile",
"build:clean": "rimraf ./lib",
Expand All @@ -15,17 +17,19 @@
"author": "Google, LLC",
"license": "ISC",
"devDependencies": {
"cross-env": "^7.0.3",
"rimraf": "^6.0.1",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
},
"dependencies": {
"genkit": "workspace:*",
"@genkit-ai/dev-local-vectorstore": "workspace:*",
"@genkit-ai/evaluator": "workspace:*",
"@genkit-ai/firebase": "workspace:*",
"@genkit-ai/googleai": "workspace:*",
"@genkit-ai/vertexai": "workspace:*",
"firebase-admin": "^12.1.0",
"genkit": "workspace:*",
"genkitx-chromadb": "workspace:*",
"genkitx-ollama": "workspace:*",
"genkitx-pinecone": "workspace:*"
Expand Down
132 changes: 132 additions & 0 deletions js/testapps/dev-ui-gallery/src/genkit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* 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 { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
import { genkitEval, GenkitMetric } from '@genkit-ai/evaluator';
import { geminiPro, googleAI } from '@genkit-ai/googleai';
import {
claude3Haiku,
claude3Opus,
claude3Sonnet,
textEmbeddingGecko,
vertexAI,
VertexAIEvaluationMetricType,
} from '@genkit-ai/vertexai';
import { dotprompt, genkit } from 'genkit';
import { chroma } from 'genkitx-chromadb';
import { ollama } from 'genkitx-ollama';
import { pinecone } from 'genkitx-pinecone';

// Turn off safety checks for evaluation so that the LLM as an evaluator can
// respond appropriately to potentially harmful content without error.
export const PERMISSIVE_SAFETY_SETTINGS: any = {
safetySettings: [
{
category: 'HARM_CATEGORY_HATE_SPEECH',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_HARASSMENT',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
threshold: 'BLOCK_NONE',
},
],
};

export const ai = genkit({
// load at least one plugin representing each action type
plugins: [
// model providers
googleAI({
apiVersion: ['v1', 'v1beta'],
}),
ollama({
models: [
{ name: 'llama2' },
{ name: 'llama3' },
{
name: 'gemma',
type: 'generate',
},
],
serverAddress: 'http://127.0.0.1:11434', // default local address
}),
vertexAI({
location: 'us-central1',
modelGardenModels: [claude3Haiku, claude3Sonnet, claude3Opus],
evaluation: {
metrics: [
VertexAIEvaluationMetricType.BLEU,
VertexAIEvaluationMetricType.GROUNDEDNESS,
VertexAIEvaluationMetricType.SAFETY,
{
type: VertexAIEvaluationMetricType.ROUGE,
metricSpec: {
rougeType: 'rougeLsum',
useStemmer: true,
splitSummaries: 'true',
},
},
],
},
}),

// vector stores
chroma([
{
collectionName: 'chroma-collection',
embedder: textEmbeddingGecko,
embedderOptions: { taskType: 'RETRIEVAL_DOCUMENT' },
},
]),
devLocalVectorstore([
{
indexName: 'naive-index',
embedder: textEmbeddingGecko,
embedderOptions: { taskType: 'RETRIEVAL_DOCUMENT' },
},
]),
pinecone([
{
indexId: 'pinecone-index',
embedder: textEmbeddingGecko,
embedderOptions: { taskType: 'RETRIEVAL_DOCUMENT' },
},
]),

// evaluation
genkitEval({
judge: geminiPro,
judgeConfig: PERMISSIVE_SAFETY_SETTINGS,
embedder: textEmbeddingGecko,
metrics: [
GenkitMetric.ANSWER_RELEVANCY,
GenkitMetric.FAITHFULNESS,
GenkitMetric.MALICIOUSNESS,
],
}),

// prompt files
dotprompt({ dir: './prompts' }),
],
});
117 changes: 0 additions & 117 deletions js/testapps/dev-ui-gallery/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,123 +14,6 @@
* limitations under the License.
*/

import { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
import { genkitEval, GenkitMetric } from '@genkit-ai/evaluator';
import { geminiPro, googleAI } from '@genkit-ai/googleai';
import {
claude3Haiku,
claude3Opus,
claude3Sonnet,
textEmbeddingGecko,
vertexAI,
VertexAIEvaluationMetricType,
} from '@genkit-ai/vertexai';
import { dotprompt, genkit } from 'genkit';
import { chroma } from 'genkitx-chromadb';
import { ollama } from 'genkitx-ollama';
import { pinecone } from 'genkitx-pinecone';

// Turn off safety checks for evaluation so that the LLM as an evaluator can
// respond appropriately to potentially harmful content without error.
export const PERMISSIVE_SAFETY_SETTINGS: any = {
safetySettings: [
{
category: 'HARM_CATEGORY_HATE_SPEECH',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_HARASSMENT',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
threshold: 'BLOCK_NONE',
},
],
};

export const ai = genkit({
// load at least one plugin representing each action type
plugins: [
// model providers
googleAI({
apiVersion: ['v1', 'v1beta'],
}),
ollama({
models: [
{ name: 'llama2' },
{ name: 'llama3' },
{
name: 'gemma',
type: 'generate',
},
],
serverAddress: 'http://127.0.0.1:11434', // default local address
}),
vertexAI({
location: 'us-central1',
modelGardenModels: [claude3Haiku, claude3Sonnet, claude3Opus],
evaluation: {
metrics: [
VertexAIEvaluationMetricType.BLEU,
VertexAIEvaluationMetricType.GROUNDEDNESS,
VertexAIEvaluationMetricType.SAFETY,
{
type: VertexAIEvaluationMetricType.ROUGE,
metricSpec: {
rougeType: 'rougeLsum',
useStemmer: true,
splitSummaries: 'true',
},
},
],
},
}),

// vector stores
chroma([
{
collectionName: 'chroma-collection',
embedder: textEmbeddingGecko,
embedderOptions: { taskType: 'RETRIEVAL_DOCUMENT' },
},
]),
devLocalVectorstore([
{
indexName: 'naive-index',
embedder: textEmbeddingGecko,
embedderOptions: { taskType: 'RETRIEVAL_DOCUMENT' },
},
]),
pinecone([
{
indexId: 'pinecone-index',
embedder: textEmbeddingGecko,
embedderOptions: { taskType: 'RETRIEVAL_DOCUMENT' },
},
]),

// evaluation
genkitEval({
judge: geminiPro,
judgeConfig: PERMISSIVE_SAFETY_SETTINGS,
embedder: textEmbeddingGecko,
metrics: [
GenkitMetric.ANSWER_RELEVANCY,
GenkitMetric.FAITHFULNESS,
GenkitMetric.MALICIOUSNESS,
],
}),

// prompt files
dotprompt({ dir: './prompts' }),
],
});

export * from './main/flows-firebase-functions.js';
export * from './main/flows.js';
export * from './main/prompts.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { noAuth, onFlow } from '@genkit-ai/firebase/functions';
import { gemini15Flash } from '@genkit-ai/googleai';
import { DecodedIdToken } from 'firebase-admin/auth';
import { run, z } from 'genkit';
import { ai } from '../index.js';
import { ai } from '../genkit.js';

export const flowBasicAuth = ai.defineFlow(
{
Expand Down
2 changes: 1 addition & 1 deletion js/testapps/dev-ui-gallery/src/main/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { run, z } from 'genkit';
import { generateString } from '../common/util';
import { ai } from '../index.js';
import { ai } from '../genkit.js';

//
// Flow - simple
Expand Down
2 changes: 1 addition & 1 deletion js/testapps/dev-ui-gallery/src/main/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { gemini15Flash } from '@genkit-ai/googleai';
import { promptRef, z } from 'genkit';
import { HelloFullNameSchema, HelloSchema } from '../common/types.js';
import { ai } from '../index.js';
import { ai } from '../genkit.js';

//
// Prompt defined in code, subsequently loaded into a flow, plus an additional variant.
Expand Down
6 changes: 3 additions & 3 deletions js/testapps/dev-ui-gallery/src/main/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { gemini15Flash } from '@genkit-ai/googleai';
import { z } from 'genkit';
import { WeatherSchema } from '../common/types';
import { ai } from '../index.js';
import { ai } from '../genkit.js';

ai.defineTool(
{
Expand Down Expand Up @@ -58,8 +58,8 @@ const template = `
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
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}}`;

const weatherPrompt = ai.definePrompt(
Expand Down

0 comments on commit 8c5d2dd

Please sign in to comment.