@weisiren000/oiiai 0.1.2 → 0.1.4
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.
- package/README.md +165 -201
- package/dist/index.d.mts +339 -15
- package/dist/index.d.ts +339 -15
- package/dist/index.js +1648 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1640 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +59 -55
package/README.md
CHANGED
|
@@ -1,201 +1,165 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
```typescript
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
class MyProvider extends BaseProvider {
|
|
168
|
-
readonly name = 'my-provider';
|
|
169
|
-
|
|
170
|
-
constructor(apiKey: string) {
|
|
171
|
-
super();
|
|
172
|
-
// 初始化 SDK 客户端
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async chat(options: ChatOptions): Promise<ChatResult> {
|
|
176
|
-
// 实现非流式请求
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async *chatStream(options: ChatOptions): AsyncGenerator<StreamChunk> {
|
|
180
|
-
// 实现流式请求
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
`BaseProvider` 已实现 `ask` 和 `askWithSystem` 的默认逻辑,子类只需实现 `chat` 和 `chatStream`。
|
|
186
|
-
|
|
187
|
-
## 获取模型列表
|
|
188
|
-
|
|
189
|
-
```typescript
|
|
190
|
-
const models = await ai.listModels();
|
|
191
|
-
|
|
192
|
-
for (const model of models) {
|
|
193
|
-
console.log(`${model.id}: ${model.name}`);
|
|
194
|
-
console.log(` 上下文长度: ${model.contextLength}`);
|
|
195
|
-
console.log(` 价格: $${model.pricing.prompt}/token (输入)`);
|
|
196
|
-
}
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
## License
|
|
200
|
-
|
|
201
|
-
MIT
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# 🤖 @weisiren000/oiiai
|
|
4
|
+
|
|
5
|
+
**统一的 AI Provider 接口封装库**
|
|
6
|
+
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
[](https://nodejs.org/)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
[](https://www.npmjs.com/package/@weisiren000/oiiai)
|
|
11
|
+
|
|
12
|
+
支持 **OpenRouter** · **Gemini** · **Groq** · **HuggingFace** · **ModelScope**
|
|
13
|
+
|
|
14
|
+
[📖 详细文档](./docs/providers.md) · [🚀 快速开始](#快速开始) · [💡 示例](#使用示例)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## ✨ 特性
|
|
21
|
+
|
|
22
|
+
- 🔌 **统一接口** - 所有 Provider 使用相同 API,学会一个就会全部
|
|
23
|
+
- 🧠 **Reasoning 支持** - 统一的思考模式配置,自动转换各 Provider 格式
|
|
24
|
+
- 🌊 **流式输出** - 支持实时流式响应,区分思考/回答内容
|
|
25
|
+
- 📦 **TypeScript** - 完整类型定义,开发体验友好
|
|
26
|
+
- 🔧 **可扩展** - 轻松实现自定义 Provider
|
|
27
|
+
|
|
28
|
+
## 📦 安装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @weisiren000/oiiai
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 🚀 快速开始
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { ai } from '@weisiren000/oiiai';
|
|
38
|
+
|
|
39
|
+
// 创建 Provider(所有 Provider 使用方式完全一致)
|
|
40
|
+
const openrouter = ai.openrouter('your-api-key');
|
|
41
|
+
const gemini = ai.gemini('your-api-key');
|
|
42
|
+
const groq = ai.groq('your-api-key');
|
|
43
|
+
|
|
44
|
+
// 简单问答
|
|
45
|
+
const answer = await openrouter.ask('openai/gpt-4o', '你好');
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 💡 使用示例
|
|
49
|
+
|
|
50
|
+
### 简单问答
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const answer = await provider.ask('model-id', '什么是 TypeScript?');
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 带系统提示
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
const answer = await provider.askWithSystem(
|
|
60
|
+
'model-id',
|
|
61
|
+
'你是一个专业的代码助手',
|
|
62
|
+
'如何定义 TypeScript 接口?'
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 完整对话
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const result = await provider.chat({
|
|
70
|
+
model: 'model-id',
|
|
71
|
+
messages: [
|
|
72
|
+
{ role: 'system', content: '你是一个友好的助手' },
|
|
73
|
+
{ role: 'user', content: '你好!' },
|
|
74
|
+
],
|
|
75
|
+
temperature: 0.7,
|
|
76
|
+
maxTokens: 1000,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.log(result.content); // 回答内容
|
|
80
|
+
console.log(result.usage); // Token 使用情况
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 流式输出
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
for await (const chunk of provider.chatStream({
|
|
87
|
+
model: 'model-id',
|
|
88
|
+
messages: [{ role: 'user', content: '写一首诗' }],
|
|
89
|
+
})) {
|
|
90
|
+
if (chunk.type === 'reasoning') {
|
|
91
|
+
process.stdout.write(`[思考] ${chunk.text}`);
|
|
92
|
+
} else {
|
|
93
|
+
process.stdout.write(chunk.text);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 思考模式 (Reasoning)
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const result = await provider.chat({
|
|
102
|
+
model: 'deepseek/deepseek-r1',
|
|
103
|
+
messages: [{ role: 'user', content: '9.11 和 9.9 哪个大?' }],
|
|
104
|
+
reasoning: {
|
|
105
|
+
effort: 'high', // 'off' | 'low' | 'medium' | 'high'
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
console.log('思考过程:', result.reasoning);
|
|
110
|
+
console.log('最终答案:', result.content);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## 🔧 支持的 Provider
|
|
114
|
+
|
|
115
|
+
| Provider | 服务商 | Reasoning 参数 | 参考文档 |
|
|
116
|
+
| ------------- | ------------- | ---------------------------- | ---------------------------------------------------------------------- |
|
|
117
|
+
| `openrouter` | OpenRouter | `reasoning.effort` | [Docs](https://openrouter.ai/docs/requests) |
|
|
118
|
+
| `gemini` | Google Gemini | `reasoning_effort` | [Docs](https://ai.google.dev/gemini-api/docs/text-generation) |
|
|
119
|
+
| `groq` | Groq | `reasoning_format: 'parsed'` | [Docs](https://console.groq.com/docs/reasoning) |
|
|
120
|
+
| `huggingface` | HuggingFace | 不支持 | - |
|
|
121
|
+
| `modelscope` | 魔搭社区 | `enable_thinking` | [Docs](https://www.alibabacloud.com/help/en/model-studio/deepseek-api) |
|
|
122
|
+
|
|
123
|
+
## 📝 常用模型
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// OpenRouter
|
|
127
|
+
'openai/gpt-4o';
|
|
128
|
+
'anthropic/claude-sonnet-4';
|
|
129
|
+
'deepseek/deepseek-r1';
|
|
130
|
+
|
|
131
|
+
// Gemini
|
|
132
|
+
'gemini-2.5-flash';
|
|
133
|
+
'gemini-2.5-pro';
|
|
134
|
+
|
|
135
|
+
// Groq
|
|
136
|
+
'llama-3.3-70b-versatile';
|
|
137
|
+
'qwen/qwen3-32b';
|
|
138
|
+
|
|
139
|
+
// ModelScope
|
|
140
|
+
'deepseek-ai/DeepSeek-R1';
|
|
141
|
+
'Qwen/Qwen2.5-72B-Instruct';
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 🛠️ 自定义 Provider
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { BaseProvider } from '@weisiren000/oiiai';
|
|
148
|
+
import type { ChatOptions, ChatResult, StreamChunk } from '@weisiren000/oiiai';
|
|
149
|
+
|
|
150
|
+
class MyProvider extends BaseProvider {
|
|
151
|
+
readonly name = 'my-provider';
|
|
152
|
+
|
|
153
|
+
async chat(options: ChatOptions): Promise<ChatResult> {
|
|
154
|
+
// 实现非流式请求
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async *chatStream(options: ChatOptions): AsyncGenerator<StreamChunk> {
|
|
158
|
+
// 实现流式请求
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## 📄 License
|
|
164
|
+
|
|
165
|
+
MIT
|