chatCompletionFunctionOptions
SUPPORT · function · 运行时值
Build the tool-selection request fields for a function-calling stage.
The adapter expects exactly one function call per response, so tool use is required and parallel calls are disabled.
导入
ts
import { chatCompletionFunctionOptions } from '@jangts/agent-core/support';1
js
import { chatCompletionFunctionOptions } from '@jangts/agent-core/support';1
其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
调用
ts
chatCompletionFunctionOptions(options: ModelOptions): Record<string, unknown>1
Build the tool-selection request fields for a function-calling stage.
The adapter expects exactly one function call per response, so tool use is required and parallel calls are disabled.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | ModelOptions | 必需 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
json | boolean | undefined | 可省略 | Request a JSON-object response from providers that support it. |
temperature | number | undefined | 可省略 | Sampling temperature; provider range is not normalized by Core. |
maxOutputTokens | number | undefined | 可省略 | Output ceiling for the call; the effective value is the smaller of this and the budget. |
includeUsage | boolean | undefined | 可省略 | Ask the provider to report usage for streamed responses. |
onUsage | ((usage: ModelUsage) => void) | undefined | 可省略 | Receives usage the provider reports; may be called once per call. |
functions | readonly ModelFunction[] | undefined | 可省略 | Functions offered to the model for this call. |
toolChoice | "auto" | "required" | "none" | undefined | 可省略 | Tool-selection strategy; omitted keeps the provider's default. |
parallelToolCalls | boolean | "omit" | undefined | 可省略 | Request parallel tool calls; 'omit' leaves the wire field out entirely. |
返回: Record<string, unknown>。
使用示例
- 兼容 Provider 与自定义 Provider:Support 负责常见 Chat Completions HTTP/SSE 生命周期;App 可替换为自己的 ChatProvider。
真实服务端配置接入常见协议
入门 · Node.js
运行此例需要提供可用模型服务的连接配置。
ts
import { ModelRunner, Support } from '@jangts/agent-core';
const provider = new Support.CompatibleModelProvider({ config: {
baseUrl: process.env.MODEL_BASE_URL!, apiKey: process.env.MODEL_API_KEY!, model: process.env.MODEL_NAME!,
} });
const runner = new ModelRunner({ provider });
console.log((await runner.invoke('你好')).value);1
2
3
4
5
6
2
3
4
5
6
js
import { ModelRunner, Support } from '@jangts/agent-core';
const provider = new Support.CompatibleModelProvider({ config: {
baseUrl: process.env.MODEL_BASE_URL, apiKey: process.env.MODEL_API_KEY, model: process.env.MODEL_NAME,
} });
const runner = new ModelRunner({ provider });
console.log((await runner.invoke('你好')).value);1
2
3
4
5
6
2
3
4
5
6
直连工具线程的声明字段与 wire 映射
配置 · Node.js
只演示消息转换,无网络请求或工具执行。原始 tool_calls、tool_call_id 和宿主附加数据不透传。
ts
import { Support } from '@jangts/agent-core';
const messages: Support.ProviderMessage[] = [
{ role: 'user', content: '查询资料' },
{ role: 'assistant', content: '', toolCalls: [
{ id: 'call-1', name: 'lookup', arguments: '{"query":"资料"}' },
] },
{ role: 'tool', content: '{"found":true}', toolCallId: 'call-1' },
];
console.log(Support.chatCompletionMessages(messages));1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
js
import { Support } from '@jangts/agent-core';
const messages = [
{ role: 'user', content: '查询资料' },
{ role: 'assistant', content: '', toolCalls: [
{ id: 'call-1', name: 'lookup', arguments: '{"query":"资料"}' },
] },
{ role: 'tool', content: '{"found":true}', toolCallId: 'call-1' },
];
console.log(Support.chatCompletionMessages(messages));1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
同一合同接入离线对象实现
配置 · Node.js
ts
import { ModelRunner } from '@jangts/agent-core/model';
const provider = { async *stream(messages: readonly { content: string }[]) {
yield '离线回答:' + messages.at(-1)!.content;
} };
console.log((await new ModelRunner({ provider }).invoke('你好')).value);1
2
3
4
5
2
3
4
5
js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = { async *stream(messages) {
yield '离线回答:' + messages.at(-1).content;
} };
console.log((await new ModelRunner({ provider }).invoke('你好')).value);1
2
3
4
5
2
3
4
5