Skip to main content

Interact with a Service

In this section we describe how your Skill can communicate with a Service (or the Service component of a HardwareDevice).

Prerequisites

Before attempting this guide, you should:

  1. Deploy a Solution
  2. Set up your development environment
  3. Connect to an Organization
  4. Know how to create Skills
  5. Understand the Skill interface

Take a look at the Calculator Service. This service performs basic calculator operations.

Let's create a Skill to add two numbers using this Service. Create a Skill with the following settings:

  • Language: Python
  • Skill ID: com.example.adder

Declare a dependency on the Calculator gRPC service

The Skill needs to specify a dependency in its Skill manifest on the intrinsic_proto.services.Calculator gRPC service provided by the Calculator Service. Add the following dependency to your Skill parameters in adder/adder.proto:

import "intrinsic/assets/proto/field_metadata.proto";
import "intrinsic/assets/proto/v1/resolved_dependency.proto";

message AdderParams {
intrinsic_proto.assets.v1.ResolvedDependency calculator = 1
[(intrinsic_proto.assets.field_metadata).dependency = {
requires: "grpc://intrinsic_proto.services.Calculator"
}];
}

Also add the corresponding dependencies in your BUILD file. Add the following to the deps attribute of your "adder_proto" proto_library target in adder/BUILD:

"@ai_intrinsic_sdks//intrinsic/assets/proto:field_metadata_proto",
"@ai_intrinsic_sdks//intrinsic/assets/proto/v1:resolved_dependency_proto",

Prepare the Skill implementation

The Skill will need to create a gRPC client to communicate with the Calculator service. To do this, we will use the connect util function provided in intrinsic.assets.dependencies.utils.

Add the following imports to adder.py:

from intrinsic.assets.dependencies import utils
from intrinsic.assets.services.examples.calcserver import calc_server_pb2
from intrinsic.assets.services.examples.calcserver import calc_server_pb2_grpc

Also add the corresponding dependencies in your BUILD file. Add the following to the deps attribute of your "adder" py_library target in adder/BUILD:

"@ai_intrinsic_sdks//intrinsic/assets/dependencies:utils_py",
"@ai_intrinsic_sdks//intrinsic/assets/services/examples/calcserver:calc_server_py_pb2",
"@ai_intrinsic_sdks//intrinsic/assets/services/examples/calcserver:calc_server_py_pb2_grpc",
warning

A Skill should only connect to either Services that it has specified as dependencies via its parameters or platform services provided via its context. Circumventing this constraint by hardcoding known service addresses can lead to unexpected behavior.

Implement the adder Skill

Add the following fields to adder/adder.proto. These fields represent the two numbers to add:

message AdderParams {
...

int32 x = 2;
int32 y = 3;
}

The adder Skill must create a gRPC client to the Calculator service, and then tell it to add the two numbers.

Implement your execute method as follows in adder/adder.py:

def execute(
self,
request: skill_interface.ExecuteRequest[adder_pb2.AdderParams],
context: skill_interface.ExecuteContext,
) -> None:
channel = utils.connect(
request.params.calculator,
"grpc://intrinsic_proto.services.Calculator",
)
stub = calc_server_pb2_grpc.CalculatorStub(channel)

response = stub.Calculate(
calc_server_pb2.CalculatorRequest(
operation=calc_server_pb2.CALCULATOR_OPERATION_ADD,
x=request.params.x,
y=request.params.y,
)
)

logging.info("Result: %d", response.result)

return None

Install the Skill into your Solution.

Add Calculator Service instance to your Solution

In order to use your new Skill, there must be a Calculator Service instance actually running in your Solution for your Skill to interact with.

  1. In the Solution Editor, click on the Services tab on the right sidebar, then click Add Service.
  2. Use the search bar to search for "calculator".
  3. Select the ai.intrinsic.calculator_service, then click Add to add an instance of it to your Solution.
  4. In the newly opened Add configuration to Service dialog, set the Service name field to "my_calculator".
  5. When configuring the Skill node in your Process that will call the adder Skill, you can now select the "my_calculator" instance from the dropdown list to resolve the Skill's calculator dependency.

What's next