Sign up now for $10 free budget - Get an extra $40 when you get in touch

Using LangChain.js

Call the AI Router using the LangChain.js package

Setting up langchain.js

If you don't have it installed already, install the LangChain OpenAI package using npm:

npm install @langchain/openai

Setting up the AI Router

Set the openai base url to the AI Router base url https://api.airouter.io in your code and api key to the one you generated. Then proceed to call the OpenAI methods as you would normally do:

import { ChatOpenAI } from '@langchain/openai';
import { PromptTemplate } from '@langchain/core/prompts';

const llm = new ChatOpenAI(
  {
    model: 'auto',
    apiKey: '<THE-API-KEY-YOU-GENERATED>'
    configuration: {
      baseURL: "https://api.airouter.io",
    },
  },
);
const prompt = PromptTemplate.fromTemplate(
    'What is the capital of France?',
);
const chain = prompt.pipe(llm);
const result = await chain.invoke({});

The above example lets the AI Router select the most appropriate model for each request. It is possible to also supply a list of models to limit the models the AI Router may choose from.