代码
LlamaIndex
使用 LlamaIndex 的 OpenAILike 类,填入基础 URL、你的密钥和模型 ID。再设置它无法自行推断的四个参数,LlamaIndex 的其余功能就能在此之上正常工作。
核对于 2026年10月29日
安装与连接
pip install llama-index-core llama-index-llms-openai-like
export NOKEEP_API_KEY="sk-nokeep-test" # your own key once it connectsimport os
from llama_index.llms.openai_like import OpenAILike
llm = OpenAILike(
model="deepseek-v4.1-flash-uncensored",
api_base="https://nokeep.ai/v1",
api_key=os.environ["NOKEEP_API_KEY"],
is_chat_model=True,
is_function_calling_model=True,
context_window=1000000,
temperature=0.8,
)
print(llm.complete("Name three good names for a tavern.").text)is_chat_model=True会发送聊天补全请求。不设置的话,LlamaIndex 会调用/v1/completions,而 nokeep 没有这个接口。is_function_calling_model=True让智能体和chat_with_tools能够发送工具。context_window默认为 3,900 个 token,因此 LlamaIndex 会裁剪长对话和检索到的文本以适配。请设为模型的真实上下文:DeepSeek 为 1,000,000,GLM 为 128,000。temperature默认为 0.1,这会让小说写得平淡、重复。请自行设置。
对话与流式输出
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(role="system", content="You are a blunt editor."),
ChatMessage(role="user", content="Cut this opening line to ten words: ..."),
]
print(llm.chat(messages).message.content)
for part in llm.stream_chat(messages):
print(part.delta, end="", flush=True)工具与智能体
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
def get_weather(city: str) -> str:
"""Get the weather in a city."""
return f"18°C and clear in {city}"
agent = FunctionAgent(
tools=[get_weather],
llm=llm,
system_prompt="You answer questions about the weather.",
)
async def main():
print(await agent.run("What's the weather in Lisbon?"))
asyncio.run(main())若只需一次带工具的调用、不需要循环,请使用 llm.chat_with_tools([tool], user_msg=...),并用 llm.get_tool_calls_from_response(...) 读取调用结果。参见工具调用。
思考与其他字段
import os
from llama_index.llms.openai_like import OpenAILike
thinker = OpenAILike(
model="deepseek-v4.1-flash-uncensored",
api_base="https://nokeep.ai/v1",
api_key=os.environ["NOKEEP_API_KEY"],
is_chat_model=True,
context_window=1000000,
max_tokens=4000,
additional_kwargs={"reasoning_effort": "high", "min_p": 0.05},
)additional_kwargs 会原样附加到每个请求中。思考内容计入 max_tokens,所以要留出足够空间。参见思考。
注意事项
- `reasoning_effort` 只接受模型自身的取值。
deepseek-v4.1-flash-uncensored接受none、low、high、max;glm-5.3-flash-uncensored接受low、high。其他应用传入的minimal、medium和xhigh会按模型最接近的档位运行(medium按high,模型支持时xhigh按max);任何其他值都会返回400 invalid_reasoning_effort。参见思考。 - GLM 自己选择工具。
glm-5.3-flash-uncensored只接受tool_choice为auto或none。强制调用工具(required,或指定某个函数名)会返回400 unsupported_tool_choice。需要强制调用时请用 DeepSeek。 - 模型不支持的字段会被丢弃,而不是拒绝。 调用仍会返回结果,响应头
x-nokeep-ignored会列出被丢弃的内容。GLM 只保留tools、tool_choice和reasoning_effort,因此它的temperature和response_format会被丢弃。参见各模型支持的字段。 - `n` 必须为 1。 需要多个回复时,请发起多次调用。
- 只支持聊天补全。 没有
/v1/responses,也没有/v1/embeddings;默认使用其中之一的框架需要改为指向聊天补全,而嵌入功能需要另一家提供方。
对于 RAG 应用,设置 Settings.llm = llm,并保留你原有的嵌入模型:nokeep 只运行聊天模型,不提供嵌入。使用 glm-5.3-flash-uncensored 时,LlamaIndex 每次调用发送的 temperature 会被丢弃,并在 x-nokeep-ignored 中注明。
常见问题
为什么用 OpenAILike 而不是 OpenAI?
LlamaIndex 的 OpenAI 类会把模型名与它自己的列表对照,并拒绝不认识的名称。OpenAILike 接受任意名称和任意基础 URL。
LlamaIndex 会在 nokeep 上为我的文档建立索引吗?
不会。索引和嵌入在你设置的地方运行。只有聊天调用会到达 nokeep,而 nokeep 只为每次调用保存 token 计数。