Build a Knowledge Base¶
In this tutorial, you'll learn how to create an Integration API to add and retrieve knowledge from a knowledge base using WSO2 MI, enabling seamless integration and deployment with minimal hassle.
What you'll build¶
PineValley Bank needs to create a knowledge base to store its product-related documents and enable efficient search capabilities. To achieve this, the bank has decided to use a vector database as the solution. In this tutorial, we will build a vector database and access it using WSO2 Micro Integrator (MI) with the help of the AI Module. This integration will allow the bank to provide accurate and efficient responses to customer inquiries by leveraging advanced search and retrieval mechanisms.
In this tutorial, you will build two API resources to interact with the vector database:
-
Add Data to Knowledge Base: This resource allows you to add information to the vector database. It processes the input data, generates embeddings using an AI model, and stores the data in the vector database for efficient retrieval.
-
Retrieve Data from Knowledge Base: This resource enables you to query the vector database and retrieve relevant information based on the input query. It uses embeddings to perform similarity searches and returns the most relevant results.
These resources will form the foundation of your knowledge base API, enabling seamless data storage and retrieval.
What you'll learn¶
- Create an integration to access and manage a knowledge base efficiently.
- Seamlessly deploy and test the integration.
Prerequisites¶
-
You need Visual Studio Code (VS Code) with the Micro Integrator for VS Code extension installed. The MI for VS Code extension is the official developer tool for designing, developing, and testing integration solutions with WSO2 Micro Integrator.
Info
See the Install Micro Integrator for VS Code documentation to learn how to install Micro Integrator for VS Code.
-
In the earlier tutorial, we created a new project in the Micro Integrator extension for VS Code. You can continue using that same project here. If you haven’t created a project yet, follow the steps in the Create a new project documentation to set one up.
Follow the steps below to create the Knowledge Base API.
Step 1 - Create an API¶
Let's create these APIs step by step.
-
In the Add Artifact interface, under Create an Integration, click on API. This opens the API Form.
-
Enter
KnowledgeBaseAPI
as the API Name. The API Context field will be automatically populated with the same value. -
Click Create.
Once you create the API, a default resource will be automatically generated. You can see this default resource listed in the Service Designer under Available resources. You'll use this resource in this tutorial.
Step 3 - Design the integration¶
Now it's time to design your API. This is the underlying logic that's executed behind the scenes when an API request is made. In this scenario, you need to process user requests to add data to or retrieve data from the Knowledge Base. For that, you have to add the Add to Knowledge and Get from Knowledge operations of the AI Module.
Follow the steps below to design these operations.
-
Edit the default resource to
POST
and change resource path toaddToStore
by clicking the three dots icon in the resource. -
Create a new resource with
POST
and set the resource path togetFromStore
by clicking on the + Resource button.Let's design the
addToStore
resource first. -
Open the Resource View of the
addToStore
API resource by clicking thePOST
resource under Available resources on Service Designer. -
Define the expected payload in the start node to streamline the development process.
Note
Setting an input payload for the integration flow is not mandatory. However, it is recommended, as it will be used to enable expression suggestions, which you will explore in later steps of this tutorial.
Below is the expected payload for this API resource:
{ "content" : "This is a sample content" }
-
Once you open the Resource View, click on the + icon on the canvas to open the Mediator Palette.
-
Under Mediators, go to AI section and select the Add to Knowledge operation.
-
Select the + Add new connection in the Vector Store Connection field.
-
On the Add New Connection page, choose your desired
Vector Database
. For this tutorial, we will be usingMI_VECTOR_STORE
as the database.Note
MI_VECTOR_STORE
is a built-in vector store that comes with the AI Module. It is a simple in-memory vector store that is suitable for development and testing purposes.Warning
The
MI_VECTOR_STORE
is not recommended for production use. For production, you can use any of the supported vector stores such asPinecone
,Chroma DB
,PGVector
. -
Complete the connection form by entering
KB_CONN
as the Connection Name. -
Next, add the embedding model connection. You can reuse the existing OpenAI connection (
OPENAI_CONN
) created for the LLM in the previous tutorial as the embedding model connection.If you don't have an existing connection, you can create a new one by following these steps:
- In the Embedding Model Connection field, click on the Add new connection link.
- Choose OpenAI as the connection type from the available options.
- Provide
OPENAI_CONN
as the Connection Name. - Enter your API Key in the designated field.
- Click Add to finalize and store the connection.
-
Now, we shall complete the Add To Knowledge operation configuration by filling the
Input
field and disabling theParse
.Note
Enable parsing only when the input is an encoded string. If you are sending plain text content, you can safely disable the
Parse
option.Now let's add a Respond Mediator to respond the message to the client.
-
Click on the + icon placed just after the Chat operation to open the Mediator Palette.
-
Select the Respond mediator from the Mediator Palette, and click Add to add it to the integration flow.
Now we have completed the
addToStore
resource. Let's move on to thegetFromStore
resource. -
Open the Resource View of the
getFromStore
API resource from the project explorer on the left side. -
Define the expected payload in the start node to streamline the development process.
Below is the expected payload for this API resource:
{ "content" : "This is a sample content" }
-
Select the
Get from Knowledge
operation from the mediator panel. -
Complete the form with the following values.
Vector Store Connection:
KB_CONN
Embedding Model Connection:OPENAI_CONN
Input:payload.content
(withEX
enabled)
Overwrite Message Body:true
Now let's add a Respond Mediator to respond the message to the client.
-
Click the + icon placed just after the Chat operation to open the Mediator Palette.
-
Select the Respond mediator from the Mediator Palette, and click Add to add it to the integration flow.
You may refer to the following API configuration for reference,
Knowledge Base API
Info
You can view the source view by clicking on the Show Source (</>
) icon located in the top right corner of the VS Code.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/knowledgebaseapi" name="KnowledgeBaseAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/addToStore">
<inSequence>
<ai.addToKnowledge>
<connections>
<embeddingConfigKey>OPENAI_CONN</embeddingConfigKey>
<vectorStoreConfigKey>KB_CONN</vectorStoreConfigKey>
</connections>
<input>{${payload.content}}</input>
<needParse>false</needParse>
<needSplit>true</needSplit>
<splitStrategy>Recursive</splitStrategy>
<maxSegmentSize>1000</maxSegmentSize>
<maxOverlapSize>200</maxOverlapSize>
<needEmbedding>true</needEmbedding>
<embeddingModel>text-embedding-3-small</embeddingModel>
<responseVariable>ai_addToKnowledge_1</responseVariable>
<overwriteBody>true</overwriteBody>
</ai.addToKnowledge>
<respond/>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
<resource methods="POST" uri-template="/getFromStore">
<inSequence>
<ai.getFromKnowledge>
<connections>
<embeddingConfigKey>OPENAI_CONN</embeddingConfigKey>
<vectorStoreConfigKey>KB_CONN</vectorStoreConfigKey>
</connections>
<input>{${payload.content}}</input>
<needEmbedding>true</needEmbedding>
<embeddingModel>text-embedding-3-small</embeddingModel>
<maxResults>5</maxResults>
<minScore>0.75</minScore>
<responseVariable>ai_getFromKnowledge_1</responseVariable>
<overwriteBody>true</overwriteBody>
</ai.getFromKnowledge>
<respond/>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
</api>
Step 4 - Run the Integration API¶
Now that you have developed an integration using the Micro Integrator for the Visual Studio Code plugin, it's time to deploy the integration to the Micro Integrator server runtime.
Click the Build and Run icon located in the top right corner of VS Code.
Step 5 - Test the Integration API¶
Now, let's test the Integration API. For that, you can use the inbuilt try-it functionality in the MI for VS Code extension.
When you run the integration as in Step 4, the Runtime Services interface is opened up. You can see all the available services.
-
Select
KnowledgeBaseAPI
that you have developed and test the resource.First, let's test the
addToStore
resource. -
Expand the
POST /addToStore
resource and click on the Try it button. -
Click on the Try it out button to try out the resource.
-
Let's give a sample content as the request payload. Below is the product brochure of the High-Interest Savings Account (HISA) offered by PineValley Bank. Let us add this to the knowledge base.
You can use the following JSON payload to add the above content to the knowledge base,Article: Overview of High-Interest Savings Account (HISA) Document Title: PineValley Bank – HISA Product Brochure Last Updated: March 2025 Document Type: Product Brochure Applies To: All US residents aged 18 and older Product Summary A High-Interest Savings Account (HISA) offers a competitive interest rate to help you grow your savings faster while maintaining easy access to your funds. Key Features - Interest Rate: 3.5% per annum - Minimum Balance: $1,000 - Withdrawals: Unlimited and free - Eligibility: US residents, 18+ years old Onboarding Requirements To open a HISA with PineValley Bank: 1. Provide a valid government-issued ID 2. Upload proof of address (e.g., utility bill) 3. Agree to HISA terms & conditions via e-signature Estimated completion time: 10 minutes
{ "content": "Article: Overview of High-Interest Savings Account (HISA)\nDocument Title: PineValley Bank – HISA Product Brochure\nLast Updated: March 2025\nDocument Type: Product Brochure\nApplies To: All US residents aged 18 and older\n\nProduct Summary\nA High-Interest Savings Account (HISA) offers a competitive interest rate to help you grow your savings faster while maintaining easy access to your funds.\n\nKey Features\n\n- Interest Rate: 3.5% per annum\n- Minimum Balance: $1,000\n- Withdrawals: Unlimited and free\n- Eligibility: US residents, 18+ years old\n\nOnboarding Requirements\nTo open a HISA with PineValley Bank:\n\n1. Provide a valid government-issued ID\n2. Upload proof of address (e.g., utility bill)\n3. Agree to HISA terms & conditions via e-signature\n\nEstimated completion time: 10 minutes" }
-
Click on the Execute button to execute the request.
Now that we have added some knowledge to the knowledge base, let's proceed to retrieve it using the getFromStore
resource.
Let us retrieve the information available in the knowledge base about the savings account products offered by PineValley Bank.
-
Scroll down to the
POST /getFromStore
resource and expand it. -
Click on the Try it out button to try out the resource.
-
Let's give a keyword as the request payload to retrieve the stored data.
Here is a sample request payload to retrieve the stored data,
{ "content": "High Interest Savings Account" }
-
Click on the Execute button to execute the request.
Congratulations!
You have successfully created your first Knowledge Base Integration API, enabling seamless interaction with a knowledge base.
What's Next?¶
So far, you have built a knowledge base and exposed it through an API. In the next tutorial, you will learn how to integrate this knowledge base with a Large Language Model (LLM) to enable Retrieval-Augmented Generation (RAG). This will allow you to create more advanced, context-aware integrations for enhanced user experiences.
Click on the Next button below to continue to the next tutorial.