feat(ai): infer model provider options (#39493)
This commit is contained in:
parent
d9555f138b
commit
309c4fe6f0
5 changed files with 125 additions and 33 deletions
62
packages/ai/test/llm-option-types.types.ts
Normal file
62
packages/ai/test/llm-option-types.types.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import { Schema } from "effect"
|
||||
import { LLM, Model, type ModelProviderOptions, type ProviderOptions } from "../src"
|
||||
import { OpenAIChat } from "../src/protocols"
|
||||
|
||||
interface ExampleOptions {
|
||||
readonly [key: string]: unknown
|
||||
readonly mode?: "fast" | "thorough"
|
||||
}
|
||||
|
||||
type ExampleProviderOptions = ProviderOptions & {
|
||||
readonly example?: ExampleOptions
|
||||
}
|
||||
|
||||
const model = OpenAIChat.route
|
||||
.with({ endpoint: { baseURL: "https://example.com/v1" } })
|
||||
.model<ExampleProviderOptions>({ id: "example" })
|
||||
|
||||
LLM.request({ model, prompt: "Hello", providerOptions: { example: { mode: "fast" } } })
|
||||
LLM.request({ model, prompt: "Hello", providerOptions: { future: { option: true } } })
|
||||
|
||||
LLM.request({
|
||||
model,
|
||||
prompt: "Hello",
|
||||
// @ts-expect-error Known provider options preserve their value types.
|
||||
providerOptions: { example: { mode: "slow" } },
|
||||
})
|
||||
|
||||
LLM.generateObject({
|
||||
model,
|
||||
prompt: "Hello",
|
||||
schema: Schema.Struct({ answer: Schema.String }),
|
||||
providerOptions: { example: { mode: "thorough" } },
|
||||
})
|
||||
|
||||
LLM.generateObject({
|
||||
model,
|
||||
prompt: "Hello",
|
||||
jsonSchema: { type: "object" },
|
||||
// @ts-expect-error Dynamic object generation uses the selected model's provider options.
|
||||
providerOptions: { example: { mode: false } },
|
||||
})
|
||||
|
||||
declare const generic: Model
|
||||
LLM.request({ model: generic, prompt: "Hello", providerOptions: { arbitrary: { option: true } } })
|
||||
|
||||
const options: ModelProviderOptions<typeof model> = { example: { mode: "fast" } }
|
||||
void options
|
||||
|
||||
model.route.model<ExampleProviderOptions>({
|
||||
id: "example-with-defaults",
|
||||
defaults: {
|
||||
// @ts-expect-error Low-level model defaults preserve known provider option types.
|
||||
providerOptions: { example: { mode: 1 } },
|
||||
},
|
||||
})
|
||||
|
||||
Model.update(model, {
|
||||
defaults: {
|
||||
// @ts-expect-error Updating a model cannot contradict its provider option type.
|
||||
providerOptions: { example: { mode: "slow" } },
|
||||
},
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue