### Install Gradient SDK Source: https://github.com/digitalocean/gradient-python/blob/main/README.md Use pip to install the package from PyPI. ```sh # install from PyPI pip install gradient ``` -------------------------------- ### Install from Wheel File Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md Install the Gradient Python library using a locally built wheel file. ```sh pip install ./path-to-wheel-file.whl ``` -------------------------------- ### Add and Run Examples Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md Add new example Python files to the `examples/` directory and make them executable. These files are not modified by the generator and can be freely edited. ```python # add an example to examples/.py #!/usr/bin/env -S rye run python … ``` ```sh $ chmod +x examples/.py # run the example against your api $ ./examples/.py ``` -------------------------------- ### Install aiohttp for async performance Source: https://github.com/digitalocean/gradient-python/blob/main/README.md Install the necessary dependency to enable the aiohttp HTTP backend. ```sh # install from PyPI pip install gradient[aiohttp] ``` -------------------------------- ### Install from Git Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md Install the Gradient Python library directly from its Git repository using pip. ```sh pip install git+ssh://git@github.com/digitalocean/gradient-python.git ``` -------------------------------- ### Install Dependencies with Pip Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md If not using Rye, install development dependencies using pip and the lock file. ```sh pip install -r requirements-dev.lock ``` -------------------------------- ### Create Agent Function Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Example of defining a function with parameters for an agent. ```python function = client.agents.functions.create( agent_id="agent_123", name="get_order_status", description="Retrieve the status of a customer order", parameters={ "type": "object", "properties": { "order_id": { "type": "string", "description": "The order ID" }, "customer_id": { "type": "string", "description": "The customer ID" } }, "required": ["order_id"] } ) print(f"Function created: {function.id}") ``` -------------------------------- ### AsyncGradient Usage Examples Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Common patterns for initializing and using the AsyncGradient client, including basic usage, context managers, and custom HTTP backends. ```python import asyncio from gradient import AsyncGradient async def main(): client = AsyncGradient(access_token="your_token") response = await client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) print(response.choices[0].message.content) asyncio.run(main()) ``` ```python import asyncio from gradient import AsyncGradient async def main(): async with AsyncGradient(access_token="your_token") as client: response = await client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) print(response.choices[0].message.content) asyncio.run(main()) ``` ```python import asyncio from gradient import AsyncGradient, DefaultAioHttpClient async def main(): async with AsyncGradient( access_token="your_token", http_client=DefaultAioHttpClient() ) as client: response = await client.chat.completions.create( messages=[{"role": "user", "content": "What is Python?"}], model="llama3.3-70b-instruct" ) print(response.choices[0].message.content) asyncio.run(main()) ``` -------------------------------- ### Usage with Type Hints Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/03-types-core.md Example demonstrating explicit type annotation for client responses. ```python from gradient import Gradient from gradient.types.chat.completion_create_response import CompletionCreateResponse client: Gradient = Gradient(access_token="token") response: CompletionCreateResponse = client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) content: str | None = response.choices[0].message.content ``` -------------------------------- ### Handle NotFoundError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching a 404 Not Found error. ```python import gradient try: client = gradient.Gradient(access_token="token") response = client.agents.retrieve(agent_id="nonexistent-id") except gradient.NotFoundError as e: print(f"Not found: {e.message}") print(f"Agent with ID does not exist") ``` -------------------------------- ### Generate multiple images Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/06-api-models-images.md Example showing how to generate multiple images by setting the 'n' parameter. ```python response = client.images.generations.create( model="dall-e-3", prompt="A futuristic city with flying cars", n=4, size="1024x1024", quality="hd" ) for i, image in enumerate(response.data): print(f"Image {i+1}: {image.url}") ``` -------------------------------- ### Example System Message Usage Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/03-types-core.md Shows a basic implementation of a system message alongside a user message. ```python messages=[ { "role": "system", "content": "You are a helpful assistant. Respond concisely." }, { "role": "user", "content": "What is Python?" } ] ``` -------------------------------- ### Create Agent API Key Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Example of generating an API key for an agent, which should be used in the Authorization header. ```python api_key = client.agents.api_keys.create( agent_id="agent_123", name="production_key" ) print(f"API Key: {api_key.key}") # Use this key in Authorization header for agent API calls ``` -------------------------------- ### Text Generation Usage Example Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/06-api-models-images.md Shows how to invoke the responses.create method to generate text from a given input and model. ```python response = client.responses.create( input="Summarize: The Python programming language is known for...", model="llama3.3-70b-instruct" ) print(response.text) ``` -------------------------------- ### Handle AuthenticationError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching a 401 Authentication error. ```python import gradient try: client = gradient.Gradient(access_token="invalid_token") response = client.agents.list() except gradient.AuthenticationError as e: print(f"Authentication failed: {e.message}") print(f"Check your access_token") ``` -------------------------------- ### Vivid style generation Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/06-api-models-images.md Example of generating an image with the 'vivid' style preference. ```python response = client.images.generations.create( model="dall-e-3", prompt="Abstract geometric art", style="vivid", size="1024x1024" ) ``` -------------------------------- ### Check SDK Version Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/00-index.md Verify the currently installed version of the gradient package. ```python import gradient print(gradient.__version__) # "3.12.2" ``` -------------------------------- ### Create Action for Image Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Initiate an action on a specific image. This is typically used for operations like starting or stopping an image process. ```python client.gpu_droplets.images.actions.create(image_id, **params) -> Action ``` -------------------------------- ### Sync Dependencies with Rye Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md If installing Rye manually, use this command to sync all project dependencies, including features. ```sh rye sync --all-features ``` -------------------------------- ### Model Listing Usage Examples Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/06-api-models-images.md Various ways to query the models API, including filtering by use case and handling pagination. ```python from gradient import Gradient client = Gradient(access_token="token") response = client.models.list( usecases=["MODEL_USECASE_SERVERLESS"], public_only=True ) for model in response.models: print(f"{model.name} ({model.slug})") print(f" Context: {model.context_length} tokens") ``` ```python response = client.models.list( usecases=["MODEL_USECASE_AGENT"], page=1, per_page=10 ) print(f"Total models: {response.meta.total}") for model in response.models: print(f"- {model.slug}: {model.description}") ``` ```python response = client.models.list( usecases=["MODEL_USECASE_KNOWLEDGEBASE"] ) embedding_models = response.models print(f"Available embedding models: {len(embedding_models)}") ``` ```python page = 1 per_page = 20 while True: response = client.models.list(page=page, per_page=per_page) if not response.models: break for model in response.models: print(model.slug) page += 1 ``` -------------------------------- ### Generate a single image Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/06-api-models-images.md Basic usage example for generating one image with a specific model and prompt. ```python from gradient import Gradient client = Gradient(model_access_key="key") response = client.images.generations.create( model="stable-diffusion-3", prompt="A serene landscape with mountains and a lake at sunset", size="1024x1024" ) image_url = response.data[0].url print(f"Generated image: {image_url}") ``` -------------------------------- ### Retrieve Gradient Version and Title Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Use the gradient package attributes to programmatically check the installed version and library title. ```python import gradient print(gradient.__version__) # Returns "3.12.2" print(gradient.__title__) # Returns "gradient" ``` -------------------------------- ### Initialize Gradient Clients Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/00-index.md Demonstrates how to instantiate different Gradient client types based on the required authentication method and service target. ```python # API management api_client = Gradient(access_token="api_token") # Inference only inference_client = Gradient(model_access_key="key") # Deployed agents agent_client = Gradient( agent_access_key="agent_key", agent_endpoint="https://my-agent.agents.do-ai.run" ) ``` -------------------------------- ### Create a Knowledge Base Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Initializes a new knowledge base with a name, description, embedding model, and metadata. ```python kb = client.knowledge_bases.create( name="product-docs", description="Product documentation and FAQ", embedding_model="text-embedding-ada-002", metadata={ "category": "documentation", "version": "1.0" } ) print(f"Knowledge base created: {kb.id}") ``` -------------------------------- ### GET /v2/agents Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md List all agents. ```APIDOC ## GET /v2/agents ### Description List all agents. ### Method GET ### Endpoint /v2/agents ``` -------------------------------- ### Initialize DefaultHttpxClient Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Configures a synchronous HTTP client with custom proxy and transport settings. ```python from gradient import DefaultHttpxClient import httpx client = DefaultHttpxClient( proxy="http://proxy.example.com:8080", transport=httpx.HTTPTransport(local_address="0.0.0.0") ) ``` -------------------------------- ### Initialize Gradient Client with Environment Variables Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md The client automatically loads credentials from environment variables if no arguments are provided. ```python from gradient import Gradient # Environment variables: # DIGITALOCEAN_ACCESS_TOKEN=your_token # GRADIENT_MODEL_ACCESS_KEY=your_key # GRADIENT_AGENT_ACCESS_KEY=your_key client = Gradient() ``` -------------------------------- ### Gradient.__init__ Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Initializes the Gradient synchronous client. Credentials and endpoints can be provided explicitly or loaded from environment variables. ```APIDOC ## Gradient.__init__ ### Description Initializes the main synchronous client for accessing Gradient services including serverless inference, agent APIs, and DigitalOcean resource management. ### Parameters - **access_token** (str | None) - Optional - Bearer token for DigitalOcean API access. Auto-loaded from `DIGITALOCEAN_ACCESS_TOKEN`. - **model_access_key** (str | None) - Optional - Bearer token for serverless inference access. Auto-loaded from `GRADIENT_MODEL_ACCESS_KEY`. - **agent_access_key** (str | None) - Optional - Bearer token for agent-scoped inference access. Auto-loaded from `GRADIENT_AGENT_ACCESS_KEY`. - **agent_endpoint** (str | None) - Optional - Fully-qualified HTTPS endpoint for deployed agents. Auto-loaded from `GRADIENT_AGENT_ENDPOINT`. - **inference_endpoint** (str | None) - Optional - Base URL for serverless inference. Auto-loaded from `GRADIENT_INFERENCE_ENDPOINT`. - **kbass_endpoint** (str | None) - Optional - Base URL for knowledge base endpoints. Auto-loaded from `GRADIENT_KBASS_ENDPOINT`. - **base_url** (str | httpx.URL | None) - Optional - Base URL for DigitalOcean API requests. Auto-loaded from `GRADIENT_BASE_URL`. - **timeout** (float | Timeout | None | NotGiven) - Optional - Request timeout configuration. - **max_retries** (int) - Optional - Number of retries for failed requests. - **default_headers** (Mapping[str, str] | None) - Optional - Default HTTP headers. - **default_query** (Mapping[str, object] | None) - Optional - Default query parameters. - **http_client** (httpx.Client | None) - Optional - Custom httpx.Client instance. - **_strict_response_validation** (bool) - Optional - Enable strict validation of API responses. - **user_agent_package** (str | None) - Optional - Custom user agent package name. - **user_agent_version** (str | None) - Optional - Custom user agent package version. ``` -------------------------------- ### Handle AgentDeploymentError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching an AgentDeploymentError when an agent deployment fails. ```python import gradient try: client = gradient.Gradient(access_token="token", agent_endpoint="https://...") agent = client.agents.create( name="my-agent", model="llama3.3-70b-instruct" ) # Poll deployment status while True: status = client.agents.retrieve(agent_id=agent.id).deployment_status if status == "FAILED": raise gradient.AgentDeploymentError( "Deployment failed", status="FAILED" ) if status == "RUNNING": break except gradient.AgentDeploymentError as e: print(f"Deployment error: {e.message}, Status: {e.status}") ``` -------------------------------- ### Bootstrap Environment with Rye Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md Run this script to set up the development environment using Rye, which automatically provisions the correct Python version and dependencies. ```sh ./scripts/bootstrap ``` -------------------------------- ### Handle PermissionDeniedError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching a 403 Permission Denied error. ```python import gradient try: client = gradient.Gradient(access_token="token") response = client.gpu_droplets.create(...) except gradient.PermissionDeniedError as e: print(f"Permission denied: {e.message}") print(f"Insufficient permissions for operation") ``` -------------------------------- ### Initialize Gradient Clients Source: https://github.com/digitalocean/gradient-python/blob/main/README.md Initialize clients for API, serverless inference, and agent inference using environment variables. ```python import os from gradient import Gradient client = Gradient( access_token=os.environ.get( "DIGITALOCEAN_ACCESS_TOKEN" ), # This is the default and can be omitted ) inference_client = Gradient( model_access_key=os.environ.get( "GRADIENT_MODEL_ACCESS_KEY" ), # This is the default and can be omitted ) agent_client = Gradient( agent_access_key=os.environ.get( "GRADIENT_AGENT_ACCESS_KEY" ), # This is the default and can be omitted agent_endpoint="https://my-agent.agents.do-ai.run", ) ``` -------------------------------- ### Handle BadRequestError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching a 400 Bad Request error. ```python import gradient try: client = gradient.Gradient(access_token="token") response = client.chat.completions.create( messages=[], # Empty messages list model="invalid-model" ) except gradient.BadRequestError as e: print(f"Bad request: {e.message}") print(f"Request: {e.request.url}") ``` -------------------------------- ### List Supported Backup Policies Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Fetches a list of all supported backup policies for GPU Droplets. ```python client.gpu_droplets.backups.list_supported_policies() ``` -------------------------------- ### Handle APIResponseValidationError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching validation errors when _strict_response_validation is enabled. ```python import gradient try: client = gradient.Gradient( access_token="token", _strict_response_validation=True ) response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) except gradient.APIResponseValidationError as e: print(f"Validation failed: {e.message}") print(f"Status code: {e.status_code}") print(f"Response body: {e.body}") ``` -------------------------------- ### Create an Agent Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Initializes a Gradient client and creates a new agent with specified configuration. ```python from gradient import Gradient client = Gradient(access_token="token") agent = client.agents.create( name="my-ai-assistant", model="llama3.3-70b-instruct", description="Customer support assistant", system_prompt="You are a helpful customer support agent. Be concise and professional.", deployment_visibility="PRIVATE", tags={ "department": "support", "version": "1.0" } ) print(f"Agent created: {agent.id}") print(f"Name: {agent.name}") ``` -------------------------------- ### Handle AgentDeploymentTimeoutError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching an AgentDeploymentTimeoutError when deployment exceeds the specified timeout. ```python import gradient try: client = gradient.Gradient(access_token="token") agent = client.agents.create( name="my-agent", model="llama3.3-70b-instruct" ) # Wait for deployment with timeout client.agents.wait_for_deployment( agent_id=agent.id, timeout=1200.0 # 20 minutes ) except gradient.AgentDeploymentTimeoutError as e: print(f"Deployment timeout: {e.message}") print(f"Agent ID: {e.agent_id}") print(f"Check agent status later") ``` -------------------------------- ### Initiate Volume Action by Name Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Initiate a volume action by specifying the volume's name. ```python client.gpu_droplets.volumes.actions.initiate_by_name(**params) ``` -------------------------------- ### Base64-encoded response Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/06-api-models-images.md Example of requesting base64-encoded image data and processing it with PIL. ```python import base64 from PIL import Image from io import BytesIO response = client.images.generations.create( model="stable-diffusion-3", prompt="A cat wearing sunglasses", size="512x512", response_format="b64_json" ) # Decode and display image_data = base64.b64decode(response.data[0].b64_json) img = Image.open(BytesIO(image_data)) img.show() ``` -------------------------------- ### Initialize Gradient Client with Explicit Credentials Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Pass credentials and endpoints directly to the constructor to override environment variables. ```python from gradient import Gradient client = Gradient( access_token="dop_v1_...", model_access_key="gradient_...", agent_access_key="gradient_...", agent_endpoint="https://my-agent.agents.do-ai.run" ) ``` -------------------------------- ### AsyncGradient Streaming Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Example of consuming chat completion responses as an asynchronous stream. ```python import asyncio from gradient import AsyncGradient async def main(): client = AsyncGradient(model_access_key="your_key") stream = await client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct", stream=True ) async for chunk in stream: print(chunk.choices[0].delta.content, end="", flush=True) asyncio.run(main()) ``` -------------------------------- ### Handle APITimeoutError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching and handling timeout errors with a custom timeout configuration. ```python import gradient try: client = gradient.Gradient( access_token="token", timeout=5.0 # Short timeout for demo ) response = client.chat.completions.create( messages=[{"role": "user", "content": "Generate a long response"}], model="llama3.3-70b-instruct" ) except gradient.APITimeoutError as e: print(f"Request timed out: {e.message}") ``` -------------------------------- ### Handle APIConnectionError Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching and handling connection errors when making API requests. ```python import gradient try: client = gradient.Gradient(access_token="token") response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) except gradient.APIConnectionError as e: print(f"Connection error: {e.message}") print(f"Underlying cause: {e.__cause__}") print(f"Check network connectivity") ``` -------------------------------- ### Asynchronous Agent and Knowledge Base Operations Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Demonstrates using the AsyncGradient client to create agents, list knowledge bases, and perform chat completions asynchronously. ```python import asyncio from gradient import AsyncGradient async def main(): async with AsyncGradient(access_token="token") as client: # Create agent agent = await client.agents.create( name="async-agent", model="llama3.3-70b-instruct" ) # List knowledge bases kbs = await client.knowledge_bases.list() # Chat with agent response = await client.agents.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) asyncio.run(main()) ``` -------------------------------- ### Handle RateLimitError in application code Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching a RateLimitError and accessing the Retry-After header. ```python import gradient try: client = gradient.Gradient( access_token="token", max_retries=5 # Increase retries for rate limit scenarios ) for i in range(1000): response = client.chat.completions.create(...) except gradient.RateLimitError as e: print(f"Rate limited: {e.message}") print(f"Retry-After: {e.response.headers.get('Retry-After')}") ``` -------------------------------- ### List Backup Policies Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Retrieves a list of available backup policies for GPU Droplets. ```python client.gpu_droplets.backups.list_policies(**params) ``` -------------------------------- ### Handle InternalServerError in application code Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching an InternalServerError when the server encounters an unexpected issue. ```python import gradient try: client = gradient.Gradient(access_token="token") response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) except gradient.InternalServerError as e: print(f"Server error: {e.message}") print(f"Status: {e.status_code}") print(f"Please retry later") ``` -------------------------------- ### Gradient Client Configuration Source: https://github.com/digitalocean/gradient-python/blob/main/README.md Methods for initializing the Gradient client with custom retry and timeout settings. ```APIDOC ## Gradient Client Configuration ### Description The `Gradient` client can be configured with custom `max_retries` and `timeout` settings to manage network reliability and request duration. ### Parameters - **max_retries** (int) - Optional - Number of times to retry failed requests. Default is 2. - **timeout** (float or httpx.Timeout) - Optional - Request timeout duration in seconds. Default is 60.0 seconds. ### Usage Example ```python from gradient import Gradient # Configure client with custom settings client = Gradient( max_retries=0, timeout=20.0 ) # Override per-request client.with_options(max_retries=5, timeout=5.0).chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) ``` ``` -------------------------------- ### Handle UnprocessableEntityError in application code Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching an UnprocessableEntityError when submitting semantically invalid data. ```python import gradient try: client = gradient.Gradient(access_token="token") response = client.agents.create( name="agent", model="unsupported-model" # Not a valid model ) except gradient.UnprocessableEntityError as e: print(f"Unprocessable: {e.message}") print(f"Invalid entity data: {e.body}") ``` -------------------------------- ### Create a Volume Snapshot Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Create a snapshot for a specified volume. ```python client.gpu_droplets.volumes.snapshots.create(volume_id, **params) ``` -------------------------------- ### Retrieve a Volume Action Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Use this method to get details of a specific volume action by its ID. ```python client.gpu_droplets.volumes.actions.retrieve(action_id, *volume_id, **params) ``` -------------------------------- ### Initialize AsyncGradient with DefaultAioHttpClient Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Initializes an asynchronous client using the aiohttp backend. Requires the gradient[aiohttp] extra. ```python from gradient import AsyncGradient, DefaultAioHttpClient async with AsyncGradient( access_token="token", http_client=DefaultAioHttpClient() ) as client: # Use client... ``` -------------------------------- ### Create Knowledge Base Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Creates a new knowledge base. Accepts parameters for configuration. ```APIDOC ## POST /v2/gen-ai/knowledge_bases ### Description Creates a new knowledge base. ### Method POST ### Endpoint /v2/gen-ai/knowledge_bases ### Parameters #### Request Body - **params** (object) - Required - Parameters for creating the knowledge base. ### Request Example ```json { "example": "request body for create knowledge base" } ``` ### Response #### Success Response (200) - **KnowledgeBaseCreateResponse** (object) - Details of the created knowledge base. ### Response Example ```json { "example": "response body for create knowledge base" } ``` ``` -------------------------------- ### Handle ConflictError in application code Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Example of catching a ConflictError when attempting to create a resource that already exists. ```python import gradient try: client = gradient.Gradient(access_token="token") response = client.agents.create(name="duplicate-agent") except gradient.ConflictError as e: print(f"Conflict: {e.message}") print(f"Agent with this name already exists") ``` -------------------------------- ### Run Tests Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md Execute the project's test suite using the provided script. ```sh ./scripts/test ``` -------------------------------- ### Initialize ChatResource Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/04-api-chat-completions.md Constructor for the top-level chat resource. ```python class ChatResource(SyncAPIResource): def __init__(self, client: SyncAPIClient) -> None ``` -------------------------------- ### create Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Creates a new AI agent with the specified configuration. ```APIDOC ## create ### Description Creates a new AI agent with the provided configuration, including model, system prompt, and knowledge bases. ### Parameters - **name** (str) - Required - Unique agent name - **model** (str) - Optional - Foundation model ID - **description** (str) - Optional - Agent description - **system_prompt** (str) - Optional - System message defining agent behavior - **deployment_visibility** (Literal["PUBLIC", "PRIVATE"]) - Optional - Access control for deployed agent - **routing_rules** (Iterable[RoutingRule]) - Optional - Request routing rules - **knowledge_bases** (Iterable[str]) - Optional - Knowledge base IDs for RAG - **tags** (Dict[str, str]) - Optional - Custom metadata tags ### Returns - **AgentCreateResponse** - Created agent ``` -------------------------------- ### Execute chat completion with tools Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/03-types-core.md Demonstrates calling a chat completion with a defined function tool. ```python from gradient import Gradient client = Gradient(model_access_key="key") response = client.chat.completions.create( messages=[ { "role": "user", "content": "Get the weather in San Francisco" } ], model="llama3.3-70b-instruct", tools=[ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], tool_choice="auto" ) ``` -------------------------------- ### Construct Serverless Inference Client Source: https://github.com/digitalocean/gradient-python/blob/main/MIGRATION.md Update client initialization for serverless inference by switching from the Gradient class to the pydo inference Client. ```python from gradient import Gradient client = Gradient(model_access_key=os.environ["GRADIENT_MODEL_ACCESS_KEY"]) ``` ```python from pydo.inference import Client # Same Model Access Key value as GRADIENT_MODEL_ACCESS_KEY, or a full-access PAT client = Client(api_key=os.environ["GRADIENT_MODEL_ACCESS_KEY"]) # equivalently: # client = Client(token=os.environ["DIGITALOCEAN_TOKEN"]) # must be full-access ``` -------------------------------- ### Replace the Gradient package Source: https://github.com/digitalocean/gradient-python/blob/main/MIGRATION.md Uninstall the deprecated gradient package and install the pydo SDK, including optional async support. ```sh pip uninstall gradient pip install pydo # optional async extras pip install "pydo[aio]" ``` -------------------------------- ### Configure Multiple Clients Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/05-configuration-advanced.md Instantiate separate client objects to manage different authentication scopes or service endpoints. ```python from gradient import Gradient # Separate clients for different purposes api_client = Gradient( access_token="api_token" ) inference_client = Gradient( model_access_key="inference_key" ) agent_client = Gradient( agent_access_key="agent_key", agent_endpoint="https://my-agent.agents.do-ai.run" ) ``` -------------------------------- ### Determine installed library version Source: https://github.com/digitalocean/gradient-python/blob/main/README.md Access the __version__ attribute of the imported gradient module to verify the currently active version at runtime. ```py import gradient print(gradient.__version__) ``` -------------------------------- ### Configure Endpoints via Environment Variables Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/05-configuration-advanced.md Set base URLs using environment variables for configuration without code changes. ```bash export GRADIENT_BASE_URL="https://api.internal.example.com" export GRADIENT_INFERENCE_ENDPOINT="https://inference.internal.example.com" export GRADIENT_AGENT_ENDPOINT="https://my-agent.internal.example.com" ``` -------------------------------- ### Retrieve Action for Image Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Get the details of a specific action performed on an image. Requires both the action ID and the associated image ID. ```python client.gpu_droplets.images.actions.retrieve(action_id, *, image_id) -> Action ``` -------------------------------- ### Execute chat completion with streaming options Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/03-types-core.md Demonstrates enabling streaming and including usage information in the response. ```python response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct", stream=True, stream_options={"include_usage": True} ) ``` -------------------------------- ### Construct Control-plane Client Source: https://github.com/digitalocean/gradient-python/blob/main/MIGRATION.md Update client initialization for control-plane or GenAI management operations. ```python from gradient import Gradient client = Gradient(access_token=os.environ["DIGITALOCEAN_ACCESS_TOKEN"]) ``` ```python from pydo import Client # Same PAT value as DIGITALOCEAN_ACCESS_TOKEN; preferred env name is DIGITALOCEAN_TOKEN client = Client(token=os.environ["DIGITALOCEAN_TOKEN"]) ``` -------------------------------- ### List Evaluation Metric Regions Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Fetches a list of available regions for evaluation metrics. This method corresponds to the get /v2/gen-ai/regions API endpoint. ```python client.agents.evaluation_metrics.list_regions(**params) -> EvaluationMetricListRegionsResponse ``` -------------------------------- ### Asynchronous Gradient Client Usage Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/06-api-models-images.md Demonstrates using the AsyncGradient client to list models and generate images asynchronously. ```python import asyncio from gradient import AsyncGradient async def main(): async with AsyncGradient(access_token="token") as client: # List models models = await client.models.list( usecases=["MODEL_USECASE_SERVERLESS"] ) print(f"Models: {len(models.models)}") # Generate image image = await client.images.generations.create( model="dall-e-3", prompt="A beautiful sunset" ) print(f"Image: {image.data[0].url}") asyncio.run(main()) ``` -------------------------------- ### Initiate Volume Action by ID Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Initiate a volume action using the volume's ID. ```python client.gpu_droplets.volumes.actions.initiate_by_id(volume_id, **params) ``` -------------------------------- ### Create Knowledge Base Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Use this method to create a new Knowledge Base. It accepts parameters defined in `knowledge_base_create_params.py`. ```python client.knowledge_bases.create(**params) -> KnowledgeBaseCreateResponse ``` -------------------------------- ### Monitor Indexing Job Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Demonstrates how to create an indexing job and poll its status until completion or failure. ```python # Create indexing job job = client.knowledge_bases.indexing_jobs.create( knowledge_base_id="kb_123", documents=[...] ) # Poll for completion import time while True: status = client.knowledge_bases.indexing_jobs.retrieve(job.id) if status.status == "COMPLETED": print(f"Indexing complete: {status.indexed_count} documents") break elif status.status == "FAILED": print(f"Indexing failed: {status.error}") break time.sleep(5) # Check every 5 seconds ``` -------------------------------- ### List Supported Backup Policies Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Lists all supported backup policies. ```APIDOC ## GET /v2/droplets/backups/supported_policies ### Description Lists all supported backup policies. ### Method GET ### Endpoint /v2/droplets/backups/supported_policies ### Response #### Success Response (200) - **BackupListSupportedPoliciesResponse** - A list of supported backup policies. ``` -------------------------------- ### Run SDK Smoke Tests Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/00-index.md Execute live API smoke tests locally after setting the required environment variables. ```bash # Set required environment variables export DIGITALOCEAN_ACCESS_TOKEN=... export GRADIENT_MODEL_ACCESS_KEY=... export GRADIENT_AGENT_ACCESS_KEY=... export GRADIENT_AGENT_ENDPOINT=... # Run smoke tests pytest tests/test_smoke_sdk.py -v ``` -------------------------------- ### Configuring Connection Pooling Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/05-configuration-advanced.md Optimizes HTTP connection limits by passing a custom httpx client to the Gradient constructor. ```python import httpx from gradient import Gradient, DefaultHttpxClient # Optimize connection limits for high throughput http_client = DefaultHttpxClient( limits=httpx.Limits( max_connections=200, max_keepalive_connections=50, keepalive_expiry=30 ) ) client = Gradient( access_token="token", http_client=http_client ) ``` -------------------------------- ### Basic Chat Completion Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/04-api-chat-completions.md Initializes the Gradient client and performs a standard chat completion request. ```python from gradient import Gradient client = Gradient(model_access_key="your_key") response = client.chat.completions.create( messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is 2+2?"} ], model="llama3.3-70b-instruct" ) print(response.choices[0].message.content) # Output: 2+2 equals 4. ``` -------------------------------- ### create Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Creates a new knowledge base for Retrieval-Augmented Generation. ```APIDOC ## create ### Description Creates a new knowledge base with the specified name, description, embedding model, and metadata. ### Parameters - **name** (str) - Required - Unique knowledge base name - **description** (str) - Optional - KB description - **embedding_model** (str) - Optional - Model for generating embeddings - **metadata** (Dict[str, str]) - Optional - Custom metadata ### Returns - **KnowledgeBaseCreateResponse** - Created knowledge base ``` -------------------------------- ### Create an Agent Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Method signature for creating a new AI agent with specified configuration. ```python def create( self, *, name: str, model: str | Omit = omit, description: str | Omit = omit, system_prompt: str | Omit = omit, deployment_visibility: Literal["PUBLIC", "PRIVATE"] | Omit = omit, routing_rules: Iterable[agent_create_params.RoutingRule] | Omit = omit, knowledge_bases: Iterable[str] | Omit = omit, tags: Dict[str, str] | Omit = omit, extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> AgentCreateResponse ``` -------------------------------- ### Import GPU Droplet Backup Types Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Import necessary types for managing GPU Droplet backups. ```python from gradient.types.gpu_droplets import ( BackupListResponse, BackupListPoliciesResponse, BackupListSupportedPoliciesResponse, BackupRetrievePolicyResponse, ) ``` -------------------------------- ### Retrieve SDK Version and Title Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/05-configuration-advanced.md Access the library version and title metadata using the gradient package attributes. ```python import gradient print(f"Version: {gradient.__version__}") # "3.12.2" print(f"Title: {gradient.__title__}") # "gradient" ``` -------------------------------- ### Use Gradient Client as a Context Manager Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md The client supports the context manager pattern for automatic resource handling. ```python from gradient import Gradient with Gradient(access_token="your_token") as client: response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) ``` -------------------------------- ### Configure Retry Behavior Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/02-errors.md Demonstrates setting global retry counts during client initialization and overriding retry limits for individual requests. ```python from gradient import Gradient # Default: 2 retries client = Gradient(access_token="token") # Custom retry count client_with_retries = Gradient( access_token="token", max_retries=5 ) # Per-request retry override response = client.with_options(max_retries=10).chat.completions.create( messages=[{"role": "user", "content": "Hello"}], model="llama3.3-70b-instruct" ) ``` -------------------------------- ### Build Distribution Files Source: https://github.com/digitalocean/gradient-python/blob/main/CONTRIBUTING.md Build the distributable package (source tarball and wheel file) for the library using Rye or Python's build module. ```sh rye build ``` ```sh python -m build ``` -------------------------------- ### Create KnowledgeBase method signature Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Defines the parameters for creating a new knowledge base, including optional metadata and embedding model configuration. ```python def create( self, *, name: str, description: str | Omit = omit, embedding_model: str | Omit = omit, metadata: Dict[str, str] | Omit = omit, extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> KnowledgeBaseCreateResponse ``` -------------------------------- ### List Snapshots for a GPU Droplet Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Fetches a list of snapshots for a specific GPU Droplet. ```python client.gpu_droplets.list_snapshots(droplet_id, **params) ``` -------------------------------- ### Create Firewall Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Creates a new Firewall. Requires parameters defining the firewall's configuration. ```python client.gpu_droplets.firewalls.create(**params) -> FirewallCreateResponse ``` -------------------------------- ### AsyncGradient Constructor Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md The constructor method for initializing the AsyncGradient client with optional configuration parameters. ```python async def __init__( self, *, access_token: str | None = None, model_access_key: str | None = None, agent_access_key: str | None = None, agent_endpoint: str | None = None, inference_endpoint: str | None = None, kbass_endpoint: str | None = None, base_url: str | httpx.URL | None = None, timeout: float | Timeout | None | NotGiven = not_given, max_retries: int = DEFAULT_MAX_RETRIES, default_headers: Mapping[str, str] | None = None, default_query: Mapping[str, object] | None = None, http_client: httpx.AsyncClient | None = None, _strict_response_validation: bool = False, user_agent_package: str | None = None, user_agent_version: str | None = None, ) -> None ``` -------------------------------- ### POST /v2/agents Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Create a new agent. ```APIDOC ## POST /v2/agents ### Description Create a new agent. ### Method POST ### Endpoint /v2/agents ``` -------------------------------- ### Import Volume Snapshot Types Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Import necessary types for managing volume snapshots. ```python from gradient.types.gpu_droplets.volumes import ( SnapshotCreateResponse, SnapshotRetrieveResponse, SnapshotListResponse, ) ``` -------------------------------- ### Create Volume Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Creates a new volume. ```APIDOC ## POST /v2/volumes ### Description Creates a new volume. ### Method POST ### Endpoint /v2/volumes ### Parameters #### Request Body - **params** (object) - Required - Parameters for creating a volume. ### Request Example ```json { "name": "my-volume", "size_gigabytes": 100, "description": "My new volume" } ``` ### Response #### Success Response (200) - **VolumeCreateResponse** (object) - Details of the created volume. ``` -------------------------------- ### Firewall Management Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Manage firewalls for GPU droplets using the Gradient Python client. ```APIDOC ## Firewalls ### Description Provides methods to manage firewalls for GPU droplets. ### Methods - **create**: Creates a new firewall. - **Endpoint**: `POST /v2/firewalls` - **Parameters**: Accepts various parameters for firewall configuration. - **Returns**: `FirewallCreateResponse` - **retrieve**: Retrieves details of a specific firewall. - **Endpoint**: `GET /v2/firewalls/{firewall_id}` - **Parameters**: `firewall_id` (string) - The ID of the firewall. - **Returns**: `FirewallRetrieveResponse` - **update**: Updates an existing firewall. - **Endpoint**: `PUT /v2/firewalls/{firewall_id}` - **Parameters**: `firewall_id` (string) - The ID of the firewall. Accepts various parameters for firewall configuration. - **Returns**: `FirewallUpdateResponse` - **list**: Lists all firewalls. - **Endpoint**: `GET /v2/firewalls` - **Parameters**: Accepts various parameters for filtering and pagination. - **Returns**: `FirewallListResponse` - **delete**: Deletes a firewall. - **Endpoint**: `DELETE /v2/firewalls/{firewall_id}` - **Parameters**: `firewall_id` (string) - The ID of the firewall. - **Returns**: `None` ``` -------------------------------- ### Chat Completion with Sampling Parameters Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/04-api-chat-completions.md Configures generation behavior using temperature, max_tokens, and top_p parameters. ```python response = client.chat.completions.create( messages=[ {"role": "user", "content": "Write a creative story"} ], model="llama3.3-70b-instruct", temperature=0.8, # More random max_tokens=500, top_p=0.95 # Nucleus sampling ) print(response.choices[0].message.content) ``` -------------------------------- ### Create Floating IP Action Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Initiates an action on a specific floating IP. Requires the floating IP ID and action parameters. ```python client.gpu_droplets.floating_ips.actions.create(floating_ip, **params) ``` -------------------------------- ### Create Load Balancer Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Provision a new Load Balancer. Use the provided params type to specify configuration details. ```python client.gpu_droplets.load_balancers.create(**params) -> LoadBalancerCreateResponse ``` -------------------------------- ### List KnowledgeBases method signature Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Defines the parameters for retrieving a paginated list of existing knowledge bases. ```python def list( self, *, page: int | Omit = omit, per_page: int | Omit = omit, extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = not_given, ) -> KnowledgeBaseListResponse ``` -------------------------------- ### list Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/07-api-agents-knowledge-bases.md Retrieves a paginated list of knowledge bases. ```APIDOC ## list ### Description Retrieves a paginated list of existing knowledge bases. ### Parameters - **page** (int) - Optional - Page number - **per_page** (int) - Optional - Number of items per page ### Returns - **KnowledgeBaseListResponse** - Paginated list ``` -------------------------------- ### Configure Client Timeouts Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/05-configuration-advanced.md Set global or granular timeouts for client operations using standard or httpx-specific configurations. ```python from gradient import Gradient client = Gradient( access_token="token", timeout=30.0 # 30 seconds for all operations ) ``` ```python import httpx from gradient import Gradient client = Gradient( access_token="token", timeout=httpx.Timeout( timeout=60.0, # Overall timeout connect=2.0, # Connection establishment read=10.0, # Reading response write=5.0, # Writing request pool=1.0 # Pool timeout ) ) ``` -------------------------------- ### AsyncGradient Constructor Source: https://github.com/digitalocean/gradient-python/blob/main/_autodocs/01-client-initialization.md Initializes a new instance of the AsyncGradient client for asynchronous operations. ```APIDOC ## AsyncGradient Constructor ### Description Initializes the asynchronous client for non-blocking access to the Gradient API. ### Signature ```python async def __init__( self, *, access_token: str | None = None, model_access_key: str | None = None, agent_access_key: str | None = None, agent_endpoint: str | None = None, inference_endpoint: str | None = None, kbass_endpoint: str | None = None, base_url: str | httpx.URL | None = None, timeout: float | Timeout | None | NotGiven = not_given, max_retries: int = DEFAULT_MAX_RETRIES, default_headers: Mapping[str, str] | None = None, default_query: Mapping[str, object] | None = None, http_client: httpx.AsyncClient | None = None, _strict_response_validation: bool = False, user_agent_package: str | None = None, user_agent_version: str | None = None, ) -> None ``` ### Parameters - **access_token** (str | None) - Optional - API access token. - **model_access_key** (str | None) - Optional - Access key for models. - **agent_access_key** (str | None) - Optional - Access key for agents. - **http_client** (httpx.AsyncClient | None) - Optional - Custom async HTTP client instance. ``` -------------------------------- ### List Backups for a GPU Droplet Source: https://github.com/digitalocean/gradient-python/blob/main/api.md Retrieves a list of backups for a specific GPU Droplet, with optional parameters. ```python client.gpu_droplets.backups.list(droplet_id, **params) ```