Introduction to Inline Agents¶
In this tutorial, you'll learn how to connect an AI agent to a GraphQL service, enabling the agent to be invoked directly within a GraphQL resolver. This demonstrates the use of an inline agent—a powerful capability in the WSO2 Integrator: BI.
Unlike chat agents, which are exposed as REST APIs for external interaction, inline agents are not tied to an API endpoint. Instead, they can be invoked programmatically from anywhere within your integration logic, just like a regular function call.
In this example, we'll define a GraphQL schema with a query that invokes the inline agent to generate dynamic responses based on input parameters. The agent runs within the resolver logic and returns results directly as part of the GraphQL response.
Prerequisites¶
- Sign up at OpenAI.
- Get an API key from the API section.
Step 1: Create a new integration project¶
- Click on the BI icon in the sidebar.
- Click on the Create New Integration button.
- Enter the project name as
GraphqlService
. - Select the project directory by clicking on the Select Location button.
-
Click the Create New Integration button to generate the integration project.
Step 2: Create a GraphQL service¶
- Click the + button on the WSO2 Integrator: BI side panel or navigate back to the design screen and click on Add Artifact.
- Select GraphQL Service under the Integration as API artifacts.
-
Keep the default Listener and Service base path configurations, and click Create.
Step 3: Create a GraphQL resolver¶
- Click the + Create Operations button in the GraphQL design view.
- In the side panel, click the + button in the Mutation section to add a mutation operation.
- Provide
task
as the value for the Field name. - Click the Add Argument button to add a GraphQL input
- Provide
query
for the Argument name. - Provide
string
for the Argument type. - Click Add to save the argument.
- Provide
-
Provide
string|error
for the Field type, as this will be used as the return type of the resolver.
Step 4: Implement the resolving logic with an inline agent¶
- Click the created
task
operation in the side panel to navigate to the resolver editor view. - Click the + button in the flow to open the side panel.
- Click Agent under Statement, which will navigate you to the agent creation panel.
- Update Variable Name to
response
. This is the variable where the agent's output will be stored. - Update the Role and Instructions to configure the agent’s behavior.
- Provide the query parameter as the input for Query. This will serve as the command that the agent will execute.
- Click Save.
- Next, configure the agent’s memory, model, and tools. For guidance, refer to the Chat Agent configuration steps and the Personal Assistant setup guide to make the agent function as a personal assistant.
- After configuring the agent, click the + button on the flow and select Return under Control from the side panel.
-
For the Expression, provide the
response
variable as the input.
At this point, we've created a GraphQL resolver that takes a user-provided query
as input, passes it to an inline agent for processing, and returns the agent’s response
as the result of the resolver.
Note
You must implement a query operation to have a valid GraphQL service. Similar to creating the task
operation in Step 3, add an operation named greet
by pressing the + button in the Query section, without any input parameters. For the implementation, you can simply return a string literal saying "welcome"
.