知识库章节 ▾

导航

▸ 从这里开始 按角色

类别

工具 54
词汇表 12

工具

LlamaIndex + Gonka AI — 低成本的 RAG 应用程序

LlamaIndex 是一个用于构建 RAG 应用和 Python AI 代理的领先框架(也有 TypeScript 版本 LlamaIndex.TS)。它处理文档加载、分块、索引、向量搜索和回答合成——您只需要描述数据,LlamaIndex 就能将其转化为基于任何 LLM 的问答系统。

存在一个问题——推理成本。RAG 本质上是资源密集型的:对于每个问题,模型接收查询以及检索到的多个上下文片段,对于大型集合的索引,还会添加嵌入。在生产环境规模下,这意味着每天数以千计的请求。使用 OpenAI(每 100 万 token $2.50–$15)或 Anthropic(每 100 万 token $3–$15),即使是一个适度的问答服务,每月的成本也会高达数万美元。

LlamaIndex 通过 OpenAILike 类原生支持任何兼容 OpenAI 的 endpoint。这意味着 JoinGonka Gateway 只需几行代码即可连接——无需自定义提供商或补丁。结果:同一个 RAG 系统通过 去中心化 Gonka 网络 运行,成本仅为 $0.0062/100 万输入 tokens(输出 ×3)——比云端 API 便宜数百到数千倍。

快速入门:通过 OpenAILike 连接

JoinGonka API 密钥:在 gate.joingonka.ai/register 注册——起步即赠 3M 免费代币——并在 Dashboard 中创建一个 jg-xxx 密钥。

安装:

pip install llama-index llama-index-llms-openai-like

对于任意兼容 OpenAI 的 API,LlamaIndex 提供了来自 llama_index.llms.openai_like 包的 OpenAILike 类。以下是向 Gonka 发送请求的最小示例:

from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    api_base="https://gate.joingonka.ai/v1",
    api_key="jg-your-key",
    model="MiniMaxAI/MiniMax-M2.7",
    is_chat_model=True,            # Gonka is a chat endpoint
    is_function_calling_model=True, # native tool calling is supported
    context_window=180000,         # MiniMax M2.7 context window
    max_tokens=8192,               # Gateway output cap
)

response = llm.complete("Explain what RAG is in three sentences.")
print(response)

关于 OpenAILike 的重要提示:务必设置 is_chat_model=True——否则 LlamaIndex 会以原始提示词访问已弃用的 Completions 端点,而非进行对话。is_function_calling_model=True 启用原生工具调用。context_window 请按模型设置,以便 LlamaIndex 正确切分上下文。

示例:带有查询引擎的 RAG 管道

LlamaIndex 的经典用法——为你的文档建立索引,并通过 query_engine 向其发起查询。全局 LLM 只需通过 Settings.llm 设置一次,之后整个流程就会自动使用 Gonka。

from llama_index.core import (
    VectorStoreIndex,
    SimpleDirectoryReader,
    Settings,
)
from llama_index.llms.openai_like import OpenAILike
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

# 1. LLM via Gonka (set once, globally)
Settings.llm = OpenAILike(
    api_base="https://gate.joingonka.ai/v1",
    api_key="jg-your-key",
    model="MiniMaxAI/MiniMax-M2.7",
    is_chat_model=True,
    context_window=180000,
    max_tokens=8192,
)

# 2. Local embeddings (free, no OpenAI)
Settings.embed_model = HuggingFaceEmbedding(
    model_name="BAAI/bge-small-en-v1.5"
)

# 3. Load and index documents from the ./data folder
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)

# 4. Query the knowledge base
query_engine = index.as_query_engine()
response = query_engine.query("What is this document about?")
print(response)

关于嵌入的关键细节:默认情况下 VectorStoreIndex 使用 OpenAI 的嵌入(text-embedding-ada-002)——这是对 OpenAI 的单独付费调用,而不是对 Gonka。要彻底摆脱 OpenAI,请通过 Settings.embed_model 设置本地嵌入模型(如上面的示例——HuggingFaceEmbedding,包为 pip install llama-index-embeddings-huggingface)。这样生成走 Gonka,而向量化则在本地免费完成。

成本:一次 RAG 流程请求(检索 + 生成)消耗约 2–5K 个 LLM token。通过 Gonka,这只要不到一美分的一小部分;通过 OpenAI/Anthropic 则要贵 3–4 个数量级。在每天数千次请求的量级下,这一差异每月能转化为数万美元的成本节省。

RAG 负载成本比较

RAG 应用不是一次性的对话,而是持续不断的请求流:用户的每个问题都会带上 2–5K LLM token(问题本身加上检索到的上下文片段)。我们来算算典型用量以及在各家提供商那里的花费。JoinGonka Gateway 上的 Gonka 价格:输入 ~$0.0062/1M,输出 ×3。

场景LLM tokenOpenAI / AnthropicJoinGonka Gonka
向知识库提一个问题~4K$0.01 — $0.06~$0.00004
客服聊天机器人(每天 1K 次请求)~4M/天每天 $10 — $60每天 ~$0.038
语料库索引 + Q&A(100 万词)~5M$12 — $75~$0.048
生产级服务,每月 50K 次请求~200M/月每月 $500 — $3,000每月 ~$1.92

用免费的 3M token,你可以跑通 RAG 流水线的最初请求、为测试语料库建立索引、推送成千上万次查询——一分钱都不用花。在生产级用量下,JoinGonka Gateway 让 RAG 从一项昂贵服务变成一笔几乎察觉不到的开销。

代理、工具调用和模型选择

LlamaIndex 不仅能基于文档回答问题,还能构建带工具的 agent。Gonka 的三款模型均支持原生 tool calling——agent 以结构化方式调用函数,无需解析文本。以下是一个带工具的 agent 示例:

import asyncio
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    api_base="https://gate.joingonka.ai/v1",
    api_key="jg-your-key",
    model="MiniMaxAI/MiniMax-M2.7",
    is_chat_model=True,
    is_function_calling_model=True,
    context_window=180000,
    max_tokens=8192,
)

def multiply(a: float, b: float) -> float:
    """Multiplies two numbers."""
    return a * b

agent = FunctionAgent(
    tools=[multiply],
    llm=llm,
    system_prompt="You are a helpful assistant. Use the tools for calculations.",
)

async def main():
    result = await agent.run("What is 1234 multiplied by 5678?")
    print(result)

asyncio.run(main())

模型选择(model 字段及相应的 context_window / max_tokens 限制):

模型(model)上下文最大输出适用场景
MiniMaxAI/MiniMax-M2.7180K8192默认(如示例所示):tool calling、agent、RAG
deepseek-ai/DeepSeek-V4-Flash-0731380K32768网络中上下文长度第二、输出最长、适合廉价的大 prompt
zai-org/GLM-5.3-Flash390K8192推理模型:复杂逻辑、网络中上下文最长;推理过程会占用部分 max_tokens

通过 Gateway 的 max_tokens 上限:MiniMax M2.7 和 GLM-5.3 Flash 最高 8192,DeepSeek V4 Flash 最高 32768。如果非流式请求未设置 max_tokens,默认最多返回 1500 个 token——对于 RAG 回答和 agent 步骤,请显式设置该值。

TypeScript:LlamaIndex.TS 有一条对应的路径——@llamaindex/openai 包中的 OpenAI 类接受 baseURL 和 apiKey(或读取 OPENAI_BASE_URL / OPENAI_API_KEY 环境变量),因此同一个 Gateway 也能接入 Node.js 技术栈。如果你也在用 Python 框架构建 AI 应用,不妨看看 LangChain 指南。

LlamaIndex + Gonka = 以极低成本实现生产级的 RAG 和 Agent。通过 OpenAILike (is_chat_model=True) 连接,支持原生 tool calling 和本地嵌入 (local embeddings) — 输入价格仅为 $0.0062/1M,而 OpenAI 为 $2.50–15。初始免费 token 让你无需绑定银行卡即可测试流程。

想了解更多?

探索其他章节或立即开始赚取 GNK。

领取免费 token →