跳到正文
LlamaIndex

框架与 SDK

在 LlamaIndex 中使用 Shannon

将 LlamaIndex 的 OpenAILike 类指向 Shannon 的基础 URL;聊天、流式输出和函数调用都可通过它使用。

  • 测试于 2026年11月7日
  • OpenAI Chat Completions
  • 约 2 分钟

关于 LlamaIndex

LlamaIndex 是一个 Python 和 TypeScript 框架,适用于读取你的文档的应用:索引、检索、智能体和工作流。它的 OpenAILike 类可与任何兼容 OpenAI 的服务器通信,因此 Shannon 模型可以回答问题、调用工具并驱动 LlamaIndex 智能体。

你需要准备

Shannon API 密钥
密钥与用量
基础 URL
https://api.shannon-ai.com/v1 端点: /v1/chat/completions
下面的所有代码示例都会随你的选择而变化。

设置步骤

  1. 安装 OpenAI-like 集成

    Shell
    pip install llama-index-core llama-index-llms-openai-like
  2. 创建模型并流式输出

    is_chat_model=True 使用聊天端点;is_function_calling_model=True 让智能体能够调用工具;context_window 告诉 LlamaIndex 能容纳多少文本。

    Python
    from llama_index.llms.openai_like import OpenAILike
    
    llm = OpenAILike(
        model="Kimi-K3-uncensored-NVFP4",
        api_base="https://api.shannon-ai.com/v1",
        api_key="YOUR_API_KEY",
        context_window=1022976,
        is_chat_model=True,
        is_function_calling_model=True,
    )
    
    for r in llm.stream_complete("Name three colours a cat can be."):
        print(r.delta, end="", flush=True)
  3. 调用工具

    Python
    from llama_index.core.tools import FunctionTool
    
    def get_weather(city: str) -> str:
        """Weather for a city."""
        return f"Sunny in {city}"
    
    tool = FunctionTool.from_defaults(fn=get_weather)
    resp = llm.chat_with_tools([tool], user_msg="What is the weather in Oslo?")
    print(llm.get_tool_calls_from_response(resp))  # [ToolSelection(tool_name='get_weather', tool_kwargs={'city': 'Oslo'})]

检查是否正常工作

发送: 运行流式输出示例。

你应该看到: 颜色随流式输出逐个打印出来。

适合 LlamaIndex 的模型

下面的每个 id 都满足 LlamaIndex 的要求。第一个是代码示例中的默认值。

模型 id上下文窗口最大输出图像输入
Kimi-K3-uncensored-NVFP4 1,022,976 262,144 是
Kimi-K3-uncensored-3BIT-REAP 225,280 225,280 是
shannon-3 196,608 65,536 是
shannon-coder-1 131,072 65,536 否

模型设置

LlamaIndex 会要求填写这些值。它们取决于你选择的模型。

设置项值原因
context_window 1022976 LlamaIndex 会把检索到的文本压缩到这个 tokens 数量以内。

须知

  • 没有 is_chat_model=True 时,OpenAILike 会调用旧的 /v1/completions 端点,而它在 Shannon 上会返回 404。
  • 没有 is_function_calling_model=True 时,智能体会退回到基于文本的工具使用。
  • 检索需要嵌入模型;Shannon 不提供 /v1/embeddings,因此请把 LlamaIndex 的 embed_model 设为其他提供商或本地模型。

提示

  1. 只需设置一次 Settings.llm = llm,每个索引、查询引擎和智能体就都会使用 Shannon。
  2. FunctionAgent 和 ReActAgent 都接受这个 llm。
  3. 处理长文档时,请选择窗口较大的模型,并相应调高 context_window。

故障排查

现象原因解决办法
/v1/completions 返回 404 没有设置 is_chat_model。 传入 is_chat_model=True。
建立索引时要求提供 OpenAI 密钥 LlamaIndex 默认的嵌入模型是 OpenAI 的。 把 Settings.embed_model 设为你有权使用的嵌入模型。
401 Unauthorized 密钥缺失、输入有误或已被轮换。 从密钥与用量重新复制密钥;它以 sk- 开头。
报错 context_length_exceeded 或 “prompt is too long”(提示词过长) 对话长度超出了模型的上下文窗口。 开启新对话、让工具压缩对话,或选择窗口更大的模型。
insufficient_quota 你的余额不足以支付该请求。 充值或更换计划;该请求不会自动重试。

常见问题

LlamaIndex 能与 Shannon 一起使用吗?

可以,通过 OpenAILike 并设置 api_base="https://api.shannon-ai.com/v1"。

LlamaIndex 智能体能在 Shannon 上调用工具吗?

能;设置 is_function_calling_model=True 后,模型会返回原生工具调用,我们已测试过。

选择你的语言

所有语言一视同仁。请选择你想使用的语言。

欧洲

中东和北非

南亚

东南亚

东亚

中亚和高加索

非洲

大洋洲

人造语言和古典语言