Track the environmental impact of your use of AI
Project description
Scope3AI Python SDK
Track the environmental impact of your use of AI ! The Scope3AI Python SDK provides an easy-to-use interface for interacting with Scope3AI's API. This library enables users to record, trace, and analyze the impact of interactions with a focus on sustainability metrics. Below are the different ways to use the SDK.
Installation
To install the SDK, use pip:
pip install scope3ai
Library and SDK support Matrix
| Library/SDK | Text generation | TTS | STT | Image Generation | Translation |
|---|---|---|---|---|---|
| Anthropic | ✅ | ||||
| Cohere | ✅ | ||||
| OpenAI | ✅ | ||||
| Huggingface | ✅ | ✅ | ✅ | ✅ | ✅ |
| LiteLLM | ✅ |
Roadmap:
- Langchain
Getting Started
Initializing the SDK
The SDK can be initialized with your API key and custom configurations.
from scope3ai import Scope3AI
scope3 = Scope3AI.init(
api_key="YOUR_API_KEY", # Replace "YOUR_API_KEY" with your actual key
api_url="https://api.scope3.ai/v1", # Optional: Specify the API URL
enable_debug_logging=False, # Enable debug logging (default: False)
sync_mode=False, # Enable synchronous mode when sending telemetry to the API (default: False)
)
Environment variables
You can also use environment variable to setup the SDK:
SCOPE3AI_API_KEY: Your Scope3AI API keySCOPE3AI_API_URL: The API endpoint URL. Default:https://api.scope3.ai/v1SCOPE3AI_SYNC_MODE: IfTrue, every interaction will be send synchronously to the API, otherwise it will use a background worker. Default:False
from scope3ai import Scope3AI
scope3 = Scope3AI.init()
Usage Examples
1. Using Context Management for Tracing
Within the context of a trace, all interactions are recorded and you can query the impact of the trace.
As the interactions are captured and send to Scope3 AI for analysis, the impact is calculated and returned asynchronously.
This will automatically wait for all traces to be processed and return the impact.
with scope3.trace() as tracer:
# Perform your interactions
interact()
interact()
# Print the impact of the recorded trace
impact = tracer.impact()
print(f"Total Energy Wh: {impact.total_energy_wh}")
print(f"Total GCO2e: {impact.total_gco2e}")
print(f"Total MLH2O: {impact.total_mlh2o}")
2. Single interaction
For a single interaction, the response is augmented with a scope3ai attribute that contains the
request and impact data. The impact data is calculated asynchronously so we need to wait
for the impact to be calculated and for the attribute to be ready.
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello world"}],
stream=False,
)
response.scope3ai.wait_impact()
impact = response.scope3ai.impact
print(f"Total Energy Wh: {impact.total_energy_wh}")
print(f"Total GCO2e: {impact.total_gco2e}")
print(f"Total MLH2O: {impact.total_mlh2o}")
3. Enabling Synchronous Mode for Immediate Impact Response
In synchronous mode, the SDK will include the impact data directly in the interaction response. This is useful when you want to get the impact data immediately after the interaction without waiting.
scope3.sync_mode = True
response = interact()
impact = response.scope3ai.impact
print(f"Total Energy Wh: {impact.total_energy_wh}")
print(f"Total GCO2e: {impact.total_gco2e}")
print(f"Total MLH2O: {impact.total_mlh2o}")
Development
This project use conventional commits and semantic versioning.
Also:
- pre-commit for code formatting, linting and conventional commit checks
- uv for project and dependency management.
Initial setup
$ pre-commit install
$ pre-commit install --hook-type commit-msg
Using with specific env
You can use UV_ENV_FILE or --env-file to specify the environment file to use.
$ export UV_ENV_FILE=.env
$ uv sync --all-extras --all-groups
$ uv run python -m examples.openai-sync-chat
Update typesgen.py
$ uv run datamodel-codegen \
--input tests/api-mocks/aiapi.yaml \
--input-file-type openapi \
--output scope3ai/api/typesgen.py \
--output-model-type pydantic_v2.BaseModel \
--use-schema-description \
--allow-extra-fields \
&& uv run ruff format scope3ai/api/typesgen.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scope3ai-0.1.1a3.tar.gz.
File metadata
- Download URL: scope3ai-0.1.1a3.tar.gz
- Upload date:
- Size: 25.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52515a11c812871eecff0c798b3bd8036af4f1f5d30633c3113610ce22cc6a9b
|
|
| MD5 |
69957c8f969ad36a432357a8d4aad1ca
|
|
| BLAKE2b-256 |
5369e6da515cdd346cb0c999f7367390312415f3d0485f0401f3e76bd77d4653
|
Provenance
The following attestation bundles were made for scope3ai-0.1.1a3.tar.gz:
Publisher:
release.yml on scope3data/scope3ai-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scope3ai-0.1.1a3.tar.gz -
Subject digest:
52515a11c812871eecff0c798b3bd8036af4f1f5d30633c3113610ce22cc6a9b - Sigstore transparency entry: 157939279
- Sigstore integration time:
-
Permalink:
scope3data/scope3ai-py@2f2850f9770333bc69080f614d8cf8615a0c42ed -
Branch / Tag:
refs/heads/main - Owner: https://github.com/scope3data
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2f2850f9770333bc69080f614d8cf8615a0c42ed -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file scope3ai-0.1.1a3-py3-none-any.whl.
File metadata
- Download URL: scope3ai-0.1.1a3-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9127673f87b7e03ce820c56dfee0f069ab750eb83e137f7f6a12542a075a84f6
|
|
| MD5 |
83bb7e45d84404c8cd8b8114e98c306e
|
|
| BLAKE2b-256 |
0c50e84b7769ee1bc8cb903012052563821fa062846e0f2680e1a65785e20b41
|
Provenance
The following attestation bundles were made for scope3ai-0.1.1a3-py3-none-any.whl:
Publisher:
release.yml on scope3data/scope3ai-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scope3ai-0.1.1a3-py3-none-any.whl -
Subject digest:
9127673f87b7e03ce820c56dfee0f069ab750eb83e137f7f6a12542a075a84f6 - Sigstore transparency entry: 157939280
- Sigstore integration time:
-
Permalink:
scope3data/scope3ai-py@2f2850f9770333bc69080f614d8cf8615a0c42ed -
Branch / Tag:
refs/heads/main - Owner: https://github.com/scope3data
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2f2850f9770333bc69080f614d8cf8615a0c42ed -
Trigger Event:
workflow_dispatch
-
Statement type: