### Run Python Async Chat Streaming Example Source: https://venice-ai.readthedocs.io/en/latest/index.html/_sources/async_chat_streaming_guide.rst This command shows how to execute the provided Python script (`run_async_stream.py`) from the terminal to start the asynchronous chat streaming demonstration. ```bash python run_async_stream.py ``` -------------------------------- ### Python Example: Get Model Pricing Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_client An example demonstrating how to instantiate the `VeniceClient` with an API key and use the `get_model_pricing` method to retrieve and print the input and output token pricing in USD for a specific model like 'llama-3.3-70b'. ```python >>> client = VeniceClient(api_key="your-api-key") >>> pricing = client.get_model_pricing("llama-3.3-70b") >>> print(f"Input: ${pricing['input']['usd']}/1k tokens") >>> print(f"Output: ${pricing['output']['usd']}/1k tokens") ``` -------------------------------- ### Stream Chat Completions Asynchronously with Venice AI Python Client Source: https://venice-ai.readthedocs.io/en/latest/index.html/async_chat_streaming_guide This example demonstrates how to use the `AsyncVeniceClient` to send a prompt and stream the chat response asynchronously. It covers setting up the client, defining prompt messages, enabling streaming with `stream=True`, and iterating over `ChatCompletionChunk` objects to assemble the full response. Error handling for API and unexpected errors is also included. Prerequisites include installing the `venice-ai` package and setting the `VENICE_API_KEY` environment variable. ```python import os import asyncio from venice_ai import AsyncVeniceClient, APIError async def main(): """Demonstrates asynchronous chat streaming with Venice AI.""" try: # Ensure VENICE_API_KEY is set in your environment if not os.getenv("VENICE_API_KEY"): print("Error: VENICE_API_KEY environment variable not set.") return async with AsyncVeniceClient() as client: print("Streaming chat response from Venice AI...") print("---") prompt_messages = [ {"role": "user", "content": "Tell me a short story about a brave knight and a friendly dragon."} ] # Using a common model, replace if needed model_id = "llama-3.2-3b" stream = await client.chat.completions.create( model=model_id, messages=prompt_messages, stream=True, max_completion_tokens=150 # Optional: limit response length ) full_response = [] async for chunk in stream: # Ensure 'choices' and 'delta' are present and valid if chunk.choices and len(chunk.choices) > 0: delta = chunk.choices[0].delta if delta and delta.content: content_delta = delta.content print(content_delta, end="", flush=True) full_response.append(content_delta) print("\n---\nStream finished.") # print(f"Full assembled response: {''.join(full_response)}") except APIError as e: print(f"\nAn API Error occurred: {e.status_code} - {e.message}") if e.body: print(f"Error details: {e.body}") except Exception as e: print(f"\nAn unexpected error occurred: {e}") if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Python Example: Generate Image with Venice AI Client Source: https://venice-ai.readthedocs.io/en/latest/index.html/api An asynchronous Python code example demonstrating how to use the `venice_ai` client to generate an image. It shows setting the model, prompt, width, height, and steps for the image generation request. ```Python # client = AsyncVeniceClient() response = await client.image.generate( model="venice-sd35", prompt="A serene landscape with mountains and a lake", width=1024, height=768, steps=30 ) # Process response.images[0] (base64 string) ``` -------------------------------- ### Stream Chat Completions Asynchronously with Python Source: https://venice-ai.readthedocs.io/en/latest/index.html/_sources/async_chat_streaming_guide.rst This Python example demonstrates how to use `AsyncVeniceClient` to send a chat prompt and stream the AI's response. It includes setup for environment variables, error handling for API and unexpected issues, and processes `ChatCompletionChunk` objects as they arrive. ```python import os import asyncio from venice_ai import AsyncVeniceClient, APIError async def main(): """Demonstrates asynchronous chat streaming with Venice AI.""" try: # Ensure VENICE_API_KEY is set in your environment if not os.getenv("VENICE_API_KEY"): print("Error: VENICE_API_KEY environment variable not set.") return async with AsyncVeniceClient() as client: print("Streaming chat response from Venice AI...") print("---") prompt_messages = [ {"role": "user", "content": "Tell me a short story about a brave knight and a friendly dragon."} ] # Using a common model, replace if needed model_id = "llama-3.2-3b" stream = await client.chat.completions.create( model=model_id, messages=prompt_messages, stream=True, max_completion_tokens=150 # Optional: limit response length ) full_response = [] async for chunk in stream: # Ensure 'choices' and 'delta' are present and valid if chunk.choices and len(chunk.choices) > 0: delta = chunk.choices[0].delta if delta and delta.content: content_delta = delta.content print(content_delta, end="", flush=True) full_response.append(content_delta) print("\n---\nStream finished.") # print(f"Full assembled response: {''.join(full_response)}") except APIError as e: print(f"\nAn API Error occurred: {e.status_code} - {e.message}") if e.body: print(f"Error details: {e.body}") except Exception as e: print(f"\nAn unexpected error occurred: {e}") if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Python Example: Generate Image with Venice AI Client Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/image Demonstrates how to use the Venice AI Python client to generate an image. This example shows how to call the `client.image.generate` method with essential parameters like model, prompt, width, height, and steps. ```python # client = VeniceClient() response = client.image.generate( model="venice-sd35", prompt="A serene landscape with mountains and a lake", width=1024, height=768, steps=30 ) # Process response.images[0] (base64 string) ``` -------------------------------- ### Python: Listing API Keys with Pagination Example Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/api_keys This Python example illustrates how to use the `ApiKeys.list` method to retrieve API keys. It demonstrates fetching all keys and applying pagination by specifying `page` and `limit` parameters to retrieve a subset of keys. ```python # List all API keys all_keys = client.api_keys.list() # List with pagination page_keys = client.api_keys.list(page=1, limit=5) for key in page_keys: print(f"Key ID: {key.id}, Description: {key.description}") ``` -------------------------------- ### Run Async Model Utilities Example Source: https://venice-ai.readthedocs.io/en/latest/index.html/client_utilities Demonstrates how to execute an asynchronous utility function, `example_model_utilities`, using Python's `asyncio.run`. ```python asyncio.run(example_model_utilities()) ``` -------------------------------- ### AsyncVeniceClient __aenter__ Method Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_async_client Defines the entry point for the asynchronous context manager, returning the client instance for `async with` statements. No additional setup is required as the client is already initialized. ```APIDOC AsyncVeniceClient: __aenter__(self) -> "AsyncVeniceClient" Description: Enter the asynchronous context manager, enabling use with 'async with' statements. This method is called automatically when entering an ``async with`` statement and simply returns the client instance itself. The client is already fully initialized and ready for use at this point, so no additional setup is required. Returns: type: AsyncVeniceClient description: This client instance, ready for making API requests. ``` -------------------------------- ### Example: Get Model Pricing with AsyncVeniceClient Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Demonstrates how to asynchronously retrieve model pricing using the `get_model_pricing` method of the `AsyncVeniceClient`. ```Python async with AsyncVeniceClient(api_key="your-api-key") as client: pricing = await client.get_model_pricing("llama-3.3-70b") print(f"Input: ${pricing['input']['usd']}/1k tokens") print(f"Output: ${pricing['output']['usd']}/1k tokens") ``` -------------------------------- ### List API Keys with Python Source: https://venice-ai.readthedocs.io/en/latest/index.html/api This Python example demonstrates how to list API keys for the authenticated account. It includes examples for fetching all keys and using pagination to retrieve a specific page of keys. ```Python # List all API keys all_keys = client.api_keys.list() # List with pagination page_keys = client.api_keys.list(page=1, limit=5) for key in page_keys: print(f"Key ID: {key.id}, Description: {key.description}") ``` -------------------------------- ### Python: Initialize Async Venice Client and List Models Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Illustrates the basic setup of an `AsyncVeniceClient` and how to asynchronously list all available models and print their names and IDs. ```python from venice_ai import AsyncVeniceClient async def main(): client = AsyncVeniceClient() models = await client.models.list() for model in models.data: print(f"Model: {model.name} (ID: {model.id})") ``` -------------------------------- ### Python Example: Asynchronous API Key Management Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/api_keys Illustrates the asynchronous management of API keys, including listing existing keys and creating a new one. This example uses `AsyncVeniceClient` and demonstrates awaiting API calls. ```python import asyncio from venice_ai import AsyncVeniceClient from venice_ai.types.api_keys import ApiKeyCreateRequest async def manage_api_keys(): client = AsyncVeniceClient() # List existing API keys keys = await client.api_keys.list(limit=10) for key in keys: print(f"Key ID: {key.id}, Description: {key.description}") # Create a new API key create_request = ApiKeyCreateRequest( description="My Async Test Key", apiKeyType="INFERENCE" ) new_key = await client.api_keys.create(api_key_request=create_request) print(f"Created key: {new_key.apiKey}") # Only shown on creation asyncio.run(manage_api_keys()) ``` -------------------------------- ### Python Example: Chat Completion with Tool/Function Calling Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/chat/completions Shows how to integrate tool/function calling with `venice_ai.VeniceClient` chat completions. This example defines a `get_weather` function with parameters for location and unit, enabling the model to invoke external tools based on user queries. ```python # Using tools/function calling response = client.chat.completions.create( model="llama-3.3-70b", messages=[{"role": "user", "content": "What's the weather in New York?"}], tools=[{ "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a location", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "City name"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["location"] } } }] ) ``` -------------------------------- ### Python Example for Generating Images with Venice AI Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Demonstrates how to use the `client.image.generate` method to create an image with specified model, prompt, dimensions, and steps. This example shows basic usage for asynchronous image generation. ```python # client = AsyncVeniceClient() response = await client.image.generate( model="venice-sd35", prompt="A serene landscape with mountains and a lake", width=1024, height=768, steps=30 ) # Process response.images[0] (base64 string) ``` -------------------------------- ### Python: Create Basic and Advanced Venice AI API Keys Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Provides examples for creating new API keys using `ApiKeyCreateRequest`. The first example shows a basic key creation, while the second demonstrates setting an expiration date and a consumption limit for a more advanced key. ```python from venice_ai.types.api_keys import ApiKeyCreateRequest # Create a basic API key create_request = ApiKeyCreateRequest( description="My Test Key", apiKeyType="INFERENCE" ) new_key = client.api_keys.create(api_key_request=create_request) print(f"Created key ID: {new_key.id}") print(f"API Key: {new_key.apiKey}") # Store this securely! # Create a key with expiration and limits advanced_request = ApiKeyCreateRequest( description="Limited Production Key", apiKeyType="INFERENCE", expiresAt="2024-12-31T23:59:59Z", consumptionLimit=10000 ) ``` -------------------------------- ### Python: Example for Creating an API Key Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/api_keys This Python example demonstrates how to use the `create` method to asynchronously generate a new API key. It shows how to construct the `ApiKeyCreateRequest` and handle the returned `ApiKey` object, emphasizing the need to securely store the `apiKey` value. ```python from venice_ai.types.api_keys import ApiKeyCreateRequest # Create a basic API key asynchronously create_request = ApiKeyCreateRequest( description="My Async Test Key", apiKeyType="INFERENCE" ) new_key = await client.api_keys.create(api_key_request=create_request) print(f"Created key ID: {new_key.id}") print(f"API Key: {new_key.apiKey}") # Store this securely! ``` -------------------------------- ### Example: Generate Image with Venice AI simple_generate Method Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/image Demonstrates how to use the `simple_generate` method of the Venice AI client to generate an image with specified model, prompt, size, and style. This example shows a typical API call and hints at how to process the response. ```python # client = VeniceClient() response = client.image.simple_generate( model="venice-sd35", prompt="A cute cat sitting on a windowsill", size="1024x1024", style="natural" ) # Process response.data[0] (ImageDataItem) ``` -------------------------------- ### Python Example: List API Keys Asynchronously Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Demonstrates how to asynchronously list all API keys and how to use pagination to retrieve a specific page of keys with a defined limit using the `client.api_keys.list()` method. ```Python # List all API keys asynchronously all_keys = await client.api_keys.list() # List with pagination page_keys = await client.api_keys.list(page=1, limit=5) for key in page_keys: print(f"Key ID: {key.id}, Description: {key.description}") ``` -------------------------------- ### Python Example: Creating Venice AI API Keys Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/api_keys Illustrates how to create API keys using the `venice_ai` client library in Python. It shows examples for creating a basic inference key and an advanced key with expiration and consumption limits. ```python from venice_ai.types.api_keys import ApiKeyCreateRequest # Create a basic API key create_request = ApiKeyCreateRequest( description="My Test Key", apiKeyType="INFERENCE" ) new_key = client.api_keys.create(api_key_request=create_request) print(f"Created key ID: {new_key.id}") print(f"API Key: {new_key.apiKey}") # Store this securely! # Create a key with expiration and limits advanced_request = ApiKeyCreateRequest( description="Limited Production Key", apiKeyType="INFERENCE", expiresAt="2024-12-31T23:59:59Z", consumptionLimit=10000 ) limited_key = client.api_keys.create(api_key_request=advanced_request) ``` -------------------------------- ### Python: ApiKeys.create Method Signature Start Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/api_keys This snippet provides the initial lines of the `create` method definition within the `ApiKeys` class. It indicates the beginning of the method responsible for creating new API keys, showing its self-parameter and the start of keyword-only arguments. ```python def create( self, *, ``` -------------------------------- ### Python Examples for Venice AI Chat Completions Create Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_async_client Demonstrates how to use the `venice_ai.chat.completions.create` method in Python for both non-streaming and streaming chat completion scenarios. ```python # Non-streaming usage async with AsyncVeniceClient(api_key="your-api-key") as client: response = await client.chat.completions.create( model="venice-1", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me a joke."} ] ) print(response["choices"][0]["message"]["content"]) ``` ```python # Streaming usage async with AsyncVeniceClient(api_key="your-api-key") as client: async for chunk in client.chat.completions.create( model="venice-1", messages=[{"role": "user", "content": "Tell me a joke."}], stream=True ): content = chunk["choices"][0]["delta"].get("content", "") if content: print(content, end="", flush=True) ``` -------------------------------- ### APIDOC: BaseClient Class Definition Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_client Documentation for the `BaseClient` class, which serves as the foundational class for both synchronous and asynchronous Venice AI clients, providing common initialization, retry, and transport setup. ```APIDOC class BaseClient: description: Base client class providing common functionality for both sync and async Venice AI clients. This class contains shared initialization logic, retry configuration, and transport setup that is used by both VeniceClient and AsyncVeniceClient. ``` -------------------------------- ### Python Example: Asynchronous Image Generation with `simple_generate` Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/image Demonstrates how to use the `simple_generate` method of the `AsyncVeniceClient` to generate an image asynchronously, specifying the model, prompt, size, and style. ```python # client = AsyncVeniceClient() response = await client.image.simple_generate( model="venice-sd35", prompt="A cute cat sitting on a windowsill", size="1024x1024", style="natural" ) # Process response.data[0] (ImageDataItem) ``` -------------------------------- ### Run Asynchronous Chat Streaming Script Source: https://venice-ai.readthedocs.io/en/latest/index.html/async_chat_streaming_guide Execute the Python script from the terminal to demonstrate asynchronous chat completion streaming, displaying AI-generated content word by word. ```shell python run_async_stream.py ``` -------------------------------- ### Python Example for Retrieving Available Image Styles Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Illustrates how to use the `client.image.get_available_styles` method to fetch and print the names of available image generation styles from the Venice AI API asynchronously. ```python # client = AsyncVeniceClient() styles_response = await client.image.get_available_styles() for style_name in styles_response.data: print(f"Available style: {style_name}") ``` -------------------------------- ### Estimate Token Count with Venice AI Utilities Source: https://venice-ai.readthedocs.io/en/latest/index.html/client_utilities This example shows how to use the `estimate_token_count` function from `venice_ai.utils` to get an approximate token count for a given text string. An optional `tiktoken` dependency can be installed for more accurate tokenization. ```python from venice_ai.utils import estimate_token_count # Count tokens in various text chunks text = "This is a sample text to count tokens in." token_count = estimate_token_count(text) print(f"Estimated token count: {token_count}") ``` -------------------------------- ### Python Venice AI Client Internal Initialization and Resource Setup Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_client This code demonstrates the internal logic for initializing the Venice AI client, handling both cases where an external `httpx.Client` is provided or an internal one is created. It shows how base URL, timeout, and authorization headers are applied, and how various API resources (chat, models, image, audio) are instantiated and attached to the client. ```Python super().__init__( api_key=api_key, base_url=base_url, timeout=timeout, default_timeout=default_timeout, http_client=http_client, # Pass HTTP transport options http_transport_options=http_transport_options, # Pass httpx client settings proxy=proxy, transport=transport, limits=limits, cert=cert, verify=verify, trust_env=trust_env, http1=http1, http2=http2, follow_redirects=follow_redirects, max_redirects=max_redirects, default_encoding=default_encoding, event_hooks=event_hooks, ) self._is_closed = False # Initialize for idempotency # Initialize the httpx client if http_client is not None: self._client = http_client self._should_close_session = False # Don't close user-provided client # Apply SDK-level settings to the user-provided client # Update base_url to ensure SDK's base URL is used self._client.base_url = self._base_url # Update timeout to ensure SDK's timeout is used self._client.timeout = self._timeout # Ensure the Authorization header is set on external clients self._client.headers["Authorization"] = f"Bearer {self._api_key}" else: self._should_close_session = True # We created it, so we should close it # Use BaseClient's _build_raw_client method which includes retry logic self._client = self._build_raw_client() # Apply SDK-specific headers self._client.headers.update({ "Accept": "application/json", "Authorization": f"Bearer {self._api_key}", }) # Initialize resource namespaces self.chat = ChatResource(self) # Pass client instance to resource self.models = Models(self) # Initialize the Models resource self.image = Image(self) # Initialize the Image resource self.audio = Audio(self) # Initialize the Audio resource ``` -------------------------------- ### Python Example: Get Rate Limit Logs Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/api_keys Demonstrates how to retrieve and iterate through rate limit logs for a specific date range using the Venice AI client. It shows how to access event type and timestamp from each log entry. ```python print(f"Event: {log_entry.event_type} at {log_entry.timestamp}") # Get logs for a specific date range logs = client.api_keys.get_rate_limit_logs( start_date="2024-01-01T00:00:00Z", end_date="2024-01-31T23:59:59Z", limit=50 ) ``` -------------------------------- ### Get Venice AI Billing Usage Data (JSON and CSV) with Python Source: https://venice-ai.readthedocs.io/en/latest/index.html/api This Python example demonstrates how to initialize the Venice AI client, retrieve billing usage data in JSON format, iterate through records, and fetch data in CSV format, then write it to a file. It showcases both JSON parsing and binary file writing for CSV. ```Python from venice_ai import VeniceClient from venice_ai.types.billing import BillingFormatEnum client = VeniceClient(api_key="your-api-key") # Get JSON usage data usage_response = client.billing.get_usage( startDate="2025-01-01T00:00:00Z", endDate="2025-05-01T00:00:00Z", limit=10, page=1 ) # Access usage records for usage_record in usage_response['data']: print(f"Date: {usage_record['timestamp']}, Cost: {usage_record['amount']}") # Get CSV usage data usage_csv = client.billing.get_usage( format=BillingFormatEnum.CSV, startDate="2025-01-01T00:00:00Z", endDate="2025-05-01T00:00:00Z" ) # Write CSV to file with open("usage.csv", "wb") as f: f.write(usage_csv) ``` -------------------------------- ### Configure Synchronous VeniceClient with httpx Settings Source: https://venice-ai.readthedocs.io/en/latest/index.html/_sources/api.rst This example demonstrates how to initialize the `VeniceClient` by directly passing `httpx` configuration parameters such as proxies, custom transport, connection limits, and SSL verification settings. The SDK manages the lifecycle of the internal `httpx.Client`. ```python from venice_ai import VeniceClient import httpx # For httpx.Limits and httpx.HTTPTransport # Pass httpx settings directly to VeniceClient constructor # The SDK will create and manage its internal httpx.Client with these settings client = VeniceClient( api_key="YOUR_API_KEY", proxies={"all://": "http://localhost:8080"}, # Example proxy transport=httpx.HTTPTransport(retries=3), # Example custom transport limits=httpx.Limits(max_connections=100, max_keepalive_connections=20), # Example limits verify=False # Example: disable SSL verification (use with caution) ) # Use the client as usual (SDK manages httpx.Client lifecycle) with client: # Or client.close() when done models = client.models.list() print(models) ``` -------------------------------- ### API Reference: Asynchronous GET Request Method Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Documents the `get` method for making asynchronous GET requests, detailing its parameters, return types, and potential exceptions. This method wraps the lower-level `_request` method, handling header configuration for GET requests. ```APIDOC async get(path: str, *, params: Mapping[str, Any] | None = None, cast_to: Type[venice_ai._async_client.T] | None = None, **kwargs) Description: Make an asynchronous GET request to the specified API endpoint. This is a convenience method that wraps the lower-level `_request` method specifically for GET requests. It automatically handles proper header configuration for GET requests (removing Content-Type headers) and provides a clean interface for retrieving data from the API. Parameters: path: str - API endpoint path relative to the client’s base URL. Should not include a leading slash as it will be properly joined with the base URL. params: Optional[Mapping[str, Any]] - URL query parameters to include in the request. These will be properly URL-encoded and appended to the request URL. cast_to: Optional[Type[T]] - Optional Pydantic model to cast the response to. kwargs: - Additional keyword arguments to pass to the underlying `_request` method. This can include options like `headers`, `timeout`, or `raw_response`. Returns: Any - Parsed JSON response data as Python objects (typically dict or list). Raises: venice_ai.exceptions.InvalidRequestError - For HTTP 400 errors indicating invalid request parameters. venice_ai.exceptions.AuthenticationError - For HTTP 401 errors indicating invalid or missing API key. venice_ai.exceptions.PermissionDeniedError - For HTTP 403 errors indicating insufficient permissions. venice_ai.exceptions.NotFoundError - For HTTP 404 errors indicating the requested resource was not found. venice_ai.exceptions.RateLimitError - For HTTP 429 errors indicating rate limit exceeded. venice_ai.exceptions.InternalServerError - For HTTP 5xx errors indicating server-side problems. venice_ai.exceptions.APITimeoutError - If the request times out before completion. ``` -------------------------------- ### Define Asynchronous GET Request Convenience Method in Python Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_async_client This Python method provides a high-level interface for making asynchronous GET requests. It wraps a lower-level request mechanism, automatically handling necessary header configurations for GET operations, simplifying data retrieval from an API endpoint. ```Python async def get(self, path: str, *, params: Optional[Mapping[str, Any]] = None, cast_to: Optional[Type[T]] = None, **kwargs) -> Any: """ Make an asynchronous GET request to the specified API endpoint. This is a convenience method that wraps the lower-level ``_request`` method specifically for GET requests. It automatically handles proper header configuration for GET requests (removing Content-Type headers) and provides a clean interface for retrieving data from the API. """ ``` ```APIDOC Method: get Description: Make an asynchronous GET request to the specified API endpoint. This is a convenience method that wraps the lower-level _request method specifically for GET requests. It automatically handles proper header configuration for GET requests (removing Content-Type headers) and provides a clean interface for retrieving data from the API. Parameters: path (str): The API endpoint path. params (Optional[Mapping[str, Any]]): Optional dictionary of query parameters. cast_to (Optional[Type[T]]): Optional type to cast the response to. **kwargs: Additional keyword arguments passed to the underlying request. Returns: Any (The parsed API response, potentially cast to a specific type.) ``` -------------------------------- ### Venice AI Client Initialization and Internal HTTP Client API (APIDOC) Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_client Detailed API documentation for the `__init__` method of the Venice AI Python client, outlining all configurable parameters for client setup, including API key, base URL, timeouts, and various HTTP client options. Also includes documentation for the internal `_build_raw_client` method. ```APIDOC VeniceAIClient.__init__( api_key: Union[str, None], base_url: Union[str, httpx.URL, None] = None, timeout: Union[float, Timeout, None] = None, default_timeout: Union[float, Timeout, None] = None, http_client: Union[httpx.Client, None] = None, http_transport_options: Union[Mapping[str, Any], None] = None, proxy: Union[str, httpx.Proxy, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, transport: Union[httpx.BaseTransport, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, async_transport: Union[httpx.AsyncBaseTransport, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, limits: Union[httpx.Limits, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, cert: Union[httpx._types.CertTypes, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, verify: Union[bool, str, ssl.SSLContext, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, trust_env: Union[bool, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, http1: Union[bool, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, http2: Union[bool, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, follow_redirects: Union[bool, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, max_redirects: Union[int, venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, default_encoding: Union[str, Callable[[bytes], str], venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN, event_hooks: Union[Mapping[str, List[Callable[..., Any]]], venice_ai.utils.NotGiven] = venice_ai.utils.NOT_GIVEN ) api_key: The API key for authentication. Can be set via parameter or "VENICE_API_KEY" environment variable. Required. base_url: The base URL for the API. Defaults to _constants.DEFAULT_BASE_URL. timeout: A timeout value in seconds (float) or an httpx.Timeout object. default_timeout: Overrides 'timeout' if provided. http_client: An optional pre-configured httpx.Client instance. If provided, other HTTP options are ignored. http_transport_options: A dictionary of options passed to the underlying httpx transport. proxy: A proxy URL string or an `httpx.Proxy` instance. Used if `http_client` is not provided. transport: A custom synchronous HTTPX transport instance (e.g., `httpx.HTTPTransport`). Used if `http_client` is not provided. async_transport: A custom asynchronous HTTPX transport instance (e.g., `httpx.AsyncHTTPTransport`). Used if `http_client` is not provided. limits: Configuration for connection limits (e.g., `httpx.Limits(max_connections=100)`). Used if `http_client` is not provided. cert: SSL certificate configuration. Can be a path to a PEM file or a 2-tuple of (cert, key) paths. Used if `http_client` is not provided. verify: SSL verification setting. Can be a boolean, a path to a CA bundle, or an `ssl.SSLContext`. Defaults to `True`. Used if `http_client` is not provided. trust_env: If `True`, trusts environment variables for proxy configuration, SSL certificates, etc. Defaults to `True`. Used if `http_client` is not provided. http1: If `True`, enables HTTP/1.1 support. Defaults to `True`. Used if `http_client` is not provided. http2: If `True`, enables HTTP/2 support. Defaults to `False` (httpx default). Used if `http_client` is not provided. follow_redirects: If `True`, automatically follows redirects. Defaults to `False` for the SDK client. Used if `http_client` is not provided. max_redirects: Maximum number of redirects to follow if `follow_redirects` is `True`. Used if `http_client` is not provided. default_encoding: Default encoding for response text. Can be a string or a callable. Used if `http_client` is not provided. event_hooks: Event hooks for the request/response lifecycle (e.g., `{"request": [log_request]}`). Used if `http_client` is not provided. VeniceAIClient._build_raw_client() -> httpx.Client Description: Builds and configures the synchronous httpx client without retry transport. ``` -------------------------------- ### AsyncVeniceClient: Make asynchronous GET request Source: https://venice-ai.readthedocs.io/en/latest/index.html/api This method makes an asynchronous GET request to the specified API endpoint. It wraps the lower-level `_request` method, automatically handling proper header configuration for GET requests (removing Content-Type headers) and providing a clean interface for retrieving data from the API. ```APIDOC AsyncVeniceClient.get( path: str, params: Mapping[str, Any] | None = None, cast_to: Type[T] | None = None, **kwargs ) Parameters: path (str): API endpoint path relative to the client’s base URL. Should not include a leading slash as it will be properly joined with the base URL. params (Optional[Mapping[str, Any]]): URL query parameters to include in the request. These will be properly URL-encoded and appended to the request URL. ``` -------------------------------- ### Delete API Key Examples (Python) Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Provides Python code examples for deleting an API key, including a direct deletion and a safer pattern for deleting test keys after listing them. ```python # Delete an API key (use with caution) result = client.api_keys.delete(api_key_id="key_123456789") print(f"Deletion result: {result}") # Safe deletion pattern keys = client.api_keys.list() test_keys = [k for k in keys if "test" in k.description.lower()] for test_key in test_keys: client.api_keys.delete(api_key_id=test_key.id) print(f"Deleted test key: {test_key.id}") ``` -------------------------------- ### Python: List and Create Venice AI API Keys Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Illustrates how to initialize the `VeniceClient` and use the `api_keys` resource to list existing API keys and create a new one. It shows how to access key details and the newly generated API key. ```python from venice_ai import VeniceClient from venice_ai.types.api_keys import ApiKeyCreateRequest client = VeniceClient() # List existing API keys keys = client.api_keys.list(limit=10) for key in keys: print(f"Key ID: {key.id}, Description: {key.description}") # Create a new API key create_request = ApiKeyCreateRequest( description="My Test Key", apiKeyType="INFERENCE" ) new_key = client.api_keys.create(api_key_request=create_request) print(f"Created key: {new_key.apiKey}") # Only shown on creation ``` -------------------------------- ### API Reference for Venice AI Chat Completions Create Method Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_async_client Detailed documentation for the `venice_ai.chat.completions.create` method, outlining its parameters, return types, and potential exceptions. ```APIDOC Parameters: presence_penalty: Optional[float] - Penalty for new tokens based on whether they appear in the text so far. response_format: Optional[venice_ai.types.chat.ResponseFormat] - Specifies the response format, e.g., for JSON mode. seed: Optional[int] - Random seed for reproducibility. stop: Optional[Union[str, Sequence[str]]] - Up to 4 sequences where the API will stop generation. temperature: Optional[float] - Sampling temperature to control randomness. Lower values make output more deterministic. top_p: Optional[float] - Nucleus sampling probability mass to control diversity. tools: Optional[Sequence[venice_ai.types.chat.Tool]] - A list of tools the model may call. tool_choice: Optional[Union[Literal["none", "auto"], venice_ai.types.chat.ToolChoiceObject]] - Controls which tool is called by the model. user: Optional[str] - Identifier for the end-user (currently discarded by the API). venice_parameters: Optional[venice_ai.types.chat.VeniceParameters] - Venice-specific parameters. logprobs: Optional[bool] - Whether to return log probabilities. top_logprobs: Optional[int] - Number of top log probabilities to return. parallel_tool_calls: Optional[bool] - Enable parallel function calling. repetition_penalty: Optional[float] - Penalty for repetition. stop_token_ids: Optional[Sequence[int]] - Array of token IDs to stop generation. top_k: Optional[int] - Number of highest probability tokens to keep. stream_options: Optional[venice_ai.types.chat.StreamOptions] - Options for controlling the streaming response. kwargs: Additional keyword arguments to pass to the API. Returns: Union[venice_ai.types.chat.ChatCompletion, AsyncIterator[venice_ai.types.chat.ChatCompletionChunk]] - A ChatCompletion object if stream is False, otherwise an AsyncIterator of ChatCompletionChunk objects. Raises: venice_ai.exceptions.InvalidRequestError: If parameters are invalid. venice_ai.exceptions.AuthenticationError: If the API key is invalid. venice_ai.exceptions.PermissionDeniedError: If access is denied. venice_ai.exceptions.NotFoundError: If the model or resource is not found. venice_ai.exceptions.RateLimitError: If rate limits are exceeded. venice_ai.exceptions.APIError: For other API-related errors. ``` -------------------------------- ### Make GET Request to Venice AI API Source: https://venice-ai.readthedocs.io/en/latest/index.html/api This method sends a GET request to a specified API endpoint relative to the base URL. It automatically handles header configuration appropriate for GET requests. Parameters include the API path, optional URL query parameters, an optional Pydantic model for response casting, and additional keyword arguments. It returns the parsed JSON response body or raises an APIError on failure. ```APIDOC Method: get Signature: get(*path: str, **, params: Mapping[str, Any] | None = None, cast_to: Type[venice_ai._client.T] | None = None, **kwargs) Description: Make a GET request to the specified API endpoint. This is a convenience method for making GET requests. It automatically handles header configuration appropriate for GET requests. Parameters: path (str): API endpoint path relative to the base URL. params (Optional[Mapping[str, Any]]): URL query parameters to include in the request. cast_to (Optional[Type[T]]): Optional Pydantic model to cast the response to. kwargs: Additional arguments to pass to _request(). Returns: Any: Parsed JSON response body. Raises: venice_ai.exceptions.APIError: If the request fails. ``` -------------------------------- ### APIDOC: BaseClient.__init__ Method Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/_client Detailed API documentation for the `__init__` method of the `BaseClient` class, explaining its parameters for configuring API key, base URL, timeouts, HTTP client, and various transport options. ```APIDOC BaseClient.__init__( api_key: Optional[str] = None, base_url: Optional[Union[str, httpx.URL]] = None, timeout: Union[float, httpx.Timeout, None] = _constants.DEFAULT_TIMEOUT, default_timeout: Optional[httpx.Timeout] = None, http_client: Optional[Union[httpx.Client, httpx.AsyncClient]] = None, http_transport_options: Optional[Dict[str, Any]] = None, proxy: Union[ProxyTypes, NotGiven] = NOT_GIVEN, transport: Union[httpx.BaseTransport, NotGiven] = NOT_GIVEN, async_transport: Union[httpx.AsyncBaseTransport, NotGiven] = NOT_GIVEN, limits: Union[httpx.Limits, NotGiven] = NOT_GIVEN, cert: Union[CertTypes, NotGiven] = NOT_GIVEN, verify: Union[bool, str, ssl.SSLContext, NotGiven] = NOT_GIVEN, trust_env: Union[bool, NotGiven] = NOT_GIVEN, http1: Union[bool, NotGiven] = NOT_GIVEN, http2: Union[bool, NotGiven] = NOT_GIVEN, follow_redirects: Union[bool, NotGiven] = NOT_GIVEN, max_redirects: Union[int, NotGiven] = NOT_GIVEN, default_encoding: Union[str, Callable[[bytes], str], NotGiven] = NOT_GIVEN, event_hooks: Union[Mapping[str, List[Callable[..., Any]]], NotGiven] = NOT_GIVEN, ) -> None description: Initialize the BaseClient with common configuration. This constructor sets up the foundational configuration shared by both VeniceClient and AsyncVeniceClient, including authentication, base URL, timeout settings, and HTTP client configuration options. parameters: api_key: type: Optional[str] description: The API key for authenticating requests. If not provided, it attempts to read from the `VENICE_API_KEY` environment variable. base_url: type: Optional[Union[str, httpx.URL]] description: The base URL for the API. Defaults to `_constants.DEFAULT_BASE_URL` if not provided. timeout: type: Union[float, httpx.Timeout, None] description: The default timeout for requests. Can be a float (seconds) or an `httpx.Timeout` object. This is superseded by `default_timeout` if that is provided. default_timeout: type: Optional[httpx.Timeout] description: A more specific global default timeout. If provided, this takes precedence over the `timeout` parameter. http_client: type: Optional[Union[httpx.Client, httpx.AsyncClient]] description: An optional, pre-configured `httpx.Client` or `httpx.AsyncClient` instance. If provided, the SDK will attempt to use it. Note: Lifecycle management of a user-provided client is typically handled by the derived SDK clients (`VeniceClient`, `AsyncVeniceClient`). http_transport_options: type: Optional[Dict[str, Any]] description: Dictionary of options to pass to the underlying `httpx.HTTPTransport` or `httpx.AsyncHTTPTransport` if a custom `transport` or `async_transport` is not provided. These options are used when the SDK creates its internal transport. Example: `{\"retries\": 3}`. proxy: type: Union[httpx._types.ProxyTypes, venice_ai.utils.NotGiven] description: Proxy configuration for HTTP requests. Can be a URL string, a dictionary mapping schemes to proxy URLs. ``` -------------------------------- ### Python: Example for Asynchronously Retrieving Billing Usage Data Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/billing This Python example demonstrates how to asynchronously retrieve billing usage data using the `AsyncVeniceClient` and its `billing.get_usage` method. It illustrates the process of initializing the client with an API key, specifying filtering parameters like `startDate`, `endDate`, `limit`, and `page`, and then iterating through the `data` field of the JSON response. The example highlights the asynchronous pattern for non-blocking API interactions. ```python import asyncio from venice_ai import AsyncVeniceClient from venice_ai.types.billing import BillingFormatEnum async def get_usage_example(): async with AsyncVeniceClient(api_key="your-api-key") as client: # Get JSON usage data usage_response = await client.billing.get_usage( startDate="2025-01-01T00:00:00Z", endDate="2025-05-01T00:00:00Z", limit=10, page=1 ) # Access usage records for usage_record in usage_response['data']: ``` -------------------------------- ### Configure Synchronous VeniceClient with Direct httpx Settings in Python Source: https://venice-ai.readthedocs.io/en/latest/index.html/api This example demonstrates how to initialize a `VeniceClient` instance by directly passing `httpx` client configuration parameters like proxies, custom transport with retries, connection limits, and SSL verification. The SDK automatically creates and manages the internal `httpx.Client` with these settings, handling its lifecycle. The client is then used to list available models, showcasing the applied configurations. ```python from venice_ai import VeniceClient import httpx # For httpx.Limits and httpx.HTTPTransport # Pass httpx settings directly to VeniceClient constructor # The SDK will create and manage its internal httpx.Client with these settings client = VeniceClient( api_key="YOUR_API_KEY", proxies={"all://": "http://localhost:8080"}, # Example proxy transport=httpx.HTTPTransport(retries=3), # Example custom transport limits=httpx.Limits(max_connections=100, max_keepalive_connections=20), # Example limits verify=False # Example: disable SSL verification (use with caution) ) # Use the client as usual (SDK manages httpx.Client lifecycle) with client: # Or client.close() when done models = client.models.list() print(models) ``` -------------------------------- ### Initialize Venice AI Client and Create Chat Completion Source: https://venice-ai.readthedocs.io/en/latest/index.html/_modules/venice_ai/resources/chat/completions Demonstrates how to initialize the `VeniceClient` and use the `chat.completions.create` method to send a chat message to a Venice AI model and print the response. This example shows a basic non-streaming chat interaction. ```python from venice_ai import VeniceClient # Initialize the client client = VeniceClient(api_key="your-api-key") # Create a chat completion response = client.chat.completions.create( model="venice-1", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me about Venice AI."} ] ) # Access the response content print(response["choices"][0]["message"]["content"]) ``` -------------------------------- ### Python Sync Chat Completion Example Source: https://venice-ai.readthedocs.io/en/latest/index.html/api Demonstrates a synchronous chat completion using `VeniceClient` in Python. The example covers client initialization, sending messages to a specified model, and accessing the generated content from the response. ```Python from venice_ai import VeniceClient # Initialize the client client = VeniceClient(api_key="your-api-key") # Create a chat completion response = client.chat.completions.create( model="venice-1", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me about Venice AI."} ] ) # Access the response content print(response["choices"][0]["message"]["content"]) ```