The official Python SDK for Crewplus, used to interact with Crewplus backend services programmatically.
Project description
Crewplus Client SDK
crewplus-client is the official Python SDK for the Crewplus platform. It provides developers with a concise, friendly, and object-oriented interface to interact with various Crewplus services programmatically.
✨ Features
- Complete API Coverage: Aims to cover all Crewplus backend API endpoints.
- Object-Oriented Design: Abstracts core resources like knowledge bases, documents, and tasks into Python objects for intuitive interaction.
- Automatic Authentication: After initializing the client, the SDK automatically handles the authentication process for all requests.
- Robust Error Handling: Maps HTTP error codes to specific Python exceptions for easy capturing and handling.
- Type Hinting Support: Data models built on Pydantic provide comprehensive type hints to improve development efficiency.
🚀 Quick Start
1. Installation
You can install the library from PyPI using pip:
pip install crewplus-python
2. Basic Usage
The following is a complete example that demonstrates how to use the SDK to create a knowledge base, query it, and then delete it.
# examples/01_kb_workflow.py
import os
import uuid
import time
from crewplus_client import CrewPlusClient, ApiException, NotFoundException
def run_kb_workflow():
"""
Demonstrates the complete lifecycle of a knowledge base: Create -> Query -> Delete.
"""
# --- 1. Initialize the Client ---
# It's recommended to read the configuration from environment variables to avoid hardcoding.
API_HOST = os.environ.get("CREWPLUS_API_HOST")
API_KEY = os.environ.get("CREWPLUS_API_KEY")
if not API_HOST or not API_KEY:
print("Error: Please set the CREWPLUS_API_HOST and CREWPLUS_API_KEY environment variables.")
print("For example: export CREWPLUS_API_HOST='http://127.0.0.1:9000'")
print(" export CREWPLUS_API_KEY='your_api_key_here'")
return
print(f"Client initialized, target host: {API_HOST}")
client = CrewPlusClient(host=API_HOST, api_key=API_KEY)
# --- 2. Prepare Data ---
# The coll_name and coll_id of the knowledge base need to be planned according to business requirements.
unique_id = str(uuid.uuid4())[:8]
kb_coll_name = f"sdk-test-kb-{unique_id}"
kb_coll_id = str(uuid.uuid4())
# Used to temporarily store the ID of the successfully created knowledge base for later cleanup.
created_kb_id = None
try:
# --- 3. Create a New Knowledge Base ---
print(f"\n--> Creating knowledge base '{kb_coll_name}'...")
new_kb = client.knowledge_bases.create(
coll_name=kb_coll_name,
coll_id=kb_coll_id,
description="This is a test knowledge base created via the SDK"
)
created_kb_id = new_kb.id
print(f"✔️ Knowledge base created successfully! ID: {created_kb_id}, Name: '{new_kb.name}'")
# Wait a moment to ensure backend data synchronization.
time.sleep(1)
# --- 4. Fetch the Newly Created Knowledge Base ---
print(f"\n--> Fetching knowledge base by ID ({created_kb_id})...")
fetched_kb = client.knowledge_bases.get(kb_id=created_kb_id)
print(f"✔️ Fetched successfully! Name: '{fetched_kb.name}', Description: '{fetched_kb.description}'")
# --- 5. Find Knowledge Base by coll_name ---
print(f"\n--> Finding knowledge base by coll_name ('{kb_coll_name}')...")
found_kb = client.knowledge_bases.find_by_coll_name(coll_name=kb_coll_name)
print(f"✔️ Found knowledge base! ID: {found_kb.id}")
# --- 6. List All Knowledge Bases (for demonstration) ---
print("\n--> Listing the top 5 knowledge bases...")
kb_list = client.knowledge_bases.list(limit=5)
print(f"✔️ Listed {len(kb_list)} knowledge bases:")
for kb in kb_list:
print(f" - ID: {kb.id}, Name: {kb.name}")
except NotFoundException as e:
print(f"\n❌ Operation failed: Resource not found. {e}")
except ApiException as e:
print(f"\n❌ Operation failed: An API error occurred. {e}")
except Exception as e:
print(f"\n❌ Operation failed: An unknown error occurred. {e}")
finally:
# --- 7. Clean Up Resources ---
if created_kb_id:
print(f"\n--> Cleaning up the created knowledge base (ID: {created_kb_id})...")
try:
client.knowledge_bases.delete(kb_id=created_kb_id)
print("✔️ Cleanup successful!")
except ApiException as e:
print(f"❌ Cleanup failed. {e}")
if __name__ == "__main__":
run_kb_workflow()
Contributing
We welcome contributions of all forms! If you have any questions or suggestions, please feel free to submit an Issue or Pull Request.
License
This project is open-sourced under the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file crewplus_python-0.1.4.tar.gz.
File metadata
- Download URL: crewplus_python-0.1.4.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.2 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ad38e960a9d9866205f114853d85702a24fa5bb2622489d1905306e33331703
|
|
| MD5 |
b432359fd9c7cfa1e678a5bb6f0ec2d4
|
|
| BLAKE2b-256 |
3ad7e1e6bb3ad58c4013a7005cdf832dadb22c4bc123710c7b4b43d00f966925
|
File details
Details for the file crewplus_python-0.1.4-py3-none-any.whl.
File metadata
- Download URL: crewplus_python-0.1.4-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.2 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9334f5c922030fdfdee75817dc1425e43776d7eb5cb9c71ad80908d7f66ae0c2
|
|
| MD5 |
055dad07d3a6677f279c1e4370ef04f6
|
|
| BLAKE2b-256 |
830ec33b5d5a42f59730bd0422deb8ae3c198d245edadbd1e1d42fd883db54d3
|