avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2536

Abstract

ass="hljs-title function_">predict_action</span>(<span class="hljs-params"><span class="hljs-variable language_">self</span>, input_data</span>): <span class="hljs-comment"># Logic to predict the next action based on input_data</span> <span class="hljs-comment"># ...</span> <span class="hljs-keyword">return</span> <span class="hljs-title class_">AgentAction</span>(action=<span class="hljs-string">'tool_name'</span>, action_input=<span class="hljs-string">'input_data'</span>)

<span class="hljs-keyword">def</span> <span class="hljs-title function_">handle_observation</span>(<span class="hljs-params"><span class="hljs-variable language_">self</span>, observation</span>):
    <span class="hljs-comment"># Logic to handle the observation and decide whether to continue or finish</span>
    <span class="hljs-comment"># ...</span>
    <span class="hljs-keyword">if</span> observation == <span class="hljs-string">'stop_condition'</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-title class_">AgentFinish</span>(message=<span class="hljs-string">'Agent finished'</span>)
    <span class="hljs-symbol">else:</span>
		<span class="hljs-comment"># Return the next action based on the observation</span>
        <span class="hljs-keyword">return</span> <span class="hljs-title class_">AgentAction</span>(action=<span class="hljs-string">'next_tool'</span>, action_input=<span class="hljs-string">'next_input'</span>)</pre></div><h2 id="71a2">LLMSingleActionAgent</h2><p id="49e9">The <code>LLMSingleActionAgent</code> is a modular implementation of the <code>BaseSingleActionAgent</code> that provides greater customization. It consists of a <code>PromptTemplate</code>, <code>LLM</code>, <code>stop</code> sequence, and <code>OutputParser</code>, 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.</p><p id="305d">Below is an example of an <code>LLMSingleActionAgent</code> in TypeScript:</p><div id="ca46"><pre><span class="hljs-keyword">import</span> { <span class="hljs-type">BaseSingleActionAgent</span>, <span class="hljs-type">AgentAction</span>, <span class="hljs-type">AgentFinish</span> } from 'langchain';

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyLLMSingleActionAgent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseSingleActionAgent</span> </span>{ async

Options

predictAction(inputData: any): <span class="hljs-type">Promise</span><<span class="hljs-type">AgentAction</span>> { <span class="hljs-comment">// Use PromptTemplate to create a prompt based on the input data</span> const prompt = <span class="hljs-type">PromptTemplate</span>.generatePrompt(inputData);

    <span class="hljs-comment">// Call the LLM with the prompt and stop sequence</span>
    const output = await <span class="hljs-type">LLM</span>.generateOutput(prompt, 'stop_sequence');
    
    <span class="hljs-comment">// Use the OutputParser to parse the LLM output</span>
    const action = <span class="hljs-type">OutputParser</span>.parseOutput(output);
    
    <span class="hljs-keyword">return</span> action;
}

}</pre></div><p id="069b">The <code>LLMSingleActionAgent</code> provides extensive customization options, including defining prompt templates, modifying the language model, and handling output parsing.</p><p id="5b97">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.</p><p id="2249">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.</p><div id="6cb7" class="link-block"> <a href="https://readmedium.com/langchain-round-agents-c59c3eb62796"> <div> <div> <h2>LANGCHAIN — Round Agents</h2> <div><h3>The human spirit must prevail over technology. — Albert Einstein</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nu7ZXSdSXeo6aCLEJYoZpg.jpeg)"></div> </div> </div> </a> </div><p id="f1da">For more detailed guides and documentation on creating custom agents using Python and TypeScript, refer to the official LangChain documentation for <a href="https://python.langchain.com/docs/modules/agents/how_to/custom_llm_agent">Python Custom Agent Docs</a> and <a href="https://js.langchain.com/docs/modules/agents/agents/custom_llm">TypeScript Custom Agent Docs</a>.</p></article></body>

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.

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.

Langchain
Custom
ChatGPT
Recommended from ReadMedium