Merge branch 'refs/heads/release/v0.2.3' into fix/release_memory_bug

# Conflicts:
#	api/app/core/memory/agent/langgraph_graph/write_graph.py
This commit is contained in:
lixinyue
2026-02-04 13:43:15 +08:00
38 changed files with 563 additions and 109 deletions

View File

@@ -148,8 +148,10 @@ class LangChainAgent:
messages.append(HumanMessage(content=user_content))
return messages
# TODO: 移到memory module
async def term_memory_save(self,long_term_messages,actual_config_id,end_user_id,type):
db = next(get_db())
#TODO: 魔法数字
scope=6
try:
@@ -159,6 +161,12 @@ class LangChainAgent:
from app.core.memory.agent.utils.redis_tool import write_store
result = write_store.get_session_by_userid(end_user_id)
# Handle case where no session exists in Redis (returns False)
if not result or result is False:
logger.debug(f"No existing session in Redis for user {end_user_id}, skipping short-term memory update")
return
if type=="chunk" or type=="aggregate":
data = await format_parsing(result, "dict")
chunk_data = data[:scope]
@@ -166,7 +174,14 @@ class LangChainAgent:
repo.upsert(end_user_id, chunk_data)
logger.info(f'写入短长期:')
else:
# TODO: This branch handles type="time" strategy, currently unused.
# Will be activated when time-based long-term storage is implemented.
# TODO: 魔法数字 - extract 5 to a constant
long_time_data = write_store.find_user_recent_sessions(end_user_id, 5)
# Handle case where no session exists in Redis (returns False or empty)
if not long_time_data or long_time_data is False:
logger.debug(f"No recent sessions in Redis for user {end_user_id}")
return
long_messages = await messages_parse(long_time_data)
repo.upsert(end_user_id, long_messages)
logger.info(f'写入短长期:')
@@ -307,9 +322,12 @@ class LangChainAgent:
elapsed_time = time.time() - start_time
if memory_flag:
long_term_messages=await agent_chat_messages(message_chat,content)
# AI 回复写入(用户消息和 AI 回复配对,一次性写入完整对话)
# TODO: DUPLICATE WRITE - Remove this immediate write once batched write (term_memory_save) is verified stable.
# This writes to Neo4j immediately via Celery task, but term_memory_save also writes to Neo4j
# when the window buffer reaches scope (6 messages). This causes duplicate entities in the graph.
# Recommended: Keep only term_memory_save for batched efficiency, or only self.write for real-time.
await self.write(storage_type, actual_end_user_id, message_chat, content, user_rag_memory_id, actual_end_user_id, actual_config_id)
'''长期'''
# Batched long-term memory storage (Redis buffer + Neo4j when window full)
await self.term_memory_save(long_term_messages,actual_config_id,end_user_id,"chunk")
response = {
"content": content,
@@ -441,9 +459,13 @@ class LangChainAgent:
yield total_tokens
break
if memory_flag:
# AI 回复写入(用户消息和 AI 回复配对,一次性写入完整对话)
# TODO: DUPLICATE WRITE - Remove this immediate write once batched write (term_memory_save) is verified stable.
# This writes to Neo4j immediately via Celery task, but term_memory_save also writes to Neo4j
# when the window buffer reaches scope (6 messages). This causes duplicate entities in the graph.
# Recommended: Keep only term_memory_save for batched efficiency, or only self.write for real-time.
long_term_messages = await agent_chat_messages(message_chat, full_content)
await self.write(storage_type, end_user_id, message_chat, full_content, user_rag_memory_id, end_user_id, actual_config_id)
# Batched long-term memory storage (Redis buffer + Neo4j when window full)
await self.term_memory_save(long_term_messages, actual_config_id, end_user_id, "chunk")
except Exception as e:

View File

@@ -43,6 +43,7 @@ async def write_messages(end_user_id,langchain_messages,memory_config):
for node_name, node_data in update_event.items():
if 'save_neo4j' == node_name:
massages = node_data
# TODO删除
massagesstatus = massages.get('write_result')['status']
contents = massages.get('write_result')
print(contents)
@@ -60,6 +61,7 @@ async def window_dialogue(end_user_id,langchain_messages,memory_config,scope):
scope窗口大小
'''
scope=scope
redis_messages = []
is_end_user_id = count_store.get_sessions_count(end_user_id)
if is_end_user_id is not False:
is_end_user_id = count_store.get_sessions_count(end_user_id)[0]
@@ -91,6 +93,9 @@ async def memory_long_term_storage(end_user_id,memory_config,time):
memory_config: 内存配置对象
'''
long_time_data = write_store.find_user_recent_sessions(end_user_id, time)
# Handle case where no session exists in Redis (returns False or empty)
if not long_time_data or long_time_data is False:
return
format_messages = await chat_data_format(long_time_data)
if format_messages!=[]:
await write_messages(end_user_id, format_messages, memory_config)
@@ -108,8 +113,9 @@ async def aggregate_judgment(end_user_id: str, ori_messages: list, memory_config
try:
# 1. 获取历史会话数据(使用新方法)
result = write_store.get_all_sessions_by_end_user_id(end_user_id)
history = await format_parsing(result)
if not result:
# Handle case where no session exists in Redis (returns False or empty)
if not result or result is False:
history = []
else:
history = await format_parsing(result)

View File

@@ -44,7 +44,7 @@ class CodeNodeConfig(BaseNodeConfig):
description="code content"
)
language: Literal['python3', 'nodejs'] = Field(
language: Literal['python3', 'javascript'] = Field(
...,
description="language"
)

View File

@@ -110,7 +110,7 @@ class CodeNode(BaseNode):
code=code,
inputs_variable=input_variable_dict,
)
elif self.typed_config.language == 'nodejs':
elif self.typed_config.language == 'javascript':
final_script = NODEJS_SCRIPT_TEMPLATE.substitute(
code=code,
inputs_variable=input_variable_dict,

View File

@@ -4,16 +4,19 @@
从文件系统加载预定义的工作流模板
"""
import os
from pathlib import Path
from typing import Optional
import yaml
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
class TemplateLoader:
"""工作流模板加载器"""
def __init__(self, templates_dir: str = "app/templates/workflows"):
def __init__(self, templates_dir: str = TEMPLATE_DIR):
"""初始化模板加载器
Args:
@@ -22,7 +25,7 @@ class TemplateLoader:
self.templates_dir = Path(templates_dir)
if not self.templates_dir.exists():
raise ValueError(f"模板目录不存在: {templates_dir}")
def list_templates(self) -> list[dict]:
"""列出所有可用的模板
@@ -30,22 +33,22 @@ class TemplateLoader:
模板列表,每个模板包含 id, name, description 等信息
"""
templates = []
# 遍历模板目录
for template_dir in self.templates_dir.iterdir():
if not template_dir.is_dir():
continue
# 检查是否有 template.yml 文件
template_file = template_dir / "template.yml"
if not template_file.exists():
continue
try:
# 读取模板配置
with open(template_file, 'r', encoding='utf-8') as f:
template_data = yaml.safe_load(f)
# 提取模板信息
templates.append({
"id": template_dir.name,
@@ -59,9 +62,9 @@ class TemplateLoader:
except Exception as e:
print(f"加载模板 {template_dir.name} 失败: {e}")
continue
return templates
def load_template(self, template_id: str) -> Optional[dict]:
"""加载指定的模板
@@ -73,14 +76,14 @@ class TemplateLoader:
"""
template_dir = self.templates_dir / template_id
template_file = template_dir / "template.yml"
if not template_file.exists():
return None
try:
with open(template_file, 'r', encoding='utf-8') as f:
template_data = yaml.safe_load(f)
# 返回工作流配置部分
return {
"name": template_data.get("name", template_id),
@@ -94,7 +97,7 @@ class TemplateLoader:
except Exception as e:
print(f"加载模板 {template_id} 失败: {e}")
return None
def get_template_readme(self, template_id: str) -> Optional[str]:
"""获取模板的 README 文档
@@ -106,10 +109,10 @@ class TemplateLoader:
"""
template_dir = self.templates_dir / template_id
readme_file = template_dir / "README.md"
if not readme_file.exists():
return None
try:
with open(readme_file, 'r', encoding='utf-8') as f:
return f.read()

View File

@@ -0,0 +1,219 @@
# 智能客服工作流模板
id: customer_service_v1
name: 智能客服工作流
description: 智能客服场景,包含意图识别、知识库查询和回复生成
category: customer_service
version: "1.0.0"
author: RedBear Memory Team
tags:
- 客服
- 意图识别
- 知识库
- 多步骤
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 200
- id: intent_recognition
type: llm
name: 意图识别
config:
prompt: |
分析用户的问题,识别意图类型。
用户问题:{{ var.user_message }}
请从以下类型中选择一个:
- product_inquiry: 产品咨询
- technical_support: 技术支持
- complaint: 投诉建议
- other: 其他
只返回类型名称,不要其他内容。
model: gpt-3.5-turbo
temperature: 0.3
max_tokens: 50
position:
x: 300
y: 200
- id: intent_router
type: condition
name: 意图路由
position:
x: 500
y: 200
- id: product_handler
type: llm
name: 产品咨询处理
config:
prompt: |
用户咨询产品相关问题。
问题:{{ var.user_message }}
意图:{{ node.intent_recognition.output }}
请提供专业、友好的产品咨询回复。
model: gpt-3.5-turbo
temperature: 0.7
max_tokens: 500
position:
x: 700
y: 100
- id: support_handler
type: llm
name: 技术支持处理
config:
prompt: |
用户需要技术支持。
问题:{{ var.user_message }}
意图:{{ node.intent_recognition.output }}
请提供详细的技术支持方案。
model: gpt-3.5-turbo
temperature: 0.5
max_tokens: 800
position:
x: 700
y: 200
- id: complaint_handler
type: llm
name: 投诉处理
config:
prompt: |
用户提出投诉或建议。
问题:{{ var.user_message }}
意图:{{ node.intent_recognition.output }}
请以同理心回应,并提供解决方案。
model: gpt-3.5-turbo
temperature: 0.8
max_tokens: 600
position:
x: 700
y: 300
- id: general_handler
type: llm
name: 通用处理
config:
prompt: |
用户的问题类型:其他
问题:{{ var.user_message }}
请提供友好的回复。
model: gpt-3.5-turbo
temperature: 0.7
max_tokens: 400
position:
x: 700
y: 400
- id: end
type: end
name: 结束
position:
x: 900
y: 200
edges:
- source: start
target: intent_recognition
label: 开始分析
- source: intent_recognition
target: intent_router
label: 识别完成
- source: intent_router
target: product_handler
condition: "'product_inquiry' in node['intent_recognition']['output']"
label: 产品咨询
- source: intent_router
target: support_handler
condition: "'technical_support' in node['intent_recognition']['output']"
label: 技术支持
- source: intent_router
target: complaint_handler
condition: "'complaint' in node['intent_recognition']['output']"
label: 投诉建议
- source: intent_router
target: general_handler
condition: "True" # 默认路径
label: 其他
- source: product_handler
target: end
label: 完成
- source: support_handler
target: end
label: 完成
- source: complaint_handler
target: end
label: 完成
- source: general_handler
target: end
label: 完成
# 变量定义
variables:
- name: user_message
type: string
required: true
description: 用户的消息
default: ""
- name: user_name
type: string
required: false
description: 用户姓名(可选)
default: "客户"
# 执行配置
execution_config:
max_execution_time: 120
max_iterations: 10
# 触发器
triggers: []
# 使用示例
examples:
- name: 产品咨询
description: 用户咨询产品功能
input:
user_message: "你们的产品支持多语言吗?"
user_name: "张三"
expected_output: "产品功能介绍"
- name: 技术支持
description: 用户遇到技术问题
input:
user_message: "我无法登录系统,一直显示密码错误"
user_name: "李四"
expected_output: "技术支持方案"
- name: 投诉处理
description: 用户提出投诉
input:
user_message: "你们的服务态度太差了,我要投诉"
user_name: "王五"
expected_output: "同理心回应和解决方案"

View File

@@ -0,0 +1,131 @@
# 数据处理工作流模板
id: data_processing_v1
name: 数据处理工作流
description: 数据提取、转换和分析的完整流程
category: data_processing
version: "1.0.0"
author: RedBear Memory Team
tags:
- 数据处理
- ETL
- 分析
- Transform
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 200
- id: extract_data
type: transform
name: 数据提取
config:
expression: |
{
"raw_text": var['input_text'],
"length": len(var['input_text']),
"timestamp": sys['execution_id']
}
position:
x: 300
y: 200
- id: analyze_data
type: llm
name: 数据分析
config:
prompt: |
请分析以下数据:
原始文本:{{ node.extract_data.raw_text }}
文本长度:{{ node.extract_data.length }}
请提供:
1. 主题分类
2. 情感分析
3. 关键信息提取
以 JSON 格式返回结果。
model: gpt-3.5-turbo
temperature: 0.3
max_tokens: 500
position:
x: 500
y: 200
- id: transform_result
type: transform
name: 结果转换
config:
expression: |
{
"original_length": node['extract_data']['length'],
"analysis": node['analyze_data']['output'],
"processed_at": sys['execution_id'],
"status": "completed"
}
position:
x: 700
y: 200
- id: end
type: end
name: 结束
position:
x: 900
y: 200
edges:
- source: start
target: extract_data
label: 开始提取
- source: extract_data
target: analyze_data
label: 开始分析
- source: analyze_data
target: transform_result
label: 转换结果
- source: transform_result
target: end
label: 完成
# 变量定义
variables:
- name: input_text
type: string
required: true
description: 待处理的文本数据
default: ""
# 执行配置
execution_config:
max_execution_time: 180
max_iterations: 5
# 触发器
triggers: []
# 使用示例
examples:
- name: 文本分析
description: 分析一段文本
input:
input_text: "今天天气真好,心情也很愉快。我们公司推出了新产品,市场反响热烈。"
expected_output:
original_length: 35
analysis: "主题:天气和产品,情感:积极"
status: "completed"
- name: 长文本处理
description: 处理较长的文本
input:
input_text: "这是一段很长的文本..."
expected_output:
status: "completed"

View File

@@ -0,0 +1,99 @@
# 多步骤问答工作流
# 演示节点输出参数的使用
id: multi_step_qa_v1
name: 多步骤问答工作流
description: 先分析问题,再生成答案,展示节点间的数据传递
category: advanced
version: "1.0.0"
author: RedBear Memory Team
tags:
- 问答
- 多步骤
- LLM
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 100
- id: analyze_question
type: llm
name: 分析问题
description: 分析用户问题的类型和意图
config:
model_id: gpt-3.5-turbo
temperature: 0.3
max_tokens: 500
messages:
- role: system
content: |
你是一个问题分析专家。请分析用户的问题,提取以下信息:
1. 问题类型(事实性、观点性、操作性等)
2. 问题领域(科技、历史、文化等)
3. 关键词
- role: user
content: "{{ sys.message }}"
position:
x: 300
y: 100
- id: generate_answer
type: llm
name: 生成答案
description: 根据问题分析结果生成详细答案
config:
model_id: gpt-3.5-turbo
temperature: 0.7
max_tokens: 1000
messages:
- role: system
content: |
你是一个专业的AI助手。根据问题分析结果生成准确、详细的答案。
问题分析结果:
{{ analyze_question.output }}
- role: user
content: "{{ sys.message }}"
position:
x: 500
y: 100
- id: end
type: end
name: 结束
config:
output: "{{ generate_answer.output }}"
position:
x: 700
y: 100
edges:
- source: start
target: analyze_question
label: 开始分析
- source: analyze_question
target: generate_answer
label: 生成答案
- source: generate_answer
target: end
label: 完成
# 变量定义
variables:
- name: user_question
type: string
required: true
description: 用户的问题
default: ""
# 执行配置
execution_config:
max_execution_time: 120
max_iterations: 1

View File

@@ -0,0 +1,93 @@
# 简单问答工作流模板
id: simple_qa_v1
name: 简单问答工作流
description: 最基础的问答工作流,适合快速开始
category: basic
version: "1.0.0"
author: RedBear Memory Team
tags:
- 问答
- 基础
- LLM
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 100
- id: llm_qa
type: llm
name: LLM 问答
config:
# 使用 LangChain 标准的消息格式
messages:
- role: system
content: |
你是一个专业、友好且乐于助人的 AI 助手。
你的职责:
- 准确理解用户的问题并提供有价值的回答
- 保持回答的专业性和准确性
- 如果不确定答案,诚实地告知用户
- 使用清晰、易懂的语言进行交流
回答风格:
- 简洁明了,直击要点
- 必要时提供详细解释和示例
- 使用友好、礼貌的语气
- 适当使用格式化(如列表、段落)提高可读性
model_id: null
temperature: 0.7
max_tokens: 1000
position:
x: 500
y: 100
- id: end
type: end
name: 结束
config:
output: "{{llm_qa.output}}"
position:
x: 900
y: 100
edges:
- source: start
target: llm_qa
label: 开始处理
- source: llm_qa
target: end
label: 完成
# 变量定义
variables: []
# 执行配置
execution_config:
max_execution_time: 60
max_iterations: 1
# 触发器(可选)
triggers: []
# 使用示例
examples:
- name: 基础问答
description: 询问一个简单的问题
input:
user_question: "什么是人工智能?"
expected_output: "关于人工智能的解释"
- name: 技术咨询
description: 询问技术问题
input:
user_question: "如何学习 Python 编程?"
expected_output: "Python 学习建议"