Use the TypeScript client

Estimated reading: 5 minutes 170 views

The Robility flow TypeScript client allows your TypeScript applications to programmatically interact with the Robility flow API.

Install the Robility flow TypeScript package

To install the Robility flow typescript client package, use one of the following commands:

a. npm
b. yarn
c. pnpm

npm install @datastax/Robility flow -client

Initialize the Robility flow TypeScript client

1. Import the client into your code.

import { Robility flow Client } from “@datastax/Robility flow -client”;

2. Initialize a Robility flow Client object to interact with your server:

const baseUrl = “BASE_URL”;

const apiKey = “API_KEY”;

const client = new Robility flow Client({ baseUrl, apiKey });

Replace BASE_URL and API_KEY with values from your deployment. The default Robility flow  base URL is http://localhost:7860. To create an API key, see API keys and authentication.

Robility flow TypeScript client quickstart

1. With your Robility flow  client initialized, test the connection by calling your Robility flow  server.

The following example runs a flow (runFlow) by sending the flow ID and a chat input string:

import { Robility flow Client } from “@datastax/Robility flow -client”;

const baseUrl = “http://localhost:7860”;

const client = new Robility flow Client({ baseUrl });

async function runFlow() {

    const flowId = “aa5a238b-02c0-4f03-bc5c-cc3a83335cdf”;

    const flow = client.flow(flowId);

    const input = “Is anyone there?”;

    const response = await flow.run(input);

    console.log(response);

}

runFlow().catch(console.error);

Replace the following:

a. baseUrl: The URL of your Robility flow server
b. flowId: The ID of the flow you want to run
c. input: The chat input message you want to send to trigger the flow.

2. Review the result to confirm that the client connected to your Robility flow server.

The following example shows the response from a well-formed runFlow request that reached the Robility flow  server and successfully started the flow:

FlowResponse {

  sessionId: ‘aa5a238b-02c0-4f03-bc5c-cc3a83335cdf’,

  outputs: [ { inputs: [Object], outputs: [Array] } ]

}

In this case, the response includes a sessionID that is a unique identifier for the client-server session and an outputs array that contains information about the flow run.

3. If you want to get full response objects from the server, change console.log to stringify the returned JSON object:

console.log(JSON.stringify(response, null, 2));

The exact structure of the returned inputs and outputs objects depends on the components and configuration of your flow.

4. If you want the response to include only the chat message from the Chat Output component, change console.log to use the chatOutputText convenience function:

console.log(response.chatOutputText());

Use advanced TypeScript client features

The TypeScript client can do more than just connect to your server and run a flow.

This example builds on the quickstart with additional features for interacting with Robility flow .

1. Pass tweaks to your code as an object with the request.

Tweaks change values within components for all calls to your flow.

This example tweaks the OpenAI component to enforce using the gpt-4o-mini model:

const tweaks = { model_name: “gpt-4o-mini” };

2. Pass a session ID with the request to separate the conversation from other flow runs, and to be able to continue this conversation by calling the same session ID in the future:

const session_id = “aa5a238b-02c0-4f03-bc5c-cc3a83335cdf”;

3. Instead of calling run on the Flow object, call stream with the same arguments:

const response = await client.flow(flowId).stream(input);

for await (const event of response) {

  console.log(event);

}

The response is a ReadableStream of objects. For more information on streaming Robility flow responses, see the /run endpoint.

4. Run the modified TypeScript application to run the flow with tweaks and session_id and then stream the response back.

Replace baseUrl and flowId with values from your deployment.

import { Robility flow Client } from “@datastax/Robility flow -client”;

const baseUrl = “http://localhost:7860”;

const client = new Robility flow Client({ baseUrl });

async function runFlow() {

    const flowId = “aa5a238b-02c0-4f03-bc5c-cc3a83335cdf”;

    const input = “Is anyone there?”;

    const tweaks = { model_name: “gpt-4o-mini” };

    const session_id = “test-session”;

    const response = await client.flow(flowId).stream(input, {

        session_id,

        tweaks,

      });

    for await (const event of response) {

        console.log(event);

    }

}

runFlow().catch(console.error);

Replace baseUrl and flowId with your server URL and flow ID, as you did in the previous run.

Retrieve Robility flow logs with the TypeScript client

To retrieve Robility flow logs, you must enable log retrieval on your Robility flow server by including the following values in your server’s .env file:

1. ROBILITY FLOW _ENABLE_LOG_RETRIEVAL=True
2. ROBILITY FLOW _LOG_RETRIEVER_BUFFER_SIZE=10000
3. ROBILITY FLOW _LOG_LEVEL=DEBUG

The following example script starts streaming logs in the background, and then runs a flow so you can monitor the flow run:

import { Robility flow Client } from “@datastax/Robility flow -client”;

const baseUrl = “http://localhost:7863”;

const flowId = “86f0bf45-0544-4e88-b0b1-8e622da7a7f0”;

async function runFlow(client: Robility flow Client) {

    const input = “Is anyone there?”;

    const response = await client.flow(flowId).run(input);

    console.log(‘Flow response:’, response);

}

async function main() {

    const client = new Robility flow Client({ baseUrl: baseUrl });

    // Start streaming logs

    console.log(‘Starting log stream…’);

    for await (const log of await client.logs.stream()) {

        console.log(‘Log:’, log);

    }

    // Run the flow

    await runFlow(client);

}

main().catch(console.error);

Replace baseUrl and flowId with your server URL and flow ID, as you did in the previous run.

Logs begin streaming indefinitely, and the flow runs once.

The following example result is truncated for readability, but you can follow the messages to see how the flow instantiates its components, configures its model, and processes the outputs.

The FlowResponse object, at the end of the stream, is returned to the client with the flow result in the outputs array.

For more information, see Logs endpoints.

Share this Doc

Use the TypeScript client

Or copy link

CONTENTS