Connect LangChain to Gonka Broker
LangChain is a framework for building applications on top of language models. Its OpenAI chat-model integration accepts a custom base URL, so you can point it at Gonka Broker and build on open-source models: the only change from a standard OpenAI setup is the base URL, the key, and the model id.
The integration is the same in Python and JavaScript/TypeScript; only the package name and the constructor differ. Pick your language below.
Prerequisites
Section titled “Prerequisites”- A Gonka API key (starts with
gnk-prx-). See Create a Gonka API Key. - Python 3.9+ or Node.js 18+, depending on your stack.
Install and configure
Section titled “Install and configure”Install the OpenAI integration package:
pip install langchain-openaiPoint ChatOpenAI at Gonka Broker:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI( model="moonshotai/Kimi-K2.6", base_url="https://proxy.gonkabroker.com/v1", api_key="gnk-prx-your-api-key",)
response = llm.invoke("Say hello from Gonka Broker.")print(response.content)Install the OpenAI integration package:
npm install @langchain/openaiPoint ChatOpenAI at Gonka Broker:
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({ model: "moonshotai/Kimi-K2.6", apiKey: "gnk-prx-your-api-key", configuration: { baseURL: "https://proxy.gonkabroker.com/v1", },});
const response = await llm.invoke("Say hello from Gonka Broker.");console.log(response.content);The model must match a Gonka-supported id exactly, for example moonshotai/Kimi-K2.6 or MiniMaxAI/MiniMax-M2.7 (see Supported Models). Once the model is configured, use it anywhere a LangChain chat model goes: chains, agents, and LCEL pipelines all work unchanged.
Embeddings for RAG
Section titled “Embeddings for RAG”LangChain’s OpenAIEmbeddings works with Gonka Broker the same way: same base URL and key, with BAAI/bge-m3 as the model (see Embeddings & RAG). The embeddings drop into any LangChain vector store.
from langchain_openai import OpenAIEmbeddingsfrom langchain_core.vectorstores import InMemoryVectorStore
embeddings = OpenAIEmbeddings( model="BAAI/bge-m3", base_url="https://proxy.gonkabroker.com/v1", api_key="gnk-prx-your-api-key", check_embedding_ctx_length=False,)
store = InMemoryVectorStore(embeddings)store.add_texts([ "Gonka Broker bills in USD per token.", "The Eiffel Tower is in Paris.",])print(store.similarity_search("How am I charged?", k=1)[0].page_content)check_embedding_ctx_length=False is required: without it, LangChain pre-tokenizes your text with tiktoken and sends token arrays instead of strings, which the API rejects with a 400.
import { OpenAIEmbeddings } from "@langchain/openai";
const embeddings = new OpenAIEmbeddings({ model: "BAAI/bge-m3", apiKey: "gnk-prx-your-api-key", configuration: { baseURL: "https://proxy.gonkabroker.com/v1", },});
const vector = await embeddings.embedQuery("How am I charged?");console.log(vector.length); // 1024No extra flags needed; the JS client sends plain strings. Pass the embeddings object to any LangChain vector store (MemoryVectorStore, Chroma, pgvector, …) to build retrieval.
Verify
Section titled “Verify”Run the snippet above. A printed reply confirms LangChain is reaching Gonka Broker through your key.
Troubleshooting
Section titled “Troubleshooting”- 401 / invalid API key: wrong or paused key. Create a fresh one from Create a Gonka API Key.
- Model not found / unsupported: the
modelmust match a model Gonka Broker serves exactly (see Supported Models). - Connection errors: confirm the base URL is exactly
https://proxy.gonkabroker.com/v1. - 400 “input must be a string or a non-empty array of strings” on embeddings: Python’s
OpenAIEmbeddingssent tiktoken token arrays instead of text. Setcheck_embedding_ctx_length=False(see the embeddings section above). - Looking for the model’s thinking: reasoning models return their thinking in a separate
reasoningresponse field, never insidecontent— the text LangChain returns is clean answer text. To skip thinking entirely (shorter, cheaper responses), see reasoning control.