Esta página ainda não foi traduzida para Português. Exibindo o original em inglês.
LangChain
Pointing `ChatOpenAI` at our endpoint
LangChain has no MyIP-specific integration, and does not need one. Give ChatOpenAI our base URL and key — we implement the OpenAI-compatible shape it already speaks.
| Item | Value |
|---|---|
| Base URL | https://openrouter.myip.co.kr/api/v1 |
| Environment variable | MYIP_API_KEY |
| Example models | google/gemma-4-26b-a4b, lgai/exaone-4.0-32b |
Python
pip install langchain langchain-openai
export MYIP_API_KEY="sk-mo-v1-..."import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(
model="google/gemma-4-26b-a4b",
base_url="https://openrouter.myip.co.kr/api/v1",
api_key=os.environ["MYIP_API_KEY"],
temperature=0.7,
default_headers={
"HTTP-Referer": "https://example.com",
"X-Title": "My LangChain App",
},
)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a concise assistant."),
("human", "{question}"),
])
chain = prompt | llm | StrOutputParser()
print(chain.invoke({"question": "What is the capital of South Korea?"}))JavaScript / TypeScript
npm install @langchain/openai @langchain/core
export MYIP_API_KEY="sk-mo-v1-..."import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { StringOutputParser } from '@langchain/core/output_parsers';
const llm = new ChatOpenAI({
model: 'google/gemma-4-26b-a4b',
apiKey: process.env.MYIP_API_KEY,
temperature: 0.7,
configuration: {
baseURL: 'https://openrouter.myip.co.kr/api/v1',
defaultHeaders: {
'HTTP-Referer': 'https://example.com',
'X-Title': 'My LangChain App',
},
},
});
const prompt = ChatPromptTemplate.fromMessages([
['system', 'You are a concise assistant.'],
['human', '{question}'],
]);
const chain = prompt.pipe(llm).pipe(new StringOutputParser());
console.log(await chain.invoke({ question: 'What is the capital of South Korea?' }));Streaming
for chunk in llm.stream("Write a short poem."):
print(chunk.content, end="", flush=True)const stream = await llm.stream('Write a short poem.');
for await (const chunk of stream) {
process.stdout.write(String(chunk.content));
}You do not need to enable stream_options.include_usage — we always request it. To have LangChain fold the final usage chunk into its own totals, though, turn on stream usage collection:
llm = ChatOpenAI(
model="lgai/exaone-4.0-32b",
base_url="https://openrouter.myip.co.kr/api/v1",
api_key=os.environ["MYIP_API_KEY"],
stream_usage=True,
)Reading cost and the generation id
LangChain hides response headers, so the reliable route to cost is the generation id in the response metadata.
msg = llm.invoke("Hello")
print(msg.usage_metadata) # token counts
gen_id = msg.response_metadata.get("id") # 'gen-...'import requests
def cost_krw(generation_id: str) -> float:
res = requests.get(
"https://openrouter.myip.co.kr/api/v1/generation",
params={"id": generation_id},
headers={"Authorization": f"Bearer {os.environ['MYIP_API_KEY']}"},
timeout=10,
)
return res.json()["data"]["total_cost"] # KRW
print(cost_krw(gen_id), "KRW")Settlement finishes asynchronously, so a lookup immediately after a stream ends can be early; wait a moment and retry. All amounts are in Korean won (KRW).
Tool calling
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Look up the current weather for a city."""
return f"{city}: clear, 21C"
agent_llm = llm.bind_tools([get_weather])
msg = agent_llm.invoke("What is the weather in Seoul?")
print(msg.tool_calls)The model has to support tool calling. Check that supported_parameters in the GET /api/v1/models response contains tools. See Tool calling.
What you cannot use
OpenAIEmbeddings— there is no embeddings endpoint; you get 404not_supported.- LangChain's image and audio integrations — those endpoints do not exist here.
For a RAG pipeline, generate embeddings elsewhere (a local embedding model, for instance) and send only the generation step through our gateway. The full list is in Unsupported endpoints.
Related
Última atualização: 5 de set. de 2026