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

Makes inputSchema optional for tools. #822

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 9 additions & 5 deletions js/ai/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

import {
Action,
GenkitError,
StreamingCallback,
config as genkitConfig,
GenkitError,
runWithStreamingCallback,
StreamingCallback,
} from '@genkit-ai/core';
import { lookupAction } from '@genkit-ai/core/registry';
import { toJsonSchema, validateSchema } from '@genkit-ai/core/schema';
import { z } from 'zod';
import { DocumentData } from './document.js';
import { extractJson } from './extract.js';
import {
GenerateUtilParamSchema,
generateAction,
GenerateUtilParamSchema,
inferRoleFromParts,
} from './generateAction.js';
import {
Expand All @@ -47,7 +47,7 @@ import {
ToolRequestPart,
ToolResponsePart,
} from './model.js';
import { ToolArgument, resolveTools, toToolDefinition } from './tool.js';
import { resolveTools, ToolArgument, toToolDefinition } from './tool.js';

/**
* Message represents a single role's contribution to a generation. Each message
Expand Down Expand Up @@ -610,7 +610,11 @@ export async function generate<

return await runWithStreamingCallback(
resolvedOptions.streamingCallback,
async () => new GenerateResponse<O>(await generateAction(params))
async () =>
new GenerateResponse<O>(
mbleigh marked this conversation as resolved.
Show resolved Hide resolved
await generateAction(params),
await toGenerateRequest(resolvedOptions)
)
);
}

Expand Down
5 changes: 3 additions & 2 deletions js/ai/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,12 @@ export const ToolDefinitionSchema = z.object({
description: z.string(),
inputSchema: z
.record(z.any())
.describe('Valid JSON Schema representing the input of the tool.'),
.describe('Valid JSON Schema representing the input of the tool.')
.nullish(),
outputSchema: z
.record(z.any())
.describe('Valid JSON Schema describing the output of the tool.')
.optional(),
.nullish(),
});
export type ToolDefinition = z.infer<typeof ToolDefinitionSchema>;

Expand Down
2 changes: 1 addition & 1 deletion js/plugins/vertexai/src/openai_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function toOpenAiTool(tool: ToolDefinition): ChatCompletionTool {
type: 'function',
function: {
name: tool.name,
parameters: tool.inputSchema,
parameters: tool.inputSchema || undefined,
},
};
}
Expand Down
39 changes: 37 additions & 2 deletions js/testapps/flow-simple-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { generate, generateStream, retrieve } from '@genkit-ai/ai';
import { defineTool } from '@genkit-ai/ai/tool';
import { defineTool, generate, generateStream, retrieve } from '@genkit-ai/ai';
import { configureGenkit } from '@genkit-ai/core';
import { dotprompt, prompt } from '@genkit-ai/dotprompt';
import { defineFirestoreRetriever, firebase } from '@genkit-ai/firebase';
Expand Down Expand Up @@ -429,6 +428,7 @@ export const invalidOutput = defineFlow(
}
);

import { MessageSchema } from '@genkit-ai/ai/model';
import { GoogleAIFileManager } from '@google/generative-ai/server';
const fileManager = new GoogleAIFileManager(
process.env.GOOGLE_GENAI_API_KEY || process.env.GOOGLE_API_KEY!
Expand Down Expand Up @@ -465,3 +465,38 @@ export const fileApi = defineFlow(
return result.text();
}
);

export const testTools = [
// test a tool with no input / output schema
defineTool(
{ name: 'getColor', description: 'gets a random color' },
async () => {
const colors = [
'red',
'orange',
'yellow',
'blue',
'green',
'indigo',
'violet',
];
return colors[Math.floor(Math.random() * colors.length)];
}
),
];

export const toolTester = defineFlow(
{
name: 'toolTester',
inputSchema: z.string(),
outputSchema: z.array(MessageSchema),
},
async (query) => {
const result = await generate({
model: gemini15Flash,
prompt: query,
tools: testTools,
});
return result.toHistory();
}
);
Loading