fix(prompt-optimizer): 修复数据模型和业务逻辑问题
- 修复PromptOptimizerSessionHistory模型中session_id外键关系错误 - 统一会话ID的使用逻辑,区分内部ID和外部session_id - 修复服务层create_update_model_config方法参数不匹配问题 - 优化会话历史查询逻辑,确保正确的数据关联 - 修复消息创建时的会话验证和ID映射问题 - 改进Repository层的类型注解准确性
This commit is contained in:
@@ -163,7 +163,7 @@ def set_system_prompt(
|
||||
|
||||
model_config = PromptOptimizerService(db).create_update_model_config(
|
||||
current_user.tenant_id,
|
||||
data.id, data.model_id,
|
||||
data.id,
|
||||
data.system_prompt
|
||||
)
|
||||
return success(data=model_config.id)
|
||||
|
||||
@@ -86,12 +86,10 @@ class PromptOptimizerSession(Base):
|
||||
|
||||
Columns:
|
||||
id (UUID):
|
||||
Primary key. Internal unique identifier for the session record.
|
||||
Public-facing session identifier used to group conversation history.
|
||||
tenant_id (UUID):
|
||||
Foreign key referencing `tenants.id`.
|
||||
Identifies the tenant under which the session is created.
|
||||
session_id (UUID):
|
||||
Public-facing session identifier used to group conversation history.
|
||||
user_id (UUID):
|
||||
Foreign key referencing `users.id`.
|
||||
Identifies the user who initiated the session.
|
||||
@@ -105,10 +103,9 @@ class PromptOptimizerSession(Base):
|
||||
"""
|
||||
__tablename__ = "prompt_opt_session_list"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True, comment="Session ID")
|
||||
tenant_id = Column(UUID(as_uuid=True), ForeignKey("tenants.id"), nullable=False, comment="Tenant ID")
|
||||
# app_id = Column(UUID(as_uuid=True), ForeignKey("apps.id"), nullable=False, comment="Application ID")
|
||||
session_id = Column(UUID(as_uuid=True), nullable=False, comment="Session ID")
|
||||
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False, comment="User ID")
|
||||
|
||||
created_at = Column(DateTime, default=datetime.datetime.now, comment="Creation Time", index=True)
|
||||
|
||||
@@ -141,7 +141,6 @@ class PromptOptimizerSessionRepository:
|
||||
session = PromptOptimizerSession(
|
||||
tenant_id=tenant_id,
|
||||
user_id=user_id,
|
||||
session_id=uuid.uuid4(),
|
||||
)
|
||||
self.db.add(session)
|
||||
self.db.commit()
|
||||
@@ -172,8 +171,17 @@ class PromptOptimizerSessionRepository:
|
||||
f"user_id={user_id}, session_id={session_id}")
|
||||
|
||||
try:
|
||||
# First get the internal session ID from the session list table
|
||||
session = self.db.query(PromptOptimizerSession).filter(
|
||||
PromptOptimizerSession.id == session_id,
|
||||
PromptOptimizerSession.user_id == user_id
|
||||
).first()
|
||||
|
||||
if not session:
|
||||
return []
|
||||
|
||||
history = self.db.query(PromptOptimizerSessionHistory).filter(
|
||||
PromptOptimizerSessionHistory.session_id == session_id,
|
||||
PromptOptimizerSessionHistory.session_id == session.id,
|
||||
PromptOptimizerSessionHistory.user_id == user_id
|
||||
).order_by(PromptOptimizerSessionHistory.created_at.asc()).all()
|
||||
return history
|
||||
@@ -195,9 +203,20 @@ class PromptOptimizerSessionRepository:
|
||||
This method is a placeholder for future implementation.
|
||||
"""
|
||||
try:
|
||||
# Get the session to ensure it exists and belongs to the user
|
||||
session = self.db.query(PromptOptimizerSession).filter(
|
||||
PromptOptimizerSession.id == session_id,
|
||||
PromptOptimizerSession.user_id == user_id,
|
||||
PromptOptimizerSession.tenant_id == tenant_id
|
||||
).first()
|
||||
|
||||
if not session:
|
||||
db_logger.error(f"Session {session_id} not found for user {user_id}")
|
||||
raise ValueError(f"Session {session_id} not found for user {user_id}")
|
||||
|
||||
message = PromptOptimizerSessionHistory(
|
||||
tenant_id=tenant_id,
|
||||
session_id=session_id,
|
||||
session_id=session.id,
|
||||
user_id=user_id,
|
||||
role=role.value,
|
||||
content=content,
|
||||
|
||||
@@ -73,7 +73,6 @@ class PromptOptimizerService:
|
||||
self,
|
||||
tenant_id: uuid.UUID,
|
||||
config_id: uuid.UUID,
|
||||
model_id: uuid.UUID,
|
||||
system_prompt: str,
|
||||
) -> PromptOptimizerModelConfig:
|
||||
"""
|
||||
@@ -86,7 +85,6 @@ class PromptOptimizerService:
|
||||
Args:
|
||||
tenant_id (uuid.UUID): The unique identifier of the tenant.
|
||||
config_id (uuid.UUID): The unique identifier of the configuration to create or update.
|
||||
model_id (uuid.UUID): The unique identifier of the model associated with this configuration.
|
||||
system_prompt (str): The system prompt content used for prompt optimization.
|
||||
|
||||
Returns:
|
||||
|
||||
Reference in New Issue
Block a user