refactor(llm): move system helpers onto schema
This commit is contained in:
@@ -10,10 +10,10 @@ import {
|
||||
HttpOptions,
|
||||
LLMRequest,
|
||||
Message,
|
||||
SystemPart,
|
||||
ToolChoice,
|
||||
ToolDefinition,
|
||||
type ContentPart,
|
||||
type SystemPart,
|
||||
ToolCallPart,
|
||||
ToolResultPart,
|
||||
} from "./schema"
|
||||
@@ -50,12 +50,7 @@ export const limits = modelLimits
|
||||
|
||||
export const text = Message.text
|
||||
|
||||
export const system = (value: string): SystemPart => ({ type: "text", text: value })
|
||||
|
||||
const systemParts = (input?: string | SystemPart | ReadonlyArray<SystemPart>) => {
|
||||
if (input === undefined) return []
|
||||
return typeof input === "string" ? [system(input)] : Array.isArray(input) ? [...input] : [input]
|
||||
}
|
||||
export const system = SystemPart.make
|
||||
|
||||
export const message = Message.make
|
||||
|
||||
@@ -97,7 +92,7 @@ export const request = (input: RequestInput) => {
|
||||
} = input
|
||||
return new LLMRequest({
|
||||
...rest,
|
||||
system: systemParts(requestSystem),
|
||||
system: SystemPart.content(requestSystem),
|
||||
messages: [...(messages?.map(message) ?? []), ...(prompt === undefined ? [] : [user(prompt)])],
|
||||
tools: tools?.map(toolDefinition) ?? [],
|
||||
toolChoice: requestToolChoice ? toolChoice(requestToolChoice) : undefined,
|
||||
|
||||
@@ -253,13 +253,23 @@ export class CacheHint extends Schema.Class<CacheHint>("LLM.CacheHint")({
|
||||
ttlSeconds: Schema.optional(Schema.Number),
|
||||
}) {}
|
||||
|
||||
export const SystemPart = Schema.Struct({
|
||||
const systemPartSchema = Schema.Struct({
|
||||
type: Schema.Literal("text"),
|
||||
text: Schema.String,
|
||||
cache: Schema.optional(CacheHint),
|
||||
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
||||
}).annotate({ identifier: "LLM.SystemPart" })
|
||||
export type SystemPart = Schema.Schema.Type<typeof SystemPart>
|
||||
export type SystemPart = Schema.Schema.Type<typeof systemPartSchema>
|
||||
|
||||
const makeSystemPart = (text: string): SystemPart => ({ type: "text", text })
|
||||
|
||||
export const SystemPart = Object.assign(systemPartSchema, {
|
||||
make: makeSystemPart,
|
||||
content: (input?: string | SystemPart | ReadonlyArray<SystemPart>) => {
|
||||
if (input === undefined) return []
|
||||
return typeof input === "string" ? [makeSystemPart(input)] : Array.isArray(input) ? [...input] : [input]
|
||||
},
|
||||
})
|
||||
|
||||
export const TextPart = Schema.Struct({
|
||||
type: Schema.Literal("text"),
|
||||
|
||||
Reference in New Issue
Block a user