知识库
快速开始
用官方 openai Python SDK 上传文件、创建知识库并搜索。
在服务端运行 Python,将 develop API Key 放入 PRAXIS_API_KEY 环境变量。示例连接 develop,每次创建一个知识库和两个文本文件,并在结束时删除它们。
python -m pip install openai保存为 quickstart.py,运行 python quickstart.py。先上传政策文件,再建库并等待处理完成;upload_and_poll 将第二个文件的上传、添加和等待合成一次调用。上传成功后仍需检查入库状态。
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["PRAXIS_API_KEY"],
base_url="https://api.deepraxis.ai/openai/v1",
timeout=120.0,
)
store = None
file_ids = []
try:
uploaded = client.files.create(
file=("refund-policy.txt", b"Refunds are available within 30 days of purchase."),
purpose="assistants",
)
file_ids.append(uploaded.id)
print("uploaded:", uploaded.id)
store = client.vector_stores.create(
name="Knowledge quickstart",
file_ids=[uploaded.id],
)
print("store:", store.id)
first = client.vector_stores.files.poll(
uploaded.id, vector_store_id=store.id, poll_interval_ms=1000,
)
if first.status != "completed":
raise RuntimeError(f"Ingestion failed: {first.last_error}")
attached = client.vector_stores.files.upload_and_poll(
vector_store_id=store.id,
file=("refund-timing.txt", b"Refunds are processed in 5 business days."),
poll_interval_ms=1000,
)
file_ids.append(attached.id)
print("ingestion:", attached.status)
if attached.status != "completed":
raise RuntimeError(f"Ingestion failed: {attached.last_error}")
results = client.vector_stores.search(
vector_store_id=store.id,
query="How many business days does a refund take?",
max_num_results=5,
)
assert results.data, "Expected at least one matching chunk"
for result in results.data:
print(result.filename, result.score)
for part in result.content:
print(part.text)
print("pagination:", results.has_more, results.next_page)
finally:
try:
if store is not None:
deleted = client.vector_stores.delete(store.id)
print("store deleted:", deleted.deleted)
finally:
for file_id in file_ids:
deleted = client.files.delete(file_id)
print("file deleted:", file_id, deleted.deleted)
client.close()输出包含文件 ID、知识库 ID、ingestion: completed、命中文本与清理结果。每个文件一条结果,命中的文本块按相关度排列。搜索不支持翻页;结果没有被截断时最后输出 pagination: False None。
2026-10-11 的 develop 实测中,文本、Markdown、PDF、DOCX、CSV、JSON 与 HTML 处理约需 4–21 秒;一张 PNG 约需 87 秒。这些是样例耗时,不是时延保证。服务允许处理至多 30 分钟;失败时查看 last_error。
示例删除知识库后,知识库会保留 7 天供恢复;源文件单独删除。若要继续使用这些资料,请在自己的程序中将清理步骤放到使用结束之后。