实现聊天参数面板

本次功能

支持调:

  • temperature
  • top_p
  • max_tokens

效果:

  • 每个会话可独立配置参数
  • 调参后立即影响后续对话
  • 更像正式 AI Playground / Agent 调试台

1)改 web/src/utils/session.js

createSession

yaml 复制代码
  return {
    id: crypto.randomUUID(),
    title,
    mode,
    customPrompt: persona.systemPrompt,
    temperature: 0.7,
    topP: 1,
    maxTokens: 1200,
    pinned: false,
    createdAt: Date.now(),
    updatedAt: Date.now(),
    messages: [

loadSessions 里的 normalize

yaml 复制代码
      return {
        mode,
        customPrompt:
          item.customPrompt ||
          item.messages?.find(m => m.role === 'system')?.content ||
          persona.systemPrompt,
        temperature: 0.7,
        topP: 1,
        maxTokens: 1200,
        pinned: false,
        ...item,
      }

2)改 server/app.py

ChatRequest 新增字段

python 复制代码
class ChatRequest(BaseModel):
    messages: List[Message]
    session_id: Optional[str] = None
    temperature: Optional[float] = 0.7
    top_p: Optional[float] = 1
    max_tokens: Optional[int] = 1200

/api/chat 里替换模型调用参数

ini 复制代码
        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=final_messages,
            temperature=req.temperature or 0.7,
            top_p=req.top_p or 1,
            max_tokens=req.max_tokens or 1200,
        )

/api/chat/stream 里替换模型调用参数

ini 复制代码
            stream = client.chat.completions.create(
                model=MODEL_NAME,
                messages=final_messages,
                temperature=req.temperature or 0.7,
                top_p=req.top_p or 1,
                max_tokens=req.max_tokens or 1200,
                stream=True,
            )

3)改 web/src/App.vue

新增计算属性

ruby 复制代码
const currentParams = computed(() => {
  return {
    temperature: currentSession.value?.temperature ?? 0.7,
    topP: currentSession.value?.topP ?? 1,
    maxTokens: currentSession.value?.maxTokens ?? 1200,
  }
})

新增状态

csharp 复制代码
const temperatureDraft = ref(0.7)
const topPDraft = ref(1)
const maxTokensDraft = ref(1200)

新增监听

ini 复制代码
watch(
  currentParams,
  newVal => {
    temperatureDraft.value = newVal.temperature
    topPDraft.value = newVal.topP
    maxTokensDraft.value = newVal.maxTokens
  },
  { immediate: true, deep: true }
)

新增方法

javascript 复制代码
const handleSaveParams = () => {
  if (!currentSession.value) return

  const nextTemperature = Math.min(2, Math.max(0, Number(temperatureDraft.value) || 0.7))
  const nextTopP = Math.min(1, Math.max(0, Number(topPDraft.value) || 1))
  const nextMaxTokens = Math.min(4000, Math.max(100, Number(maxTokensDraft.value) || 1200))

  sessions.value = sortSessions(
    sessions.value.map(item =>
      item.id === currentSessionId.value
        ? {
            ...item,
            temperature: nextTemperature,
            topP: nextTopP,
            maxTokens: nextMaxTokens,
            updatedAt: Date.now(),
          }
        : item
    )
  )
}

sendMessageStream

yaml 复制代码
    body: JSON.stringify({
      messages,
      session_id: currentSession.value.id,
      temperature: currentSession.value.temperature,
      top_p: currentSession.value.topP,
      max_tokens: currentSession.value.maxTokens,
    }),

4)改模板

ini 复制代码
<div class="params-panel">
  <div class="params-panel-header">
    <div class="params-panel-title">聊天参数</div>
    <button class="prompt-btn" @click="handleSaveParams">保存参数</button>
  </div>

  <div class="params-grid">
    <div class="param-item">
      <label class="param-label">temperature</label>
      <input v-model="temperatureDraft" class="param-input" type="number" min="0" max="2" step="0.1" />
      <div class="param-tip">越高越发散,越低越稳定</div>
    </div>

    <div class="param-item">
      <label class="param-label">top_p</label>
      <input v-model="topPDraft" class="param-input" type="number" min="0" max="1" step="0.1" />
      <div class="param-tip">控制采样范围</div>
    </div>

    <div class="param-item">
      <label class="param-label">max_tokens</label>
      <input v-model="maxTokensDraft" class="param-input" type="number" min="100" max="4000" step="100" />
      <div class="param-tip">限制单次最大输出长度</div>
    </div>
  </div>
</div>

5)补充样式

css 复制代码
.params-panel {
  margin-bottom: 16px;
  padding: 16px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  background: #fafafa;
}

.params-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.params-panel-title {
  font-size: 16px;
  font-weight: 600;
  color: #111827;
}

.params-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.param-item {
  padding: 12px;
  border-radius: 10px;
  background: #fff;
  border: 1px solid #e5e7eb;
}

.param-label {
  display: block;
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 600;
  color: #111827;
}

.param-input {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  outline: none;
  background: #fff;
}

.param-tip {
  margin-top: 8px;
  font-size: 12px;
  color: #6b7280;
}

6)验证

测试 temperature

设成:

复制代码
0.2

问同一个开放问题两次,回答会更稳定。

再设成:

复制代码
0.7

回答会更发散。

测试 max_tokens

设成:

复制代码
100

让 AI 输出长文,回复会明显变短。

nice !

本次提交修改地址

github.com/fhj414/ai-c...

相关推荐
孔汤姆2 小时前
ESP32 掌机改造全记录:WiFi 遥测、一键启动 AI 终端
人工智能
HyperAI超神经2 小时前
【vLLM 学习】Disaggregated Prefill
人工智能·深度学习·学习·vllm
kyriewen2 小时前
我踩了三次同一个坑才明白:React里"改了数据却不重新渲染"的真正原因
前端·javascript·react.js
用户059540174462 小时前
把AI对话记忆测试从手动比对到Pytest+Redis自动化,验证效率提升了10倍,还顺带揪出3个隐蔽bug
前端·css
我星期八休息2 小时前
网络编程—网络层
开发语言·前端·网络·人工智能·智能路由器
逻辑君3 小时前
ANNA 7.2 方块机器人实验技术报告
人工智能·深度学习·机器学习·机器人
A24207349303 小时前
React中请求拦截与响应拦截的统一处理方案
前端·javascript·react.js
SomeB1oody3 小时前
【RustyML入门】2.9. MeanShift
开发语言·后端·机器学习·rust·教程
菜鸟‍3 小时前
【论文学习】Medical Image Analysis 2024 || 医学图像分割中失败检测方法的比较基准研究:揭示置信度聚合的作用
人工智能·学习
AI导出鸭3 小时前
怎么让千问做表格?AI导出鸭苹果版将千问输出的管道表格智能解析为二维结构,一键导出为Excel或Word标准表格。
人工智能·chatgpt·word·excel·ai导出鸭