Skip to content

Commit

Permalink
apply ruff reformat in api/core
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenliang123 committed Aug 26, 2024
1 parent 23cedc3 commit 596b5c1
Show file tree
Hide file tree
Showing 685 changed files with 20,894 additions and 20,402 deletions.
2 changes: 1 addition & 1 deletion api/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import core.moderation.base
import core.moderation.base
236 changes: 127 additions & 109 deletions api/core/agent/base_agent_runner.py

Large diffs are not rendered by default.

215 changes: 94 additions & 121 deletions api/core/agent/cot_agent_runner.py

Large diffs are not rendered by default.

26 changes: 12 additions & 14 deletions api/core/agent/cot_chat_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ def _organize_system_prompt(self) -> SystemPromptMessage:
prompt_entity = self.app_config.agent.prompt
first_prompt = prompt_entity.first_prompt

system_prompt = first_prompt \
.replace("{{instruction}}", self._instruction) \
.replace("{{tools}}", json.dumps(jsonable_encoder(self._prompt_messages_tools))) \
.replace("{{tool_names}}", ', '.join([tool.name for tool in self._prompt_messages_tools]))
system_prompt = (
first_prompt.replace("{{instruction}}", self._instruction)
.replace("{{tools}}", json.dumps(jsonable_encoder(self._prompt_messages_tools)))
.replace("{{tool_names}}", ", ".join([tool.name for tool in self._prompt_messages_tools]))
)

return SystemPromptMessage(content=system_prompt)

def _organize_user_query(self, query, prompt_messages: list[PromptMessage] = None) -> list[PromptMessage]:
def _organize_user_query(self, query, prompt_messages: list[PromptMessage] = None) -> list[PromptMessage]:
"""
Organize user query
"""
Expand All @@ -43,7 +44,7 @@ def _organize_user_query(self, query, prompt_messages: list[PromptMessage] = No

def _organize_prompt_messages(self) -> list[PromptMessage]:
"""
Organize
Organize
"""
# organize system prompt
system_message = self._organize_system_prompt()
Expand All @@ -53,7 +54,7 @@ def _organize_prompt_messages(self) -> list[PromptMessage]:
if not agent_scratchpad:
assistant_messages = []
else:
assistant_message = AssistantPromptMessage(content='')
assistant_message = AssistantPromptMessage(content="")
for unit in agent_scratchpad:
if unit.is_final():
assistant_message.content += f"Final Answer: {unit.agent_response}"
Expand All @@ -71,18 +72,15 @@ def _organize_prompt_messages(self) -> list[PromptMessage]:

if assistant_messages:
# organize historic prompt messages
historic_messages = self._organize_historic_prompt_messages([
system_message,
*query_messages,
*assistant_messages,
UserPromptMessage(content='continue')
])
historic_messages = self._organize_historic_prompt_messages(
[system_message, *query_messages, *assistant_messages, UserPromptMessage(content="continue")]
)
messages = [
system_message,
*historic_messages,
*query_messages,
*assistant_messages,
UserPromptMessage(content='continue')
UserPromptMessage(content="continue"),
]
else:
# organize historic prompt messages
Expand Down
21 changes: 12 additions & 9 deletions api/core/agent/cot_completion_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ def _organize_instruction_prompt(self) -> str:
prompt_entity = self.app_config.agent.prompt
first_prompt = prompt_entity.first_prompt

system_prompt = first_prompt.replace("{{instruction}}", self._instruction) \
.replace("{{tools}}", json.dumps(jsonable_encoder(self._prompt_messages_tools))) \
.replace("{{tool_names}}", ', '.join([tool.name for tool in self._prompt_messages_tools]))

system_prompt = (
first_prompt.replace("{{instruction}}", self._instruction)
.replace("{{tools}}", json.dumps(jsonable_encoder(self._prompt_messages_tools)))
.replace("{{tool_names}}", ", ".join([tool.name for tool in self._prompt_messages_tools]))
)

return system_prompt

def _organize_historic_prompt(self, current_session_messages: list[PromptMessage] = None) -> str:
Expand Down Expand Up @@ -46,7 +48,7 @@ def _organize_prompt_messages(self) -> list[PromptMessage]:

# organize current assistant messages
agent_scratchpad = self._agent_scratchpad
assistant_prompt = ''
assistant_prompt = ""
for unit in agent_scratchpad:
if unit.is_final():
assistant_prompt += f"Final Answer: {unit.agent_response}"
Expand All @@ -61,9 +63,10 @@ def _organize_prompt_messages(self) -> list[PromptMessage]:
query_prompt = f"Question: {self._query}"

# join all messages
prompt = system_prompt \
.replace("{{historic_messages}}", historic_prompt) \
.replace("{{agent_scratchpad}}", assistant_prompt) \
prompt = (
system_prompt.replace("{{historic_messages}}", historic_prompt)
.replace("{{agent_scratchpad}}", assistant_prompt)
.replace("{{query}}", query_prompt)
)

return [UserPromptMessage(content=prompt)]
return [UserPromptMessage(content=prompt)]
16 changes: 10 additions & 6 deletions api/core/agent/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AgentToolEntity(BaseModel):
"""
Agent Tool Entity.
"""

provider_type: Literal["builtin", "api", "workflow"]
provider_id: str
tool_name: str
Expand All @@ -18,6 +19,7 @@ class AgentPromptEntity(BaseModel):
"""
Agent Prompt Entity.
"""

first_prompt: str
next_iteration: str

Expand All @@ -31,6 +33,7 @@ class Action(BaseModel):
"""
Action Entity.
"""

action_name: str
action_input: Union[dict, str]

Expand All @@ -39,8 +42,8 @@ def to_dict(self) -> dict:
Convert to dictionary.
"""
return {
'action': self.action_name,
'action_input': self.action_input,
"action": self.action_name,
"action_input": self.action_input,
}

agent_response: Optional[str] = None
Expand All @@ -54,10 +57,10 @@ def is_final(self) -> bool:
Check if the scratchpad unit is final.
"""
return self.action is None or (
'final' in self.action.action_name.lower() and
'answer' in self.action.action_name.lower()
"final" in self.action.action_name.lower() and "answer" in self.action.action_name.lower()
)


class AgentEntity(BaseModel):
"""
Agent Entity.
Expand All @@ -67,8 +70,9 @@ class Strategy(Enum):
"""
Agent Strategy.
"""
CHAIN_OF_THOUGHT = 'chain-of-thought'
FUNCTION_CALLING = 'function-calling'

CHAIN_OF_THOUGHT = "chain-of-thought"
FUNCTION_CALLING = "function-calling"

provider: str
model: str
Expand Down
Loading

0 comments on commit 596b5c1

Please sign in to comment.