Knowledge Base Sections ▾

Navigation

▸ Start here By roles

Categories

Tools 32
Glossary 12

Tools

LangChain + Gonka AI - AI applications for pennies

LangChain is the most popular framework for building AI applications in Python and JavaScript. RAG pipelines, chains, agents, and document handling — LangChain provides abstractions for all of this.

LangChain natively supports OpenAI-compatible APIs via the ChatOpenAI class. This means JoinGonka Gateway integrates in 3 lines of code — without additional packages or configuration.

The result: an RAG system, chatbot, or AI agent running for $0.003/1M tokens instead of $2.50-15 via OpenAI.

Quick Start: 3 lines of code

Minimal example — connecting LangChain to Gonka:

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    base_url="https://gate.joingonka.ai/v1",
    api_key="jg-your-key",
    model="MiniMaxAI/MiniMax-M2.7",
)

response = llm.invoke("Explain what RAG is")
print(response.content)

That is it. Three lines — and your LangChain project runs via the decentralized Gonka network for pennies.

Install dependencies:

pip install langchain langchain-openai

Recommendation: explicitly specify max_tokens=8192 — this is the output limit via JoinGonka Gateway for all network models. The network models' context window is 200K tokens — keep this in mind when configuring chunk_size in RAG pipelines.

Example: RAG pipeline with Gonka

RAG (Retrieval-Augmented Generation) is the most popular pattern for AI applications. You upload documents, split them into chunks, create embeddings, search for relevant fragments, and generate a response with context.

from langchain_openai import ChatOpenAI
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import RetrievalQA
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.document_loaders import TextLoader

# 1. LLM via Gonka
llm = ChatOpenAI(
    base_url="https://gate.joingonka.ai/v1",
    api_key="jg-your-key",
    model="MiniMaxAI/MiniMax-M2.7",
    streaming=True,
)

# 2. Document loading and indexing
loader = TextLoader("docs/my_document.txt")
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
chunks = splitter.split_documents(docs)

# 3. Vector storage (local, free)
embeddings = HuggingFaceEmbeddings()
vectorstore = FAISS.from_documents(chunks, embeddings)

# 4. RAG-chain
qa = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=vectorstore.as_retriever(),
)

# 5. Request
result = qa.invoke("What is this document about?")
print(result["result"])

Cost: a single RAG pipeline request (retrieval + generation) uses ~2-5K LLM tokens. Through Gonka, this is $0.00001-0.000024. Through OpenAI — $0.005-0.05. The difference is 2,000x.

For production systems processing thousands of requests per day, the savings amount to tens of thousands of dollars per month.

Example: AI agent with tool calling

LangChain allows you to create agents with tools. Kimi K2.6 supports native tool calling — agents work reliably, without parsing text responses.

from langchain_openai import ChatOpenAI
from langchain.agents import create_openai_tools_agent, AgentExecutor
from langchain.tools import tool
from langchain.prompts import ChatPromptTemplate

llm = ChatOpenAI(
    base_url="https://gate.joingonka.ai/v1",
    api_key="jg-your-key",
    model="MiniMaxAI/MiniMax-M2.7",
)

@tool
def calculator(expression: str) -> str:
    """Calculates a mathematical expression."""
    return str(eval(expression))

@tool
def search_web(query: str) -> str:
    """Searches for information on the web."""
    return f"Search results for: {query}"

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_openai_tools_agent(llm, [calculator, search_web], prompt)
executor = AgentExecutor(agent=agent, tools=[calculator, search_web])

result = executor.invoke({"input": "What is 2**10 * 3.14?"})
print(result["output"])

The agent calls calculator, receives the result, and formats the response. The entire cycle costs ~$0.00005 via Gonka. Via OpenAI — $0.01-0.05. For systems with thousands of users, this is a difference of tens of thousands of dollars.

LangChain + Gonka = production-ready AI applications for pennies. RAG, agents, chains — all in 3 lines of code with ChatOpenAI. Cost — $0.003/1M tokens, native tool calling, streaming.

Want to learn more?

Explore other sections or start earning GNK right now.

Get 10M free tokens →