Using tools
Tools are also runnables, and can therefore be used within a chain:
- npm
- Yarn
- pnpm
npm install @langchain/anthropic
yarn add @langchain/anthropic
pnpm add @langchain/anthropic
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatAnthropic } from "@langchain/anthropic";
import { SerpAPI } from "@langchain/community/tools/serpapi";
const search = new SerpAPI();
const prompt =
PromptTemplate.fromTemplate(`Turn the following user input into a search query for a search engine:
{input}`);
const model = new ChatAnthropic({});
const chain = prompt.pipe(model).pipe(new StringOutputParser()).pipe(search);
const result = await chain.invoke({
input: "Who is the current prime minister of Malaysia?",
});
console.log(result);
/*
Anwar Ibrahim
*/
API Reference:
- PromptTemplate from
@langchain/core/prompts
- StringOutputParser from
@langchain/core/output_parsers
- ChatAnthropic from
@langchain/anthropic
- SerpAPI from
@langchain/community/tools/serpapi