Skip to content
LlamaIndex 文档版本 跟随上游 源快照 7495b815 更新于 2026-10-18

Helicone 是一个兼容 OpenAI 的 AI 网关,可将请求路由到众多提供商,并提供可观测性、控制和缓存。请在 Helicone 文档中了解更多,并查看可用的模型。

如果你在 Colab 中打开这个 Notebook,可能需要安装下面的集成包。

请注意:

  • 只需要你的 Helicone API 密钥(HELICONE_API_KEY);不需要提供商密钥。
  • 默认基础 URL 为 https://ai-gateway.helicone.ai/v1。可通过 api_base 或 HELICONE_API_BASE 覆盖。
%pip install llama-index-llms-helicone
!pip install llama-index
from llama_index.llms.helicone import Helicone
from llama_index.core.llms import ChatMessage

使用 ChatMessage 列表调用 chat

你需要设置环境变量 HELICONE_API_KEY,或在构造函数中传入 api_key。

# import os
# os.environ["HELICONE_API_KEY"] = "<your-helicone-api-key>"

llm = Helicone(
    api_key="<your-helicone-api-key>",  # or set HELICONE_API_KEY
    model="gpt-4o-mini",  # routed via the Helicone AI Gateway
    max_tokens=256,
)
message = ChatMessage(role="user", content="Tell me a joke")
resp = llm.chat([message])
print(resp)

流式

message = ChatMessage(role="user", content="Tell me a story in 200 words")
resp = llm.stream_chat([message])
for r in resp:
    print(r.delta, end="")

API 支持(仅聊天;不支持旧版 Completions)

Helicone 支持兼容 OpenAI 的 Chat Completions 以及更新的 Responses API。不支持旧版 Completions API。

在 LlamaIndex 中,使用 llm.chat(...) 和 llm.stream_chat(...)。

模型配置

# Choose any OpenAI-compatible model routed by Helicone.
# See https://www.helicone.ai/models for options.

# If HELICONE_API_KEY is set in your environment, you can omit api_key here.
llm = Helicone(model="gpt-4o-mini")
message = ChatMessage(
    role="user", content="Write one sentence about Rust dragons coding."
)
resp = llm.chat([message])
print(resp)

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。