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

fix: make KurtVertexAI properly throw KurtCapabilityError for the schema-constrained tokens case #64

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
32 changes: 31 additions & 1 deletion packages/kurt-vertex-ai/spec/generateStructuredData.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { describe, test, expect } from "@jest/globals"
import { z } from "zod"
import { snapshotAndMock, snapshotAndMockWithError } from "./snapshots"
import { KurtResultValidateError } from "@formula-monks/kurt"
import {
KurtCapabilityError,
KurtResultValidateError,
} from "@formula-monks/kurt"

describe("KurtVertexAI generateStructuredData", () => {
test("says hello (response format 1)", async () => {
Expand Down Expand Up @@ -46,6 +49,33 @@ describe("KurtVertexAI generateStructuredData", () => {
expect(result.data).toEqual({ say: "hello" })
})

test("throws a capability error for schema constrained tokens", async () => {
await snapshotAndMockWithError(
(kurt) =>
kurt.generateStructuredData({
prompt: "Say hello!",
schema: z
.object({
say: z.string().describe("A single word to say"),
})
.describe("Say a word"),
sampling: {
// This is not available as a capability of Vertex AI.
forceSchemaConstrainedTokens: true,
},
}),

(errorAny) => {
expect(errorAny).toBeInstanceOf(KurtCapabilityError)
const error = errorAny as KurtCapabilityError
expect(error.missingCapability).toEqual(
"forceSchemaConstrainedTokens is not available for Vertex AI"
)
expect(error.message).toContain(error.missingCapability)
}
)
})

test("throws a validate error from an impossible schema", async () => {
await snapshotAndMockWithError(
(kurt) =>
Expand Down
8 changes: 8 additions & 0 deletions packages/kurt-vertex-ai/src/KurtVertexAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
KurtResultValidateError,
KurtResultLimitError,
KurtResultBlockedError,
KurtCapabilityError,
} from "@formula-monks/kurt"
import type {
VertexAI,
Expand Down Expand Up @@ -91,6 +92,13 @@ export class KurtVertexAI
model: this.options.model,
}) as VertexAIGenerativeModel

if (options.sampling.forceSchemaConstrainedTokens) {
throw new KurtCapabilityError(
this,
"forceSchemaConstrainedTokens is not available for Vertex AI"
)
}

// VertexAI requires that system messages be sent as a single message,
// so we filter them out from the main messages array to send separately.
const normalMessages = options.messages.filter((m) => m.role !== "system")
Expand Down
Loading