Skip to content

Commit a73cf2c

Browse files
Merge pull request #77 from SasinduDilshara/update-natural-functions
Update the natural function documentation with newest changes
2 parents 731f178 + 02e8337 commit a73cf2c

File tree

11 files changed

+69
-57
lines changed

11 files changed

+69
-57
lines changed
196 KB
Loading
-595 KB
Loading
226 KB
Loading
346 KB
Loading
-10.7 KB
Loading
200 KB
Loading
-112 KB
Loading
105 KB
Loading
161 KB
Loading
Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
# Natural Functions
22

3-
In this tutorial, you will learn how to use natural functions in Ballerina Integrator, which allow the function to contain instructions in natural language.
4-
Such a function is evaluated at runtime with a call to an LLM. The example uses a natural function to analyze blog content to suggest a category and rate it based on predefined criteria.
3+
In this tutorial, you will create and use a natural function using the Ballerina Integrator. 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.
4+
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.
55

66
???+ tip "Natural Programming"
7-
To learn more about Ballerina natural programming library module, see [natural programming](https://central.ballerina.io/ballerinax/np).
7+
To learn more about natural programming and natural functions, see [Natural Language is Code: A hybrid approach with Natural Programming](https://blog.ballerina.io/posts/2025-04-26-introducing-natural-programming/).
88

99

1010
## Implementation
1111
Follow the steps below to implement the integration.
1212

13-
### Step 1: Create a new integration project.
13+
### Step 1: Create a new integration project
1414
1. Click on the Ballerina Integrator icon on the sidebar.
1515
2. Click on the **`Create New Integration`** button.
16-
3. Enter the project name as `NaturalProgramming`.
16+
3. Enter `BlogReviewer` as the project name.
1717
4. Select Project Directory and click on the **`Select Location`** button.
1818
5. Click on the **`Create New Integration`** button to create the integration project.
1919

2020
### Step 2: Define Types
2121
1. Click on the **`Add Artifacts`** button and select **`Type`** in the **`Other Artifacts`** section.
2222
2. Click on **`+ Add Type`** to add a new type.
23-
3. Add the **`Record Name`** as `Blog` and paste the following JSON payload. Select **`Is Closed Record`** and click on the **`Import`** button.
23+
3. Use `Blog` as the **`Name`**. Then click on the **`JSON`** button and paste the following JSON payload. Tick **`Is Closed`** and click on the **`Import`** button. Then click the **`Save`** button.
24+
2425
```json
2526
{
2627
"title": "Tips for Growing a Beautiful Garden",
2728
"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."
2829
}
2930
```
30-
4. Add another type with the **`Record Name`** as `Review` and paste the following JSON payload. Select **`Is Closed Record`** and click on the **`Import`** button.
31+
32+
4. Add another type with `Review` as the **`Name`** and paste the following JSON payload. Select **`Is Closed`** and click on the **`Import`** button. Then click the **`Save`** button.
33+
3134
```json
3235
{
33-
"name": "Gardening",
36+
"suggestedCategory": "Gardening",
3437
"rating": 5
3538
}
3639
```
40+
3741
5. The types are now available in the project. `Blog` and `Review` are the types that represent the blog content and review respectively.
38-
<a href="{{base_path}}/assets/img/natural-functions/types.png"><img src="{{base_path}}/assets/img/natural-functions/types.png" alt="Types" width="70%"></a>
3942

43+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/types.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/types.png" alt="Types" width="70%"></a>
4044

41-
### Step 3: Create an HTTP service.
42-
1. In the design view, click on the **`Add Artifact`** button.
43-
2. Select **`HTTP Service`** under the **`Integration as API`** category.
44-
3. Select the **`Create and use the default HTTP listener`** option from the **`Listener`** dropdown.
45-
4. Add the service base path as `/blogs` and select the **`Design from Scratch`** option as the **`The contract of the service`**.
46-
5. Click on the **`Create`** button to create the new service with the specified configurations.
47-
<a href="{{base_path}}/assets/img/natural-functions/service.png"><img src="{{base_path}}/assets/img/natural-functions/service.png" alt="HTTP Service" width="70%"></a>
4845

49-
### Step 4: Add a Natural Function
46+
### Step 3: Add a Natural Function
5047
1. Click on the **`Add Artifact`** button and select **`Natural Function`** under the **`Other Artifacts`** category.
51-
2. Add the function name as `reviewBlog`, input parameter as `blog` of type `Blog`, return type of `Review` and click on the **`Create`** button.
52-
<a href="{{base_path}}/assets/img/natural-functions/natural-function.png"><img src="{{base_path}}/assets/img/natural-functions/natural-function.png" alt="Natural Function" width="70%"></a>
53-
3. Click on the **`Edit`** button to add the function logic.
54-
4. Add the following prompt to the function and click on the **`Save`** button.
48+
2. Use `reviewBlog` as the name of the function. Then click the **`Add Parameter`** button to add a parameter of type `Blog` named `blog`. Use `Review` as the return type and convert it to nilable type using type operators. Then click on the **`Create`** button.
49+
50+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/natural-function.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/natural-function.png" alt="natural function"></a>
51+
52+
3. Click on the **`Edit`** button to specify the requirement in natural language (i.e., the prompt).
53+
4. Use the following prompt and click on the **`Save`** button. Note how interpolations refer to the `blog` parameter.
54+
5555
```plaintext
5656
You are an expert content reviewer for a blog site that
5757
categorizes posts under the following categories: "Gardening", "Sports", "Health", "Technology", "Travel"
@@ -72,36 +72,52 @@ Follow the steps below to implement the integration.
7272
Title: ${blog.title}
7373
Content: ${blog.content}
7474
```
75-
76-
### Step 5: Update the resource method
77-
1. The service will have a default resource named `greeting` with the **`GET`** method. Click on three dots appear in front of the `/convert` service resource and select **`Edit`** from menu.
78-
2. Then click the edit button in front of `/greeting` resource.
79-
3. Change the resource HTTP method to **`POST`**.
80-
4. Change the resource name as `review`.
81-
5. Add a payload parameter named `blog` to the resource of type `Blog`.
82-
6. Change the 201 response return type to `Review`.
83-
7. Click on the **`Save`** button to update the resource with the specified configurations.
84-
<a href="{{base_path}}/assets/img/natural-functions/update-resource.png"><img src="{{base_path}}/assets/img/natural-functions/update-resource.png" alt="Resource" width="70%"></a>
85-
86-
### Step 6: Implement resource logic
75+
76+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/natural-function-view.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/natural-function-view.png" alt="natural function view" width="70%"></a>
77+
78+
79+
### Step 4: Create an HTTP service
80+
1. In the design view, click on the **`Add Artifact`** button.
81+
2. Select **`HTTP Service`** under the **`Integration as API`** category.
82+
3. Select the **`Create and use the default HTTP listener (port: 9090)`** option from the **`Listeners`** dropdown.
83+
4. Select the **`Design from Scratch`** option as the **`Service Contract`** and use `/blogs` as the **`Service base path`**.
84+
5. Click on the **`Create`** button to create the new service with the specified configurations.
85+
86+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/service.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/service.png" alt="HTTP Service" width="70%"></a>
87+
88+
6. The service will have a default resource named `greeting` with the **`GET`** method. Click on the three dots that appear in front of the `/blogs` service and select **`Edit`** from the menu.
89+
7. Then click the **`Edit`** button in front of `/greeting` resource.
90+
8. Change the resource HTTP method to **`POST`**.
91+
9. Change the resource name to `review`.
92+
10. Click on **`Add Payload`** and specify `blog` as the name and `Blog` as the type.
93+
11. Change the 201 response return type to `Review`.
94+
12. Click on the **`Save`** button to update the resource with the specified configurations.
95+
96+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/update-resource.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/update-resource.png" alt="Resource" width="70%"></a>
97+
98+
### Step 5: Implement the resource logic
8799
1. Click on the `review` resource to navigate to the resource implementation designer view.
88-
2. Delete the default `Return` action from the resource.
89-
3. Hover to the arrow after start and click the ➕ button to add a new action to the resource.
90-
4. Select **`Call Function Call`** from the node panel.
91-
5. Select the `reviewBlog` function from the suggestions.
92-
6. Change the **`Variable Name`** to `review`, **`Variable Type`** as `Review` and **`Blog`** to `blog` and click on the **`Save`** button.
93-
<a href="{{base_path}}/assets/img/natural-functions/call-np.gif"><img src="{{base_path}}/assets/img/natural-functions/call-np.gif" alt="Function Call" width="70%"></a>
94-
7. Add a new node after the `reviewBlog` function call and select **`Return`** from the node panel.
95-
8. Select the `reviewResult` variable from the dropdown and click **`Save`**.
96-
<a href="{{base_path}}/assets/img/natural-functions/add-return.png"><img src="{{base_path}}/assets/img/natural-functions/add-return.png" alt="Add Return" width="70%"></a>
97-
9. The resource implementation is now complete. The function `reviewBlog` is called with the blog content as input and the review is returned as the response.
100+
2. Hover over the arrow after start and click the ➕ button to add a new action to the resource.
101+
3. Select **`Call Natural Function`** from the node panel.
102+
4. Select the `reviewBlog` function from the suggestions.
103+
5. For the **`Blog`** parameter, use `blog` as the argument and click on the **`Save`** button.
104+
105+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/call-np.gif"><img src="{{base_path}}/assets/img/learn/references/natural-functions/call-np.gif" alt="Function Call"></a>
106+
107+
6. Add a new node after the `reviewBlog` function call and select **`Return`** from the node panel.
108+
7. Select the `review` variable from the dropdown and click **`Save`**.
109+
110+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/add-return.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/add-return.png" alt="Add Return" width="70%"></a>
111+
112+
8. The resource implementation is now complete. The function `reviewBlog` is called with the `blog` content as input, and the `review` is returned as the response.
98113

99-
### Step 7: Configure model for natural function
100-
1. Press `Ctrl + Shift + P` on Windows and Linux, or `shift + ⌘ + P` on a Mac and type `>Ballerina: Configure default model for natural functions` to configure the default model for natural functions.
101-
<a href="{{base_path}}/assets/img/natural-functions/configure-model.png"><img src="{{base_path}}/assets/img/natural-functions/configure-model.png" alt="Configure Model" width="70%"></a>
114+
### Step 6: Configure model for natural function
115+
1. Press `Ctrl + Shift + P` on Windows and Linux, or `Shift + ⌘ + P` on a Mac, and type `>Ballerina: Configure default model for natural functions (Experimental)` to configure the default model for natural functions.
102116

117+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/configure-model.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/configure-model.png" alt="Configure Model" width="70%"></a>
103118

104-
### Step 8: Run the integration
119+
120+
### Step 7: Run the integration
105121

106122
!!! warning "Response May Vary"
107123
Since this integration involves an LLM (Large Language Model) call, the response values may not always be identical across different executions.
@@ -110,18 +126,14 @@ Follow the steps below to implement the integration.
110126
2. The integration will start and the service will be available at `http://localhost:9090/blogs`.
111127
3. Click on the **`Try it`** button to open the embedded HTTP client.
112128
4. Enter the blog content in the request body and click on the ▶️ button to send the request.
129+
113130
```json
114131
{
115132
"title": "The Healthy Maven",
116133
"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."
117134
}
118135
```
119-
5. The response will be a review of the blog content with the category and rating.
120-
```json
121-
{
122-
"name": "Health",
123-
"rating": 8
124-
}
125-
```
126-
6. The blog content is analyzed by the natural function to suggest a category and rate it based on predefined criteria.
127-
<a href="{{base_path}}/assets/img/natural-functions/run-integration.png"><img src="{{base_path}}/assets/img/natural-functions/run-integration.png" alt="Run Integration" width="70%"></a>
136+
137+
5. The blog content is analyzed by the natural function to suggest a category and rate it based on predefined criteria.
138+
139+
<a href="{{base_path}}/assets/img/learn/references/natural-functions/run-integration.png"><img src="{{base_path}}/assets/img/learn/references/natural-functions/run-integration.png" alt="Run Integration" width="70%"></a>

0 commit comments

Comments
 (0)