Skip to content

Commit

Permalink
docs[minor]: Replace deprecated code examples with LCEL & AgentExecut…
Browse files Browse the repository at this point in the history
…or (#6149)

* adding v0.2 langchainjs support replaced ZeroShotAgent with createReactAgent

* Using the LangChain Expression Language (LCEL) instead of LLMChain deprecated class

* fixing farmat ci check

---------

Co-authored-by: Brace Sproul <[email protected]>
  • Loading branch information
Srijan-D and bracesproul authored Jul 19, 2024
1 parent b9f3bcf commit 326cb85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions examples/src/chains/llm_chain.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { OpenAI } from "@langchain/openai";
import { LLMChain } from "langchain/chains";
import { PromptTemplate } from "@langchain/core/prompts";

// We can construct an LLMChain from a PromptTemplate and an LLM.
const model = new OpenAI({ temperature: 0 });
const prompt = PromptTemplate.fromTemplate(
"What is a good name for a company that makes {product}?"
);
const chainA = new LLMChain({ llm: model, prompt });

const chainA = prompt.pipe({ llm: model });

// The result is an object with a `text` property.
const resA = await chainA.invoke({ product: "colorful socks" });
Expand Down
56 changes: 24 additions & 32 deletions examples/src/chat/agent.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { LLMChain } from "langchain/chains";
import { ChatOpenAI } from "@langchain/openai";
import { ZeroShotAgent, AgentExecutor } from "langchain/agents";
import {
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
} from "@langchain/core/prompts";
import { AgentExecutor, createReactAgent } from "langchain/agents";
import { pull } from "langchain/hub";
import type { PromptTemplate } from "@langchain/core/prompts";

import { OpenAI } from "@langchain/openai";

import { SerpAPI } from "@langchain/community/tools/serpapi";

export const run = async () => {
// Define the tools the agent will have access to.
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
Expand All @@ -17,36 +16,29 @@ export const run = async () => {
}),
];

const prompt = ZeroShotAgent.createPrompt(tools, {
prefix: `Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:`,
suffix: `Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args"`,
});

const chatPrompt = ChatPromptTemplate.fromMessages([
new SystemMessagePromptTemplate(prompt),
HumanMessagePromptTemplate.fromTemplate(`{input}
// Get the prompt to use - you can modify this!
// If you want to see the prompt in full, you can at:
// https://smith.langchain.com/hub/hwchase17/react
const prompt = await pull<PromptTemplate>("hwchase17/react");

This was your previous work (but I haven't seen any of it! I only see what you return as final answer):
{agent_scratchpad}`),
]);

const chat = new ChatOpenAI({});

const llmChain = new LLMChain({
prompt: chatPrompt,
llm: chat,
const llm = new OpenAI({
temperature: 0,
});

const agent = new ZeroShotAgent({
llmChain,
allowedTools: tools.map((tool) => tool.name),
const agent = await createReactAgent({
llm,
tools,
prompt,
});

const executor = AgentExecutor.fromAgentAndTools({ agent, tools });
const agentExecutor = new AgentExecutor({
agent,
tools,
});

const response = await executor.invoke({
input: "How many people live in canada as of 2023?",
const result = await agentExecutor.invoke({
input: "what is LangChain?",
});

console.log(response);
console.log(result);
};

0 comments on commit 326cb85

Please sign in to comment.