llm

llm就是 Agent 跟大模型“打电话”的那一层,负责“怎么调用大模型”

llm是最底层、最干净的一层它不关心业务、不关心表单、不关心流程

 

1.统一封装大模型调用

1.1OpenAI / Azure OpenAI

1.2Moonshot / DeepSeek

1.3本地模型(Ollama)

 

2.抹平不同模型的差异(超关键),不同模型返回格式不一样:

2.1有的 content是 string 

2.2有的支持toll_calls

2.3有的 JSON 不稳定

负责 “模型 → 统一结构”

 

3.处理工程问题(而不是智能问题),这些都应该在 llm

3.1temperature / max_tokens

3.2超时

3.3重试(retry)

3.4fallback 模型

3.5日志 / debug

3.6JSON 修复(可选)

 

// llm.ts
export interface ChatMessage {
  role: 'system' | 'user' | 'assistant'
  content: string
}

export interface LLMOptions {
  temperature?: number
  json?: boolean
}

export async function callLLM(
  messages: ChatMessage[],
  options?: LLMOptions
): Promise<{ content: string }> {
  const res = await client.chat.completions.create({
    model: 'gpt-4.1-mini',
    messages,
    temperature: options?.temperature ?? 0,
  })

  return {
    content: res.choices[0].message.content ?? '',
  }
}


简单前端demo
// llm.ts
import OpenAI from 'openai'
import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions'

export const client = new OpenAI({
  apiKey: 'xxx',
  baseURL: 'https://api.deepseek.com/v1',
  dangerouslyAllowBrowser: true
})

export async function chat(messages: ChatCompletionMessageParam[]) {
  const res = await client.chat.completions.create({
    model: 'deepseek-chat',
    messages,
    temperature: 0
  })

  return res.choices[0].message.content ?? ''
}

 

temperature模型性格直观感受
0 / 0.1 死板、确定 像写 if-else
0.2 ~ 0.3 稳定、理性 Agent 最常用
0.5 平衡 人味开始有
0.8 发散 开始乱聊
1.0+ 放飞 创作 / 胡扯

posted on 2026-01-23 09:51  sss大辉  阅读(0)  评论(0)    收藏  举报

导航