文心一言4.5开源部署指南及文学领域测评_文心一言 开源
📝个人主页:哈__
期待您的关注
目录
一、引言
2.1 MoE架构
2.2 文心一言MoE架构
三、文心一言稠密模型部署
3.1 产品选择
3.2 环境选择
3.3 Python3.12安装
3.3 PaddlePaddle-GPU安装
3.4 FastDeploy-GPU安装
编辑3.5 模型部署
四、模型测试
4.1 文心一言4.5基础请求测试
编辑
4.2 文心一言4.5多次请求测试
4.3 让文心一言4.5自己想一些测试方向
4.4 中文处理和生成测评
4.4.1 文学文本理解能力(基础核心)
4.4.2 文学知识储备(专业基础)
4.4.3 文学创作能力(核心应用)
五、测试总结
一、引言
文心一言(ERNIE Bot)是百度推出的人工智能大语言模型,它基于飞桨平台和文心知识增强技术,具备文本处理、AI 绘画等功能,能应用于创作、数据分析、代码生成等多个场景,支持 PC、APP 及 API 接入等使用方式,用户规模庞大,生态不断扩展,不同版本在性能上各有提升,满足不同需求。
2025年6月30日,百度做出了一件具有里程碑意义的事情:开源文心一言大模型,这一举措旨在构建更加繁荣的开发者生态系统。此举不仅在行业内引发了广泛关注,也标志着中国AI市场竞争格局的重大变化。百度的这一战略转变,可能会推动整个行业从性能竞争逐步转向价格战,总体来说,文心一言的开源将来可能会推动AI行业的发展。
这一决策将打破AI技术壁垒,让AI社区开发者可以更加直观的了解到文心一言大模型的架构设计和运行机制,对于中小型AI相关企业来说,这是一个足以推动企业快速发展的决策。
二、文心一言开源模型
文心4.5系列开源模型共10款,涵盖了激活参数规模分别为47B和3B的混合专家(MoE)模型(最大的模型总参数量为424B),以及0.3B的稠密参数模型。
开源模型分为两类:MoE模型以及稠密型模型。
2.1 MoE架构
混合专家模型(Mixed Expert Models,简称 MoEs) ,最早是随着 Mixtral 8x7B 的推出而逐渐引起人们的广泛关注。这个概念与集成学习方法相似,旨在为由多个单独网络组成的系统建立一个监管机制。在这种系统中,每个网络 (被称为“专家”) 处理训练样本的不同子集,专注于输入空间的特定区域。
混合专家模型 (MoE) 的一个显著优势是它们能够在远少于稠密模型所需的计算资源下进行有效的预训练。MoE的核心思想用多个 “专家网络(FFNN 1~4)”+“路由器(Router)” 替代传统的单一大 FFNN,让 不同输入由最擅长的 “专家” 处理,实现 “分工协作”。
2.2 文心一言MoE架构
针对 MoE 架构,文心一言提出了一种创新性的多模态异构模型结构,通过跨模态参数共享机制实现模态间知识融合,同时为各单一模态保留专用参数空间。此架构非常适用于从大语言模型向多模态模型的持续预训练范式,在保持甚至提升文本任务性能的基础上,显著增强多模态理解能力。
来看一下官方对于文心一言MoE架构的解释。
多模态混合专家模型预训练
文心4.5通过在文本和视觉两种模态上进行联合训练,更好地捕捉多模态信息中的细微差别,提升在文本生成、图像理解以及多模态推理等任务中的表现。为了让两种模态学习时互相提升,我们提出了一种多模态异构混合专家模型结构,结合了多维旋转位置编码,并且在损失函数计算时,增强了不同专家间的正交性,同时对不同模态间的词元进行平衡优化,达到多模态相互促进提升的目的。
高效训练推理框架
为了支持文心4.5模型的高效训练,我们提出了异构混合并行和多层级负载均衡策略。通过节点内专家并行、显存友好的流水线调度、FP8混合精度训练和细粒度重计算等多项技术,显著提升了预训练吞吐。推理方面,我们提出了多专家并行协同量化方法和卷积编码量化算法,实现了效果接近无损的4-bit量化和2-bit量化。此外,我们还实现了动态角色转换的预填充、解码分离部署技术,可以更充分地利用资源,提升文心4.5 MoE模型的推理性能。基于飞桨框架,文心4.5在多种硬件平台均表现出优异的推理性能。
针对模态的后训练
为了满足实际场景的不同要求,我们对预训练模型进行了针对模态的精调。其中,大语言模型针对通用语言理解和生成进行了优化,多模态大模型侧重于视觉语言理解,支持思考和非思考模式。每个模型采用了SFT、DPO或UPO(UnifiedPreferenceOptimization,统一偏好优化技术)的多阶段后训练。
我理解的文心一言MoE架构设计:
文心4.5系列模型均使用飞桨深度学习框架进行高效训练、推理和部署。在大语言模型的预训练中,模型FLOPs利用率(MFU)达到47%。实验结果显示,该系列模型在多个文本和多模态基准测试中达到SOTA水平,在指令遵循、世界知识记忆、视觉理解和多模态推理任务上效果尤为突出。模型权重按照Apache 2.0协议开源,支持开展学术研究和产业应用。此外,基于飞桨提供开源的产业级开发套件,广泛兼容多种芯片,降低后训练和部署门槛。
三、文心一言稠密模型部署
为方便个人学习,博主部署了 “paddlepaddle/ERNIE-4.5-0.3B-Paddle”模型,这里记录一下部署的流程,详细的模型请参考网站:https://ai.gitcode.com/theme/1939325484087291906?pId=3037
3.1 产品选择
个人学习上,我采购了一张4090卡进行部署,具体配置如下。
3.2 环境选择
我在这里选购了一台包含CUDA驱动的服务器,现在的价格不贵,有活动优惠,0.99/h,感兴趣可以自行了解一下,丹摩DAMODEL|让AI开发更简单!算力租赁上丹摩!
在选择镜像时,直接选择现有的镜像,这里我踩过坑,选择基础的ubuntu部署会有很多错误。
采购好后可通过SSH方式登录服务器。
现在来配置部署所需环境。
3.3 Python3.12安装
安装python3.12之前,首先执行指令更新核心依赖否则会报错。
apt update && apt install -y libgomp1 libssl-dev zlib1g-dev
apt install -y python3.12 python3-pip
安装完成。
由于下方还有不少踩坑的点,这里还需要再去安装一些其他东西。因为我们的环境中是有python3.10的。
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.pypython3.12 get-pip.py --force-reinstallpython3.12 -m pip install --upgrade setuptools
3.3 PaddlePaddle-GPU安装
参考PaddlePaddle官方命令,我们这里安装适配CUDA12.6版本的PaddlePaddle-GPU。
python3.12 -m pip install paddlepaddle-gpu==3.1.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/
输入代码验证 。
import paddlepaddle.utils.run_check()
出现下方输出证明我们成功的安装了适配当前CUDA版本的PaddlePaddle-GPU。
3.4 FastDeploy-GPU安装
官方给出的安装流程要区别于GPU的架构,不同架构的GPU有不同的安装指令。
For SM80/90 architecture GPUs(e.g A30/A100/H100/):
# Install stable releasepython -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-80_90/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple# Install latest Nightly buildpython -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/fastdeploy-gpu-80_90/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
For SM86/89 architecture GPUs(e.g A10/4090/L20/L40):
# Install stable releasepython -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-86_89/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple# Install latest Nightly buildpython -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/fastdeploy-gpu-86_89/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
在这里我们输入下方指令安装。
python3.12 -m pip install fastdeploy-gpu -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-80_90/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
3.5 模型部署
输入下方的命令,当页面中出现端口证明我们成功的部署了模型
python3.12 -m fastdeploy.entrypoints.openai.api_server \\ --model baidu/ERNIE-4.5-0.3B-Paddle \\ --port 8180 \\ --metrics-port 8181 \\ --engine-worker-queue-port 8182 \\ --max-model-len 32768 \\ --max-num-seqs 32
四、模型测试
4.1 文心一言4.5基础请求测试
首先对于文心一言进行基础的请求测试,看看文心一言会不会正常的返回数据给我们。
来看一个典型的逻辑问题吧,3.11和3.8哪个大。
import requestsimport json def main(): url = \"http://127.0.0.1:8180/v1/chat/completions\" headers = { \"Content-Type\": \"application/json\" } data = { \"model\": \"baidu/ERNIE-4.5-0.3B-PT\", \"messages\": [ { \"role\": \"user\", \"content\": \"你觉得3.11和3.8哪个大,我觉得3.11大,因为11大于8,你觉得我说的对吗\" } ] } try: response = requests.post(url, headers=headers, data=json.dumps(data)) response.raise_for_status() result = response.json() print(\"状态码:\", response.status_code) print(\"响应内容:\") print(json.dumps(result, indent=2, ensure_ascii=False)) # 提取并打印AI的回复内容 if \"choices\" in result and len(result[\"choices\"]) > 0: ai_result = result[\"choices\"][0][\"message\"][\"content\"] print() print(\"\\nAI回复:\", ai_result) except requests.exceptions.RequestException as e: print(f\"请求错误: {e}\") except json.JSONDecodeError: print(f\"JSON解析错误,响应内容: {response.text}\") except Exception as e: print(f\"发生错误: {e}\") if __name__ == \"__main__\": main()
哈哈,从结果来看文心一言确实给出了答案,不过是错误的,因为我部署的是参数最小的模型,可以理解。基本的沟通是没有问题的
现在让我们调整代码,连续问答。
import requestsimport jsondef main(): url = \"http://127.0.0.1:8180/v1/chat/completions\" headers = {\"Content-Type\": \"application/json\"} # 第一轮对话:询问3.11和3.8哪个大 data1 = { \"model\": \"baidu/ERNIE-4.5-0.3B-PT\", \"messages\": [ { \"role\": \"user\", \"content\": \"你觉得3.11和3.8哪个大,我觉得3.11大,因为11大于8,你觉得我说的对吗\" } ] } try: # 发送第一轮请求 response1 = requests.post(url, headers=headers, data=json.dumps(data1)) response1.raise_for_status() result1 = response1.json() # 提取AI回复内容 ai_reply = result1[\"choices\"][0][\"message\"][\"content\"] if \"choices\" in result1 and len(result1[\"choices\"]) > 0 else \"\" print(\"第一轮对话 - 用户问题:\") print(data1[\"messages\"][0][\"content\"]) print(\"\\n第一轮对话 - AI回复:\") print(ai_reply) # 构建第二轮对话,追加\"你确定你说的答案吗\" data2 = { \"model\": \"baidu/ERNIE-4.5-0.3B-PT\", \"messages\": [ { \"role\": \"user\", \"content\": \"你觉得3.11和3.8哪个大,我觉得3.11大,因为11大于8,你觉得我说的对吗\" }, { \"role\": \"assistant\", \"content\": ai_reply }, { \"role\": \"user\", \"content\": \"你确定你说的答案吗\" } ] } # 发送第二轮请求 response2 = requests.post(url, headers=headers, data=json.dumps(data2)) response2.raise_for_status() result2 = response2.json() # 提取第二轮AI回复内容 ai_reply2 = result2[\"choices\"][0][\"message\"][\"content\"] if \"choices\" in result2 and len(result2[\"choices\"]) > 0 else \"\" print(\"\\n\\n第二轮对话 - 用户追问:\") print(data2[\"messages\"][2][\"content\"]) print(\"\\n第二轮对话 - AI回复:\") print(ai_reply2) except requests.exceptions.RequestException as e: print(f\"请求错误: {e}\") except json.JSONDecodeError: print(f\"JSON解析错误,响应内容: {response.text}\") except Exception as e: print(f\"发生错误: {e}\")if __name__ == \"__main__\": main()
可以,连续问答的功能都有。不过我部署的是小参数模型,这些不重要。
4.2 文心一言4.5多次请求测试
我们在官方访问AI时,有时候我们连续询问AI问题,AI的回复会有卡顿,现在我们模拟多次请求测试,检测文心一言模型的回答情况。这里准备了多个问题。
import requestsimport jsonimport timefrom random import choicedef main(): # 基础配置 url = \"http://127.0.0.1:8180/v1/chat/completions\" headers = {\"Content-Type\": \"application/json\"} model = \"baidu/ERNIE-4.5-0.3B-PT\" questions = [ \"Python列表和元组的区别?\", \"什么是HTTP 200状态码?\", \"解释面向对象的多态?\", \"如何优化SQL查询?\", \"区块链基本原理?\" ] request_count = 5 # 测试次数 delay = 0.5 # 请求间隔(秒) # 数据记录 response_times = [] # 响应时间(毫秒) success_count = 0 # 成功次数 print(f\"开始测试(共{request_count}次请求)...\\n\") for i in range(request_count): question = choice(questions) print(f\"第{i+1}次 | 问题:{question[:30]}...\") # 计时与请求 start = time.perf_counter() try: response = requests.post( url, headers=headers, data=json.dumps({ \"model\": model, \"messages\": [{\"role\": \"user\", \"content\": question}] }), timeout=10 ) response.raise_for_status() result = response.json() # 计算响应时间 cost = (time.perf_counter() - start) * 1000 response_times.append(cost) success_count += 1 # 简要输出 ai_reply = result[\"choices\"][0][\"message\"][\"content\"][:50] if \"choices\" in result else \"格式异常\" print(f\"✅ 成功 | 耗时:{cost:.2f}ms | 回复:{ai_reply}...\") except Exception as e: print(f\"❌ 失败 | 错误:{str(e)}\") # 间隔(最后一次不间隔) if i < request_count - 1: time.sleep(delay) print(\"-\" * 50) # 生成报告 print(\"\\n\" + \"=\"*30) print(\"性能测试报告\") print(\"=\"*30) print(f\"总请求数:{request_count}\") print(f\"成功数:{success_count} | 失败数:{request_count - success_count}\") print(f\"成功率:{success_count/request_count*100:.1f}%\") if response_times: print(\"\\n响应时间统计(毫秒):\") print(f\"平均:{sum(response_times)/len(response_times):.2f}\") print(f\"最小:{min(response_times):.2f}\") print(f\"最大:{max(response_times):.2f}\") else: print(\"\\n无有效响应时间数据(全部失败)\")if __name__ == \"__main__\": main()
4.3 让文心一言4.5自己想一些测试方向
import requestsimport json def main(): url = \"http://127.0.0.1:8180/v1/chat/completions\" headers = { \"Content-Type\": \"application/json\" } data = { \"model\": \"baidu/ERNIE-4.5-0.3B-PT\", \"messages\": [ { \"role\": \"user\", \"content\": \"你做为文心一言大模型,你擅长什么领域,针对于这些领域给出一些测评思路\" } ] } try: response = requests.post(url, headers=headers, data=json.dumps(data)) response.raise_for_status() result = response.json() print(\"状态码:\", response.status_code) print(\"响应内容:\") print(json.dumps(result, indent=2, ensure_ascii=False)) # 提取并打印AI的回复内容 if \"choices\" in result and len(result[\"choices\"]) > 0: ai_result = result[\"choices\"][0][\"message\"][\"content\"] print() print(\"\\nAI回复:\", ai_result) except requests.exceptions.RequestException as e: print(f\"请求错误: {e}\") except json.JSONDecodeError: print(f\"JSON解析错误,响应内容: {response.text}\") except Exception as e: print(f\"发生错误: {e}\") if __name__ == \"__main__\": main()
针对于给出的测评思路,我们主要在中文处理方向进行测评。
4.4 中文处理和生成测评
4.4.1 文学文本理解能力(基础核心)
import requestsimport jsonimport timedef test_literature_comprehension(): \"\"\"测试大模型对文学文本的理解能力\"\"\" url = \"http://127.0.0.1:8180/v1/chat/completions\" headers = {\"Content-Type\": \"application/json\"} model = \"baidu/ERNIE-4.5-0.3B-PT\" # 定义测试问题集 test_questions = [ { \"id\": 1, \"category\": \"小说细节理解\", \"question\": \"「《红楼梦》中,黛玉葬花时所葬的花主要是什么品种?这一行为与她的人物性格有何关联?」\", \"expected_answer_keywords\": [\"桃花\", \"敏感\", \"孤傲\", \"对美好易逝的感伤\"] }, { \"id\": 2, \"category\": \"诗歌隐喻解读\", \"question\": \"「分析李商隐《锦瑟》中『庄生晓梦迷蝴蝶,望帝春心托杜鹃』两句的隐喻意义,涉及哪些典故?」\", \"expected_answer_keywords\": [\"庄周梦蝶\", \"望帝化鹃\", \"人生困惑\", \"情感怅惘\"] }, { \"id\": 3, \"category\": \"叙事结构分析\", \"question\": \"「莫言《红高粱家族》采用『非线性叙事』,请举例说明这种结构如何增强作品的历史厚重感?」\", \"expected_answer_keywords\": [\"时空交错\", \"家族记忆\", \"战争创伤\"] } ] test_results = [] for q in test_questions: print(f\"\\n===== 测试 {q[\'id\']}: {q[\'category\']} =====\") print(f\"问题: {q[\'question\']}\") # 构建请求体 data = { \"model\": model, \"messages\": [{\"role\": \"user\", \"content\": q[\"question\"]}] } try: # 发送请求并计时 start_time = time.time() response = requests.post(url, headers=headers, data=json.dumps(data), timeout=30) response.raise_for_status() result = response.json() end_time = time.time() # 提取AI回复 ai_reply = result[\"choices\"][0][\"message\"][\"content\"] if \"choices\" in result and len(result[\"choices\"]) > 0 else \"\" # 评估回答质量 keyword_matches = [kw for kw in q[\"expected_answer_keywords\"] if kw.lower() in ai_reply.lower()] # 记录结果 test_results.append({ \"question_id\": q[\"id\"], \"question\": q[\"question\"], \"response_time\": end_time - start_time, \"success\": True, \"matched_keywords\": keyword_matches, \"ai_reply\": ai_reply[:200] + \"...\" if len(ai_reply) > 200 else ai_reply }) print(f\"✅ 成功 | 耗时: {end_time - start_time:.2f}秒 | 得分: {score:.1f}分\") print(f\"匹配关键词: {\', \'.join(keyword_matches)}\") print(f\"回复: {ai_reply}...\") except Exception as e: print(f\"❌ 失败: {str(e)}\") test_results.append({ \"question_id\": q[\"id\"], \"question\": q[\"question\"], \"success\": False, \"error\": str(e) }) # 生成测试报告 generate_report(test_results, \"文学文本理解能力测试\")def generate_report(results, test_type): \"\"\"生成测试报告\"\"\" success_count = sum(1 for r in results if r.get(\"success\", False)) total_count = len(results) print(\"\\n\" + \"=\"*50) print(f\"{test_type} 报告\") print(\"=\"*50) print(f\"总测试题数: {total_count}\") print(f\"成功数: {success_count} | 失败数: {total_count - success_count}\") if __name__ == \"__main__\": test_literature_comprehension()
从结果来看,对文学有一定的理解。
4.4.2 文学知识储备(专业基础)
import requestsimport jsonimport timedef test_literature_knowledge(): \"\"\"测试大模型的文学知识储备\"\"\" url = \"http://127.0.0.1:8180/v1/chat/completions\" headers = {\"Content-Type\": \"application/json\"} model = \"baidu/ERNIE-4.5-0.3B-PT\" # 定义测试问题集 test_questions = [ { \"id\": 1, \"category\": \"作家与代表作匹配\", \"question\": \"「下列作家与其代表作对应错误的是哪一项?A. 卡夫卡-《变形记》 B. 沈从文-《边城》 C. 卡尔维诺-《百年孤独》 D. 铁凝-《哦,香雪》」\", \"expected_answer\": \"C\", \"explanation\": \"《百年孤独》作者是加西亚·马尔克斯,卡尔维诺的代表作是《看不见的城市》等\" }, { \"id\": 2, \"category\": \"文学流派特征\", \"question\": \"「什么是『魔幻现实主义』?除了《百年孤独》,再列举3部该流派的拉美文学作品。」\", \"expected_answer_keywords\": [\"现实与魔幻融合\", \"佩德罗·巴拉莫\", \"家长的没落\", \"跳房子\"] }, { \"id\": 3, \"category\": \"文学史事件影响\", \"question\": \"「1919年胡适《尝试集》出版对中国现代文学的意义是什么?它引发了哪些文学论争?」\", \"expected_answer_keywords\": [\"第一部白话诗集\", \"推动白话文运动\", \"文白之争\"] } ] test_results = [] for q in test_questions: print(f\"\\n===== 测试 {q[\'id\']}: {q[\'category\']} =====\") print(f\"问题: {q[\'question\']}\") # 构建请求体 data = { \"model\": model, \"messages\": [{\"role\": \"user\", \"content\": q[\"question\"]}] } try: # 发送请求并计时 start_time = time.time() response = requests.post(url, headers=headers, data=json.dumps(data), timeout=30) response.raise_for_status() result = response.json() end_time = time.time() # 提取AI回复 ai_reply = result[\"choices\"][0][\"message\"][\"content\"] if \"choices\" in result and len(result[\"choices\"]) > 0 else \"\" # 评估回答质量 if \"expected_answer\" in q: # 选择题评估 is_correct = q[\"expected_answer\"].lower() in ai_reply.lower() score = 100 if is_correct else 0 explanation = q.get(\"explanation\", \"\") eval_result = { \"is_correct\": is_correct, \"expected\": q[\"expected_answer\"], \"explanation\": explanation } else: # 关键词匹配评估 keyword_matches = [kw for kw in q[\"expected_answer_keywords\"] if kw.lower() in ai_reply.lower()] eval_result = { \"matched_keywords\": keyword_matches } # 记录结果 test_results.append({ \"question_id\": q[\"id\"], \"question\": q[\"question\"], \"response_time\": end_time - start_time, \"success\": True, \"evaluation\": eval_result, \"ai_reply\": ai_reply[:200] + \"...\" if len(ai_reply) > 200 else ai_reply }) print(f\"✅ 成功 | 耗时: {end_time - start_time:.2f}秒 | 得分: {score:.1f}分\") if \"is_correct\" in eval_result: print(f\"结果: {\'正确\' if eval_result[\'is_correct\'] else \'错误\'} | 正确答案: {eval_result[\'expected\']}\") if eval_result.get(\"explanation\"): print(f\"解释: {eval_result[\'explanation\']}\") else: print(f\"匹配关键词: {\', \'.join(eval_result[\'matched_keywords\'])}\") print(f\"回复: {ai_reply}...\") except Exception as e: print(f\"❌ 失败: {str(e)}\") test_results.append({ \"question_id\": q[\"id\"], \"question\": q[\"question\"], \"success\": False, \"error\": str(e) }) # 生成测试报告 generate_report(test_results, \"文学知识储备测试\")def generate_report(results, test_type): \"\"\"生成测试报告\"\"\" success_count = sum(1 for r in results if r.get(\"success\", False)) total_count = len(results) print(\"\\n\" + \"=\"*50) print(f\"{test_type} 报告\") print(\"=\"*50) print(f\"总测试题数: {total_count}\") print(f\"成功数: {success_count} | 失败数: {total_count - success_count}\") if __name__ == \"__main__\": test_literature_knowledge()
这里我对结果进行省略了。
4.4.3 文学创作能力(核心应用)
这里才是真正的测试,我们一般都是要靠大模型写作的,看看0.3B的模型效果如何。
import requestsimport jsonimport timedef test_literature_creativity(): \"\"\"测试大模型的文学创作能力\"\"\" # 基础配置 url = \"http://127.0.0.1:8180/v1/chat/completions\" headers = {\"Content-Type\": \"application/json\"} model = \"baidu/ERNIE-4.5-0.3B-PT\" # 创作任务集(覆盖仿写、续写、原创) creation_tasks = [ { \"id\": 1, \"task_type\": \"仿写经典风格\", \"task\": \"模仿鲁迅《呐喊》的语言风格(冷峻、讽刺、口语化),写一段100字左右的街头场景描写。\" }, { \"id\": 2, \"task_type\": \"续写经典作品\", \"task\": \"假设《阿Q正传》中阿Q未被处死,续写一段他参加北伐后的经历(200字左右),需符合人物性格(精神胜利法、愚昧)。\" }, { \"id\": 3, \"task_type\": \"原创特定体裁\", \"task\": \"以『秋夜归乡』为主题,写一首七言绝句,要求押韵(平水韵‘尤’部),包含‘月’‘霜’两个意象。\" } ] # 存储测试结果 test_records = [] print(\"开始文学创作能力测试...\\n\") for task in creation_tasks: print(f\"===== 创作任务 {task[\'id\']}: {task[\'task_type\']} =====\") print(f\"任务要求: {task[\'task\']}\\n\") # 构建请求体 data = { \"model\": model, \"messages\": [{\"role\": \"user\", \"content\": task[\"task\"]}] } try: # 记录响应时间 start_time = time.time() response = requests.post( url, headers=headers, data=json.dumps(data), timeout=60 # 创作任务超时设为60秒 ) response.raise_for_status() result = response.json() end_time = time.time() # 提取完整AI创作内容 ai_creation = result[\"choices\"][0][\"message\"][\"content\"] if \"choices\" in result and result[\"choices\"] else \"未生成有效内容\" # 记录结果 test_records.append({ \"task_id\": task[\"id\"], \"task_type\": task[\"task_type\"], \"task\": task[\"task\"], \"success\": True, \"response_time\": round(end_time - start_time, 2), \"ai_creation\": ai_creation }) # 打印完整创作内容 print(f\"✅ 创作完成 | 响应时间: {end_time - start_time:.2f}秒\") print(\"AI创作内容:\\n\" + ai_creation + \"\\n\") except Exception as e: error_msg = str(e) test_records.append({ \"task_id\": task[\"id\"], \"task_type\": task[\"task_type\"], \"task\": task[\"task\"], \"success\": False, \"error\": error_msg }) print(f\"❌ 创作失败 | 错误: {error_msg}\\n\") # 任务间隔 if task[\"id\"] != len(creation_tasks): time.sleep(1) # 创作任务间隔1秒,避免请求过密 # 生成简洁报告 generate_creation_report(test_records)def generate_creation_report(records): \"\"\"生成创作能力测试报告\"\"\" total_tasks = len(records) success_tasks = sum(1 for r in records if r[\"success\"]) fail_tasks = total_tasks - success_tasks print(\"\\n\" + \"=\"*60) print(\"文学创作能力测试报告\") print(\"=\"*60) print(f\"总创作任务数: {total_tasks} | 成功: {success_tasks} | 失败: {fail_tasks}\") if success_tasks > 0: avg_time = sum(r[\"response_time\"] for r in records if r[\"success\"]) / success_tasks print(f\"平均响应时间: {avg_time:.2f}秒\\n\") print(\"=\"*40) print(\"成功创作内容汇总\") print(\"=\"*40) for r in records: if r[\"success\"]: print(f\"\\n【任务{r[\'task_id\']}: {r[\'task_type\']}】\") print(f\"任务要求: {r[\'task\']}\") print(f\"响应时间: {r[\'response_time\']}秒\") print(\"创作内容:\\n\" + r[\"ai_creation\"] + \"\\n\") print(\"-\"*50) if fail_tasks > 0: print(\"\\n\" + \"=\"*40) print(\"失败任务汇总\") print(\"=\"*40) for r in records: if not r[\"success\"]: print(f\"\\n【任务{r[\'task_id\']}: {r[\'task_type\']}】\") print(f\"任务要求: {r[\'task\']}\") print(f\"失败原因: {r[\'error\']}\\n\") print(\"-\"*50)if __name__ == \"__main__\": test_literature_creativity()
看来结果还是不错的。
五、测试总结
本人测试了“paddlepaddle/ERNIE-4.5-0.3B-Paddle”模型,该模型对于文学领域的处理是十分不错的,由于模型参数量较小,没有对逻辑分析领域进行测评,至于3.11和3.8谁大的问题,对于0.3B的模型来说是可以接受的,毕竟当初GPT那么大的参数还会出错。本人测评水平有限,如有更好的测评思路,还请各位大佬指正。