avatarLaxfed Paulacy

Summary

The web content outlines the foundational development of LLM applications using LangChain.js and Zep, detailing the creation of a conversational bot, a Retrieval Augmented Generation (RAG) app, and a REACT-type agent.

Abstract

The article "LANGCHAIN — Foundations of LLM App Development with LangChain JS and Zep" delves into the use of LangChain.js and Zep for building three core applications leveraging large language models (LLMs). It begins by explaining the initialization of Zep in an app, creating a session ID, and loading chat history into memory. The first application demonstrated is a simple conversational bot, which utilizes ZepMemory and a conversation chain to recall chat history and interact with users. The second example is a Q&A over Docs/RAG-type app, which uses ZepVectorStore to search over a document collection and answer questions based on the retrieved information. The final example illustrates the construction of a REACT-type agent that employs Zep’s memory retrieval and search capabilities as tools for more complex interactions, such as searching for authors, characters, and passages within a specific domain. The article emphasizes the importance of waiting for Zep to finish embedding documents and provides code snippets for each step of the application development process, offering a comprehensive guide for developers to build upon when creating advanced LLM applications.

Opinions

  • The author suggests that digital design is an ongoing process, akin to painting with never-drying paint, emphasizing the dynamic and evolving nature of technology and design.
  • The article conveys that technology, particularly in the realm of AI and communication, is a prevalent myth of our times, highlighting the nuanced relationship between technological advancement and human interaction.
  • The author's approach to teaching LLM app development is hands-on, providing readers with practical code examples and step-by-step instructions.
  • The use of Zep's capabilities, such as ZepMemory and ZepVectorStore, is presented as a key factor in streamlining the development of LLM applications.
  • The article implies that the integration of AI endpoints, as demonstrated by the examples, is crucial for building unified and advanced AI-driven applications.

LANGCHAIN — Foundations of LLM App Development with LangChain JS and Zep

Digital design is like painting, except the paint never dries. — Neville Brody

In this article, we will explore the foundations of LLM app development using LangChain.js and Zep. We will cover the creation of three foundational LLM apps, including a simple conversational bot, a Retrieval Augmented Generation (RAG) app, and a REACT-type agent. Each example will demonstrate the usage of LangChain’s ZepMemory and ZepVectorStore classes.

Building a Simple Conversational Bot

To begin, let’s initialize Zep in our app and create a session ID to represent the user or a chat session. Then, we’ll load some test data into the chat history for this session using the following code:

const client = await ZepClient.init(ZEP_API_URL, ZEP_API_KEY);
const sessionId = randomUUID();
const messages = history.map(({ role, content }) => new Message({ role, content }));
const zepMemory = new Memory({ messages });
await client.memory.addMemory(sessionId, zepMemory);

Next, we’ll create an instance of ZepMemory and a conversation chain to recall the chat history. We'll ask the LLM what has been discussed so far, as shown in the following code snippet:

const model = new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0 });
const memorySimple = new ZepMemory({ sessionId, baseURL: ZEP_API_URL, apiKey: ZEP_API_KEY });
const conversationChain = new ConversationChain({ llm: model, memory: memorySimple });
const res1 = await conversationChain.run("What have we discussed so far?");
console.log(res1);

Building a Q&A over Docs / RAG-type App

In this example, we’ll use Zep’s ZepVectorStore to support a ConversationalRetrievalQAChain that searches over a Zep document collection. We'll load documents into the vector store and wait for Zep to finish embedding them, as shown in the following code snippet:

const vectorStore = await loadDocsIntoVectorStore(ZEP_COLLECTION_CONFIG);

// Wait for the ZepVectorStore to finish embedding the documents
while (true) {
  const c = await client.document.getCollection(ZEP_COLLECTION_NAME);
  console.log(`Embedding status: ${c.document_embedded_count}/${c.document_count} documents embedded`);
  await new Promise((resolve) => setTimeout(resolve, 1000));
  if (c.status === "ready") {
    break;
  }
}

We’ll then configure the ZepMemory and ConversationalRetrievalQAChain classes and question the LLM about the books. The following code demonstrates the setup:

const memory = new ZepMemory({ sessionId, baseURL: ZEP_API_URL, apiKey: ZEP_API_KEY, memoryKey: "chat_history", inputKey: "question", outputKey: "text" });
const chain = ConversationalRetrievalQAChain.fromLLM(model, vectorStore.asRetriever({ searchType: "mmr", k: 4 }), { memory: memory });

Building a REACT-type Agent with Zep Memory Retrieval and Search as Tools

Lastly, we’ll build an agent that uses Zep’s conversational history and vector store as tools. We’ll use the ZepRetriever for searching over the session's chat history and create tools for searching for authors, characters, and passages in sci-fi books. Below is an example of setting up the tools and initializing the agent:

const zepMemoryRetriever = await new ZepRetriever({ url: ZEP_API_URL, apiKey: ZEP_API_KEY, sessionId: sessionId });

const tools = [
  new DynamicTool({
    name: "peopleRetriever",
    description: `call this if you want to search for authors, characters, or people we may have discussed in the past. input should be a search string`,
    func: async (query) => await getPeopleFromMemoryTool(query, zepMemoryRetriever),
  }),
  new DynamicTool({
    name: "bookSearch",
    description: "call this if to search for passages in sci-fi books. input should be a search string",
    func: async (query) => await getBookSearchTool(query, vectorStore),
  }),
];

const executor = await initializeAgentExecutorWithOptions(tools, model, { agentType: "zero-shot-react-description" });

These examples illustrate how to build foundational LLM apps using LangChain.js and Zep. They provide a solid starting point for building more advanced applications with LangChain’s features and Zep’s capabilities.

Langchain
Foundations
Development
ChatGPT
Zep
Recommended from ReadMedium