Skip to content

Commit

Permalink
Merge pull request #421 from Pingdred/better_agent
Browse files Browse the repository at this point in the history
Reactivated the hook agent_prompt_instructions
  • Loading branch information
pieroit authored Aug 21, 2023
2 parents e2d94d2 + b017266 commit 3041428
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 53 deletions.
22 changes: 2 additions & 20 deletions core/cat/looking_glass/agent_manager.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import re
import traceback
import json
from copy import copy

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.agents import AgentExecutor, LLMSingleActionAgent, AgentOutputParser
from langchain.agents import AgentExecutor, LLMSingleActionAgent

from cat.looking_glass.prompts import ToolPromptTemplate
from cat.looking_glass.output_parser import ToolOutputParser
Expand Down Expand Up @@ -33,7 +28,7 @@ def execute_tool_agent(self, agent_input, allowed_tools):
allowed_tools_names = [t.name for t in allowed_tools]

prompt = ToolPromptTemplate(
#template= TODO: get from hook,
template = self.cat.mad_hatter.execute_hook("agent_prompt_instructions"),
tools=allowed_tools,
# This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically
# This includes the `intermediate_steps` variable because it is needed to fill the scratchpad
Expand Down Expand Up @@ -109,21 +104,8 @@ def execute_agent(self, agent_input):
return fast_reply

prompt_prefix = mad_hatter.execute_hook("agent_prompt_prefix")
#prompt_format_instructions = mad_hatter.execute_hook("agent_prompt_instructions")
prompt_suffix = mad_hatter.execute_hook("agent_prompt_suffix")

#input_variables = [
# "input",
# "chat_history",
# "episodic_memory",
# "declarative_memory",
# "agent_scratchpad",
#]

#input_variables = mad_hatter.execute_hook("before_agent_creates_prompt", input_variables,
# " ".join([prompt_prefix, prompt_format_instructions, prompt_suffix]))


allowed_tools = mad_hatter.execute_hook("agent_allowed_tools")

# Try to get information from tools if there is some allowed
Expand Down
2 changes: 1 addition & 1 deletion core/cat/looking_glass/output_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from langchain.agents import AgentOutputParser
from langchain.schema import AgentAction, AgentFinish, OutputParserException
from typing import List, Union
from typing import Union


class ToolOutputParser(AgentOutputParser):
Expand Down
27 changes: 1 addition & 26 deletions core/cat/looking_glass/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,9 @@
from langchain.agents.tools import BaseTool
from langchain.prompts import StringPromptTemplate

# TODO: get by hook
DEFAULT_TOOL_TEMPLATE = """Answer the following question: `{input}`
You can only reply using these tools:
{tools}
none_of_the_others: none_of_the_others(None) - Use this tool if none of the others tools help. Input is always None.
If you want to use tools, use the following format:
Action: the name of the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
...
Action: the name of the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
When you have a final answer respond with:
Final Answer: the final answer to the original input question
Begin!
Question: {input}
{agent_scratchpad}"""


class ToolPromptTemplate(StringPromptTemplate):
# The template to use
template: str = DEFAULT_TOOL_TEMPLATE
template: str
# The list of tools available
tools: List[BaseTool]

Expand Down
30 changes: 24 additions & 6 deletions core/cat/mad_hatter/core_plugin/hooks/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import List, Dict
from datetime import timedelta
from langchain.docstore.document import Document
from langchain.agents.conversational import prompt

from cat.utils import verbal_timedelta
from cat.mad_hatter.decorators import hook
Expand Down Expand Up @@ -87,13 +86,32 @@ def agent_prompt_instructions(cat) -> str:
"""

# Check if procedural memory is disabled
prompt_settings = cat.working_memory["user_message_json"]["prompt_settings"]
if not prompt_settings["use_procedural_memory"]:
return ""
DEFAULT_TOOL_TEMPLATE = """Answer the following question: `{input}`
You can only reply using these tools:
{tools}
none_of_the_others: none_of_the_others(None) - Use this tool if none of the others tools help. Input is always None.
If you want to use tools, use the following format:
Action: the name of the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
...
Action: the name of the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
When you have a final answer respond with:
Final Answer: the final answer to the original input question
Begin!
Question: {input}
{agent_scratchpad}"""


# here we piggy back directly on langchain agent instructions. Different instructions will require a different OutputParser
return prompt.FORMAT_INSTRUCTIONS
return DEFAULT_TOOL_TEMPLATE


@hook(priority=0)
Expand Down

0 comments on commit 3041428

Please sign in to comment.