大模型接口
更新: 6/20/2026 字数: 0 字 时长: 0 分钟
总所周知,我们在使用大模型的时候本事是调用了服务提供商提供的一个接口,无论再高级的框架,本身都是对于服务提供商提供的接口进行再封装
很多人感觉Agent很高级,但是做程序员这一行,最重要的就是知道:“再厉害的代码也是人写的,所有的代码本身都离不来最简单的语句”,Agent业务也一样,如果说Agent=LLM+TOOL,Harness=Agent+成熟的AI使用方法论,那么Agent业务=调用API+针对该API特别的书写范式
现在的Agent业务的困难之处在于LLM是完全黑盒的操作,许多老程序员已经熟悉了确定性编程,这就导致在面对黑盒编程的时候会感到手足无措,但是实际上,我们的大多数业务面临的都是两个环节:
- 许多简单的Agent业务其实不需要很严谨专业的输出
- 即时需要严谨专业的输出也有专门的工程化来帮助我们“调教”LLM
因此现代程序员根本不用对Agent开发望而却步,这其实就是传统后端的一种拓展业务而已
本文在这里基于DeepSeek接口文档和大家一同来看一下在抛弃Langchain等框架后调用大模型会是个什么样子
聊天接口
目前主流的接口格式为OpenAI API和Anthropic API两种格式,这里我们以OpenAI API为例
DeepSeek用于兼容OpenAI API的URL是https://api.deepseek.com,为我们提供了deepseek-v4-pro和deepseek-v4-flash两种模型
我们先看一下一个简单的调用的示例
interface APIResponse {
id: string;
object: string;
created: number;
model: string;
choices: Array<{
index: number;
message: {
role: string;
content: string;
};
finish_reason: string;
}>;
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}
async function debugFullResponse() {
const apiKey = 'your-api-key';
const url = 'https://api.deepseek.com/chat/completions';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'deepseek-v4-pro', // 或其他模型
messages: [{ role: 'user', content: '你好,请用一句话自我介绍。' }],
stream: false // 关闭流式
})
});
// 1. 打印原始响应状态和 Headers
console.log('--- Status & Headers ---');
console.log(`Status: ${response.status} ${response.statusText}`);
console.log(`Content-Type: ${response.headers.get('content-type')}`);
// 2. 打印最底层的原始 JSON 字符串
const rawText = await response.text();
console.log('\n--- Raw JSON Response Body ---');
console.log(rawText);
// 3. 转为 TS 对象研究字段
const json: APIResponse = JSON.parse(rawText);
console.log('\n--- Analyzed Fields ---');
console.log(`结束原因: ${json.choices[0].finish_reason}`);
console.log(`Token 消耗: 提示 ${json.usage.prompt_tokens} + 回复 ${json.usage.completion_tokens}`);
}
debugFullResponse();这里我们可以观察到几个事情
- APIKEY其实就是简单的Token,其本身也是拼接在了Bearer后面
- 访问方式是POST,header中是Content-Type和Authorization
- 整体内容全部拼接在body中,其中有model,message,stream,其中message就是历史消息列表,里面有用户和内容content
我们来看返回值
{
"id": "f7f01192-7fc3-4b17-a3d7-afded8307c3c",
"object": "chat.completion",
"created": 1781959299,
"model": "deepseek-v4-pro",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!我是DeepSeek,由深度求索公司创造的AI助手,免费且支持超长上下文,很高兴为你服务~"
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 11,
"completion_tokens": 162,
"total_tokens": 173,
"prompt_tokens_details": {
"cached_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 134
},
"prompt_cache_hit_tokens": 0,
"prompt_cache_miss_tokens": 11
},
"system_fingerprint": "fp_9954b31ca7_prod0820_fp8_kvcache_20260402"
}我们发现核心内容都在Choices和usage中,每个参数的意义就是他们的名字,这里就不过多阐述了
至于如果我们要打开思考模式,那么DeepSeek API文档中也给了我们专门的参数
| 控制参数(OpenAI 格式) | 控制参数(Anthropic 格式) | |
| 思考模式开关(1) | {"thinking": {"type": "enabled/disabled"}} | {"thinking": {"type": "enabled/disabled"}} |
| 思考强度控制(2)(3) | {"reasoning_effort": "high/max"} | {"output_config": {"effort": "high/max"}} |
(1) 默认思考开关为 enabled | ||
(2) 思考模式下,对普通请求,默认 effort 为 high;对一些复杂 Agent 类请求(如 Claude Code、OpenCode),effort 自动设置为 max | ||
(3) 思考模式下,出于兼容考虑 low、medium 会映射为 high, xhigh 会映射为 max |
返回的choices中会多一个reasoning_content,这个内容是思考的过程
{
"id": "f7f01192-7fc3-4b17-a3d7-afded8307c3c",
"object": "chat.completion",
"created": 1781959299,
"model": "deepseek-v4-pro",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!我是DeepSeek,由深度求索公司创造的AI助手,免费且支持超长上下文,很高兴为你服务~",
"reasoning_content": "好的,用户让我用一句话自我介绍。这是一个非常简单的请求,不需要复杂拆解。\n\n我需要用简洁、自然的方式说明我的身份和核心功能。想到了直接说“我是DeepSeek,由深度求索公司创造的AI助手”,这样既点明了身份,也说明了创造者。然后补充一下免费可用和知识截止时间,这样信息比较完整。最后用开放式的提问结束,保持友好和乐于助人的语气。\n\n 嗯,一句话的话,可以把这些信息整合成一句流畅的话,就是“你好!我是DeepSeek,由深度求索公司创造的AI助手,免费且支持超长上下文,很高兴为你服务~”。" },
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 11,
"completion_tokens": 162,
"total_tokens": 173,
"prompt_tokens_details": {
"cached_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 134
},
"prompt_cache_hit_tokens": 0,
"prompt_cache_miss_tokens": 11
},
"system_fingerprint": "fp_9954b31ca7_prod0820_fp8_kvcache_20260402"
}工具定义
const tools = [
{
type: "function",
function: {
name: "getCurrentTime",
description: "获取当前的日期和时间,包括年、月、日、时、分、秒以及星期几",
parameters: {
type: "object",
properties: {
timezone: {
type: "string",
description: "时区,例如 'Asia/Shanghai'、'America/New_York',默认为本地时区",
},
},
},
},
},
];- type:用来表示你这个工具的类型是一个函数,部分框架还支持
retrieval或web_search等类型 - name:当前函数的唯一标识符,Agent会返回名字来告诉你使用哪个工具
- description:工具的描述,用来让Agent知道合时该用这个工具
- parameters:参数的类型,其中type是根参数的类型,properties是具体的参数列表
让我们接着来看一下返回的数据
{
"id": "2e16c162-7392-4267-aa68-4fc3c41276dd",
"object": "chat.completion",
"created": 1781965948,
"model": "deepseek-v4-pro",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "",
"reasoning_content": "The user wants to know the current time in Beijing. I should use the getCurrentTime function with the timezone \"Asia/Shanghai\" which is the timezone for Beijing.",
"tool_calls": [
{
"index": 0,
"id": "call_00_gtvp7oMzKmS1UhTELmlR8838",
"type": "function",
"function": {
"name": "getCurrentTime",
"arguments": "{\"timezone\": \"Asia/Shanghai\"}"
}
}
]
},
"logprobs": null,
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 316,
"completion_tokens": 86,
"total_tokens": 402,
"prompt_tokens_details": {
"cached_tokens": 256
},
"completion_tokens_details": {
"reasoning_tokens": 37
},
"prompt_cache_hit_tokens": 256,
"prompt_cache_miss_tokens": 60
},
"system_fingerprint": "fp_9954b31ca7_prod0820_fp8_kvcache_20260402"
}这里的核心是tool_calls,这个参数只有在Agent想要调用工具的时候会出现,里面包含了调用工具的所有所需内容
然后我们进行第二轮输入
[
{
"role": "user",
"content": "现在北京时间几点了?"
},
{
"role": "assistant",
"content": "",
"reasoning_content": "用户想知道现在北京时间几点了。我需要调用getCurrentTime工具,指定时区为\"Asia/Shanghai\"来获取北京时间。",
"tool_calls": [
{
"index": 0,
"id": "call_00_N4sF8qei9hTzLWnW4z7a1992",
"type": "function",
"function": {
"name": "getCurrentTime",
"arguments": "{\"timezone\": \"Asia/Shanghai\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_00_N4sF8qei9hTzLWnW4z7a1992",
"content": "2026年06月20日星期六 22:34:43"
}
]我们发现这里出现了一个新的角色:tool,这就是向Agent输入我们返回的数据
另外值得一提的是,在DeepSeekAPI中,如果使用了思考模式且调用工具,那么历史消息中一定要返回思考的内容