|
1 | 1 | import { z, ZodSchema } from "zod"; |
2 | 2 | import zodToJsonSchema from "zod-to-json-schema"; |
3 | | -import { Job } from "./job"; |
4 | | -import { JobBaseSchema } from "./schema"; |
5 | | - |
6 | | -export const MessageContentSchema = z.union([ |
7 | | - z.string(), |
8 | | - z.array( |
9 | | - z.object({ |
10 | | - type: z.literal("text"), |
11 | | - text: z.string(), |
12 | | - }) |
13 | | - ), |
14 | | - z.array( |
15 | | - z.object({ |
16 | | - type: z.literal("image"), |
17 | | - image_url: z.string().optional(), |
18 | | - source: z |
19 | | - .object({ |
20 | | - type: z.literal("base64"), |
21 | | - data: z.string(), |
22 | | - media_type: z.enum([ |
23 | | - "image/jpeg", |
24 | | - "image/png", |
25 | | - "image/webp", |
26 | | - "image/gif", |
27 | | - ]), |
28 | | - }) |
29 | | - .optional(), |
30 | | - }) |
31 | | - ), |
32 | | -]); |
33 | | - |
34 | | -export const MessageSchema = z.object({ |
35 | | - role: z.enum(["system", "user", "assistant"]), |
36 | | - content: MessageContentSchema, |
37 | | -}); |
38 | | - |
39 | | -export const StreamOptionsSchema = z.object({ |
40 | | - includeUsage: z.boolean().optional(), |
41 | | -}); |
42 | | - |
43 | | -export const ResponseFormatSchema = z.object({ |
44 | | - type: z.enum(["json_object", "json_schema"]), |
45 | | - json_schema: z.any().optional(), |
46 | | -}); |
47 | | - |
48 | | -export const ChatToolSchema = z.object({ |
49 | | - params: z.object({ |
50 | | - name: z.string(), |
51 | | - description: z.string().optional(), |
52 | | - parameters: z.any().optional(), |
53 | | - }), |
54 | | - toJSON: z.function().returns(z.any()).optional(), |
55 | | -}); |
56 | | - |
57 | | -export const JsonSchemaDefSchema = z.object({ |
58 | | - name: z.string(), |
59 | | - description: z.string().optional(), |
60 | | - schema: z.any(), |
61 | | -}); |
62 | | - |
63 | | -export const ChatJobParamsSchema = z.object({ |
64 | | - temperature: z.number().optional(), |
65 | | - stream: z.boolean().optional(), |
66 | | - streamOptions: StreamOptionsSchema.optional(), |
67 | | - maxTokens: z.number().optional(), |
68 | | - messages: z.array(MessageSchema), |
69 | | - tools: z.array(ChatToolSchema).optional(), |
70 | | - toolChoice: z.string().optional(), |
71 | | - responseFormat: ResponseFormatSchema.optional(), |
72 | | - topP: z.number().optional(), |
73 | | - topK: z.number().optional(), |
74 | | - systemPrompt: z.string().optional(), |
75 | | - jsonSchema: JsonSchemaDefSchema.optional(), |
76 | | -}); |
77 | | - |
78 | | -export const ChatResultSchema = z.object({ |
79 | | - message: z.object({ |
80 | | - role: z.literal("assistant"), |
81 | | - content: z.string().nullable(), |
82 | | - }), |
83 | | - usage: z |
84 | | - .object({ |
85 | | - prompt_tokens: z.number(), |
86 | | - completion_tokens: z.number(), |
87 | | - total_tokens: z.number(), |
88 | | - }) |
89 | | - .optional(), |
90 | | - tool_calls: z |
91 | | - .array( |
92 | | - z.object({ |
93 | | - name: z.string(), |
94 | | - arguments: z.record(z.any()), |
95 | | - }) |
96 | | - ) |
97 | | - .optional(), |
98 | | -}); |
99 | | - |
100 | | -export const ChatJobSchema = JobBaseSchema.extend({ |
101 | | - type: z.literal("chat"), |
102 | | - model: z.string(), |
103 | | - params: ChatJobParamsSchema, |
104 | | - result: ChatResultSchema.optional(), |
105 | | -}); |
106 | | - |
107 | | -export type ChatJobSchemaType = z.infer<typeof ChatJobSchema>; |
108 | | -export type ChatJobParams = z.infer<typeof ChatJobParamsSchema>; |
| 3 | +import { Job } from "../job"; |
| 4 | +import type { |
| 5 | + ChatJobSchemaType, |
| 6 | + ChatJobParams, |
| 7 | + ChatResultSchema, |
| 8 | +} from "./schema"; |
109 | 9 |
|
110 | 10 | export function systemPrompt(content: string) { |
111 | 11 | return { role: "system", content }; |
|
0 commit comments