大模型 API 接入实战:从零构建 AI 应用

大模型 API 接入实战

一、基础调用

const resp = await fetch('https://api.example.com/v1/chat/completions', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: '你好' }]
  })
});

二、核心参数

参数 说明
temperature 随机性,越低越稳定
max_tokens 生成上限
top_p 采样策略

三、构建智能助手

通过 System Prompt 设定角色,构建上下文实现多轮对话。