AIStudio Python SDK
The `aistudio-sdk` is a Python client library for interacting with the AIStudio API. It provides convenient access to AI models for tasks like text generation. Currently at version 0.3.8, it receives frequent updates aligning with the underlying API's evolution.
Warnings
- breaking As the library is in its 0.x.x version, breaking changes to the API surface (class names, method signatures, return types) may occur in minor releases without a full major version bump.
- gotcha The SDK primarily relies on the `AISTUDIO_API_KEY` environment variable for authentication. While a `Client` object can be instantiated with an `api_key` argument, service classes like `TextGeneration` do not directly accept `api_key` in their constructor.
- gotcha The example models provided in the documentation (e.g., 'gpt-3.5-turbo') are illustrative. The actual availability and names of models depend on the AIStudio API itself and may change. Using an unsupported model will result in an API error.
- gotcha API responses may contain detailed error messages or specific structures beyond just `response.text` for generation results. Ignoring these can lead to missed debugging opportunities.
Install
-
pip install aistudio-sdk
Imports
- TextGeneration
from aistudio import TextGeneration
- Client
from aistudio import Client
Quickstart
import os
from aistudio import TextGeneration
# Ensure the AISTUDIO_API_KEY environment variable is set.
# Example: export AISTUDIO_API_KEY="your_secret_key"
# For local testing without setting a real key (will likely fail API calls):
# os.environ['AISTUDIO_API_KEY'] = 'sk-aistudio-dummy-key'
try:
# The SDK automatically picks up AISTUDIO_API_KEY from environment variables.
gen = TextGeneration()
response = gen.create(
prompt="Hello, my name is",
model="gpt-3.5-turbo", # Replace with an available model on AIStudio
temperature=0.7,
max_tokens=20
)
print("Generated text:", response.text)
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure AISTUDIO_API_KEY is set and valid, and the model specified is available.")