大模型多轮对话技巧

大模型多轮对话技巧
temperature 如果是0每次返回的是增量代码temperature 如果是0.7每次返回的是全部代码没有多轮对话的效果。import urllib import json import requests import base64 from http.client import HTTPException from typing import List, Dict, Any, Optional from pathlib import Path import os from img_val.val_img_xiti import test_single_image from tools.ggb_html_tool import generate_multi_ggb_html _MODEL_ID None BASE_URL http://192.168.100.203:17890 # 请根据实际服务地址修改 def get_model_id() - str: 获取 vLLM 服务的模型 ID并缓存 global _MODEL_ID if _MODEL_ID is not None: return _MODEL_ID try: resp urllib.request.urlopen(f{BASE_URL}/v1/models, timeout30) data json.loads(resp.read().decode(utf-8)) _MODEL_ID data[data][0][id] print(f获取到模型 ID: {_MODEL_ID}) return _MODEL_ID except Exception as e: print(f获取模型 ID 失败: {e}) raise HTTPException(status_code500, detail无法获取 vLLM 模型 ID) def chat_with_text(text: str, system_prompt: str , temperature: float 0.7, history: Optional[List[Dict[str, str]]] None, stream: bool True) - Dict[str, Any]: model_id get_model_id() messages [] if system_prompt: messages.append({role: system, content: system_prompt}) if history: messages.extend(history) # 添加当前用户消息 messages.append({role: user, content: text}) payload {model: model_id, messages: messages, temperature: temperature, max_tokens: 4096, stream: stream, } full_response try: response requests.post(f{BASE_URL}/v1/chat/completions, jsonpayload, streamstream, timeout300) if response.status_code ! 200: print(f❌ Error: {response.status_code}) print(response.text) return {response: , history: history or []} if stream: # 流式输出 for line in response.iter_lines(): if line: line line.decode(utf-8) if line.startswith(data: ): data line[6:] if data [DONE]: break try: chunk json.loads(data) choices chunk.get(choices, []) if choices: delta choices[0].get(delta, {}) content_delta delta.get(content, ) if content_delta: print(content_delta, end, flushTrue) full_response content_delta except json.JSONDecodeError: continue print() # 换行 else: # 非流式输出 data response.json() full_response data.get(choices, [{}])[0].get(message, {}).get(content, ) print(full_response) # 更新历史记录 updated_history (history or []).copy() updated_history.append({role: user, content: text}) updated_history.append({role: assistant, content: full_response}) return {response: full_response, history: updated_history} except requests.exceptions.Timeout: print(❌ Request timeout) return {response: , history: history or []} except Exception as e: print(f❌ Error: {e}) return {response: , history: history or []} class ChatSession: 对话会话类方便管理多轮对话 def __init__(self, system_prompt: str , temperature: float 0.7): self.system_prompt system_prompt self.temperature temperature self.history: List[Dict[str, str]] [] self.responses: List[str] [] def ask(self, text: str, stream: bool True) - str: result chat_with_text(texttext, system_promptself.system_prompt, temperatureself.temperature, historyself.history, streamstream) self.history result[history] response result[response] self.responses.append(response) return response def clear_history(self): 清空对话历史 self.history [] self.responses [] def get_full_conversation(self) - str: 获取完整的对话文本 conversation [] for msg in self.history: role 用户 if msg[role] user else 助手 conversation.append(f{role}: {msg[content]}) return \n\n.join(conversation) if __name__ __main__: # 方式1直接使用 chat_with_text 函数手动管理历史 if 0: print(方式1手动管理历史) history [] questions [画一个圆 生成ggb代码不要注释, 圆内画一个内接三角形ABC, 过点C 画AB的垂线, 过点A 画角A的角平分线,画一个三角形] for i, q in enumerate(questions): print(f\n--- 第 {i 1} 轮对话 ---) print(f用户: {q}) result chat_with_text(textq, system_prompt你是一个数学几何助手请根据要求生成GeoGebra代码。, historyhistory,temperature0.0, streamTrue) history result[history] print(fresponse: {result[response]}) # 只打印前100字符 if 1: # 方式2使用 ChatSession 类更方便 print(方式2使用 ChatSession 类) session ChatSession(system_prompt你是一个数学几何助手请根据要求生成GeoGebra代码。, temperature0.7) questions [画一个圆 生成ggb代码不要注释, 圆内画一个内接三角形ABC, 过点C 画AB的垂线, 过点A 画角A的角平分线,画一个四边形] for i, q in enumerate(questions): print(f--- 第 {i 1} 轮对话 ---) print(fask: {q}) response session.ask(q, streamTrue) print(fresponse: {response}) # 打印完整对话历史 print(\n * 50) print(完整对话历史:) print( * 50) print(session.get_full_conversation())