Install
pip install langchain-openai
npm install @langchain/openai
Setup
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://kymaapi.com/v1",
api_key="ky-your-api-key",
model="qwen-3.6-plus",
)
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
configuration: { baseURL: "https://kymaapi.com/v1" },
apiKey: "ky-your-api-key",
model: "qwen-3.6-plus",
});
Basic Usage
response = llm.invoke("Explain RAG in simple terms")
print(response.content)
const response = await llm.invoke("Explain RAG in simple terms");
console.log(response.content);
Streaming
for chunk in llm.stream("Write a short story"):
print(chunk.content, end="", flush=True)
const stream = await llm.stream("Write a short story");
for await (const chunk of stream) {
process.stdout.write(chunk.content);
}
With Prompt Template
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are a {role}."),
("user", "{question}")
])
chain = prompt | llm
response = chain.invoke({
"role": "Python expert",
"question": "How do decorators work?"
})
print(response.content)
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a {role}."],
["user", "{question}"],
]);
const chain = prompt.pipe(llm);
const response = await chain.invoke({
role: "Python expert",
question: "How do decorators work?",
});
console.log(response.content);
Tool Calling
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get weather for a city."""
return f"Sunny, 72°F in {city}"
llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in Tokyo?")
print(response.tool_calls)
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const getWeather = tool(
async ({ city }) => `Sunny, 72°F in ${city}`,
{ name: "get_weather", description: "Get weather for a city", schema: z.object({ city: z.string() }) }
);
const llmWithTools = llm.bindTools([getWeather]);
const response = await llmWithTools.invoke("What's the weather in Tokyo?");
console.log(response.tool_calls);
RAG Example
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
llm = ChatOpenAI(
base_url="https://kymaapi.com/v1",
api_key="ky-your-api-key",
model="gemini-2.5-flash", # 1M context for large documents
)
prompt = ChatPromptTemplate.from_messages([
("system", "Answer based on the following context:\n\n{context}"),
("user", "{question}")
])
chain = prompt | llm
response = chain.invoke({
"context": "Your retrieved documents here...",
"question": "What is the main topic?"
})
Recommended Models
| Use Case | Model | Why |
|---|---|---|
| General chains | qwen-3.6-plus | Best overall quality |
| RAG / long docs | gemini-2.5-flash | 1M context window |
| Tool calling | kimi-k2.6 | Best function calling |
| Fast responses | qwen-3-32b | Ultra-fast inference |
Kyma’s API is OpenAI-compatible, so
langchain-openai / @langchain/openai work directly. No special package needed.