<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tobin Tom on Medium]]></title>
        <description><![CDATA[Stories by Tobin Tom on Medium]]></description>
        <link>https://medium.com/@tobintom?source=rss-8b06c0539df2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Tobin Tom on Medium</title>
            <link>https://medium.com/@tobintom?source=rss-8b06c0539df2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 08:43:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tobintom/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a multi-agent chatbot with Google ADK]]></title>
            <link>https://medium.com/@tobintom/building-a-multi-agent-chatbot-with-google-adk-dde9f5babb16?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/dde9f5babb16</guid>
            <category><![CDATA[google-adk]]></category>
            <category><![CDATA[vertex-ai]]></category>
            <category><![CDATA[agentic-rag]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[google-vertex-ai]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Sat, 20 Sep 2025 06:49:24 GMT</pubDate>
            <atom:updated>2025-09-20T06:49:24.063Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-EyccfIyyvstrjlh3qSjuw.png" /></figure><p>Google Agent Development Kit (<a href="https://google.github.io/adk-docs/">https://google.github.io/adk-docs/</a>) is a framework for developing and deploying AI Agents into the Google ecosystem. Applications built on ADK can be deployed in multiple ways, though the preferred route is into Agent Engine in Google Vertex AI. The application can also be containerized and run from Cloud Run or deployed to any container orchestration platform, e.g. GKE. While ADK is optimized for Gemini, it is really LLM model agnostic and can be used with other frameworks.</p><p>The ADK framework has several features built-in to help develop functionalities faster.</p><ol><li>Has multiple types of Agents to suit different use cases. <a href="https://google.github.io/adk-docs/agents/">https://google.github.io/adk-docs/agents/</a> Normal LLMAgents, Workflow Agents, Multi-agent flows to create parent and sub-agent flows.</li><li>Has good support of Tools (<a href="https://google.github.io/adk-docs/tools/">https://google.github.io/adk-docs/tools/</a>) and also has a number of built-in tools. Allows for easy integration with third party libraries.</li><li>It is easy to deploy, can be quickly deployed to Vertex AI Engine or can be containerized and deployed to a container framework.</li><li>Has built-in tools for evaluation <a href="https://google.github.io/adk-docs/evaluate/">https://google.github.io/adk-docs/evaluate/</a>.</li><li>Has support to allow creating guardrails and safety <a href="https://google.github.io/adk-docs/safety/">https://google.github.io/adk-docs/safety/</a></li></ol><h3>Sample Application</h3><p>In this article we will build a multi-agent application that models on a parent sub-agent framework to handle multiple tasks. This is built in a GCP project with Vertex AI enabled. We will also create a UI for the multi-agent application using Streamlit.</p><h4>Project Structure</h4><pre>main-application/<br>|___agents/<br>|     |___agent_one/<br>|     |___agent_two/<br>|     |___agent_three/<br>|<br>|____main_agent/<br>|<br>|____app_ui/<br>|<br>|____config/<br></pre><p>In this sample application there are multiple sub-agents that will be coordinated with a parent agent (<a href="https://google.github.io/adk-docs/agents/multi-agents/#agent-hierarchy-parent-agent-sub-agents">https://google.github.io/adk-docs/agents/multi-agents/#agent-hierarchy-parent-agent-sub-agents</a>).</p><p><strong>Main agent:</strong></p><p>Some of the business context has been generalized to provide an idea of how it can be used.</p><pre>from google.adk.agents import Agent<br>from google.adk.models.lite_llm import LiteLlm<br><br>from agents.agent_one  import agent_one<br>from agents.agent_two import agent_two<br>from agents.agent_three import agent_three<br>from agents.agent_four import agent_four<br>from agents.agent_five import agent_five<br><br> <br>root_agent = Agent(<br>        name = &quot;Coordinator&quot;,<br>        model= &quot;gemini-2.0-flash-001&quot;,<br>        description= &quot;Classifies the user&#39;s query and routes to specialized sub agents&quot;,<br>        instruction= &quot;&quot;&quot;<br>        **Your Role:** You are a classifier agent that analyzes the user&#39;s query and transfers to the appropriate agent.<br>        **Query Routing Flow (Follow below steps to route query properly):<br>        1.   **Analyze user&#39;s query to determine Intent: ** Use the below guidance to determine user&#39;s intent.<br>                **Intent one: Provide a detailed description that would allow the agent to decide what is intent one<br>                **Intent two: Provide a detailed description that would allow the agent to decide what is intent two                               <br>                **Intent three: Provide a detailed description that would allow the agent to decide what is intent three                               <br>                **Intent four: Provide a detailed description that would allow the agent to decide what is intent four                                 <br>                **Intent five: If none of the other itents are satisfied, this is the fallback intent five.<br><br>        2. **FORWARD query to sub-agent based on Intent:<br>             **Intent one: forward to agent_one<br>             **Intent two: forward to agent_two<br>             **Intent three: forward to agent_three<br>             **Intent four: forward to agent_four<br>             **Intent five: forward to agent_five<br>         <br>        3. **After getting the response from the sub-agent, return it to the user without modifying the response<br><br>        &quot;&quot;&quot;,<br>        sub_agents= [agent_one,agent_two,agent_three,agent_four,agent_five]<br><br>)</pre><p>Now that we have the parent agent defined, we can define each sub-agent and add relevant tools or RAG to help it do it’s job. The agent instructions provide the llm the directions to decide which sub-agent to invoke depending on the query.</p><p><strong>agent_one</strong></p><pre>from google.adk.agents import Agent<br>from google.adk.models.lite_llm import LiteLlm<br>import requests<br>import json<br><br><br>def fetch_some_details(id: str)-&gt; str:<br>    &quot;&quot;&quot;<br>    Retrieves the details of a business entity using an ID<br><br>    Parameters:<br>    - id (str): Id of business asset being queried<br><br>    Returns:<br>    - JSON service payload with business asset details<br><br>    Use this tool when the user asks about a business asset and provides an id<br>    &quot;&quot;&quot;<br><br>    url = f&quot;https://services.some.path.com?Id={id}&quot;<br>    try:<br>        r = requests.get(url,timeout=20)<br>        r.raise_for_status()<br>        data = r.json()<br>        #<br>        # BELOW IS JUST A SAMPLE OF A SERVICE CALL RETURNING SOMETHING THE LLM<br>        # CAN USE<br>        #<br>        items = data.get(&quot;data&quot;,[])<br>        if not items:<br>            return json.dumps({&quot;error&quot;:f&quot;No details found for {id}&quot;})<br>        out = []<br>        for entry in items:<br>            obj = entry.get(&quot;data&quot;,{})<br>            descs = entry.get(&quot;descriptions&quot;,[])<br>            desc = None<br>            if descs:<br>                en = [d[&#39;value&#39;] for d in descs if d.get(&#39;lang&#39; ,&#39;en&#39;)==&#39;en&#39;]<br>                desc = en[0] if en else descs[0][&#39;value&#39;]<br>             <br>            out.append({<br>                &quot;id&quot;: obj.get(&#39;id&#39;),<br>                &quot;description&quot; : desc <br>            })<br>        return json.dumps({&quot;details&quot;:items}, ensure_ascii=False)<br>    except Exception as e:<br>        return json.dumps({&quot;error&quot;:str(e)})<br><br><br>agent_one = Agent(<br>    name = &quot;agent_one&quot;,<br>    model = LiteLlm(&quot;gemini-2.0-flash-lite&quot;),<br>    description=&quot;Finds business asset information&quot;,<br>    instruction= &quot;&quot;&quot;<br>    Give full instructions on what this agent is supposed to do and under what circumstances the <br>    tool needs to be called.<br>    &quot;&quot;&quot;,<br>    tools=[fetch_some_details]    <br>)</pre><p>This sub-agent is defined to make an API call and that is registered as a tool. When defining the agent, instructions are given to allow the llm to decide when to invoke the tool. The API tool returns a json payload, which the llm uses to construct a meaningful reply using it’s instructions.</p><p><strong>agent_two</strong></p><pre>from google.adk.agents import Agent<br>from google.adk.tools.retrieval.vertex_ai_rag_retrieval import VertexAiRagRetrieval<br>from vertexai.preview import rag<br>from vertexai.generative_models import GenerativeModel, Tool<br>from google.cloud.aiplatform_v1beta1.types import EncryptionSpec<br>import vertexai<br>import requests<br>import json<br><br>from .rag import FAQRag<br><br><br>rag_store = FAQRag(collection_name=&quot;csv_collection&quot;,chroma_dir=&quot;chroma_dir&quot;)<br><br>try:<br>    test = rag_store.collection.peek()<br>    is_empty = not test or len(test.get(&quot;ids&quot;,[])) == 0<br>except Exception:<br>    res = rag_store.collection.query(query_texts=[&quot;XX&quot;], n_results=1)<br>    is_empty = (len(res.get(&quot;documents&quot;,[[]])[0]) == 0)<br><br>if is_empty:<br>    print(&quot;Ingesting csv into chroma db&quot;)<br>    rag_store.read_csv(&quot;./agents/agent_two/channel_questions.csv&quot;)<br><br>def rag_tool(query:str, top_k: int = 3):<br>    &quot;&quot;&quot;<br>    Retrieves answers to questions about XX<br><br>    Parameters:<br>    - query (str): The user query asking questions about XX<br><br>    Returns:<br>    - answer to the user&#39;s query from the XX RAG<br><br>    Use this tool when the user asks questions about XX, how its used or any query regarding XX<br>    &quot;&quot;&quot;<br>    return rag_store.rag_query(query=query, top_k=top_k)<br><br>def fetch_project_details(project_name: str)-&gt; str:<br>    &quot;&quot;&quot;<br>    Retrieves the vulnerability details about a project that is scanned by XX<br><br>    Parameters:<br>    - project_name (str): The Github repository project scanned by XX<br><br>    Returns:<br>    - JSON service payload with XX scan results<br><br>    Use this tool when the user provides a project name asks about scan results about the project or vulnerabilties about the project<br>    &quot;&quot;&quot;<br><br>    url = f&quot;https://api.xx.io/rest/orgs?version=2024-10-15&amp;name={project_name}&quot;<br>    try:<br>        headers = {<br>            &quot;Authorization&quot;: &quot;XXXXX&quot;,<br>            &quot;Content-Type&quot;: &quot;application/json&quot;<br>        }<br>        r = requests.get(url,timeout=20,headers=headers)<br>        r.raise_for_status()<br>        data = r.json()<br>        items = data.get(&quot;data&quot;,[])<br>        if not items:<br>            return json.dumps({&quot;error&quot;:f&quot;No details found for project {project_name}&quot;})<br>        project_id = items[0].get(&quot;id&quot;)<br>        if project_id:<br>            proj_url = f&quot;https://api.xx.io/rest/orgs/{project_id}/issues?version=2024-10-15&amp;status=open&quot;<br>            p_headers = {<br>                &quot;Authorization&quot;: &quot;XXXXX&quot;,<br>                &quot;Content-Type&quot;: &quot;application/json&quot;<br>            }<br>            r = requests.get(proj_url,timeout=20,headers=p_headers)<br>            r.raise_for_status()<br>            data = r.json()<br>            details = data.get(&quot;data&quot;,[])<br>            return json.dumps({&quot;project_details&quot;:details}, ensure_ascii=False)<br><br>        else:<br>            return json.dumps({&quot;error&quot;:f&quot;No details found for project {project_name}&quot;})      <br>         <br>    except Exception as e:<br>        return json.dumps({&quot;error&quot;:str(e)})<br><br><br>agent_two = Agent(<br>    name=&quot;agent_two&quot;,<br>    model=&quot;gemini-2.0-flash-001&quot;,<br>    description=&quot;Answers questions about xx&quot;,<br>    instruction=&quot;&quot;&quot;<br>    If the query is not about XX route back to the coordinator to find the appropriate agent.<br>    You are a security agent specializing in answering questions regarding XX or getting project details or vulnerability details of a project.<br>    - When the user asks a question about XX, call the provided rag_tool to fetch relevant csv based context. Use that context to craft a helpful, concise answer. If the context does not contain an answer, say <br>    you could not find specific information.<br>    - When the user asks a question about a project and provides a project name, use the provided fetch_project_details to get details about the project and use the response to create a helpful concise answer. If<br>    no project details were found from the fetch_project_details tool, respond saying no project details were found. Ensure the response is detailed with a list of vulnerabilities with a section for each severity, ensure all vulnerability<br>    details are mentioned including the CVE ID when available. Provide a well structured reply with good detail that is easy to read with proper sections.<br>    &quot;&quot;&quot;,<br>    tools=[rag_tool,fetch_project_details]<br>    )</pre><p>For agent_two, there are two tools defined. One does API calls to get more business information and the other performs RAG. In this case, the business has content stored in a csv, that can be vectorized, chunked and used as an FAQ source. Typically, you can use the Vertex AI RAG engine, but in this example we are creating a chroma db vector store to illustrate how easy it is to integrate with third party tools.</p><p><strong>raqrag</strong></p><pre>import os<br>from typing import List, Dict, Any, Optional<br>import pandas as pd<br>from sentence_transformers import SentenceTransformer<br>import chromadb<br>from chromadb.config import Settings<br>from chromadb.utils import embedding_functions<br><br>CHROMA_DIR = os.path.join(os.path.dirname(__file__),&quot;..&quot;,&quot;chroma_db&quot;)<br><br>class FAQRag:<br>    def __init__(self,<br>                 collection_name: str = &quot;csv_collection&quot;,<br>                 chroma_dir:Optional[str] = CHROMA_DIR,<br>                 embedding_model: str = &quot;all-MiniLM-L6-v2&quot;,<br>                 persist:bool = True) -&gt; None:<br>        <br>        self.model = SentenceTransformer(embedding_model)<br>        self.embedding_dim = self.model.get_sentence_embedding_dimension()<br><br>        settings = Settings(<br>            chroma_db_impl = &quot;duckdb+parquet&quot;,<br>            persist_directory = os.path.abspath(chroma_dir) if persist else None<br>        )<br>        self.client = chromadb.PersistentClient(path=os.path.abspath(chroma_dir))<br><br>        def sbert_embed_fn(texts: List[str]) -&gt; List[List[float]]:<br>            return [emb.toList() for emb in self.model.encode(texts, show_progress_bar=False)]<br>        <br>        # self.embed_fn = embedding_functions.EmbeddingFunction(<br>        #     sbert_embed_fn,<br>        #     embedding_dimensions=self.embedding_dim<br>        # )<br><br>        self.collection = self.client.get_or_create_collection(<br>            name=collection_name,<br>           # embedding_function=self.embed_fn<br>        )<br><br>    def read_csv(self, csv_path: str, text_columns: Optional[List[str]] = None):<br>        print(os.getcwd())<br>        df = pd.read_csv(csv_path, dtype=str).fillna(&quot;&quot;)<br><br>        if text_columns is None:<br>            text_columns = [c for c in df.columns]<br>        docs = []<br>        metadatas = []<br>        ids = []<br>        <br>        for _, row in df.iterrows():<br>            doc_text = &quot; | &quot;.join([f&quot;{col}: {row[col]}&quot; for col in text_columns])<br>            docs.append(doc_text)<br>            row_meta = {col: row[col] for col in df.columns}            <br>            metadatas.append(row_meta)<br><br>        ids = [str(i) for i in range(len(docs))]<br>        self.collection.add(<br>            ids = ids,<br>            documents=docs,<br>            metadatas=metadatas<br>        )<br><br>    def rag_query(self, query: str, top_k: int = 3) -&gt; Dict[str, Any]:<br><br>        results = self.collection.query(query_texts=[query], n_results=top_k)<br><br>        hits = []         <br><br>        docs = results.get(&quot;documents&quot;, [[]])[0]<br>        ids = results.get(&quot;ids&quot;, [[]])[0]<br>        metadatas = results.get(&quot;metadatas&quot;,[[]])[0]<br>        distances = results.get(&quot;distances&quot;,[[]])[0]<br><br>        for i in range(len(docs)):<br>            hits.append({<br>                &quot;id&quot;:ids[i],<br>                &quot;document&quot; : docs[i],<br>                &quot;metadata&quot; : metadatas[i],<br>                &quot;distance&quot; : float(distances[i]) if i &lt; len(distances) else None<br>            })<br>        <br>        context_parts = []<br>        for h in hits:<br>            snippet = h[&quot;document&quot;]<br>            context_parts.append(f&quot;[id={h[&#39;id&#39;]}] {snippet}&quot;)<br>        <br>        context = &quot;\n\n&quot;.join(context_parts) if context_parts else &quot;No context found&quot;<br><br>        return {<br>            &quot;query&quot; : query,<br>            &quot;top_k&quot; : top_k,<br>            &quot;context&quot; : context,<br>            &quot;hits&quot; : hits<br>        }</pre><p>This rag class reads the contents from the csv and embeds it into chromadb, using a SentenceTransformer from HuggingFace.</p><p><strong>agent_three</strong></p><pre>from google.adk.agents import Agent<br>from google.adk.tools.retrieval.vertex_ai_rag_retrieval import VertexAiRagRetrieval<br>from vertexai.preview import rag<br>from vertexai.generative_models import GenerativeModel, Tool<br>from google.cloud.aiplatform_v1beta1.types import EncryptionSpec<br>import vertexai<br><br>from .rag import FAQRag<br><br> <br><br>rag_store = FAQRag(collection_name=&quot;xx_collection&quot;,chroma_dir=&quot;chroma_dir&quot;)<br><br>try:<br>    test = rag_store.collection.peek()<br>    is_empty = not test or len(test.get(&quot;ids&quot;,[])) == 0<br>except Exception:<br>    res = rag_store.collection.query(query_texts=[&quot;xxx&quot;], n_results=1)<br>    is_empty = (len(res.get(&quot;documents&quot;,[[]])[0]) == 0)<br><br>if is_empty:<br>    print(&quot;Ingesting xxx into chroma db&quot;)<br>    rag_store.embed_web_pages()<br><br>def rag_tool(query:str, top_k: int = 3):<br>    docs, sources = rag_store.rag_query(query=query, top_k=top_k)<br>    combined = &quot; | &quot;.join(docs)<br>    refs = &quot; , &quot;.join(set(sources))<br><br>    return f&quot;Answer candidates: {combined}\n (Sources: {refs})&quot;<br><br> <br><br>agent_three = Agent(<br>    name=&quot;agent_three&quot;,<br>    model=&quot;gemini-2.0-flash-001&quot;,<br>    description=&quot;Answers questions about XXX from web pages&quot;,<br>    instruction=&quot;&quot;&quot;<br>    If the query is not about XX, third party libraries or container images, route back to the coordinator to find the appropriate agent.<br>    You are a security agent specializing in answering questions regarding XX which is the artifactory used, access to XX, best practices with XX and third party library and container scanning and uploading to XX. When the user asks a question, call the provided rag_tool<br>    to fetch relevant XX based context. Use that context to craft a helpful, concise answer. Also include the url of the source page as reference for the user to check for more information.<br>    &quot;&quot;&quot;,<br>    tools=[rag_tool]<br>    )</pre><p>This agent answers questions from a RAG source that contains the webscrapped data from about4 webpages.</p><p><strong>faqrag</strong></p><pre>import os<br>from typing import List, Dict, Any, Optional<br>import pandas as pd<br>from sentence_transformers import SentenceTransformer<br>import chromadb<br>from chromadb.config import Settings<br>import requests<br>from bs4 import BeautifulSoup<br>import tiktoken<br>from chromadb.utils import embedding_functions<br><br>CHROMA_DIR = os.path.join(os.path.dirname(__file__),&quot;..&quot;,&quot;chroma_db&quot;)<br><br>class FAQRag:<br>    def __init__(self,<br>                 collection_name: str = &quot;xxx_collection&quot;,<br>                 chroma_dir:Optional[str] = CHROMA_DIR,<br>                 embedding_model: str = &quot;all-MiniLM-L6-v2&quot;,<br>                 persist:bool = True) -&gt; None:<br>        <br>        self.model = SentenceTransformer(embedding_model)<br>        self.embedding_dim = self.model.get_sentence_embedding_dimension()<br><br>        settings = Settings(<br>            chroma_db_impl = &quot;duckdb+parquet&quot;,<br>            persist_directory = os.path.abspath(chroma_dir) if persist else None<br>        )<br>        self.client = chromadb.PersistentClient(path=os.path.abspath(chroma_dir))<br><br>        def sbert_embed_fn(texts: List[str]) -&gt; List[List[float]]:<br>            return [emb.toList() for emb in self.model.encode(texts, show_progress_bar=False)]<br>        <br>        # self.embed_fn = embedding_functions.EmbeddingFunction(<br>        #     sbert_embed_fn,<br>        #     embedding_dimensions=self.embedding_dim<br>        # )<br><br>        self.collection = self.client.get_or_create_collection(<br>            name=collection_name,<br>           # embedding_function=self.embed_fn<br>        )<br><br>    def embed_text(self, txt:str):<br>        embedding = self.model.encode(txt).tolist()<br>        return embedding<br>    <br>    def scrape_page(self, url: str) -&gt; str:<br>        response = requests.get(url, timeout=10)<br>        soup = BeautifulSoup(response.text, &quot;html.parser&quot;)<br>        for script in soup([&quot;script&quot;,&quot;style&quot;]):<br>            script.decompose()<br>        return soup.get_text(separator=&quot; &quot;, strip=True)<br>    <br>    def chun_text(self, txt:str, max_tokens: int = 300) -&gt; list[str]:<br>        enc = tiktoken.get_encoding(&quot;cl100k_base&quot;)<br>        tokens = enc.encode(txt)<br>        chunks = []<br>        for i in range(0, len(tokens), max_tokens):<br>            chunk_tokens = tokens[i:i+max_tokens]<br>            chunk_text = enc.decode(chunk_tokens)<br>            chunks.append(chunk_text)<br>        return chunks<br>    <br>    def embed_web_pages(self):<br>        pages = [&quot;https://sit1&quot;,<br>                 #|<br>                 #| Multiple sites<br>                 #|<br>                 &quot;https://site2&quot;]<br>        for page in pages:<br>            text = self.scrape_page(page)<br>            chunks = self.chun_text(text)<br>            for i, chunk in enumerate(chunks):<br>                embedding = self.embed_text(chunk)<br>                self.collection.add(<br>                    ids=[f&quot;{page}-{i}&quot;],<br>                    embeddings=[embedding],<br>                    documents=[chunk],<br>                    metadatas=[{&quot;source&quot;:page}]<br>                )     <br><br>    def rag_query(self, query: str, top_k: int = 3) :<br><br>        query_emb = self.embed_text(query)<br>        results = self.collection.query(query_embeddings=[query_emb],n_results=top_k)<br>        docs = results[&quot;documents&quot;][0]<br>        sources = [meta[&quot;source&quot;] for meta in results[&quot;metadatas&quot;][0]]<br>        return docs, sources<br><br></pre><p>This RAG class takes the websites, uses BeautifulSoup to extract the text from the webpages and puts them into a vector store. This is then used to answer questions on the web pages, in agent_three.</p><p><strong>agent_four</strong></p><pre>from google.adk.agents import Agent<br>from google.adk.tools.retrieval.vertex_ai_rag_retrieval import VertexAiRagRetrieval<br>from vertexai.preview import rag<br>from vertexai.generative_models import GenerativeModel, Tool<br>from google.cloud.aiplatform_v1beta1.types import EncryptionSpec<br>import vertexai<br><br>from .rag import DSOFAQRag<br><br>rag_store = DSOFAQRag(collection_name=&quot;dso_collection&quot;,chroma_dir=&quot;chroma_dir&quot;)<br><br>try:<br>    test = rag_store.collection.peek()<br>    is_empty = not test or len(test.get(&quot;ids&quot;,[])) == 0<br>except Exception:<br>    res = rag_store.collection.query(query_texts=[&quot;wiz&quot;], n_results=1)<br>    is_empty = (len(res.get(&quot;documents&quot;,[[]])[0]) == 0)    <br><br>if is_empty:<br>    print(&quot;Ingesting dso into chroma db&quot;)<br>    rag_store.embed_web_pages()<br><br>def dso_rag_tool(query:str, top_k: int = 3):<br>    docs, sources = rag_store.rag_query(query=query, top_k=top_k)<br>    combined = &quot; | &quot;.join(docs)<br>    refs = &quot; , &quot;.join(set(sources))<br><br>    return f&quot;Answer candidates: {combined}\n (Sources: {refs})&quot;<br><br> <br><br>agent_four = Agent(<br>    name=&quot;agent_four&quot;,<br>    model=&quot;gemini-2.0-flash-001&quot;,<br>    description=&quot;Answers questions about from web pages&quot;,<br>    instruction=&quot;&quot;&quot;<br>     ##INSTRUCTIONS<br>    &quot;&quot;&quot;,<br>    tools=[rag_tool]<br>    )</pre><p>Agent four is a little different from agent three, but a lot similar. In agent_four, the web pages are sharepoint sites that are not accessible by the application. These were downloaded and then webscrapped .</p><p><strong>rag</strong></p><pre>import os<br>from typing import List, Dict, Any, Optional<br>import pandas as pd<br>from sentence_transformers import SentenceTransformer<br>import chromadb<br>from chromadb.config import Settings<br>import requests<br>from bs4 import BeautifulSoup<br>import tiktoken<br>from chromadb.utils import embedding_functions<br><br>CHROMA_DIR = os.path.join(os.path.dirname(__file__),&quot;..&quot;,&quot;chroma_db&quot;)<br><br>class DSOFAQRag:<br>    def __init__(self,<br>                 collection_name: str = &quot;dso_collection&quot;,<br>                 chroma_dir:Optional[str] = CHROMA_DIR,<br>                 embedding_model: str = &quot;all-MiniLM-L6-v2&quot;,<br>                 persist:bool = True) -&gt; None:<br>        <br>        self.model = SentenceTransformer(embedding_model)<br>        self.embedding_dim = self.model.get_sentence_embedding_dimension()<br><br>        settings = Settings(<br>            chroma_db_impl = &quot;duckdb+parquet&quot;,<br>            persist_directory = os.path.abspath(chroma_dir) if persist else None<br>        )<br>        self.client = chromadb.PersistentClient(path=os.path.abspath(chroma_dir))<br><br>        def sbert_embed_fn(texts: List[str]) -&gt; List[List[float]]:<br>            return [emb.toList() for emb in self.model.encode(texts, show_progress_bar=False)]<br>        <br>        # self.embed_fn = embedding_functions.EmbeddingFunction(<br>        #     sbert_embed_fn,<br>        #     embedding_dimensions=self.embedding_dim<br>        # )<br><br>        self.collection = self.client.get_or_create_collection(<br>            name=collection_name,<br>           # embedding_function=self.embed_fn<br>        )<br><br>    def embed_text(self, txt:str):<br>        embedding = self.model.encode(txt).tolist()<br>        return embedding<br>    <br>    def scrape_page(self, page: str) -&gt; str:<br>        with open(page, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<br>                html_content = file.read()<br>                soup = BeautifulSoup(html_content, &quot;html.parser&quot;)<br>                for script in soup([&quot;script&quot;,&quot;style&quot;]):<br>                    script.decompose()<br>                return soup.get_text(separator=&quot; &quot;, strip=True)<br>    <br>    def chun_text(self, txt:str, max_tokens: int = 300) -&gt; list[str]:<br>        enc = tiktoken.get_encoding(&quot;cl100k_base&quot;)<br>        tokens = enc.encode(txt)<br>        chunks = []<br>        for i in range(0, len(tokens), max_tokens):<br>            chunk_tokens = tokens[i:i+max_tokens]<br>            chunk_text = enc.decode(chunk_tokens)<br>            chunks.append(chunk_text)<br>        return chunks<br>    <br>    def embed_web_pages(self):<br>        pages = [(&quot;/path/to/site1.html&quot;,&quot;https://internal-site-url1&quot;),<br>                  #| <br>                  #|Multiple Downloaded Sites<br>                  #|<br>                 (&quot;/path/to/site2.html&quot;,&quot;https://internal-site-url2&quot;)<br>                  <br>                ]<br>        for page in pages:<br>            text = self.scrape_page(page[0])<br>            chunks = self.chun_text(text)<br>            for i, chunk in enumerate(chunks):<br>                embedding = self.embed_text(chunk)<br>                self.collection.add(<br>                    ids=[f&quot;{page[1]}-{i}&quot;],<br>                    embeddings=[embedding],<br>                    documents=[chunk],<br>                    metadatas=[{&quot;source&quot;:page[1]}]<br>                )     <br><br>    def rag_query(self, query: str, top_k: int = 3) :<br><br>        query_emb = self.embed_text(query)<br>        results = self.collection.query(query_embeddings=[query_emb],n_results=top_k)<br>        docs = results[&quot;documents&quot;][0]<br>        sources = [meta[&quot;source&quot;] for meta in results[&quot;metadatas&quot;][0]]<br>        return docs, sources<br><br></pre><p>This is similar to the earlier RAG, the only difference is that, here the pages being webscraped are downloaded pages and not done dynamically.</p><p><strong>agent_five</strong></p><p>This is the fall back agent that just greets the user and has not been modified.</p><pre>from google.adk.agents import Agent<br>from google.adk.models.lite_llm import LiteLlm<br><br>agent_five = Agent(<br>    name = &quot;agent five&quot;,<br>    model = LiteLlm(&quot;gemini-2.0-flash-lite&quot;),<br>    description=&quot;A greeting agent&quot;,<br>    instruction= &quot;&quot;&quot;<br>    You are a greeting agent. Respond with a polite greeting.<br>    &quot;&quot;&quot;<br>)</pre><p>With all the applications setup, you can run the ADK lab version by running:</p><p><strong><em>adk web — port 8080</em></strong></p><p>In this sample app, we are going to interface with the application using Streamlit (<a href="https://streamlit.io/">https://streamlit.io/</a>).</p><p><strong>adk_app</strong></p><pre>import streamlit as st<br>import asyncio<br>import time<br>import os<br>from google.adk.sessions import InMemorySessionService<br>from google.adk.runners import Runner<br>from google.genai import types as genai_types<br>from vertexai.agent_engines import AdkApp<br>from classifier_agent.agent import root_agent<br>from config.settings import APP_NAME_FOR_ADK, USER_ID, INITIAL_STATE, ADK_SESSION_KEY<br><br>def session_service_builder():<br>    return InMemorySessionService()<br><br>app = AdkApp(<br>    agent=root_agent,<br>    session_service_builder=session_service_builder<br>)<br><br>session_service = InMemorySessionService()<br>runner = Runner(agent=root_agent, app_name=APP_NAME_FOR_ADK, session_service=session_service)<br><br>async def call_agent_async(query: str, runner: Runner, user_id: str = USER_ID, session_id: str = &quot;session-1&quot;) -&gt; str:<br>    content = genai_types.Content(role=&quot;user&quot;, parts=[genai_types.Part(text=query)])<br><br>    await session_service.create_session(app_name= APP_NAME_FOR_ADK, user_id=user_id, session_id=session_id)<br><br>    events = runner.run_async(user_id=user_id, session_id=session_id, new_message=content)<br><br>    final_text = &quot;&quot;<br><br>    async for event in events:<br>        try:<br>            if event.is_final_response():<br>                final_text = event.content.parts[0].text<br>        except Exception:<br>            print(&quot;exception&quot;)<br>            pass<br>    return final_text</pre><p><strong>UI</strong></p><p>streamlit_app</p><pre>import streamlit as st<br>from adk.app_adk import initialize_adk, run_adk_sync<br>from config.settings import MESSAGE_HISTORY_KEY #, get_api_key<br>def run_streamlit_app():<br>    &quot;&quot;&quot;<br>    Sets up and runs the Streamlit web application for the ADK chat assistant.<br>    &quot;&quot;&quot;<br>    st.set_page_config(page_title=&quot; Chat Agent&quot;, layout=&quot;wide&quot;) # Configures the browser tab title and page layout.<br>    st.title(&quot;Chat Bot&quot;) # Main title of the app.<br>    st.markdown(&quot;An chatbot to help answer questions&quot;) # Descriptive text.<br>    st.divider() # A visual separator.<br>    # api_key = get_api_key() # Retrieve the API key from settings.<br>    # if not api_key:<br>    #     st.error(&quot;⚠️ Action Required: Google API Key Not Found or Invalid! Please set GOOGLE_API_KEY in your .env file. ⚠️&quot;)<br>    #     st.stop() # Stop the application if the API key is missing, prompting the user for action.<br>    # Initialize ADK runner and session ID (cached to run only once).<br>    adk_runner, current_session_id = initialize_adk()<br>    <br>    st.divider()<br>    st.subheader(&quot;Chat&quot;) # Subheading for the chat section.<br>    # Initialize chat message history in Streamlit&#39;s session state if it doesn&#39;t exist.<br>    if MESSAGE_HISTORY_KEY not in st.session_state:<br>        st.session_state[MESSAGE_HISTORY_KEY] = []<br>    # Display existing chat messages from the session state.<br>    for message in st.session_state[MESSAGE_HISTORY_KEY]:<br>        with st.chat_message(message[&quot;role&quot;]): # Use Streamlit&#39;s chat message container for styling.<br>            st.markdown(message[&quot;content&quot;])<br>    # Handle new user input.<br>    if prompt := st.chat_input(&quot;Ask ..&quot;):<br>        # Append user&#39;s message to history and display it.<br>        st.session_state[MESSAGE_HISTORY_KEY].append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt})<br>        with st.chat_message(&quot;user&quot;):<br>            st.markdown(prompt)<br>        # Process the user&#39;s message with the ADK agent and display the response.<br>        with st.chat_message(&quot;assistant&quot;):<br>            message_placeholder = st.empty() # Create an empty placeholder to update with the assistant&#39;s response.<br>            with st.spinner(&quot;Assistant is thinking...&quot;): # Show a spinner while the agent processes the request.<br>                print(&quot;PROCESSING CHAT MESSAGE&quot;)<br>                agent_response = run_adk_sync(adk_runner, current_session_id, prompt) # Call the synchronous ADK runner.<br>                message_placeholder.markdown(agent_response) # Update the placeholder with the final response.<br>        <br>        # Append assistant&#39;s response to history.<br>        st.session_state[MESSAGE_HISTORY_KEY].append({&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: agent_response})</pre><p>Streamlit does not support async calls, so we had to wrap it around a synchronized call.</p><p>To run the application:</p><pre>streamlit run .\streamlit_app.py</pre><p>Sample application using ADK and Streamlit: <a href="https://github.com/tobintom/llm_multiagent_adk">tobintom/llm_multiagent_adk: A multi-agent application built with Google ADK</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dde9f5babb16" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi-Agent chatbot with LangGraph]]></title>
            <link>https://medium.com/@tobintom/agentic-ai-with-langgraph-437fcde22054?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/437fcde22054</guid>
            <category><![CDATA[langchain-agents]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[agentic-workflow]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Wed, 20 Aug 2025 05:37:21 GMT</pubDate>
            <atom:updated>2025-08-20T16:58:52.313Z</atom:updated>
            <content:encoded><![CDATA[<h3>Multi-Agent Chatbot with LangGraph</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*9HFsjXjgw6oG2HwQ3Okgwg.png" /></figure><p>LangGraph (<a href="https://www.langchain.com/langgraph">https://www.langchain.com/langgraph</a>) has turned into one of the most popular frameworks for building Agentic AI applications. With Agentic AI, the application has a lot more scope and tasks to accomplish by navigating various flows and autonomously invoking various agents to fulfill a task completely. LangGraph is built within the LangChain system to act as an orchestration framework to build a multi-step flow for each task execution. Unlike a linear chain of events that you build with LangChain, with a multi-step flow, the orchestration can have logical conditions which decide which agent to invoke, it can make decisions, use various tools and maintain the state of the conversation throughout the flow.</p><p>Some basic elements of a LangGraph application are shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*JQJrMCvZHrwPmL9_TfcLFw.png" /></figure><p><strong>Node: </strong>A node can be considered as an individual unit that accomplishes a piece of task. The task can be a unit of work or a conditional decision. A node can be a llm invocation, a function call, some kind of code execution, a condition check execution etc. The node needs to execute and return a result.</p><p><strong>Edge: </strong>An edge defines the next step after a node is completed. It can link different nodes together and defines the path after a node execution is complete. An edge can also be used as a conditional decision to decide which node to invoke next in the graph flow.</p><p><strong>State: </strong>When the flow is executed, LangGraph maintains the state that is passed between the nodes, this allows different nodes to understand the context of execution.</p><p><strong>Graph: </strong>This whole workflow is the graph. It defines the workflow of execution. The application is compiled and deployed and for each invocation, it executes the flow from beginning to end.</p><h3><strong>Application Setup</strong></h3><p>In this article, we will create a LangGraph application that is an internal employee support agent that helps employees handle business operations. Sample datasets were taken from <a href="https://huggingface.co/datasets/AyoubChLin/northwind_Shipping_orders">https://huggingface.co/datasets/AyoubChLin/northwind_Shipping_orders</a> and <a href="https://huggingface.co/datasets/AyoubChLin/northwind_invocies">https://huggingface.co/datasets/AyoubChLin/northwind_invocies</a>. In this imaginary business, we are creating a support chatbot for employees working with invoices and orders. The application has a folder /invoices that has invoice pdfs and another folder /orders that has order pdfs. There is also an API call that gives the order status of a given order id.</p><p>The user can ask about orders, invoices or general queries about the business. We can now define the workflow we want the chatbot to implement. We have three separate agents that will answer different queries. One to answer about orders, one for invoices and another for general queries. When the query is received, we want a classifier to determine what kind of query this is, is it a query for orders, invoices or a general question. Depending on the query, its appropriate agent is invoked. The orders agent has a ChromaDB vector store of orders PDFs and an API to check for order status. The invoice agent has a ChromaDB vector store of invoices and the general agent utilizes the LLM to answer general queries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/306/1*-5jKrnhBmpCo36DJkAYVIg.png" /></figure><p>We will first load the PDFs from the /orders and /invoices folders, chunk them and embed them into a ChromaDB store. We are using a hugging face model for the embedding — BAAI/bge-small-en-v1.5.</p><pre>def load_and_split_pdfs(folder_path):<br>    &quot;&quot;&quot;Load PDFs from folder and split into chunks.&quot;&quot;&quot;<br>    if not os.path.exists(folder_path):<br>        logger.warning(f&quot;Folder {folder_path} does not exist. Creating it...&quot;)<br>        os.makedirs(folder_path, exist_ok=True)<br>        return []<br>    <br>    docs = []<br>    pdf_files = [f for f in os.listdir(folder_path) if f.endswith(&quot;.pdf&quot;)]<br>    <br>    if not pdf_files:<br>        logger.warning(f&quot;No PDF files found in {folder_path}&quot;)<br>        return []<br>    <br>    logger.info(f&quot;Found {len(pdf_files)} PDF files&quot;)<br>    <br>    for file in pdf_files:<br>        try:<br>            file_path = os.path.join(folder_path, file)<br>            logger.info(f&quot;Loading {file}...&quot;)<br>            loader = PyPDFLoader(file_path)<br>            file_docs = loader.load()<br>            docs.extend(file_docs)<br>            logger.info(f&quot;Loaded {len(file_docs)} pages from {file}&quot;)<br>        except Exception as e:<br>            logger.error(f&quot;Error loading {file}: {str(e)}&quot;)<br>    <br>    if not docs:<br>        logger.warning(&quot;No documents were loaded successfully&quot;)<br>        return []<br>    <br>    # Split documents into chunks<br>    splitter = RecursiveCharacterTextSplitter(<br>        chunk_size=1000, <br>        chunk_overlap=200<br>    )<br>    <br>    split_docs = splitter.split_documents(docs) <br>    return split_docs<br><br>def create_or_load_vectorstore(documents=None, name=&quot;docs&quot;):<br>    model_name = &quot;BAAI/bge-small-en-v1.5&quot; #&quot;BAAI/bge-base-en-v1.5&quot;<br>    embeddings = HuggingFaceBgeEmbeddings(<br>        model_name=model_name,<br>        model_kwargs={&quot;device&quot;: &quot;cpu&quot;},  # or &quot;cuda&quot; for GPU<br>        encode_kwargs={&quot;normalize_embeddings&quot;: True}<br>    )<br><br>    # # If DB exists, load it<br>    # if os.path.exists(PERSIST_DIR) and os.listdir(PERSIST_DIR):<br>    #     print(&quot;✅ Found existing ChromaDB. Loading without re-embedding...&quot;)<br>    #     return Chroma(persist_directory=PERSIST_DIR, embedding_function=embeddings)<br><br>    # Otherwise, create it<br>    print(&quot;📄 Creating ChromaDB and embedding documents...&quot;)<br>    vectordb = Chroma.from_documents(documents, embeddings, persist_directory=PERSIST_DIR,collection_name=name)<br>    vectordb.persist()<br>    return vectordb</pre><p>We will now create the orders_retriever and invoices_retriever using these vector stores.</p><pre>invoice_docs = load_and_split_pdfs(&quot;invoices&quot;)<br>order_docs = load_and_split_pdfs(&quot;orders&quot;)<br>invoice_vectordb = create_or_load_vectorstore(invoice_docs,&quot;invoices_pdfs&quot;)<br>order_vectordb = create_or_load_vectorstore(order_docs,&quot;orders_pdfs&quot;)<br><br>orders_retriever = order_vectordb.as_retriever(search_type=&quot;similarity&quot;, search_kwargs={&quot;k&quot;: 4})<br>invoices_retriever = invoice_vectordb.as_retriever(search_type=&quot;similarity&quot;, search_kwargs={&quot;k&quot;: 4})  </pre><p>We have three tools available, one for orders PDF content search, one api call for order status and one for invoices PDF content search.</p><pre>#RAG Tool for orders PDFs<br>@tool<br>def search_orders(query: str) -&gt; str:<br>    &quot;&quot;&quot;Search the orders PDFs for information on order details, shipping details, customer details, shipper details, products and number of orders &quot;&quot;&quot;<br>    print(f&quot;Inside search_orders {query}&quot;)<br>    docs = orders_retriever.get_relevant_documents(query)<br>    print(docs)<br>    if not docs:<br>        return &quot;No matches found.&quot;<br>    chunks = []<br>    for i, d in enumerate(docs, 1):<br>        meta = d.metadata or {}<br>        src = meta.get(&quot;source&quot;, &quot;unknown&quot;)<br>        page = meta.get(&quot;page&quot;, None)<br>        where = f&quot; (page {page})&quot; if page is not None else &quot;&quot;<br>        chunks.append(f&quot;[{i}] {src}{where}:\n{d.page_content[:800]}&quot;)<br>    return &quot;\n\n&quot;.join(chunks)<br><br>#RAG tool for invoices PDFs<br>@tool<br>def search_invoices(query: str) -&gt; str:<br>    &quot;&quot;&quot;Search the invoices PDFs for information on invoice details&quot;&quot;&quot;<br>    docs = invoices_retriever.get_relevant_documents(query)<br>    if not docs:<br>        return &quot;No matches found.&quot;<br>    chunks = []<br>    for i, d in enumerate(docs, 1):<br>        meta = d.metadata or {}<br>        src = meta.get(&quot;source&quot;, &quot;unknown&quot;)<br>        page = meta.get(&quot;page&quot;, None)<br>        where = f&quot; (page {page})&quot; if page is not None else &quot;&quot;<br>        chunks.append(f&quot;[{i}] {src}{where}:\n{d.page_content[:800]}&quot;)<br>    return &quot;\n\n&quot;.join(chunks)<br><br>#Fictional API call to get the order status<br>@tool(&quot;get_order_status&quot;, return_direct=False, description=&quot;Get the order status of an order id&quot;)<br>def get_order_status(order_id: str) -&gt; str:<br>    statuses = [&quot;PENDING&quot;, &quot;PROCESSING&quot;, &quot;SHIPPED&quot;, &quot;DELIVERED&quot;, &quot;CANCELLED&quot;]<br>    idx = sum(ord(c) for c in order_id) % len(statuses)<br>    return f&quot;Order {order_id} status: {statuses[idx]}&quot;</pre><p>We need to bind these tools appropriately based on the agent that will invoke them.</p><pre>orders_tools = [search_orders, get_order_status]<br>invoices_tools = [search_invoices]</pre><p>Now that we created the appropriate tools and have associated them in the right categories, we can now build out the LangGraph workflow and build agents to use these tools to answer queries.</p><p>The first step is the define the State that the Graph is going to use. This defines the data that is passed along for each step of the graph traversal. In this case, we will define the state as the messages and their history as well as a ‘query_type’.</p><pre>class State(TypedDict):<br>    messages: Annotated[list, add_messages]<br>    query_type: str | None</pre><p>When the user input is received, the first flow involves analyzing the query and trying to classify it as a ‘order’, ‘invoice’ or ‘general’ query and then defining the router to forward it to the right agent. To classify the query, we will use the chatbot’s LLM. In this article, we are using Ollama to host Llama3.2.</p><pre>llm = ChatOllama(model=&quot;llama3.2&quot;, temperature=0.7)<br><br>def classify_query(state: State):<br>    message = state[&quot;messages&quot;][-1]<br>     <br>    classifier_llm = llm.with_structured_output(QueryType)<br>    result = classifier_llm.invoke([<br>        {<br>            &quot;role&quot; : &quot;system&quot;,<br>            &quot;content&quot; : &quot;&quot;&quot;Classify the user message as either questions on invoices, orders or general.<br>            - &#39;order&#39;: If the user asks about an order or the query has the word order or orders in it. Or the number of orders or mentions order date or order details<br>            - &#39;invoice&#39;: If the user asks about an invoice or the query has the word invoice or invoices or mentions invoice date or invoice details<br>            - &#39;general&#39; : If the user asks anything other than about invoices or orders &quot;&quot;&quot;<br>        },<br>        {<br>            &quot;role&quot; : &quot;user&quot;,<br>            &quot;content&quot; : message.content<br>        }<br>    ])    <br>    return {&quot;query_type&quot;: result.query_type}<br><br>def router(state: State):<br>    query_type = state.get(&quot;query_type&quot; , &quot;general&quot;)<br>     <br>    if query_type == &quot;order&quot;:<br>        return {&quot;next&quot; : &quot;order&quot;}<br>    elif query_type == &quot;invoice&quot;:<br>        return {&quot;next&quot; : &quot;invoice&quot;}<br>    return {&quot;next&quot; : &quot;general&quot;}</pre><p>As shown above, the llm is used to classify the query and then the router() determines the next agent invocation.</p><p>Each agent uses the tools it needs, by binding it with the llm.</p><pre>orders_tools = [search_orders, get_order_status]<br>&quot;&quot;&quot;Handle tool calls for order queries&quot;&quot;&quot;<br>llm_with_tools = llm.bind_tools(orders_tools)<br> # Build proper message history<br> messages = [<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are an assistant that helps with order queries. Use the available tools when needed to search for order information or check order status.&quot;}<br> ] + state[&quot;messages&quot;]<br>    <br> response = llm_with_tools.invoke(messages)</pre><p>Similarly the invoice agent uses the invoice_tools and binds it to the llm to answer questions about invoices. The general agent uses the llm to answer general queries.</p><pre>def general_agent(state: State):<br>    last_message = state[&quot;messages&quot;][-1]<br><br>    messages = [<br>        { &quot;role&quot;: &quot;system&quot;,<br>          &quot;content&quot;: &quot;&quot;&quot;You are a business internal employees assistant focusing on helping them with orders, invoices or other<br>          common queries they may have.&quot;&quot;&quot;<br>        },<br>        {<br>            &quot;role&quot;:&quot;user&quot;,<br>            &quot;content&quot;:last_message.content<br>        }<br>    ]<br>    response = llm.invoke(messages)<br><br>    print(f&quot;General agent {response}&quot;)<br>    return {&quot;messages&quot;: [response]}<br><br>def invoices_agent(state: State):<br>    &quot;&quot;&quot;Handle tool calls and responses properly for invoices&quot;&quot;&quot;<br>    llm_with_tools = llm.bind_tools(invoices_tools)<br>    <br>    # Build proper message history<br>    messages = [<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are an assistant that helps with invoice queries. Use the available tools when needed to search for invoice information.&quot;}<br>    ] + state[&quot;messages&quot;]<br>    <br>    response = llm_with_tools.invoke(messages)</pre><p>Now that we have built all the elements of the graph, e.g. the state, the conditional classifier, router and agents, we can now bind them all together to create a workflow.</p><p>First all the nodes of the workflow are created.</p><pre>graph_builder = StateGraph(State)<br>graph_builder.add_node(&quot;classifier&quot;, classify_query)<br>graph_builder.add_node(&quot;router&quot;,router)<br>graph_builder.add_node(&quot;order&quot;,orders_agent)<br>graph_builder.add_node(&quot;invoice&quot;,invoices_agent)<br>graph_builder.add_node(&quot;general&quot;,general_agent)</pre><p>Then, they are bound with edges.</p><pre>graph_builder.add_edge(START, &quot;classifier&quot;)<br>graph_builder.add_edge(&quot;classifier&quot;, &quot;router&quot;)<br>graph_builder.add_conditional_edges(&quot;router&quot;,<br>    lambda state: state.get(&quot;next&quot;),<br>        {&quot;order&quot;:&quot;order&quot;,&quot;invoice&quot;:&quot;invoice&quot;,&quot;general&quot;:&quot;general&quot;}<br>)<br>graph_builder.add_edge(&quot;order&quot;, END)<br>graph_builder.add_edge(&quot;invoice&quot;, END)<br>graph_builder.add_edge(&quot;general&quot;, END)<br><br>graph = graph_builder.compile()</pre><p>We can now invoke the graph with a user message as shown below:</p><pre> user_message = HumanMessage(content=user_input)<br> state[&quot;messages&quot;] = state.get(&quot;messages&quot;,[]) + [user_message] <br>        <br> # Invoke the graph<br> result = graph.invoke(state)</pre><p>The full code is shown below:</p><pre>import os<br><br>from typing import Annotated, Literal<br>from langgraph.graph import StateGraph, START, END<br>from langgraph.graph.message import add_messages<br>from pydantic import BaseModel, Field<br><br>from langchain.text_splitter import RecursiveCharacterTextSplitter<br>from typing_extensions import TypedDict<br>from langchain_ollama import ChatOllama<br>from langchain_core.documents import Document<br>from langchain_community.document_loaders import DirectoryLoader, PyPDFLoader<br>from langchain_community.vectorstores import FAISS<br>from langchain_community.embeddings import OllamaEmbeddings<br>from langchain_core.messages import HumanMessage, AIMessage, ToolMessage<br><br>from langchain_community.embeddings import HuggingFaceBgeEmbeddings<br><br><br># vector store (Chroma)<br>from langchain.vectorstores import Chroma<br><br>from langchain.tools import tool<br>import logging<br><br>logging.basicConfig(level=logging.INFO)<br>logger = logging.getLogger(__name__)<br><br>PERSIST_DIR = &quot;chroma_db&quot;<br><br>def load_and_split_pdfs(folder_path):<br>    &quot;&quot;&quot;Load PDFs from folder and split into chunks.&quot;&quot;&quot;<br>    if not os.path.exists(folder_path):<br>        logger.warning(f&quot;Folder {folder_path} does not exist. Creating it...&quot;)<br>        os.makedirs(folder_path, exist_ok=True)<br>        return []<br>    <br>    docs = []<br>    pdf_files = [f for f in os.listdir(folder_path) if f.endswith(&quot;.pdf&quot;)]<br>    <br>    if not pdf_files:<br>        logger.warning(f&quot;No PDF files found in {folder_path}&quot;)<br>        return []<br>    <br>    logger.info(f&quot;Found {len(pdf_files)} PDF files&quot;)<br>    <br>    for file in pdf_files:<br>        try:<br>            file_path = os.path.join(folder_path, file)<br>            logger.info(f&quot;Loading {file}...&quot;)<br>            loader = PyPDFLoader(file_path)<br>            file_docs = loader.load()<br>            docs.extend(file_docs)<br>            logger.info(f&quot;Loaded {len(file_docs)} pages from {file}&quot;)<br>        except Exception as e:<br>            logger.error(f&quot;Error loading {file}: {str(e)}&quot;)<br>    <br>    if not docs:<br>        logger.warning(&quot;No documents were loaded successfully&quot;)<br>        return []<br>    <br>    # Split documents into chunks<br>    splitter = RecursiveCharacterTextSplitter(<br>        chunk_size=1000, <br>        chunk_overlap=200<br>    )<br>    <br>    split_docs = splitter.split_documents(docs) <br>    return split_docs<br><br>def create_or_load_vectorstore(documents=None, name=&quot;docs&quot;):<br>    model_name = &quot;BAAI/bge-small-en-v1.5&quot; #&quot;BAAI/bge-base-en-v1.5&quot;<br>    embeddings = HuggingFaceBgeEmbeddings(<br>        model_name=model_name,<br>        model_kwargs={&quot;device&quot;: &quot;cpu&quot;},  # or &quot;cuda&quot; for GPU<br>        encode_kwargs={&quot;normalize_embeddings&quot;: True}<br>    )<br><br>    # # If DB exists, load it<br>    # if os.path.exists(PERSIST_DIR) and os.listdir(PERSIST_DIR):<br>    #     print(&quot;✅ Found existing ChromaDB. Loading without re-embedding...&quot;)<br>    #     return Chroma(persist_directory=PERSIST_DIR, embedding_function=embeddings)<br><br>    # Otherwise, create it<br>    print(&quot;📄 Creating ChromaDB and embedding documents...&quot;)<br>    vectordb = Chroma.from_documents(documents, embeddings, persist_directory=PERSIST_DIR,collection_name=name)<br>    vectordb.persist()<br>    return vectordb<br><br>invoice_docs = load_and_split_pdfs(&quot;invoices&quot;)<br>order_docs = load_and_split_pdfs(&quot;orders&quot;)<br>invoice_vectordb = create_or_load_vectorstore(invoice_docs,&quot;invoices_pdfs&quot;)<br>order_vectordb = create_or_load_vectorstore(order_docs,&quot;orders_pdfs&quot;)<br><br>orders_retriever = order_vectordb.as_retriever(search_type=&quot;similarity&quot;, search_kwargs={&quot;k&quot;: 4})<br>invoices_retriever = invoice_vectordb.as_retriever(search_type=&quot;similarity&quot;, search_kwargs={&quot;k&quot;: 4})  <br><br>#RAG Tool for orders PDFs<br>@tool<br>def search_orders(query: str) -&gt; str:<br>    &quot;&quot;&quot;Search the orders PDFs for information on order details, shipping details, customer details, shipper details, products and number of orders &quot;&quot;&quot;<br>    print(f&quot;Inside search_orders {query}&quot;)<br>    docs = orders_retriever.get_relevant_documents(query)<br>    print(docs)<br>    if not docs:<br>        return &quot;No matches found.&quot;<br>    chunks = []<br>    for i, d in enumerate(docs, 1):<br>        meta = d.metadata or {}<br>        src = meta.get(&quot;source&quot;, &quot;unknown&quot;)<br>        page = meta.get(&quot;page&quot;, None)<br>        where = f&quot; (page {page})&quot; if page is not None else &quot;&quot;<br>        chunks.append(f&quot;[{i}] {src}{where}:\n{d.page_content[:800]}&quot;)<br>    return &quot;\n\n&quot;.join(chunks)<br><br>#RAG tool for invoices PDFs<br>@tool<br>def search_invoices(query: str) -&gt; str:<br>    &quot;&quot;&quot;Search the invoices PDFs for information on invoice details&quot;&quot;&quot;<br>    docs = invoices_retriever.get_relevant_documents(query)<br>    if not docs:<br>        return &quot;No matches found.&quot;<br>    chunks = []<br>    for i, d in enumerate(docs, 1):<br>        meta = d.metadata or {}<br>        src = meta.get(&quot;source&quot;, &quot;unknown&quot;)<br>        page = meta.get(&quot;page&quot;, None)<br>        where = f&quot; (page {page})&quot; if page is not None else &quot;&quot;<br>        chunks.append(f&quot;[{i}] {src}{where}:\n{d.page_content[:800]}&quot;)<br>    return &quot;\n\n&quot;.join(chunks)<br><br>#Fictional API call to get the order status<br>@tool(&quot;get_order_status&quot;, return_direct=False, description=&quot;Get the order status of an order id&quot;)<br>def get_order_status(order_id: str) -&gt; str:<br>    statuses = [&quot;PENDING&quot;, &quot;PROCESSING&quot;, &quot;SHIPPED&quot;, &quot;DELIVERED&quot;, &quot;CANCELLED&quot;]<br>    idx = sum(ord(c) for c in order_id) % len(statuses)<br>    return f&quot;Order {order_id} status: {statuses[idx]}&quot;<br><br>class QueryType(BaseModel):<br>    query_type: Literal[&quot;invoice&quot;,&quot;order&quot;,&quot;general&quot;] = Field(<br>        ...,<br>        description=&quot;Classify if the message was a query about invoices, orders or a general query. If the query is about invoices return invoice, else If the query is about orders return order, else return general&quot;<br>    )<br><br>class State(TypedDict):<br>    messages: Annotated[list, add_messages]<br>    query_type: str | None<br><br>llm = ChatOllama(model=&quot;llama3.2&quot;, temperature=0.7)<br><br><br>orders_tools = [search_orders, get_order_status]<br>invoices_tools = [search_invoices]<br><br>def chatbot(state: State):<br>    return{&quot;messages&quot;: [llm.invoke(state[&quot;messages&quot;])]}<br><br>def classify_query(state: State):<br>    message = state[&quot;messages&quot;][-1]<br>     <br>    classifier_llm = llm.with_structured_output(QueryType)<br>    result = classifier_llm.invoke([<br>        {<br>            &quot;role&quot; : &quot;system&quot;,<br>            &quot;content&quot; : &quot;&quot;&quot;Classify the user message as either questions on invoices, orders or general.<br>            - &#39;order&#39;: If the user asks about an order or the query has the word order or orders in it. Or the number of orders or mentions order date or order details<br>            - &#39;invoice&#39;: If the user asks about an invoice or the query has the word invoice or invoices or mentions invoice date or invoice details<br>            - &#39;general&#39; : If the user asks anything other than about invoices or orders &quot;&quot;&quot;<br>        },<br>        {<br>            &quot;role&quot; : &quot;user&quot;,<br>            &quot;content&quot; : message.content<br>        }<br>    ])<br>    print(result.query_type)<br>    return {&quot;query_type&quot;: result.query_type}<br><br>def router(state: State):<br>    query_type = state.get(&quot;query_type&quot; , &quot;general&quot;)<br>    print(f&quot;ROUTER {query_type}&quot;)<br>    if query_type == &quot;order&quot;:<br>        return {&quot;next&quot; : &quot;order&quot;}<br>    elif query_type == &quot;invoice&quot;:<br>        return {&quot;next&quot; : &quot;invoice&quot;}<br>    return {&quot;next&quot; : &quot;general&quot;}<br><br>def orders_agent(state: State):<br>    &quot;&quot;&quot;Handle tool calls and responses properly&quot;&quot;&quot;<br>    llm_with_tools = llm.bind_tools(orders_tools)<br>    <br>    # Build proper message history<br>    messages = [<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are an assistant that helps with order queries. Use the available tools when needed to search for order information or check order status.&quot;}<br>    ] + state[&quot;messages&quot;]<br>    <br>    response = llm_with_tools.invoke(messages)<br>    print(f&quot;Orders agent response: {response}&quot;)<br>    <br>    # Check if the model wants to use tools<br>    if response.tool_calls:<br>        # Process tool calls<br>        tool_messages = []<br>        for tool_call in response.tool_calls:<br>            print(f&quot;Tool call: {tool_call}&quot;)<br>            <br>            # Find and execute the tool<br>            tool_name = tool_call[&quot;name&quot;]<br>            tool_args = tool_call[&quot;args&quot;]<br>            <br>            if tool_name == &quot;search_orders&quot;:<br>                result = search_orders(tool_args.get(&#39;query&#39;))<br>            elif tool_name == &quot;get_order_status&quot;:<br>                result = get_order_status(tool_args.get(&#39;order_id&#39;))<br>            else:<br>                result = f&quot;Unknown tool: {tool_name}&quot;<br>            <br>            # Create tool message<br>            tool_message = ToolMessage(<br>                content=result,<br>                tool_call_id=tool_call[&quot;id&quot;]<br>            )<br>            tool_messages.append(tool_message)<br>        <br>        # Get final response with tool results<br>        final_messages = messages + [response] + tool_messages<br>        final_response = llm.invoke(final_messages)<br>        <br>        return {&quot;messages&quot;: [response] + tool_messages + [final_response]}<br>    else:<br>        # No tools needed<br>        return {&quot;messages&quot;: [response]}<br><br>def general_agent(state: State):<br>    last_message = state[&quot;messages&quot;][-1]<br><br>    messages = [<br>        { &quot;role&quot;: &quot;system&quot;,<br>          &quot;content&quot;: &quot;&quot;&quot;You are a business internal employees assistant focusing on helping them with orders, invoices or other<br>          common queries they may have.&quot;&quot;&quot;<br>        },<br>        {<br>            &quot;role&quot;:&quot;user&quot;,<br>            &quot;content&quot;:last_message.content<br>        }<br>    ]<br>    response = llm.invoke(messages)<br>    <br>    return {&quot;messages&quot;: [response]}<br><br>def invoices_agent(state: State):<br>    &quot;&quot;&quot;Handle tool calls and responses properly for invoices&quot;&quot;&quot;<br>    llm_with_tools = llm.bind_tools(invoices_tools)<br>    <br>    # Build proper message history<br>    messages = [<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are an assistant that helps with invoice queries. Use the available tools when needed to search for invoice information.&quot;}<br>    ] + state[&quot;messages&quot;]<br>    <br>    response = llm_with_tools.invoke(messages)<br>    print(f&quot;Invoice agent response: {response}&quot;)<br>    <br>    # Check if the model wants to use tools<br>    if response.tool_calls:<br>        # Process tool calls<br>        tool_messages = []<br>        for tool_call in response.tool_calls:<br>            print(f&quot;Tool call: {tool_call}&quot;)<br>            <br>            # Find and execute the tool<br>            tool_name = tool_call[&quot;name&quot;]<br>            tool_args = tool_call[&quot;args&quot;]<br>            <br>            if tool_name == &quot;search_invoices&quot;:           <br>            <br>                result = search_invoices(tool_args.get(&#39;query&#39;))<br>            else:<br>                result = f&quot;Unknown tool: {tool_name}&quot;<br>            <br>            # Create tool message<br>            tool_message = ToolMessage(<br>                content=result,<br>                tool_call_id=tool_call[&quot;id&quot;]<br>            )<br>            tool_messages.append(tool_message)<br>        <br>        # Get final response with tool results<br>        final_messages = messages + [response] + tool_messages<br>        final_response = llm.invoke(final_messages)<br>        <br>        return {&quot;messages&quot;: [response] + tool_messages + [final_response]}<br>    else:<br>        # No tools needed<br>        return {&quot;messages&quot;: [response]}<br><br>graph_builder = StateGraph(State)<br>graph_builder.add_node(&quot;classifier&quot;, classify_query)<br>graph_builder.add_node(&quot;router&quot;,router)<br>graph_builder.add_node(&quot;order&quot;,orders_agent)<br>graph_builder.add_node(&quot;invoice&quot;,invoices_agent)<br>graph_builder.add_node(&quot;general&quot;,general_agent)<br><br>graph_builder.add_edge(START, &quot;classifier&quot;)<br>graph_builder.add_edge(&quot;classifier&quot;, &quot;router&quot;)<br>graph_builder.add_conditional_edges(&quot;router&quot;,<br>    lambda state: state.get(&quot;next&quot;),<br>        {&quot;order&quot;:&quot;order&quot;,&quot;invoice&quot;:&quot;invoice&quot;,&quot;general&quot;:&quot;general&quot;}<br>)<br>graph_builder.add_edge(&quot;order&quot;, END)<br>graph_builder.add_edge(&quot;invoice&quot;, END)<br>graph_builder.add_edge(&quot;general&quot;, END)<br><br>graph = graph_builder.compile() <br><br>def run():<br>    state = {&quot;messages&quot;:[], &quot;query_type&quot;:None}<br><br>    while True:<br>        user_input = input(&quot;Message: &quot;)<br>        if user_input == &quot;exit&quot;:<br>            print(&quot;Bye..&quot;)<br>            break<br><br>        # Create proper message object<br>        user_message = HumanMessage(content=user_input)<br>        state[&quot;messages&quot;] = state.get(&quot;messages&quot;,[]) + [user_message] <br>        <br>        # Invoke the graph<br>        result = graph.invoke(state)<br>        <br>        # Update state with results<br>        state = result<br>        <br>        # Print the last assistant message<br>        if result.get(&quot;messages&quot;) and len(result[&quot;messages&quot;])&gt;0:<br>            last_message = result[&quot;messages&quot;][-1]<br>            if hasattr(last_message, &#39;content&#39;):<br>                print(f&quot;Assistant: {last_message.content}&quot;)<br>            else:<br>                print(f&quot;Assistant: {last_message}&quot;)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    run()</pre><h3>Testing the application</h3><pre>Message: how many invoices do you have?<br>Assistant: I have a total of 3 invoices available in the system. Would you like to search for a specific invoice or retrieve more information about one of these invoices?<br><br>Message: what is the status of the order from july 15, 2016<br>Assistant: The order #10251 was cancelled. <br>Here is the updated details of the order:<br><br>* Status: CANCELLED<br>* Shipped Date: N/A (order was cancelled before shipment)<br>* Products:<br>        + Gustaf&#39;s Knäckebröd (cancelled)<br><br><br>Message: what are the details of the invoice from July 5, 2016<br>Assistant: Based on the available information, here are the details of the order placed on July 5, 2016:<br>* Invoice Number: 10249<br>* Customer Name: Toms Spezialitäten<br>* Salesperson: Michael Suyama<br>* Invoice Date: July 5, 2016<br>* Products:<br>        + Tofu (9 units) - $18.60 per unit = $167.40 total<br>        + Manjimup Dried Apples (40 units) - $42.40 per unit = $1696.00 total<br>* Total Price: $1863.40</pre><pre>Message: Hi<br>Assistant: Hello! Welcome to our company&#39;s internal assistance center. I&#39;m here to help you with any questions or issues related to orders, invoices, or other common queries.<br>What can I assist you with today? Do you have a specific question about an order, need help with an invoice, or is there something else on your mind?</pre><p>As you can see, the bot is able to answer questions specifically about invoices, orders, order statuses and general questions using the appropriate agents and using the right tools.</p><p>All code can be found at <a href="https://github.com/tobintom/llm_langgraph">https://github.com/tobintom/llm_langgraph</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=437fcde22054" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG with langchain and ChromaDB]]></title>
            <link>https://medium.com/@tobintom/rag-with-langchain-and-chromadb-685e916ea4b4?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/685e916ea4b4</guid>
            <category><![CDATA[llm-rag]]></category>
            <category><![CDATA[chromadb]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[llama-3]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Tue, 12 Aug 2025 05:48:41 GMT</pubDate>
            <atom:updated>2025-08-12T05:48:41.108Z</atom:updated>
            <content:encoded><![CDATA[<h3>Llama 3.2 RAG with Langchain and ChromaDB</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ZBznbiHogyVSu7FCbQKMPw.png" /></figure><p>Large language models (LLMs) are trained with billions or (with the latest models) trillions of data parameters and has a vast knowledge base. But if you wanted to create an interactive interface with a constantly changing dataset, especially dataset that is very specific to your business or organization, chances are, the LLM will not know much about it. To solve this problem, a basic solution is Retrieval Augmented Generation (RAG). RAG allows models to pull in current and specific information from external sources.</p><p>In this article, we will use an open source Llama 3.2 model to implement a RAG pipeline using Langchain and ChromaDB to get information from a directory of PDF files.</p><h3>Langchain and Chroma DB</h3><p>Langchain (<a href="https://www.langchain.com/">https://www.langchain.com/</a>) is a very popular opensource framework to simplify the development of applications powered by large language models (LLMs). It provides a set of tools, components, and abstractions that make it easier to build complex AI applications. It acts as a bridge between LLMs and external data sources, APIs, and tools. It standardizes how you interact with different language models and chain together multiple AI operations. Chroma DB (<a href="https://www.trychroma.com/">https://www.trychroma.com/</a>) is an opensource vector db, designed for storing and retrieving embeddings (vector representations of data like text, images etc). Embeddings are a way of representing data as points in space, numerical representation of data. The relationships between the values capture the semantic meaning of the object they represent, allowing the LLM to understand when to use them.</p><h3>Application Setup</h3><p>For this application, we will create a RAG pipeline using a set of PDF invoices. This test dataset was retrieved from huggingface, from <a href="https://huggingface.co/datasets/AyoubChLin/northwind_invocies">https://huggingface.co/datasets/AyoubChLin/northwind_invocies</a>, I only used 3 invoices for testing. We will create a chatbot that has information on these invoices and can answer any question with regard to the invoices. With regard to RAG, our first step is to take the PDFs and embed them into ChromaDB. Each PDF is converted into documents, then split into chunks, converted into embeddings and stored in a vector database (chroma in this case).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*-2fSIuzv4_lNlyyh.png" /><figcaption>Image from internet source</figcaption></figure><p>With this dataset, the llm will now have contextual information when asked about content about the pdfs. This is a very simple example of RAG with vector embeddings, there are multiple forms of RAG and multiple types of embeddings. In this article, we will create embeddings of about three invoices which are pdfs.</p><p>The first step is to load all the pdfs and convert them into documents and chunk the information as shown below. The chunk size and overlap are set at 1000 and 200 respectively.</p><pre># 1) load all PDFs<br>    docs = []<br>    for pdf_path in pdf_dir.glob(&quot;*.pdf&quot;):<br>        print(&quot;Loading&quot;, pdf_path.name)<br>        loader = PyPDFLoader(str(pdf_path))<br>        pages = loader.load()<br>        docs.extend(pages)<br><br>    # 2) split into chunks<br>    text_splitter = RecursiveCharacterTextSplitter(<br>        chunk_size=1000,<br>        chunk_overlap=200,<br>    )<br>    print(&quot;Splitting into chunks...&quot;)<br>    docs = text_splitter.split_documents(docs)</pre><p>The chunks can now be converted into embeddings and stored into our chromadb vector store.</p><pre># 3) create BGE embeddings (use CPU or cuda)<br>    # model_name can be &quot;BAAI/bge-small-en-v1.5&quot;<br>    model_name = &quot;BAAI/bge-small-en-v1.5&quot;<br>    model_kwargs = {&quot;device&quot;: bge_device}<br>    encode_kwargs = {&quot;normalize_embeddings&quot;: True}<br>    print(&quot;Creating HuggingFaceBgeEmbeddings (this will load the model)...&quot;)<br>    bge = HuggingFaceBgeEmbeddings(<br>        model_name=model_name,<br>        model_kwargs=model_kwargs,<br>        encode_kwargs=encode_kwargs,<br>    )<br><br>    # 4) create/persist Chroma DB<br>    print(&quot;Creating Chroma vectorstore (persisted)...&quot;)<br>    vectordb = Chroma.from_documents(<br>        documents=docs,<br>        embedding=bge,<br>        persist_directory=persist_dir,<br>        collection_name=&quot;pdfs_collection&quot;<br>    )<br>    vectordb.persist()</pre><p>To create embeddings, we need an embedding model and for this article I am using BAAI/bge-small-en-v1.5 from hugging face. After the embeddings are stored, we can now use Langchain to act as a framework that stiches the vector db and our llm to get them to work together. A very simple example is shown below:</p><pre>def make_qa_chain(vectordb, ollama_model_name=&quot;llama3.2:1b&quot;):<br>    # create retriever<br>    retriever = vectordb.as_retriever(search_type=&quot;similarity&quot;, search_kwargs={&quot;k&quot;: 4})<br>   <br>    llm = ChatOllama(model=ollama_model_name, base_url=None)  # base_url=&quot;http://localhost:11434&quot; if needed<br>    # ConversationalRetrievalChain wraps the LLM + retriever<br>    chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, return_source_documents=True)<br>    return chain</pre><p>All we did here is, used the Langchain framework to attach the vector db to the llama3.2 LLM, which is our main llm model to handle user questions. With this framework, the llm knows to check the vector store for answers if the questions have a similarity with the data stored. We can now include gradio to provide the user interface.</p><pre>def chat(question, history):<br>    history_langchain_format = []<br>    for msg in history:<br>        if msg[&#39;role&#39;] == &quot;user&quot;:<br>            history_langchain_format.append(HumanMessage(content=msg[&#39;content&#39;]))<br>        elif msg[&#39;role&#39;] == &quot;assistant&quot;:<br>            history_langchain_format.append(AIMessage(content=msg[&#39;content&#39;]))<br>    history_langchain_format.append(HumanMessage(content=question))<br><br>    result = qa_chain.invoke({&quot;question&quot;: question,&quot;chat_history&quot;:history_langchain_format})<br>    sources = result.get(&quot;source_documents&quot;, [])<br>    answer = result[&quot;answer&quot;]<br>    print(&quot;\nAssistant:&quot;, answer, &quot;\n&quot;)<br>    if sources:<br>        print(&quot;Top source chunks (text snippets):&quot;)<br>        for i, doc in enumerate(sources[:3], start=1):<br>            print(f&quot;--- source {i} (metadata: {doc.metadata}):&quot;)<br>            snippet = doc.page_content<br>            print(snippet[:600].strip(), &quot;\n---\n&quot;) <br>    <br>    return answer<br><br> <br>if __name__ == &quot;__main__&quot;:<br>    view = gr.ChatInterface(chat,type=&quot;messages&quot;).launch(inbrowser=True)</pre><p>Since the gradio interface requires the messages in the prompt message format, we modified the history parameters to match it. the full implementation is shown below.</p><pre>&quot;&quot;&quot;<br>apprag.py<br>RAG pipeline:<br>- Load PDFs from a folder<br>- Split to chunks<br>- Embed with BAAI/bge-small-en-v1.5 via LangChain community BGE wrapper<br>- Persist vectors to ChromaDB<br>- Query via a ConversationalRetrievalChain using Ollama (llama3.2)<br>&quot;&quot;&quot;<br><br>from pathlib import Path<br>import os<br>import gradio as gr<br># langchain &amp; loaders<br>from langchain.document_loaders import PyPDFLoader   <br>from langchain.text_splitter import RecursiveCharacterTextSplitter<br>from langchain.chains import ConversationalRetrievalChain<br>from langchain.schema import AIMessage, HumanMessage<br><br># embeddings (LangChain community BGE wrapper)<br>from langchain_community.embeddings import HuggingFaceBgeEmbeddings<br><br># vector store (Chroma)<br>from langchain.vectorstores import Chroma<br><br># Ollama LLM wrapper<br>from langchain_ollama import ChatOllama  # conversational wrapper <br><br># utilities<br>from langchain.schema import Document<br><br>def index_pdfs_to_chroma(pdf_dir: str, persist_dir: str, bge_device: str = &quot;cpu&quot;):<br>    pdf_dir = Path(pdf_dir)<br>    assert pdf_dir.exists(), f&quot;{pdf_dir} not found&quot;<br><br>    # 1) load all PDFs<br>    docs = []<br>    for pdf_path in pdf_dir.glob(&quot;*.pdf&quot;):<br>        print(&quot;Loading&quot;, pdf_path.name)<br>        loader = PyPDFLoader(str(pdf_path))<br>        pages = loader.load()<br>        docs.extend(pages)<br><br>    # 2) split into chunks<br>    text_splitter = RecursiveCharacterTextSplitter(<br>        chunk_size=1000,<br>        chunk_overlap=200,<br>    )<br>    print(&quot;Splitting into chunks...&quot;)<br>    docs = text_splitter.split_documents(docs)<br><br>    # 3) create BGE embeddings (use CPU or cuda)<br>    # model_name can be &quot;BAAI/bge-small-en-v1.5&quot;<br>    model_name = &quot;BAAI/bge-small-en-v1.5&quot;<br>    model_kwargs = {&quot;device&quot;: bge_device}<br>    encode_kwargs = {&quot;normalize_embeddings&quot;: True}<br>    print(&quot;Creating HuggingFaceBgeEmbeddings (this will load the model)...&quot;)<br>    bge = HuggingFaceBgeEmbeddings(<br>        model_name=model_name,<br>        model_kwargs=model_kwargs,<br>        encode_kwargs=encode_kwargs,<br>    )<br><br>    # 4) create/persist Chroma DB<br>    print(&quot;Creating Chroma vectorstore (persisted)...&quot;)<br>    vectordb = Chroma.from_documents(<br>        documents=docs,<br>        embedding=bge,<br>        persist_directory=persist_dir,<br>        collection_name=&quot;pdfs_collection&quot;<br>    )<br>    vectordb.persist()<br>    print(&quot;Indexing complete. persisted to&quot;, persist_dir)<br>    return vectordb<br><br>def make_qa_chain(vectordb, ollama_model_name=&quot;llama3.2:1b&quot;):<br>    # create retriever<br>    retriever = vectordb.as_retriever(search_type=&quot;similarity&quot;, search_kwargs={&quot;k&quot;: 4})<br>   <br>    llm = ChatOllama(model=ollama_model_name, base_url=None)  # base_url=&quot;http://localhost:11434&quot; if needed<br>    # ConversationalRetrievalChain wraps the LLM + retriever<br>    chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, return_source_documents=True)<br>    return chain<br><br>PDF_DIR = &quot;./docs&quot;           # folder containing your PDFs<br>PERSIST_DIR = &quot;./chroma_persist&quot;<br>chat_history = []<br><br># set to &#39;cuda&#39; if you have GPU and BGE on GPU, else &#39;cpu&#39;<br>BGE_DEVICE = &quot;cpu&quot;<br><br># 1) Index PDFs to Chroma (first-run)<br>vectordb = index_pdfs_to_chroma(PDF_DIR, PERSIST_DIR, bge_device=BGE_DEVICE)<br><br># 2) Build chain<br>qa_chain = make_qa_chain(vectordb, ollama_model_name=&quot;llama3.2&quot;)<br><br>def chat(question, history):<br>    history_langchain_format = []<br>    for msg in history:<br>        if msg[&#39;role&#39;] == &quot;user&quot;:<br>            history_langchain_format.append(HumanMessage(content=msg[&#39;content&#39;]))<br>        elif msg[&#39;role&#39;] == &quot;assistant&quot;:<br>            history_langchain_format.append(AIMessage(content=msg[&#39;content&#39;]))<br>    history_langchain_format.append(HumanMessage(content=question))<br><br>    result = qa_chain.invoke({&quot;question&quot;: question,&quot;chat_history&quot;:history_langchain_format})<br>    sources = result.get(&quot;source_documents&quot;, [])<br>    answer = result[&quot;answer&quot;]<br>    print(&quot;\nAssistant:&quot;, answer, &quot;\n&quot;)<br>    if sources:<br>        print(&quot;Top source chunks (text snippets):&quot;)<br>        for i, doc in enumerate(sources[:3], start=1):<br>            print(f&quot;--- source {i} (metadata: {doc.metadata}):&quot;)<br>            snippet = doc.page_content<br>            print(snippet[:600].strip(), &quot;\n---\n&quot;) <br>    <br>    return answer<br><br> <br>if __name__ == &quot;__main__&quot;:<br>    view = gr.ChatInterface(chat,type=&quot;messages&quot;).launch(inbrowser=True)</pre><p>This creates a ChromaDB store called /chroma_persist and the PDFs are kept in a /docs folder. We can now test out our simple RAG implemenation.</p><h3>Testing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hWbQaUf-EFG1EBJ3wnQong.png" /></figure><p>As you can see, the llm can now answer questions directly related to the pdfs. For context, the pdf questioned above is shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*nj2WL_BbstQ1mEvH7j90TA.png" /></figure><p>We can add further questions to test it out even more.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qtGBDAJUPKeOiNXkDd_KQQ.png" /></figure><p>We see the conversation history is being used in the context and correctly answers who the customer is and who served the customer. All code is available at <a href="https://github.com/tobintom/llm_rag-langchain-chroma">https://github.com/tobintom/llm_rag-langchain-chroma</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=685e916ea4b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a quick opensource chatbot with Llama and Gradio]]></title>
            <link>https://medium.com/@tobintom/building-a-quick-opensource-chatbot-with-llama-and-gradio-5d8129c123bb?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d8129c123bb</guid>
            <category><![CDATA[llm-function-calling]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[gradio]]></category>
            <category><![CDATA[llama-3]]></category>
            <category><![CDATA[ollama]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Sun, 10 Aug 2025 00:32:31 GMT</pubDate>
            <atom:updated>2025-08-10T00:32:31.002Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building a quick opensource personal assistant chatbot with Llama 3.2, llm function calling and Gradio</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*CpOWjyxRgtXNm0y1ayehFQ.png" /></figure><p>Generative AI has come a long way with multiple large and small language models that can be leveraged in various ways to boost productivity. In this article we will build a very simple chatbot that uses an opensource llama model, uses llm function calling to get specific information regarding our chatbot’s context and build the UI with gradio.</p><h3>Hosting llama3.2 locally</h3><p>As a first step, we will host the llama open source model (<a href="https://www.llama.com/models/llama-3/">https://www.llama.com/models/llama-3/</a>) from meta using Ollama. You can install Ollama from <a href="https://ollama.com/download">https://ollama.com/download</a>. After installing Ollama, you can pull the model you want with a simple Ollama command:</p><pre>ollama pull llama3.2</pre><p>llama also has other variant of 3.2, like llama3.2:1b, which is a slimmer version and runs better on your system, if it does not have sufficient resources. After downloading the model, you can run it with:</p><pre>ollama run llama3.2</pre><p>This runs the llama3.2 model on your local system and by default creates an endpoint based at <a href="http://localhost:11434/v1">http://localhost:11434/v1</a>. We can now build a simple chatbot with this interface.</p><h3>Creating the Chatbot</h3><p>The chatbot is supposed to be a personal assistant. We need to give the llm a system context that explains in what context the user’s prompts need to be answered in.</p><p>The initial code of the initial version of the chatbot is shown below.</p><pre>import json<br>from openai import OpenAI<br>import gradio as gr<br><br>MODEL = &quot;llama3.2&quot;<br>xapi_key = &quot;ollama&quot;<br>system_prompt = &quot;You are a helpful personal assistant that helps users with questions and plans their day for them. &quot;<br>system_prompt += &quot;Give short, polite answers and always be accurate, if you do not &quot;<br>system_prompt += &quot;know the answer, be polite and say so.&quot;<br><br>def run():<br>    gr.ChatInterface(fn=chat_bot, type=&quot;messages&quot;).launch()<br><br>def chat_bot(user_prompt, history):    <br>    messages = [{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: system_prompt}] + history + [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: user_prompt}]<br>    ollama = OpenAI(base_url=&#39;http://localhost:11434/v1&#39;, api_key=xapi_key)<br>    response_text = ollama.chat.completions.create(model=MODEL, messages=messages,temperature=0.7, stream=True)<br>    response = &quot;&quot;<br>    for chunk in response_text:<br>        response += chunk.choices[0].delta.content or &#39;&#39;<br>        yield response<br></pre><p>When running llama, you can use the openai library to interface with it by using the default endpoint llama ollama exposes llama at <a href="http://localhost:11434/v1.">http://localhost:11434/v1.</a> You can also use the ollama library as well. We give the system prompt that tells the llm the context it needs to use when answering questions. The questions are provided with the user prompt and we keep the history of the conversation for contextual memory. The chat is then rendered with Gradio. The end result is a very simple opensource chatbot powered by llama3.2. You can now run the implementation to test it out (the Gradio interface will be available on the browser at <a href="http://127.0.0.1:7860/">http://127.0.0.1:7860/</a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*e8WxMZBUt-ZL5Yx5.gif" /></figure><p>Now that our chatbot is working, let us ask it some questions you would ask your personal assistant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rYkzSkffmO8SKzwwwkbC6Q.png" /></figure><p>As you can see, the llm cannot find information that is realtime. It only has the information it was trained on. Fortunately, with llm tools you can provide it with more contextual information to help it answer questions better. An example of an llm tool, is llm function calling that can be used to call an API for data. To do this, we have to describe a tool (or mutiple tools) and give context on when they should be called. The LLM invokes them in the right context to give a better answer.</p><p>First we will create two llm function calls; one to get the current weather and the other to get the user’s schedule. For the purpose of this demo, the schedule is a hard coded response.</p><pre>def getWeather(city=&quot;dallas&quot;):<br>    resp = requests.get(&quot;https://goweather.xyz/weather/&quot; + str(city))<br>    if(resp.status_code == 200):<br>        return resp.json()<br>    else:<br>        return {&quot;error&quot;:&quot;Could not get the weather&quot;}<br><br>def getSchedule(date):<br>    return {<br>        &quot;09:00&quot; : &quot;Meeting with David to plan Martin&#39;s birthday&quot;,<br>        &quot;13:00&quot; : &quot;Lunch with Andy Sauer&quot;,<br>        &quot;17:00&quot; : &quot;Take Arnold to soccer class&quot;<br>    }</pre><p>Now we need to define these functions and the context the llm needs to use to call them:</p><pre>schedule_function = {<br>    &quot;name&quot;: &quot;getSchedule&quot;,<br>    &quot;description&quot;: &quot;Get the schedule of the user for the provided date. Use this function to know the user&#39;s planned day and when the user wants to know the schedule for the day.&quot;,<br>    &quot;parameters&quot;: {<br>        &quot;type&quot;: &quot;object&quot;,<br>        &quot;properties&quot;: {<br>            &quot;date&quot;: {<br>                &quot;type&quot;: &quot;string&quot;,<br>                &quot;description&quot;: &quot;The date of the schedule&quot;,<br>            },<br>        },<br>        &quot;required&quot;: [&quot;date&quot;],<br>        &quot;additionalProperties&quot;: False<br>    }<br>}<br><br>weather_function = {<br>    &quot;name&quot;: &quot;getWeather&quot;,<br>    &quot;description&quot;: &quot;Get the current weather of a given location. Use this function to answer any question that may need information about the weather. For example any questions regarding advice on what to wear, outdoor activities.&quot;,<br>    &quot;parameters&quot;: {<br>        &quot;type&quot;: &quot;object&quot;,<br>        &quot;properties&quot;: {<br>            &quot;city&quot;: {<br>                &quot;type&quot;: &quot;string&quot;,<br>                &quot;description&quot;: &quot;The city that the weather information is needed&quot;,<br>            },<br>        },<br>        &quot;required&quot;: [&quot;city&quot;],<br>        &quot;additionalProperties&quot;: False<br>    }<br>}</pre><p>With the definitions complete, we can now associate them to the llm as tools.</p><pre>tools = [{&quot;type&quot;: &quot;function&quot;, &quot;function&quot;: weather_function}, {&quot;type&quot;: &quot;function&quot;, &quot;function&quot;: schedule_function}]<br>response_text = ollama.chat.completions.create(model=MODEL, messages=messages,temperature=0.7, tools=tools, tool_choice=&quot;auto&quot;)</pre><p>The last part is to handle the function calls. Whenever the LLM decides to invoke the function tool, the tool’s response needs to be added back as context to the llm to answer the original user prompt.</p><pre>response_text = ollama.chat.completions.create(model=MODEL, messages=messages,temperature=0.7, tools=tools, tool_choice=&quot;auto&quot;)<br>    <br>    if response_text.choices[0].message.tool_calls:<br>        tool_call = response_text.choices[0].message.tool_calls[0]         <br>        if tool_call.function.name == &quot;getWeather&quot;:<br>            # Extract parameters<br>            args = eval(tool_call.function.arguments)<br>            <br>            city = args.get(&#39;city&#39;)<br>            result = getWeather(city)<br>            <br>            response = {<br>                &quot;role&quot;: &quot;tool&quot;,<br>                &quot;content&quot;: json.dumps(result),<br>                &quot;tool_call_id&quot;: tool_call.id<br>            }             <br>            messages.append(response)            <br>            # Send the function&#39;s result back to the LLM for final answer<br>            response_text = ollama.chat.completions.create(<br>                model=MODEL,<br>                messages=messages<br>            )<br>        elif tool_call.function.name == &quot;getSchedule&quot;:<br>            # Extract parameters<br>            args = eval(tool_call.function.arguments)<br>            <br>            date = args.get(&#39;date&#39;)<br>            result = getSchedule(date)<br>             <br>            response = {<br>                &quot;role&quot;: &quot;tool&quot;,<br>                &quot;content&quot;: json.dumps(result),<br>                &quot;tool_call_id&quot;: tool_call.id<br>            }             <br>            messages.append(response)            <br>            # Send the function&#39;s result back to the LLM for final answer<br>            response_text = ollama.chat.completions.create(<br>                model=MODEL,<br>                messages=messages<br>            )<br><br><br>    return (response_text.choices[0].message.content)</pre><p>The full implementation looks something like:</p><pre>from enum import auto<br>import json<br>from types import CoroutineType<br>from openai import OpenAI<br>import gradio as gr<br>import requests<br>from datetime import datetime<br><br>MODEL = &quot;llama3.2&quot;<br>xapi_key = &quot;ollama&quot;<br>system_prompt = &quot;You are a helpful personal assistant that helps users with questions and plans their day for them. &quot;<br>system_prompt += &quot;Give short, polite answers and always be accurate.&quot;<br><br>schedule_function = {<br>    &quot;name&quot;: &quot;getSchedule&quot;,<br>    &quot;description&quot;: &quot;Get the schedule of the user for the provided date. Use this function to know the user&#39;s planned day and when the user wants to know the schedule for the day.&quot;,<br>    &quot;parameters&quot;: {<br>        &quot;type&quot;: &quot;object&quot;,<br>        &quot;properties&quot;: {<br>            &quot;date&quot;: {<br>                &quot;type&quot;: &quot;string&quot;,<br>                &quot;description&quot;: &quot;The date of the schedule&quot;,<br>            },<br>        },<br>        &quot;required&quot;: [&quot;date&quot;],<br>        &quot;additionalProperties&quot;: False<br>    }<br>}<br><br>weather_function = {<br>    &quot;name&quot;: &quot;getWeather&quot;,<br>    &quot;description&quot;: &quot;Get the current weather of a given location. Use this function to answer any question that may need information about the weather. For example any questions regarding advice on what to wear, outdoor activities.&quot;,<br>    &quot;parameters&quot;: {<br>        &quot;type&quot;: &quot;object&quot;,<br>        &quot;properties&quot;: {<br>            &quot;city&quot;: {<br>                &quot;type&quot;: &quot;string&quot;,<br>                &quot;description&quot;: &quot;The city that the weather information is needed&quot;,<br>            },<br>        },<br>        &quot;required&quot;: [&quot;city&quot;],<br>        &quot;additionalProperties&quot;: False<br>    }<br>}<br><br>tools = [{&quot;type&quot;: &quot;function&quot;, &quot;function&quot;: weather_function}, {&quot;type&quot;: &quot;function&quot;, &quot;function&quot;: schedule_function}]<br><br>def run():<br>    gr.ChatInterface(fn=chat_bot, type=&quot;messages&quot;).launch()<br><br>def chat_bot(user_prompt, history):   <br>    now = datetime.now() <br>    messages = [{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: system_prompt}] + history + [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: user_prompt}]<br>    ollama = OpenAI(base_url=&#39;http://localhost:11434/v1&#39;, api_key=xapi_key)<br>    response_text = ollama.chat.completions.create(model=MODEL, messages=messages,temperature=0.7, tools=tools, tool_choice=&quot;auto&quot;)<br>    <br>    if response_text.choices[0].message.tool_calls:<br>        tool_call = response_text.choices[0].message.tool_calls[0]         <br>        if tool_call.function.name == &quot;getWeather&quot;:<br>            # Extract parameters<br>            args = eval(tool_call.function.arguments)<br>            <br>            city = args.get(&#39;city&#39;)<br>            result = getWeather(city)<br>            <br>            response = {<br>                &quot;role&quot;: &quot;tool&quot;,<br>                &quot;content&quot;: json.dumps(result),<br>                &quot;tool_call_id&quot;: tool_call.id<br>            }             <br>            messages.append(response)            <br>            # Send the function&#39;s result back to the LLM for final answer<br>            response_text = ollama.chat.completions.create(<br>                model=MODEL,<br>                messages=messages<br>            )<br>        elif tool_call.function.name == &quot;getSchedule&quot;:<br>            # Extract parameters<br>            args = eval(tool_call.function.arguments)<br>            <br>            date = args.get(&#39;date&#39;)<br>            result = getSchedule(date)<br>             <br>            response = {<br>                &quot;role&quot;: &quot;tool&quot;,<br>                &quot;content&quot;: json.dumps(result),<br>                &quot;tool_call_id&quot;: tool_call.id<br>            }             <br>            messages.append(response)            <br>            # Send the function&#39;s result back to the LLM for final answer<br>            response_text = ollama.chat.completions.create(<br>                model=MODEL,<br>                messages=messages<br>            )<br><br><br>    return (response_text.choices[0].message.content)<br>    <br>    <br>    # response = &quot;&quot;<br>    # for chunk in response_text:<br>    #     response += chunk.choices[0].delta.content or &#39;&#39;<br>    #     yield response<br><br><br>def getWeather(city=&quot;dallas&quot;):<br>    resp = requests.get(&quot;https://goweather.xyz/weather/&quot; + str(city))<br>    if(resp.status_code == 200):<br>        return resp.json()<br>    else:<br>        return {&quot;error&quot;:&quot;Could not get the weather&quot;}<br><br>def getSchedule(date):<br>    return {<br>        &quot;09:00&quot; : &quot;Meeting with David to plan Martin&#39;s birthday&quot;,<br>        &quot;13:00&quot; : &quot;Lunch with Andy Sauer&quot;,<br>        &quot;17:00&quot; : &quot;Take Arnold to soccer class&quot;<br>    }<br><br></pre><p>We can now test out the chatbot and ensure the llm functions are being invoked correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nVxX3nTOp-keG9pOdCODeQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fFGYW_BTbrBx_SJ-yZg-XA.png" /></figure><p>As you can see now, the llm has the context for weather and uses that when answering questions about the weather and also when answering questions about what to wear. In the llm function definition, we had given context to use the function for fashion advice.</p><p>Similarly our schedule function also gets correctly invoked when needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BI2xct2JSiTy8zXJBgPa2w.png" /></figure><p>This is a very simple and basic demo on using your own chatbot. This simple setup can be used to create your own business customer service chatbot with an array of function calls to answer specific questions about the services you provide.</p><p>All code can be found at <a href="https://github.com/tobintom/llama_function_call">https://github.com/tobintom/llama_function_call</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d8129c123bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Spring Boot with GraphQL]]></title>
            <link>https://medium.com/@tobintom/introducing-spring-boot-with-graphql-4043cd59f28d?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/4043cd59f28d</guid>
            <category><![CDATA[services]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Tue, 22 Jul 2025 23:02:28 GMT</pubDate>
            <atom:updated>2025-07-22T23:02:28.339Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T4x-7hDVPZs2wG59gB7yVg.png" /></figure><p>GraphQL is a query language for APIs and it is also a runtime for extracting data with queries against your existing data. Traditional REST APIs have a resource that is served based to a request. The response is typically pre-defined and serves a particular purpose. E.g. /getUserById would take an ID as input and serve the User details, /getUserAddressById would take an ID and serve the User Address etc.</p><p>With GraphQL (<a href="https://graphql.org/">https://graphql.org/</a>), you can obtain data from different data sources in a single operation. It is served using ‘queries’ (equivalent to GET in REST services) and ‘mutations’ (equivalent to POST or DELETE in REST services). GraphQL allows callers to request the exact data elements they require which is not possible with a traditional REST service. GraphQL therefore reduces under-fetching and over-fetching of data. In this example, we are going to use a MySQL database to hold User and User_Detail information, use SpringBoot and GraphQL to query, modify and insert data.</p><h3>Database Setup</h3><p>I will be using a MySQL instance installed on my local machine. Setting up the Database, schema and tables.</p><pre>CREATE DATABASE emp_db;<br><br>USE emp_db;<br><br>CREATE TABLE user(<br>  id INT AUTO_INCREMENT PRIMARY KEY,<br>  username VARCHAR(50) NOT NULL,<br>  email VARCHAR(100) NOT NULL UNIQUE,<br>  fullname VARCHAR(100) NOT NULL<br>);<br><br>CREATE TABLE user_emp (<br>  id INT NOT NULL,<br>  role VARCHAR(50),<br>  department VARCHAR(100),<br>  organization VARCHAR(100),<br>  PRIMARY KEY (id),<br>  FOREIGN KEY (id) REFERENCES user(id)<br>    ON DELETE CASCADE<br>    ON UPDATE CASCADE<br>);<br><br>INSERT INTO emp_db.user (id, username, email, fullname) VALUES<br>(1, &#39;c8001&#39;, &#39;john.doe@email.com&#39;, &#39;John Doe&#39;),<br>(2, &#39;c8002&#39;, &#39;jane.smith@email.com&#39;, &#39;Jane Smith&#39;);<br><br>INSERT INTO emp_db.user_emp (id, role, department, organization) VALUES<br>(1, &#39;Senior Developer&#39;, &#39;Tech&#39;, &#39;Tech Corp&#39;),<br>(2, &#39;HR Manager&#39;, &#39;Human Resources&#39;, &#39;Global Inc&#39;);</pre><h3>Application Setup</h3><p>A Springboot application with dependencies on Spring Web, graphQL, Spring Data JPA and MySQL db driver needs to be created.</p><p><em>pom.xml</em></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;<br>         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;<br>    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;<br>    &lt;groupId&gt;com.example&lt;/groupId&gt;<br>    &lt;artifactId&gt;graphql-demo&lt;/artifactId&gt;<br>    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;<br>    &lt;parent&gt;<br>        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;<br>        &lt;version&gt;3.2.0&lt;/version&gt; <br>    &lt;/parent&gt;<br>    &lt;properties&gt;<br>       &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;<br>       &lt;java.version&gt;21&lt;/java.version&gt;<br>       &lt;maven.compiler.release&gt;${java.version}&lt;/maven.compiler.release&gt;<br>   &lt;/properties&gt;<br>    &lt;dependencies&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>            &lt;artifactId&gt;spring-boot-starter-graphql&lt;/artifactId&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br>  &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>            &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;com.mysql&lt;/groupId&gt;<br>            &lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;<br>            &lt;scope&gt;runtime&lt;/scope&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;<br>            &lt;scope&gt;test&lt;/scope&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>          &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;<br>          &lt;artifactId&gt;lombok&lt;/artifactId&gt; <br>          &lt;scope&gt;provided&lt;/scope&gt;<br>       &lt;/dependency&gt;<br>    &lt;/dependencies&gt;<br>    &lt;build&gt;<br>        &lt;plugins&gt;<br>            &lt;plugin&gt;<br>                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>            &lt;/plugin&gt;<br>        &lt;/plugins&gt;<br>    &lt;/build&gt;<br>&lt;/project&gt;</pre><p><em>schema.graphqls </em>(to be kept in src/main/resources/graphql folder)</p><pre><br>type User {<br>    id: Int!<br>    username: String!<br>    email: String!<br>    fullname: String!<br>    userEmp: UserEmp<br>}<br><br>type UserEmp {<br>    id: Int!<br>    role: String<br>    department: String<br>    organization: String<br>}<br><br>input CreateUserInput {<br> id: Int!<br>    username: String!<br>    email: String!<br>    fullname: String!<br>    userEmp: CreateUserEmpInput<br>}<br><br>input CreateUserEmpInput {<br>    id: Int!<br>    role: String<br>    department: String<br>    organization: String<br>}<br><br>input UpdateUserInput {<br>    id: Int!<br>    username: String<br>    email: String<br>    fullname: String<br>    userEmp: UpdateUserEmpInput<br>}<br><br>input UpdateUserEmpInput {<br>    role: String<br>    department: String<br>    organization: String<br>}<br><br>type Query {<br>    user(id: Int!): User<br>}<br><br>type Mutation {<br>    createUser(input: CreateUserInput!): User!<br>    updateUser(input: UpdateUserInput!): User!<br>}</pre><p><em>application.properties</em></p><pre>server.port=8080<br>spring.datasource.url=jdbc:mysql://localhost:3306/emp_db<br>spring.datasource.username=ID_OF_DB<br>spring.datasource.password=PASSWORD_OF_DB<br>spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver<br>spring.jpa.hibernate.ddl-auto=update<br>spring.graphql.graphiql.enabled=true<br>spring.graphql.graphiql.path=/graphiql<br>spring.jpa.show-sql=true<br>logging.level.org.hibernate.SQL=DEBUG<br>logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE</pre><p><em>DemoApplication.java</em></p><pre>package com.example.graphqldemo;<br><br>import org.springframework.boot.SpringApplication;<br>import org.springframework.boot.autoconfigure.SpringBootApplication;<br><br>@SpringBootApplication<br>public class DemoApplication {<br> public static void main(String[] args) {<br>  SpringApplication.run(DemoApplication.class,args);<br> }<br>}</pre><p>Models used for the application. The DB structure has the User as the main table and User_Emp as a child table. The User object has a userEmp child object.</p><p><em>User.java</em></p><pre>package com.example.graphqldemo.model;<br><br>import jakarta.persistence.CascadeType;<br>import jakarta.persistence.Entity;<br>import jakarta.persistence.FetchType;<br>import jakarta.persistence.Id;<br>import jakarta.persistence.OneToOne;<br>import jakarta.persistence.Table;<br><br>@Entity<br>@Table(name = &quot;user&quot;)<br>public class User {<br>    @Id<br>    private Integer id;<br>    private String username;<br>    private String email;<br>    private String fullname;<br><br>    @OneToOne(mappedBy = &quot;user&quot;,cascade = CascadeType.ALL, fetch = FetchType.LAZY,optional = true)<br>    private UserEmp userEmp;<br><br>    // Constructors<br>    public User() {}<br><br>    public User(String username, String email, String fullname) {<br>        this.username = username;<br>        this.email = email;<br>        this.fullname = fullname;<br>    }<br><br>    // Getters and Setters<br>    public Integer getId() { return id; }<br>    public void setId(Integer id) { this.id = id; }    <br>    public String getUsername() {return username;}<br> public void setUsername(String username) {this.username = username; }<br><br> public String getEmail() {<br>  return email;<br> }<br><br> public void setEmail(String email) {<br>  this.email = email;<br> }<br><br> public String getFullname() { return fullname; }<br>    public void setFullname(String fullname) { this.fullname = fullname; }<br>    public UserEmp getUserEmp() { return userEmp; }<br>    public void setUserEmp(UserEmp userEmp) { this.userEmp = userEmp; }<br>}</pre><p><em>UserEmp.java</em></p><pre>package com.example.graphqldemo.model;<br><br>import jakarta.persistence.Entity;<br>import jakarta.persistence.Id;<br>import jakarta.persistence.JoinColumn;<br>import jakarta.persistence.MapsId;<br>import jakarta.persistence.OneToOne;<br>import jakarta.persistence.Table;<br><br>@Entity<br>@Table(name = &quot;user_emp&quot;)<br>public class UserEmp {<br>    @Id<br>    private Integer id;<br><br>    @OneToOne(optional = false)<br>    @MapsId<br>    @JoinColumn(name = &quot;id&quot;, referencedColumnName = &quot;id&quot;,nullable = false, unique = true)<br>    private User user;<br><br>    private String role;<br>    private String department;<br>    private String organization;<br><br>    // Constructors<br>    public UserEmp() {}<br><br>    public UserEmp(User user, String role, String department, String organization) {<br>        this.user = user;<br>        this.id = user.getId();<br>        this.role = role;<br>        this.department = department;<br>        this.organization = organization;<br>    }<br><br>    // Getters and Setters<br>    public Integer getId() { return id; }<br>    public void setId(Integer id) { this.id = id; }<br>    public User getUser() { return user; }<br>    public void setUser(User user) { this.user = user; }<br>    public String getRole() { return role; }<br>    public void setRole(String role) { this.role = role; }<br>    public String getDepartment() { return department; }<br>    public void setDepartment(String department) { this.department = department; }<br>    public String getOrganization() { return organization; }<br>    public void setOrganization(String organization) { this.organization = organization; }<br>}</pre><p>The following model objects are used for GraphQL mutation operation DTO.</p><p><em>UpdateUserInput.java</em></p><pre>package com.example.graphqldemo.model;<br><br>import lombok.Data;<br><br>@Data<br>public class UpdateUserInput {<br> private Integer id;<br>    private String username;<br>    private String email;<br>    private String fullname;<br>    private UpdateUserEmpInput userEmp;<br><br>}</pre><p><em>UpdateUserEmpInput.java</em></p><pre>package com.example.graphqldemo.model;<br><br>import lombok.Data;<br><br>@Data<br>public class UpdateUserEmpInput {<br> private String role;<br>    private String department;<br>    private String organization;<br>}</pre><p><em>CreateUserInput.java</em></p><pre>package com.example.graphqldemo.model;<br><br>import lombok.Data;<br>import lombok.Getter;<br>import lombok.Setter;<br><br><br>@Getter<br>@Setter<br>public class CreateUserInput {<br> private Integer id;<br> private String username;<br>    private String email;<br>    private String fullname;<br>    private CreateUserEmpInput userEmp;<br>}</pre><p><em>CreateUserEmpInput.java</em></p><pre>package com.example.graphqldemo.model;<br>import lombok.Data;<br><br>@Data<br>public class CreateUserEmpInput {<br> private String role;<br>    private String department;<br>    private String organization;<br>}</pre><p>The following are the JPA repository classes for the User and UserEmp tables.</p><p><em>UserRepository.java</em></p><pre>package com.example.graphqldemo.repository;<br><br>import com.example.graphqldemo.model.User;<br>import org.springframework.data.jpa.repository.JpaRepository;<br>import org.springframework.data.jpa.repository.Query;<br>import org.springframework.data.repository.query.Param;<br>import org.springframework.stereotype.Repository;<br>import java.util.Optional;<br><br>@Repository<br>public interface UserRepository extends JpaRepository&lt;User, Integer&gt; {<br>    <br>    @Query(&quot;SELECT u FROM User u LEFT JOIN FETCH u.userEmp WHERE u.id = :id&quot;)<br>    Optional&lt;User&gt; findByIdWithEmployeeDetails(@Param(&quot;id&quot;) Integer id);<br>    <br>    boolean existsByUsername(String username);<br>}</pre><p><em>UserEmpRepository.java</em></p><pre>package com.example.graphqldemo.repository;<br><br>import com.example.graphqldemo.model.UserEmp;<br>import org.springframework.data.jpa.repository.JpaRepository;<br>import org.springframework.stereotype.Repository;<br><br>@Repository<br>public interface UserEmpRepository extends JpaRepository&lt;UserEmp, Integer&gt; {<br>}</pre><p><em>UserService.java </em>(Used by the GraphQL resolver to implement the DB operations)</p><pre>package com.example.graphqldemo.service;<br><br>import com.example.graphqldemo.model.*;<br>import com.example.graphqldemo.repository.UserRepository;<br>import com.example.graphqldemo.repository.UserEmpRepository;<br>import lombok.RequiredArgsConstructor;<br>import org.springframework.stereotype.Service;<br>import org.springframework.transaction.annotation.Transactional;<br><br>@Service<br>@RequiredArgsConstructor<br>public class UserService {<br><br> private final UserRepository userRepository;<br>    private final UserEmpRepository userEmpRepository;<br>    <br>    @Transactional<br>    public User createUser(CreateUserInput input) {<br>        <br>        if (userRepository.existsByUsername(input.getUsername())) {<br>            throw new RuntimeException(&quot;Username already exists: &quot; + input.getUsername());<br>        }<br>        <br>        // Create user<br>        User user = new User();<br>        user.setId(input.getId());<br>        user.setUsername(input.getUsername());<br>        user.setEmail(input.getEmail());<br>        user.setFullname(input.getFullname());<br>                <br>        // Save user first to get the ID<br>        user = userRepository.save(user);   <br>        <br>        // Create employee details if provided<br>        if (input.getUserEmp() != null) {<br>            UserEmp userEmp = new UserEmp();             <br>            userEmp.setRole(input.getUserEmp().getRole());<br>            userEmp.setDepartment(input.getUserEmp().getDepartment());<br>            userEmp.setOrganization(input.getUserEmp().getOrganization());<br>            userEmp.setUser(user);            <br>            userEmpRepository.save(userEmp); <br>            user.setUserEmp(userEmp);<br>        }<br>        <br>        return user;<br>    }<br>    <br>    @Transactional<br>    public User updateUser(UpdateUserInput input) {<br>        User user = userRepository.findById(input.getId())<br>            .orElseThrow(() -&gt; new RuntimeException(&quot;User not found with id: &quot; + input.getId()));<br>        <br>        // Update user fields if provided<br>        if (input.getUsername() != null) {<br>            if (userRepository.existsByUsername(input.getUsername()) &amp;&amp; <br>                !user.getUsername().equals(input.getUsername())) {<br>                throw new RuntimeException(&quot;Username already exists: &quot; + input.getUsername());<br>            }<br>            user.setUsername(input.getUsername());<br>        }      <br>        <br>        <br>        if (input.getFullname() != null) {<br>            user.setFullname(input.getFullname());<br>        }<br>        <br>        if (input.getUsername() != null) {<br>            user.setUsername(input.getUsername());<br>        }<br>        if (input.getEmail() != null) {<br>            user.setEmail(input.getEmail());<br>        }<br>        <br>        // Save user changes<br>        user = userRepository.save(user);<br>        <br>        // Update employee details if provided<br>        if (input.getUserEmp() != null) {<br>            UserEmp userEmp = user.getUserEmp();<br>            <br>            if (userEmp == null) {<br>                // Create new employee details<br>                userEmp = new UserEmp();<br>                userEmp.setId(user.getId());<br>                userEmp.setUser(user);<br>            }<br>            <br>            // Update fields if they are provided (not null)<br>            if (input.getUserEmp().getRole() != null) {<br>                userEmp.setRole(input.getUserEmp().getRole());<br>            }<br>            if (input.getUserEmp().getDepartment() != null) {<br>                userEmp.setDepartment(input.getUserEmp().getDepartment());<br>            }<br>            if (input.getUserEmp().getOrganization() != null) {<br>                userEmp.setOrganization(input.getUserEmp().getOrganization());<br>            }<br>            <br>            userEmpRepository.save(userEmp);<br>            user.setUserEmp(userEmp);<br>        }<br>        <br>        return userRepository.findByIdWithEmployeeDetails(user.getId()).orElse(user);<br>    }<br>    <br>    public User getUserById(Integer id) {<br>        return userRepository.findByIdWithEmployeeDetails(id)<br>            .orElseThrow(() -&gt; new RuntimeException(&quot;User not found with id: &quot; + id));<br>    }<br>}</pre><p>The GraphQL resolver class <em>UserController.java</em></p><pre>package com.example.graphqldemo.controller;<br> <br>import lombok.RequiredArgsConstructor;<br>import org.springframework.graphql.data.method.annotation.Argument;<br>import org.springframework.graphql.data.method.annotation.MutationMapping;<br>import org.springframework.graphql.data.method.annotation.QueryMapping;<br>import org.springframework.stereotype.Controller;<br><br>import com.example.graphqldemo.model.CreateUserInput;<br>import com.example.graphqldemo.model.UpdateUserInput;<br>import com.example.graphqldemo.model.User;<br>import com.example.graphqldemo.service.UserService;<br><br>@Controller<br>@RequiredArgsConstructor<br>public class UserController {<br>    <br>    private final UserService userService;<br>    <br>    @QueryMapping<br>    public User user(@Argument Integer id) {      <br>        return userService.getUserById(id);<br>    }<br>    <br>    @MutationMapping<br>    public User createUser(@Argument CreateUserInput input) {<br>        return userService.createUser(input);<br>    }<br>    <br>    @MutationMapping<br>    public User updateUser(@Argument UpdateUserInput input) {<br>        return userService.updateUser(input);<br>    }<br>}</pre><p>After building and running the application, the interface is accessible at <a href="http://localhost:8080/graphiql."><em>http://localhost:8080/graphiql.</em></a></p><h3>Application Testing</h3><p><strong>Retrieval:</strong></p><p>We can test the retrieval of the existing records in the DB by running a query against the application url using the user(:id) query (defined in the graphql schema) as follows:</p><pre>query {<br>  user(id: 1) {<br>    id<br>    username<br>    email<br>    fullname<br>    userEmp {<br>      id<br>      role<br>      department<br>      organization<br>    }<br>  }<br>}</pre><p>This returns the requested parameters for User ID 1.</p><pre>{<br>    &quot;data&quot;: {<br>        &quot;user&quot;: {<br>            &quot;id&quot;: 1,<br>            &quot;username&quot;: &quot;c8001&quot;,<br>            &quot;email&quot;: &quot;john.doe@email.com&quot;,<br>            &quot;fullname&quot;: &quot;John Doe&quot;,<br>            &quot;userEmp&quot;: {<br>                &quot;id&quot;: 1,<br>                &quot;role&quot;: &quot;Senior Developer&quot;,<br>                &quot;department&quot;: &quot;Tech&quot;,<br>                &quot;organization&quot;: &quot;Tech Corp&quot;<br>            }<br>        }<br>    }<br>}</pre><p>If you change the parameters in the query, only the requested attributes are returned in the results. Thus you can re-use the same query to fetch only the required data for your use case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*Bg0OAgTKJ1wy0Fdnp7MKDQ.png" /></figure><p><strong>Create:</strong></p><p>We can use the mutation defined for createUser() to create a new entry into the Database. This is done by invoking the createUser() mutation as shown below:</p><pre>mutation CreateUser($input: CreateUserInput!) { <br>    createUser(input: $input) {<br>        id<br>        username<br>        email<br>        fullname<br>        userEmp{<br>            id<br>            role<br>            department<br>            organization<br>        }<br>    }<br>}</pre><p>The variables need to be defined for the operation as well:</p><pre>{<br>  &quot;input&quot;: {<br>    &quot;id&quot;: 3,<br>    &quot;username&quot;: &quot;C90111&quot;,<br>    &quot;email&quot;: &quot;run.doe@email.com&quot;,<br>    &quot;fullname&quot;: &quot;Run Dow&quot;,<br>    &quot;userEmp&quot;:{<br>        &quot;id&quot;: 3,<br>        &quot;role&quot;: &quot;Mechanical Supervisor&quot;,<br>        &quot;department&quot;: &quot;Engineering&quot;,<br>        &quot;organization&quot;: &quot;RZ Inc&quot;<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*31WT0cGm7g-bhBfTPTOuAg.png" /><figcaption>GraphQL Create Mutation</figcaption></figure><p>This operation creates a new record in the database as defined by the variables of the mutation.</p><p><strong>Update:</strong></p><p>Similarly we can use the UpdateUser() mutation to update an existing record in the DB.</p><pre>mutation UpdateUser($input: UpdateUserInput!) { <br>    updateUser(input: $input) {<br>        id<br>        username<br>        email<br>        fullname<br>        userEmp{<br>            role<br>            department<br>            organization<br>        }<br>    }<br>}</pre><pre>{<br>  &quot;input&quot;: {<br>    &quot;id&quot;: 3,<br>    &quot;username&quot;: &quot;C90112&quot;,<br>    &quot;email&quot;: &quot;run.doe@email.com&quot;,<br>    &quot;fullname&quot;: &quot;Run Dow&quot;,<br>    &quot;userEmp&quot;:{<br>        <br>        &quot;role&quot;: &quot;Mechanical Supervisor&quot;,<br>        &quot;department&quot;: &quot;Engineering&quot;,<br>        &quot;organization&quot;: &quot;RZ Inc&quot;<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*ldcLDeee2tXQyXXFKjsd4Q.png" /><figcaption>GraphQL Update mutation</figcaption></figure><p>This will update the record in the DB and the mutations can be verified by running the retrieval query to validate the DB changes were successfully implemented.</p><p>All source code is available at <a href="https://github.com/tobintom/graphqldemo">https://github.com/tobintom/graphqldemo</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4043cd59f28d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Apache OpenNLP with Spring Boot to create a Chat Application]]></title>
            <link>https://medium.com/@tobintom/using-apache-opennlp-with-spring-boot-to-create-a-chat-application-551895c7d3f6?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/551895c7d3f6</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[apache-opennlp]]></category>
            <category><![CDATA[opennlp]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Mon, 16 Oct 2023 05:58:01 GMT</pubDate>
            <atom:updated>2023-10-16T05:58:01.404Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*PDerZlmR_fyOopR9O3L63g.png" /></figure><p>Apache OpenNLP (<a href="https://opennlp.apache.org/docs/2.3.0/manual/opennlp.html">https://opennlp.apache.org</a>) is a machine learning based library for processing natural language text. It has a set of APIs for use cases like Language Detection, Named Entity Recognition, Sentence Detection, Detecting Parts of Speech etc. In this tutorial we will build a Spring Boot application that uses Apache OpenNLP to perform a few of these tasks and expose a service endpoint for a chat. Apache OpenNLP use-cases may require trained models to fulfill its APIs, you can download the pre-built models here (<a href="https://opennlp.apache.org/models.html">https://opennlp.apache.org/models.html</a>).</p><p>Before we build the chat service, we will go over some of the functional capabilities of Apache OpenNLP.</p><h3>1. Language Detection</h3><p>The OpenNLP Language Detector classifies a document in ISO-639–3 languages according to the model capabilities. In essence, it can read text and determine the post probability of the language it is in. To enable this language classification, it needs a language model. In this example we will use the model from <a href="https://opennlp.apache.org/models.html">https://opennlp.apache.org/models.html</a> (langdetect-183.bin)</p><p>We will write a small program that takes text as input, feeds it to the API and it will output the language detected.</p><pre> InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;langdetect-183.bin&quot;); <br>//langdetect-183.bin is the model downloaded<br> LanguageDetectorModel langModel = new LanguageDetectorModel(is); <br> String input = &quot;This is an example of text in English &quot;; <br> LanguageDetector langDetector = new LanguageDetectorME(langModel); <br> Language language = langDetector.predictLanguage(input); <br><br> System.out.println(&quot;Language Detected: &quot; + language.getLang() +&quot; Confidence Score: &quot; + language.getConfidence());<br><br> Language[] languages = langDetector.predictLanguages(input);<br> System.out.println(&quot;Language Possibilities: &quot; + Arrays.toString(languages));</pre><p>In this example, we take in an input and detect the most likely language and find its confidence score. We also look at the possibilities of languages detected.</p><p>Output:</p><blockquote>Language Detected: eng Confidence Score: 0.02684587630799388</blockquote><blockquote>Language Possibilities: [eng (0.02684587630799388), lat (0.019378488575466788), cmn (0.018178562099219604), war (0.01722122819288186), nld (0.017201643532576142), deu (0.016945470113149893), tgl (0.016292692379484566), nds (0.016109854942443214), swe (0.015980003976836476), glg (0.01594004639385804), fra (0.01499428555034837), oci (0.014963629189470156), ron (0.014823802051435374), ….]</blockquote><p>From this, we see that <strong>eng</strong> is the language detected with the highest score. In the following output, we see all the other language scores. We can try this with a separate input in a different language:</p><pre>String input = &quot;مرحبا هناك كيف حالك&quot;; </pre><p>Output:</p><blockquote>Language Detected: ara Confidence Score: 0.022397876226584727</blockquote><blockquote>Language Possibilities: [ara (0.022397876226584727), fas (0.019983070460488875), pes (0.019604789556935245), pus (0.01848681421455882), kor (0.013464178670585301),…..</blockquote><p>The language detected now is <strong>ara</strong>(arabic).</p><h3>2. Sentence Detection</h3><p>There is capability to detect sentences and requires the model for the language you need the sentence detection in.</p><pre>InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin&quot;); <br>//opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin is the model for English sentences<br>SentenceModel model= new SentenceModel(is);<br>String input = &quot;Hello, how are you? I am fine, thank you for asking. It is a wonderful day today, isnt it? &quot;;<br>      <br>SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);<br>String sentences[] = sentenceDetector.sentDetect(input);<br>System.out.println(&quot;Number of sentences detected: &quot; + sentences.length );<br>System.out.println(&quot;Second Sentence: &quot; + sentences[1] );</pre><p>Output:</p><blockquote>Number of sentences detected: 3</blockquote><blockquote>Second Sentence: I am fine, thank you for asking.</blockquote><p>As you can see, we used the English sentences model and used it to decipher an input text to detect the number of sentences and output the second sentence.</p><h3>3. Tokenizing Text</h3><p>Tokenizing is the process of breaking down a document into words and punctuations. The evaluation needs the token model for its appropriate language.</p><pre>InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin&quot;); <br>//opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin is the English Token model<br>TokenizerME myCategorizer = new TokenizerME(new TokenizerModel(is));<br>     <br>String sentence = &quot;Hello, how are you? I am fine, thank you for asking. It is a wonderful day today, isnt it? &quot;;<br>String[] tokens = myCategorizer.tokenize(sentence);<br>System.out.println(&quot;Tokenizer : &quot; + Arrays.stream(tokens).collect(Collectors.joining(&quot; | &quot;)));</pre><p>Output:</p><blockquote>Tokenizer : Hello | , | how | are | you | ? | I | am | fine | , | thank | you | for | asking | . | It | is | a | wonderful | day | today | , | isnt | it | ?</blockquote><p>As you can see, the tokenizer breaks down the sentence into words and punctuations.</p><h3>4. Finding Parts of Speech</h3><p>Tagging parts of speech allows you to determine nouns, verbs, adjectives etc. Typically this is applied on top of tokenized elements, to detect parts of speech of a token. So we would first use the token model to generate tokens of a sentence and then apply the parts of speech model to generate the parts of speech for each token.</p><pre>InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin&quot;); <br>TokenizerME myCategorizer = new TokenizerME(new TokenizerModel(is));<br>     <br>String sentence = &quot;Hello, how are you? I am fine, thank you for asking. It is a wonderful day today, isnt it? &quot;;<br>String[] tokens = myCategorizer.tokenize(sentence);<br>  <br>InputStream posis = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-pos-1.0-1.9.3.bin&quot;); <br>//opennlp-en-ud-ewt-pos-1.0-1.9.3.bin is the model for Parts of Speech<br>POSModel posModel = new POSModel(posis);<br>POSTaggerME pos = new POSTaggerME(posModel);<br>String partsOfSpeech[] = pos.tag(tokens);<br>System.out.println(&quot;Parts of Speech found : &quot; + partsOfSpeech.length);<br><br>for (int i = 0; i &lt; tokens.length; i++){<br>  System.out.println(tokens[i] + &quot; = &quot; + partsOfSpeech[i]);<br>} </pre><p>Output:</p><blockquote>Parts of Speech found : 25</blockquote><blockquote>Hello = INTJ</blockquote><blockquote>, = PUNCT</blockquote><blockquote>how = ADV</blockquote><blockquote>are = AUX</blockquote><blockquote>you = PRON</blockquote><blockquote>? = PUNCT</blockquote><blockquote>I = PRON</blockquote><blockquote>am = AUX</blockquote><blockquote>fine = ADJ</blockquote><blockquote>, = PUNCT</blockquote><blockquote>thank = VERB</blockquote><blockquote>you = PRON</blockquote><blockquote>for = ADP</blockquote><blockquote>asking = VERB</blockquote><blockquote>. = PUNCT</blockquote><blockquote>It = PRON</blockquote><blockquote>is = AUX</blockquote><blockquote>a = DET</blockquote><blockquote>wonderful = ADJ</blockquote><blockquote>day = NOUN</blockquote><blockquote>today = NOUN</blockquote><blockquote>, = PUNCT</blockquote><blockquote>isnt = ADP</blockquote><blockquote>it = PRON</blockquote><blockquote>? = PUNCT</blockquote><h3>5. Lemmatization</h3><p>Lemmatization is where you can use the part of speech for each token and analyze it to obtain a mapping of the word form to have a tense, gender, mood etc that can be associated to the base form of the word. In OpenNLP, it can be done using <strong>statistical lemmatization</strong>, where you build a model using training data or using <strong>a dictionary based lemmatization</strong> that already contains all the possible combinations.</p><p>In this example, we will use a dictionary from here:</p><p><a href="https://raw.githubusercontent.com/richardwilly98/elasticsearch-opennlp-auto-tagging/master/src/main/resources/models/en-lemmatizer.dict">https://raw.githubusercontent.com/richardwilly98/elasticsearch-opennlp-auto-tagging/master/src/main/resources/models/en-lemmatizer.dict</a></p><p>and we will also use a pos model (en-pos-maxent.bin) from here: <a href="https://opennlp.sourceforge.net/models-1.5/">https://opennlp.sourceforge.net/models-1.5/</a>.</p><pre>InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin&quot;); <br>TokenizerME myCategorizer = new TokenizerME(new TokenizerModel(is));<br>String sentence = &quot;Hello, how are you? I am fine, thank you for asking. It is a wonderful day today, isnt it? &quot;;<br>String[] tokens = myCategorizer.tokenize(sentence);<br>  <br>InputStream posis = this.getClass().getClassLoader().getResourceAsStream(&quot;en-pos-maxent.bin&quot;); <br>POSModel posModel = new POSModel(posis);<br>POSTaggerME pos = new POSTaggerME(posModel);<br>String partsOfSpeech[] = pos.tag(tokens);<br>     <br>//Lemmatization<br>InputStream lemmais = this.getClass().getClassLoader().getResourceAsStream(&quot;en-lemmatizer.dict&quot;);         <br>DictionaryLemmatizer lemmatizer = new DictionaryLemmatizer(lemmais);<br>String[] lemmas = lemmatizer.lemmatize(tokens, partsOfSpeech);<br><br>System.out.println(&quot;TOKEN : PART-OF-SPEECH : LEMMA&quot;);<br>for(int i=0;i&lt; tokens.length;i++){<br>  System.out.println(tokens[i]+&quot; : &quot;+partsOfSpeech[i]+&quot; : &quot;+lemmas[i]);<br>}</pre><p>Output:</p><blockquote>TOKEN : PART-OF-SPEECH : LEMMA</blockquote><blockquote>Hello : UH : O</blockquote><blockquote>, : , : O</blockquote><blockquote>how : WRB : how</blockquote><blockquote>are : VBP : be</blockquote><blockquote>you : PRP : you</blockquote><blockquote>? : . : O</blockquote><blockquote>I : PRP : O</blockquote><blockquote>am : VBP : be</blockquote><blockquote>fine : JJ : fine</blockquote><blockquote>, : , : O</blockquote><blockquote>thank : VB : thank</blockquote><blockquote>you : PRP : you</blockquote><blockquote>for : IN : for</blockquote><blockquote>asking : VBG : ask</blockquote><blockquote>. : . : O</blockquote><blockquote>It : PRP : it</blockquote><blockquote>is : VBZ : be</blockquote><blockquote>a : DT : a</blockquote><blockquote>wonderful : JJ : wonderful</blockquote><blockquote>day : NN : O</blockquote><blockquote>today : NN : today</blockquote><blockquote>, : , : O</blockquote><blockquote>isnt : IN : O</blockquote><blockquote>it : PRP : it</blockquote><blockquote>? : . : O</blockquote><h3>6. Training your own model</h3><p>We can also create our own trained models based on a set of inputs we want to train on. In this example we will create a model that categorizes a chat bot that helps people needing help to go fishing.</p><p>The training input is a text file with a category name and input text data to train the model to categorize it in that category name. E.G train.txt:</p><pre>greeting hi. hello . hey . good morning good evening good afternoon   <br>continue ok wow oh ohh Thats great Good Really hmm hm hmmm hmmmmm that&#39;s nice to know great fine<br>places  Where can I go fishing ? Where ? Where&#39;s the best place ? Places to go fishing . tips on where to go . where can i go ?<br>species What kind of fish ? what species are there ? what fish can you catch ? types of fish . what do you get ? what is there ? what&#39;s there ?<br>times when is the best time to go ? whens best ? what time time of the day ? times to go<br>complete Thanks done that&#39;s all thats it. thats it I am done Thank you for information Thank. thanks. thank you. thanks nothing else thats it.</pre><p>Here there are 6 categories.</p><p>greeting : This detects what kind of text would be a greeting. Similarly there are categories for ‘continue’ — to detect a pause in the conversation to continue or ask for more prompts. Then there are categories for ‘places’, ‘species’, ‘times’ and ‘complete’. Each has training data to help categorize input. This is a very simple example, in actual usage models will have extensive training data.</p><p>The training model is created as such:</p><pre>InputStreamFactory inputStreamFactory = new MarkableFileInputStreamFactory(new File(this.getClass().getClassLoader().getResource(&quot;train.txt&quot;).toURI()  ));<br>ObjectStream&lt;String&gt; lineStream = new PlainTextByLineStream(inputStreamFactory, StandardCharsets.UTF_8);<br>ObjectStream&lt;DocumentSample&gt; sampleStream = new DocumentSampleStream(lineStream);<br>  <br>DoccatFactory factory = new DoccatFactory(new FeatureGenerator[] { new BagOfWordsFeatureGenerator() });<br>  <br>TrainingParameters params = ModelUtil.createDefaultTrainingParameters();<br>params.put(TrainingParameters.CUTOFF_PARAM, 0);<br>  <br>// Train a model with classifications from above file.<br>model = DocumentCategorizerME.train(&quot;en&quot;, sampleStream, params, factory);</pre><p>Using this, we can now put all this together to create a very simple chat service that helps people trying to go fishing.</p><h3>Application Setup</h3><p><em>pom.xml</em></p><pre>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;<br>  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;<br>  &lt;parent&gt;<br>   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;<br>   &lt;version&gt;2.7.1&lt;/version&gt;<br>   &lt;relativePath/&gt;<br>  &lt;/parent&gt;<br>  &lt;groupId&gt;com.demo.opennlp&lt;/groupId&gt;<br>  &lt;artifactId&gt;opennlpdemo&lt;/artifactId&gt;<br>  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;<br>  &lt;name&gt;opennlpdemo&lt;/name&gt;  <br>  <br>  &lt;dependencies&gt;<br>    &lt;dependency&gt;<br>       &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>       &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br>    &lt;/dependency&gt;<br>    &lt;dependency&gt;<br>    &lt;groupId&gt;org.apache.opennlp&lt;/groupId&gt;<br>    &lt;artifactId&gt;opennlp-tools&lt;/artifactId&gt;<br>    &lt;version&gt;2.3.0&lt;/version&gt;<br>  &lt;/dependency&gt;<br>  &lt;/dependencies&gt;  <br>  &lt;build&gt;<br>  &lt;plugins&gt;<br>   &lt;plugin&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>   &lt;/plugin&gt;<br>   &lt;plugin&gt;<br>             &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;<br>             &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;<br>             &lt;version&gt;3.0.0&lt;/version&gt;<br>             &lt;configuration&gt;<br>                 &lt;mainClass&gt;com.demo.opennlp.Application&lt;/mainClass&gt;<br>             &lt;/configuration&gt;<br>         &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br> &lt;/build&gt;  <br>&lt;/project&gt;</pre><p><em>Application.java</em></p><pre>package com.demo.opennlp;<br><br>import org.springframework.boot.SpringApplication;<br>import org.springframework.boot.autoconfigure.SpringBootApplication;<br><br>@SpringBootApplication<br>public class Application {<br> public static void main(String[] args) {<br>  SpringApplication.run(Application.class,args);<br> }<br>}</pre><p><em>RequestModel.java</em></p><pre>package com.demo.opennlp.model;<br><br>public class RequestModel {<br> <br> String inputData;<br><br> public String getInputData() {<br>  return inputData;<br> }<br><br> public void setInputData(String inputData) {<br>  this.inputData = inputData;<br> }<br>}</pre><p><em>ResponseModel.java</em></p><pre>package com.demo.opennlp.model;<br><br>public class ResponseModel {<br> <br> String output;<br> <br> public ResponseModel(String out) {<br>  this.output = out;<br> }<br><br> public String getOutput() {<br>  return output;<br> }<br><br> public void setOutput(String output) {<br>  this.output = output;<br> } <br>}</pre><p><em>ChatBean.java</em></p><pre>package com.demo.opennlp.bean;<br><br>import java.io.File;<br>import java.io.FileNotFoundException;<br>import java.io.IOException;<br>import java.net.URISyntaxException;<br>import java.nio.charset.StandardCharsets;<br>import java.util.HashMap;<br>import java.util.Map;<br><br>import org.springframework.beans.factory.InitializingBean;<br>import org.springframework.stereotype.Component;<br><br>import opennlp.tools.doccat.BagOfWordsFeatureGenerator;<br>import opennlp.tools.doccat.DoccatFactory;<br>import opennlp.tools.doccat.DoccatModel;<br>import opennlp.tools.doccat.DocumentCategorizerME;<br>import opennlp.tools.doccat.DocumentSample;<br>import opennlp.tools.doccat.DocumentSampleStream;<br>import opennlp.tools.doccat.FeatureGenerator;<br>import opennlp.tools.util.InputStreamFactory;<br>import opennlp.tools.util.MarkableFileInputStreamFactory;<br>import opennlp.tools.util.ObjectStream;<br>import opennlp.tools.util.PlainTextByLineStream;<br>import opennlp.tools.util.TrainingParameters;<br>import opennlp.tools.util.model.ModelUtil;<br><br>@Component<br>public class ChatBean implements InitializingBean {<br> <br> private  Map&lt;String, String&gt; chatResponse = new HashMap();<br> private DoccatModel model;<br> <br> private  void trainCategorizerModel() throws FileNotFoundException, IOException, URISyntaxException {<br>  // train.txt is a custom training data with categories<br>  InputStreamFactory inputStreamFactory = new MarkableFileInputStreamFactory(new File(this.getClass().getClassLoader().getResource(&quot;train.txt&quot;).toURI()  ));<br>  ObjectStream&lt;String&gt; lineStream = new PlainTextByLineStream(inputStreamFactory, StandardCharsets.UTF_8);<br>  ObjectStream&lt;DocumentSample&gt; sampleStream = new DocumentSampleStream(lineStream);<br>  <br>  DoccatFactory factory = new DoccatFactory(new FeatureGenerator[] { new BagOfWordsFeatureGenerator() });<br>  <br>  TrainingParameters params = ModelUtil.createDefaultTrainingParameters();<br>  params.put(TrainingParameters.CUTOFF_PARAM, 0);<br>  <br>  // Train a model with classifications from above file.<br>  model = DocumentCategorizerME.train(&quot;en&quot;, sampleStream, params, factory);<br>   <br> }<br> <br> @Override<br>    public void afterPropertiesSet() throws Exception {<br>        // Your code here<br>  chatResponse.put(&quot;greeting&quot;, &quot;Hello, how can I help you?&quot;);<br>  chatResponse.put(&quot;places&quot;,&quot;There are several lakes in your locality, for example Lake Lavon, Lake Lexie, Lake Newsteen&quot;);<br>  chatResponse.put(&quot;species&quot;, &quot;In all of these lakes you can get Bass, Crappie, Salmon and Sunfish&quot;);<br>  chatResponse.put(&quot;times&quot;, &quot;The best times are early morning and early evening.&quot;);<br>  chatResponse.put(&quot;continue&quot;, &quot;How else may I help you?&quot;);<br>  chatResponse.put(&quot;complete&quot;, &quot;Thank you for using our service.&quot;);<br>  <br>  trainCategorizerModel();<br>  <br>    }<br> <br> public DoccatModel getModel() {<br>  return model;<br> }<br><br> public Map&lt;String, String&gt; getQuestionAnswer() {<br>  return chatResponse;<br> }<br><br>}</pre><p>In this bean we define responses that our chat application will respond with after it has detected the category of the conversation.</p><p><em>OpenNLPController.java</em></p><pre>package com.demo.opennlp.controller;<br><br>import java.io.IOException;<br>import java.io.InputStream;<br>import java.util.Arrays;<br><br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.web.bind.annotation.PostMapping;<br>import org.springframework.web.bind.annotation.RequestBody;<br>import org.springframework.web.bind.annotation.RequestMapping;<br>import org.springframework.web.bind.annotation.RestController;<br><br>import com.demo.opennlp.bean.ChatBean;<br>import com.demo.opennlp.model.RequestModel;<br>import com.demo.opennlp.model.ResponseModel;<br><br>import opennlp.tools.doccat.DoccatModel;<br>import opennlp.tools.doccat.DocumentCategorizerME;<br>import opennlp.tools.langdetect.Language;<br>import opennlp.tools.langdetect.LanguageDetector;<br>import opennlp.tools.langdetect.LanguageDetectorME;<br>import opennlp.tools.langdetect.LanguageDetectorModel;<br>import opennlp.tools.lemmatizer.DictionaryLemmatizer;<br>import opennlp.tools.postag.POSModel;<br>import opennlp.tools.postag.POSTaggerME;<br>import opennlp.tools.sentdetect.SentenceDetectorME;<br>import opennlp.tools.sentdetect.SentenceModel;<br>import opennlp.tools.tokenize.TokenizerME;<br>import opennlp.tools.tokenize.TokenizerModel;<br><br>@RestController<br>@RequestMapping(&quot;/nlp&quot;)<br>public class OpenNLPController {<br> <br> @Autowired<br> ChatBean chatBean;<br> <br> @PostMapping(&quot;/chat&quot;)  <br> public ResponseModel chat(@RequestBody RequestModel request) {<br>  try {<br>      <br>   String input = request.getInputData();<br>   String outputResponse = &quot;&quot;;<br>   if(input!=null &amp;&amp; input.length()&gt;0) {<br>    for(String sentence:nlpSentences(input)) {<br>     String[] tokens = nlpTokens(sentence);<br>     String[] lemmas = nlpLemma(tokens);<br>     String category = detectCategory(chatBean.getModel(), lemmas);<br>     outputResponse = outputResponse + &quot; &quot; + chatBean.getQuestionAnswer().get(category);     <br>    }<br>   }<br>      <br>      return new ResponseModel(outputResponse);<br>      <br>  }catch(Exception e) {<br>   e.printStackTrace();<br>   return new ResponseModel(&quot;Error. Please try again&quot;);<br>  }<br>   }<br> <br> private String detectCategory(DoccatModel model, String[] lemmas) throws IOException {<br>  // Initialize document categorizer tool<br>  DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model);  <br>  // Get best possible category.<br>  double[] probabilitiesOfOutcomes = myCategorizer.categorize(lemmas);<br>  String category = myCategorizer.getBestCategory(probabilitiesOfOutcomes);  <br>  return category;<br> }<br><br> private String[] nlpLemma(String[] tokens) throws IOException {<br>  InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin&quot;); <br>     TokenizerME myCategorizer = new TokenizerME(new TokenizerModel(is));    <br>     InputStream posis = this.getClass().getClassLoader().getResourceAsStream(&quot;en-pos-maxent.bin&quot;); <br>     POSModel posModel = new POSModel(posis);<br>     POSTaggerME pos = new POSTaggerME(posModel);<br>     String partsOfSpeech[] = pos.tag(tokens);<br>     <br>     //Lemmatization<br>        InputStream lemmais = this.getClass().getClassLoader().getResourceAsStream(&quot;en-lemmatizer.dict&quot;);         <br>        DictionaryLemmatizer lemmatizer = new DictionaryLemmatizer(lemmais);<br>        String[] lemmas = lemmatizer.lemmatize(tokens, partsOfSpeech);<br>        return lemmas;     <br> }  <br> <br> private String[] nlpTokens(String sentence) throws IOException {<br>     InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin&quot;); <br>     TokenizerME myCategorizer = new TokenizerME(new TokenizerModel(is));     <br>  String[] tokens = myCategorizer.tokenize(sentence);<br>  return tokens; <br> }<br>  <br>  private String[] nlpSentences(String input) throws IOException {<br>   InputStream is = this.getClass().getClassLoader().getResourceAsStream(&quot;opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin&quot;); <br>      SentenceModel sentModel = new SentenceModel(is);      <br>   SentenceDetectorME sentenceDetector = new SentenceDetectorME(sentModel);<br>   String sentences[] = sentenceDetector.sentDetect(input);<br>   return sentences;<br>  } <br><br>}</pre><p>The controller gets the input text, breaks it down into sentences and then creates tokens. Using the tokens it generates parts of speech and lemmatization. Using the lemmatization, it uses it as input to the custom categorization model to get a category of input and uses that to generate a predefined response.</p><h3>Testing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*1WwMcRKRIp9DxotHidqmhw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*7RhWCwmfH40e9EOuEHhwKw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*1P_uw_P7XssDqY2QoTBouQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*mZHCanhwRX0gGfunU1W4Uw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*MNx8nWrD_D8YPZdu2VGbbQ.png" /></figure><p>All code is available at: <a href="https://github.com/tobintom/OpenNLP/tree/master/opennlpdemo">https://github.com/tobintom/OpenNLP/tree/master/opennlpdemo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=551895c7d3f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot with Drools Rules Engine]]></title>
            <link>https://medium.com/@tobintom/spring-boot-with-drools-rules-engine-d73e1af3c411?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d73e1af3c411</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[drools]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[rules-engine]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Mon, 03 Jul 2023 21:51:47 GMT</pubDate>
            <atom:updated>2023-07-03T21:51:47.208Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*onAsP4NIAMuEbbfFJd-VlA.png" /><figcaption>SpringBoot with Drools</figcaption></figure><p>A rules engine is used to serve as pluggable software components which execute business rules that a business rules approach has externalized or separated from application code. This externalization or separation allows business users to modify the rules without the need for IT intervention. In cases of large distributed applications that process complex logic, a rules engine can be used to isolate the application code from common rules logic that can be applied commonly across the distributed platform.</p><p>In this article we will be building a sample Spring Boot REST service that uses the <strong>Drools Rule Engine</strong> to determine the outcome of various inputs.</p><p>The Drools Rules Engine (<a href="https://www.drools.org/">https://www.drools.org/</a>) is an open source rules engine implementation from JBoss.</p><p>The KIE project (<a href="https://www.kie.org/about/">https://www.kie.org/about/</a>) allows integration of the Drools Rules engine with Spring Boot.</p><h3><strong>Application Setup</strong></h3><p>We will create a simple REST service application using SpringBoot which will use a Drools Engine to determine output of the service.</p><p>We will have a Banking Service that takes an applicant’s details and returns a loan rate of interest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*ZJ3oqkF406ZnxkYD5ydXMA.png" /><figcaption>Application Structure</figcaption></figure><p><strong>Pom.xml</strong></p><pre>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;<br>  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;<br>  &lt;parent&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;<br>    &lt;version&gt;2.6.7&lt;/version&gt;<br>  &lt;/parent&gt;<br>  &lt;groupId&gt;com.drools.example&lt;/groupId&gt;<br>  &lt;artifactId&gt;drools&lt;/artifactId&gt;<br>  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;<br>  &lt;name&gt;droolsSample&lt;/name&gt;<br>  &lt;description&gt;DroolsSample&lt;/description&gt;<br>  &lt;properties&gt;<br>        &lt;java.version&gt;11&lt;/java.version&gt;<br>        &lt;maven-jar-plugin.version&gt;3.1.1&lt;/maven-jar-plugin.version&gt;<br>        &lt;drools.version&gt;8.40.0.Final&lt;/drools.version&gt;<br>    &lt;/properties&gt;<br>    &lt;dependencies&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br>        &lt;/dependency&gt;<br> <br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;<br>            &lt;artifactId&gt;lombok&lt;/artifactId&gt;<br>            &lt;optional&gt;true&lt;/optional&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;<br>            &lt;scope&gt;test&lt;/scope&gt;<br>        &lt;/dependency&gt;<br>         <br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.drools&lt;/groupId&gt;<br>            &lt;artifactId&gt;drools-core&lt;/artifactId&gt;<br>            &lt;version&gt;${drools.version}&lt;/version&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.drools&lt;/groupId&gt;<br>            &lt;artifactId&gt;drools-compiler&lt;/artifactId&gt;<br>            &lt;version&gt;${drools.version}&lt;/version&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.drools&lt;/groupId&gt;<br>            &lt;artifactId&gt;drools-decisiontables&lt;/artifactId&gt;<br>            &lt;version&gt;${drools.version}&lt;/version&gt;<br>        &lt;/dependency&gt;<br>        &lt;dependency&gt;<br>            &lt;groupId&gt;org.drools&lt;/groupId&gt;<br>            &lt;artifactId&gt;drools-mvel&lt;/artifactId&gt;<br>            &lt;version&gt;${drools.version}&lt;/version&gt;<br>        &lt;/dependency&gt;<br>    &lt;/dependencies&gt;<br>    &lt;build&gt;<br>  &lt;plugins&gt;<br>   &lt;plugin&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>   &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br> &lt;/build&gt;<br>&lt;/project&gt;</pre><p>In the pom.xml, we will need to define the drools dependencies, the drools-core, drools-compiler, drools-mvel and decisiontables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*Byvho3qwspY59Hwee4IqPA.png" /></figure><p>The service accepts a request payload and invokes the Drools Rules engine to fire all configured rules and returns a response.</p><p><strong>DroolsSampleApp.java</strong></p><pre>package com.drools.sample;<br><br>import org.springframework.boot.SpringApplication;<br>import org.springframework.boot.autoconfigure.SpringBootApplication;<br><br>@SpringBootApplication<br>public class DroolsSampleApp {<br> public static void main(String[] args) {<br>  SpringApplication.run(DroolsSampleApp.class, args);<br> }<br>}</pre><p><strong>DroolsApplicationConfig.java</strong></p><pre>package com.drools.sample.config; <br><br>import org.kie.api.KieServices;<br>import org.kie.api.builder.KieBuilder;<br>import org.kie.api.builder.KieFileSystem;<br>import org.kie.api.builder.KieModule;<br>import org.kie.api.runtime.KieContainer;<br>import org.kie.internal.io.ResourceFactory;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br><br>@Configuration<br>public class DroolsApplicationConfig {<br> <br> private static final KieServices kieServices = KieServices.Factory.get();<br> private static final String RULES_CUSTOMER_RULES_DRL = &quot;rules/loan_rate.drl&quot;;<br> <br>   @Bean<br>     public KieContainer kieContainer() {<br>         KieFileSystem kieFileSystem = kieServices.newKieFileSystem();<br>         kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_CUSTOMER_RULES_DRL));<br>         KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);<br>         kb.buildAll();<br>         KieModule kieModule = kb.getKieModule();<br>         KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());<br>         return kieContainer;<br>     }<br>}</pre><p>Here we define the rules being placed in the <strong>/rules</strong> folder under /src/main/resources and we have defined a rule file <strong>loan_rate.drl</strong>. The actual Drools Rules are defined in files with the drl extension.</p><p><strong>loan_rate.drl</strong></p><pre>import com.drools.sample.model.Participant;<br>global com.drools.sample.model.Rate rate;<br><br>dialect &quot;mvel&quot;<br><br>rule &quot;Checking Existing Debt Against Loan Amount&quot;<br> when <br>  Participant(loanAmount &gt; (2 * existingDebt))<br> then<br>  rate.setLoanStatus(&quot;Rejected - Too Much Debt&quot;);<br>  rate.setLoanRate(999);<br>end<br><br>rule &quot;Checking Annual Salary is greater than 50000&quot;<br> when<br>  Participant(annualSalary &lt;= 50000)<br> then<br>  rate.setLoanStatus(&quot;Rejected - Too low salary&quot;);<br>  rate.setLoanRate(999);<br>end<br><br>rule &quot;Checking Credit Score less than 550&quot;<br> when<br>  Participant(creditScore &lt; 550 , annualSalary &gt; 50000,  loanAmount &lt; (2 * existingDebt))<br> then<br>  rate.setLoanStatus(&quot;Rejected&quot;);<br>  rate.setLoanRate(999);<br>end<br><br>rule &quot;Checking Credit Score less than 650&quot;<br> when<br>  Participant((creditScore &lt; 650 &amp;&amp; creditScore &gt;= 550), annualSalary &gt; 50000,  loanAmount &lt; (2 * existingDebt))<br> then<br>  rate.setLoanStatus(&quot;Approved&quot;);<br>  rate.setLoanRate(7.25);<br>end<br><br>rule &quot;Checking Credit Score less than 750&quot;<br> when<br>  Participant((creditScore &lt; 750 &amp;&amp; creditScore &gt;= 650), annualSalary &gt; 50000,  loanAmount &lt; (2 * existingDebt))<br> then<br>  rate.setLoanStatus(&quot;Approved&quot;);<br>  rate.setLoanRate(6.25);<br>end<br><br>rule &quot;Checking Credit Score greater than 750&quot;<br> when<br>  Participant(creditScore &gt;= 750, annualSalary &gt; 50000,  loanAmount &lt; (2 * existingDebt))<br> then<br>  rate.setLoanStatus(&quot;Approved&quot;);<br>  rate.setLoanRate(5.25);<br>end</pre><p>Here we define the rules of execution:</p><ol><li>If the user is asking for a loan amount &gt; (twice the existing debt), the loan process is rejected.</li><li>If the user has an annual salary of 50000 or less, the loan process is rejected.</li><li>If the user satisfies the above conditions, then the loan rate is decided by the credit score.</li></ol><p><strong>MODEL REQUESTS:</strong></p><p><strong>PARTICIPANT REQUEST OBJECT:</strong></p><pre>package com.drools.sample.model;<br><br>import lombok.Getter;<br>import lombok.Setter;<br><br>@Getter<br>@Setter<br>public class Participant {<br><br> private String name;<br> private int age;<br> private int creditScore;<br> private long annualSalary;<br> private long existingDebt;<br> private long loanAmount;<br>}</pre><p><strong>RATE RESPONSE OBJECT:</strong></p><pre>package com.drools.sample.model;<br><br>import lombok.Getter;<br>import lombok.Setter;<br><br>@Getter<br>@Setter<br>public class Rate {<br><br> private String loanStatus;<br> private double loanRate;<br>}</pre><p><strong>DROOLS SERVICE:</strong></p><pre>package com.drools.sample.service;<br><br>import org.kie.api.runtime.KieContainer;<br>import org.kie.api.runtime.KieSession;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.stereotype.Service;<br><br>import com.drools.sample.model.Participant;<br>import com.drools.sample.model.Rate;<br><br>@Service<br>public class DroolsService {<br> <br> @Autowired<br>    private KieContainer kieContainer;<br> <br>  public Rate getRate(Participant applicantRequest) {<br>    Rate rate = new Rate();<br>         KieSession kieSession = kieContainer.newKieSession();<br>         kieSession.setGlobal(&quot;rate&quot;, rate);<br>         kieSession.insert(applicantRequest);<br>         kieSession.fireAllRules();<br>         kieSession.dispose();<br>         return rate;<br>     }<br><br>}</pre><p><strong>DROOLS CONTROLLER:</strong></p><pre>package com.drools.sample.controller;<br><br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.http.HttpStatus;<br>import org.springframework.http.ResponseEntity;<br>import org.springframework.web.bind.annotation.PostMapping;<br>import org.springframework.web.bind.annotation.RequestBody;<br>import org.springframework.web.bind.annotation.RequestMapping;<br>import org.springframework.web.bind.annotation.RestController;<br><br>import com.drools.sample.model.Participant;<br>import com.drools.sample.model.Rate;<br>import com.drools.sample.service.DroolsService;<br><br>@RestController()<br>@RequestMapping(&quot;/bankservice&quot;)<br>public class DroolsSampleController {<br> <br> @Autowired<br> private DroolsService bankService;<br> <br> @PostMapping(&quot;/getrate&quot;)<br> public ResponseEntity&lt;Rate&gt; getRate(@RequestBody Participant request){<br>  Rate rate = bankService.getRate(request);<br>  return new ResponseEntity&lt;&gt;(rate, HttpStatus.OK);<br> }<br><br>}</pre><p>The controller takes in a Participant.java object and fires the rules configured in the loan_rate.drl and returns a Rate.java object as response.</p><h3>Testing the Service</h3><p>Now we can build the Spring Boot application and run tests against it at: <strong><em>http://localhost:8080/bankservice/getrate</em></strong></p><ol><li>Testing with Salary less than 50000:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*cE6M_VRkk9du7LsTK-yfxQ.png" /></figure><p>2. Testing with High salary, but too much debt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*tCscdZVNjaR9b4ZPMWNVcQ.png" /></figure><p>3. Testing with Credit Scores:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*w_rNknJOOKkKpUhNy6laKQ.png" /><figcaption>Credit Score less than 650</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*5cxKR308-0Ub53nEz5__IA.png" /><figcaption>Credit Score &lt;750 and ≥ 650</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*06REKC29-8W6E_VgwvGx9Q.png" /><figcaption>Credit score greater than or equal to 750</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d73e1af3c411" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Passkeys and Webauthn]]></title>
            <link>https://medium.com/@tobintom/introducing-passkeys-and-webauthn-170213dd6a63?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/170213dd6a63</guid>
            <category><![CDATA[webauthn]]></category>
            <category><![CDATA[fido2]]></category>
            <category><![CDATA[passkey]]></category>
            <category><![CDATA[passwordless]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Thu, 04 May 2023 17:19:14 GMT</pubDate>
            <atom:updated>2023-05-04T17:28:46.555Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*-MzczTXwfFn8M7s3zhmWcg.png" /></figure><h3>About FIDO2 and Webauthn</h3><p>FIDO (<strong>F</strong>ast <strong>ID</strong>entity <strong>O</strong>nline), or FIDO Alliance (<a href="https://fidoalliance.org/">https://fidoalliance.org/</a>)is a consortium that develops secure authentication protocols. One of its protocols is FIDO2, which contains core specifications <strong>WebAuthn </strong>(the client API) and <strong>CTAP(</strong>the authenticator API). CTAP (Client to Authenticator Protocols) is a set of low level protocols to communicate with the authenticators over the BLE/NFC/USB.</p><p><strong>WebAuthn</strong> <strong>API </strong>is a browser JS API that describes an interface for creating and managing public key credentials.</p><p>Please refer to <a href="https://webauthn.guide/">https://webauthn.guide/</a> for more details. The goal is to eliminate the use of passwords for authentication and allow a user to generate a <strong>passkey. </strong>A Passkey is a cryptographic entity used in place of a password that contains a private-public keypair. A user can then use this passkey to login, without using a traditional password.</p><h3>How does it work with the Webauthn API</h3><p>To implement a FIDO2 passkey passwordless authentication, you need a FIDO server to handle the authentication and store the public key. The client, in this case will be a webauthn capable browser.</p><p>There are two stages:</p><p>1. Registering a new passkey</p><p>2. Logging in with a passkey</p><h3>Registering a new passkey</h3><p>A new credential is created using the native webauthn API navigator.credentials.create(), with a publicKey object passed to it, as shown below.</p><pre>const publicKeyCredentialCreationOptions = {     <br>challenge: randomStringFromServer as Array Buffer,     <br>rp: { name: &quot;website&quot;,         <br>      id: &quot;website.com&quot; ,     <br>    },     <br>user: {         <br>      id: Array Buffer of user id,         <br>      displayName: &quot;User 1&quot;,     <br>    },     <br>pubKeyCredParams: [{alg: -7, type: &quot;public-key&quot;}],     <br>authenticatorSelection: {         <br>      authenticatorAttachment: &quot;cross-platform&quot;<br>      },     <br>timeout: 60000,     <br>attestation: &quot;direct&quot; <br>};  <br><br>const credential = await navigator.credentials.create({publicKey: publicKeyCredentialCreationOptions });</pre><p>When the browser executes navigator.credentials.create(publicKey), it opens a UI to allow the user to create a passkey. The publicKeyCredentialCreationOptions object has the following elements:</p><p><strong>challenge</strong>: A random string as Array Buffer</p><p><strong>rp</strong>: Relying Party, and the id should be a subset of the domain the browser is running</p><p><strong>user: </strong>The user object contains the id in array buffer form, of the ID of the user to be associated with this passkey. It also contains a name and display name of the user.</p><p><strong>pubKeyCredParams</strong>: This is an array of objects describing what public key types are acceptable to a server.</p><p><strong>timeout: </strong>A time within which the passkey registration should be completed</p><p><strong>authenticatorSelection: </strong>This optional object helps relying parties make further restrictions on the type of authenticators allowed for registration. Can be left blank, or ‘platform’ or ‘cross-platform’. For ‘platform’ the device the browser runs on should have a platform authenticator and this can be checked in the browser by checking if <strong>PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable() </strong>resolves to true.</p><p>After the authenticator completes the navigator.credentials.create() call, it returns a PublicKeyCredential object. The private key will be saved in the platform, depending on the authenticatorSelection. It can be on the device, on a separate device or usb stick.</p><pre>PublicKeyCredential {     <br>    id: &#39;ADSUllKQmbqdGtpu4sjseh4cg2TxSvrbcHDTBsv4NSSX9...&#39;,     <br>    rawId: ArrayBuffer(59),     <br>    response: AuthenticatorAttestationResponse {         <br>            clientDataJSON: ArrayBuffer(121),         <br>            attestationObject: ArrayBuffer(306)<br>     },     <br>    type: &#39;public-key&#39; }</pre><p>This is then sent to the FIDO server for validation and then the public key is associated to the id and saved on the FIDO server.</p><h3>Login with a passkey</h3><p>To login using a passkey, the user will need the private key of the passkey credential they registered the user id with. Usually it is on the same device or a different device.</p><pre>const publicKeyCredentialRequestOptions = {     <br>    challenge: randomStringFromServer as Array Buffer,     <br>    allowCredentials: [{id: Array Buffer of user id, type: &#39;public-key&#39;, transports: [&#39;usb&#39;, &#39;ble&#39;, &#39;nfc&#39;],     }],     <br>    timeout: 60000<br> }  <br><br>const assertion = await navigator.credentials.get({     publicKey: publicKeyCredentialRequestOptions });</pre><p>To login, it starts with calling navigator.credentials.get() passing into it a publicKey object. When this is executed, the browser gives the user the option to provide it with the saved passkey.</p><p>If a passkey matching the <strong>allowCredentials</strong> array is found on the device, the authenticator replies with again a publicKeyCredential:</p><pre>PublicKeyCredential {     <br>      id: &#39;ADSUllKQmbqdGtpu4sjseh4cg2TxSvrbcHDTBsv4NSSX9...&#39;,     <br>       rawId: ArrayBuffer(59),     <br>       response: AuthenticatorAssertionResponse {         <br>                  authenticatorData: ArrayBuffer(191),         <br>                  clientDataJSON: ArrayBuffer(118),         <br>                  signature: ArrayBuffer(70),         <br>                  userHandle: ArrayBuffer(10)     <br>          },<br>     type: &#39;public-key&#39; }</pre><p>This is sent to the FIDO server, which can match with the public key saved to ensure the credential is correct or not.</p><p>This is the basic implementation of a FIDO2 webauthn passkey transaction.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=170213dd6a63" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Node.js REST Services with Mongo DB and securing OAuth2 with Keycloak]]></title>
            <link>https://medium.com/@tobintom/building-node-js-rest-services-with-mongo-db-and-securing-oauth2-with-keycloak-196422ddf03a?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/196422ddf03a</guid>
            <category><![CDATA[oauth2]]></category>
            <category><![CDATA[keycloak]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[expressjs]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Wed, 29 Mar 2023 04:32:01 GMT</pubDate>
            <atom:updated>2023-03-29T04:32:01.452Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*226_FnFE6gI7ozfQ1tmaSQ.png" /></figure><p>In this article we will build a set of Node JS REST service APIs that connect to Mongo DB and is secured by OAuth2 using Keycloak using the ‘client_credentials’ grant.</p><h3>Building the Node JS Services</h3><p>For building the Node JS services, we will be using Visual Studio Code. Install Node JS from the official site (<a href="https://nodejs.org/en/download">https://nodejs.org/en/download</a>), if you do not already have it. In VS Code, create a new folder for the service and enter the new directory, e.g</p><blockquote>mkdir Nodeservice</blockquote><blockquote>cd Nodeservice</blockquote><p>To initialize the node js project, run:</p><blockquote>npm init</blockquote><p>This initializes the Node js project, now let us install express (web server module) and mongoose (module to connect to MongoDB).</p><blockquote>npm install express mongoose --save</blockquote><p>Now create the following directories</p><blockquote>mkdir models</blockquote><blockquote>mkdir controllers</blockquote><h4>Building the web server</h4><p>In the root folder, create a file called server.js, which will be the entry point of the application. In this application, I have installed Mongo DB on my local machine and connect to the localhost version. Add the following code for the <strong>server.js</strong>:</p><pre>//import express and mongoose<br>const express = require(&#39;express&#39;);<br>const mongoose = require(&#39;mongoose&#39;);<br><br>//create the express app<br>const app = express();<br><br>app.use(express.urlencoded({extended:true}));<br>app.use(express.json());<br><br>mongoose.Promise = global.Promise;<br><br>// Connecting to the database<br>mongoose.connect(&#39;mongodb://127.0.0.1:27017/users_db&#39;, {<br>    useNewUrlParser: true<br>}).then(() =&gt; {<br>    console.log(&quot;Successfully connected to the database&quot;);    <br>}).catch(error =&gt; {<br>    console.log(&#39;Could not connect to the database.&#39;, error);<br>    process.exit();<br>});<br><br>//Application listens on port 8100 <br>app.listen(8100);</pre><p>We have created a starting point of the application to listen on port 8100 and also created a connection to local host Mongo DB on the users_db database. In this application we will create APIs to manage users, e.g. creating a user, reading user details, editing a user and deleting a user.</p><h4>Creating a User Model</h4><p>In the /models folder, we will create a model object for User, create a user.js inside the /<strong>models</strong> directory we created earlier.</p><pre>const mongoose = require(&#39;mongoose&#39;);<br><br>const UserSchema = mongoose.Schema({<br>    id: String,<br>    firstName: String,<br>    lastName: String<br>}, {<br>    timestamps: true<br>});<br><br>module.exports = mongoose.model(&#39;User&#39;, UserSchema);</pre><p>This is a simple user model with fields id, firstname and lastname. The timestamps option allows mongoose to automatically add created/updated timestamp fields.</p><h4>Creating the Controllers</h4><p>We will now create the controller script that handles the logic for each API end point. In the /controllers directory, create the apicontroller.js:</p><pre>const express = require(&#39;express&#39;);<br>const User = require(&#39;../models/user.js&#39;);<br> <br>var router = express.Router();<br><br>//Create a user<br>router.post(&#39;/user&#39;,function(req,res){<br>    //Create User Object<br>    const user = new User({<br>        userid: req.body.userid,<br>        firstName: req.body.firstName,<br>        lastName: req.body.lastName<br>    });<br>    //Save in DB<br>    user.save()<br>    .then(data =&gt; {<br>        res.send(&quot;Successfully added user&quot; +data.firstName);<br>    }).catch(err =&gt; {<br>        res.status(500).send({message:`Error creating user ${err.message}`});<br>    });<br>});<br><br>//Find a user<br>router.get(&#39;/user/:id&#39;, function(req,res){<br>    User.find({userid:req.params.id})<br>    .then(user =&gt; {<br>        if(!user) {<br>            return res.status(404).send({<br>                message: &quot;User not found with id &quot; + req.params.userid<br>            });            <br>        }<br>        res.send(user);<br>    }).catch(err =&gt; {         <br>        return res.status(500).send({<br>            message: `Error retrieving user : ${err.message}`<br>        });<br>    });<br>    <br>});<br><br>//Update a user<br>router.put(&#39;/user&#39;, function(req,res){<br>    // Find note and update it with the request body<br>    <br>    User.findOneAndUpdate({userid:req.body.userid}, {<br>        userid:req.body.userid,<br>        firstName: req.body.firstName,<br>        lastName: req.body.lastName<br>    }, {new: true})<br>    .then(user =&gt; {<br>        if(!user) {<br>            return res.status(404).send({<br>                message: &quot;User not found&quot;<br>            });<br>        }<br>        res.send(user);<br>    }).catch(err =&gt; {        <br>        return res.status(500).send({<br>            message: `Error updating user: ${err.message} `<br>        });<br>    });<br>});<br><br>//Delete a user<br>router.delete(&#39;/user/:id&#39;,function (req,res){<br>    User.findOneAndDelete({userid:req.params.id})<br>    .then(user =&gt; {<br>        if(!user) {<br>            return res.status(404).send({<br>                message: &quot;User not found &quot;<br>            });<br>        }<br>        res.send({message: &quot;User deleted successfully!&quot;});<br>    }).catch(err =&gt; {         <br>        return res.status(500).send({<br>            message: &quot;Could not delete user with id &quot; + req.params.userid<br>        });<br>    });<br>});<br><br>module.exports = router;</pre><p>Now modify the <strong>server.js </strong>to use this apicontroller.js</p><pre>const apicontroller = require(&#39;./controllers/apicontroller&#39;);<br><br>//use the API controller<br>app.use(apicontroller);</pre><h4>Testing the Service</h4><ul><li>Creating a User</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*S3__UjmxGHUO29a5lxe45g.png" /><figcaption>Running the POST create user API</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*UssAy1AAOk53_7kyZtIPjQ.png" /><figcaption>User Object created in Mongo DB</figcaption></figure><ul><li>Reading a User</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*imZoPQqD4XcwwvWZ0dlAPQ.png" /><figcaption>Running the GET operation</figcaption></figure><ul><li>Updating a User</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*wyb4K7B7PMlbjrMvLAlROg.png" /><figcaption>Running the Update Operation</figcaption></figure><ul><li>Deleting a User</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*yhlybXHSoh93P9g_fTK-0w.png" /><figcaption>Running the Delete operation</figcaption></figure><p>Now our service is working fine and all the API endpoints are exposed to anyone who knows the URL. We will now secure the API endpoints with keycloak.</p><h3>Setting Up Keycloak</h3><p>We will first need to download and setup Keycloak. It can be downloaded from here <a href="https://www.keycloak.org/">https://www.keycloak.org/</a>. Choose the appropriate distribution type and download the distribution. After downloading the distribution, you can start up an instance by following the Keycloak guides here <a href="https://www.keycloak.org/guides#getting-started">https://www.keycloak.org/guides#getting-started</a>. If you are on windows (which is what I am on), you can start up an instance by running the <strong>kc.bat</strong> command under /bin folder:</p><blockquote><strong><em>kc.bat start-dev --http-port 9000</em></strong></blockquote><p>I changed the default port to use 9000 so that it does not conflict with any other existing applications runnig on the default keycloak port. After you execute this, you should be able to view keycloak running at <a href="http://localhost:9000/">http://localhost:9000/</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*JAKFG5QRufIetwqg.png" /></figure><p>Local Instance of Keycloak</p><p>Create an admin credential and navigate to the administration console, which will ask you to enter the admin credentials you created. Create a new Realm in Keycloak, using the ‘Create Realm’ option under the default Master Realm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*tvbiHJa3987Ofzc6.png" /></figure><p>Create a new Realm called ‘<strong>NodeJS-Realm</strong>’. Now we can create Clients, Roles, Users etc under this realm. Create a new Client under this realm of type <strong>OpenID Connect</strong>, I named mine ‘<strong>OAuth2-demo-client’</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*RSETjH4-upp1WRr9JvXzDA.png" /><figcaption>From the realm created, select Clients and ‘Create Client’</figcaption></figure><p>After giving the name, click on Next. In the next screen, enable ‘<strong>Client authentication</strong>’, disable ‘<strong>Standard flow</strong>’ and enable ‘<strong>Service account roles</strong>’ and click on save.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*WvmbbWD2LoJYWWnR.png" /></figure><p>Navigate to the ‘Credentials’ tab of the newly created client and you will find the <strong>client secret</strong>, which we will use for the demo.</p><p>Keycloak will be the authorization server for our APIs. When a request to the service comes in, the gateway needs to ensure that it is from a client setup in Keycloak. We have setup one client in keycloak so far and we will use it’s credentials for the demo. We need to find the JWT token issuer URI that the service needs to communicate with to ensure the service request credentials are valid.</p><p>In Keycloak, in the Client Settings, navigate to the Credentials tab and copy the <strong>Client Secret.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CUSxW0rF8OSrU_rMBu_zWw.png" /><figcaption>Get the Client Secret</figcaption></figure><p>In Keycloak, in the <strong>Realm Settings</strong> of the realm we created (<strong>NodeJS-Realm</strong>), click on the <strong>OpenID Endpoint Configuration</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*Ek_8TSRcgP5VU_mjFK7Dwg.png" /></figure><p>This will open up a new window with the realm configurations. Note down the values for <strong>issuer, authorization_endpoint </strong>and <strong>token_endpoint</strong>.</p><pre>{<br> &quot;issuer&quot;:&quot;http://localhost:9000/realms/NodeJS-Realm&quot;,<br> &quot;authorization_endpoint&quot;:&quot;http://localhost:9000/realms/NodeJS-Realm/protocol/openid-connect/auth&quot;,<br> &quot;token_endpoint&quot;:&quot;http://localhost:9000/realms/NodeJS-Realm/protocol/openid-connect/token&quot;<br>}</pre><p>Now that we have completed the authorization setup, we will now plug this into our NodeJS service API endpoints.</p><p>Navigate to the Realm Settings of the NodeJS-Realm and go to the ‘keys’ tab and click on the ‘Public Key’ link of the RSA256 key:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tbpa8ppYYcGgpXls9xeQ7A.png" /></figure><p>Clicking on the link will open a popup with the public key:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*KH81yLELBpYkvjbI8aX7-g.png" /></figure><p>Copy the value of this public key, we will need it.</p><p>In the ‘Client Details’, go to ‘Action’ -&gt; ‘Download adapter config’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xEv47HAOptQcl0EPivwmoA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*ruQpONA1rdAfsHGYwiSwfA.png" /></figure><p>Download this file (keycloak.json) and keep it in the root folder of the nodejs application.</p><p><strong>keycloak.json:</strong></p><p>Add the following entries to this JSON:</p><pre>&quot;bearer-only&quot;:true,<br>&quot;realm-public-key&quot;: &quot;MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkOvrdmZrhIz4yEwYRJ/Mh+vyxaV0zp597v+vM07ZP7ZncRgGI2PUi3Ci1DGcuN2NhXD0srOpdVbYEmG6fNpHX5+3cIO+4655SQZ+U4XkJcN8Yilk/naVtTF6VziHLfBrq0AU3XolZOx7/JxCkrzRdEIbtKMsDFEYLRCvLWgnsBS8f9sOW/1UQqsNlU8E+HjEtAKcQe2xn3cW3RicnbW189UO8MWeQAToGcttI4OCd42bU0lGMF2Nvu4gTyiZrwN+V1z8s/70YirH6UwxNEi0cu/ZCAGrVs8iGK9jJuqHt7+Grpi66umlMWc6a7StmLgJKUidZnO/foWhTsHcgYWGPwIDAQAB&quot;</pre><p>The realm-public-key value is the public key copied from the Realm Settings, RSA256 key. So the complete keycloak.json will look something like:</p><pre>{<br>  &quot;realm&quot;: &quot;NodeJS-Realm&quot;,<br>  &quot;realm-public-key&quot;: &quot;MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkOvrdmZrhIz4yEwYRJ/Mh+vyxaV0zp597v+vM07ZP7ZncRgGI2PUi3Ci1DGcuN2NhXD0srOpdVbYEmG6fNpHX5+3cIO+4655SQZ+U4XkJcN8Yilk/naVtTF6VziHLfBrq0AU3XolZOx7/JxCkrzRdEIbtKMsDFEYLRCvLWgnsBS8f9sOW/1UQqsNlU8E+HjEtAKcQe2xn3cW3RicnbW189UO8MWeQAToGcttI4OCd42bU0lGMF2Nvu4gTyiZrwN+V1z8s/70YirH6UwxNEi0cu/ZCAGrVs8iGK9jJuqHt7+Grpi66umlMWc6a7StmLgJKUidZnO/foWhTsHcgYWGPwIDAQAB&quot;,<br>  &quot;auth-server-url&quot;: &quot;http://localhost:9000/&quot;,<br>  &quot;ssl-required&quot;: &quot;none&quot;,<br>  &quot;resource&quot;: &quot;OAuth2-demo-client&quot;,<br>  &quot;bearer-only&quot;: true,<br>  &quot;credentials&quot;: {<br>    &quot;secret&quot;: &quot;xksSGM0NVgpKzao4bb9GHPKA5aNg7sAi&quot;<br>  },<br>  &quot;confidential-port&quot;: 0<br>}</pre><h3>Securing Node JS Service with Keycloak</h3><p>First, install the <strong>keycloak-connect</strong> and <strong>express-session </strong>dependencies to the project:</p><blockquote>npm install keycloak-connect express-session --save</blockquote><p>Create a new folder called config.</p><blockquote>mkdir config</blockquote><p>Add a new file called keycloak.js:</p><pre>var session = require(&#39;express-session&#39;);<br>var Keycloak = require(&#39;keycloak-connect&#39;);<br><br>let keycloak;<br>let memoryStore;<br><br>function initKeycloak() {<br>    if (keycloak) {      <br>        return keycloak;<br>    } <br>    else {        <br>        memoryStore = new session.MemoryStore();<br>        keycloak = new Keycloak({ store: memoryStore });<br>        return keycloak;<br>    }<br>}<br><br>function getKeycloak() {     <br>    if (keycloak) {        <br>        return keycloak;<br>    }  <br>}<br><br>function getMemoryStore() {    <br>    if (memoryStore) {         <br>        return memoryStore;<br>    } <br>}<br><br>module.exports = {<br>    initKeycloak,<br>    getMemoryStore,<br>    getKeycloak<br>};</pre><p>This will intialize the keycloak instance using the <strong>keycloak.json </strong>we created earlier in the root folder of the application.</p><p>In the apicontroller.js, retrieve the instance of the keycloak instance by:</p><pre>const keycloak = require(&#39;../config/keycloak.js&#39;).getKeycloak();</pre><p>And the for all the API end-points that you want to protect via client_credentials grant, add the <strong>keycloak.protect() </strong>as a parameter in the router API call.</p><pre>router.get(&#39;/user/:id&#39;, keycloak.protect() ,function(req,res){</pre><p>So the full apicontroller.js is now:</p><pre>const express = require(&#39;express&#39;);<br>const User = require(&#39;../models/user.js&#39;);<br>const keycloak = require(&#39;../config/keycloak.js&#39;).getKeycloak();<br><br>var router = express.Router();<br><br>//Create a user<br>router.post(&#39;/user&#39;, keycloak.protect(), function(req,res){<br>    //Create User Object<br>    const user = new User({<br>        userid: req.body.userid,<br>        firstName: req.body.firstName,<br>        lastName: req.body.lastName<br>    });<br>    //Save in DB<br>    user.save()<br>    .then(data =&gt; {<br>        res.send(&quot;Successfully added user&quot; +data.firstName);<br>    }).catch(err =&gt; {<br>        res.status(500).send({message:`Error creating user ${err.message}`});<br>    });<br>});<br><br>//Find a user<br>router.get(&#39;/user/:id&#39;, keycloak.protect() ,function(req,res){<br>    User.find({userid:req.params.id})<br>    .then(user =&gt; {<br>        if(!user) {<br>            return res.status(404).send({<br>                message: &quot;User not found with id &quot; + req.params.userid<br>            });            <br>        }<br>        res.send(user);<br>    }).catch(err =&gt; {         <br>        return res.status(500).send({<br>            message: `Error retrieving user : ${err.message}`<br>        });<br>    });<br>    <br>});<br><br>//Update a user<br>router.put(&#39;/user&#39;,keycloak.protect(), function(req,res){<br>    // Find note and update it with the request body<br>    <br>    User.findOneAndUpdate({userid:req.body.userid}, {<br>        userid:req.body.userid,<br>        firstName: req.body.firstName,<br>        lastName: req.body.lastName<br>    }, {new: true})<br>    .then(user =&gt; {<br>        if(!user) {<br>            return res.status(404).send({<br>                message: &quot;User not found&quot;<br>            });<br>        }<br>        res.send(user);<br>    }).catch(err =&gt; {        <br>        return res.status(500).send({<br>            message: `Error updating user: ${err.message} `<br>        });<br>    });<br>});<br><br>//Delete a user<br>router.delete(&#39;/user/:id&#39;,keycloak.protect(),function (req,res){<br>    User.findOneAndDelete({userid:req.params.id})<br>    .then(user =&gt; {<br>        if(!user) {<br>            return res.status(404).send({<br>                message: &quot;User not found &quot;<br>            });<br>        }<br>        res.send({message: &quot;User deleted successfully!&quot;});<br>    }).catch(err =&gt; {         <br>        return res.status(500).send({<br>            message: &quot;Could not delete user with id &quot; + req.params.userid<br>        });<br>    });<br>});<br><br>module.exports = router;</pre><p>In the server.js, immediately after creating the express app, add the following:</p><pre>//create the express app<br>const app = express();<br><br>const keycloak = require(&#39;./config/keycloak.js&#39;).initKeycloak();<br>const memoryStore = require(&#39;./config/keycloak.js&#39;).getMemoryStore();<br><br>app.use(session({<br>    secret: &#39;some secret&#39;,<br>    resave: false,<br>    saveUninitialized: true,<br>    store: memoryStore<br>  }));<br>app.use(keycloak.middleware());</pre><p>So the full server.js is now:</p><pre>//import express and mongoose<br>const express = require(&#39;express&#39;);<br>const mongoose = require(&#39;mongoose&#39;);<br>const session = require(&#39;express-session&#39;);<br><br>//create the express app<br>const app = express();<br><br>const keycloak = require(&#39;./config/keycloak.js&#39;).initKeycloak();<br>const memoryStore = require(&#39;./config/keycloak.js&#39;).getMemoryStore();<br><br>app.use(session({<br>    secret: &#39;some secret&#39;,<br>    resave: false,<br>    saveUninitialized: true,<br>    store: memoryStore<br>  }));<br>app.use(keycloak.middleware());<br><br>app.use(express.urlencoded({extended:true}));<br>app.use(express.json());<br><br>const apicontroller = require(&#39;./controllers/apicontroller&#39;);<br><br>//use the API controller<br>app.use(apicontroller);<br><br>mongoose.Promise = global.Promise;<br><br>// Connecting to the database<br>mongoose.connect(&#39;mongodb://127.0.0.1:27017/users_db&#39;, {<br>    useNewUrlParser: true<br>}).then(() =&gt; {<br>    console.log(&quot;Successfully connected to the database&quot;);    <br>}).catch(error =&gt; {<br>    console.log(&#39;Could not connect to the database.&#39;, error);<br>    process.exit();<br>});<br><br><br>//Application listens on port 8100 <br>app.listen(8100);</pre><h4>Testing the Protected Service</h4><p>Now when you try to execute the GET call to view a User Object, you get an ‘Access Denied’ error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*e3FbXWiz_O2elPOHX7XzhA.png" /></figure><p>So now you need to first generate a Token from Keycloak and use that to call the API endpoints.</p><p>In Postman, navigate to the ‘Authorization’ tab, select ‘Type’ as <strong>OAuth2.0. </strong>Add the following details:</p><p><strong>Grant Type: </strong>Client Credentials</p><p><strong>Token Access URL: </strong><em>This will be the token_endpoint retrieved from the OpenID endpoint configuration.</em><strong><em> e.g:</em></strong></p><pre>&quot;token_endpoint&quot;:&quot;http://localhost:9000/realms/NodeJS-Realm/protocol/openid-connect/token&quot;</pre><p><strong>Client ID: </strong><em>The Client ID created in Keycloak (</em>OAuth2-demo-client<em>)</em></p><p><strong>Client Secret: </strong><em>Copy the value from keycloak.json -&gt; credentials: secret</em></p><p><strong>Scope: </strong>openid</p><p><strong>Client Authentication: </strong>Send Client Credentials in Body</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*qht9YOBEiSWTDWk16EiTXg.png" /></figure><p>Then click on ‘Get New Access Token’ and this will generate a new access token you can use to execute the service API. Once a token is generated:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*vo3P9CIVOi5X1FaQ67EmFg.png" /></figure><p>Click on ‘Use Token’ and then try running the API again, this time you get a response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*S17JMVq9_PpQKjVSNvtlgw.png" /></figure><p>So this is how to secure your node js API using Keycloak with the client_credentials grant type. You can also create fine grained access controls by creating Roles and users and defining which API can by run by which users etc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=196422ddf03a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Elasticsearch with SpringBoot]]></title>
            <link>https://medium.com/@tobintom/introducing-elasticsearch-with-springboot-3707370c2a41?source=rss-8b06c0539df2------2</link>
            <guid isPermaLink="false">https://medium.com/p/3707370c2a41</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[spring-data]]></category>
            <category><![CDATA[elasticsearch-service]]></category>
            <category><![CDATA[spring-data-jpa]]></category>
            <dc:creator><![CDATA[Tobin Tom]]></dc:creator>
            <pubDate>Wed, 11 Jan 2023 04:33:10 GMT</pubDate>
            <atom:updated>2023-01-11T04:33:10.002Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/558/0*k0DjLbsleVc1GW92.png" /></figure><p><strong>Elasticsearch</strong> is a real-time distributed and open source full-text search and analytics engine built on top of Apache Lucene. According to the official website (<a href="https://www.elastic.co/elasticsearch/">https://www.elastic.co/elasticsearch/</a>) <strong>Extract</strong>: <em>Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. </em>Elasticsearch stores data as <strong>documents</strong>(Rows) in <strong>indexes</strong>(Database tables). A user can search through this data using <strong>fields</strong>(Columns).</p><p>In this demo, we will install a standalone version of Elasticsearch interact with it using postman REST calls. We will also create a Spring Boot application and use that as an Elasticsearch client.</p><h3>Installing Elasticsearch</h3><p>In this demo, I installed a local standalone version of Elasticsearch on Windows. If you look at <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html</a>, you can get the zip archive of Elasticsearch and extract it to a suitable folder.</p><p>Once extracted, run the <strong>elasticsearch.bat</strong> batch command. If your environment variables are all correctly setup, this will bring up elasticsearch. When the Elasticsearch server node is fully up, look for something along the lines of the following:</p><blockquote>Elasticsearch security features have been automatically configured!<br>Authentication is enabled and cluster connections are encrypted.</blockquote><blockquote>Password for the <strong>elastic</strong> user (reset with `bin/elasticsearch-reset-password -u elastic`):<br> <strong>PCt5zeXuZGs6=JVWorzk</strong></blockquote><blockquote>HTTP CA certificate SHA-256 fingerprint:<br> b6f8518e585c93282da4454c02d5088a52f0eba0f29093f3b6d2da0fc8f5279f</blockquote><p>SSL is enabled by default (https) and the default id/password is given in the console as highlighted above. It also shows the CA cert fingerprint. Once the instance is up, we can try running REST calls against it.</p><h4>Testing with Postman</h4><p>By default Elasticsearch runs on port 9200. Set the Authorization to <strong>Basic Auth</strong> and set the username and password to the values shown in the console while the server started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yAJlzkPDpWKzEUlkH76Ztg.png" /><figcaption>Setup Basic Auth</figcaption></figure><p>Now you can hit the server with <a href="https://localhost:9200/"><strong>https</strong>://localhost:9200</a> and you should see a response similar to the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*q8fP1oxIhkWwbvdf-FX7fQ.png" /><figcaption>Elasticsearch server responding</figcaption></figure><p>With this, the Elasticsearch instance is installed properly and responding to REST queries. Now we can build a Spring Boot client application.</p><h3>Application Setup</h3><p>We will create a simple Spring Boot application that will have REST calls to interact with the Elasticsearch instance. We will add data, modify it and retrieve it and also execute some custom queries.</p><p>Remember that the Elasticsearch instance that we have setup is secured and runs on https. To invoke it from our Spring Boot application, we will need to configure it with a trust store that can be used from the application. To do that I created a custom <strong>JKS</strong> file containing the self signed certificate of the Elasticsearch instance.</p><p>First I invoked the Elasticsearch instance from a browser and exported the certificate from it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ya_RYB_Oq1kx0EkgEdxRUw.png" /><figcaption>Invoked the Elasticsearch instance and exported the self signed certificate</figcaption></figure><p>I then used a simple jks tool to create a JKS called <strong>key.jks</strong> and its password was set to <strong>test123</strong>. The jks was placed in a folder named <em>keystore</em> inside the /main/resources folder of the Spring Boot application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*B4x1auy7pjI88m2r8J702w.png" /></figure><p><strong>pom.xml</strong></p><pre>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;<br>  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;<br>  &lt;parent&gt;<br>  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>  &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;<br>  &lt;version&gt;2.7.1&lt;/version&gt;<br>  &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;<br>  &lt;/parent&gt; <br>  &lt;groupId&gt;com.demo.elasticsearch&lt;/groupId&gt;<br>  &lt;artifactId&gt;elasticsearchdemo&lt;/artifactId&gt;<br>  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;<br>  &lt;name&gt;ElasticSearchDemo&lt;/name&gt;<br>  &lt;dependencies&gt;<br>   &lt;dependency&gt;<br>  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>  &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br> &lt;/dependency&gt;<br> &lt;dependency&gt;<br>     &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;<br>     &lt;artifactId&gt;spring-data-elasticsearch&lt;/artifactId&gt;<br>   &lt;/dependency&gt;<br>   &lt;dependency&gt;<br>     &lt;groupId&gt;co.elastic.clients&lt;/groupId&gt;<br>     &lt;artifactId&gt;elasticsearch-java&lt;/artifactId&gt;<br>     &lt;version&gt;8.5.3&lt;/version&gt;<br>   &lt;/dependency&gt;<br>   &lt;dependency&gt;<br>      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;<br>      &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;<br>      &lt;version&gt;2.12.3&lt;/version&gt;<br>    &lt;/dependency&gt;<br>   &lt;dependency&gt;<br>   &lt;groupId&gt;jakarta.json&lt;/groupId&gt;<br>   &lt;artifactId&gt;jakarta.json-api&lt;/artifactId&gt;<br>   &lt;version&gt;2.0.1&lt;/version&gt;<br>     &lt;/dependency&gt;<br>  &lt;/dependencies&gt;<br>  &lt;build&gt;<br>  &lt;plugins&gt;<br>   &lt;plugin&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>   &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br> &lt;/build&gt;<br>&lt;/project&gt;</pre><p><strong>application.properties</strong></p><pre>server.port=8081<br>elastic.client.username=elastic<br>elastic.client.password=PCt5zeXuZGs6=JVWorzk<br>elastic.host=localhost<br>elastic.port=9200<br>http.server.ssl.trust-store=classpath:keystore/key.jks<br>http.server.ssl.trust-store-password=test123</pre><p>The entries for elastic.client.username and password were retrieved from the Elasticsearch instance startup. On startup, it printed the username and password as mentioned earlier. The entries for the trust store location and password are also mentioned in the properties file.</p><p><strong>ElasticSearchDemo.java</strong></p><pre>package com.demo.elasticsearch;<br><br>import org.springframework.boot.SpringApplication;<br>import org.springframework.boot.autoconfigure.SpringBootApplication;<br><br>@SpringBootApplication<br>public class ElasticSearchDemo {<br> <br> public static void main(String[] args) {<br>  SpringApplication.run(ElasticSearchDemo.class,args);<br> }<br>}</pre><p><strong>Employee.java</strong></p><pre>package com.demo.elasticsearch.entity;<br><br>import org.springframework.data.annotation.Id;<br>import org.springframework.data.elasticsearch.annotations.Document;<br>import org.springframework.data.elasticsearch.annotations.Field;<br>import org.springframework.data.elasticsearch.annotations.FieldType;<br><br>@Document(indexName = &quot;employeeindex&quot;)<br>public class Employee {<br> <br> @Id<br>    private String id;<br> <br> @Field(type = FieldType.Text, name = &quot;employeeId&quot;)<br> private String employeeId;<br> <br> @Field(type = FieldType.Text, name = &quot;firstName&quot;)<br> private String firstName;<br> <br> @Field(type = FieldType.Text, name = &quot;lastName&quot;)<br> private String lastName;<br> <br> @Field(type = FieldType.Text, name = &quot;street&quot;)<br> private String street;<br> <br> @Field(type = FieldType.Text, name = &quot;city&quot;)<br> private String city;<br> <br> @Field(type = FieldType.Text, name = &quot;state&quot;)<br> private String state;<br> <br> @Field(type = FieldType.Text, name = &quot;zip&quot;)<br> private String zip;<br> <br> @Field(type = FieldType.Text, name = &quot;department&quot;)<br> private String department;<br> <br> @Field(type = FieldType.Double, name = &quot;salary&quot;)<br> private Double salary;<br><br> public String getId() {<br>  return id;<br> }<br><br> public void setId(String id) {<br>  this.id = id;<br> }<br><br> public String getEmployeeId() {<br>  return employeeId;<br> }<br><br> public void setEmployeeId(String employeeId) {<br>  this.employeeId = employeeId;<br> }<br><br> public String getFirstName() {<br>  return firstName;<br> }<br><br> public void setFirstName(String firstName) {<br>  this.firstName = firstName;<br> }<br><br> public String getLastName() {<br>  return lastName;<br> }<br><br> public void setLastName(String lastName) {<br>  this.lastName = lastName;<br> }<br><br> public String getStreet() {<br>  return street;<br> }<br><br> public void setStreet(String street) {<br>  this.street = street;<br> }<br><br> public String getCity() {<br>  return city;<br> }<br><br> public void setCity(String city) {<br>  this.city = city;<br> }<br><br> public String getState() {<br>  return state;<br> }<br><br> public void setState(String state) {<br>  this.state = state;<br> }<br><br> public String getZip() {<br>  return zip;<br> }<br><br> public void setZip(String zip) {<br>  this.zip = zip;<br> }<br><br> public String getDepartment() {<br>  return department;<br> }<br><br> public void setDepartment(String department) {<br>  this.department = department;<br> }<br><br> public Double getSalary() {<br>  return salary;<br> }<br><br> public void setSalary(Double salary) {<br>  this.salary = salary;<br> }<br>}</pre><p>This represents the data entity that will be stored in the Elasticsearch instance. The data is indexed on employeeindex.</p><p><strong>ElasticSearchConfiguration.java</strong></p><pre>package com.demo.elasticsearch.config;<br><br>import javax.net.ssl.SSLContext;<br><br>import org.apache.http.Header;<br>import org.apache.http.HttpHeaders;<br>import org.apache.http.HttpHost;<br>import org.apache.http.auth.AuthScope;<br>import org.apache.http.auth.UsernamePasswordCredentials;<br>import org.apache.http.client.CredentialsProvider;<br>import org.apache.http.impl.client.BasicCredentialsProvider;<br>import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;<br>import org.apache.http.message.BasicHeader;<br>import org.apache.http.ssl.SSLContexts;<br>import org.elasticsearch.client.RestClient;<br>import org.elasticsearch.client.RestClientBuilder;<br>import org.elasticsearch.client.RestHighLevelClient;<br>import org.springframework.beans.factory.annotation.Value;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br>import org.springframework.core.io.Resource; <br>import org.springframework.data.elasticsearch.core.ElasticsearchOperations;<br>import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;<br>import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;<br><br>@Configuration<br>@EnableElasticsearchRepositories(basePackages = &quot;com.demo.elasticsearch.repository&quot;)<br>public class ElasticSearchConfiguration  {<br><br> @Value(&quot;${http.server.ssl.trust-store}&quot;)<br>    private Resource trustStore;<br><br>    @Value(&quot;${http.server.ssl.trust-store-password}&quot;)<br>    private String trustStorePassword;<br>    <br>    @Value(&quot;${elastic.client.username}&quot;)<br>    private String elasticUserName;<br>    <br>    @Value(&quot;${elastic.client.password}&quot;)<br>    private String elasticPassword;<br>    <br>    @Value(&quot;${elastic.host}&quot;)<br>    private String elasticHost;<br>    <br>    @Value(&quot;${elastic.port}&quot;)<br>    private String elasticPort;<br>    <br> <br>    @Bean<br>    public RestHighLevelClient client() { <br>   try {<br>    final SSLContext sslContext = SSLContexts.custom()<br>            .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())<br>            .build();<br>    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();<br> <br>       credentialsProvider.setCredentials(AuthScope.ANY,<br>                  new UsernamePasswordCredentials(elasticUserName, elasticPassword));<br> <br>       //Create a client.<br>       RestClientBuilder builder = RestClient.builder(new HttpHost(elasticHost, Integer.parseInt(elasticPort), &quot;https&quot;))<br>         .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {<br>           @Override<br>           public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {<br>             return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext);<br>               }<br>             }).setDefaultHeaders(compatibilityHeaders());<br>       RestHighLevelClient client = new RestHighLevelClient(builder);<br>         return client;<br>   }catch(Exception e) {<br>    e.printStackTrace();<br>    return null;<br>   }<br>    }<br><br>    @Bean<br>    public ElasticsearchOperations elasticsearchTemplate() {<br>        return new ElasticsearchRestTemplate(client());<br>    }<br>    <br>    private Header[] compatibilityHeaders() {<br>        return new Header[]{new BasicHeader(HttpHeaders.ACCEPT, &quot;application/vnd.elasticsearch+json;compatible-with=7&quot;),<br>          new BasicHeader(HttpHeaders.CONTENT_TYPE, &quot;application/vnd.elasticsearch+json;compatible-with=7&quot;)};<br>     }<br>}</pre><p>This class configures the Spring Boot application to connect with the Elasticsearch instance. It uses the truststore we created and also the username and password values.</p><p><strong>ElasticSearchRepository.java</strong></p><pre>package com.demo.elasticsearch.repository;<br><br>import java.util.List;<br><br>import org.springframework.data.domain.Page;<br>import org.springframework.data.domain.Pageable;<br>import org.springframework.data.elasticsearch.annotations.Query;<br>import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;<br>import org.springframework.stereotype.Repository;<br><br>import com.demo.elasticsearch.entity.Employee;<br><br>@Repository<br>public interface ElasticSearchRepository extends ElasticsearchRepository&lt;Employee, String&gt;{<br> <br> public List&lt;Employee&gt; findByEmployeeId(String employeeId);<br> <br> @Query(&quot;{\&quot;bool\&quot;: {\&quot;must\&quot;: [{\&quot;match\&quot;: {\&quot;department\&quot;: \&quot;?0\&quot;}}]}}&quot;)<br> public Page&lt;Employee&gt; findEmployeesByDepartment(String department, Pageable pageable);<br><br>}</pre><p>This is the repository against the Elasticsearch instance. It also has a custom query method that we will test.</p><p><strong>ElasticSearchController.java</strong></p><pre>package com.demo.elasticsearch.controller;<br><br>import java.util.ArrayList;<br>import java.util.List;<br><br>import org.elasticsearch.index.query.QueryBuilder;<br>import org.elasticsearch.index.query.QueryBuilders; <br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.data.domain.PageRequest; <br>import org.springframework.data.elasticsearch.core.ElasticsearchOperations;<br>import org.springframework.data.elasticsearch.core.SearchHits;<br>import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;<br>import org.springframework.data.elasticsearch.core.query.Criteria;<br>import org.springframework.data.elasticsearch.core.query.CriteriaQuery;<br>import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;<br>import org.springframework.data.elasticsearch.core.query.Query;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.PathVariable;<br>import org.springframework.web.bind.annotation.PostMapping;<br>import org.springframework.web.bind.annotation.RequestBody;<br>import org.springframework.web.bind.annotation.RequestMapping;<br>import org.springframework.web.bind.annotation.RequestParam;<br>import org.springframework.web.bind.annotation.RestController;<br><br>import com.demo.elasticsearch.entity.Employee;<br>import com.demo.elasticsearch.repository.ElasticSearchRepository;<br><br>@RestController<br>@RequestMapping(&quot;/elasticsearch&quot;)<br>public class ElasticSearchController {<br> <br> private static final String DOCUMENT_INDEX = &quot;employeeindex&quot;;<br> <br> @Autowired<br> ElasticSearchRepository repository;<br> <br> private ElasticsearchOperations elasticsearchOperations;<br> <br> public ElasticSearchController(ElasticsearchOperations e) {<br>  super();<br>  this.elasticsearchOperations = e;<br> }<br> <br>  @PostMapping(&quot;/employee&quot;)<br>  public String addUpdateEmployee(@RequestBody Employee newEmployee) {<br>      repository.save(newEmployee);<br>      return &quot;Employee added/modified successfully&quot;;<br>   }<br>  <br>  @GetMapping(&quot;/employees&quot;)<br>   List&lt;Employee&gt; findAllEmployees() {     <br>     Iterable&lt;Employee&gt; emps = repository.findAll();<br>     List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();<br>        emps.iterator().forEachRemaining(employees::add);<br>        return employees;<br>   }<br>  <br>  @GetMapping(&quot;/employees/{id}&quot;)<br>   Employee findEmployee(@PathVariable String id) {     <br>     return repository.findById(id).get();<br>   }<br>  <br>  @GetMapping(&quot;/employee/delete/{id}&quot;)<br>   String deleteEmployee(@PathVariable String id) {     <br>       repository.deleteById(id);<br>       return &quot;Employee Deleted Successfully&quot;;<br>   }<br>  <br>  @GetMapping(&quot;/employeesByDept&quot;)<br>   List&lt;Employee&gt; findEmployeesByDept(@RequestParam String department) {     <br>     Iterable&lt;Employee&gt; emps = repository.findEmployeesByDepartment(department,PageRequest.of(0, 10));<br>     List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();<br>       emps.iterator().forEachRemaining(employees::add);<br>       return employees;<br>   }<br>  <br>  @GetMapping(&quot;/employeesByZip&quot;)<br>   List&lt;Employee&gt; findEmployeesByZip(@RequestParam String zip) {     <br>   QueryBuilder queryBuilder =  QueryBuilders.matchQuery(&quot;zip&quot;, zip);<br>         Query searchQuery = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();<br><br>   SearchHits&lt;Employee&gt; emps = elasticsearchOperations.search(searchQuery, Employee.class,<br>             IndexCoordinates.of(DOCUMENT_INDEX));  <br>      <br>     List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();<br>     emps.forEach(hit -&gt;{<br>      employees.add(hit.getContent());<br>     });<br>      <br>     return employees;<br>   }<br>  <br>  @GetMapping(&quot;/employeesSalGreaterThan9000&quot;)<br>   List&lt;Employee&gt; employeesSalGreaterThan9000() {     <br>   Criteria criteria = new Criteria(&quot;salary&quot;).greaterThan(9000);<br>   Query searchQuery = new CriteriaQuery(criteria);<br><br>   SearchHits&lt;Employee&gt; emps = elasticsearchOperations.search(searchQuery, Employee.class,<br>             IndexCoordinates.of(DOCUMENT_INDEX));  <br>      <br>     List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();<br>     emps.forEach(hit -&gt;{<br>      employees.add(hit.getContent());<br>     });<br>     <br>     return employees;<br>   }<br>}</pre><p>Finally the Controller to expose the REST APIs we will use to interact with Elasticsearch. So with this our application setup is complete and we can test the REST invocations.</p><h3>Testing the application</h3><p>With postman we will be running various REST calls.</p><p><strong>Inserting/Updating Records</strong></p><p>Using the post /employee call, we will insert multiple records:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*yjFSwNIIGy27by0CSN6g6A.png" /><figcaption>Example of a record being added</figcaption></figure><p>Using the same call, multiple records were added. You can also use the same call to update records. If you re-run the same API with a different field value, Elasticsearch updates the record with the updated field information.</p><p>After adding a couple of records, they can all the viewed using the localhost:8081/elasticsearch/employees call:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*5WW6RktEInYbdvO4pIfUUA.png" /><figcaption>View All Records</figcaption></figure><p><strong>Deleting Record</strong></p><p>A record can be deleted using the repository delete method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*5gsr6OUmHAvOrzVkcdOQnw.png" /></figure><h4><strong>SEARCHING FOR RECORDS</strong></h4><p>So there are multiple options to search for records and I have added a few options.</p><h4><strong>1. Using the Repository built in functions.</strong></h4><p>One way is using the Spring Data Repository built in functions. E.g</p><pre>@GetMapping(&quot;/employees/{id}&quot;)<br>   Employee findEmployee(@PathVariable String id) {     <br>     return repository.findById(id).get();<br>   }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*efqiAxrCGKkFeHbrAbUVYw.png" /><figcaption>REST call result</figcaption></figure><h4>2. Using the Repository Custom Queries</h4><p>We can also create custom queries against the repository. In this example, the result should match the department value passed to it.</p><pre>@Query(&quot;{\&quot;bool\&quot;: {\&quot;must\&quot;: [{\&quot;match\&quot;: {\&quot;department\&quot;: \&quot;?0\&quot;}}]}}&quot;)<br> public Page&lt;Employee&gt; findEmployeesByDepartment(String department, Pageable pageable);</pre><pre>@GetMapping(&quot;/employeesByDept&quot;)<br>   List&lt;Employee&gt; findEmployeesByDept(@RequestParam String department) {     <br>     Iterable&lt;Employee&gt; emps = repository.findEmployeesByDepartment(department,PageRequest.of(0, 10));<br>     List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();<br>       emps.iterator().forEachRemaining(employees::add);<br>       return employees;<br>   }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*LR4x4gdDcXGfCT0-oBQ4Xw.png" /><figcaption>REST Call Result</figcaption></figure><h4>3. Using Native Queries</h4><p>Using the repository may not always be feasible especially when you need to run complex queries against Elasticsearch. You have the option to create Native Queries. To run it, you will need to run it against <strong>elasticSearchOperations</strong> instance. It does not use the JPA Repository.</p><pre>@GetMapping(&quot;/employeesByZip&quot;)<br>   List&lt;Employee&gt; findEmployeesByZip(@RequestParam String zip) {     <br>   QueryBuilder queryBuilder =  QueryBuilders.matchQuery(&quot;zip&quot;, zip);<br>         Query searchQuery = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();<br><br>   SearchHits&lt;Employee&gt; emps = elasticsearchOperations.search(searchQuery, Employee.class,<br>             IndexCoordinates.of(DOCUMENT_INDEX));  <br>      <br>     List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();<br>     emps.forEach(hit -&gt;{<br>      employees.add(hit.getContent());<br>     });<br>      <br>     return employees;<br>   }</pre><p>This is a simple example that returns results where the zip matches what was passed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*bbsmutneeeT3GW7GiNvwFw.png" /><figcaption>REST Call Result</figcaption></figure><h4>4. Using Criteria Queries</h4><p>Very similar to the native queries, using criteria.</p><pre>@GetMapping(&quot;/employeesSalGreaterThan9000&quot;)<br>   List&lt;Employee&gt; employeesSalGreaterThan9000() {     <br>   Criteria criteria = new Criteria(&quot;salary&quot;).greaterThan(9000);<br>   Query searchQuery = new CriteriaQuery(criteria);<br><br>   SearchHits&lt;Employee&gt; emps = elasticsearchOperations.search(searchQuery, Employee.class,<br>             IndexCoordinates.of(DOCUMENT_INDEX));  <br>      <br>     List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();<br>     emps.forEach(hit -&gt;{<br>      employees.add(hit.getContent());<br>     });<br>     <br>     return employees;<br>   }</pre><p>In this example, we want employee records with salary &gt; 9000</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*h6weaY3M2eP4vs86oKQf9Q.png" /><figcaption>REST call result</figcaption></figure><p>So that is it, a very quick and simple introduction to using Spring Boot with a secured instance of Elasticsearch. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3707370c2a41" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>