Natural Functions¶
In this tutorial, you will create and use a natural function using the WSO2 Integrator: BI. A natural function allows the logic of the function to be described in natural language and is executed at runtime with a call to a Large Language Model (LLM), with the natural language instructions as the prompt. The tutorial uses a natural function to analyze blog content to suggest a suitable category and rate it on a scale of 1 to 10 based on specified criteria.
Natural Programming
To learn more about natural programming and natural functions, see Natural Language is Code: A hybrid approach with Natural Programming.
Step 1: Create a new integration project¶
- Click on the BI icon on the sidebar.
- Click on the
Create New Integrationbutton. - Enter
BlogRevieweras the project name. - Select Project Directory and click on the
Select Locationbutton. - Click on the
Create New Integrationbutton to create the integration project.
Step 2: Define Types¶
- Click on the
Add Artifactsbutton and selectTypein theOther Artifactssection. - Click on
+ Add Typeto add a new type and switch to theImportsection. -
Enter
Blogas theName, paste the following JSON payload, and then click theImportbutton.{ "title": "Tips for Growing a Beautiful Garden", "content": "Spring is the perfect time to start your garden. Begin by preparing your soil with organic compost and ensure proper drainage. Choose plants suitable for your climate zone, and remember to water them regularly. Don't forget to mulch to retain moisture and prevent weeds." } -
Add another type with
Reviewas theNameand paste the following JSON payload. Then click theImportbutton.{ "suggestedCategory": "Gardening", "rating": 5 } -
The types are now available in the project.
BlogandRevieware the types that represent the blog content and review respectively.
Step 3: Add a Natural Function¶
- Click on the
Add Artifactbutton and selectNatural Functionunder theOther Artifactscategory. -
Use
reviewBlogas the name of the function. Then click theAdd Parameterbutton to add a parameter of typeBlognamedblog. UseReviewas the return type and convert it to nilable type using type operators. Then click on theCreatebutton. -
Click on the
Editbutton to specify the requirement in natural language (i.e., the prompt). -
Use the following prompt and click on the
Savebutton. Note how interpolations refer to theblogparameter.You are an expert content reviewer for a blog site that categorizes posts under the following categories: "Gardening", "Sports", "Health", "Technology", "Travel" Your tasks are: 1. Suggest a suitable category for the blog from exactly the specified categories. If there is no match, use null. 2. Rate the blog post on a scale of 1 to 10 based on the following criteria: - **Relevance**: How well the content aligns with the chosen category. - **Depth**: The level of detail and insight in the content. - **Clarity**: How easy it is to read and understand. - **Originality**: Whether the content introduces fresh perspectives or ideas. - **Language Quality**: Grammar, spelling, and overall writing quality. Here is the blog post content: Title: ${blog.title} Content: ${blog.content}
Step 4: Create an HTTP service¶
- In the design view, click on the
Add Artifactbutton. - Select
HTTP Serviceunder theIntegration as APIcategory. - Select the
Create and use the default HTTP listener (port: 9090)option from theListenersdropdown. - Select the
Design from Scratchoption as theService Contractand use/blogsas theService base path. -
Click on the
Createbutton to create the new service with the specified configurations. -
The service will have a default resource named
greetingwith theGETmethod. Click on the three dots that appear in front of the/blogsservice and selectEditfrom the menu. - Then click the
Editbutton in front of/greetingresource. - Change the resource HTTP method to
POST. - Change the resource name to
review. - Click on
Add Payloadand specifyblogas the name andBlogas the type. - Change the 201 response return type to
Review. -
Click on the
Savebutton to update the resource with the specified configurations.
Step 5: Implement the resource logic¶
- Click on the
reviewresource to navigate to the resource implementation designer view. - Hover over the arrow after start and click the ➕ button to add a new action to the resource.
- Select
Call Natural Functionfrom the node panel. - Select the
reviewBlogfunction from the suggestions. -
For the
Blogparameter, useblogas the argument and click on theSavebutton. -
Add a new node after the
reviewBlogfunction call and selectReturnfrom the node panel. -
Select the
reviewvariable from the dropdown and clickSave. -
The resource implementation is now complete. The function
reviewBlogis called with theblogcontent as input, and thereviewis returned as the response.
Step 6: Configure model for natural function¶
-
Press
Ctrl + Shift + Pon Windows and Linux, orShift + ⌘ + Pon a Mac, and type>Ballerina: Configure default model for natural functions (Experimental)to configure the default model for natural functions.
Step 7: Run the integration¶
Response May Vary
Since this integration involves an LLM (Large Language Model) call, the response values may not always be identical across different executions.
- Click on the
Runbutton in the top-right corner to run the integration. - The integration will start and the service will be available at
http://localhost:9090/blogs. - Click on the
Try itbutton to open the embedded HTTP client. -
Enter the blog content in the request body and click on the ▶️ button to send the request.
{ "title": "The Healthy Maven", "content": "For those who want a 360-degree approach to self-care, with advice for betterment in the workplace, home, gym, and on the go, look no further. The Healthy Maven offers recipes for every type of meal under the sun (salads, sides, soups, and more), DIY tips (you’ll learn how to make your own yoga mat spray), and quick workouts. If you like where all this is going, there’s a supplementary podcast run by blogger Davida with guest wellness experts." } -
The blog content is analyzed by the natural function to suggest a category and rate it based on predefined criteria.








