You can use the chat completion endpoint with Vercel AI SDK under two lines of code change.

Example

import { createOpenAI, OpenAIProvider } from '@ai-sdk/openai'
import { streamText, streamObject } from 'ai'

async function main() {
  // Initialize OpenAI with Keywords proxy
  const client: OpenAIProvider = createOpenAI({
    baseURL: 'https://api.keywordsai.co',
    apiKey: 'YOUR_KEYWORDS_AI_API_KEY',
    compatibility: 'strict',
  })


  const requestParamsDefault: Parameters<typeof streamText>[0] = {
    model: client.chat('gpt-3.5-turbo'),
    messages: [
      {
        role: 'user',
        content: 'Hello! How are you doing today?',
      },
    ],
    temperature: 0.5,
  }

  try {
    console.log('Calling OpenAI with Keywords proxy...')

    const { textStream: proxyTextStream } = await streamText(requestParamsDefault)
    for await (const textPart of proxyTextStream) {
      console.log('Keywords Proxy Response:', textPart)
    }
  } catch (error) {
    console.error('Error:', error)
  }
}

main()

Add Keywords AI parameters

Adding Keywords AI parameters to the Vercel AI SDK is different than other frameworks. Here is an example of how to do it:

1

Specify Keywords AI params in an object

You should create an object with the Keywords AI parameters you want to use. Add parameters you want to use as keys in the object.

const keywordsAIHeaderContent = {
    "customer_params": {
        "customer_identifier": "customer_123",
        "name": "Hendrix Liu", //optional
        "email": "hendrix@keywordsai.co" //optional
    }
    // "cache_enabled": true or other parameters
}
2

Encode the object as a string

You should encode the object as a string and then you can send it as a header in your request.

const encoded = Buffer.from(JSON.stringify(keywordsAIHeaderContent)).toString('base64');
3

Add the header to your request

You should send it in the X-Data-Keywordsai-Params header.

const client = createOpenAI({
  baseURL: process.env.KEYWORDSAI_ENDPOINT_LOCAL,
  apiKey: process.env.KEYWORDSAI_API_KEY_TEST,
  compatibility: "strict",
  headers: {
    "X-Data-Keywordsai-Params": encoded
  }
});
4

Full example

import { streamText, streamObject } from "ai";
import { createOpenAI } from "@ai-sdk/openai";

const keywordsAIHeaderContent = {
    "customer_identifier": "test_customer_identifier_from_header"
}
const encoded = Buffer.from(JSON.stringify(keywordsAIHeaderContent)).toString('base64');

const client = createOpenAI({
  baseURL: process.env.KEYWORDSAI_ENDPOINT_LOCAL,
  apiKey: process.env.KEYWORDSAI_API_KEY_TEST,
  compatibility: "strict",
  headers: {
    "X-Data-Keywordsai-Params": encoded
  }
});

const requestParamsDefault: Parameters<typeof streamText>[0] = {
  model: client.chat("gpt-4o"),
  messages: [
    {
      role: "user",
      content: "Hello! How are you doing today?",
    },
  ],
  temperature: 0.5,
};

try {
  console.log("Calling OpenAI with Keywords proxy...");

  const { textStream: proxyTextStream } = await streamText(
    requestParamsDefault
  );
  for await (const textPart of proxyTextStream) {
    console.log("Keywords Proxy Response:", textPart);
  }
} catch (error) {
  console.error("Error:", error);
}