Home  >  Article  >  Web Front-end  >  Launching our JS/TS SDK for AI Search and RAG

Launching our JS/TS SDK for AI Search and RAG

PHPz
PHPzOriginal
2024-09-11 06:41:111192browse

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

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',
  }),
});

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',
});

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'],
      },
    ],
  },
});

Launching our JS/TS SDK for AI Search and RAG

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);

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!

The above is the detailed content of Launching our JS/TS SDK for AI Search and RAG. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn