|
| 1 | +import type {ProviderV2} from '@ai-sdk/provider' |
| 2 | +import {jsonSchema} from '@ai-sdk/provider-utils' |
| 3 | +import templite from 'templite' |
| 4 | +import invariant from 'tiny-invariant' |
| 5 | +import {z} from 'zod/v4' |
| 6 | + |
| 7 | +import {githubModels} from './provider' |
| 8 | + |
| 9 | +type Variables = Record<string, string | number | boolean> |
| 10 | + |
| 11 | +export function crateTextPrompt(config: unknown, provider?: ProviderV2) { |
| 12 | + const p = schema.parse(config) |
| 13 | + if (p.responseFormat) invariant(p.responseFormat === 'text', 'responseFormat must be "text" to generate text') |
| 14 | + |
| 15 | + provider ||= githubModels |
| 16 | + |
| 17 | + const model = provider.languageModel(p.model) |
| 18 | + |
| 19 | + return <V extends Variables>(variables: V) => { |
| 20 | + return { |
| 21 | + model, |
| 22 | + messages: messages(p, variables), |
| 23 | + ...settings(p), |
| 24 | + } as const |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export function createObjectPrompt(config: unknown, provider?: ProviderV2) { |
| 29 | + const p = schema.parse(config) |
| 30 | + invariant( |
| 31 | + p.responseFormat === 'json_object' || p.responseFormat === 'json_schema', |
| 32 | + 'responseFormat must be either "json_object" or "json_schema" to generate objects', |
| 33 | + ) |
| 34 | + |
| 35 | + provider ||= githubModels |
| 36 | + const model = provider.languageModel(p.model) |
| 37 | + |
| 38 | + let schemaProperties = {} |
| 39 | + if (p.responseFormat === 'json_schema' && p.jsonSchema) { |
| 40 | + const jSchema = JSON.parse(p.jsonSchema) |
| 41 | + schemaProperties = { |
| 42 | + output: undefined, |
| 43 | + schema: jsonSchema(jSchema.schema), |
| 44 | + schemaName: jSchema.name, |
| 45 | + schemaDescription: jSchema.description, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return <V extends Variables>(variables: V) => { |
| 50 | + return { |
| 51 | + model, |
| 52 | + messages: messages(p, variables), |
| 53 | + output: 'no-schema', |
| 54 | + ...schemaProperties, |
| 55 | + ...settings(p), |
| 56 | + } as const |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// --- |
| 61 | + |
| 62 | +function messages<V extends Variables>(config: Schema, variables: V) { |
| 63 | + const msgs = [] as Message[] |
| 64 | + for (const msg of config.messages) { |
| 65 | + msgs.push({ |
| 66 | + role: msg.role, |
| 67 | + content: templite(msg.content, variables), |
| 68 | + }) |
| 69 | + } |
| 70 | + return msgs |
| 71 | +} |
| 72 | + |
| 73 | +function settings(config: Schema) { |
| 74 | + return { |
| 75 | + maxOutputTokens: config.modelParameters?.maxTokens, |
| 76 | + temperature: config.modelParameters?.temperature, |
| 77 | + topP: config.modelParameters?.topP, |
| 78 | + } as const |
| 79 | +} |
| 80 | + |
| 81 | +const message = z.discriminatedUnion('role', [ |
| 82 | + z.object({role: z.literal('user'), content: z.string()}), |
| 83 | + z.object({role: z.literal('system'), content: z.string()}), |
| 84 | +]) |
| 85 | + |
| 86 | +type Message = z.infer<typeof message> |
| 87 | + |
| 88 | +const schema = z |
| 89 | + .object({ |
| 90 | + name: z.string().optional(), |
| 91 | + description: z.string().optional(), |
| 92 | + model: z.string(), |
| 93 | + modelParameters: z |
| 94 | + .object({ |
| 95 | + maxTokens: z.number().positive().optional(), |
| 96 | + temperature: z.number().min(0).max(1).optional(), |
| 97 | + topP: z.number().min(0).max(1).optional(), |
| 98 | + }) |
| 99 | + .optional(), |
| 100 | + messages: z.array(message), |
| 101 | + responseFormat: z.enum(['text', 'json_object', 'json_schema']).optional(), |
| 102 | + jsonSchema: z.string().optional(), |
| 103 | + }) |
| 104 | + .refine( |
| 105 | + data => { |
| 106 | + if (data.responseFormat === 'json_schema') { |
| 107 | + return data.jsonSchema !== undefined |
| 108 | + } |
| 109 | + return true |
| 110 | + }, |
| 111 | + { |
| 112 | + message: "jsonSchema must be provided when responseFormat is 'json_schema'", |
| 113 | + path: ['jsonSchema'], |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | +type Schema = z.infer<typeof schema> |
0 commit comments