Skip to content

Commit

Permalink
work in progress - tool prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-cohere committed Mar 26, 2024
1 parent d0865d0 commit 4ff583b
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions libs/partners/cohere/langchain_cohere/multi_hop/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.language_models import BaseLanguageModel
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.runnables import Runnable, RunnablePassthrough
from langchain_core.tools import BaseTool
Expand Down Expand Up @@ -188,14 +189,35 @@ def parse(self, text: str) -> Union[List[AgentAction], AgentFinish]:


def render_tool_description(tool: BaseTool) -> str:
"""Render the tool name and description. TODO: better way to render?"""
template = """```python
def {TOOL_NAME}():
\"\"\"{DESCRIPTION}
\"\"\"
"""Render the tool in the style of a Python function."""
function_signature = []
args_description = []
for parameter_name, parameter_definition in tool.args.items():
if "default" in parameter_definition:
parameter_type = f"Optional[{parameter_definition.get("type", "str")}]"
else:
parameter_type = parameter_definition.get("type", "str")
function_signature += f"{parameter_name}: {parameter_type}"
args_description += f"{parameter_name} ({parameter_type}): {parameter_definition.get("description", "")}"

Check failure on line 201 in libs/partners/cohere/langchain_cohere/multi_hop/agent.py

View workflow job for this annotation

GitHub Actions / cd libs/partners/cohere / make lint #3.11

Ruff (E501)

langchain_cohere/multi_hop/agent.py:201:89: E501 Line too long (113 > 88)

if args_description:
args_description = """Args:
"""+"\n ".join(args_description)

template = PromptTemplate(template_format="f-string").from_template("""```python
def {TOOL_NAME}({SIGNATURE}) -> List[Dict]:
\"\"\"
{DESCRIPTION}
{ARGS_DESCRIPTION}
\"\"\"
pass
```"""
return template.format(TOOL_NAME=tool.name, DESCRIPTION=tool.description)
```""")
return template.format(
TOOL_NAME=tool.name,
DESCRIPTION=tool.description,
SIGNATURE=", ".join(function_signature),
ARGS_DESCRIPTION=args_description,
)


def format_cohere_log_to_str(
Expand Down

0 comments on commit 4ff583b

Please sign in to comment.