Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs[minor]: Replace deprecated code examples with LCEL & AgentExecutor #6149

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions examples/src/chat/agent.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { LLMChain } from "langchain/chains";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey team, I've flagged a change in the PR that accesses an environment variable using process.env. Please review this to ensure it aligns with our security and configuration best practices.

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 +17,29 @@ export const run = async () => {
}),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! I noticed that this PR introduces a new external HTTP request to pull the prompt template. I've flagged this change for your review to ensure it aligns with our project's requirements. Let me know if you have any questions or need further clarification!

];

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}

This was your previous work (but I haven't seen any of it! I only see what you return as final answer):
{agent_scratchpad}`),
]);
// 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");

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);
};