-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore
tasks
namespace, create agent factory (#33)
Create an agent factory for evaluating tool usage tasks
- Loading branch information
Showing
10 changed files
with
1,040 additions
and
114 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Code for creating an agent factory for evaluating tool usage tasks.""" | ||
from langchain.agents import AgentExecutor | ||
from langchain.agents.format_scratchpad import format_to_openai_functions | ||
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder | ||
from langchain.schema.runnable import Runnable | ||
from langchain.tools.render import format_tool_to_openai_function | ||
|
||
from langchain_benchmarks.schema import ToolUsageTask | ||
|
||
|
||
def _ensure_output_exists(inputs: dict) -> dict: | ||
"""Make sure that the output key is always present.""" | ||
if "output" not in inputs: | ||
return {"output": "", **inputs} | ||
return inputs | ||
|
||
|
||
class OpenAIAgentFactory: | ||
def __init__( | ||
self, task: ToolUsageTask, *, model: str = "gpt-3.5-turbo-16k" | ||
) -> None: | ||
"""Create an OpenAI agent factory for the given task. | ||
Args: | ||
task: The task to create an agent factory for. | ||
model: The model to use -- this must be an open AI model. | ||
""" | ||
self.task = task | ||
self.model = model | ||
|
||
def create(self) -> Runnable: | ||
"""Agent Executor""" | ||
llm = ChatOpenAI( | ||
model=self.model, | ||
temperature=0, | ||
) | ||
|
||
env = self.task.create_environment() | ||
|
||
llm_with_tools = llm.bind( | ||
functions=[format_tool_to_openai_function(t) for t in env.tools] | ||
) | ||
prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
( | ||
"system", | ||
self.task.instructions, | ||
), | ||
MessagesPlaceholder(variable_name="agent_scratchpad"), | ||
("user", "{input}"), | ||
] | ||
) | ||
|
||
runnable_agent = ( | ||
{ | ||
"input": lambda x: x["question"], | ||
"agent_scratchpad": lambda x: format_to_openai_functions( | ||
x["intermediate_steps"] | ||
), | ||
} | ||
| prompt | ||
| llm_with_tools | ||
| OpenAIFunctionsAgentOutputParser() | ||
) | ||
|
||
return ( | ||
AgentExecutor( | ||
agent=runnable_agent, | ||
tools=env.tools, | ||
handle_parsing_errors=True, | ||
return_intermediate_steps=True, | ||
) | ||
| _ensure_output_exists | ||
) |
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
def test_import_tool_usage() -> None: | ||
"""Test that tool_usage can be imported""" | ||
from langchain_benchmarks.tool_usage import evaluators # noqa: F401 |