Hubris
Фреймворки

LangChain.js

Подключить Hubris к LangChain.js через ChatOpenAI.

LangChain.js

LangChain.js работает с Hubris через ChatOpenAI из пакета @langchain/openai.

Установка

npm install langchain @langchain/openai @langchain/core

Подключение

import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "anthropic/claude-haiku-4.5",
  apiKey: process.env.HUBRIS_API_KEY,
  configuration: {
    baseURL: "https://api.hubris.pw/v1",
  },
});

Внимание про configuration.baseURL. В LangChain.js есть несколько способов задать base URL — самый надёжный для стриминга — через объект configuration (он передаётся в OpenAI SDK внутри). Не используйте устаревший basePath или apiBaseUrl — они ломают стриминг в некоторых версиях.

Базовый вызов

import { HumanMessage } from "@langchain/core/messages";

const response = await llm.invoke([new HumanMessage("Привет")]);
console.log(response.content);

С шаблоном промпта

import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";

const prompt = ChatPromptTemplate.fromMessages([
  ["system", "Вы — лаконичный ассистент."],
  ["user", "{question}"],
]);

const chain = prompt.pipe(llm).pipe(new StringOutputParser());
const result = await chain.invoke({ question: "Что такое pi?" });
console.log(result);

Стриминг

const stream = await llm.stream([new HumanMessage("Расскажи историю")]);
for await (const chunk of stream) {
  process.stdout.write(chunk.content as string);
}

С tool calling

import { tool } from "@langchain/core/tools";
import { z } from "zod";

const weatherTool = tool(
  async ({ city }) => ({ city, temp: 5, condition: "sunny" }),
  {
    name: "get_weather",
    description: "Текущая погода в городе",
    schema: z.object({ city: z.string() }),
  },
);

const llmWithTools = llm.bindTools([weatherTool]);
const response = await llmWithTools.invoke([new HumanMessage("Погода в Москве?")]);
console.log(response.tool_calls);

Что дальше