@zhin.js/ai 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,283 @@
1
+ /**
2
+ * @zhin.js/ai - Ollama Provider
3
+ * 支持本地 Ollama 模型
4
+ */
5
+
6
+ import { Logger } from '@zhin.js/core';
7
+ import { BaseProvider } from './base.js';
8
+ import type {
9
+ ProviderConfig,
10
+ ChatCompletionRequest,
11
+ ChatCompletionResponse,
12
+ ChatCompletionChunk,
13
+ ChatMessage,
14
+ ToolDefinition,
15
+ } from '../types.js';
16
+
17
+ const logger = new Logger(null, 'Ollama');
18
+
19
+ export interface OllamaConfig extends ProviderConfig {
20
+ host?: string;
21
+ models?: string[];
22
+ }
23
+
24
+ /**
25
+ * 转换消息格式
26
+ */
27
+ function toOllamaMessages(messages: ChatMessage[]): any[] {
28
+ return messages.map(msg => {
29
+ let content: string;
30
+ const images: string[] = [];
31
+
32
+ if (typeof msg.content === 'string') {
33
+ content = msg.content;
34
+ } else {
35
+ content = msg.content
36
+ .filter(p => p.type === 'text')
37
+ .map(p => (p as { type: 'text'; text: string }).text)
38
+ .join('');
39
+
40
+ for (const part of msg.content) {
41
+ if (part.type === 'image_url') {
42
+ const url = part.image_url.url;
43
+ if (url.startsWith('data:')) {
44
+ // 提取 base64 数据
45
+ const base64 = url.split(',')[1];
46
+ if (base64) images.push(base64);
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ const result: any = {
53
+ role: msg.role === 'tool' ? 'user' : msg.role,
54
+ content,
55
+ };
56
+
57
+ if (images.length > 0) {
58
+ result.images = images;
59
+ }
60
+
61
+ return result;
62
+ });
63
+ }
64
+
65
+ /**
66
+ * 转换工具定义
67
+ */
68
+ function toOllamaTools(tools?: ToolDefinition[]): any[] | undefined {
69
+ if (!tools?.length) return undefined;
70
+
71
+ return tools.map(tool => ({
72
+ type: 'function',
73
+ function: {
74
+ name: tool.function.name,
75
+ description: tool.function.description,
76
+ parameters: tool.function.parameters,
77
+ },
78
+ }));
79
+ }
80
+
81
+ export class OllamaProvider extends BaseProvider {
82
+ name = 'ollama';
83
+ models: string[];
84
+
85
+ private host: string;
86
+
87
+ constructor(config: OllamaConfig = {}) {
88
+ super(config);
89
+ this.host = config.host || config.baseUrl || 'http://localhost:11434';
90
+ // 使用配置中的模型列表,如果没有则使用默认列表
91
+ this.models = config.models?.length ? config.models : [
92
+ 'llama3.3',
93
+ 'llama3.2',
94
+ 'llama3.1',
95
+ 'qwen2.5',
96
+ 'qwen2.5-coder',
97
+ 'deepseek-r1',
98
+ 'deepseek-v3',
99
+ 'mistral',
100
+ 'mixtral',
101
+ 'phi4',
102
+ 'gemma2',
103
+ ];
104
+ logger.debug(`初始化完成, host: ${this.host}`);
105
+ }
106
+
107
+ async chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
108
+ const messages = toOllamaMessages(request.messages);
109
+
110
+ logger.debug(`请求 ${request.model}, 消息: ${messages.length}`);
111
+
112
+ const ollamaRequest: any = {
113
+ model: request.model,
114
+ messages,
115
+ stream: false,
116
+ options: {},
117
+ };
118
+
119
+ if (request.temperature !== undefined) {
120
+ ollamaRequest.options.temperature = request.temperature;
121
+ }
122
+
123
+ if (request.top_p !== undefined) {
124
+ ollamaRequest.options.top_p = request.top_p;
125
+ }
126
+
127
+ if (request.max_tokens !== undefined) {
128
+ ollamaRequest.options.num_predict = request.max_tokens;
129
+ }
130
+
131
+ const tools = toOllamaTools(request.tools);
132
+ if (tools) {
133
+ ollamaRequest.tools = tools;
134
+ }
135
+
136
+ const startTime = Date.now();
137
+
138
+ const response = await this.fetch<any>(`${this.host}/api/chat`, {
139
+ method: 'POST',
140
+ json: ollamaRequest,
141
+ });
142
+
143
+ logger.debug(`响应耗时: ${Date.now() - startTime}ms, 工具调用: ${response.message?.tool_calls?.length || 0}`);
144
+
145
+ // 转换响应格式
146
+ const toolCalls = response.message?.tool_calls?.map((tc: any, i: number) => ({
147
+ id: `call_${i}`,
148
+ type: 'function' as const,
149
+ function: {
150
+ name: tc.function.name,
151
+ arguments: JSON.stringify(tc.function.arguments),
152
+ },
153
+ }));
154
+
155
+ return {
156
+ id: `ollama-${Date.now()}`,
157
+ object: 'chat.completion',
158
+ created: Date.now(),
159
+ model: response.model,
160
+ choices: [{
161
+ index: 0,
162
+ message: {
163
+ role: 'assistant',
164
+ content: response.message?.content || '',
165
+ tool_calls: toolCalls?.length ? toolCalls : undefined,
166
+ },
167
+ finish_reason: toolCalls?.length ? 'tool_calls' : 'stop',
168
+ }],
169
+ usage: {
170
+ prompt_tokens: response.prompt_eval_count || 0,
171
+ completion_tokens: response.eval_count || 0,
172
+ total_tokens: (response.prompt_eval_count || 0) + (response.eval_count || 0),
173
+ },
174
+ };
175
+ }
176
+
177
+ async *chatStream(request: ChatCompletionRequest): AsyncIterable<ChatCompletionChunk> {
178
+ const messages = toOllamaMessages(request.messages);
179
+
180
+ const ollamaRequest: any = {
181
+ model: request.model,
182
+ messages,
183
+ stream: true,
184
+ options: {},
185
+ };
186
+
187
+ if (request.temperature !== undefined) {
188
+ ollamaRequest.options.temperature = request.temperature;
189
+ }
190
+
191
+ if (request.top_p !== undefined) {
192
+ ollamaRequest.options.top_p = request.top_p;
193
+ }
194
+
195
+ const response = await globalThis.fetch(`${this.host}/api/chat`, {
196
+ method: 'POST',
197
+ headers: { 'Content-Type': 'application/json' },
198
+ body: JSON.stringify(ollamaRequest),
199
+ });
200
+
201
+ if (!response.ok) {
202
+ const error = await response.text();
203
+ throw new Error(`Ollama API Error (${response.status}): ${error}`);
204
+ }
205
+
206
+ if (!response.body) {
207
+ throw new Error('Response body is empty');
208
+ }
209
+
210
+ const reader = response.body.getReader();
211
+ const decoder = new TextDecoder();
212
+ let buffer = '';
213
+ const id = `ollama-${Date.now()}`;
214
+
215
+ while (true) {
216
+ const { done, value } = await reader.read();
217
+ if (done) break;
218
+
219
+ buffer += decoder.decode(value, { stream: true });
220
+ const lines = buffer.split('\n');
221
+ buffer = lines.pop() || '';
222
+
223
+ for (const line of lines) {
224
+ if (!line.trim()) continue;
225
+
226
+ try {
227
+ const data = JSON.parse(line);
228
+
229
+ yield {
230
+ id,
231
+ object: 'chat.completion.chunk',
232
+ created: Date.now(),
233
+ model: data.model || request.model,
234
+ choices: [{
235
+ index: 0,
236
+ delta: data.done
237
+ ? {}
238
+ : { content: data.message?.content || '' },
239
+ finish_reason: data.done ? 'stop' : null,
240
+ }],
241
+ usage: data.done ? {
242
+ prompt_tokens: data.prompt_eval_count || 0,
243
+ completion_tokens: data.eval_count || 0,
244
+ total_tokens: (data.prompt_eval_count || 0) + (data.eval_count || 0),
245
+ } : undefined,
246
+ };
247
+ } catch {
248
+ // 忽略解析错误
249
+ }
250
+ }
251
+ }
252
+ }
253
+
254
+ async listModels(): Promise<string[]> {
255
+ try {
256
+ const response = await this.fetch<{ models: { name: string }[] }>(
257
+ `${this.host}/api/tags`
258
+ );
259
+ return response.models.map(m => m.name);
260
+ } catch {
261
+ return this.models;
262
+ }
263
+ }
264
+
265
+ /**
266
+ * 拉取模型
267
+ */
268
+ async pullModel(model: string): Promise<void> {
269
+ await this.fetch(`${this.host}/api/pull`, {
270
+ method: 'POST',
271
+ json: { name: model },
272
+ });
273
+ }
274
+
275
+ async healthCheck(): Promise<boolean> {
276
+ try {
277
+ await globalThis.fetch(`${this.host}/api/tags`);
278
+ return true;
279
+ } catch {
280
+ return false;
281
+ }
282
+ }
283
+ }
@@ -0,0 +1,167 @@
1
+ /**
2
+ * @zhin.js/ai - OpenAI Provider
3
+ * 支持 OpenAI API 及兼容接口(DeepSeek、Moonshot 等)
4
+ */
5
+
6
+ import { BaseProvider } from './base.js';
7
+ import type {
8
+ ProviderConfig,
9
+ ChatCompletionRequest,
10
+ ChatCompletionResponse,
11
+ ChatCompletionChunk,
12
+ } from '../types.js';
13
+
14
+ export interface OpenAIConfig extends ProviderConfig {
15
+ organization?: string;
16
+ }
17
+
18
+ export class OpenAIProvider extends BaseProvider {
19
+ name = 'openai';
20
+ models = [
21
+ 'gpt-4o',
22
+ 'gpt-4o-mini',
23
+ 'gpt-4-turbo',
24
+ 'gpt-4',
25
+ 'gpt-3.5-turbo',
26
+ 'o1',
27
+ 'o1-mini',
28
+ 'o1-preview',
29
+ 'o3-mini',
30
+ ];
31
+
32
+ private baseUrl: string;
33
+
34
+ constructor(config: OpenAIConfig = {}) {
35
+ super(config);
36
+ this.baseUrl = config.baseUrl || 'https://api.openai.com/v1';
37
+
38
+ if (config.organization) {
39
+ this.config.headers = {
40
+ ...this.config.headers,
41
+ 'OpenAI-Organization': config.organization,
42
+ };
43
+ }
44
+ }
45
+
46
+ async chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
47
+ return this.fetch<ChatCompletionResponse>(
48
+ `${this.baseUrl}/chat/completions`,
49
+ {
50
+ method: 'POST',
51
+ json: {
52
+ ...request,
53
+ stream: false,
54
+ },
55
+ }
56
+ );
57
+ }
58
+
59
+ async *chatStream(request: ChatCompletionRequest): AsyncIterable<ChatCompletionChunk> {
60
+ const stream = this.fetchStream(
61
+ `${this.baseUrl}/chat/completions`,
62
+ {
63
+ method: 'POST',
64
+ json: {
65
+ ...request,
66
+ stream: true,
67
+ stream_options: { include_usage: true },
68
+ },
69
+ }
70
+ );
71
+
72
+ for await (const data of stream) {
73
+ try {
74
+ const chunk = JSON.parse(data) as ChatCompletionChunk;
75
+ yield chunk;
76
+ } catch {
77
+ // 忽略解析错误的行
78
+ }
79
+ }
80
+ }
81
+
82
+ async listModels(): Promise<string[]> {
83
+ interface ModelList {
84
+ data: { id: string }[];
85
+ }
86
+
87
+ try {
88
+ const response = await this.fetch<ModelList>(`${this.baseUrl}/models`);
89
+ return response.data
90
+ .map((m) => m.id)
91
+ .filter((id) => id.includes('gpt') || id.includes('o1') || id.includes('o3'));
92
+ } catch {
93
+ return this.models;
94
+ }
95
+ }
96
+ }
97
+
98
+ /**
99
+ * DeepSeek Provider(基于 OpenAI 兼容接口)
100
+ */
101
+ export class DeepSeekProvider extends OpenAIProvider {
102
+ name = 'deepseek';
103
+ models = [
104
+ 'deepseek-chat',
105
+ 'deepseek-coder',
106
+ 'deepseek-reasoner',
107
+ ];
108
+
109
+ constructor(config: ProviderConfig = {}) {
110
+ super({
111
+ ...config,
112
+ baseUrl: config.baseUrl || 'https://api.deepseek.com/v1',
113
+ });
114
+ }
115
+
116
+ async listModels(): Promise<string[]> {
117
+ return this.models;
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Moonshot Provider(基于 OpenAI 兼容接口)
123
+ */
124
+ export class MoonshotProvider extends OpenAIProvider {
125
+ name = 'moonshot';
126
+ models = [
127
+ 'moonshot-v1-8k',
128
+ 'moonshot-v1-32k',
129
+ 'moonshot-v1-128k',
130
+ ];
131
+
132
+ constructor(config: ProviderConfig = {}) {
133
+ super({
134
+ ...config,
135
+ baseUrl: config.baseUrl || 'https://api.moonshot.cn/v1',
136
+ });
137
+ }
138
+
139
+ async listModels(): Promise<string[]> {
140
+ return this.models;
141
+ }
142
+ }
143
+
144
+ /**
145
+ * 智谱 AI Provider(基于 OpenAI 兼容接口)
146
+ */
147
+ export class ZhipuProvider extends OpenAIProvider {
148
+ name = 'zhipu';
149
+ models = [
150
+ 'glm-4-plus',
151
+ 'glm-4',
152
+ 'glm-4-air',
153
+ 'glm-4-flash',
154
+ 'glm-4v-plus',
155
+ ];
156
+
157
+ constructor(config: ProviderConfig = {}) {
158
+ super({
159
+ ...config,
160
+ baseUrl: config.baseUrl || 'https://open.bigmodel.cn/api/paas/v4',
161
+ });
162
+ }
163
+
164
+ async listModels(): Promise<string[]> {
165
+ return this.models;
166
+ }
167
+ }