Qwen3.5-9B实战教程:history.json结构解析+对话数据导出与分析方法

张开发
2026/4/18 19:49:21 15 分钟阅读

分享文章

Qwen3.5-9B实战教程:history.json结构解析+对话数据导出与分析方法
Qwen3.5-9B实战教程history.json结构解析对话数据导出与分析方法1. 引言Qwen3.5-9B是一款拥有90亿参数的开源大语言模型具备强大的逻辑推理、代码生成和多轮对话能力。作为多模态模型它支持图文输入如Qwen3.5-9B-VL变体并能处理长达128K tokens的上下文。本教程将重点解析模型运行过程中生成的对话历史文件history.json的结构并详细介绍如何导出和分析这些对话数据。2. history.json文件结构解析2.1 文件位置与作用history.json文件位于Qwen3.5-9B项目目录下默认路径为/root/qwen3.5-9b/history.json该文件自动记录所有通过Web界面进行的对话历史包括用户输入和模型回复。了解其结构对于数据分析、模型优化和对话管理至关重要。2.2 JSON数据结构详解history.json采用标准的JSON格式存储数据主要包含以下字段{ version: 1.0, history: [ { timestamp: 2026-03-25T14:30:22, user_input: 你好介绍一下你自己, model_response: 我是Qwen3.5-9B一个90亿参数的多模态大语言模型..., parameters: { max_tokens: 512, temperature: 0.7, top_p: 0.9, top_k: 50 }, image_uploaded: false }, { timestamp: 2026-03-25T14:32:15, user_input: 这张图片里有什么, model_response: 图片中显示一只橘色猫咪坐在窗台上..., parameters: { max_tokens: 1024, temperature: 0.5, top_p: 0.8, top_k: 40 }, image_uploaded: true, image_info: { filename: cat.jpg, size: 1024x768, format: JPEG } } ] }2.3 关键字段说明字段类型说明versionstring历史记录格式版本historyarray对话记录数组timestampstring对话时间戳(ISO 8601格式)user_inputstring用户输入的文本model_responsestring模型生成的回复parametersobject生成参数配置image_uploadedboolean是否包含图片上传image_infoobject图片元数据(仅当image_uploaded为true时存在)3. 对话数据导出方法3.1 直接读取JSON文件最简单的导出方法是直接读取history.json文件import json with open(/root/qwen3.5-9b/history.json, r, encodingutf-8) as f: data json.load(f) conversations data[history]3.2 使用Python脚本定期备份可以创建定时任务自动备份对话历史import json import shutil from datetime import datetime def backup_history(): timestamp datetime.now().strftime(%Y%m%d_%H%M%S) backup_file f/backup/history_{timestamp}.json shutil.copy2(/root/qwen3.5-9b/history.json, backup_file) print(fBackup completed: {backup_file})3.3 导出为CSV格式将对话历史转换为CSV便于数据分析import csv import json def export_to_csv(input_file, output_file): with open(input_file, r, encodingutf-8) as f: data json.load(f) with open(output_file, w, newline, encodingutf-8) as f: writer csv.writer(f) writer.writerow([Timestamp, User Input, Model Response, Temperature, Max Tokens]) for conv in data[history]: writer.writerow([ conv[timestamp], conv[user_input], conv[model_response], conv[parameters][temperature], conv[parameters][max_tokens] ])4. 对话数据分析方法4.1 基础统计分析使用Pandas进行基础数据分析import pandas as pd # 加载数据 df pd.read_json(/root/qwen3.5-9b/history.json) history pd.json_normalize(df[history]) # 基础统计 print(f总对话轮次: {len(history)}) print(f平均回复长度: {history[model_response].str.len().mean():.1f}字符) print(f图片对话占比: {history[image_uploaded].mean()*100:.1f}%)4.2 参数影响分析分析生成参数对回复质量的影响import matplotlib.pyplot as plt # 温度与回复长度的关系 plt.scatter(history[parameters.temperature], history[model_response].str.len()) plt.xlabel(Temperature) plt.ylabel(Response Length) plt.title(Temperature vs Response Length) plt.show()4.3 对话质量评估定义简单的质量评估指标def assess_quality(row): length len(row[model_response]) temp row[parameters][temperature] # 简单质量评分规则 score 0 if length 50: score 1 if 0.5 temp 0.9: score 1 if not row[image_uploaded] and length 100: score 1 return score history[quality_score] history.apply(assess_quality, axis1)5. 高级分析技巧5.1 主题聚类分析使用NLP技术对对话主题进行聚类from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.cluster import KMeans # 合并用户输入和模型回复 texts history[user_input] history[model_response] # 向量化 vectorizer TfidfVectorizer(max_features1000) X vectorizer.fit_transform(texts) # 聚类 kmeans KMeans(n_clusters5, random_state42) history[cluster] kmeans.fit_predict(X)5.2 情感分析对模型回复进行情感分析from transformers import pipeline sentiment_analyzer pipeline(sentiment-analysis) def analyze_sentiment(text): result sentiment_analyzer(text[:512])[0] # 限制长度 return result[label], result[score] history[[sentiment, sentiment_score]] history[model_response].apply( lambda x: pd.Series(analyze_sentiment(x)) )5.3 对话流程可视化使用NetworkX可视化对话流程import networkx as nx G nx.DiGraph() for i in range(len(history)-1): G.add_edge(history.iloc[i][user_input][:20]..., history.iloc[i1][user_input][:20]...) plt.figure(figsize(12, 8)) nx.draw(G, with_labelsTrue, node_size2000, node_colorskyblue) plt.title(Conversation Flow) plt.show()6. 总结通过本教程我们深入解析了Qwen3.5-9B的history.json文件结构并掌握了多种对话数据导出和分析方法。这些技术可以帮助您更好地理解用户与模型的交互模式评估不同参数设置下的模型表现发现对话中的常见主题和模式持续优化模型的使用体验建议定期分析对话历史结合业务需求调整模型参数和交互设计以获得最佳的使用效果。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章