APIChains and Agents #4367
Replies: 1 comment
-
🤖 Sure, I can guide you on how to use agents in the LangChain Javascript framework to interact with an API endpoint using just the documentation. Here's a step-by-step guide on how to do this:
import { FaissStore } from "@langchain/community/vectorstores/faiss";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { TextLoader } from "langchain/document_loaders/fs/text";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import {
createRetrieverTool,
createConversationalRetrievalAgent,
} from "langchain/agents/toolkits";
import { ChatOpenAI } from "langchain/chat_models/openai";
const loader = new TextLoader("state_of_the_union.txt");
const docs = await loader.load();
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 0,
});
const texts = await splitter.splitDocuments(docs);
const vectorStore = await FaissStore.fromDocuments(
texts,
new OpenAIEmbeddings()
);
const retriever = vectorStore.asRetriever();
const tool = createRetrieverTool(retriever, {
name: "search_state_of_union",
description:
"Searches and returns documents regarding the state-of-the-union.",
});
const model = new ChatOpenAI({});
const executor = await createConversationalRetrievalAgent(model, [tool], {
verbose: true,
});
const result = await executor.invoke({
input: "Hi, I'm Bob!",
});
console.log(result); Please note that the above example is based on the context provided and may need to be adjusted based on the specific requirements of your application and the API endpoint you are interacting with. You can find more details in the examples/src/guides/conversational_retrieval/agent.ts file in the repository. Regarding your question about the I hope this helps! If you have any other questions, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I wanted to know since the APIChain is a legacy chain is there a way to get agents to interact with an API endpoint by just proving the documentation.
Beta Was this translation helpful? Give feedback.
All reactions