|
|
|
@ -270,7 +270,7 @@ class BaseAgentRunner(AppRunner):
|
|
|
|
|
|
|
|
|
|
|
|
def create_agent_thought(
|
|
|
|
def create_agent_thought(
|
|
|
|
self, message_id: str, message: str, tool_name: str, tool_input: str, messages_ids: list[str]
|
|
|
|
self, message_id: str, message: str, tool_name: str, tool_input: str, messages_ids: list[str]
|
|
|
|
) -> MessageAgentThought:
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Create agent thought
|
|
|
|
Create agent thought
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
@ -303,16 +303,15 @@ class BaseAgentRunner(AppRunner):
|
|
|
|
|
|
|
|
|
|
|
|
db.session.add(thought)
|
|
|
|
db.session.add(thought)
|
|
|
|
db.session.commit()
|
|
|
|
db.session.commit()
|
|
|
|
db.session.refresh(thought)
|
|
|
|
agent_thought_id = str(thought.id)
|
|
|
|
db.session.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.agent_thought_count += 1
|
|
|
|
self.agent_thought_count += 1
|
|
|
|
|
|
|
|
db.session.close()
|
|
|
|
|
|
|
|
|
|
|
|
return thought
|
|
|
|
return agent_thought_id
|
|
|
|
|
|
|
|
|
|
|
|
def save_agent_thought(
|
|
|
|
def save_agent_thought(
|
|
|
|
self,
|
|
|
|
self,
|
|
|
|
agent_thought: MessageAgentThought,
|
|
|
|
agent_thought_id: str,
|
|
|
|
tool_name: str | None,
|
|
|
|
tool_name: str | None,
|
|
|
|
tool_input: Union[str, dict, None],
|
|
|
|
tool_input: Union[str, dict, None],
|
|
|
|
thought: str | None,
|
|
|
|
thought: str | None,
|
|
|
|
@ -325,12 +324,9 @@ class BaseAgentRunner(AppRunner):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Save agent thought
|
|
|
|
Save agent thought
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
updated_agent_thought = (
|
|
|
|
agent_thought = db.session.query(MessageAgentThought).filter(MessageAgentThought.id == agent_thought_id).first()
|
|
|
|
db.session.query(MessageAgentThought).filter(MessageAgentThought.id == agent_thought.id).first()
|
|
|
|
if not agent_thought:
|
|
|
|
)
|
|
|
|
|
|
|
|
if not updated_agent_thought:
|
|
|
|
|
|
|
|
raise ValueError("agent thought not found")
|
|
|
|
raise ValueError("agent thought not found")
|
|
|
|
agent_thought = updated_agent_thought
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if thought:
|
|
|
|
if thought:
|
|
|
|
agent_thought.thought += thought
|
|
|
|
agent_thought.thought += thought
|
|
|
|
@ -345,7 +341,7 @@ class BaseAgentRunner(AppRunner):
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
|
tool_input = json.dumps(tool_input)
|
|
|
|
tool_input = json.dumps(tool_input)
|
|
|
|
|
|
|
|
|
|
|
|
updated_agent_thought.tool_input = tool_input
|
|
|
|
agent_thought.tool_input = tool_input
|
|
|
|
|
|
|
|
|
|
|
|
if observation:
|
|
|
|
if observation:
|
|
|
|
if isinstance(observation, dict):
|
|
|
|
if isinstance(observation, dict):
|
|
|
|
@ -354,27 +350,27 @@ class BaseAgentRunner(AppRunner):
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
|
observation = json.dumps(observation)
|
|
|
|
observation = json.dumps(observation)
|
|
|
|
|
|
|
|
|
|
|
|
updated_agent_thought.observation = observation
|
|
|
|
agent_thought.observation = observation
|
|
|
|
|
|
|
|
|
|
|
|
if answer:
|
|
|
|
if answer:
|
|
|
|
agent_thought.answer = answer
|
|
|
|
agent_thought.answer = answer
|
|
|
|
|
|
|
|
|
|
|
|
if messages_ids is not None and len(messages_ids) > 0:
|
|
|
|
if messages_ids is not None and len(messages_ids) > 0:
|
|
|
|
updated_agent_thought.message_files = json.dumps(messages_ids)
|
|
|
|
agent_thought.message_files = json.dumps(messages_ids)
|
|
|
|
|
|
|
|
|
|
|
|
if llm_usage:
|
|
|
|
if llm_usage:
|
|
|
|
updated_agent_thought.message_token = llm_usage.prompt_tokens
|
|
|
|
agent_thought.message_token = llm_usage.prompt_tokens
|
|
|
|
updated_agent_thought.message_price_unit = llm_usage.prompt_price_unit
|
|
|
|
agent_thought.message_price_unit = llm_usage.prompt_price_unit
|
|
|
|
updated_agent_thought.message_unit_price = llm_usage.prompt_unit_price
|
|
|
|
agent_thought.message_unit_price = llm_usage.prompt_unit_price
|
|
|
|
updated_agent_thought.answer_token = llm_usage.completion_tokens
|
|
|
|
agent_thought.answer_token = llm_usage.completion_tokens
|
|
|
|
updated_agent_thought.answer_price_unit = llm_usage.completion_price_unit
|
|
|
|
agent_thought.answer_price_unit = llm_usage.completion_price_unit
|
|
|
|
updated_agent_thought.answer_unit_price = llm_usage.completion_unit_price
|
|
|
|
agent_thought.answer_unit_price = llm_usage.completion_unit_price
|
|
|
|
updated_agent_thought.tokens = llm_usage.total_tokens
|
|
|
|
agent_thought.tokens = llm_usage.total_tokens
|
|
|
|
updated_agent_thought.total_price = llm_usage.total_price
|
|
|
|
agent_thought.total_price = llm_usage.total_price
|
|
|
|
|
|
|
|
|
|
|
|
# check if tool labels is not empty
|
|
|
|
# check if tool labels is not empty
|
|
|
|
labels = updated_agent_thought.tool_labels or {}
|
|
|
|
labels = agent_thought.tool_labels or {}
|
|
|
|
tools = updated_agent_thought.tool.split(";") if updated_agent_thought.tool else []
|
|
|
|
tools = agent_thought.tool.split(";") if agent_thought.tool else []
|
|
|
|
for tool in tools:
|
|
|
|
for tool in tools:
|
|
|
|
if not tool:
|
|
|
|
if not tool:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
@ -385,7 +381,7 @@ class BaseAgentRunner(AppRunner):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
labels[tool] = {"en_US": tool, "zh_Hans": tool}
|
|
|
|
labels[tool] = {"en_US": tool, "zh_Hans": tool}
|
|
|
|
|
|
|
|
|
|
|
|
updated_agent_thought.tool_labels_str = json.dumps(labels)
|
|
|
|
agent_thought.tool_labels_str = json.dumps(labels)
|
|
|
|
|
|
|
|
|
|
|
|
if tool_invoke_meta is not None:
|
|
|
|
if tool_invoke_meta is not None:
|
|
|
|
if isinstance(tool_invoke_meta, dict):
|
|
|
|
if isinstance(tool_invoke_meta, dict):
|
|
|
|
@ -394,7 +390,7 @@ class BaseAgentRunner(AppRunner):
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
|
tool_invoke_meta = json.dumps(tool_invoke_meta)
|
|
|
|
tool_invoke_meta = json.dumps(tool_invoke_meta)
|
|
|
|
|
|
|
|
|
|
|
|
updated_agent_thought.tool_meta_str = tool_invoke_meta
|
|
|
|
agent_thought.tool_meta_str = tool_invoke_meta
|
|
|
|
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
db.session.commit()
|
|
|
|
db.session.close()
|
|
|
|
db.session.close()
|
|
|
|
|