Nanbeige 4.1-3B实战教程接入Notion API实现像素对话自动归档与检索1. 项目概述与目标Nanbeige 4.1-3B是一款具有独特像素游戏风格的AI对话模型本教程将指导您如何将其与Notion API集成实现对话内容的自动归档与智能检索。通过本教程您将能够在保留像素游戏UI特色的基础上增加对话持久化功能自动将对话内容分类存储到Notion数据库中实现基于自然语言的对话历史检索打造完整的对话-存档-检索工作流2. 环境准备与配置2.1 基础环境要求Python 3.8或更高版本已部署的Nanbeige 4.1-3B模型服务Notion账户及创建好的数据库稳定的网络连接2.2 安装必要依赖pip install notion-client streamlit requests2.3 Notion API配置步骤登录Notion并创建一个新页面作为数据库访问Notion开发者页面创建新集成记录下生成的Internal Integration Token在数据库页面点击Share邀请您的集成加入3. Notion数据库设计3.1 数据库结构设计我们建议创建包含以下属性的数据库属性名类型描述对话内容Text存储完整的对话文本角色Select区分用户/AI时间戳Date对话发生时间主题标签Multi-select对话内容分类标签思考过程Text存储 标签内容3.2 创建数据库的Python代码from notion_client import Client notion Client(authyour_integration_token) def create_database(parent_page_id): database notion.databases.create( parent{page_id: parent_page_id}, titleNanbeige对话存档, properties{ 对话内容: {title: {}}, 角色: { select: { options: [ {name: 玩家, color: blue}, {name: Nanbeige, color: green} ] } }, 时间戳: {date: {}}, 主题标签: { multi_select: { options: [ {name: 游戏, color: red}, {name: 技术, color: blue}, # 添加更多分类... ] } }, 思考过程: {rich_text: {}} } ) return database.id4. 集成实现步骤4.1 对话记录功能扩展在原有像素UI项目中添加对话记录模块import datetime def save_to_notion(database_id, role, content, thinkNone): properties { 角色: {select: {name: role}}, 对话内容: {title: [{text: {content: content}}]}, 时间戳: {date: {start: datetime.datetime.now().isoformat()}}, } if think: properties[思考过程] {rich_text: [{text: {content: think}}]} notion.pages.create( parent{database_id: database_id}, propertiesproperties )4.2 流式对话中的实时存档修改原有对话处理逻辑在生成响应时同步存档def generate_response(user_input): # 原有像素UI生成逻辑... response model.generate(user_input) # 保存用户输入 save_to_notion(DATABASE_ID, 玩家, user_input) # 保存AI响应 think_content extract_think_tags(response) # 提取think标签内容 clean_response remove_think_tags(response) # 清理响应中的标签 save_to_notion(DATABASE_ID, Nanbeige, clean_response, think_content) return clean_response5. 对话检索功能实现5.1 基础检索功能添加检索界面到像素UI中def search_dialogs(query): results notion.databases.query( DATABASE_ID, filter{ property: 对话内容, text: {contains: query} } ) return results[results] # 在Streamlit UI中添加搜索组件 search_query st.text_input( 搜索对话历史, keysearch) if search_query: results search_dialogs(search_query) for item in results: role item[properties][角色][select][name] content item[properties][对话内容][title][0][text][content] # 以像素风格显示搜索结果...5.2 高级检索功能实现基于自然语言的语义检索from sentence_transformers import SentenceTransformer encoder SentenceTransformer(paraphrase-multilingual-MiniLM-L12-v2) def semantic_search(query, top_k5): # 获取所有对话记录 all_dialogs notion.databases.query(DATABASE_ID)[results] # 编码查询和对话内容 query_embedding encoder.encode(query) dialog_embeddings [ encoder.encode(d[properties][对话内容][title][0][text][content]) for d in all_dialogs ] # 计算相似度并排序 similarities [ cosine_similarity(query_embedding, emb) for emb in dialog_embeddings ] sorted_indices np.argsort(similarities)[-top_k:][::-1] return [all_dialogs[i] for i in sorted_indices]6. 像素UI集成与优化6.1 保持像素风格的一致性在搜索结果展示中延续像素游戏风格def display_search_result(result): role result[properties][角色][select][name] content result[properties][对话内容][title][0][text][content] time result[properties][时间戳][date][start] # 像素风格渲染 st.markdown(f div styleborder: 4px solid #2C2C2C; padding: 12px; margin: 8px 0; background: {#4D96FF if role 玩家 else #6BCB77}; color: white; font-family: Courier New, monospace; span stylecolor: #FFD700;[{role}]/span span stylefont-size: 0.8em; color: #FDF6E3;({time})/spanbr {content} /div , unsafe_allow_htmlTrue)6.2 性能优化建议使用缓存减少Notion API调用st.cache_data(ttl3600) def cached_search(query): return search_dialogs(query)批量处理对话存档def batch_save(dialogs): with ThreadPoolExecutor() as executor: executor.map(lambda d: save_to_notion(**d), dialogs)7. 总结与进阶建议通过本教程您已经成功将Nanbeige 4.1-3B的像素对话系统与Notion集成实现了对话的自动归档和智能检索。以下是进一步的优化方向自动标签分类添加小型分类模型自动为对话打标签对话关系图谱构建对话之间的关联关系实现更智能的检索多平台同步扩展支持其他笔记工具如Obsidian、Logseq等本地存储选项为隐私敏感场景添加SQLite等本地存储方案这个集成方案不仅保留了Nanbeige独特的像素游戏体验还为其增添了实用的知识管理能力使AI对话成为真正可沉淀的知识资产。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。
Nanbeige 4.1-3B实战教程:接入Notion API实现像素对话自动归档与检索
Nanbeige 4.1-3B实战教程接入Notion API实现像素对话自动归档与检索1. 项目概述与目标Nanbeige 4.1-3B是一款具有独特像素游戏风格的AI对话模型本教程将指导您如何将其与Notion API集成实现对话内容的自动归档与智能检索。通过本教程您将能够在保留像素游戏UI特色的基础上增加对话持久化功能自动将对话内容分类存储到Notion数据库中实现基于自然语言的对话历史检索打造完整的对话-存档-检索工作流2. 环境准备与配置2.1 基础环境要求Python 3.8或更高版本已部署的Nanbeige 4.1-3B模型服务Notion账户及创建好的数据库稳定的网络连接2.2 安装必要依赖pip install notion-client streamlit requests2.3 Notion API配置步骤登录Notion并创建一个新页面作为数据库访问Notion开发者页面创建新集成记录下生成的Internal Integration Token在数据库页面点击Share邀请您的集成加入3. Notion数据库设计3.1 数据库结构设计我们建议创建包含以下属性的数据库属性名类型描述对话内容Text存储完整的对话文本角色Select区分用户/AI时间戳Date对话发生时间主题标签Multi-select对话内容分类标签思考过程Text存储 标签内容3.2 创建数据库的Python代码from notion_client import Client notion Client(authyour_integration_token) def create_database(parent_page_id): database notion.databases.create( parent{page_id: parent_page_id}, titleNanbeige对话存档, properties{ 对话内容: {title: {}}, 角色: { select: { options: [ {name: 玩家, color: blue}, {name: Nanbeige, color: green} ] } }, 时间戳: {date: {}}, 主题标签: { multi_select: { options: [ {name: 游戏, color: red}, {name: 技术, color: blue}, # 添加更多分类... ] } }, 思考过程: {rich_text: {}} } ) return database.id4. 集成实现步骤4.1 对话记录功能扩展在原有像素UI项目中添加对话记录模块import datetime def save_to_notion(database_id, role, content, thinkNone): properties { 角色: {select: {name: role}}, 对话内容: {title: [{text: {content: content}}]}, 时间戳: {date: {start: datetime.datetime.now().isoformat()}}, } if think: properties[思考过程] {rich_text: [{text: {content: think}}]} notion.pages.create( parent{database_id: database_id}, propertiesproperties )4.2 流式对话中的实时存档修改原有对话处理逻辑在生成响应时同步存档def generate_response(user_input): # 原有像素UI生成逻辑... response model.generate(user_input) # 保存用户输入 save_to_notion(DATABASE_ID, 玩家, user_input) # 保存AI响应 think_content extract_think_tags(response) # 提取think标签内容 clean_response remove_think_tags(response) # 清理响应中的标签 save_to_notion(DATABASE_ID, Nanbeige, clean_response, think_content) return clean_response5. 对话检索功能实现5.1 基础检索功能添加检索界面到像素UI中def search_dialogs(query): results notion.databases.query( DATABASE_ID, filter{ property: 对话内容, text: {contains: query} } ) return results[results] # 在Streamlit UI中添加搜索组件 search_query st.text_input( 搜索对话历史, keysearch) if search_query: results search_dialogs(search_query) for item in results: role item[properties][角色][select][name] content item[properties][对话内容][title][0][text][content] # 以像素风格显示搜索结果...5.2 高级检索功能实现基于自然语言的语义检索from sentence_transformers import SentenceTransformer encoder SentenceTransformer(paraphrase-multilingual-MiniLM-L12-v2) def semantic_search(query, top_k5): # 获取所有对话记录 all_dialogs notion.databases.query(DATABASE_ID)[results] # 编码查询和对话内容 query_embedding encoder.encode(query) dialog_embeddings [ encoder.encode(d[properties][对话内容][title][0][text][content]) for d in all_dialogs ] # 计算相似度并排序 similarities [ cosine_similarity(query_embedding, emb) for emb in dialog_embeddings ] sorted_indices np.argsort(similarities)[-top_k:][::-1] return [all_dialogs[i] for i in sorted_indices]6. 像素UI集成与优化6.1 保持像素风格的一致性在搜索结果展示中延续像素游戏风格def display_search_result(result): role result[properties][角色][select][name] content result[properties][对话内容][title][0][text][content] time result[properties][时间戳][date][start] # 像素风格渲染 st.markdown(f div styleborder: 4px solid #2C2C2C; padding: 12px; margin: 8px 0; background: {#4D96FF if role 玩家 else #6BCB77}; color: white; font-family: Courier New, monospace; span stylecolor: #FFD700;[{role}]/span span stylefont-size: 0.8em; color: #FDF6E3;({time})/spanbr {content} /div , unsafe_allow_htmlTrue)6.2 性能优化建议使用缓存减少Notion API调用st.cache_data(ttl3600) def cached_search(query): return search_dialogs(query)批量处理对话存档def batch_save(dialogs): with ThreadPoolExecutor() as executor: executor.map(lambda d: save_to_notion(**d), dialogs)7. 总结与进阶建议通过本教程您已经成功将Nanbeige 4.1-3B的像素对话系统与Notion集成实现了对话的自动归档和智能检索。以下是进一步的优化方向自动标签分类添加小型分类模型自动为对话打标签对话关系图谱构建对话之间的关联关系实现更智能的检索多平台同步扩展支持其他笔记工具如Obsidian、Logseq等本地存储选项为隐私敏感场景添加SQLite等本地存储方案这个集成方案不仅保留了Nanbeige独特的像素游戏体验还为其增添了实用的知识管理能力使AI对话成为真正可沉淀的知识资产。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。