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

feat: add forceSchemaConstrainedTokens to KurtSamplingOptions #62

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/kurt/spec/Kurt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe("Kurt", () => {
maxOutputTokens: 1024,
temperature: 0.1,
topP: 0.5,
forceSchemaConstrainedTokens: true,
}
const { adapter, kurt } = createV1({ sampling })

Expand All @@ -45,6 +46,7 @@ describe("Kurt", () => {
maxOutputTokens: 1024,
temperature: 0.1,
topP: 0.5,
forceSchemaConstrainedTokens: true,
}
kurt.generateNaturalLanguage({ prompt: "Hello!", sampling })

Expand Down
19 changes: 19 additions & 0 deletions packages/kurt/src/Kurt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ export const KurtSamplingOptionsDefault = {
* without any "top tokens" filtering being applied before sampling.
*/
topP: 0.95,

/**
* When true, structured data generation and/or tool calls will always use
* schema-aware constrained token sampling, rather than conditionally
* doing so, or falling back to relying only on the LLM's training for
* responding with structured data schemas.
*
* This gives a hard guarantee that the output will match the schema,
* but for some underlying LLM providers this may introduce more constraints
* on the kinds of JSON Schemas that are supported, which explains why
* it might not be the default behavior for a particular adapter.
*
* If this feature is unavailable for a particular adapter, the adapter
* should throw a `KurtCapabilityError` when this option is set to true.
*
* If this feature is available without downsides, the adapter should silently
* enable this feature regardless of the option being set to true or false.
*/
forceSchemaConstrainedTokens: false,
}

export interface KurtGenerateNaturalLanguageOptions {
Expand Down
20 changes: 20 additions & 0 deletions packages/kurt/src/KurtError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ export abstract class KurtError extends Error {
abstract readonly adapter: KurtAdapter
}

/**
* Base class for all Kurt errors thrown for rejected requests.
*/
export abstract class KurtRequestError extends KurtError {}

/**
* Thrown by a Kurt adapter when the request tried to use a capability or
* feature that isn't supported by this particular adapter (but might be
* supported by other adapters, or by the same adapter class if instantiated
* with different configuration, such as selecting a different LLM snapshot).
*/
export class KurtCapabilityError extends KurtError {
constructor(
readonly adapter: KurtAdapter,
readonly missingCapability: string
) {
super(`This adapter doesn't support: ${missingCapability}`)
}
}

/**
* Base class for all Kurt errors thrown when handling a result stream.
*/
Expand Down
Loading