openrouter-examples icon indicating copy to clipboard operation
openrouter-examples copied to clipboard

[Website] Langchain example is outdated

Open bran0h opened this issue 7 months ago • 1 comments

Please could you update usage of openrouter with langchain js?

bran0h avatar Jul 25 '25 12:07 bran0h

solution:

//* example
import { HumanMessage, SystemMessage } from '@langchain/core/messages';
import { ChatOpenAI } from '@langchain/openai';

const chat = new ChatOpenAI(
  {
    model: '<model_name>',
    temperature: 0.8,
    streaming: true,
    apiKey: '${API_KEY_REF}',
    configuration: {
      baseURL: 'https://openrouter.ai/api/v1',
      defaultHeaders: {
        'HTTP-Referer': '<YOUR_SITE_URL>', 
        'X-Title': '<YOUR_SITE_NAME>', 
      },
    },
  },
  // { //old example config
  //   baseURL: 'https://openrouter.ai/api/v1',
  //   defaultHeaders: {
  //     'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openrouter.ai.
  //     'X-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openrouter.ai.
  //   },
  // },
);

// Example usage
const response = await chat.invoke([
  new SystemMessage('You are a helpful assistant.'),
  new HumanMessage('Hello, how are you?'),
]);

kwanLeeFrmVi avatar Aug 06 '25 05:08 kwanLeeFrmVi