|
| 1 | +# Natural Functions |
| 2 | + |
| 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. |
| 5 | + |
| 6 | +???+ tip "Natural Programming" |
| 7 | + To learn more about Ballerina natural programming library module, see [natural programming](https://central.ballerina.io/ballerinax/np). |
| 8 | + |
| 9 | + |
| 10 | +## Implementation |
| 11 | +Follow the steps below to implement the integration. |
| 12 | + |
| 13 | +### Step 1: Create a new integration project. |
| 14 | +1. Click on the Ballerina Integrator icon on the sidebar. |
| 15 | +2. Click on the **`Create New Integration`** button. |
| 16 | +3. Enter the project name as `NaturalProgramming`. |
| 17 | +4. Select Project Directory and click on the **`Select Location`** button. |
| 18 | +5. Click on the **`Create New Integration`** button to create the integration project. |
| 19 | + |
| 20 | +### Step 2: Define Types |
| 21 | +1. Click on the **`Add Artifacts`** button and select **`Type`** in the **`Other Artifacts`** section. |
| 22 | +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. |
| 24 | + ```json |
| 25 | + { |
| 26 | + "title": "Tips for Growing a Beautiful Garden", |
| 27 | + "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." |
| 28 | + } |
| 29 | + ``` |
| 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 | + ```json |
| 32 | + { |
| 33 | + "name": "Gardening", |
| 34 | + "rating": 5 |
| 35 | + } |
| 36 | + ``` |
| 37 | +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> |
| 39 | + |
| 40 | + |
| 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> |
| 48 | + |
| 49 | +### Step 4: Add a Natural Function |
| 50 | +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. |
| 55 | + ```plaintext |
| 56 | + You are an expert content reviewer for a blog site that |
| 57 | + categorizes posts under the following categories: "Gardening", "Sports", "Health", "Technology", "Travel" |
| 58 | + |
| 59 | + Your tasks are: |
| 60 | + 1. Suggest a suitable category for the blog from exactly the specified categories. |
| 61 | + If there is no match, use null. |
| 62 | + |
| 63 | + 2. Rate the blog post on a scale of 1 to 10 based on the following criteria: |
| 64 | + - **Relevance**: How well the content aligns with the chosen category. |
| 65 | + - **Depth**: The level of detail and insight in the content. |
| 66 | + - **Clarity**: How easy it is to read and understand. |
| 67 | + - **Originality**: Whether the content introduces fresh perspectives or ideas. |
| 68 | + - **Language Quality**: Grammar, spelling, and overall writing quality. |
| 69 | + |
| 70 | + Here is the blog post content: |
| 71 | + |
| 72 | + Title: ${blog.title} |
| 73 | + Content: ${blog.content} |
| 74 | + ``` |
| 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 |
| 87 | +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. |
| 98 | + |
| 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> |
| 102 | + |
| 103 | + |
| 104 | +### Step 8: Run the integration |
| 105 | + |
| 106 | +!!! warning "Response May Vary" |
| 107 | + Since this integration involves an LLM (Large Language Model) call, the response values may not always be identical across different executions. |
| 108 | + |
| 109 | +1. Click on the **`Run`** button in the top-right corner to run the integration. |
| 110 | +2. The integration will start and the service will be available at `http://localhost:9090/blogs`. |
| 111 | +3. Click on the **`Try it`** button to open the embedded HTTP client. |
| 112 | +4. Enter the blog content in the request body and click on the ▶️ button to send the request. |
| 113 | + ```json |
| 114 | + { |
| 115 | + "title": "The Healthy Maven", |
| 116 | + "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." |
| 117 | + } |
| 118 | + ``` |
| 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> |
0 commit comments