Ad network that delivers ads to the LLM's response
Project description
Adstract AI Python SDK
Ad network SDK that enhances LLM prompts with integrated advertisements.
Install
python -m pip install adstractai
Quickstart
from adstractai import Adstract
client = Adstract(api_key="sk_test_1234567890")
enhanced_prompt = client.request_ad_enhancement(
prompt="How do I improve analytics in my LLM app?",
conversation={
"conversation_id": "conv-1",
"session_id": "sess-1",
"message_id": "msg-1",
},
user_agent=(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
),
x_forwarded_for="192.168.1.1",
)
print(enhanced_prompt) # Enhanced prompt with integrated ads
client.close()
Authentication
Pass an API key when initializing the client or set ADSTRACT_API_KEY.
export ADSTRACT_API_KEY="sk_test_1234567890"
from adstractai import Adstract
client = Adstract()
Required Parameters
All ad enhancement methods require both user_agent and x_forwarded_for parameters. Missing either parameter will raise a MissingParameterError:
from adstractai import Adstract
from adstractai.errors import MissingParameterError
client = Adstract(api_key="sk_test_1234567890")
try:
# This will raise MissingParameterError
client.request_ad_enhancement(
prompt="Test prompt",
conversation={"conversation_id": "c", "session_id": "s", "message_id": "m"},
user_agent="", # Empty user_agent
x_forwarded_for="192.168.1.1",
)
except MissingParameterError as e:
print(f"Error: {e}")
Available Methods
request_ad_enhancement()- Returns enhanced prompt, raises exception on failurerequest_ad_enhancement_or_default()- Returns enhanced prompt or original prompt on failurerequest_ad_enhancement_async()- Async version that returns enhanced promptrequest_ad_enhancement_or_default_async()- Async version with fallback behavior
Advanced usage
from adstractai import Adstract
client = Adstract(api_key="sk_test_1234567890", retries=2)
enhanced_prompt = client.request_ad_enhancement(
prompt="Need performance tips",
conversation={
"conversation_id": "conv-42",
"session_id": "sess-42",
"message_id": "msg-42",
},
user_agent=(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
),
x_forwarded_for="203.0.113.1",
constraints={
"max_ads": 2,
"safe_mode": "standard",
},
)
# For fallback behavior that returns original prompt on failure
safe_prompt = client.request_ad_enhancement_or_default(
prompt="Need performance tips",
conversation={
"conversation_id": "conv-42",
"session_id": "sess-42",
"message_id": "msg-42",
},
user_agent=(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
),
x_forwarded_for="203.0.113.1",
)
print(enhanced_prompt)
client.close()
Async usage
import asyncio
from adstractai import Adstract
async def main() -> None:
client = Adstract(api_key="sk_test_1234567890")
enhanced_prompt = await client.request_ad_enhancement_async(
prompt="Need performance tips",
conversation={
"conversation_id": "conv-99",
"session_id": "sess-99",
"message_id": "msg-99",
},
user_agent=(
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
),
x_forwarded_for="192.0.2.1",
)
# For fallback behavior in async
safe_prompt = await client.request_ad_enhancement_or_default_async(
prompt="Need performance tips",
conversation={
"conversation_id": "conv-99",
"session_id": "sess-99",
"message_id": "msg-99",
},
user_agent=(
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
),
x_forwarded_for="192.0.2.1",
)
print(enhanced_prompt)
await client.aclose()
asyncio.run(main())
Development setup
python -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[dev]"
pre-commit install
Scripts
ruff format .
ruff check .
pyright
pytest
python -m build
Release
- Bump the version in
pyproject.toml. - Update
CHANGELOG.md. - Commit the changes.
- Tag the release:
git tag vX.Y.Z. - Push commits and tags:
git push && git push --tags.
Publishing to PyPI happens automatically via GitHub Actions using trusted publishing.
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 adstractai-0.0.8.tar.gz.
File metadata
- Download URL: adstractai-0.0.8.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e567b14f0ae926824ec94f8a73443f379dda3c8558aecc324dfb4f3f28e4b51
|
|
| MD5 |
6dfea0ad20eede5a3292128356ce2325
|
|
| BLAKE2b-256 |
aec9f63debd12c2f008b431dff6de8722dc2382c92ca6ab104e9cd6e87ed3dd4
|
Provenance
The following attestation bundles were made for adstractai-0.0.8.tar.gz:
Publisher:
publish.yml on Adstract-AI/adstract-library
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
adstractai-0.0.8.tar.gz -
Subject digest:
8e567b14f0ae926824ec94f8a73443f379dda3c8558aecc324dfb4f3f28e4b51 - Sigstore transparency entry: 924516161
- Sigstore integration time:
-
Permalink:
Adstract-AI/adstract-library@8ee26ea5f69a4283ee46cb1c3e50f99a6e798348 -
Branch / Tag:
refs/tags/v0.0.8 - Owner: https://github.com/Adstract-AI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8ee26ea5f69a4283ee46cb1c3e50f99a6e798348 -
Trigger Event:
push
-
Statement type:
File details
Details for the file adstractai-0.0.8-py3-none-any.whl.
File metadata
- Download URL: adstractai-0.0.8-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c3a6f4b0ec7bf4cb70d9add544d384d0e1fd7043aa28c79bc12cf1063312796
|
|
| MD5 |
269fe5875afc0065848d3d50349f54db
|
|
| BLAKE2b-256 |
2ebba183ad6ae730178efc77cc82a3cad10830c42c72bef950f0e1017614707e
|
Provenance
The following attestation bundles were made for adstractai-0.0.8-py3-none-any.whl:
Publisher:
publish.yml on Adstract-AI/adstract-library
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
adstractai-0.0.8-py3-none-any.whl -
Subject digest:
8c3a6f4b0ec7bf4cb70d9add544d384d0e1fd7043aa28c79bc12cf1063312796 - Sigstore transparency entry: 924516164
- Sigstore integration time:
-
Permalink:
Adstract-AI/adstract-library@8ee26ea5f69a4283ee46cb1c3e50f99a6e798348 -
Branch / Tag:
refs/tags/v0.0.8 - Owner: https://github.com/Adstract-AI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8ee26ea5f69a4283ee46cb1c3e50f99a6e798348 -
Trigger Event:
push
-
Statement type: