欧美做电影 迅雷下载网站,二次开发手册,做分销的官网网站,设计网站怎么设计一、环境#xff08;一定要按照以下的版本#xff0c;惨痛的教训#xff09;1. vllm0.6.1#xff08;建议使用uv pip安装#xff09;2. gradio5.49.1#xff08;建议使用uv pip安装#xff09;3. python3.12.0#xff08;建议使用conda安装虚拟环境#xff09;二、vll…一、环境一定要按照以下的版本惨痛的教训1. vllm0.6.1建议使用uv pip安装2. gradio5.49.1建议使用uv pip安装3. python3.12.0建议使用conda安装虚拟环境二、vllm后端1. 代码如下怎么下载Qwen2.5模型参考上一篇文章import os import uvicorn import time from fastapi import FastAPI, HTTPException from fastapi.responses import StreamingResponse from pydantic import BaseModel from typing import List, Dict, Generator # 环境变量配置 os.environ[HUGGINGFACE_HUB_CACHE] r/home/plc/cache os.environ[HF_ENDPOINT] https://hf-mirror.com # 导入库 from vllm import LLM, SamplingParams from transformers import AutoTokenizer # 加载分词器 model_name Qwen/Qwen2.5-3B tokenizer AutoTokenizer.from_pretrained( model_name, trust_remote_codeTrue, mirrorhttps://hf-mirror.com ) # 加载模型优化重复控制 print(正在加载Qwen2.5模型...) llm LLM( modelmodel_name, trust_remote_codeTrue, dtypefloat16, gpu_memory_utilization0.8, max_num_batched_tokens4096, max_model_len2048, max_num_seqs2, disable_log_statsTrue, download_diros.environ[HUGGINGFACE_HUB_CACHE], seed42, ) print(模型加载完成) # FastAPI应用 app FastAPI(titlevllm 0.6.1 大模型服务) # 请求体 class ChatRequest(BaseModel): messages: List[Dict[str, str]] # 流式聊天接口修复重复问题 app.post(/chat/stream) def chat_stream(request: ChatRequest) - StreamingResponse: if not request.messages: raise HTTPException(status_code400, detailmessages不能为空) # 构建Prompt增加“不重复”提示 # 关键在system prompt中明确要求“不要生成重复内容” system_prompt request.messages[0][content] if request.messages[0][role] system else system_prompt \n注意输出内容需连贯禁止生成重复、无意义的字符或句子。代码部分之间需要换行输出 # 重新构造messages更新system prompt new_messages [{role: system, content: system_prompt}] request.messages[1:] # 应用Qwen模板 prompt tokenizer.apply_chat_template( new_messages, tokenizeFalse, add_generation_promptTrue, ) print(f最终Prompt\n{prompt}) # 通用问答最优配置平衡稳定性多样性无重复 sampling_params SamplingParams( max_tokens800, # 3B模型无需过长输出800足够覆盖绝大多数问答场景 temperature0.6, # 0.6是3B模型的黄金值兼顾稳定和少量多样性 top_p0.9, # 配合temperature过滤低概率词汇提升输出质量 repetition_penalty1.15, # 3B模型最优重复惩罚1.1~1.2区间 skip_special_tokensTrue, stop[|im_end|, /s, \n\n\n], # 保留有效停止标记新增连续换行标记 seedNone, # 生产环境设为None测试时可固定如42 ) # 定义生成器增加重复检测 def generate_stream() - Generator[str, None, None]: try: outputs llm.generate(prompts[prompt], sampling_paramssampling_params) generated_text outputs[0].outputs[0].text.strip() # 重复内容检测如果出现连续重复字符串截断 if GSGSG in generated_text: generated_text generated_text.split(GSGSG)[0].strip() # 检测连续重复的字符如“aaaaa” if len(generated_text) 10: last_char generated_text[-1] if generated_text[-5:] last_char*5: generated_text generated_text[:-5].strip() # 流式返回 for char in generated_text: yield fdata: {char}\n\n time.sleep(0.005) yield data: [DONE]\n\n except Exception as e: yield fdata: ERROR: {str(e)}\n\n return StreamingResponse( generate_stream(), media_typetext/event-stream, headers{ Cache-Control: no-cache, X-Accel-Buffering: no, Connection: keep-alive, Content-Type: text/event-stream; charsetutf-8 } ) # 启动服务 if __name__ __main__: uvicorn.run( app, host0.0.0.0, port8001, reloadFalse, workers1 )三、gradio前端1. 代码如下代码中需要调整温度、重复等参数因为我们使用了小参数模型import gradio as gr import requests import time # API流式服务地址 API_STREAM_URL http://localhost:8001/chat/stream # 工控大模型系统提示词 SYSTEM_PROMPT 你是一名PLC代码专家请参考给出的标准功能块信息和仿照示例代码,生成符合IEC61131-3标准和plcopen标准的功能代码,要求代码尽可能详细不要使用while循环。 示例代码: PROGRAM PLC_PRG VAR i :INT; mcPower :MC_Power; mcMoveAbs :MC_MoveAbsolute; mcReset :MC_Reset; mcReadActPos :MC_ReadActualTorque; MoveExe: BOOL; END_VAR IF DIO THEN MoveExe : TRUE; END_IF mcPower( Axis: Axis1, Enable: , bRegulatorOn: , bDriveStart: , Status , bRegulatorRealState , bDriveStartRealState , Busy , Error , ErrorID ); 输出时请使用代码块格式 # 流式调用API的生成器函数优化解析逻辑 def call_chat_api_stream(message, history): 流式接收后端响应逐字更新界面 :param message: 当前用户输入 :param history: Gradio历史对话 [(用户1, 助手1), ...] :return: 生成器逐次返回更新后的历史 # 构建完整的messages列表确保格式正确 messages [{role: system, content: SYSTEM_PROMPT.strip()}] for user_msg, assistant_msg in history: if user_msg and user_msg.strip(): messages.append({role: user, content: user_msg.strip()}) if assistant_msg and assistant_msg.strip(): messages.append({role: assistant, content: assistant_msg.strip()}) messages.append({role: user, content: message.strip()}) try: # 发送流式POST请求优化超时和连接配置 response requests.post( API_STREAM_URL, json{messages: messages}, streamTrue, timeout300, # 延长超时时间 headers{Content-Type: application/json} ) if response.status_code ! 200: history.append((message, f请求失败状态码{response.status_code}{response.text[:200]})) yield history return # 逐行接收流式数据优化解析逻辑 full_response history.append((message, )) # 初始化空回复 for line in response.iter_lines(chunk_size1, decode_unicodeTrue): if not line: continue # 严格解析SSE格式 if line.startswith(data: ): content line[6:].strip() # 处理结束标记 if content [DONE]: break # 处理错误信息 if content.startswith(ERROR:): history[-1] (message, f生成错误{content[6:]}) yield history return # 逐字符追加内容 if content: full_response content history[-1] (message, full_response) yield history # 实时更新界面 time.sleep(0.005) # 优化刷新频率 except requests.exceptions.Timeout: history.append((message, 请求超时超过5分钟请简化问题重试)) yield history except requests.exceptions.ConnectionError: history.append((message, f无法连接到后端服务{API_STREAM_URL}\n请检查后端是否启动)) yield history except Exception as e: history.append((message, f调用出错{str(e)})) yield history # 创建Gradio界面 with gr.Blocks(title工控大模型流式版) as demo: gr.Markdown( # 工控大模型 基于Qwen模型微调支持流式输出的PLC代码生成工具 ) # 聊天界面 chatbot gr.Chatbot( label问答对话流式输出, height600, bubble_full_widthFalse, ) # 输入框 msg gr.Textbox( label请输入你的问题, placeholder例如编写一段西门子S7-1200的电机启停PLC程序, lines4 ) # 按钮区域 with gr.Row(): submit_btn gr.Button(发送, variantprimary) clear_btn gr.Button(清空对话) # 绑定事件 submit_btn.click( fncall_chat_api_stream, inputs[msg, chatbot], outputschatbot, queueTrue ).then( fnlambda: , outputsmsg ) msg.submit( fncall_chat_api_stream, inputs[msg, chatbot], outputschatbot, queueTrue ).then( fnlambda: , outputsmsg ) clear_btn.click( fnlambda: [], outputschatbot ).then( fnlambda: , outputsmsg ) # 启动前端 if __name__ __main__: demo.queue(max_size10) # 增加队列大小 demo.launch( server_name0.0.0.0, server_port7860, shareFalse, inbrowserTrue, show_errorTrue # 显示错误信息便于调试 )2. 为什么使用Qwen2系列呢因为vllm的版本偏低。