{"id":37396,"date":"2023-10-30T22:01:15","date_gmt":"2023-10-30T22:01:15","guid":{"rendered":"https:\/\/www.baeldung.com\/java-langchain-basics"},"modified":"2023-10-30T22:01:15","modified_gmt":"2023-10-30T22:01:15","slug":"introduction-to-langchain","status":"publish","type":"post","link":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/2023\/10\/30\/introduction-to-langchain\/","title":{"rendered":"Introduction to LangChain"},"content":{"rendered":"<p><img src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/08\/Baeldung-Featured-Image-1024x536.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"\" decoding=\"async\" style=\"float: left; margin-right: 5px;\" loading=\"lazy\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/08\/Baeldung-Featured-Image-1024x536.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/08\/Baeldung-Featured-Image-300x157.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/08\/Baeldung-Featured-Image-768x402.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/08\/Baeldung-Featured-Image-100x52.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/08\/Baeldung-Featured-Image.png 1200w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/p>\n<h2  id=\"bd-introduction\" data-id=\"introduction\">1. Introduction<\/h2>\n<div class=\"bd-anchor\" id=\"introduction\"><\/div>\n<p>In this tutorial, we&#8217;ll examine the details of <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.langchain.com\/\">LangChain<\/a>, a framework for developing applications powered by language models. We&#8217;ll begin by gathering basic concepts around the language models that will help in this tutorial.<\/p>\n<p>Although LangChain is primarily available in Python and JavaScript\/TypeScript versions, there are options to use LangChain in Java. We&#8217;ll discuss the building blocks of LangChain as a framework and then proceed to experiment with them in Java.<\/p>\n<h2  id=\"bd-background\" data-id=\"background\">2. Background<\/h2>\n<div class=\"bd-anchor\" id=\"background\"><\/div>\n<p>Before we go deeper into why we need a framework for building applications powered by language models, it&#8217;s imperative that we first understand what language models are. We&#8217;ll also cover some of the typical complexities encountered when working with language models.<\/p>\n<h3 id=\"bd-1-large-language-models\" data-id=\"1-large-language-models\">2.1. Large Language Models<\/h3>\n<div class=\"bd-anchor\" id=\"1-large-language-models\"><\/div>\n<p>A language model is <strong>a probabilistic model of a natural language<\/strong> that can generate probabilities of a series of words. A <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/cs\/large-language-models\">large language model<\/a> (LLM) is a language model characterized by its large size. They&#8217;re artificial neural networks with possibly billions of parameters.<\/p>\n<p>An LLM is often <strong>pre-trained on a vast amount of unlabeled data<\/strong> using <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/en.wikipedia.org\/wiki\/Self-supervised_learning\">self-supervised<\/a> and <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/en.wikipedia.org\/wiki\/Weak_supervision\">semi-supervised learning<\/a> techniques. Then, the pre-trained model is adapted for specific tasks using various techniques like fine-tuning and prompt engineering:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/Large-Language-Model.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-179521\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/Large-Language-Model.jpg\" alt=\"\" width=\"573\" height=\"277\" \/><\/a><\/p>\n<p>These LLMs are capable of performing several natural language processing tasks like language translation and content summarization. They&#8217;re also <strong>capable of generative tasks like content creation<\/strong>. Hence, they can be extremely valuable in applications like answering questions.<\/p>\n<p>Almost all <strong>major cloud service providers have included large language models<\/strong> in their service offerings. For instance, <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/azure.microsoft.com\/en-us\/solutions\/ai\">Microsoft Azure<\/a> offers LLMs like Llama 2 and OpenAI GPT-4. <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/aws.amazon.com\/bedrock\/\">Amazon Bedrock<\/a> offers models from AI21 Labs, Anthropic, Cohere, Meta, and Stability AI.<\/p>\n<h3 id=\"bd-2-prompt-engineering\" data-id=\"2-prompt-engineering\">2.2. Prompt Engineering<\/h3>\n<div class=\"bd-anchor\" id=\"2-prompt-engineering\"><\/div>\n<p>LLMs are foundation models trained on a massive set of text data. Hence, they can capture the syntax and semantics inherent to human languages. However, they\u00a0<strong>must be adapted to perform specific tasks<\/strong> that we want them to perform.<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/en.wikipedia.org\/wiki\/Prompt_engineering\">Prompt engineering<\/a> is one of the quickest ways to adapt an LLM. It&#8217;s a <strong>process of structuring text that can be interpreted and understood by an LLM<\/strong>. Here, we use natural language text to describe the task that we expect an LLM to perform:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/Prompt-Engineering.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-179537\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/Prompt-Engineering.jpg\" alt=\"\" width=\"677\" height=\"237\" \/><\/a><\/p>\n<p>The prompts we create help an LLM <strong>perform <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/en.wikipedia.org\/wiki\/Prompt_engineering#In-context_learning\">in-context learning<\/a>, which is temporary<\/strong>. We can use prompt engineering to promote the safe usage of LLMs and build new capabilities like augmenting LLMs with domain knowledge and external tools.<\/p>\n<p>This is an active area of research, and new techniques keep surfacing now and then. However, techniques like <strong><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.promptingguide.ai\/techniques\/cot\">chain-of-thought prompting<\/a> have already become quite popular<\/strong>. The idea here is for the LLMs to solve a problem as a series of intermediate steps before giving a final answer.<\/p>\n<h3 id=\"bd-3-word-embeddings\" data-id=\"3-word-embeddings\">2.3. Word Embeddings<\/h3>\n<div class=\"bd-anchor\" id=\"3-word-embeddings\"><\/div>\n<p>As we&#8217;ve seen, LLMs are capable of processing a large volume of natural language text. The performance of LLMs vastly improves if we represent the words in natural languages as <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/en.wikipedia.org\/wiki\/Word_embedding\">word embeddings<\/a>. This is a <strong>real-valued vector capable of encoding words&#8217; meanings<\/strong>.<\/p>\n<p>Typically, the word embeddings are generated using an algorithm like <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/en.wikipedia.org\/wiki\/Word2vec\">Tom\u00e1\u0161 Mikolov&#8217;s Word2vec<\/a> or <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/nlp.stanford.edu\/projects\/glove\/\">Stanford University&#8217;s GloVe<\/a>. The <strong>GloVe is an unsupervised learning algorithm<\/strong> trained on aggregated global word-word co-occurrence statistics from a corpus:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/Word-Embedding-Illustration.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-179541\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/Word-Embedding-Illustration.jpg\" alt=\"\" width=\"662\" height=\"245\" \/><\/a><\/p>\n<p>In prompt engineering, we convert the prompts into their word embeddings, allowing the model to understand better and respond to the prompt. Further, it&#8217;s also very <strong>helpful in augmenting the context we provide to the model<\/strong> allowing them to provide more contextual answers.<\/p>\n<p>For instance, we can generate word embeddings from an existing dataset and store them in a vector database. Further, we can <strong>use the user-provided input to perform a semantic search<\/strong> into this vector database. Then we can use the search result as additional context to the model.<\/p>\n<h2  id=\"bd-the_llm_stack_with_langchain\" data-id=\"the_llm_stack_with_langchain\">3. The LLM Tech Stack With LangChain<\/h2>\n<div class=\"bd-anchor\" id=\"the_llm_stack_with_langchain\"><\/div>\n<p>As we&#8217;ve seen already, <strong>creating effective prompts is a key element<\/strong> for successfully harnessing the power of LLMs in any application. This includes making the interaction with language models context-aware and being able to rely on a language model to reason.<\/p>\n<p>For this, we&#8217;re required to perform <strong>several tasks, like creating templates for prompts, making calls to language models, and providing user-specific data<\/strong> from multiple sources to language models. To make these tasks simpler, we require a framework like LangChain as part of our LLM tech stack:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LLM-Tech-Stack-with-LangChain.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-179552\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LLM-Tech-Stack-with-LangChain.jpg\" alt=\"\" width=\"647\" height=\"347\" \/><\/a><\/p>\n<p>The framework also helps in developing applications that require chaining multiple language models and <strong>being able to recall information about past interactions<\/strong> with a language model. Then, there are more complex use cases that involve using a language model as a reasoning engine.<\/p>\n<p>Finally, we can <strong>perform logging, monitoring, streaming, and other essential tasks<\/strong> for maintenance and troubleshooting. The LLM tech stack is evolving rapidly to address many of these concerns. However, LangChain is fast becoming a valuable part of LLM tech stacks.<\/p>\n<h2  id=\"bd-langchain_for_java\" data-id=\"langchain_for_java\">4. LangChain for Java<\/h2>\n<div class=\"bd-anchor\" id=\"langchain_for_java\"><\/div>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.langchain.com\/\">LangChain<\/a> was <strong>launched in 2022 as an open-source project<\/strong> and soon gathered momentum through community support. It was originally developed by Harrison Chase in Python and soon turned out to be one of the fastest-growing start-ups in the AI space.<\/p>\n<p>There was <strong>a JavaScript\/TypeScript version of LangChain that followed the Python version<\/strong> in early 2023. It soon became quite popular and started supporting multiple JavaScript environments like Node.js, web browsers, CloudFlare workers, Vercel\/Next.js, Deno, and Supabase Edge functions.<\/p>\n<p>Unfortunately, there is <strong>no official Java version of LangChain<\/strong> that is available for Java\/Spring applications. However, <strong>there is a community version of LangChain for Java called <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/langchain4j\/langchain4j\">LangChain4j<\/a><\/strong>. It works with Java 8 or higher and supports Spring Boot 2 and 3.<\/p>\n<p>The various dependencies of LangChain are <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/mvnrepository.com\/artifact\/dev.langchain4j\/langchain4j\/0.23.0\">available at Maven Central<\/a>. We may <strong>need to add one or more dependencies<\/strong> in our application, depending on the features we use:<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\r\n    &lt;groupId&gt;dev.langchain4j&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;langchain4j&lt;\/artifactId&gt;\r\n    &lt;version&gt;0.23.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>For instance, we&#8217;ll also require the dependencies that <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/mvnrepository.com\/artifact\/dev.langchain4j\/langchain4j-open-ai\/0.23.0\">support integration to OpenAI models<\/a>, provide <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/mvnrepository.com\/artifact\/dev.langchain4j\/langchain4j-embeddings\/0.23.0\">support for embeddings<\/a>, and <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/mvnrepository.com\/artifact\/dev.langchain4j\/langchain4j-embeddings-all-minilm-l6-v2-q\/0.23.0\">a sentence-transformer model like all-MiniLM-L6-v2<\/a> in the following sections of the tutorial.<\/p>\n<p>With similar design goals to that of LangChain, LangChain4j <strong>provides a simple and coherent layer of abstractions<\/strong> along with its numerous implementations. It already supports several language model providers like OpenAI and embedding store providers like Pinecone.<\/p>\n<p>However, since both <strong>LangChain and LangChain4j are evolving quickly<\/strong>, there may be features that are supported in the Python or JS\/TS version that are not yet there in the Java version. Nevertheless, the fundamental concept, general structure, and vocabulary are largely the same.<\/p>\n<h2  id=\"bd-building_blocks_of_langchain\" data-id=\"building_blocks_of_langchain\">5. Building Blocks of LangChain<\/h2>\n<div class=\"bd-anchor\" id=\"building_blocks_of_langchain\"><\/div>\n<p>LangChain offers several value propositions for our applications available as module components. Modular components provide useful abstractions along with a collection of implementations for working with language models. Let&#8217;s discuss some of these modules with examples in Java.<\/p>\n<h3 id=\"bd-1-models-io\" data-id=\"1-models-io\">5.1. Models I\/O<\/h3>\n<div class=\"bd-anchor\" id=\"1-models-io\"><\/div>\n<p>When working with any language model, we need the ability to interface with it. LangChain provides the necessary building blocks like the <strong>ability to templatize prompts and to dynamically select and manage model inputs<\/strong>. Also, we can use output parsers to extract information from model outputs:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LangChain-Model-IO.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-179587\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LangChain-Model-IO.jpg\" alt=\"\" width=\"629\" height=\"244\" \/><\/a><\/p>\n<p>Prompt templates are pre-defined recipes for generating prompts for language models and may include instructions, <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.promptingguide.ai\/techniques\/fewshot\">few-shot examples<\/a>, and specific context:<\/p>\n<pre><code class=\"language-java\">PromptTemplate promptTemplate = PromptTemplate\r\n  .from(&quot;Tell me a {{adjective}} joke about {{content}}..&quot;);\r\nMap&lt;String, Object&gt; variables = new HashMap&lt;&gt;();\r\nvariables.put(&quot;adjective&quot;, &quot;funny&quot;);\r\nvariables.put(&quot;content&quot;, &quot;computers&quot;);\r\nPrompt prompt = promptTemplate.apply(variables);<\/code><\/pre>\n<p>Here, we create a prompt template capable of accepting multiple variables. The variables are something we receive from the user input and feed to the prompt template.<\/p>\n<p>LangChain supports integrating with two types of models, language models and chat models. Chat models are also backed by language models but provide chat capabilities:<\/p>\n<pre><code class=\"language-java\">ChatLanguageModel model = OpenAiChatModel.builder()\r\n  .apiKey(&lt;OPENAI_API_KEY&gt;)\r\n  .modelName(GPT_3_5_TURBO)\r\n  .temperature(0.3)\r\n  .build();\r\nString response = model.generate(prompt.text());<\/code><\/pre>\n<p>Here, we create a chat model with a particular OpenAI model and the associated API key. We can obtain an API key from <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/openai.com\/\">OpenAI<\/a> by registering for free. The parameter temperature is used to control the randomness of the model output.<\/p>\n<p>Finally, the output from the language models may not be structured enough for presentation. LangChain provides <strong>output parsers that help us structure language model responses<\/strong> \u2014 for instance, extracting the information from the output as a POJO in Java.<\/p>\n<h3 id=\"bd-2-memory\" data-id=\"2-memory\">5.2. Memory<\/h3>\n<div class=\"bd-anchor\" id=\"2-memory\"><\/div>\n<p>Typically, an application leveraging a LLM has a conversational interface. An important aspect of any conversation is to be <strong>able to refer to information introduced earlier in the conversation<\/strong>. The ability to store information about past interactions is called memory:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LangChain-Memory.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-179588\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LangChain-Memory.jpg\" alt=\"\" width=\"569\" height=\"287\" \/><\/a><\/p>\n<p>LangChain offers key enablers for adding memory to an application. For instance, we need the ability to read from the memory to augment the user input. Then, we need the ability to write the inputs and outputs of the current run to the memory:<\/p>\n<pre><code class=\"language-java\">ChatMemory chatMemory = TokenWindowChatMemory\r\n  .withMaxTokens(300, new OpenAiTokenizer(GPT_3_5_TURBO));\r\nchatMemory.add(userMessage(&quot;Hello, my name is Kumar&quot;));\r\nAiMessage answer = model.generate(chatMemory.messages()).content();\r\nSystem.out.println(answer.text()); \/\/ Hello Kumar! How can I assist you today?\r\nchatMemory.add(answer);\r\nchatMemory.add(userMessage(&quot;What is my name?&quot;));\r\nAiMessage answerWithName = model.generate(chatMemory.messages()).content();\r\nSystem.out.println(answer.text()); \/\/ Your name is Kumar.\r\nchatMemory.add(answerWithName);<\/code><\/pre>\n<p>Here, we implement a fixed window chat memory with <em>TokenWindowChatMemory<\/em>, which allows us to read and write chat messages that we exchange with the language model.<\/p>\n<p>LangChain also offers <strong>more complex data structures and algorithms to return selected messages<\/strong> from the memory instead of returning everything. For instance, it has support for returning a summary of the past few messages, or only returning messages relevant to the current run.<\/p>\n<h3 id=\"bd-3-retrieval\" data-id=\"3-retrieval\">5.3. Retrieval<\/h3>\n<div class=\"bd-anchor\" id=\"3-retrieval\"><\/div>\n<p>Large language models are generally trained on a vast quantity of text corpus. Hence, they&#8217;re quite efficient in general tasks but may not be so useful in a domain-specific task. For this, we need to <strong>retrieve relevant external data and pass it to the language model<\/strong> during the generation step.<\/p>\n<p>This process is known as the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.promptingguide.ai\/techniques\/rag\">Retrieval Augmented Generation (RAG)<\/a>. It <strong>helps in grounding the models on relevant and accurate information<\/strong> as well as giving us insight into the model&#8217;s generative process. LangChain provides the necessary building blocks to create RAG applications:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LangChain-Retrieval.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-179589\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2023\/10\/LangChain-Retrieval.jpg\" alt=\"\" width=\"651\" height=\"227\" \/><\/a><\/p>\n<p>To begin with, LangChain provides document loaders that are used to retrieve a document from a storage location. Then, there are transformers available to prepare the documents for processing further. For instance, we can have it split a large document into smaller chunks:<\/p>\n<pre><code class=\"language-java\">Document document = FileSystemDocumentLoader.loadDocument(&quot;simpson&#039;s_adventures.txt&quot;);\r\nDocumentSplitter splitter = DocumentSplitters.recursive(100, 0, \r\n  new OpenAiTokenizer(GPT_3_5_TURBO));\r\nList&lt;TextSegment&gt; segments = splitter.split(document);<\/code><\/pre>\n<p>Here, we&#8217;re using the <em>FileSystemDocumentLoader<\/em> to load a document from the file system. Then, we split that document into smaller chunks using <em>OpenAiTokenizer<\/em>.<\/p>\n<p>To make the retrieval more efficient, the <strong>documents are generally converted into their embeddings and stored in vector databases<\/strong>. LangChain supports several embedding providers and methods and integrates with almost all popular vector stores:<\/p>\n<pre><code class=\"language-java\">EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();\r\nList&lt;Embedding&gt; embeddings = embeddingModel.embedAll(segments).content();\r\nEmbeddingStore&lt;TextSegment&gt; embeddingStore = new InMemoryEmbeddingStore&lt;&gt;();\r\nembeddingStore.addAll(embeddings, segments);<\/code><\/pre>\n<p>Here, we&#8217;re using the <em>AllMiniLmL6V2EmbeddingModel<\/em> to create the embeddings of document segments. Then, we store the embeddings in an in-memory vector store.<\/p>\n<p>Now that we have our external data present in a vector store as embeddings, we&#8217;re ready to retrieve from it. LangChain supports several retrieval algorithms like simple semantic search and complex ones like ensemble retriever:<\/p>\n<pre><code class=\"language-java\">String question = &quot;Who is Simpson?&quot;;\r\n\/\/The assumption here is that the answer to this question is contained in the document we processed earlier.\r\nEmbedding questionEmbedding = embeddingModel.embed(question).content();\r\nint maxResults = 3;\r\ndouble minScore = 0.7;\r\nList&lt;EmbeddingMatch&lt;TextSegment&gt;&gt; relevantEmbeddings = embeddingStore\r\n  .findRelevant(questionEmbedding, maxResults, minScore);<\/code><\/pre>\n<p>We create the embedding of the user question and then use the question embedding to retrieve relevant matches from the vector store. Now, we can send the relevant matches retrieved as context by adding them to the prompt that we intend to send to the model.<\/p>\n<h2  id=\"bd-complex_applications_of_langchain\" data-id=\"complex_applications_of_langchain\">6. Complex Applications of LangChain<\/h2>\n<div class=\"bd-anchor\" id=\"complex_applications_of_langchain\"><\/div>\n<p>So far, we&#8217;ve seen how to use individual components to create an application with a language model. LangChain also offers components to build more complex applications. For instance, we can use chains and agents to build more adaptive applications with enhanced capabilities.<\/p>\n<h3 id=\"bd-1-chains\" data-id=\"1-chains\">6.1. Chains<\/h3>\n<div class=\"bd-anchor\" id=\"1-chains\"><\/div>\n<p>Generally, an application will require <strong>multiple components to be called in a specific sequence<\/strong>. This is what&#8217;s referred to as a chain in LangChain. It simplifies developing more complex applications and makes it easier to debug, maintain, and improve.<\/p>\n<p>This is also useful for <strong>combining multiple chains to form more complex applications<\/strong> that potentially require an interface with more than one language model. LangChain offers convenient ways to create such chains and provides many pre-built chains:<\/p>\n<pre><code class=\"language-java\">ConversationalRetrievalChain chain = ConversationalRetrievalChain.builder()\r\n  .chatLanguageModel(chatModel)\r\n  .retriever(EmbeddingStoreRetriever.from(embeddingStore, embeddingModel))\r\n  .chatMemory(MessageWindowChatMemory.withMaxMessages(10))\r\n  .promptTemplate(PromptTemplate\r\n    .from(&quot;Answer the following question to the best of your ability: {{question}}\\n\\nBase your answer on the following information:\\n{{information}}&quot;))\r\n  .build();<\/code><\/pre>\n<p>Here, we&#8217;re using a pre-built chain <em>ConversationalRetreivalChain<\/em> that allows us to use a chat model together with a retriever along with memory and prompt template. Now, we can simply use the chain to execute user queries:<\/p>\n<pre><code class=\"language-java\">String answer = chain.execute(&quot;Who is Simpson?&quot;);<\/code><\/pre>\n<p>The chain comes with a default memory and prompt template that we can override. It&#8217;s also quite easy to create our custom chains. The ability to create chains makes it <strong>easier to achieve modular implementation of complex applications<\/strong>.<\/p>\n<h3 id=\"bd-2-agents\" data-id=\"2-agents\">6.2. Agents<\/h3>\n<div class=\"bd-anchor\" id=\"2-agents\"><\/div>\n<p>LangChain also offers more powerful constructs like agents. Unlike chains, <strong>agents use a language model as a reasoning engine<\/strong> to determine which actions to take and in which order. We can also provide agents access to the right tools to perform necessary actions.<\/p>\n<p>In LangChain4j, <strong>agents are available as AI Services<\/strong> to declaratively define complex AI behavior. Let&#8217;s see if we can provide a calculator as a tool for an AI Service and enable a language model to perform calculations.<\/p>\n<p>First, we&#8217;ll define a class with some basic calculator functions and describe each function in natural language for the model to understand:<\/p>\n<pre><code class=\"language-java\">public class AIServiceWithCalculator {\r\n    static class Calculator {\r\n        @Tool(&quot;Calculates the length of a string&quot;)\r\n        int stringLength(String s) {\r\n            return s.length();\r\n        }\r\n        @Tool(&quot;Calculates the sum of two numbers&quot;)\r\n        int add(int a, int b) {\r\n            return a + b;\r\n        }\r\n    }<\/code><\/pre>\n<p>Then, we&#8217;ll define the interface for our AI Service to build from. It&#8217;s quite simple here, but it can also describe more complex behaviors:<\/p>\n<pre><code class=\"language-java\">interface Assistant {\r\n    String chat(String userMessage);\r\n}<\/code><\/pre>\n<p>Now, we&#8217;ll build an AI Service from the builder factory provided by LangChain4j using the interface we just defined and the tool we created:<\/p>\n<pre><code class=\"language-java\">Assistant assistant = AiServices.builder(Assistant.class)\r\n  .chatLanguageModel(OpenAiChatModel.withApiKey(&lt;OPENAI_API_KEY&gt;))\r\n  .tools(new Calculator())\r\n  .chatMemory(MessageWindowChatMemory.withMaxMessages(10))\r\n  .build();<\/code><\/pre>\n<p>That&#8217;s it! We can now start sending questions\u00a0that contain some calculations to be performed to our language model:<\/p>\n<pre><code class=\"language-java\">String question = &quot;What is the sum of the numbers of letters in the words \\&quot;language\\&quot; and \\&quot;model\\&quot;?&quot;;\r\nString answer = assistant.chat(question);\r\nSystem.out.prtintln(answer); \/\/ The sum of the numbers of letters in the words &quot;language&quot; and &quot;model&quot; is 13. <\/code><\/pre>\n<p>When we run this code, we&#8217;ll observe that the language model is now capable of performing calculations.<\/p>\n<p>It&#8217;s important to note that <strong>language models have difficulty performing some tasks<\/strong> that require them to have the notion of time and space or perform complex arithmetic procedures. However, we can always supplement the model with the necessary tools to solve this problem.<\/p>\n<h2  id=\"bd-conclusion\" data-id=\"conclusion\">7. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion\"><\/div>\n<p>In this tutorial, we went through some of the basic elements of creating an application powered by large language models. Further, we discussed the value of including a framework like LangChain as part of the tech stack for developing such an application.<\/p>\n<p>This allowed us to explore some of the core elements of LangChain4j, a Java version of LangChain. These libraries will evolve rapidly in the days to come. But, they&#8217;re already making the process of developing applications powered by language models mature and fun!<\/p>\n<p>Don\u2019t forget to check out the full source code of this article <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/libraries-llms\">over on GitHub<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/803945138\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/803945138\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2021%2F08%2FBaeldung-Featured-Image-1024x536.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-langchain-basics#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-langchain-basics\/feed\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<h2><b>Commercials Cooperation Advertisements:<\/b><\/h2>\r\n<p><br>(1) IT Teacher IT Freelance<br> <\/p>\r\n<a href=https:\/\/itteacheritfreelance.hk\/wordpress><img src=http:\/\/gamefootballmobileanimeiphone.com\/wp-content\/uploads\/2023\/09\/ITTeacherITFreelance-Website.png alt=IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\/><\/a><p><a href=https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/findteacher>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427! <\/a><br>\r\n\r\n\u7535\u5b50\u8ba1\u7b97\u673a -\u6559\u80b2 -IT \u96fb\u8166\u73ed\u201d ( IT\u96fb\u8166\u88dc\u7fd2 ) \u63d0\u4f9b\u4e00\u500b\u65b9\u4fbf\u7684\u7535\u5b50\u8ba1\u7b97\u673a \u6559\u80b2\u5e73\u53f0, \u70ba\u5927\u5bb6\u914d\u5c0d\u4fe1\u606f\u6280\u672f, \u96fb\u8166 \u8001\u5e2b, IT freelance \u548c programming expert. \u8b93\u5927\u5bb6\u65b9\u4fbf\u5730\u5c31\u80fd\u627e\u5230\u5408\u9069\u7684\u96fb\u8166\u88dc\u7fd2, \u96fb\u8166\u73ed, \u5bb6\u6559, \u79c1\u4eba\u8001\u5e2b.  <br>\r\n\r\nWe are a education and information platform which you can find a IT private tutorial teacher or freelance. <br>\r\n\r\nAlso we provide different information about information technology, Computer, programming, mobile, Android, apple, game, movie, anime, animation\u2026 \r\n<\/p>\n<p><br>(2) ITSec<br> <\/p><a href=https:\/\/itsec.vip><img src=http:\/\/gamefootballmobileanimeiphone.com\/wp-content\/uploads\/2023\/09\/ITSec-Main-Promotion-Image.png alt= https:\/\/itsec.vip\/\r\nSecure Your Computers from Cyber Threats and mitigate risks with professional services to defend Hackers.  \r\nITSec provide IT Security and Compliance Services, including IT Compliance Services, Risk Assessment, IT Audit, Security Assessment and Audit, ISO 27001 Consulting and Certification, GDPR Compliance Services, Privacy Impact Assessment (PIA), Penetration test, Ethical Hacking, Vulnerabilities scan, IT Consulting, Data Privacy Consulting, Data Protection Services, Information Security Consulting, Cyber Security Consulting, Network Security Audit, Security Awareness Training.\/><\/a> \r\n<br><br> \r\n<p><a href=https:\/\/itsec.vip>www.ITSec.vip<\/a> <br> <br> \r\n<p><a href=https:\/\/sraa.com.hk>www.Sraa.com.hk<\/a> <br> <br> \r\n<p><a href=https:\/\/itsec.hk>www.ITSec.hk<\/a> <br> <br> \r\n<p><a href=https:\/\/penetrationtest.hk>www.Penetrationtest.hk<\/a> <br> <br> \r\n<p><a href=https:\/\/itseceu.uk>www.ITSeceu.uk<\/a> <br> <br> \r\nSecure Your Computers from Cyber Threats and mitigate risks with professional services to defend Hackers. <br><br>\r\nITSec provide IT Security and Compliance Services, including IT Compliance Services, Risk Assessment, IT Audit, Security Assessment and Audit, ISO 27001 Consulting and Certification, GDPR Compliance Services, Privacy Impact Assessment (PIA), Penetration test, Ethical Hacking, Vulnerabilities scan, IT Consulting, Data Privacy Consulting, Data Protection Services, Information Security Consulting, Cyber Security Consulting, Network Security Audit, Security Awareness Training. \r\n<br><br>Contact us right away. <br><br>Email (Prefer using email to contact us): <br>SalesExecutive@ITSec.vip<\/p>","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/08\/Baeldung-Featured-Image-1024x536.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"\" loading=\"lazy\"><\/p>\n<p>Learn how to work with large language models in Java with LangChain.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>\u00a0<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/803945138\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2021%2F08%2FBaeldung-Featured-Image-1024x536.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>\u00a0<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>\u00a0<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>\u00a0<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/803945138\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>\u00a0<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-langchain-basics#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>\u00a0<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-langchain-basics\/feed\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>\u00a0<\/div>\n","protected":false},"author":262,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[22],"tags":[61,122,127,129,124,128,125,132,131,133,126,130,123,66,94,88,97,56,64,65,60,112,40,75,95,104,33,120,105,101,98,115,30,29,41,86,70,69,68,72,71,26,118,108,87,46,55,48,52,54,51,50,83,62,58,57,109,35,59,63,85,79,82,96,80,27,81,114,44,42,43,45,38,39,110,117,100,111,116,73,89,90,92,91,93,84,78,37,102,34,36,77,67,74,99,113,119,28,121,32,47,49,53,103,31,76],"class_list":["post-37396","post","type-post","status-publish","format-standard","hentry","category-mobile","tag-airpods","tag-anime","tag-anime-characters","tag-anime-cosplay","tag-anime-edits","tag-anime-merchandise","tag-anime-movies","tag-anime-news","tag-anime-recommendations","tag-anime-reviews","tag-anime-series","tag-anime-streaming","tag-animes","tag-app-store","tag-app-store-samsung","tag-appgallery","tag-appgallery-oneplus","tag-apple","tag-apple-music","tag-apple-tv","tag-apple-watch","tag-bbc-sport","tag-best-mobile-games","tag-bixby","tag-bixby-xiaomi","tag-champions-league","tag-cyberpunk","tag-cyberpunk-2077","tag-fantasy-football","tag-fifa","tag-football","tag-formula-1","tag-fortnite","tag-free-fire","tag-free-mobile-games","tag-freebuds-pro","tag-galaxy-a52","tag-galaxy-note-20","tag-galaxy-s21","tag-galaxy-watch-4","tag-galaxy-z-fold-3","tag-game","tag-games","tag-golf","tag-harmonyos","tag-how-to-backup-iphone","tag-how-to-factory-reset-iphone","tag-how-to-reset-iphone","tag-how-to-restore-iphone","tag-how-to-unlock-iphone","tag-how-to-unlock-iphone-5","tag-how-to-unlock-iphone-6","tag-huawei","tag-ios","tag-ipad","tag-iphone","tag-live-soccer","tag-lol","tag-macbook","tag-macos","tag-mate-40-pro","tag-mi-11-lite","tag-mi-home-security-camera-basic-1080p","tag-mi-home-security-camera-basic-1080p-huawei","tag-mi-smart-band-6","tag-minecraft","tag-miui","tag-mlb-scores","tag-mobile-game-design","tag-mobile-game-development","tag-mobile-game-marketing","tag-mobile-game-monetization","tag-mobile-games","tag-mobile-gaming","tag-nba-scores","tag-nba-standings","tag-nfl","tag-nfl-scores","tag-nhl-scores","tag-one-ui","tag-oneplus","tag-oneplus-9-pro","tag-oneplus-buds-pro","tag-oneplus-nord-ce-5g","tag-oxygenos","tag-p40-pro-plus","tag-poco-x3-pro","tag-pokemon","tag-premier-league","tag-pubg","tag-pubg-mobile","tag-redmi-note-10-pro","tag-samsung","tag-samsung-pay","tag-soccer","tag-sports","tag-steam","tag-steeam","tag-top-10-anime","tag-valorant","tag-when-do-the-iphone-7-come-out","tag-when-does-the-iphone-7-come-out","tag-when-is-the-iphone-7-coming-out","tag-world-cup","tag-xbox-series-x","tag-xiaomi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts\/37396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/users\/262"}],"replies":[{"embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/comments?post=37396"}],"version-history":[{"count":1,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts\/37396\/revisions"}],"predecessor-version":[{"id":37400,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts\/37396\/revisions\/37400"}],"wp:attachment":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/media?parent=37396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/categories?post=37396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/tags?post=37396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}