
LANGCHAIN — Custom Agents
Technological change is not additive; it is ecological. A new technology does not merely add something; it changes everything. — Neil Postman
Creating custom agents is an essential part of working with LangChain. The platform provides powerful abstractions that allow you to create custom agents using Python and TypeScript. In this article, we’ll explore the BaseSingleActionAgent and LLMSingleActionAgent abstractions and how they can be used to build highly customizable agents.
BaseSingleActionAgent
The BaseSingleActionAgent is the fundamental abstraction for creating agents that predict a single action at a time. It is used in the current AgentExecutor, which can be thought of as a loop that passes user input and any previous steps to the agent. The agent returns an AgentFinish to end the run or an AgentAction to call a tool and get an Observation. This process continues until an AgentFinish is emitted.
Here’s a basic implementation of a BaseSingleActionAgent in Python:
from langchain.agents import BaseSingleActionAgent, AgentAction, AgentFinish
class MySingleActionAgent(BaseSingleActionAgent):
def predict_action(self, input_data):
# Logic to predict the next action based on input_data
# ...
return AgentAction(action='tool_name', action_input='input_data')
def handle_observation(self, observation):
# Logic to handle the observation and decide whether to continue or finish
# ...
if observation == 'stop_condition':
return AgentFinish(message='Agent finished')
else:
# Return the next action based on the observation
return AgentAction(action='next_tool', action_input='next_input')LLMSingleActionAgent
The LLMSingleActionAgent is a modular implementation of the BaseSingleActionAgent that provides greater customization. It consists of a PromptTemplate, LLM, stop sequence, and OutputParser, allowing you to instruct the language model on what to do, define the language model, specify a stop sequence, and parse the output of the language model.
Below is an example of an LLMSingleActionAgent in TypeScript:
import { BaseSingleActionAgent, AgentAction, AgentFinish } from 'langchain';
class MyLLMSingleActionAgent extends BaseSingleActionAgent {
async predictAction(inputData: any): Promise<AgentAction> {
// Use PromptTemplate to create a prompt based on the input data
const prompt = PromptTemplate.generatePrompt(inputData);
// Call the LLM with the prompt and stop sequence
const output = await LLM.generateOutput(prompt, 'stop_sequence');
// Use the OutputParser to parse the LLM output
const action = OutputParser.parseOutput(output);
return action;
}
}The LLMSingleActionAgent provides extensive customization options, including defining prompt templates, modifying the language model, and handling output parsing.
By utilizing these abstractions, you can create highly customizable agents tailored to your specific use case. Whether you need agents with unique personalities, custom language models, or specialized output parsing, LangChain’s abstractions provide the flexibility to meet your requirements.
In future directions, LangChain aims to expand the capabilities of agents, including using embeddings for tool selection, introducing new types of agents, and inviting the community to contribute to agent development.
LANGCHAIN — Round Agents
The human spirit must prevail over technology. — Albert Einstein
medium.com
For more detailed guides and documentation on creating custom agents using Python and TypeScript, refer to the official LangChain documentation for Python Custom Agent Docs and TypeScript Custom Agent Docs.
