|
1 | 1 | import { z, ZodSchema } from "zod"; |
2 | | -import zodToJsonSchema from "zod-to-json-schema"; |
3 | 2 | import { Job } from "../job"; |
4 | 3 | import type { |
5 | 4 | ChatJobSchemaType, |
6 | 5 | ChatJobParams, |
7 | 6 | ChatResultSchema, |
| 7 | + ChatStreamOptions, |
| 8 | + ResponseFormat, |
8 | 9 | } from "./schema"; |
| 10 | +import { ChatTool } from "./tool"; |
9 | 11 |
|
10 | 12 | export function systemPrompt(content: string) { |
11 | 13 | return { role: "system", content }; |
@@ -64,81 +66,6 @@ export function tool(name: string) { |
64 | 66 | return new ChatTool(name); |
65 | 67 | } |
66 | 68 |
|
67 | | -export class ChatTool { |
68 | | - public params: { |
69 | | - name: string; |
70 | | - description?: string; |
71 | | - parameters?: ZodSchema; |
72 | | - }; |
73 | | - |
74 | | - constructor(name: string) { |
75 | | - this.params = { name }; |
76 | | - } |
77 | | - |
78 | | - description(description: string) { |
79 | | - this.params.description = description; |
80 | | - return this; |
81 | | - } |
82 | | - |
83 | | - parameters(parameters: ZodSchema) { |
84 | | - this.params.parameters = parameters; |
85 | | - return this; |
86 | | - } |
87 | | - |
88 | | - toJSON() { |
89 | | - return { |
90 | | - type: "function", |
91 | | - function: { |
92 | | - name: this.params.name, |
93 | | - description: this.params.description, |
94 | | - parameters: zodToJsonSchema(this.params.parameters!), |
95 | | - }, |
96 | | - }; |
97 | | - } |
98 | | -} |
99 | | - |
100 | | -export interface ChatUsage { |
101 | | - input_tokens: number; |
102 | | - output_tokens: number; |
103 | | -} |
104 | | - |
105 | | -export interface ChatTextResponse { |
106 | | - usage?: ChatUsage; |
107 | | - text?: string; |
108 | | -} |
109 | | - |
110 | | -export interface ChatObjectResponse { |
111 | | - usage?: ChatUsage; |
112 | | - object?: any; |
113 | | -} |
114 | | - |
115 | | -export interface StreamOptions { |
116 | | - includeUsage?: boolean; |
117 | | -} |
118 | | - |
119 | | -export interface ResponseFormat { |
120 | | - type: "json_object" | "json_schema"; |
121 | | - json_schema?: ZodSchema; |
122 | | -} |
123 | | - |
124 | | -export type MessageContent = |
125 | | - | string |
126 | | - | { text: string; type: "text" }[] |
127 | | - | { |
128 | | - type: "image"; |
129 | | - image_url?: string; |
130 | | - source?: { |
131 | | - type: "base64"; |
132 | | - data: string; |
133 | | - media_type: "image/jpeg" | "image/png" | "image/webp" | "image/gif"; |
134 | | - }; |
135 | | - }[]; |
136 | | - |
137 | | -export interface Message { |
138 | | - role: "system" | "user" | "assistant"; |
139 | | - content: MessageContent; |
140 | | -} |
141 | | - |
142 | 69 | export class ChatJob<T extends ChatJobSchemaType> extends Job<T> { |
143 | 70 | model: string; |
144 | 71 | params: ChatJobParams; |
@@ -218,7 +145,7 @@ export class ChatJob<T extends ChatJobSchemaType> extends Job<T> { |
218 | 145 | return this; |
219 | 146 | } |
220 | 147 |
|
221 | | - stream(streamOptions?: StreamOptions) { |
| 148 | + stream(streamOptions?: ChatStreamOptions) { |
222 | 149 | this.params.stream = true; |
223 | 150 | if (streamOptions) { |
224 | 151 | this.params.streamOptions = streamOptions; |
|
0 commit comments