Launching our JS/TS SDK for AI Search and RAG

Nick K - Sep 10 - - Dev Community

If you have used Trieve in a JavaScript application, you probably know that you need to make most of your calls to Trieve using fetch. While this approach is good, it's not ideal, and we want to provide users with an easier way to use our APIs.

Well, behind the scenes we have been working on making Trieve easier to use than ever in JavaScript applications and that includes making a new JavaScript SDK that makes it much simpler to integrate Trieve into any application.

First things first, you can install the new trieve-ts-sdk with your favorite package manager:

yarn add trieve-ts-sdk
# or
npm install trieve-ts-sdk
# or
pnpm install trieve-ts-sdk
Enter fullscreen mode Exit fullscreen mode

And now let's see how it works, and let's take a search call as an example.

Before you would need to do something like:

fetch('https://api.trieve.ai/api/chunk/search', {
  method: 'POST',
  headers: {
    'TR-Dataset': 'dc6f3b0d-cf21-412b-9d16-fb7ade090365',
    Authorization: 'tr-********************************',
  },
  body: JSON.stringify({
    query: 'Sonic the Hedgehog',
  }),
});
Enter fullscreen mode Exit fullscreen mode

While this method works well, it's not the cleanest approach. You will need to have the documentation open next to your code editor, as there are no types to assist you in making your function calls.Now, with the new SDK you can call it like so:

import { TrieveSDK } from 'trieve-ts-sdk';

export const trieve = new TrieveSDK({
  apiKey: '<your-api-key>',
  datasetId: '<dataset-to-use>',
});

const results = await trieve.search({
  query: 'Sonic the Hedgehog',
});
Enter fullscreen mode Exit fullscreen mode

With the help of the exported types it's also much easier to create a much more complicated search that includes, for example, filters:

import { TrieveSDK } from 'trieve-ts-sdk';

const results = await trieve.search({
  query: 'Sonic the Hedgehog',
  search_type: 'hybrid',
  filters: {
    must: [
      {
        field: 'meta.rating',
        range: {
          gt: 80,
        },
      },
    ],
    must_not: [
      {
        field: 'metadata.console',
        match: ['gba', 'wii'],
      },
    ],
  },
});
Enter fullscreen mode Exit fullscreen mode

screenshot of typed Trieve SDK

And it's not just methods for chunks, we have functions for most of our API that you can use, want to stream a RAG completion? We got that:

const reader = await trieve.createMessageReader({
  topic_id: id || currentTopic,
  new_message_content: currentQuestion,
  llm_options: {
    completion_first: true,
  },
});
handleReader(reader);
Enter fullscreen mode Exit fullscreen mode

We also created comprehensive docs so that all these functions are easy for you to find whether you use TypeScript or not.

Okay, the last step is to install it and get to building search and RAG in your application!

. . .
Terabox Video Player