Quickstart with Pieces OS Client Python SDK
The Pieces OS Client SDK has a built-in wrapper that simplifies the process of interacting with the Pieces OS server. Here's how you can get started with the wrapper.
Installation
To get started with the Pieces OS Client SDK, follow these steps:
-
Download Pieces OS: Pieces OS serves as the primary backend service, providing essential functionality for the SDK. Download the appropriate version for your operating system:
-
Install the SDK: Use pip to install the Pieces OS Client SDK package:
pip install pieces_os_client
Initialize the Pieces Client
from pieces_os_client.wrapper import PiecesClient
# Initialize the PiecesClient
pieces_client = PiecesClient()
Determining Your Base URL
In the code snippet above, we use the platform
package to determine the base URL based on the operating system. However, you can also set the base URL manually if you know your application will only run on a specific operating system.
- Local Instance of Pieces OS:
- On macOS/Windows, use
http://localhost:1000
- On Linux, use
http://localhost:5323
- On macOS/Windows, use
- Remote Instance of Pieces OS:
- Use the URL you have set up for your remote instance
Examples
Here are some examples of how you can use to get familiar with the Pieces OS Client SDK.
Create a New Asset
To create a new asset, you can use the create_asset
method of the Pieces Client. Here's an example of how to create a new asset:
from pieces_os_client.wrapper import PiecesClient
from pieces_os_client import FragmentMetadata
# Initialize the PiecesClient
pieces_client = PiecesClient()
# Set the content and metadata for the new asset
content = "print('Hello, World!')"
metadata = FragmentMetadata(ext=ClassificationSpecificEnum.PY) # optional metadata
# Create the new asset using the content and metadata
new_asset_id = pieces_client.create_asset(content, metadata)
print(f"Created asset with ID: {new_asset_id}")
Get All Assets
To get all your assets, you can use the assets
method of the Pieces Client. Here's an example of how to get all your assets and print their names:
from pieces_os_client.wrapper import PiecesClient
# Initialize the PiecesClient
pieces_client = PiecesClient()
# Get all assets and print their names
assets = pieces_client.assets()
for asset in assets:
logger.info(f"Asset Name: {asset.name}")
Ask a Question to Pieces Copilot
To ask a question to Pieces Copilot and stream the response, you can use the stream_question
method of the Pieces Client. Here's an example of how to ask a question and stream the response:
from pieces_os_client.wrapper import PiecesClient
# Initialize the PiecesClient
pieces_client = PiecesClient()
# Set the question you want to ask
question = "What is Object-Oriented Programming?"
# Ask the question and stream the response
for response in pieces_client.copilot.stream_question(question):
if response.question:
# Each answer is a chunk of the entire response to the question
answers = response.question.answers.iterable
for answer in answers:
print(answer.text,end="")
Next Steps
You can explore more features and functionalities of the built-in wrapper.