К содержимому

Node.js и TypeScript LLM API: запросы и подключение

Официальные SDK OpenAI и Anthropic для Node.js и TypeScript.

Установка

Установите клиент в проекте и передайте ключ через переменную RELAY_API_KEY среды.

OpenAI SDK

bash
npm install openai

Anthropic SDK

bash
npm install @anthropic-ai/sdk

Первый запрос

Задайте адрес, ключ и идентификатор модели, затем прочитайте текст ответа.

OpenAI SDK

ts
import OpenAI from 'openai'

const client = new OpenAI({ baseURL: 'https://relay-llm.ru/v1', apiKey: process.env.RELAY_API_KEY })

const reply = await client.chat.completions.create({
  model: 'gpt-5.6-luna',
  messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
})
console.log(reply.choices[0]?.message.content)

Anthropic SDK

ts
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({ baseURL: 'https://relay-llm.ru', apiKey: process.env.RELAY_API_KEY })

const reply = await client.messages.create({
  model: 'claude-haiku-5-5',
  max_tokens: 256,
  messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
})
console.log(reply.content[0]?.type === 'text' ? reply.content[0].text : '')

Потоковый ответ

Поток показывает текст по мере генерации и сохраняет usage, если протокол его отдаёт.

OpenAI SDK

ts
import OpenAI from 'openai'

const client = new OpenAI({ baseURL: 'https://relay-llm.ru/v1', apiKey: process.env.RELAY_API_KEY })

const stream = await client.chat.completions.create({
  model: 'gpt-5.6-luna',
  messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
  stream: true,
  stream_options: { include_usage: true },
})
for await (const chunk of stream) {
  const text = chunk.choices[0]?.delta.content
  if (text) process.stdout.write(text)
  if (chunk.usage) console.log(`\n${chunk.usage.total_tokens} tokens`)
}

Anthropic SDK

ts
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({ baseURL: 'https://relay-llm.ru', apiKey: process.env.RELAY_API_KEY })

const stream = client.messages.stream({
  model: 'claude-haiku-5-5',
  max_tokens: 256,
  messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
})
stream.on('text', (text) => process.stdout.write(text))
const final = await stream.finalMessage()
console.log(`\n${final.usage.output_tokens} output tokens`)

Вызов инструмента

Пример делает первый запрос с описанием функции, передаёт результат инструмента и получает финальный ответ.

OpenAI SDK

ts
import OpenAI from 'openai'

const client = new OpenAI({ baseURL: 'https://relay-llm.ru/v1', apiKey: process.env.RELAY_API_KEY })

const tools = [
  {
    type: 'function' as const,
    function: {
      name: 'get_weather',
      description: 'Current weather in a city',
      parameters: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] },
    },
  },
]
const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: 'user', content: 'What is the weather in Paris?' },
]

const first = await client.chat.completions.create({ model: 'gpt-5.6-luna', messages, tools })
const call = first.choices[0]?.message.tool_calls?.[0]
if (call?.type !== 'function') throw new Error('no tool call')
const { city } = JSON.parse(call.function.arguments)

messages.push(
  first.choices[0].message,
  { role: 'tool', tool_call_id: call.id, content: JSON.stringify({ city, temp_c: 18 }) },
)
const final = await client.chat.completions.create({ model: 'gpt-5.6-luna', messages, tools })
console.log(final.choices[0]?.message.content)

Anthropic SDK

ts
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({ baseURL: 'https://relay-llm.ru', apiKey: process.env.RELAY_API_KEY })

const tool: Anthropic.Tool = {
  name: 'get_weather',
  description: 'Current weather in a city',
  input_schema: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] },
}
const messages: Anthropic.MessageParam[] = [
  { role: 'user', content: 'What is the weather in Paris?' },
]

const first = await client.messages.create({
  model: 'claude-haiku-5-5',
  max_tokens: 512,
  tools: [tool],
  messages,
})
const call = first.content.find((block) => block.type === 'tool_use')
if (!call) throw new Error('no tool call')

messages.push(
  { role: 'assistant', content: first.content },
  {
    role: 'user',
    content: [
      { type: 'tool_result', tool_use_id: call.id, content: JSON.stringify({ temp_c: 18 }) },
    ],
  },
)
const final = await client.messages.create({
  model: 'claude-haiku-5-5',
  max_tokens: 512,
  tools: [tool],
  messages,
})
console.log(final.content[0]?.type === 'text' ? final.content[0].text : '')

Ошибки и повторы

Клиент использует ограниченное число повторов и выводит идентификатор запроса при ошибке.

OpenAI SDK

ts
import OpenAI from 'openai'

const client = new OpenAI({
  baseURL: 'https://relay-llm.ru/v1',
  apiKey: process.env.RELAY_API_KEY,
  maxRetries: 3,
})

try {
  const reply = await client.chat.completions.create({
    model: 'gpt-5.6-luna',
    messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
  })
  console.log(reply.choices[0]?.message.content)
} catch (error) {
  if (error instanceof OpenAI.APIError) {
    console.error(error.status, error.code, error.requestID)
  }
  throw error
}

Anthropic SDK

ts
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({
  baseURL: 'https://relay-llm.ru',
  apiKey: process.env.RELAY_API_KEY,
  maxRetries: 3,
})

try {
  const reply = await client.messages.create({
    model: 'claude-haiku-5-5',
    max_tokens: 256,
    messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
  })
  console.log(reply.content[0]?.type === 'text' ? reply.content[0].text : '')
} catch (error) {
  if (error instanceof Anthropic.APIError) {
    console.error(error.status, error.headers?.get('x-request-id'))
  }
  throw error
}

Проверка ключа

GET /v1/key проверяет ключ и показывает его ограничения без списания.

OpenAI SDK

ts
import OpenAI from 'openai'

const client = new OpenAI({ baseURL: 'https://relay-llm.ru/v1', apiKey: process.env.RELAY_API_KEY })

const info = await client.get<{ balance: string; rate_limit_rpm: number }>('/key')
console.log(info.balance, info.rate_limit_rpm)

Anthropic SDK

ts
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({ baseURL: 'https://relay-llm.ru', apiKey: process.env.RELAY_API_KEY })

const info = await client.get<{ balance: string; rate_limit_rpm: number }>('/v1/key')
console.log(info.balance, info.rate_limit_rpm)
Исполняемые варианты: 10 из 12.
Готовы? Три шага, чтобы начатьВойдите в консоль · пополните баланс · создайте API Key