AI
调用用户在 lingtools 里配置的 AI 模型(OpenAI 兼容的服务商)或内置的本地小模型。插件不用自己管理 API Key。需要 ai 权限。
用户在 设置 →「AI」 里添加服务商和模型,并选择「插件默认对话模型」。插件只能拿到模型列表和调用结果,拿不到 Key。
ling.ai
ts
ai(option: AiOption, streamCallback?: (delta: AiMessage) => void): AiPromisets
const reply = await window.ling.ai({
messages: [
{ role: 'system', content: '你是翻译助手,只输出译文。' },
{ role: 'user', content: text },
],
})
console.log(reply.content)流式输出:传第二个参数,每收到一段新内容调用一次(参数是增量):
ts
let output = ''
const task = window.ling.ai({ messages }, (delta) => {
output += delta.content ?? ''
})
stopButton.onclick = () => task.abort() // 取消,之后以 aborted reject
const final = await task参数
| 字段 | 说明 |
|---|---|
model | 模型 ID,取自 allAiModels();省略时用用户设置的默认模型,没设置时以 unavailable 失败 |
messages | 对话消息,至少一条。role:system / user / assistant / tool;content 为文字或分段数组(文字段与图片段,图片只接受 Data URL) |
tools | 交给模型的工具(function 类型) |
temperature | 0–2 |
maxTokens | 最多生成的 token 数,1–32768(本地模型最多 1024) |
timeout | 总时长上限(秒,5–600)。默认非流式 120 秒;流式不限总时长,60 秒没有新内容时超时 |
结果
一条 assistant 消息:{ role, content, reasoning_content?, tool_calls?, usage? }。
工具调用
与 uTools 不同,lingtools 不会自动执行工具:模型发起的调用在结果的 tool_calls 里交还插件,插件执行后把结果作为 tool 消息发起下一轮。
ts
const messages = [{ role: 'user', content: '北京现在几点?' }]
const tools = [{ type: 'function', function: { name: 'now', description: '获取某城市当前时间', parameters: { type: 'object', properties: { city: { type: 'string' } } } } }]
let reply = await window.ling.ai({ messages, tools })
while (reply.tool_calls?.length) {
messages.push(reply)
for (const call of reply.tool_calls) {
const args = JSON.parse(call.function.arguments)
messages.push({ role: 'tool', tool_call_id: call.id, content: await runTool(call.function.name, args) })
}
reply = await window.ling.ai({ messages, tools })
}错误
| 错误码 | 说明 |
|---|---|
unavailable | 没有可用模型(用户没配置) |
ai_failed | 服务商调用失败,error.data 为 { kind, status?, message? },kind:http、network、timeout、rate_limit、invalid_response |
aborted | 调用了 abort(),或页面 / 插件被关闭 |
allAiModels
ts
allAiModels(): Promise<AiModel[]>当前可用的模型:用户启用的远程模型,加上已下载且允许对话的本地模型。每项:
| 字段 | 说明 |
|---|---|
id | 交给 ling.ai 的 model,如 deepseek/deepseek-chat;本地模型为 local |
label | 显示名 |
model | 服务商那边的模型 ID |
providerId / providerName | 服务商 ID 与名称 |
icon | 图标 Data URL(可能没有) |
适合在插件里做「选择模型」的下拉框。
本地模型 localAi
lingtools 内置一个小型本地模型(Qwen3 0.6B 与对应的嵌入模型),不联网、不花钱,适合分类、提取、向量搜索等轻量任务。模型需要用户在设置里下载;没下载时调用以 unavailable 失败,插件不能替用户触发下载。
| 方法 | 说明 |
|---|---|
localAi.status(): Promise<{ downloaded, chatEnabled, models }> | 是否已下载、是否允许用于对话、内置的模型 |
localAi.embed(texts, { instruct? }): Promise<number[][]> | 计算文本的嵌入向量,用于语义搜索、相似度 |
localAi.classify(input, choices): Promise<string | null> | 从选项中选出最合适的一个,返回选项 id;都不合适时为 null。选项 { id, description } |
localAi.generate({ prompt, grammar?, jsonSchema?, maxTokens? }): Promise<{ text, usage }> | 生成文字;可以用 GBNF 语法或 JSON Schema 约束输出格式 |
ts
const kind = await window.ling.localAi.classify(text, [
{ id: 'json', description: 'JSON 数据,如 {"a":1}' },
{ id: 'url', description: '网址,如 https://example.com' },
{ id: 'sql', description: 'SQL 语句,如 SELECT * FROM users' },
])给 AI 提供工具
插件也可以反过来把功能提供给 AI 客户端调用(registerTool、unregisterTool、ling.ai.toolResult),见 给 AI 提供工具。