> ## Documentation Index
> Fetch the complete documentation index at: https://friendli.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# FriendliAI + Weaviate (Node.js)

> Build RAG apps with FriendliAI and Weaviate in Node.js. Combine vector search with Friendli Engine inference to reduce hallucinations in responses.

Integration with [**Weaviate**](https://github.com/weaviate/weaviate) enables performing Retrieval Augmented Generation (RAG) directly within the Weaviate database.
This combines the power of [**Friendli Engine**](https://friendli.ai/why-friendliai) and Weaviate's efficient storage and fast retrieval capabilities to generate personalized and context-aware responses.

## How to Use

Before you start, ensure you've already obtained the `API_KEY` from the [Friendli Suite > Personal Settings > API Keys](https://friendli.ai/suite/~/setting/keys).
Also, set up your Weaviate instance following this [guide](https://weaviate.io/developers/weaviate/starter-guides/which-weaviate).
Your Weaviate instance must be configured with the FriendliAI generative AI integration (`generative-friendliai`) module.

<CodeGroup>
  ```bash npm theme={null}
  npm i weaviate-client
  ```

  ```bash yarn theme={null}
  yarn add weaviate-client
  ```

  ```bash pnpm theme={null}
  pnpm add weaviate-client
  ```
</CodeGroup>

### Instantiation

Now you can instantiate a [Weaviate collection](https://weaviate.io/developers/weaviate/manage-data/collections) using the model.
Choose the example for your endpoint type:

You can specify one of the [available models](https://friendli.ai/models?products=SERVERLESS) for the Model APIs.
The default model (i.e. `zai-org/GLM-5.2`) will be used if no model is specified.

<CodeGroup>
  ```ts Model APIs theme={null}
  import weaviate from "weaviate-client"

  const client = await weaviate.connectToWeaviateCloud(
    'WEAVIATE_INSTANCE_URL',  // your Weaviate instance URL
    {
      authCredentials: new weaviate.ApiKey('WEAVIATE_INSTANCE_APIKEY'),
      headers: {
        'X-Friendli-Api-Key': process.env.API_KEY,
      }
    }
  )

  await client.collections.create({
    name: 'DemoCollection',
    generative: weaviate.configure.generative.friendliai({
      model: 'zai-org/GLM-5.2'
    }),
    // Additional parameters ...
  });

  client.close()
  ```

  ```ts Dedicated Endpoints theme={null}
  import weaviate from "weaviate-client"

  const client = await weaviate.connectToWeaviateCloud(
    'WEAVIATE_INSTANCE_URL',  // your Weaviate instance URL
    {
      authCredentials: new weaviate.ApiKey('WEAVIATE_INSTANCE_APIKEY'),
      headers: {
        'X-Friendli-Api-Key': process.env.API_KEY,
        "X-Friendli-Baseurl": "https://api.friendli.ai/dedicated",
      }
    }
  )

  await client.collections.create({
    name: 'DemoCollection',
    generative: weaviate.configure.generative.friendliai({
      model: 'YOUR_ENDPOINT_ID'
    }),
    // Additional parameters ...
  });

  client.close()
  ```

  ```ts Fine-tuned Dedicated Endpoints theme={null}
  import weaviate from "weaviate-client"

  const client = await weaviate.connectToWeaviateCloud(
    'WEAVIATE_INSTANCE_URL',  // your Weaviate instance URL
    {
      authCredentials: new weaviate.ApiKey('WEAVIATE_INSTANCE_APIKEY'),
      headers: {
        'X-Friendli-Api-Key': process.env.API_KEY,
        "X-Friendli-Baseurl": "https://api.friendli.ai/dedicated",
      }
    }
  )

  await client.collections.create({
    name: 'DemoCollection',
    generative: weaviate.configure.generative.friendliai({
      model: 'YOUR_ENDPOINT_ID:YOUR_ADAPTER_ROUTE'
    }),
    // Additional parameters ...
  });

  client.close()
  ```
</CodeGroup>

#### Configurable Parameters

Configure the following generative parameters to customize the model behavior.

```ts theme={null}
await client.collections.create({
  name: 'DemoCollection',
  generative: weaviate.configure.generative.friendliai({
    model: 'zai-org/GLM-5.2',
    maxTokens: 500,
    temperature: 0.7,
  }),
  // Additional parameters ...
});
```

### Retrieval Augmented Generation

After configuring Weaviate, perform RAG operations, either with the single prompt or grouped task method.

#### Single Prompt

To generate text for each object in the search results, use the single prompt method.
The example below generates outputs for each of the n search results, where n is specified by the limit parameter.

When creating a single prompt query, use braces `{}` to interpolate the object properties you want Weaviate to pass on to the language model.
For example, to pass on the object's title property, include `{title}` in the query.

```ts theme={null}
let myCollection = client.collections.get('DemoCollection');

const singlePromptResults = await myCollection.generate.nearText(
  ['A holiday film'],
  {
    singlePrompt: `Translate this into French: {title}`,
  },
  {
    limit: 2,
  }
);

for (const obj of singlePromptResults.objects) {
  console.log(obj.properties['title']);
  console.log(`Generated output: ${obj.generated}`);  // Note that the generated output is per object
}
```

#### Grouped Task

To generate one text for the entire set of search results, use the grouped task method.
In other words, when you have n search results, the generative model generates one output for the entire group.

```ts theme={null}
let myCollection = client.collections.get('DemoCollection');
const groupedTaskResults = await myCollection.generate.nearText(
  ['A holiday film'],
  {
    groupedTask: `Write a fun tweet to encourage readers to check out these films.`,
  },
  {
    limit: 2,
  }
);

console.log(`Generated output: ${groupedTaskResults.generated}`);  // Note that the generated output is per query
for (const obj of groupedTaskResults.objects) {
  console.log(obj.properties['title']);
}
```

### Further Resources

Once the integrations are configured at the collection, the data management and search operations in Weaviate work identically to any other collection.
See the following model-agnostic examples:

* [How-to manage data guides show how to perform data operations](https://weaviate.io/developers/weaviate/manage-data/create).
* [How-to search guides show how to perform search operations](https://weaviate.io/developers/weaviate/search/basics).
