Skip to content

Latest commit

 

History

History
143 lines (102 loc) · 3.69 KB

README.md

File metadata and controls

143 lines (102 loc) · 3.69 KB

React SDK

npm version License: MIT Documentation

This is the official Inferable AI SDK for React. It is used to start and interact with Inferable runs from React applications.

It does not currently support registering functions as the backend SDKs do.

Installation

npm install @inferable/react
yarn add @inferable/react
pnpm add @inferable/react

Quick Start

More details on Inferable front-end usage can be found here.

useRun Hook

The useRun hook returns an object with the following properties:

{
  client: ApiClient;        // The underlying API client
  createMessage: Function;  // Function to add new messages to the run
  messages: Message[];      // Array of all messages in the run
  run?: Run;               // Current run status and metadata
  start: Function;         // Function to start the run
}

Existing Runs

It can be used to interact with an existing run by specifying the runId:

const { messages, run, createMessage, start } = useRun({
  clusterId: 'your-cluster-id',
  customAuthToken: 'your-custom-auth-token',
  // apiSecret: 'your-api-secret', // Not recommended
  // pollInterval: 1000, // Optional: defaults to 1000ms
});

start({
  runId: 'your-run-id',
})

New Runs

It can be used to create a new run by specifying an initialPrompt:

const { messages, run, createMessage, start } = useRun({
  clusterId: 'your-cluster-id',
  customAuthToken: 'your-custom-auth-token',
  // apiSecret: 'your-api-secret', // Not recommended
  // pollInterval: 1000, // Optional: defaults to 1000ms
});

start({
  initialPrompt: 'Hello!',
})

Adding Messages

You can add messages to the run by calling the createMessage function:

// Optional: Send follow-up messages
await createMessage({
  message: 'Hello!',
  type: 'human'
});

Error Handling

You can handle errors by providing an onError callback:

const { messages, run, createMessage } = useRun({
  clusterId: 'your-cluster-id',
  customAuthToken: 'your-custom-auth-token',
  // apiSecret: 'your-api-secret', // Not recommended
  configId: 'your-config-id',
  onError: (error) => {
    console.error('Run error:', error);
  }
});

Polling Interval

The hook polls for updates by default every 1000ms. You can customize this:

const { messages } = useRun({
  // ... other options
  pollInterval: 2000  // Poll every 2 seconds
});

Local Development

There is development server included in the repository at ./demo.

  1. Start the development server:
npm run dev

This will start a Vite dev server at http://localhost:3000 with the test page, which provides a simple interface to test the SDK's functionality.

Documentation

Support

For support or questions, please create an issue in the repository.

Contributing

Contributions to the Inferable React SDK are welcome. Please ensure that your code adheres to the existing style and includes appropriate tests.