AIStudio Python SDK

0.3.8 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to perform text generation using the `TextGeneration` class. The SDK automatically picks up the `AISTUDIO_API_KEY` from environment variables. Remember to replace 'gpt-3.5-turbo' with an actual model supported by the AIStudio API.

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.")

view raw JSON →