快速开始
在服务端运行 Python,将 API Key 放入 PRAXIS_API_KEY 环境变量。每次运行创建一个知识库和两个文本文件,并在结束时删除它们。
python -m pip install openai保存为 quickstart.py,运行 python quickstart.py。先上传政策文件,再建库并等待处理完成;第二个文件也显式用 purpose=user_data 上传,再添加并等待。上传成功后仍需检查入库状态。
import osfrom openai import OpenAI
client = OpenAI( api_key=os.environ["PRAXIS_API_KEY"], base_url="https://api.deepraxis.com/openai/v1", timeout=120.0,)store = Nonefile_ids = []try: uploaded = client.files.create( file=("refund-policy.txt", b"Refunds are available within 30 days of purchase."), purpose="user_data", ) 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}")
second = client.files.create( file=("refund-timing.txt", b"Refunds are processed in 5 business days."), purpose="user_data", ) file_ids.append(second.id) attached = client.vector_stores.files.create_and_poll( vector_store_id=store.id, file_id=second.id, poll_interval_ms=1000, ) 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。
文本、Markdown、PDF、DOCX、CSV、JSON 与 HTML 通常需要数秒到数十秒,图片更久。这些只是参考耗时,不是时延保证。服务允许处理至多 30 分钟;失败时查看 last_error。
示例删除知识库后,知识库会保留 7 天供恢复;源文件单独删除。若要继续使用这些资料,请在自己的程序中将清理步骤放到使用结束之后。
在控制台里建
- 打开控制台 → 知识库(
/knowledge),点「新建知识库」,填名称后点「创建」。 - 进入这个知识库,点「添加文件」,上传要让智能体查的文档。