# Create embeddings
Source: https://docs.anyapi.ai/api-reference/embeddings/create-embeddings
/openapi.json post /embeddings
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
# Create image
Source: https://docs.anyapi.ai/api-reference/images/create-image
/openapi.json post /images/generations
Creates an image given a text prompt. Supports streaming responses for real-time generation updates.
# List models
Source: https://docs.anyapi.ai/api-reference/models/list-models
/openapi.json get /models
Lists the currently available models, and provides basic information about each one such as the owner and availability.
# Check content moderation
Source: https://docs.anyapi.ai/api-reference/moderation/check-content-moderation
/openapi.json post /moderations
The moderations endpoint is a tool you can use to check whether content complies with an LLM Providers policies
# Create response
Source: https://docs.anyapi.ai/api-reference/responses/create-response
/openapi.json post /responses
Creates a comprehensive AI response with support for conversations, tools, files, and streaming
# [LEGACY] Create completion
Source: https://docs.anyapi.ai/api-reference/text/[legacy]-create-completion
/openapi.json post /completions
Create text completion from a single prompt
.
# Create chat completion
Source: https://docs.anyapi.ai/api-reference/text/create-chat-completion
/openapi.json post /chat/completions
Creates a model response for the given chat conversation
# Latency & Performance
Source: https://docs.anyapi.ai/features/latency-performance
*Lightning-fast AI requests without the complexity*
Speed matters. When your users are waiting for AI responses, every millisecond counts. That's why we built AnyAPI with performance as our obsession, not an afterthought.
## The Speed You Actually Get
**\~40ms of added latency** β that's it.
We're talking about the time between when your request hits our servers and when we forward it to your chosen AI provider. For context, that's faster than you can blink.
Here's how we keep it blazing fast:
β‘ **Edge-first architecture** β We run on Cloudflare Workers worldwide, so we're always close to your users\
π§ **Smart caching** β User data and API keys are cached at the edge for instant access\
π― **Optimized routing** β Our request processing is streamlined to the essentials
***
## What Affects Performance
### Cold Start Delays
*The "first request" phenomenon*
When we haven't seen traffic in a particular region for a while (typically 1-2 minutes), the first few requests might take a bit longer as our edge caches warm up. Think of it like starting a car on a cold morning β it needs a moment to get going.
**What this means for you**: The first request to a new region might add an extra 50-100ms while we get our caches populated. After that? Smooth sailing.
### Model Fallback Scenarios
*When Plan A doesn't work out*
Sometimes AI providers have hiccups β it's just the nature of the beast. When your primary model fails, we automatically try your next configured option. This failover protection keeps your app running, but that initial failure does add some latency to that specific request.
**What happens**:
1. Request goes to primary provider β fails (adds \~2-5 seconds)
2. We instantly retry with backup provider β succeeds
3. Your app gets the response (with some delay, but it works)
***
## Performance Optimization Playbook
### π― Smart Request Patterns
*How you send requests matters*
```python theme={"system"}
import time
import asyncio
import aiohttp
class HighPerformanceAPIClient:
def __init__(self, api_key, region="auto"):
self.api_key = api_key
self.base_url = "https://api.anyapi.ai/api/v1"
self.session = None
self.region = region
async def __aenter__(self):
# π Reuse connections for better performance
connector = aiohttp.TCPConnector(
limit=100, # Connection pool size
limit_per_host=10, # Per-host limit
keepalive_timeout=30 # Keep connections alive
)
self.session = aiohttp.ClientSession(
connector=connector,
timeout=aiohttp.ClientTimeout(total=60)
)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.session:
await self.session.close()
async def fast_completion(self, messages, model="gpt-4o", **kwargs):
"""Optimized for minimal latency"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
"X-AnyAPI-Region-Preference": self.region, # Stick to one region
"X-AnyAPI-Cache-Preference": "speed" # Prefer speed over cost
}
payload = {
"model": model,
"messages": messages,
"stream": kwargs.get("stream", False),
**kwargs
}
start_time = time.time()
async with self.session.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=payload
) as response:
result = await response.json()
latency = (time.time() - start_time) * 1000
# π Track your performance
print(f"β‘ Request completed in {latency:.0f}ms")
return result
# πββοΈ Usage for speed demons
async def main():
async with HighPerformanceAPIClient(API_KEY, region="us-east") as client:
# Multiple concurrent requests for maximum throughput
tasks = []
for i in range(5):
task = client.fast_completion([
{"role": "user", "content": f"Quick task #{i+1}"}
])
tasks.append(task)
# Execute all requests concurrently
results = await asyncio.gather(*tasks)
print(f"β
Completed {len(results)} requests concurrently")
# Run it
asyncio.run(main())
```
### πΎ Smart Caching Strategies
*Don't repeat expensive work*
```python theme={"system"}
import hashlib
import json
from functools import lru_cache
class CachedAPIClient:
def __init__(self, api_key):
self.api_key = api_key
self.response_cache = {}
def _generate_cache_key(self, messages, model, **kwargs):
"""Create consistent cache keys"""
cache_data = {
"messages": messages,
"model": model,
"temperature": kwargs.get("temperature", 1.0),
"max_tokens": kwargs.get("max_tokens")
}
cache_string = json.dumps(cache_data, sort_keys=True)
return hashlib.md5(cache_string.encode()).hexdigest()
async def cached_completion(self, messages, model="gpt-4o",
cache_ttl_minutes=5, **kwargs):
"""Lightning-fast responses for repeated requests"""
cache_key = self._generate_cache_key(messages, model, **kwargs)
# π Cache hit = instant response
if cache_key in self.response_cache:
cached_data = self.response_cache[cache_key]
if time.time() - cached_data["timestamp"] < cache_ttl_minutes * 60:
print("β‘ Cache hit! Instant response")
return cached_data["response"]
# π‘ Cache miss = API request + cache for next time
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
payload = {"model": model, "messages": messages, **kwargs}
async with aiohttp.ClientSession() as session:
async with session.post(
"https://api.anyapi.ai/api/v1/chat/completions",
headers=headers,
json=payload
) as response:
result = await response.json()
# πΎ Cache for future requests
self.response_cache[cache_key] = {
"response": result,
"timestamp": time.time()
}
print("π‘ Fresh API response cached")
return result
# Perfect for FAQ bots, repeated analyses, etc.
client = CachedAPIClient(API_KEY)
```
***
## Performance Monitoring
### Track Your Real-World Latency
*Because you can't optimize what you don't measure*
```python theme={"system"}
import time
from collections import defaultdict
class PerformanceTracker:
def __init__(self):
self.metrics = defaultdict(list)
def track_request(self, operation_name):
"""Context manager for tracking request performance"""
return RequestTimer(self.metrics, operation_name)
def get_performance_report(self):
"""Generate performance insights"""
report = {}
for operation, times in self.metrics.items():
if times:
report[operation] = {
"requests": len(times),
"avg_latency_ms": sum(times) / len(times),
"p50_latency_ms": sorted(times)[len(times)//2],
"p95_latency_ms": sorted(times)[int(len(times)*0.95)],
"max_latency_ms": max(times)
}
return report
class RequestTimer:
def __init__(self, metrics, operation_name):
self.metrics = metrics
self.operation_name = operation_name
self.start_time = None
def __enter__(self):
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.start_time:
duration_ms = (time.time() - self.start_time) * 1000
self.metrics[self.operation_name].append(duration_ms)
# π Usage
tracker = PerformanceTracker()
async def monitored_request(messages):
with tracker.track_request("chat_completion"):
# Your API request here
response = await client.fast_completion(messages)
return response
# After some requests, see how you're doing
performance_report = tracker.get_performance_report()
print("π― Performance Report:")
for operation, stats in performance_report.items():
print(f" {operation}:")
print(f" π {stats['requests']} requests")
print(f" β‘ {stats['avg_latency_ms']:.0f}ms average")
print(f" π― {stats['p95_latency_ms']:.0f}ms 95th percentile")
```
***
## When Speed Really Matters
### Real-Time Applications
*Sub-100ms total latency targets*
```python theme={"system"}
# Configuration for real-time apps (chat, live support, etc.)
real_time_config = {
"model": "openai/gpt-4o-mini", # Faster model for real-time use
"max_tokens": 150, # Shorter responses = faster delivery
"temperature": 0.7, # Consistent but not robotic
"stream": True, # Start showing results immediately
# π Performance optimizations
"provider_preferences": {
"primary": "openai",
"fallback": "anthropic"
},
"timeout": 15, # Fail fast for real-time apps
"priority": "speed" # Choose speed over cost
}
```
### Batch Processing
*When throughput beats individual request speed*
```python theme={"system"}
async def batch_process_optimized(requests_batch, concurrency=10):
"""Process many requests with optimal throughput"""
semaphore = asyncio.Semaphore(concurrency)
async def process_single_request(request_data):
async with semaphore: # Limit concurrent requests
return await client.fast_completion(**request_data)
# π Process all requests concurrently (up to limit)
tasks = [process_single_request(req) for req in requests_batch]
results = await asyncio.gather(*tasks, return_exceptions=True)
return results
```
***
## Troubleshooting Slow Performance
### π Common Issues & Quick Fixes
**Issue**: First request is slow in new regions\
**Fix**: Warm up your caches with a dummy request when deploying
**Issue**: Inconsistent latency throughout the day\
**Fix**: Check your credit balance and set up auto-topup
**Issue**: High latency for specific models\
**Fix**: Test different providers for that model family
### π Performance Debugging
```python theme={"system"}
def debug_slow_request():
"""Debug performance issues step by step"""
start_time = time.time()
# 1. Check account status
print("1οΈβ£ Checking account status...")
account_check_start = time.time()
# Account status check would go here
print(f" β
Account check: {(time.time() - account_check_start)*1000:.0f}ms")
# 2. Test simple request
print("2οΈβ£ Testing basic request...")
request_start = time.time()
# Basic API request would go here
print(f" β
API request: {(time.time() - request_start)*1000:.0f}ms")
# 3. Check network connectivity
print("3οΈβ£ Network diagnostics...")
# Network checks would go here
total_time = (time.time() - start_time) * 1000
print(f"π Total debug time: {total_time:.0f}ms")
```
***
## The Bottom Line
AnyAPI is built for speed. With proper configuration and best practices, you'll consistently see:
* **\~40ms added latency** for most requests
* **Sub-100ms total response times** for simple operations
* **Predictable performance** that scales with your application
**Quick wins for better performance:**
1. π Choose providers strategically for your regions
2. πΎ Cache responses when possible
3. π Monitor performance and optimize continuously
Ready to make your AI app lightning-fast? These optimizations will get you there. β‘
*Speed is a feature β make it yours! π*
# Model routing
Source: https://docs.anyapi.ai/features/model-routing
# Smart Model Routing
*Never let a single point of failure kill your AI workflow*
AnyAPI's routing system ensures your applications stay resilient, fast, and cost-effective by intelligently managing model selection and fallbacks. Choose your strategy: let our AI pick the perfect model, or build bulletproof fallback chains.
## π― Auto Router
*The smart choice that thinks for you*
Point your requests at `anyapi/auto` and watch as our AI-powered routing system automatically selects the optimal model for each prompt. No more guessing, no more manual optimizationβjust results.
```json theme={"system"}
{
"model": "anyapi/auto",
"messages": [
{
"role": "user",
"content": "Explain quantum computing to a 10-year-old"
}
]
}
```
**What happens behind the scenes:**
* NotDiamond AI analyzes your prompt's complexity, style, and requirements
* Dynamically routes to the best-performing model for that specific task
* Returns the response with the actual model used in the `model` field
*Perfect for: Applications that need consistent quality without micromanagement*
## π‘οΈ Fallback Chains
*Because reliability isn't optional*
Build unbreakable AI pipelines with the `models` parameter. Define your preferred model hierarchy and let AnyAPI handle the rest when things go sideways.
```json theme={"system"}
{
"models": [
"anthropic/claude-3.5-sonnet", // First choice
"openai/gpt-4o", // Solid backup
"gryphe/mythomax-l2-13b" // Always-available fallback
],
"messages": [
{
"role": "user",
"content": "Generate a product description for wireless headphones"
}
]
}
```
**Your safety net activates when:**
* π« Primary model hits rate limits
* β° Provider experiences downtime
* π Content gets flagged by moderation
* π Context length exceeds limits
* π₯ Any other model-specific errors
**The magic:** You only pay for the model that actually processes your request. No extra charges for the intelligent routing.
## π§ OpenAI SDK Integration
*Plug and play with your existing code*
Already using the OpenAI SDK? No problem. AnyAPI's routing works seamlessly with your current setup:
```typescript theme={"system"}
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://api.anyapi.ai/api/v1',
apiKey: 'your-anyapi-key'
});
const completion = await client.chat.completions.create({
model: 'openai/gpt-4o',
models: [
'anthropic/claude-3.5-sonnet',
'meta-llama/llama-3.1-70b-instruct'
],
messages: [{
role: 'user',
content: 'Write a haiku about debugging code at 3 AM'
}]
});
console.log(completion.choices[0].message.content);
```
## π‘ Pro Tips
**Mix and match strategies:** Use Auto Router for general tasks and fallback chains for mission-critical workflows.
**Strategic ordering:** Place your preferred models first in the arrayβAnyAPI tries them in sequence.
**Cost optimization:** Mix premium and budget models to balance quality and spend.
**Monitor model usage:** Check the returned `model` field to track which models are actually handling your requests.
***
*Ready to build bulletproof AI applications? Start routing smarter with AnyAPI.*
# Privacy logging
Source: https://docs.anyapi.ai/features/privacy-logging
# Privacy & Data: Your AI, Your Rules
*Because your prompts aren't anyone else's training data*
Building with AI means your data flows through multiple systemsβAnyAPI, model providers, and everything in between. Here's exactly what happens to your information, who sees what, and how to lock it down tight.
## π‘οΈ The AnyAPI Promise
*We're the pipeline, not the vault*
**The TL;DR:** AnyAPI doesn't store your prompts or responses. Period.
**What we do track:**
* π **Metadata only** (tokens used, response time, model called)
* π― **Anonymous sampling** for model ranking (completely disconnected from your account)
* π **Usage stats** to power your [activity dashboard](https://dash.anyapi.ai/?page=usage)
**What we never do:**
* π Read your conversations for fun
* πͺ Sell your data to third parties
* π€ Train our own models on your prompts
* π± Associate anonymous samples with your identity
## π’ Provider Policies: The Real Talk
*Each AI provider plays by different rules*
Every model provider on AnyAPI has their own data policies. Some are strict, others... less so. Here's how to navigate the landscape:
### π Training Data Policies
**The reality check:** Some providers use your prompts to improve their models. Others promise they won't.
**Your protection:** AnyAPI gives you granular control:
```json theme={"system"}
{
"model": "anthropic/claude-3.5-sonnet",
"data_policy": {
"training_allowed": false, // Block providers that train on data
"retention_days": 30 // Limit how long they can keep it
},
"messages": [...]
}
```
### π Data Retention Reality
**The spectrum:**
* **Zero retention:** Some providers delete everything immediately (rare but awesome)
* **Short term:** 30-90 days for abuse monitoring (common)
* **Compliance based:** Longer periods for legal/regulatory reasons
* **Indefinite:** Some free models keep data forever (buyer beware)
**Your move:** Check each provider's retention policy before sending sensitive data. AnyAPI shows this info upfrontβno hunting through legal docs required.
## βοΈ Privacy Controls That Actually Work
### Request-Level Protection
Lock down individual requests when handling sensitive data:
```json theme={"system"}
{
"model": "anyapi/auto",
"providers": {
"require": ["zero-retention", "no-training"],
"exclude": ["high-risk-provider"]
},
"messages": [
{
"role": "user",
"content": "Analyze this confidential financial report..."
}
]
}
```
## π― Smart Privacy Strategies
### The Layered Defense
* **Layer 1:** AnyAPI doesn't log by default
* **Layer 3:** Request-level overrides for ultra-sensitive data
* **Layer 4:** Data minimization in your prompts
### Data Classification
**Public data:** Use any provider, optimize for cost and speed
**Internal data:** Stick to no-training providers with short retention
**Confidential data:** Zero-retention providers only, audit everything
**Regulated data:** Compliance-certified providers with full paper trails
## π The Fine Print (Made Simple)
**Full legal details:** Check our [privacy policy](https://anyapi.ai/privacy) and [terms of service](https://anyapi.ai/terms)
**Provider specifics:** Each model's data policy is shown in real-timeβno guessing required
**Questions or concerns:** Contact our privacy team directly through the dashboard
**Remember:** Privacy isn't a checkboxβit's an ongoing practice. Review your settings regularly, especially as new providers join the platform.
***
*Your data, your rules, your peace of mind. Configure AnyAPI privacy settings that actually protect what matters.*
# Prompt caching
Source: https://docs.anyapi.ai/features/prompt-caching
# Stop Paying Twice for the Same Thing
*Turn repetitive prompts into serious savings*
Got a chunky system prompt you use over and over? Large context windows eating your budget alive? Prompt caching is your new best friendβstore frequently used content once, then reference it at a fraction of the cost.
## π° The Economics of Caching
*When the math actually makes sense*
**The problem:** You're paying full price every time you send that 5,000-token system promptβeven when it's identical to the last 100 requests.
**The solution:** Cache it once, reuse it cheap. Here's what you'll save:
| Provider | Cache Write Cost | Cache Read Cost | Your Savings |
| -------------------- | --------------------------------------------- | -------------------- | --------------- |
| **OpenAI** | Free (automatic) | 0.1xβ0.5x original | 50β90% off |
| **Anthropic Claude** | 1.25x original (5 min) / 2x original (1 hour) | 0.1x original | 90% off reads\* |
| **Grok (xAI)** | Free (automatic) | 0.1x original | 90% off |
| **DeepSeek** | Free (automatic) | 0.1x original | 90% off |
| **Google Gemini** | Auto (implicit) / standard input (explicit) | 0.1x original (2.5+) | 90% off |
*After the initial write cost, you're looking at massive savings on repeated use*
## π― When Caching Makes Bank
**Perfect for:**
* π Large system prompts (1000+ tokens)
* π Repeated context in conversations
* π Reference documents sent with every request
* π€ Multi-turn conversations with consistent setup
**Skip it for:**
* β‘ One-off requests
* π² Constantly changing prompts
* π Tiny system messages under the provider's minimum token threshold
## π οΈ Implementation That Actually Works
### The Basic Setup
*Cache your hefty system prompt once, reference it forever*
```json theme={"system"}
{
"model": "anthropic/claude-sonnet-4",
"messages": [
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are a senior software architect with 15 years of experience in distributed systems, microservices, and cloud architecture. When reviewing code, you focus on scalability, maintainability, security best practices, and performance optimization. You provide detailed technical feedback with specific examples and always suggest concrete improvements...",
"cache_control": {
"type": "ephemeral"
}
}
]
},
{
"role": "user",
"content": "Review this API design for our new user service"
}
]
}
```
### Python Implementation
*Copy, paste, profit*
```python theme={"system"}
import requests
def create_cached_request(user_message):
url = "https://api.anyapi.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
# Your expensive system prompt (cache this baby!)
system_prompt = """
You are an expert financial advisor with CFA certification and 20 years
of experience in portfolio management, risk assessment, and investment
strategy. You provide detailed analysis based on current market conditions,
regulatory requirements, and best practices in wealth management...
""" # Imagine this is 3000+ tokens
payload = {
"model": "anthropic/claude-sonnet-4",
"messages": [
{
"role": "system",
"content": [
{
"type": "text",
"text": system_prompt,
"cache_control": {"type": "ephemeral"}
}
]
},
{
"role": "user",
"content": user_message
}
]
}
return requests.post(url, headers=headers, json=payload)
# First call: Pay cache write cost
response1 = create_cached_request("Analyze my portfolio allocation")
# Subsequent calls: Pay only 10% for cached system prompt!
response2 = create_cached_request("What's your take on crypto exposure?")
response3 = create_cached_request("Should I rebalance before year-end?")
```
## π Provider Deep Dive
*Know your options, optimize your spend*
### π’ **OpenAI: Automatic & Generous**
* **Write cost:** FREE (automatic, no setup needed)
* **Read cost:** 50% off for GPT-4o, 90% off for GPT-5 / GPT-5 Mini / GPT-5.4
* **Minimum:** 1,024 tokens to qualify
* **Expiration:** Typically 5β10 minutes of inactivity, always cleared within 1 hour
* **How it works:** Fully automatic β caches the longest matching prefix starting at 1,024 tokens
### π΅ **Anthropic Claude: The Precision Player**
* **Write cost:** 25% premium for 5-minute TTL, 100% premium for 1-hour TTL
* **Read cost:** 90% discount (0.1x base input price)
* **Limitation:** Max 4 cache breakpoints per request
* **Expiration:** 5 minutes (default) or 1 hour (extended)
* **Minimum tokens:** 1,024β4,096 depending on model
* **Best for:** High-frequency workflows with explicit cache control
### π‘ **Grok (xAI): Automatic & Simple**
* **Write cost:** FREE (automatic)
* **Read cost:** 90% off original
* **How it works:** Automatic β all requests benefit from caching with no configuration
### π£ **Google Gemini: Dual Approach**
* **Implicit caching:** Automatic, enabled by default for most Gemini models
* **Explicit caching:** Manual, with configurable TTL (defaults to 1 hour) + storage costs
* **Cost:** 90% off cached tokens for Gemini 2.5+ models, 75% off for Gemini 2.0
* **Minimum tokens:** 1,024β4,096 depending on model
### π΄ **DeepSeek: Automatic Savings**
* **Write cost:** FREE (automatic KV cache on disk)
* **Read cost:** 90% off
* **How it works:** Automatic β repeated prefixes are cached and billed at the lower rate
## π― Pro Optimization Strategies
### The Smart Workflow
```python theme={"system"}
# Structure your prompts for maximum cache hits
base_system = {
"type": "text",
"text": "Your massive, reusable system prompt...",
"cache_control": {"type": "ephemeral"}
}
# Reuse the exact same cached content across requests
def make_request(user_input):
return {
"model": "anthropic/claude-sonnet-4",
"messages": [
{"role": "system", "content": [base_system]},
{"role": "user", "content": user_input}
]
}
```
### Cache Hit Maximization
**β
Do this:**
* Keep cached content **identical** byte-for-byte
* Bundle reusable context into cache-friendly chunks
* Monitor cache hit rates in your analytics
**β Avoid this:**
* Modifying cached content (instant cache miss)
* Caching tiny prompts (not worth the complexity)
* Ignoring expiration times (hello, surprise costs)
### Cost Monitoring Magic
```python theme={"system"}
# Track your cache performance
def analyze_cache_savings(requests_made, avg_system_tokens, hit_rate):
original_cost = requests_made * avg_system_tokens * INPUT_PRICE
# Anthropic example: 1.25x write + 0.1x reads
cache_cost = (avg_system_tokens * INPUT_PRICE * 1.25) + \
(requests_made * hit_rate * avg_system_tokens * INPUT_PRICE * 0.1)
savings = original_cost - cache_cost
print(f"You saved: ${savings:.2f} ({savings/original_cost*100:.1f}%)")
```
## β οΈ Cache Reality Check
**Remember:**
* π― **Exact matching required** β One character different = cache miss
* β° **Expiration is real** β Plan for cache warmup in workflows
* π **Token minimums apply** β Don't cache tiny prompts
* π **Monitor hit rates** β Low hit rates = wasted write costs
**When caching backfires:**
* Constantly changing system prompts
* Infrequent API usage
* Very short conversations
* Prompts under provider minimums
## π‘ Quick Wins
1. **Audit your system prompts** β Find the chunky, reusable ones
2. **Implement caching** β Start with your highest-traffic endpoints
3. **Monitor performance** β Track hit rates and actual savings
4. **Optimize expiration** β Align cache duration with usage patterns
***
*Ready to cut your prompt costs? Start caching the smart way with AnyAPI.*
# Structured Outputs
Source: https://docs.anyapi.ai/features/structured-outputs
# No More JSON Roulette
*When "close enough" isn't good enough for production*
Tired of AI models returning creative interpretations of your data schema? Structured outputs enforce JSON Schema validation so you get exactly what you ask for, every single time. No more parsing nightmares, no more defensive coding, no more production bugs from hallucinated field names.
## π― The Problem with "Creative" AI
*When flexibility becomes a liability*
**The AI says:** "Sure, I'll return JSON!"\
**What you get:** `{"tempurature": 72, "wheather": "sunny", "humididty": "kinda high"}`\
**What you needed:** `{"temperature": 72, "weather": "sunny", "humidity": 65}`
**The result:** Your parser breaks, your app crashes, your users complain, your weekend disappears.
**Structured outputs fix this forever.**
## β¨ What You Get with Structured Outputs
### π‘οΈ **Schema Enforcement**
The model MUST return exactly what you defineβno creative license allowed.
### π― **Type Safety**
Numbers are numbers, strings are strings, enums are enums. Period.
### π« **Zero Hallucinated Fields**
`additionalProperties: false` means exactly what it says.
### β‘ **Production-Ready Parsing**
Skip the defensive coding and error handlingβjust parse and go.
## π Getting Started: Your First Structured Response
### The Basic Setup
*From chaos to structure in one request*
```json theme={"system"}
{
"model": "openai/gpt-4o-2024-11-20",
"messages": [
{
"role": "user",
"content": "What's the weather like in London today?"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "weather_response",
"strict": true,
"schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country (e.g., 'London, UK')"
},
"temperature": {
"type": "number",
"description": "Temperature in Celsius"
},
"conditions": {
"type": "string",
"enum": ["sunny", "cloudy", "rainy", "snowy", "partly-cloudy"]
},
"humidity": {
"type": "number",
"minimum": 0,
"maximum": 100,
"description": "Humidity percentage"
}
},
"required": ["location", "temperature", "conditions"],
"additionalProperties": false
}
}
}
}
```
**What you get back:**
```json theme={"system"}
{
"location": "London, UK",
"temperature": 18,
"conditions": "cloudy",
"humidity": 72
}
```
**Every. Single. Time.** No surprises, no typos, no creative interpretations.
## π» Production-Ready Python Implementation
### The Complete Example
*Copy, paste, customize, ship*
```python theme={"system"}
import requests
from typing import Dict, Any, Optional
class StructuredAIClient:
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "https://api.anyapi.ai/v1/chat/completions"
self.headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
def extract_product_review(self, review_text: str) -> Dict[str, Any]:
"""Extract structured data from product reviews"""
payload = {
"model": "openai/gpt-4o-2024-11-20",
"messages": [
{
"role": "user",
"content": f"Extract key information from this product review: {review_text}"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "product_review_analysis",
"strict": True,
"schema": {
"type": "object",
"properties": {
"product_type": {
"type": "string",
"description": "Type of product being reviewed"
},
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"],
"description": "Overall sentiment of the review"
},
"rating": {
"type": "number",
"minimum": 1,
"maximum": 5,
"description": "Inferred rating from 1-5 stars"
},
"specs": {
"type": "object",
"properties": {
"ram": {"type": "string"},
"processor": {"type": "string"},
"battery_life": {"type": "string"},
"storage": {"type": "string"}
},
"additionalProperties": False
},
"price": {
"type": "number",
"description": "Price mentioned in the review"
},
"pros": {
"type": "array",
"items": {"type": "string"},
"description": "Positive aspects mentioned"
},
"cons": {
"type": "array",
"items": {"type": "string"},
"description": "Negative aspects mentioned"
},
"recommended": {
"type": "boolean",
"description": "Whether the reviewer recommends the product"
}
},
"required": ["product_type", "sentiment", "rating", "recommended"],
"additionalProperties": False
}
}
}
}
response = requests.post(self.base_url, headers=self.headers, json=payload)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
# Usage
client = StructuredAIClient("your-api-key")
review = """
This laptop is incredible! The M2 chip blazes through everything I throw at it.
16GB RAM handles multiple browser tabs and video editing smoothly. Battery easily
lasts 10+ hours of real work. The 512GB SSD is plenty fast. At $1,299, it's
expensive but worth every penny. Highly recommend for developers and creators!
"""
result = client.extract_product_review(review)
print(result)
# Guaranteed to match your schema exactly
```
## ποΈ Advanced Schema Patterns
### Complex Nested Structures
*For when your data gets sophisticated*
```json theme={"system"}
{
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "financial_analysis",
"strict": true,
"schema": {
"type": "object",
"properties": {
"company": {
"type": "object",
"properties": {
"name": {"type": "string"},
"ticker": {"type": "string"},
"sector": {"type": "string", "enum": ["tech", "finance", "healthcare", "energy", "retail"]}
},
"required": ["name", "ticker", "sector"],
"additionalProperties": false
},
"metrics": {
"type": "object",
"properties": {
"revenue": {"type": "number", "minimum": 0},
"profit_margin": {"type": "number", "minimum": -1, "maximum": 1},
"debt_to_equity": {"type": "number", "minimum": 0}
},
"required": ["revenue"],
"additionalProperties": false
},
"analysis": {
"type": "array",
"items": {
"type": "object",
"properties": {
"category": {"type": "string", "enum": ["growth", "profitability", "valuation", "risk"]},
"score": {"type": "number", "minimum": 1, "maximum": 10},
"reasoning": {"type": "string"},
"confidence": {"type": "number", "minimum": 0, "maximum": 1}
},
"required": ["category", "score", "reasoning"],
"additionalProperties": false
}
},
"recommendation": {
"type": "string",
"enum": ["strong_buy", "buy", "hold", "sell", "strong_sell"]
},
"target_price": {"type": "number", "minimum": 0}
},
"required": ["company", "metrics", "analysis", "recommendation"],
"additionalProperties": false
}
}
}
}
```
### Data Extraction Powerhouse
*Turn messy text into clean, structured data*
```python theme={"system"}
def extract_invoice_data(invoice_text: str):
"""Extract structured data from invoice text"""
schema = {
"type": "json_schema",
"json_schema": {
"name": "invoice_extraction",
"strict": True,
"schema": {
"type": "object",
"properties": {
"invoice_number": {"type": "string"},
"date": {"type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$"},
"vendor": {
"type": "object",
"properties": {
"name": {"type": "string"},
"address": {"type": "string"},
"tax_id": {"type": "string"}
},
"required": ["name"],
"additionalProperties": False
},
"line_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {"type": "string"},
"quantity": {"type": "number", "minimum": 0},
"unit_price": {"type": "number", "minimum": 0},
"total": {"type": "number", "minimum": 0}
},
"required": ["description", "quantity", "unit_price", "total"],
"additionalProperties": False
}
},
"subtotal": {"type": "number", "minimum": 0},
"tax_amount": {"type": "number", "minimum": 0},
"total_amount": {"type": "number", "minimum": 0}
},
"required": ["invoice_number", "date", "vendor", "line_items", "total_amount"],
"additionalProperties": False
}
}
}
# Your extraction logic here
return structured_request(invoice_text, schema)
```
## π― Real-World Use Cases
### E-commerce Product Categorization
```python theme={"system"}
product_schema = {
"type": "object",
"properties": {
"category": {"type": "string", "enum": ["electronics", "clothing", "home", "books", "sports"]},
"subcategory": {"type": "string"},
"price_range": {"type": "string", "enum": ["budget", "mid_range", "premium", "luxury"]},
"target_audience": {"type": "string", "enum": ["men", "women", "children", "unisex"]},
"tags": {"type": "array", "items": {"type": "string"}},
"confidence": {"type": "number", "minimum": 0, "maximum": 1}
},
"required": ["category", "price_range", "confidence"],
"additionalProperties": False
}
```
### Customer Support Ticket Classification
```python theme={"system"}
support_schema = {
"type": "object",
"properties": {
"urgency": {"type": "string", "enum": ["low", "medium", "high", "critical"]},
"category": {"type": "string", "enum": ["billing", "technical", "feature_request", "bug_report"]},
"sentiment": {"type": "string", "enum": ["frustrated", "neutral", "satisfied"]},
"requires_escalation": {"type": "boolean"},
"estimated_resolution_time": {"type": "string", "enum": ["minutes", "hours", "days", "weeks"]},
"key_issues": {"type": "array", "items": {"type": "string"}},
"suggested_actions": {"type": "array", "items": {"type": "string"}}
},
"required": ["urgency", "category", "sentiment", "requires_escalation"],
"additionalProperties": False
}
```
## π‘οΈ Pro Tips for Schema Design
### β
**Do This:**
```json theme={"system"}
{
"properties": {
"temperature": {
"type": "number",
"minimum": -50,
"maximum": 60,
"description": "Temperature in Celsius, realistic range for weather"
},
"status": {
"type": "string",
"enum": ["active", "inactive", "pending"],
"description": "Current status, must be one of the predefined values"
}
}
}
```
### β **Avoid This:**
```json theme={"system"}
{
"properties": {
"temperature": {"type": "string"}, // Use proper types!
"status": {"type": "string"}, // Use enums for limited values!
"extra_field": {} // Define everything explicitly!
},
"additionalProperties": true // This defeats the purpose!
}
```
### π― **Schema Design Principles:**
1. **Be Specific:** Use enums, ranges, and patterns liberally
2. **Add Descriptions:** Guide the model with clear field descriptions
3. **Enforce Types:** Don't accept strings when you need numbers
4. **Block Extras:** Always use `"additionalProperties": false`
5. **Require What Matters:** Mark critical fields as required
6. **Validate Constraints:** Use min/max, patterns, and format validations
## π¨ Model Compatibility & Error Handling
### Compatible Models
**β
Full Support:**
* OpenAI GPT-4 series ([gpt-4o](https://anyapi.ai/ai-models/openai-gpt-4o), [gpt-4-turbo](https://anyapi.ai/ai-models/openai-gpt-4-turbo))
* Anthropic [Claude 3.5 Sonnet](https://anyapi.ai/ai-models/anthropic-claude-3-5-sonnet)
* Select fine-tuned models
**β οΈ Limited Support:**
* Some open-source models
* Older model versions
**β No Support:**
* Legacy models (GPT-3.5 and older)
* Many specialized/smaller models
### Bulletproof Error Handling
```python theme={"system"}
def safe_structured_request(content: str, schema: dict, model: str = "openai/gpt-4o"):
"""Make structured requests with proper error handling"""
try:
response = make_structured_request(content, schema, model)
return {"success": True, "data": response}
except requests.exceptions.HTTPError as e:
if e.response.status_code == 400:
error_data = e.response.json()
if "structured outputs not supported" in error_data.get("error", {}).get("message", ""):
return {
"success": False,
"error": "Model doesn't support structured outputs",
"suggestion": "Try openai/gpt-4o or anthropic/claude-3.5-sonnet"
}
else:
return {
"success": False,
"error": "Invalid schema",
"details": error_data
}
else:
raise # Re-raise other HTTP errors
except Exception as e:
return {"success": False, "error": f"Unexpected error: {e}"}
```
## π‘ Advanced Patterns
### Schema Composition
```python theme={"system"}
# Reusable schema components
ADDRESS_SCHEMA = {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"},
"country": {"type": "string", "pattern": "^[A-Z]{2}$"}
},
"required": ["street", "city", "country"],
"additionalProperties": False
}
PERSON_SCHEMA = {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string", "format": "email"},
"address": ADDRESS_SCHEMA # Compose schemas
},
"required": ["name", "email"],
"additionalProperties": False
}
```
### Dynamic Schema Generation
```python theme={"system"}
def generate_extraction_schema(fields: list) -> dict:
"""Generate extraction schemas dynamically"""
properties = {}
required = []
for field in fields:
if field["required"]:
required.append(field["name"])
properties[field["name"]] = {
"type": field["type"],
"description": field["description"]
}
if field.get("enum"):
properties[field["name"]]["enum"] = field["enum"]
return {
"type": "object",
"properties": properties,
"required": required,
"additionalProperties": False
}
# Usage
fields = [
{"name": "product_name", "type": "string", "required": True, "description": "Name of the product"},
{"name": "category", "type": "string", "required": True, "enum": ["electronics", "clothing"], "description": "Product category"},
{"name": "price", "type": "number", "required": False, "description": "Product price"}
]
schema = generate_extraction_schema(fields)
```
***
*Ready to eliminate JSON parsing nightmares? Start enforcing structure with AnyAPI structured outputs.*
# Tag Routing
Source: https://docs.anyapi.ai/features/tag-routing
Route requests to backends with specific capabilities like vision, PDF input, or reasoning
# Tag Routing
*Guarantee the capabilities your request needs*
Not every backend supports every feature for a given model. Tag routing lets you declare exactly which capabilities your request requires β and AnyAPI guarantees it lands on a backend that supports them all. No more random failures because a backend can't handle your PDFs, audio, or tool calls.
```json theme={"system"}
{
"model": "anthropic/claude-sonnet-4",
"messages": [{"role": "user", "content": "Summarize this document"}],
"tags": ["messages:pdf_input"]
}
```
## How It Works
Add the `tags` parameter to your request with the capabilities you need. AnyAPI filters available backends to only those that match **all** your requested tags, then routes to the best one.
* **All tags must match** β if you send `["vision", "streaming"]`, the backend must support both
* **No match = 503** β if no backend supports all your tags, you'll get a clear error instead of a cryptic failure
* **Fully optional** β skip tags entirely and AnyAPI routes as usual
## π·οΈ Tag Format
Tags use a simple two-level naming scheme:
```
endpoint:capability β scoped to a specific API endpoint
capability β matches any endpoint
```
**Use endpoint-scoped tags when possible.** A capability might be available on one endpoint but not another β scoped tags give you precision.
### Endpoint Prefixes
| Prefix | API Endpoint |
| ------------------ | ---------------------- |
| `chat_completions` | `/v1/chat/completions` |
| `messages` | `/v1/messages` |
| `responses` | `/v1/responses` |
### Available Capabilities
| Tag | What it guarantees |
| --------------------------- | ----------------------------------------------------------- |
| `vision` | Image input support |
| `pdf_input` | PDF file input support |
| `audio_input` | Audio file input support |
| `reasoning` | Extended thinking / chain-of-thought |
| `streaming` | Streaming response support |
| `function_calling` | Function / tool calling |
| `parallel_function_calling` | Multiple tool calls in one turn |
| `tool_choice` | Explicit tool selection (`auto`, `required`, `none`, named) |
| `computer_use` | Computer use tool support |
| `assistant_prefill` | Pre-fill assistant response |
| `prompt_caching` | Prompt caching support |
| `web_search` | Built-in web search |
| `url_context` | URL context fetching |
Every capability works as a generic tag (`vision`) and as an endpoint-scoped tag (`chat_completions:vision`, `messages:vision`, `responses:vision`).
## π¨ Sending Tags
### Request Body (recommended)
Pass a `tags` array alongside your usual parameters:
```json theme={"system"}
{
"model": "anthropic/claude-sonnet-4",
"messages": [{"role": "user", "content": "Describe this image"}],
"tags": ["chat_completions:vision"]
}
```
### HTTP Header
Use the `x-router-tags` header with comma-separated values:
```bash theme={"system"}
curl https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "x-router-tags: chat_completions:vision,streaming" \
-H "Content-Type: application/json" \
-d '{"model": "anthropic/claude-sonnet-4", "messages": [...]}'
```
> If both `tags` in the body and `x-router-tags` header are present, the body takes precedence.
## π§ͺ Examples
### Vision β analyze an image
```bash theme={"system"}
curl https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4",
"messages": [
{"role": "user", "content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
]}
],
"tags": ["chat_completions:vision"]
}'
```
### PDF β summarize a document
```bash theme={"system"}
curl https://api.anyapi.ai/v1/messages \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": [
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": "..."}},
{"type": "text", "text": "Summarize this document"}
]}
],
"tags": ["messages:pdf_input"]
}'
```
### Streaming + Tool Calling
```bash theme={"system"}
curl https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4.1",
"messages": [{"role": "user", "content": "What is the weather in Berlin?"}],
"tools": [{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}}}],
"stream": true,
"tags": ["chat_completions:function_calling", "chat_completions:streaming"]
}'
```
### Reasoning via header
```bash theme={"system"}
curl https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "x-router-tags: chat_completions:reasoning" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4",
"messages": [{"role": "user", "content": "Solve step by step: what is 7! + 3!?"}]
}'
```
## β οΈ Error Handling
When no backend matches your tags, AnyAPI returns a **503** with a clear message:
```json theme={"system"}
{
"error": {
"message": "There is no available model deployment that meets your routing requirements",
"type": "None",
"code": "503"
}
}
```
**Common causes:**
* The model doesn't support this capability on any backend
* Typo in a tag name β check the tag list above
* You're using a generic tag but the capability is only available on specific endpoints
## π Discovering Tags for a Model
Query which tags any model supports:
```bash theme={"system"}
curl https://api.anyapi.ai/v1/models \
-H "Authorization: Bearer YOUR_API_KEY" | \
jq '.data[] | select(.id == "anthropic/claude-sonnet-4") | .tags'
```
This returns every tag available across all backends for the model. If a tag appears in the list, at least one backend supports it. Models with no capability tags omit the `tags` field.
## Tips
* **Start without tags** β only add them when you need a specific capability that isn't available everywhere
* **Use endpoint-scoped tags** β `chat_completions:vision` is safer than just `vision` because it guarantees the capability on the exact endpoint you're calling
* **Combine with fallback chains** β use the `models` parameter alongside tags for maximum reliability
* **Check before you build** β query `/v1/models` to see which tags your target model supports before hardcoding them
# Tool Calling
Source: https://docs.anyapi.ai/features/tool-calling
*Give your AI superpowers with external functions*
Tired of AI that can only chat? Tool calling transforms your models into action-taking powerhouses that can search databases, fetch live data, perform calculations, and interact with any system you can imagine.
Think of it as giving your AI a toolkit β instead of just talking about the weather, it can actually check the forecast. Instead of guessing about your inventory, it can query your database in real-time.
## How the Magic Works
### The Tool Calling Dance
*A simple 6-step process that changes everything*
1. **π οΈ You define the tools** β Tell the AI what functions are available
2. **π§ AI analyzes the request** β Model decides which tools it needs
3. **π AI requests tool execution** β "Hey, can you run `search_database` with these parameters?"
4. **β‘ Your code executes tools** β You run the actual functions and get results
5. **π€ You return the results** β Feed the data back to the AI
6. **β¨ AI generates final response** β Model combines tool results into a helpful answer
> **π‘ Key insight**: The AI doesn't execute tools directly β it's like having a smart assistant who knows what to ask for, but you maintain complete control over what actually happens.
***
## Quick Start: Your First Tool
### Define a Simple Tool
*Let's start with something useful*
```python theme={"system"}
import requests
import json
def search_products(query, max_price=None, category=None):
"""Search your product database"""
# Your actual database logic here
products = [
{"id": 1, "name": f"Premium {query}", "price": 899, "category": category},
{"id": 2, "name": f"Budget {query}", "price": 299, "category": category}
]
# Filter by price if specified
if max_price:
products = [p for p in products if p["price"] <= max_price]
return products
# π οΈ Tool definition - this is what the AI sees
tools = [
{
"type": "function",
"function": {
"name": "search_products",
"description": "Search the product database for items matching criteria",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "What to search for (e.g., 'laptop', 'headphones')"
},
"max_price": {
"type": "number",
"description": "Maximum price filter in dollars"
},
"category": {
"type": "string",
"enum": ["electronics", "clothing", "books", "home"],
"description": "Product category to search within"
}
},
"required": ["query"]
}
}
}
]
```
### Execute the Tool-Enhanced Conversation
*Watch the AI use your tools intelligently*
```python theme={"system"}
class ToolEnabledChat:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "https://api.anyapi.ai/v1/chat/completions"
self.available_functions = {
"search_products": search_products
}
def chat_with_tools(self, user_message, tools):
"""Handle a complete tool-calling conversation"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
messages = [{"role": "user", "content": user_message}]
# π― Initial request with tools available
payload = {
"model": "openai/gpt-4o-2024-11-20",
"messages": messages,
"tools": tools,
"tool_choice": "auto" # Let AI decide when to use tools
}
response = requests.post(self.base_url, headers=headers, json=payload)
result = response.json()
message = result["choices"][0]["message"]
messages.append(message)
# π§ Check if AI wants to use tools
if message.get("tool_calls"):
print(f"π οΈ AI wants to use {len(message['tool_calls'])} tool(s)")
# Execute each requested tool
for tool_call in message["tool_calls"]:
function_name = tool_call["function"]["name"]
function_args = json.loads(tool_call["function"]["arguments"])
print(f"β‘ Executing {function_name} with args: {function_args}")
# Execute the function
if function_name in self.available_functions:
try:
result = self.available_functions[function_name](**function_args)
print(f"β
Tool result: {result}")
# Add tool result to conversation
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps(result)
})
except Exception as e:
# Handle tool execution errors
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps({"error": str(e)})
})
else:
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps({"error": f"Unknown function: {function_name}"})
})
# π Send results back to AI for final response
payload["messages"] = messages
response = requests.post(self.base_url, headers=headers, json=payload)
final_result = response.json()
return final_result["choices"][0]["message"]["content"]
else:
# No tools needed, return the direct response
return message["content"]
# π Try it out
chat = ToolEnabledChat(API_KEY)
response = chat.chat_with_tools(
"I'm looking for laptops under $1000 in the electronics category",
tools
)
print(f"π€ AI Response: {response}")
```
***
## Real-World Power Tools
### Multi-Function Assistant
*Build an AI that can actually do things*
```python theme={"system"}
import requests
import datetime
import os
class PowerfulAIAssistant:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "https://api.anyapi.ai/v1/chat/completions"
# π οΈ Define all available functions
self.functions = {
"search_database": self.search_database,
"get_weather": self.get_weather,
"send_email": self.send_email,
"calculate": self.calculate,
"get_calendar_events": self.get_calendar_events,
"create_reminder": self.create_reminder
}
# π― Tool definitions for the AI
self.tools = [
{
"type": "function",
"function": {
"name": "search_database",
"description": "Search internal database for products, users, or orders",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search term"},
"table": {"type": "string", "enum": ["products", "users", "orders"]},
"limit": {"type": "integer", "default": 10}
},
"required": ["query", "table"]
}
}
},
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather and forecast for any city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"days": {"type": "integer", "description": "Forecast days (1-7)", "default": 1}
},
"required": ["city"]
}
}
},
{
"type": "function",
"function": {
"name": "send_email",
"description": "Send an email to someone",
"parameters": {
"type": "object",
"properties": {
"to": {"type": "string", "description": "Recipient email"},
"subject": {"type": "string", "description": "Email subject"},
"body": {"type": "string", "description": "Email content"}
},
"required": ["to", "subject", "body"]
}
}
},
{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform mathematical calculations including complex equations",
"parameters": {
"type": "object",
"properties": {
"expression": {"type": "string", "description": "Math expression to calculate"}
},
"required": ["expression"]
}
}
},
{
"type": "function",
"function": {
"name": "get_calendar_events",
"description": "Get upcoming calendar events",
"parameters": {
"type": "object",
"properties": {
"days_ahead": {"type": "integer", "description": "How many days to look ahead", "default": 7}
}
}
}
},
{
"type": "function",
"function": {
"name": "create_reminder",
"description": "Create a reminder for later",
"parameters": {
"type": "object",
"properties": {
"message": {"type": "string", "description": "Reminder text"},
"when": {"type": "string", "description": "When to remind (e.g., '2 hours', 'tomorrow 9am')"}
},
"required": ["message", "when"]
}
}
}
]
def search_database(self, query, table, limit=10):
"""Search your database"""
# Mock database results
results = {
"products": [
{"id": 1, "name": f"Product matching '{query}'", "price": 99},
{"id": 2, "name": f"Another {query} item", "price": 149}
],
"users": [
{"id": 1, "name": f"User {query}", "email": f"{query}@example.com"},
],
"orders": [
{"id": 1, "user": query, "total": 299, "status": "shipped"}
]
}
return results.get(table, [])[:limit]
def get_weather(self, city, days=1):
"""Get weather data"""
# Mock weather API
return {
"city": city,
"current": {"temp": 22, "condition": "Sunny"},
"forecast": [{"day": i+1, "temp": 20+i, "condition": "Partly cloudy"} for i in range(days)]
}
def send_email(self, to, subject, body):
"""Send email (mock implementation)"""
# In real implementation, integrate with email service
print(f"π§ EMAIL SENT:")
print(f"To: {to}")
print(f"Subject: {subject}")
print(f"Body: {body}")
return {"status": "sent", "message_id": "mock_123"}
def calculate(self, expression):
"""Safe calculator"""
try:
# Simple eval for demo - in production, use a proper math parser
result = eval(expression.replace('^', '**'))
return {"expression": expression, "result": result}
except:
return {"error": "Invalid expression"}
def get_calendar_events(self, days_ahead=7):
"""Get calendar events"""
# Mock calendar data
events = [
{"title": "Team Meeting", "date": "2024-01-15", "time": "10:00 AM"},
{"title": "Project Review", "date": "2024-01-16", "time": "2:00 PM"}
]
return events
def create_reminder(self, message, when):
"""Create a reminder"""
# Mock reminder system
return {
"reminder": message,
"scheduled_for": when,
"id": "reminder_456",
"status": "created"
}
def process_request(self, user_message):
"""Process a user request with full tool access"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
messages = [{"role": "user", "content": user_message}]
# Allow multiple tool calls if needed
max_iterations = 5
iteration = 0
while iteration < max_iterations:
payload = {
"model": "openai/gpt-4o-2024-11-20",
"messages": messages,
"tools": self.tools,
"tool_choice": "auto"
}
response = requests.post(self.base_url, headers=headers, json=payload)
result = response.json()
message = result["choices"][0]["message"]
messages.append(message)
if message.get("tool_calls"):
print(f"π Iteration {iteration + 1}: AI using {len(message['tool_calls'])} tool(s)")
# Execute all requested tools
for tool_call in message["tool_calls"]:
function_name = tool_call["function"]["name"]
function_args = json.loads(tool_call["function"]["arguments"])
if function_name in self.functions:
try:
result = self.functions[function_name](**function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps(result)
})
except Exception as e:
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps({"error": str(e)})
})
iteration += 1
else:
# No more tool calls needed
return message["content"]
return "Sorry, the task required too many tool calls to complete."
# π― Create your powerful AI assistant
assistant = PowerfulAIAssistant(API_KEY)
# π Test complex multi-tool requests
response = assistant.process_request(
"Check the weather in London, search for any orders from user 'john', and calculate 15% tip on a $85 bill. Also send me an email summary to john@example.com"
)
print(f"π€ Assistant: {response}")
```
### Smart E-commerce Bot
*Build a shopping assistant that actually knows your inventory*
```python theme={"system"}
class EcommerceBot:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "https://api.anyapi.ai/v1/chat/completions"
# Mock inventory data
self.inventory = [
{"id": 1, "name": "MacBook Pro 14\"", "price": 1999, "category": "laptops", "stock": 5},
{"id": 2, "name": "ThinkPad X1 Carbon", "price": 1299, "category": "laptops", "stock": 3},
{"id": 3, "name": "Sony WH-1000XM4", "price": 299, "category": "headphones", "stock": 12},
{"id": 4, "name": "iPhone 15 Pro", "price": 999, "category": "phones", "stock": 8}
]
self.tools = [
{
"type": "function",
"function": {
"name": "search_products",
"description": "Search available products by name, category, or price range",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search term"},
"category": {"type": "string", "description": "Product category"},
"max_price": {"type": "number", "description": "Maximum price"},
"min_price": {"type": "number", "description": "Minimum price"}
}
}
}
},
{
"type": "function",
"function": {
"name": "check_stock",
"description": "Check stock availability for specific products",
"parameters": {
"type": "object",
"properties": {
"product_ids": {"type": "array", "items": {"type": "integer"}}
},
"required": ["product_ids"]
}
}
},
{
"type": "function",
"function": {
"name": "get_recommendations",
"description": "Get product recommendations based on user preferences",
"parameters": {
"type": "object",
"properties": {
"category": {"type": "string", "description": "Preferred category"},
"budget": {"type": "number", "description": "Budget limit"},
"use_case": {"type": "string", "description": "What the user needs it for"}
}
}
}
}
]
def search_products(self, query=None, category=None, max_price=None, min_price=None):
"""Search products with filters"""
results = self.inventory.copy()
if query:
results = [p for p in results if query.lower() in p["name"].lower()]
if category:
results = [p for p in results if p["category"] == category]
if max_price:
results = [p for p in results if p["price"] <= max_price]
if min_price:
results = [p for p in results if p["price"] >= min_price]
return results
def check_stock(self, product_ids):
"""Check stock for specific products"""
stock_info = []
for pid in product_ids:
product = next((p for p in self.inventory if p["id"] == pid), None)
if product:
stock_info.append({
"product_id": pid,
"name": product["name"],
"stock": product["stock"],
"available": product["stock"] > 0
})
return stock_info
def get_recommendations(self, category=None, budget=None, use_case=None):
"""Get smart recommendations"""
recommendations = self.inventory.copy()
if category:
recommendations = [p for p in recommendations if p["category"] == category]
if budget:
recommendations = [p for p in recommendations if p["price"] <= budget]
# Sort by price and stock
recommendations.sort(key=lambda x: (x["price"], -x["stock"]))
return recommendations[:3] # Top 3 recommendations
def chat(self, user_message):
"""Handle customer inquiries with tool support"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
messages = [
{
"role": "system",
"content": "You are a helpful e-commerce assistant. Use the available tools to search products, check stock, and provide recommendations. Be friendly and helpful."
},
{"role": "user", "content": user_message}
]
payload = {
"model": "openai/gpt-4o-2024-11-20",
"messages": messages,
"tools": self.tools,
"tool_choice": "auto"
}
response = requests.post(self.base_url, headers=headers, json=payload)
result = response.json()
message = result["choices"][0]["message"]
messages.append(message)
if message.get("tool_calls"):
# Execute tools
for tool_call in message["tool_calls"]:
function_name = tool_call["function"]["name"]
function_args = json.loads(tool_call["function"]["arguments"])
if function_name == "search_products":
result = self.search_products(**function_args)
elif function_name == "check_stock":
result = self.check_stock(**function_args)
elif function_name == "get_recommendations":
result = self.get_recommendations(**function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps(result)
})
# Get final response
payload["messages"] = messages
response = requests.post(self.base_url, headers=headers, json=payload)
final_result = response.json()
return final_result["choices"][0]["message"]["content"]
return message["content"]
# ποΈ Test the e-commerce bot
bot = EcommerceBot(API_KEY)
# Try different queries
queries = [
"I'm looking for a laptop under $1500 for programming",
"Do you have the MacBook Pro in stock?",
"Can you recommend good headphones for under $400?",
"Show me all phones available"
]
for query in queries:
print(f"\nπ€ Customer: {query}")
response = bot.chat(query)
print(f"π€ Bot: {response}")
```
***
## Advanced Tool Strategies
### Tool Choice Control
*Fine-tune when and how tools are used*
```python theme={"system"}
def smart_tool_control(user_message, urgency="normal"):
"""Control tool usage based on context"""
# Different strategies for different situations
strategies = {
"force_tools": {
"tool_choice": "required", # Must use a tool
"use_case": "When you know tools are needed"
},
"specific_tool": {
"tool_choice": {
"type": "function",
"function": {"name": "search_database"}
},
"use_case": "Force a specific tool"
},
"no_tools": {
"tool_choice": "none",
"use_case": "Chat only, no external actions"
},
"auto_decide": {
"tool_choice": "auto",
"use_case": "Let AI decide (most common)"
}
}
# Choose strategy based on urgency and message content
if "emergency" in user_message.lower() or urgency == "high":
strategy = "force_tools"
elif "just chat" in user_message.lower():
strategy = "no_tools"
elif "search" in user_message.lower():
strategy = "specific_tool"
else:
strategy = "auto_decide"
return strategies[strategy]
# Usage examples
emergency_config = smart_tool_control("Emergency: find all critical alerts", urgency="high")
chat_config = smart_tool_control("Let's just chat about the weather")
search_config = smart_tool_control("Search for user data")
```
### Error Handling & Resilience
*Build bulletproof tool execution*
```python theme={"system"}
class RobustToolExecutor:
def __init__(self, api_key):
self.api_key = api_key
self.retry_count = 3
self.timeout_seconds = 30
def safe_execute_tool(self, function_name, function_args, available_functions):
"""Execute tools with comprehensive error handling"""
try:
# Validate function exists
if function_name not in available_functions:
return {
"error": f"Function '{function_name}' not available",
"available_functions": list(available_functions.keys())
}
# Validate arguments
function = available_functions[function_name]
# Execute with timeout protection
import signal
def timeout_handler(signum, frame):
raise TimeoutError("Tool execution timed out")
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(self.timeout_seconds)
try:
result = function(**function_args)
signal.alarm(0) # Cancel timeout
return {
"success": True,
"result": result,
"function": function_name,
"execution_time": "< 30s"
}
except TimeoutError:
return {
"error": f"Function '{function_name}' timed out after {self.timeout_seconds}s",
"suggestion": "Try with simpler parameters"
}
except Exception as e:
return {
"error": f"Function execution failed: {str(e)}",
"function": function_name,
"args_used": function_args
}
except Exception as e:
return {
"error": f"Tool execution system error: {str(e)}",
"contact_support": True
}
def execute_with_retry(self, function_name, function_args, available_functions):
"""Execute with automatic retry logic"""
last_error = None
for attempt in range(self.retry_count):
print(f"π Attempt {attempt + 1}/{self.retry_count} for {function_name}")
result = self.safe_execute_tool(function_name, function_args, available_functions)
if result.get("success"):
if attempt > 0:
print(f"β
Succeeded on attempt {attempt + 1}")
return result
last_error = result
# Wait before retry (exponential backoff)
import time
time.sleep(2 ** attempt)
return {
"error": f"Failed after {self.retry_count} attempts",
"last_error": last_error,
"suggestion": "Check your function implementation or arguments"
}
# π‘οΈ Usage with error handling
executor = RobustToolExecutor(API_KEY)
result = executor.execute_with_retry(
"search_database",
{"query": "test", "table": "products"},
{"search_database": search_products}
)
if result.get("success"):
print(f"β
Tool succeeded: {result['result']}")
else:
print(f"β Tool failed: {result['error']}")
```
***
## Tool Security Best Practices
### Input Validation & Sanitization
*Never trust, always verify*
```python theme={"system"}
import re
from typing import Dict, Any, List
class SecureToolValidator:
def __init__(self):
self.dangerous_patterns = [
r'__.*__', # Python dunder methods
r'eval\s*\(', # eval calls
r'exec\s*\(', # exec calls
r'import\s+', # import statements
r'open\s*\(', # file operations
r'subprocess', # system calls
]
def validate_function_args(self, function_name: str, args: Dict[str, Any]) -> Dict[str, Any]:
"""Validate and sanitize tool arguments"""
cleaned_args = {}
for key, value in args.items():
# Check for dangerous patterns in string values
if isinstance(value, str):
for pattern in self.dangerous_patterns:
if re.search(pattern, value, re.IGNORECASE):
raise ValueError(f"Potentially dangerous content detected in {key}: {pattern}")
# Basic sanitization
value = value.strip()
# Length limits
if len(value) > 1000:
raise ValueError(f"Argument {key} too long (max 1000 chars)")
# Type validation based on function
if function_name == "search_database":
if key == "limit" and (not isinstance(value, int) or value > 100):
value = min(int(value), 100)
elif key == "query" and not isinstance(value, str):
value = str(value)
cleaned_args[key] = value
return cleaned_args
def is_function_allowed(self, function_name: str, user_permissions: List[str]) -> bool:
"""Check if user has permission to use this function"""
function_permissions = {
"search_database": ["read_data", "admin"],
"send_email": ["send_messages", "admin"],
"delete_data": ["admin"], # High privilege function
"get_weather": ["public"], # Anyone can use
}
required_perms = function_permissions.get(function_name, ["admin"])
return any(perm in user_permissions for perm in required_perms)
# π Secure tool execution
validator = SecureToolValidator()
def secure_tool_execution(function_name, function_args, user_permissions, available_functions):
"""Execute tools with security checks"""
try:
# 1. Check permissions
if not validator.is_function_allowed(function_name, user_permissions):
return {"error": f"Permission denied for function: {function_name}"}
# 2. Validate and sanitize arguments
clean_args = validator.validate_function_args(function_name, function_args)
# 3. Execute function
if function_name in available_functions:
result = available_functions[function_name](**clean_args)
return {"success": True, "result": result}
else:
return {"error": f"Function not found: {function_name}"}
except ValueError as e:
return {"error": f"Validation error: {str(e)}"}
except Exception as e:
return {"error": f"Execution error: {str(e)}"}
# Usage with security
user_perms = ["read_data", "send_messages"] # User's permissions
result = secure_tool_execution(
"search_database",
{"query": "laptops", "limit": 50},
user_perms,
{"search_database": search_products}
)
```
***
## Performance Optimization
### Parallel Tool Execution
*Run multiple tools simultaneously*
```python theme={"system"}
import asyncio
import aiohttp
class ParallelToolExecutor:
def __init__(self, api_key):
self.api_key = api_key
async def execute_tools_parallel(self, tool_calls, available_functions):
"""Execute multiple tools concurrently"""
async def execute_single_tool(tool_call):
function_name = tool_call["function"]["name"]
function_args = json.loads(tool_call["function"]["arguments"])
try:
# Convert sync
```
# Uptime Optimization
Source: https://docs.anyapi.ai/features/uptime-optimization
Your users don't care which AI provider you're usingβthey just want your app to work. AnyAPI's uptime optimization makes sure it does, automatically switching between providers so your application stays bulletproof.
## What You Get
### 24/7 Provider Babysitting
We watch every provider like a hawk, tracking the stuff that matters:
* **Speed checks** - How fast each provider responds
* **Failure tracking** - When things break (and they will)
* **Availability monitoring** - Who's actually online right now
* **Model performance** - Individual model health across providers
### Smart Traffic Direction
When providers start acting up, we don't wait around:
* Route your requests to whoever's fastest and most reliable
* Automatically retry failed requests with backup providers
* Spread the load so no single provider gets hammered
* Switch seamlesslyβyour code never knows the difference
## How the Magic Happens
1. **Always Watching:** Every provider gets monitored around the clock
2. **Data Crunching:** Real-time performance analysis of everything
3. **Smart Decisions:** Route requests based on who's actually working well
4. **Instant Backup:** When stuff breaks, we switch before you notice
## Why This Rocks
* **Stay Online:** Automatic failover means no more "sorry, our AI is down" messages
* **Go Faster:** Always get routed to the speediest provider available
* **Sleep Better:** Multiple backups mean you're never stuck with one flaky provider
* **Zero Changes:** Drop-in replacement for your existing API calls
## Getting Started
### Let Us Choose (Recommended)
Just make your requestβwe'll pick the best provider automatically:
```python theme={"system"}
import requests
url = "https://api.anyapi.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
# We'll find you the best GPT-4o provider automatically
payload = {
"model": "openai/gpt-4o",
"messages": [
{"role": "user", "content": "Hello, world!"}
]
}
response = requests.post(url, headers=headers, json=payload)
# That's it. We handled everything behind the scenes.
```
### Pick Your Favorites (But Keep Backups)
Want some control? Set preferences and we'll still keep you covered:
```python theme={"system"}
payload = {
"model": "openai/gpt-4o", # Try OpenAI first
"messages": [
{"role": "user", "content": "Hello, world!"}
],
"provider": {
"order": ["openai", "azure"], # Your preference order
"allow_fallbacks": True # But save me if they're down
}
}
```
## Real-World Examples
### The Bulletproof App
```python theme={"system"}
import requests
import time
def unbreakable_ai_request(messages, model="gpt-4o", max_retries=3):
"""Make AI requests that actually work when you need them to"""
url = "https://api.anyapi.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": model,
"messages": messages,
# AnyAPI does all the heavy lifting
}
for attempt in range(max_retries):
try:
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
if attempt == max_retries - 1:
raise e
time.sleep(2 ** attempt) # Chill for a bit before trying again
return None
# Use it like any other function
messages = [{"role": "user", "content": "Analyze this quarterly report..."}]
result = unbreakable_ai_request(messages)
```
### Multi-Provider Models Made Easy
```python theme={"system"}
# These models work across multiple providersβwe'll pick the best one
popular_models = [
"claude-3-5-sonnet-20241022", # Anthropic, AWS, you name it
"llama-3.3-70b-instruct", # Available everywhere
"gpt-4o-2024-11-20" # OpenAI, Azure, others
]
for model in popular_models:
payload = {
"model": model,
"messages": [{"role": "user", "content": "Quick test"}]
# We automatically route to whoever's working best right now
}
response = requests.post(url, headers=headers, json=payload)
# Just worksβ’
```
## Behind-the-Scenes Intel
### Response Headers Tell the Story
We include headers so you can see what's happening:
```python theme={"system"}
response = requests.post(url, headers=headers, json=payload)
# Check our decision-making
provider_used = response.headers.get('X-AnyAPI-Provider')
response_time = response.headers.get('X-AnyAPI-Response-Time')
failover_count = response.headers.get('X-AnyAPI-Failovers')
print(f"Used: {provider_used}")
print(f"Speed: {response_time}ms")
print(f"Backup switches: {failover_count}")
```
### When Everything Goes Wrong
Handle the rare cases when all providers are having a bad day:
```python theme={"system"}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
result = response.json()
except requests.exceptions.HTTPError as e:
if e.response.status_code == 503:
print("The entire AI universe is down. Time for coffee.")
elif e.response.status_code == 429:
print("Everyone's getting rate limited. Slow down, cowboy.")
else:
print(f"Something weird happened: {e}")
```
## Pro Tips
1. **Add Retry Logic:** Use exponential backoffβbe persistent but not annoying
2. **Watch the Headers:** Learn which providers work best for your use case
3. **Set Smart Timeouts:** Don't wait forever, but give providers a fair shot
4. **Handle Rate Limits:** Everyone gets throttled sometimes
5. **Log Everything:** Track patterns to optimize your requests
6. **Test Disasters:** Make sure your app handles provider switches gracefully
## Advanced Configuration
### Fine-Tune the Sensitivity
```python theme={"system"}
payload = {
"model": "openai/gpt-4o",
"messages": messages,
"provider": {
"failover_threshold": 0.1, # Switch after 10% of requests fail
"response_timeout": 30, # Wait 30 seconds before giving up
"max_retries": 2 # Try each provider twice max
}
}
```
### Provider Blacklists
```python theme={"system"}
payload = {
"model": "anthropic/claude-3-5-sonnet-20241022",
"messages": messages,
"provider": {
"exclude": ["that-flaky-provider", "the-slow-one"], # Skip these
"require_region": "us-east-1" # Geography matters
}
}
```
## Perfect For
* **Production apps** that can't afford to break
* **Real-time services** where "try again later" isn't an option
* **Batch jobs** that need to churn through massive datasets
* **Customer-facing features** that need to work every single time
* **Mission-critical systems** where downtime costs money
***
*Ready to make your AI infrastructure bulletproof? [AnyAPI](https://anyapi.ai/)'s uptime optimization runs in the background, so you can focus on building amazing features instead of babysitting API providers.*
# Web Search
Source: https://docs.anyapi.ai/features/web-search
Give your AI models access to real-time web data. Instead of relying on training data alone, models can search the web and ground their responses in current information.
## What This Unlocks
* **Live web data** β No more "I don't know about recent events"
* **Current information** β Break free from training cutoff dates
* **Automatic integration** β Search results blend seamlessly into responses
* **Configurable depth** β Control how much web context to include
## How It Works
Add `web_search_options` to your API request. AnyAPI passes the search request to the model's provider, which performs the web search and returns a response grounded in real-time data. The exact search mechanism depends on the provider β Anthropic uses its native web search tool, Google uses grounding with search, OpenAI uses its built-in search, and so on.
## Chat Completions API
### Basic Example
```bash cURL theme={"system"}
curl -X POST "https://api.anyapi.ai/v1/chat/completions" \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4",
"messages": [
{
"role": "user",
"content": "What are the latest developments in AI research this week?"
}
],
"web_search_options": {
"search_context_size": "medium"
}
}'
```
```python Python theme={"system"}
import requests
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"model": "anthropic/claude-sonnet-4",
"messages": [
{
"role": "user",
"content": "What are the latest developments in AI research this week?"
}
],
"web_search_options": {
"search_context_size": "medium"
}
}
)
print(response.json()["choices"][0]["message"]["content"])
```
```javascript JavaScript theme={"system"}
const response = await fetch('https://api.anyapi.ai/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'anthropic/claude-sonnet-4',
messages: [
{
role: 'user',
content: 'What are the latest developments in AI research this week?'
}
],
web_search_options: {
search_context_size: 'medium'
}
})
});
const data = await response.json();
console.log(data.choices[0].message.content);
```
### The `web_search_options` Parameter
| Parameter | Type | Description |
| --------------------- | ------ | -------------------------------------------------------------------------------------------------- |
| `search_context_size` | string | Amount of web context to include: `"low"`, `"medium"` (default), or `"high"` |
| `user_location` | object | Optional location for localized search results (supported by OpenAI, Anthropic, and Google models) |
### Localized Search with `user_location`
Pass a location to get region-specific search results:
```python Python theme={"system"}
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"model": "anthropic/claude-sonnet-4",
"messages": [
{"role": "user", "content": "What's the weather forecast for this weekend?"}
],
"web_search_options": {
"search_context_size": "low",
"user_location": {
"type": "approximate",
"city": "San Francisco",
"region": "California",
"country": "US",
"timezone": "America/Los_Angeles"
}
}
}
)
```
## Responses API
You can also use web search through the `/v1/responses` endpoint with the `web_search_preview` tool:
```python Python theme={"system"}
import requests
response = requests.post(
"https://api.anyapi.ai/v1/responses",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"model": "openai/gpt-5",
"input": "What is happening in the stock market today?",
"tools": [
{
"type": "web_search_preview",
"search_context_size": "medium"
}
]
}
)
print(response.json())
```
```javascript JavaScript theme={"system"}
const response = await fetch('https://api.anyapi.ai/v1/responses', {
method: 'POST',
headers: {
'Authorization': 'Bearer your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'openai/gpt-5',
input: 'What is happening in the stock market today?',
tools: [
{
type: 'web_search_preview',
search_context_size: 'medium'
}
]
})
});
const data = await response.json();
console.log(data);
```
## Search Context Levels
| Level | Context | Speed | Best For |
| ---------- | -------- | -------- | ----------------------------------- |
| `"low"` | Minimal | Fastest | Quick fact-checks, simple questions |
| `"medium"` | Balanced | Moderate | News updates, general research |
| `"high"` | Maximum | Slowest | Deep analysis, complex research |
## Real-World Examples
### Latest News
```python Python theme={"system"}
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"model": "google/gemini-2.5-flash",
"messages": [
{"role": "user", "content": "What are the top tech news stories today?"}
],
"web_search_options": {
"search_context_size": "medium"
}
}
)
```
### Deep Research with Perplexity
```python Python theme={"system"}
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"model": "perplexity/sonar-pro",
"messages": [
{"role": "user", "content": "Analyze current trends in renewable energy investments"}
],
"web_search_options": {
"search_context_size": "high"
}
}
)
```
### Reasoning Over Web Results
```python Python theme={"system"}
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"model": "openai/o3",
"messages": [
{"role": "user", "content": "Compare the latest quarterly earnings of NVIDIA vs AMD"}
],
"web_search_options": {
"search_context_size": "high"
}
}
)
```
## Supported Models
Web search works with 800+ models on AnyAPI, including:
* **OpenAI:** [GPT-5](https://anyapi.ai/ai-models/openai-gpt-5), [GPT-5 Mini](https://anyapi.ai/ai-models/openai-gpt-5-mini), [GPT-4o](https://anyapi.ai/ai-models/openai-gpt-4o), [GPT-4o Search Preview](https://anyapi.ai/ai-models/openai-gpt-4o-search-preview), [o3-Pro](https://anyapi.ai/ai-models/openai-o3-pro), and more
* **Anthropic:** [Claude Opus 4.6](https://anyapi.ai/ai-models/anthropic-claude-opus-4-6), [Claude Sonnet 4.6](https://anyapi.ai/ai-models/anthropic-claude-sonnet-4-6), [Claude Haiku 4.5](https://anyapi.ai/ai-models/anthropic-claude-haiku-4-5), and more
* **Google:** [Gemini 2.5 Pro](https://anyapi.ai/ai-models/google-gemini-2-5-pro), [Gemini 2.5 Flash](https://anyapi.ai/ai-models/google-gemini-2-5-flash), [Gemini 3 Pro/Flash Preview](https://anyapi.ai/ai-models/gemini-3-pro-preview), and more
* **xAI:** [Grok 4](https://anyapi.ai/ai-models/xai-grok-4), [Grok 4.1 Fast](https://anyapi.ai/ai-models/xai-grok-4-1-fast), [Grok 3](https://anyapi.ai/ai-models/xai-grok-3), and more
* **Perplexity:** Sonar, [Sonar Pro](https://anyapi.ai/ai-models/perplexity-sonar-pro), Sonar Deep Research, [Sonar Reasoning Pro](https://anyapi.ai/ai-models/perplexity-sonar-reasoning-pro)
* **DeepSeek:** [DeepSeek V3.2](https://anyapi.ai/ai-models/deepseek-deepseek-v3-2), [DeepSeek R1](https://anyapi.ai/ai-models/deepseek-r1), and more
* **Qwen:** [Qwen3 Max](https://anyapi.ai/ai-models/qwen-qwen3-max), [Qwen3 Coder Plus](https://anyapi.ai/ai-models/owen-qwen3-coder-plus), Qwen3 235B, and more
* **Meta:** [Llama 4 Maverick](https://anyapi.ai/ai-models/meta-llama-4-maverick), Llama 4 Scout, [Llama 3.1 405B](https://anyapi.ai/ai-models/meta-llama-3-1-405b-instruct), and more
* **Mistral:** [Mistral Large](https://anyapi.ai/ai-models/mistral-large), [Codestral](https://anyapi.ai/ai-models/mistral-codestral-2508), Devstral, Magistral Small, and more
Check the [model database](https://dash.anyapi.ai/?page=model-hub) for the full list of models with web search support.
## Pricing
Web search incurs an additional per-search cost on top of regular token pricing. The exact cost depends on the model and provider.
## Pro Tips
1. **Match context to your needs:** Quick questions = `"low"`, research = `"high"`
2. **Be specific in your prompts:** "Tesla Q4 2025 earnings" beats "stock market"
3. **Use with reasoning models:** Models like o3, DeepSeek R1, and Claude with extended thinking can reason over web results for deeper analysis
4. **Use Perplexity for research:** Sonar models are built for search and return citations by default
## Things to Keep in Mind
* **Slight latency:** Web search adds a moment to response time
* **Cost scaling:** Higher `search_context_size` uses more tokens and increases cost
* **Rate limits:** Standard rate limits apply to requests with web search
***
*Give your AI models internet access. Web search turns any model into a research assistant that knows what happened five minutes ago.*
# Working with Files
Source: https://docs.anyapi.ai/features/working-with-files
Send PDFs, documents, and audio to models through /v1/chat/completions using the file and input_audio content types
You can attach files directly to a chat completion by adding a `file` or `input_audio` content part to a message, alongside `text` and `image_url`. This works for documents (PDF, and on some models TXT, CSV, DOCX, XLSX) and for audio.
## Sending a File
Add a content part with `type: "file"` to the `content` array of a message. Provide the file either as a base64-encoded data URL in `file_data`, or as a public URL in `file_id`.
```json Base64 theme={"system"}
{
"model": "google/gemini-2.5-flash",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "Summarize this document" },
{
"type": "file",
"file": {
"file_data": "data:application/pdf;base64,JVBERi0xLjQKJ..."
}
}
]
}
]
}
```
```json Public URL theme={"system"}
{
"model": "openai/gpt-4o-mini",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "Summarize this document" },
{
"type": "file",
"file": {
"file_id": "https://example.com/report.pdf"
}
}
]
}
]
}
```
A public URL is accepted in either `file_data` or `file_id` β both fields work the same way for a URL. `file_data` is the field to use for base64-encoded data.
The response is a standard `chat.completion` object, the same shape you'd get from a text-only request β there's no separate annotations, file hash, or provider-specific metadata attached to file content.
## Supported Formats by Model
PDF input is broadly supported. Support for other document formats depends on the model:
| Format | Availability |
| ---------- | ------------------------------------------------------------------------------------- |
| PDF | Supported across models (verified on Gemini 2.5 Flash, GPT-4o-mini, Claude Haiku 4.5) |
| TXT, CSV | Supported on Gemini models |
| DOCX, XLSX | Depends on the model |
Since support varies by model and can change as providers update their backends, use [tag routing](/features/tag-routing) to guarantee your request lands on a backend that supports file input, instead of hardcoding a model:
```json theme={"system"}
{
"model": "anthropic/claude-sonnet-4",
"messages": [{ "role": "user", "content": "..." }],
"tags": ["chat_completions:pdf_input"]
}
```
## Sending Audio
Audio uses a dedicated `input_audio` content part instead of `file`. Provide base64-encoded audio data and its format:
```json theme={"system"}
{
"model": "google/gemini-2.5-pro",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "Transcribe and summarize this audio" },
{
"type": "input_audio",
"input_audio": {
"data": "base64_encoded_audio_data_here...",
"format": "wav"
}
}
]
}
]
}
```
Audio input (both `input_audio` and `type: "file"` with a `data:audio/...` data URL) is currently only supported on Gemini models. Use the `audio_input` tag to route to a backend that supports it:
```json theme={"system"}
{
"tags": ["chat_completions:audio_input"]
}
```
To have the model *generate* audio back, see the `modalities` and `audio` parameters on [Create Chat Completion](/api-reference/text/create-chat-completion).
## Choosing Models by File Capability
Query `/v1/models` and filter by the `pdf_input` or `audio_input` tags to find models that support the file type you need β see [Tag Routing](/features/tag-routing) for the full list of capability tags and how to filter or route by them.
# Zero Completion Insurance
Source: https://docs.anyapi.ai/features/zero-completion-insurance
Automatic protection from being charged for failed, empty, or error AI responses
# Never Pay for Nothing
*Automatic protection from failed AI requests*
Ever get charged for an AI request that returned absolutely nothing? Those moments when the model crashes, times out, or returns an empty response on your dime? Zero Completion Insurance has your back β automatically, completely, forever.
## The Problem We Solved
**The old way:**
* Model overloaded? That'll be \$0.15 please.
* Provider timeout? Here's your bill for zero tokens.
* Empty response? Thanks for the donation!
**The AnyAPI way:**
* Failed request? **\$0.00**
* Empty response? **\$0.00**
* Provider issues? **\$0.00**
* Zero tokens delivered? **\$0.00**
**Zero exceptions. Zero configuration. Zero tolerance for charging you for nothing.**
## How It Works
Zero Completion Insurance is **always on**. No toggles, no settings, no premium plans. Every single request is automatically protected from day one.
**The system watches for:**
* **Empty responses** β Zero completion tokens = \$0 charge
* **Error responses** β Provider errors = \$0 charge
* **Failed requests** β Crashes, timeouts, etc. = \$0 charge
* **Model failures** β Overloaded, unavailable = \$0 charge
## What's Protected (And What Isn't)
### Fully Protected
* **Completion token costs** β Zero tokens = zero charges, period
* **Failed API calls** β System errors don't cost you anything
* **Empty responses** β Blank outputs are free, as they should be
* **Provider issues** β Their problems aren't your financial problems
### Partial Protection
* **Input token processing** β You may pay for prompt processing even if completion fails
* **Successful requests** β Real responses with actual content are charged normally
* **Additional features** β Web search, file processing, etc. still apply when used
### The Logic
You pay for value delivered. No value = no payment. It's that simple.
## Working with Protected Requests
### Retry Pattern with Cost Protection
```python Python theme={"system"}
import time
import requests
def resilient_request(payload, max_retries=3):
"""Make requests with automatic retry β failures are free"""
for attempt in range(max_retries):
try:
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json=payload
)
result = response.json()
content = result.get("choices", [{}])[0].get("message", {}).get("content")
completion_tokens = result.get("usage", {}).get("completion_tokens", 0)
if content and completion_tokens > 0:
return {"success": True, "content": content}
# Empty response β protected by insurance, retry
print(f"Attempt {attempt + 1}: Empty response, retrying...")
except Exception as e:
print(f"Attempt {attempt + 1}: Failed with {e}")
if attempt < max_retries - 1:
time.sleep(2 ** attempt)
return {"success": False, "message": "All attempts failed β no charges for failures"}
result = resilient_request({
"model": "openai/gpt-5",
"messages": [{"role": "user", "content": "Explain quantum computing"}]
})
```
### Fallback Model Chain
```python Python theme={"system"}
import requests
def request_with_fallback(prompt):
"""Try multiple models β only pay for the one that succeeds"""
models = [
"openai/gpt-5",
"anthropic/claude-sonnet-4",
"google/gemini-2.5-flash"
]
for model in models:
try:
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"model": model,
"messages": [{"role": "user", "content": prompt}]
}
)
result = response.json()
content = result.get("choices", [{}])[0].get("message", {}).get("content")
if content:
return {"model": model, "content": content}
print(f"{model} returned empty β trying next (no charge)")
except Exception:
print(f"{model} failed β trying next (no charge)")
return None
```
## Pro Tips
* **Use retry logic** β Insurance covers failures, retries often succeed
* **Use fallback model chains** β Increase success rates while staying protected
* **Monitor failure patterns** β High failure rates might indicate prompt or model issues
* **Implement exponential backoff** β Give overloaded models time to recover
* **Don't fear experimentation** β Try demanding prompts without worrying about failure costs
***
*Never pay for nothing again. Zero Completion Insurance: always on, always protecting, always free.*
# FAQ
Source: https://docs.anyapi.ai/get-started/faq
Find answers to the most commonly asked questions about our platform, API, and services
## Getting Started
AnyAPI provides a unified API interface to access multiple AI models and services through a single endpoint. We offer automatic failover, intelligent routing, and cost optimization across hundreds of different AI models and providers, making it easy to integrate AI capabilities into your applications.
* **Unified Interface**: Access all major AI models through one consistent API
* **Automatic Fallbacks**: Built-in redundancy ensures high availability
* **Cost Optimization**: Intelligent routing to find the most cost-effective options
* **Simplified Billing**: One account, one bill for all your AI model usage
* **Better Uptime**: Pooled provider reliability for superior service availability
1. Create an account at [anyapi.ai](https://anyapi.ai)
2. Generate API keys from your dashboard
3. Start making requests using our quickstart guide
4. Monitor your usage through the analytics dashboard
We offer multiple support channels:
* **Discord Community**: Join our [Discord server](https://discord.gg/rJgyzGynHw) for real-time help
* **Email Support**: Contact [support@anyapi.ai](mailto:support@anyapi.ai) for technical issues
* **Documentation**: Browse our comprehensive docs
* **Community Forums**: Visit our community forums for user discussions
For the fastest response, try our Discord #help channel where community members and staff are active.
## Models and Providers
AnyAPI provides access to a wide variety of AI models:
* **Language Models**: GPT, Claude, Gemini, Llama, and more
* **Vision Models**: Image understanding and generation
* **Audio Models**: Speech-to-text and text-to-speech
* **Specialized Models**: Embeddings, code generation, reasoning
View all available models
Fetch the complete models list programmatically
We continuously add new models as they become available. We maintain partnerships with major AI labs to offer cutting-edge models as soon as they're released.
Want to see a specific model? Let us know on [Discord](https://discord.gg/rJgyzGynHw) and we'll prioritize it.
## API Technical Specifications
AnyAPI supports multiple authentication methods:
```bash Bearer Token theme={"system"}
curl https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer your_api_key"
```
```javascript OAuth 2.0 theme={"system"}
const token = await getOAuthToken();
const response = await fetch('/v1/chat/completions', {
headers: { 'Authorization': `Bearer ${token}` }
});
```
* **API Keys**: Bearer token authentication for programmatic access
* **OAuth 2.0**: Standard OAuth flow for third-party integrations
* **Session Auth**: Cookie-based authentication for web interfaces
* **Webhook Auth**: HMAC-based verification for webhook endpoints
We support multiple input formats:
* **Text**: UTF-8 encoded text in various formats
* **Images**: JPEG, PNG, WebP (via URL or base64)
* **Audio**: MP3, WAV, FLAC, M4A files
* **Documents**: PDF, and depending on the model, TXT, CSV, DOCX, and XLSX
* **Structured Data**: JSON, CSV, XML
For best performance, use URLs for large files instead of base64 encoding. See [Working with files](/features/working-with-files) for supported formats by model and request examples.
Enable real-time streaming responses:
```python Python theme={"system"}
import openai
client = openai.OpenAI(
api_key="ANYAPI_API_KEY",
base_url="https://api.anyapi.ai/v1"
)
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}],
stream=True
)
for chunk in stream:
print(chunk.choices[0].delta.content, end="")
```
```javascript JavaScript theme={"system"}
const response = await fetch('/v1/chat/completions', {
method: 'POST',
headers: { 'Authorization': 'Bearer your_api_key' },
body: JSON.stringify({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }],
stream: true
})
});
const reader = response.body.getReader();
// Process streaming chunks...
```
We use Server-Sent Events (SSE) for efficient real-time token delivery.
## Pricing
It depends on your plan:
* **Free**: Your 100K daily β³nytokens reset every 24 hours. Unused tokens do not carry over to the next day.
* **Developer / Pro / Enterprise**: Your β³nytokens are accrued at the beginning of each monthly billing cycle. Unused tokens from the previous period **carry over** and are added to your new monthly allocation.
* **Pay-as-you-go**: Purchased β³nytokens **never expire**. Your balance persists indefinitely until used.
When changing plans, any unused β³nytokens you paid for carry over to the new plan. Free tier tokens are not carried over since they are complimentary. If you cancel a paid subscription, all accumulated β³nytokens are forfeited at the end of your current billing period.
Deleted accounts cannot recover unused β³nytokens. Export your data before deletion.
We accept multiple payment methods worldwide:
* **Credit Cards**: All major cards via Stripe
* **Cryptocurrency**: USDC, USDT, Bitcoin, Ethereum
* **Bank Transfers**: Available for enterprise accounts
## Privacy and Security
**Always Logged:**
* Request metadata (timestamp, model, token counts)
* Billing information for usage tracking
* Error logs (without content)
We prioritize your privacy:
* Requests are proxied to model providers for completion only
* We negotiate zero-logging agreements when possible
* Providers with logging policies are clearly marked
* Your privacy settings control provider selection
* Non-compliant providers are filtered out automatically
Your privacy preferences are strictly enforced across all requests.
Enterprise-grade security:
* **In Transit**: TLS 1.3 encryption for all communications
* **At Rest**: AES-256 encryption for stored data
* **Access Controls**: Role-based permissions and MFA
* **Compliance**: SOC 2, GDPR, CCPA, HIPAA available
* **Auditing**: Complete audit trails for all activities
## Account Management
To delete your account contact us via email:
[support@anyapi.ai](mailto:support@anyapi.ai)
Account deletion is permanent. Unused β³nytokens cannot be recovered.
Powerful organization features:
* **Shared β³nytokens**: Pool resources across team members
* **Role Management**: Admin and User roles
* **Centralized Billing**: One account for all team usage
* **API Key Control**: Manage and rotate keys centrally
* **Audit Logs**: Track all team member activities
Set up team access and billing
Add team members with appropriate roles
Comprehensive usage analytics:
**Real-time Metrics:**
* Request volume and response times
* Cost breakdown by model and time period
* Error rates and performance monitoring
* Token usage patterns and optimization insights
**Advanced Features:**
* Custom reporting and data exports
* Webhook notifications for thresholds
* Integration with monitoring tools
* Historical trends and forecasting
## Troubleshooting
**Authentication Errors:**
* Verify API key format: `Bearer ANYAPI_API_KEY`
* Check key permissions and expiration
* Ensure sufficient account credits
**Timeout Issues:**
* Implement exponential backoff retry logic
* Use streaming for long-running requests
Multiple support channels for different needs:
**Technical Issues:**
* Check our documentation first
* Search community forums
* Join Discord for real-time help
* Email [billing@anyapi.ai](mailto:billing@anyapi.ai)
**Billing Questions:**
* Email [support@anyapi.ai](mailto:support@anyapi.ai)
* Include account ID and transaction details
**Partnership Inquiries:**
* Contact [support@anyapi.ai](mailto:support@anyapi.ai)
* Include organization details and use case
* We'll schedule a discovery call
***
Don't see your question here? Join our [Discord community](https://discord.gg/rJgyzGynHw) or contact [support@anyapi.ai](mailto:support@anyapi.ai)
# Overview
Source: https://docs.anyapi.ai/get-started/overview
# Build intelligent applications with the worldβs most advanced AI capabilities.
Access over 400 AI models through a single, unified API.
AnyAPI simplifies AI integration by providing a single, unified interface to access hundreds of leading AI models from providers like OpenAI, Anthropic, Google, Cohere, and more.
Our intelligent routing system automatically handles model fallbacks, optimizes for cost-effectiveness, and ensures high availabilityβall through one consistent API endpoint.
## AI Model Categories
Unlock human-level language understanding and generation for any task, from writing to reasoning.
Transform your visual workflows with cutting-edge image generation and analysis capabilities.
Enable voice-powered experiences with advanced audio processing capabilities.
Power semantic search, recommendations, and clustering with high-quality text embeddings.
Ensure safety and trust at scale with AI models tuned for content moderation and policy enforcement.
Explore specialized models for code, math, tools, agents, and emerging AI capabilities.
## Popular Use Cases
Discover how developers are using AnyAPI to build intelligent applications.
Generate blog posts, social media content, product descriptions, and marketing copy at scale.
Accelerate development with AI-powered code generation, review, and documentation.
Extract insights from unstructured data, generate reports, and automate analysis workflows.
## Integration Options
Standard HTTP requests with JSON responses. Perfect for any programming language.
```bash theme={"system"}
curl -X POST "https://api.anyapi.ai/v1/chat/completions" \
-H "Authorization: Bearer $ANYAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
```
Official SDKs available for popular programming languages with built-in error handling and retries.
```python theme={"system"}
from anyapi import AnyAPI
client = AnyAPI(api_key="your-api-key")
response = client.chat.completions.create(
model="openai/gpt-4-turbo",
messages=[{"role": "user", "content": "Hello!"}]
)
```
Real-time streaming for chat applications and live data processing.
```javascript theme={"system"}
const ws = new WebSocket('wss://api.anyapi.ai/v1/stream');
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log(data.choices[0].delta.content);
};
```
## Pricing & Plans
**\$0/month**
* 100 000 β³nyTokens per day for free
* Basic and free models access
* Community support
* Free features
* No SLA
**\$19.90/month**
* 100m β³nyTokens
* All models access
* Priority support
* All Features
* 99,9+ Service Availability
**\$95.90/month**
* 500m β³nyTokens
* All models access
* Priority support
* All Features
* 99,9+ Service Availability
**\$279.90/month**
* 1.5b β³nyTokens
* All models access
* Dedicated support
* All Features
* 99,9+ Service Availability
**Custom**
**Custom β³nyTokens**
* Unlimited API calls
* All models access
* Dedicated support
* All Features
* Custom SLA
**Ask for more information**
* Write to our support team and ask for more information
## Need Help?
Comprehensive guides and tutorials
Complete API documentation
Join our Discord community
Contact our support team
# Setting Up
Source: https://docs.anyapi.ai/get-started/quick-start
Get started with AnyAPI in minutes. Create your account, get your API key, and make your first request.
# Setting Up AnyAPI
Get started with AnyAPI in just a few minutes. This guide will help you create an account, obtain your API key, and make your first successful API request.
## Create Your Account
1. Visit [AnyAPI Dashboard](https://dash.anyapi.ai)
2. Click **Sign Up** to create your free account
3. Verify your email address
4. Complete your profile setup
## Get Your API Key
Once logged in, navigate to your [dashboard](https://dash.anyapi.ai)
Go to [API Keys](https://dash.anyapi.ai/?page=api-keys) section and click **Create New Key**
Copy your API key and store it securely. You'll need this for authentication.
Keep your API key secure and never expose it in client-side code or public repositories.
## Make Your First Request
Here's how to make your first API call using different programming languages:
```bash cURL theme={"system"}
curl -X POST "https://api.anyapi.ai/v1/chat/completions" \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"messages": [
{"role": "user", "content": "Hello, world!"}
]
}'
```
```python Python theme={"system"}
import requests
headers = {
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
}
data = {
"model": "openai/gpt-4o",
"messages": [
{"role": "user", "content": "Hello, world!"}
]
}
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers=headers,
json=data
)
print(response.json())
```
```javascript JavaScript theme={"system"}
const response = await fetch('https://api.anyapi.ai/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-4o',
messages: [
{role: 'user', content: 'Hello, world!'}
]
})
});
const data = await response.json();
console.log(data);
```
## Authentication
All API requests require authentication using your API key in the Authorization header:
```
Authorization: Bearer your_api_key
```
## Rate Limits
AnyAPI implements rate limiting to ensure fair usage:
* **Free Tier**: 100 requests per minute
* **Pro Tier**: 1,000 requests per minute
* **Enterprise**: Custom limits
Rate limit information is included in response headers:
* `X-RateLimit-Limit`: Total requests allowed
* `X-RateLimit-Remaining`: Requests remaining in current window
* `X-RateLimit-Reset`: Time when the rate limit resets
## Error Handling
AnyAPI uses standard HTTP status codes and returns errors in JSON format:
```json theme={"system"}
{
"error": {
"type": "invalid_request_error",
"message": "Invalid API key provided",
"code": "invalid_api_key"
}
}
```
Common error codes:
* `401`: Invalid or missing API key
* `429`: Rate limit exceeded
* `400`: Invalid request format
* `500`: Internal server error
## Next Steps
Now that you're set up, explore what you can build:
Learn about our SDKs and libraries
Browse our complete API documentation
See example implementations
Integrate with your favorite tools
## Support
Need help? We're here for you:
* Join our [Discord community](https://discord.gg/rJgyzGynHw)
* Contact [Support](mailto:support@anyapi.ai)
# Supported SDKs
Source: https://docs.anyapi.ai/get-started/supported-sdks
Integrate AnyAPI using OpenAI-compatible SDKs in your favorite programming language
## Overview
AnyAPI is compatible with OpenAI SDKs, allowing you to seamlessly integrate our API into your applications using familiar tools and libraries. Simply configure the SDK to point to `https://api.anyapi.ai` and use your AnyAPI key for authentication.
Official OpenAI Python library
Official OpenAI Node.js library
Official OpenAI .NET library
Community Java library
Community Go library
Community Ruby library
Community PHP library
Community Rust library
***
## Python
Install the official OpenAI Python library:
```bash theme={"system"}
pip install openai
```
### Example Usage
```python theme={"system"}
from openai import OpenAI
# Initialize the client with AnyAPI endpoint
client = OpenAI(
api_key="your_anyapi_key_here",
base_url="https://api.anyapi.ai/v1"
)
# Create a chat completion
completion = client.chat.completions.create(
model="model_name",
messages=[
{"role": "user", "content": "Hi, can you help me?"}
]
)
print(completion.choices[0].message.content)
```
### Async Support
```python theme={"system"}
import asyncio
from openai import AsyncOpenAI
async def main():
client = AsyncOpenAI(
api_key="your_anyapi_key_here",
base_url="https://api.anyapi.ai/v1"
)
completion = await client.chat.completions.create(
model="model_name",
messages=[
{"role": "user", "content": "Hi, can you help me?"}
]
)
print(completion.choices[0].message.content)
asyncio.run(main())
```
***
## Node.js TypeScript
Install the official OpenAI Node.js library:
```bash theme={"system"}
npm install openai
```
### Example Usage (TypeScript)
```typescript theme={"system"}
import OpenAI from 'openai';
import type { ChatCompletion } from 'openai/resources';
const client = new OpenAI({
apiKey: 'your_anyapi_key_here',
baseURL: 'https://api.anyapi.ai/v1',
});
async function main(): Promise {
const completion: ChatCompletion = await client.chat.completions.create({
model: 'model_name',
messages: [
{ role: 'user', content: 'Hi, can you help me?' }
],
});
console.log(completion.choices[0].message.content);
}
main();
```
### Example Usage (JavaScript)
```javascript theme={"system"}
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'your_anyapi_key_here',
baseURL: 'https://api.anyapi.ai/v1',
});
async function main() {
const completion = await client.chat.completions.create({
model: 'model_name',
messages: [
{ role: 'user', content: 'Hi, can you help me?' }
],
});
console.log(completion.choices[0].message.content);
}
main();
```
***
## C# .NET
Install the official OpenAI .NET library:
```bash theme={"system"}
dotnet add package OpenAI
```
### Example Usage
```csharp theme={"system"}
using System;
using System.ClientModel;
using OpenAI;
using OpenAI.Chat;
namespace ConsoleApp;
class Program
{
static async Task Main()
{
var client = new OpenAIClient(
new ApiKeyCredential("your_anyapi_key_here"),
new OpenAIClientOptions
{
Endpoint = new Uri("https://api.anyapi.ai/v1")
}
);
var chatClient = client.GetChatClient("model_name");
var completion = await chatClient.CompleteChatAsync("Hi, can you help me?");
Console.WriteLine($"[ASSISTANT]: {completion.Value.Content[0].Text}");
}
}
```
***
## Java
Install a community OpenAI Java library. Here's an example using the popular `openai-java` library:
```xml theme={"system"}
com.theokanning.openai-gpt3-java
service
0.18.2
```
### Example Usage
```java theme={"system"}
import com.theokanning.openai.OpenAiApi;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.service.OpenAiService;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import java.time.Duration;
import java.util.List;
public class AnyAPIExample {
public static void main(String[] args) {
String apiKey = "your_anyapi_key_here";
String baseUrl = "https://api.anyapi.ai/";
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(Duration.ofMinutes(2))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.build();
OpenAiApi api = retrofit.create(OpenAiApi.class);
OpenAiService service = new OpenAiService(api);
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model("model_name")
.messages(List.of(
new ChatMessage("user", "Hi, can you help me?")
))
.build();
service.createChatCompletion(request)
.getChoices()
.forEach(choice -> {
System.out.println(choice.getMessage().getContent());
});
}
}
```
***
## Go
Install a community OpenAI Go library:
```bash theme={"system"}
go get github.com/sashabaranov/go-openai
```
### Example Usage
```go theme={"system"}
package main
import (
"context"
"fmt"
"log"
openai "github.com/sashabaranov/go-openai"
)
func main() {
config := openai.DefaultConfig("your_anyapi_key_here")
config.BaseURL = "https://api.anyapi.ai/v1"
client := openai.NewClientWithConfig(config)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "model_name",
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Hi, can you help me?",
},
},
},
)
if err != nil {
log.Fatalf("ChatCompletion error: %v", err)
}
fmt.Println(resp.Choices[0].Message.Content)
}
```
***
## Ruby
Install a community OpenAI Ruby library:
```bash theme={"system"}
gem install ruby-openai
```
### Example Usage
```ruby theme={"system"}
require 'openai'
client = OpenAI::Client.new(
access_token: 'your_anyapi_key_here',
uri_base: 'https://api.anyapi.ai/v1'
)
response = client.chat(
parameters: {
model: 'model_name',
messages: [
{ role: 'user', content: 'Hi, can you help me?' }
]
}
)
puts response.dig('choices', 0, 'message', 'content')
```
***
## PHP
Install a community OpenAI PHP library:
```bash theme={"system"}
composer require openai-php/client
```
### Example Usage
```php theme={"system"}
withApiKey('your_anyapi_key_here')
->withBaseUri('https://api.anyapi.ai/v1')
->make();
$response = $client->chat()->create([
'model' => 'model_name',
'messages' => [
['role' => 'user', 'content' => 'Hi, can you help me?'],
],
]);
echo $response->choices[0]->message->content;
```
***
## Rust
Install a community OpenAI Rust library:
```toml theme={"system"}
[dependencies]
async-openai = "0.17"
tokio = { version = "1", features = ["full"] }
```
### Example Usage
```rust theme={"system"}
use async_openai::{
types::{ChatCompletionRequestMessage, CreateChatCompletionRequestArgs},
Client, config::OpenAIConfig,
};
#[tokio::main]
async fn main() -> Result<(), Box> {
let config = OpenAIConfig::new()
.with_api_key("your_anyapi_key_here")
.with_api_base("https://api.anyapi.ai/v1");
let client = Client::with_config(config);
let request = CreateChatCompletionRequestArgs::default()
.model("model_name")
.messages(vec![
ChatCompletionRequestMessage::User(
"Hi, can you help me?".into()
)
])
.build()?;
let response = client.chat().create(request).await?;
println!("{}", response.choices[0].message.content.as_ref().unwrap());
Ok(())
}
```
***
## Additional Languages
Don't see your preferred language? AnyAPI's REST API is compatible with any HTTP client. You can also use any OpenAI-compatible library by configuring it to use `https://api.anyapi.ai/v1` as the base URL.
View the complete REST API documentation
***
## Authentication
All requests to AnyAPI require authentication using your API key. Set your API key in the SDK configuration:
```python Python theme={"system"}
client = OpenAI(
api_key="your_anyapi_key_here",
base_url="https://api.anyapi.ai/v1"
)
```
```javascript Node.js theme={"system"}
const client = new OpenAI({
apiKey: 'your_anyapi_key_here',
baseURL: 'https://api.anyapi.ai/v1',
});
```
```csharp C# theme={"system"}
var client = new OpenAIClient(
new ApiKeyCredential("your_anyapi_key_here"),
new OpenAIClientOptions
{
Endpoint = new Uri("https://api.anyapi.ai/v1")
}
);
```
Keep your API key secure and never expose it in client-side code or public repositories.
***
## Getting Help
* Join our [Discord community](https://discord.gg/rJgyzGynHw)
* Contact [Support](mailto:support@anyapi.ai)
# Authentication
Source: https://docs.anyapi.ai/guides/authentication
Learn how to authenticate your requests to AnyAPI
AnyAPI uses API keys to authenticate requests. Your API keys carry many privileges, so be sure to keep them secure! Do not share your secret API keys in publicly accessible areas such as GitHub, client-side code, and so forth.
All API requests must be made over HTTPS. Calls made over plain HTTP will fail. API requests without authentication will also fail.
## API Keys
Authentication to the API is performed via HTTP Bearer authentication. Provide your API key as a bearer token in the `Authorization` header.
You can find and manage your API keys in the [AnyAPI.ai Dashboard](https://dash.anyapi.ai/?page=api-keys). Keep your API keys secure and rotate them regularly for optimal security.
## Bearer Token Format
Include your API key in the `Authorization` header using the Bearer authentication scheme:
```
Authorization: Bearer your_api_key
```
## Basic Authentication Example
```bash curl theme={"system"}
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '
{
"model": "openai/gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Hello, world!"
}
]
}'
```
```javascript JavaScript theme={"system"}
const response = await fetch('https://api.anyapi.ai/v1/chat/completions', {
method: 'POST',
headers:
{
'Authorization': 'Bearer your_api_key',
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'openai/gpt-3.5-turbo',
messages: [
{
role: 'user',
content: 'Hello, world!'
}
]})
});
const data = await response.json();
console.log(data);
```
```python Python theme={"system"}
import requests
headers = {
'Authorization': 'Bearer your_api_key',
'Content-Type': 'application/json',
}
data = {
'model': 'openai/gpt-3.5-turbo',
'messages': [
{
'role': 'user',
'content': 'Hello, world!'
}
]
}
response = requests.post(
'https://api.anyapi.ai/v1/chat/completions',
headers=headers,
json=data)
print(response.json())
```
```php PHP theme={"system"}
'gpt-3.5-turbo',
'messages' => [
[
'role' => 'user',
'content' => 'Hello, world!'
]
]
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer your_api_key',
'Content-Type: application/json',
]);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
```
## Advanced cURL Examples
### Basic Chat Completion
```bash theme={"system"}
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
"max_tokens": 150,
"temperature": 0.7
}'
```
### Streaming Response
```bash theme={"system"}
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "Write a short story about AI"}
],
"stream": true
}' \
--no-buffer
```
### With Custom Headers (Optional)
```bash theme={"system"}
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-H "X-Request-ID: unique-request-id-123" \
-H "HTTP-Referer: https://yourapp.com" \
-H "X-Title: My AI Application" \
-d '{
"model": "anthropic/claude-3-sonnet",
"messages": [
{"role": "user", "content": "Hello from my application!"}
]
}'
```
## Error Handling
If your API key is missing or invalid, you'll receive an error response:
```bash Missing API Key theme={"system"}
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "openai/gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello"}]}'
```
```json Response theme={"system"}
{
"error": {
"message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY).",
"type": "invalid_request_error",
"code": "invalid_api_key"
}
}
```
```bash Invalid API Key theme={"system"}
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer invalid_key_here" \
-H "Content-Type: application/json" \
-d '{"model": "openai/gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello"}]}'
```
```json Response theme={"system"}
{
"error": {
"message": "Invalid API key provided.",
"type": "invalid_request_error",
"code": "invalid_api_key"
}
}
```
## Security Best Practices
**Never expose your API keys in client-side code.** API keys should only be used in server-side applications where they can be kept secure.
### Environment Variables
Store your API key in environment variables:
```bash .env theme={"system"}
ANYAPI_API_KEY=your_api_key_here
```
```javascript JavaScript theme={"system"}
// Use environment variables
const apiKey = process.env.ANYAPI_API_KEY;
const response = await fetch('https://api.anyapi.ai/v1/chat/completions', {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
// ... rest of request
});
```
```python Python theme={"system"}
import os
api_key = os.getenv('ANYAPI_API_KEY')
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json',
}
```
### Rate Limiting
Your API key has rate limits associated with it. Include proper error handling for rate limit responses:
```bash theme={"system"}
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{"model": "openai/gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello"}]}' \
-w "HTTP Status: %{http_code}\n"
```
## Testing Your Authentication
Use this simple curl command to test if your API key is working:
```bash theme={"system"}
curl -X GET https://api.anyapi.ai/v1/models \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json"
```
A successful response will return a list of available models:
```json theme={"system"}
{
"object": "list",
"data": [
{
"id": "openai/gpt-4",
"object": "model",
"created": 1687882411,
"owned_by": "openai"
},
{
"id": "openai/gpt-3.5-turbo",
"object": "model",
"created": 1677610602,
"owned_by": "openai"
}
// ... more models
]
}
```
## Next Steps
Learn how to make your first API call with AnyAPI.ai
# Errors
Source: https://docs.anyapi.ai/guides/errors
APIs break. Networks fail. Models go offline. It's not *if* you'll hit errorsβit's *when*. AnyAPI uses standard HTTP status codes and gives you structured error responses so you can handle failures gracefully instead of letting your app crash and burn.
## What Error Responses Look Like
Every error follows the same format, so you always know what you're dealing with:
```json theme={"system"}
{
"error": {
"code": 400,
"message": "Invalid request format",
"type": "bad_request",
"metadata": {
"field": "messages",
"reason": "Required field missing"
}
}
}
```
### Breaking It Down
* **code**: The HTTP status code (because standards matter)
* **message**: What actually went wrong, in human speak
* **type**: Error category for your code to handle programmatically
* **metadata**: Extra details when you need to dig deeper (optional)
## The Status Code Cheat Sheet
### 2xx: Everything's Cool
#### 200 OK
Your request worked perfectly. Pat yourself on the back.
```json theme={"system"}
{
"id": "gen-abc123",
"object": "chat.completion",
"choices": [...]
}
```
### 4xx: You Did Something Wrong (Don't Panic)
#### 400 Bad Request
Your request was malformed or had invalid parameters.
```json theme={"system"}
{
"error": {
"code": 400,
"message": "Invalid parameter 'temperature'. Must be between 0 and 2.",
"type": "bad_request",
"metadata": {
"parameter": "temperature",
"value": 3.5,
"valid_range": [0, 2]
}
}
}
```
**Why this happens:**
* Forgot required parameters
* Parameter values are out of bounds
* Your JSON is wonky
* Model name doesn't exist
#### 401 Unauthorized
Your API key is wrong, missing, or revoked.
```json theme={"system"}
{
"error": {
"code": 401,
"message": "Invalid API key provided",
"type": "authentication_error"
}
}
```
**Fix it:**
* Double-check your API key isn't typo'd
* Make sure you didn't accidentally commit a revoked key
* Verify the Authorization header format is correct
#### 402 Payment Required
You're out of credits. Time to top up.
```json theme={"system"}
{
"error": {
"code": 402,
"message": "Your account or API key has insufficient anytokens. Add more anytokens and retry the request.",
"type": "budget_exceeded",
"param": null,
}
}
```
**Fix it:**
* Add more credits to your account
* Switch to a cheaper model
* Reduce max\_tokens to lower costs
#### 403 Forbidden
Content moderation blocked your request.
```json theme={"system"}
{
"error": {
"code": 403,
"message": "Your request was flagged by content moderation",
"type": "moderation_error",
"metadata": {
"reasons": ["violence", "hate"],
"flagged_input": "the harmful content...",
"provider_name": "openai",
"model_slug": "gpt-4o"
}
}
}
```
**Fix it:**
* Rephrase your prompt to remove problematic content
* Try a different model with different moderation rules
* Contact support if this seems like a false positive
#### 408 Request Timeout
Your request took too long and we gave up.
```json theme={"system"}
{
"error": {
"code": 408,
"message": "Request timed out after 120 seconds",
"type": "timeout_error",
"metadata": {
"timeout_seconds": 120
}
}
}
```
**Fix it:**
* Lower your max\_tokens setting
* Simplify your prompt
* Try again (sometimes it just works the second time)
* Use streaming to get partial results
#### 422 Unprocessable Entity
Your request is valid but the model can't handle what you're asking.
```json theme={"system"}
{
"error": {
"code": 422,
"message": "Model does not support the requested feature",
"type": "unsupported_feature_error",
"metadata": {
"feature": "function_calling",
"model": "some/model-name"
}
}
}
```
#### 429 Too Many Requests
Slow down there, speed racer. You're hitting the rate limit.
```json theme={"system"}
{
"error": {
"code": 429,
"message": "Rate limit exceeded. Try again in 60 seconds.",
"type": "rate_limit_error",
"metadata": {
"retry_after": 60,
"limit_type": "requests_per_minute",
"current_usage": 1000,
"limit": 1000
}
}
}
```
**Fix it:**
* Wait for the retry\_after time (we tell you exactly how long)
* Implement exponential backoff in your code
* Upgrade to a higher tier for more requests
* Spread your requests over time
### 5xx: We Screwed Up (Sorry)
#### 500 Internal Server Error
Something went wrong on our end.
```json theme={"system"}
{
"error": {
"code": 500,
"message": "Internal server error occurred",
"type": "server_error",
"metadata": {
"request_id": "req-abc123"
}
}
}
```
#### 502 Bad Gateway
The AI model you requested is having a bad day.
```json theme={"system"}
{
"error": {
"code": 502,
"message": "Model is temporarily unavailable",
"type": "model_unavailable_error",
"metadata": {
"model": "openai/gpt-4o",
"provider_name": "openai",
"estimated_retry_after": 300
}
}
}
```
**Fix it:**
* Try a different model
* Wait a few minutes and try again
* Use our fallback models feature (it handles this automatically)
#### 503 Service Unavailable
No providers are available for your requested model.
```json theme={"system"}
{
"error": {
"code": 503,
"message": "No available providers for this model",
"type": "no_providers_error",
"metadata": {
"model": "custom/fine-tuned-model",
"checked_providers": ["provider1", "provider2"]
}
}
}
```
## Smart Error Handling Patterns
### 1. Exponential Backoff (The Right Way)
```python theme={"system"}
import time
import random
def make_bulletproof_request(request_func, max_retries=5):
"""Make requests that don't give up too easily"""
for attempt in range(max_retries):
try:
response = request_func()
if response.status_code == 200:
return response.json()
elif response.status_code == 429:
# Rate limited - respect the retry_after
retry_after = response.json().get('error', {}).get('metadata', {}).get('retry_after', 60)
print(f"Rate limited. Chilling for {retry_after} seconds...")
time.sleep(retry_after + random.uniform(1, 5))
elif response.status_code >= 500:
# Server error - back off exponentially
wait_time = (2 ** attempt) + random.uniform(0, 1)
print(f"Server error. Waiting {wait_time:.1f}s before retry {attempt + 1}")
time.sleep(wait_time)
else:
# Client error - don't retry, just fail
raise Exception(f"API error: {response.json()}")
except Exception as e:
if attempt == max_retries - 1:
raise e
time.sleep(2 ** attempt)
```
### 2. Handle Specific Errors Like a Pro
```javascript theme={"system"}
async function handleApiResponse(response) {
const data = await response.json();
if (!response.ok) {
const error = data.error;
switch (error.code) {
case 400:
throw new ValidationError(error.message, error.metadata);
case 401:
throw new AuthenticationError("Check your API key, buddy");
case 402:
throw new InsufficientCreditsError("Time to add more credits!", error.metadata);
case 403:
throw new ModerationError("Content got flagged", error.metadata);
case 429:
const retryAfter = error.metadata?.retry_after || 60;
throw new RateLimitError(`Slow down! Try again in ${retryAfter}s`, retryAfter);
case 502:
throw new ModelUnavailableError("Model is taking a nap", error.metadata);
default:
throw new ApiError(`Something went wrong: ${error.message}`, error.code);
}
}
return data;
}
```
### 3. Smart Fallback Models
```python theme={"system"}
async def generate_with_fallback(prompt, models=None):
"""Try multiple models until one works"""
if models is None:
models = [
"openai/gpt-4o", # First choice
"anthropic/claude-3-sonnet", # Solid backup
"google/gemini-pro" # Emergency option
]
for i, model in enumerate(models):
try:
response = await client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
if i > 0:
print(f"Fell back to {model} after {i} failures")
return response
except Exception as e:
if e.status_code in [502, 503]:
# Model unavailable, try next one
print(f"{model} is down, trying next fallback...")
continue
else:
# Other error, don't bother with fallbacks
raise e
raise Exception("All fallback models failed. The AI apocalypse has begun.")
```
## Provider-Specific Errors
Sometimes you get the raw error from the AI provider:
```json theme={"system"}
{
"error": {
"code": 400,
"message": "Invalid request to provider",
"type": "provider_error",
"metadata": {
"provider_name": "openai",
"raw": {
"error": {
"message": "The model `gpt-5` does not exist",
"type": "invalid_request_error",
"param": "model",
"code": "model_not_found"
}
}
}
}
}
```
## Logging & Monitoring (Because Debugging Sucks)
### Track Request IDs
Every error includes a request ID. Use it to track down issues:
```python theme={"system"}
import logging
def log_api_error(response):
"""Log errors with all the juicy details"""
error_data = response.json()
request_id = error_data.get('error', {}).get('metadata', {}).get('request_id', 'unknown')
logging.error(
f"API Error {response.status_code}: {error_data['error']['message']} "
f"(Request ID: {request_id}) - Now you can actually debug this!"
)
```
### Monitor Error Patterns
```javascript theme={"system"}
const errorStats = {
rateLimits: 0,
modelDown: 0,
authFails: 0,
clientErrors: 0,
serverErrors: 0
};
function trackError(errorCode) {
// Keep tabs on what's breaking
if (errorCode === 429) errorStats.rateLimits++;
else if (errorCode === 502) errorStats.modelDown++;
else if (errorCode === 401) errorStats.authFails++;
else if (errorCode >= 400 && errorCode < 500) errorStats.clientErrors++;
else if (errorCode >= 500) errorStats.serverErrors++;
// Alert when things get weird
if (errorStats.rateLimits > 100) {
alert("You're hitting rate limits a lot. Maybe upgrade your plan?");
}
}
```
## Testing Your Error Handling
```python theme={"system"}
def test_error_handling():
"""Make sure your error handling actually works"""
# Test bad auth
try:
client = AnyAPIClient(api_key="totally-fake-key")
response = client.chat.completions.create(...)
assert False, "This should have failed!"
except AuthenticationError:
print("β
Auth error handling works")
# Test rate limiting (carefully!)
try:
for i in range(1000): # Don't actually do this
response = client.chat.completions.create(...)
except RateLimitError as e:
print(f"β
Rate limit handling works (retry after {e.retry_after}s)")
```
## Pro Tips for Error Handling
### 1. Graceful Degradation
Always have a Plan B when the API is down:
```python theme={"system"}
def generate_text(prompt):
try:
return anyapi_generate(prompt)
except ApiError:
# Fall back to cached responses, simpler logic, or user notification
return "Sorry, AI is taking a break. Try again in a few minutes!"
```
### 2. User-Friendly Error Messages
Don't show users raw error codes:
```python theme={"system"}
def user_friendly_error(error):
"""Turn technical errors into messages humans can understand"""
friendly_messages = {
401: "Looks like there's an issue with your account. Please contact support.",
402: "You've reached your usage limit. Time to upgrade! π",
403: "That request contains content we can't process. Try rephrasing it.",
429: "Whoa there! You're making requests too quickly. Take a short break.",
500: "Our servers are having a moment. Please try again in a bit.",
502: "The AI model is temporarily unavailable. We're on it!"
}
return friendly_messages.get(error.code, "Something unexpected happened. Our team has been notified.")
```
### 3. Smart Timeouts
Set timeouts that match your use case:
```python theme={"system"}
# Real-time chat: fail fast
timeout = 10 # seconds
# Batch processing: be patient
timeout = 300 # seconds
# Long-form content generation: somewhere in between
timeout = 60 # seconds
```
### 4. Circuit Breaker Pattern
Stop hammering broken services:
```python theme={"system"}
class CircuitBreaker:
"""Protect your app from repeatedly calling broken services"""
def __init__(self, failure_threshold=5, timeout=60):
self.failure_threshold = failure_threshold
self.timeout = timeout
self.failure_count = 0
self.last_failure_time = None
self.state = 'CLOSED' # CLOSED (normal), OPEN (broken), HALF_OPEN (testing)
def call(self, func):
if self.state == 'OPEN':
if time.time() - self.last_failure_time > self.timeout:
self.state = 'HALF_OPEN'
print("Circuit breaker: Attempting to reconnect...")
else:
raise CircuitOpenError("Service is still broken, not trying again yet")
try:
result = func()
self.on_success()
return result
except Exception as e:
self.on_failure()
raise e
def on_success(self):
self.failure_count = 0
self.state = 'CLOSED'
def on_failure(self):
self.failure_count += 1
self.last_failure_time = time.time()
if self.failure_count >= self.failure_threshold:
self.state = 'OPEN'
print(f"Circuit breaker: Service marked as broken after {self.failure_count} failures")
```
## Monitor These Metrics
Set up alerts for:
* **Error rate spikes** - Something's going wrong
* **Specific error patterns** - Identify systemic issues
* **Model availability** - Know when your preferred models are down
* **Rate limit proximity** - Upgrade before you hit the wall
***
*Remember: Good error handling is invisible to users but saves your sanity. Handle errors gracefully, log everything you need for debugging, and always have a fallback plan.*
# Model Parameters
Source: https://docs.anyapi.ai/guides/parameters
Complete reference for all model parameters available in AnyChat and API requests.
Fine-tune your model's behavior with these parameters, available in the **Model Parameters** panel in AnyChat or passed directly in your API request body.
***
## Request Basics
### `messages`
**Type:** `array` Β· **Required:** Yes (for chat models)
Your conversation history. The model reads all previous messages to understand context.
```json theme={"system"}
{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi there! How can I help you?"},
{"role": "user", "content": "What's the weather like?"}
]
}
```
**Message roles:**
| Role | Purpose |
| ----------- | ----------------------------------------- |
| `system` | Sets the model's personality and behavior |
| `user` | The human's input |
| `assistant` | The model's previous response |
| `tool` | Results from function/tool calls |
***
### `prompt`
**Type:** `string` Β· **Required:** Yes (for text completion models)
Raw text for the model to continue. Used with legacy completion endpoints.
```json theme={"system"}
{
"prompt": "The future of artificial intelligence is"
}
```
***
### `model`
**Type:** `string` Β· **Required:** Optional (defaults to your account setting)
Which model to use for the request.
```json theme={"system"}
{
"model": "openai/gpt-4o"
}
```
***
## Output Control
### `temperature`
**Default:** `1` Β· **Range:** `0β2`
Controls the randomness of responses. Lower values produce focused, deterministic output; higher values make responses more varied and creative.
* Use `0` for factual tasks, data extraction, structured output
* Use `0.7β1.0` for general-purpose tasks
* Use `1.2+` for creative writing, brainstorming
```json theme={"system"}
{ "temperature": 0.7 }
```
***
### `top_p`
**Default:** `1` Β· **Range:** `0β1`
Nucleus sampling β limits the model's token selection to the top cumulative probability mass. `0.9` means only tokens covering 90% of the probability distribution are considered.
Avoid changing both `temperature` and `top_p` at the same time. Adjust one or the other.
```json theme={"system"}
{ "top_p": 0.9 }
```
***
### `top_k`
**Default:** Model-specific Β· **Range:** `[1, β)`
Limits how many top tokens the model considers at each step. Supported by Anthropic, Google, and most open-source models.
OpenAI models do not support `top_k`.
```json theme={"system"}
{ "top_k": 40 }
```
***
### `n`
**Default:** `1`
Number of response variants to generate per request. Useful for comparing alternatives.
Cost scales linearly with `n`. Setting `n: 3` triples the token cost.
```json theme={"system"}
{ "n": 3 }
```
***
### `stream`
**Default:** `true`
Enables streaming β tokens are returned as they are generated rather than waiting for the full response.
* `true` β lower perceived latency, better UX for chat interfaces
* `false` β full response returned as a single object
```json theme={"system"}
{ "stream": true }
```
***
### `stream_options`
**Default:** `null`
Additional streaming configuration. Primarily used to include token usage statistics in the final chunk.
```json theme={"system"}
{ "stream_options": { "include_usage": true } }
```
***
## Length Control
### `max_tokens`
**Default:** `null`
Maximum number of tokens in the response. When `null`, uses the model's default limit. Setting this explicitly helps control costs and prevent runaway outputs.
```json theme={"system"}
{ "max_tokens": 2048 }
```
***
### `max_completion_tokens`
**Default:** `null`
OpenAI o-series equivalent of `max_tokens`. Covers both reasoning tokens and visible output tokens. Use this instead of `max_tokens` for `o1`, `o3`, and similar reasoning models.
```json theme={"system"}
{ "max_completion_tokens": 4096 }
```
***
### `stop`
**Default:** `null`
Stop sequences β the model halts generation when it produces this string or any string in the array.
```json theme={"system"}
{ "stop": ["###", "\n\n"] }
```
***
## Repetition Penalties
### `frequency_penalty`
**Default:** `0` Β· **Range:** `-2` to `2`
Penalizes tokens proportionally to how often they've already appeared. Positive values reduce repetition of the same words and phrases. Useful for long-form content generation.
```json theme={"system"}
{ "frequency_penalty": 0.5 }
```
***
### `presence_penalty`
**Default:** `0` Β· **Range:** `-2` to `2`
Penalizes tokens that have appeared at all, regardless of frequency. Encourages the model to introduce new topics. Useful for open-ended creative tasks.
```json theme={"system"}
{ "presence_penalty": 0.4 }
```
***
### `repetition_penalty`
**Default:** `1.0` Β· **Range:** `(0, 2]`
Alternative repetition control used by many open-source models (Llama, Mistral, etc.). Values above `1.0` discourage repetition, below `1.0` encourage it.
This serves a similar purpose to `frequency_penalty` but is used by different model families. OpenAI models do not support this parameter.
```json theme={"system"}
{ "repetition_penalty": 1.1 }
```
***
## Reproducibility
### `seed`
**Default:** `null`
When set, makes responses deterministic β identical inputs with the same seed produce the same output (or very close to it). Ideal for testing, A/B comparisons, and debugging prompts.
```json theme={"system"}
{ "seed": 42 }
```
***
## Tools & Function Calling
### `tools`
**Default:** `null`
Array of tool definitions the model can call. Each tool specifies a `name`, `description`, and `parameters` schema.
```json theme={"system"}
{
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"]
}
}
}
]
}
```
***
### `tool_choice`
**Default:** `"auto"`
Controls whether and how the model calls tools.
| Value | Behavior |
| --------------------------------------------------- | ------------------------------------ |
| `"auto"` | Model decides whether to call a tool |
| `"required"` | Model must call at least one tool |
| `"none"` | Model never calls tools |
| `{"type": "function", "function": {"name": "..."}}` | Forces a specific tool |
***
### `parallel_tool_calls`
**Default:** `true`
Allows the model to call multiple tools simultaneously in a single response. Disable if your tools have sequential dependencies or shared state.
```json theme={"system"}
{ "parallel_tool_calls": false }
```
***
### `function_call` / `functions`
**Deprecated**
Legacy OpenAI function calling format. Use `tools` and `tool_choice` instead.
***
## Token Probabilities
### `logprobs`
**Default:** `false`
Returns the log-probabilities of output tokens. Useful for confidence analysis, classification tasks, and model calibration.
```json theme={"system"}
{ "logprobs": true }
```
***
### `top_logprobs`
**Default:** `null` Β· **Range:** `1β20`
Number of alternative tokens (with their probabilities) to return at each position. Requires `logprobs: true`.
```json theme={"system"}
{ "logprobs": true, "top_logprobs": 5 }
```
***
### `logit_bias`
**Default:** `null`
Map of `{token_id: bias}` to increase or decrease the likelihood of specific tokens. Range: `-100` (ban) to `100` (force). Rarely needed, but useful for restricting vocabulary or enforcing specific output formats.
```json theme={"system"}
{ "logit_bias": { "50256": -100 } }
```
***
## Multimodality
### `modalities`
**Default:** `null`
Output types to request. Use `["text", "audio"]` for models that support audio output (e.g., `gpt-4o-audio`).
```json theme={"system"}
{ "modalities": ["text", "audio"] }
```
***
### `audio`
**Default:** `null`
Audio output configuration. Works in conjunction with `modalities: ["text", "audio"]`.
```json theme={"system"}
{
"audio": {
"voice": "alloy",
"format": "mp3"
}
}
```
***
### `prediction`
**Default:** `null`
Predicted outputs β provide the expected response in advance. The model confirms or corrects it, which can reduce latency and cost. Most effective for editing tasks where much of the text remains unchanged.
```json theme={"system"}
{
"prediction": {
"type": "content",
"content": "The corrected version of the text..."
}
}
```
***
## Reasoning
### `reasoning_effort`
**Default:** `"medium"`
Controls the depth of reasoning for models that support chain-of-thought (e.g., `o1`, `o3`, Claude with extended thinking). Higher effort = more thinking tokens = better quality at higher cost.
| Value | Use when |
| ---------- | ------------------------------------------ |
| `"low"` | Speed and cost are the priority |
| `"medium"` | Balanced performance (recommended default) |
| `"high"` | Maximum accuracy for complex tasks |
```json theme={"system"}
{ "reasoning_effort": "high" }
```
***
### `thinking`
**Default:** `null`
Native Anthropic parameter for Claude models with extended thinking. Enables a reasoning phase with a configurable token budget.
```json theme={"system"}
{
"thinking": {
"type": "enabled",
"budget_tokens": 10000
}
}
```
***
## Prompt Caching
### `prompt_cache_key`
**Default:** `null`
Explicit key for managing prompt cache. Requests sharing the same key and prefix reuse the cached computation, reducing cost on repeated calls with identical system prompts or context.
```json theme={"system"}
{ "prompt_cache_key": "my-system-prompt-v1" }
```
***
### `prompt_cache_retention`
**Default:** `null`
TTL (in seconds) for how long the cached prompt prefix is retained. Useful for controlling cache lifecycle on high-frequency workloads.
```json theme={"system"}
{ "prompt_cache_retention": 3600 }
```
***
## Response Format
### `response_format`
**Default:** `null`
Forces structured output. Use `json_object` for any valid JSON, or `json_schema` to enforce a specific schema.
```json theme={"system"}
{ "response_format": { "type": "json_object" } }
```
```json theme={"system"}
{
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "my_schema",
"schema": {
"type": "object",
"properties": {
"answer": { "type": "string" }
},
"required": ["answer"]
}
}
}
}
```
***
## Storage & Metadata
### `store`
**Default:** `null`
Whether to save the request for later use (fine-tuning, evals) on the provider's side. Supported by OpenAI.
```json theme={"system"}
{ "store": true }
```
***
### `metadata`
**Default:** `null`
Arbitrary tags attached to the request. Useful for filtering in logs and analytics dashboards.
```json theme={"system"}
{
"metadata": {
"tags": ["chat_completions:reasoning"]
}
}
```
***
## Infrastructure & Reliability
### `service_tier`
**Default:** `null`
Request processing priority at the provider level. OpenAI supports `"flex"` for cheaper async processing with relaxed latency requirements.
```json theme={"system"}
{ "service_tier": "flex" }
```
***
### `max_retries`
**Default:** `null`
Number of automatic retries on failure. Useful when working with less stable providers or during high-traffic periods.
```json theme={"system"}
{ "max_retries": 3 }
```
***
### `extra_headers`
**Default:** `null`
Additional HTTP headers passed to the provider. Used for provider-specific features not covered by standard parameters.
```json theme={"system"}
{
"extra_headers": {
"X-Custom-Header": "value"
}
}
```
***
### `safety_identifier`
**Default:** `null`
Identifier for provider-side safety systems. Relevant for specific enterprise integrations that require request tagging for moderation pipelines.
***
### `web_search_options`
**Default:** `null`
Configuration for built-in [web search](/features/web-search). Available on 800+ models across all major providers.
```json theme={"system"}
{ "web_search_options": { "search_context_size": "medium" } }
```
***
## AnyAPI-Specific Parameters
### `transforms`
**Default:** `[]`
Apply smart transformations to your messages before sending them to models.
```json theme={"system"}
{ "transforms": ["middle-out"] }
```
**Available transforms:**
* `"middle-out"` β rearranges messages for better context utilization, especially useful for long conversations
***
### `models`
**Default:** `null`
Specify fallback models in order of preference. If the first model fails or is unavailable, the next one is tried.
```json theme={"system"}
{
"models": [
"openai/gpt-4o",
"anthropic/claude-sonnet-4-6",
"google/gemini-2.5-pro"
]
}
```
***
### `provider`
**Default:** `null`
Fine-tune provider selection and behavior.
```json theme={"system"}
{
"provider": {
"order": ["openai", "anthropic"],
"allow_fallbacks": true,
"data_collection": "deny"
}
}
```
| Field | Description |
| ----------------- | ---------------------------------------------- |
| `order` | Preferred provider order |
| `allow_fallbacks` | Enable automatic fallbacks |
| `data_collection` | Opt out of provider data collection (`"deny"`) |
***
## Model-Specific Behavior
Different model families support different parameter sets:
| Parameter | OpenAI | Anthropic | Google | Open Source |
| -------------------- | ------ | --------- | ------ | ----------- |
| `temperature` | Yes | Yes | Yes | Yes |
| `top_p` | Yes | Yes | Yes | Yes |
| `top_k` | No | Yes | Yes | Most |
| `frequency_penalty` | Yes | No | Yes | Some |
| `presence_penalty` | Yes | No | Yes | Some |
| `repetition_penalty` | No | Yes | No | Most |
| `response_format` | Yes | Yes | Yes | Varies |
Models silently ignore parameters they don't support β you can safely use the same parameter set across different models.
***
## Parameter Recipes
### Creative Writing
```json theme={"system"}
{
"model": "openai/gpt-4o",
"messages": [{"role": "user", "content": "Write a short story about a time-traveling chef"}],
"temperature": 1.2,
"max_tokens": 2000,
"presence_penalty": 0.3
}
```
### Code Generation
```json theme={"system"}
{
"model": "anthropic/claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Write a Python function to sort a list"}],
"temperature": 0.1,
"max_tokens": 1000
}
```
### Data Extraction
```json theme={"system"}
{
"model": "openai/gpt-4o",
"messages": [{"role": "user", "content": "Extract contact info from this text as JSON"}],
"temperature": 0,
"response_format": {"type": "json_object"},
"max_tokens": 500
}
```
### Conversational AI
```json theme={"system"}
{
"model": "anthropic/claude-sonnet-4-6",
"messages": [
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Help me debug this code"}
],
"temperature": 0.7,
"max_tokens": 1000,
"stream": true
}
```
***
## Parameter Validation
### Unknown parameters
Models ignore parameters they don't understand. You can use the same parameter set across different models without errors.
### Out-of-range values
AnyAPI clamps obviously invalid values:
* `temperature: 3.0` β `temperature: 2.0`
* `top_p: 1.5` β `top_p: 1.0`
### Wrong types
Type mismatches cause errors:
* `max_tokens: "100"` β should be a number
* `stream: "true"` β should be boolean `true`
# Quick Start
Source: https://docs.anyapi.ai/guides/quick-start
Get started with AnyAPI.ai in under 5 minutes
## Overview
This guide will walk you through creating your first API integration using AnyAPI.ai.
**Prerequisites**: You'll need an AnyAPI.ai account. [Sign up for free](https://anyapi.ai) if you haven't already.
## Sign Up and Get Your API Key
Navigate to your [AnyAPI.ai dashboard](https://dash.anyapi.ai)
Click on **Settings** β **API Keys** in the sidebar
Click **"Create API Key"** and give it a descriptive name like "My First Integration"
Copy the generated API key and store it securely
Keep your API key secure! Never commit it to version control or expose it in client-side code.
## π Congratulations!
You've successfully:
β
Created your first API connection\
β
Built a simple workflow\
β
Executed the workflow and received data
## Next Steps
Set up secure authentication for popular APIs
Explore all available endpoints and parameters
## Interactive Playground
Want to test this right now? Use our interactive playground below:
The playground will use your actual API key if you're logged into the dashboard. Make sure you're using a test environment!
## Troubleshooting
Make sure your API key is correct and includes the `Bearer ` prefix in the Authorization header.
Verify the base URL is correct and the target API is accessible. You can test the connection using the `/connections/{id}/test` endpoint.
Check the workflow configuration and ensure the connection ID is valid. Review the error details in the response.
If you're hitting rate limits, consider upgrading your plan or implementing delays between requests.
## Need Help?
* Join our [Discord community](https://discord.gg/rJgyzGynHw)
* Contact [Support](mailto:support@anyapi.ai)
# Hermes Agent
Source: https://docs.anyapi.ai/integrations/hermes
Connect AnyAPI as a custom provider in Hermes Agent for multi-model access
# Hermes Agent
[Hermes Agent](https://github.com/NousResearch/hermes-agent) is an open-source, terminal-native autonomous coding and task agent by Nous Research. It features persistent memory, agent-created skills, and a messaging gateway with 21 platform adapters (Telegram, Discord, Slack, Google Chat, WhatsApp, Signal, SMS, Email, and more).
AnyAPI is not a built-in provider in Hermes Agent. You configure it as a **named custom OpenAI-compatible provider** via the `custom_providers:` list.
## Quick Start
Sign up at [anyapi.ai](https://anyapi.ai) and create an API key in the [dashboard](https://dash.anyapi.ai/?page=api-keys).
Add the key to `~/.hermes/.env` (Hermes keeps secrets separate from settings):
```bash theme={"system"}
echo 'ANYAPI_API_KEY=your-key-here' >> ~/.hermes/.env
```
```bash theme={"system"}
hermes model
```
When prompted:
1. Select **Custom endpoint (self-hosted / VLLM / etc.)**
2. Provider name: `anyapi`
3. Base URL: `https://api.anyapi.ai/v1`
4. API key env var: `ANYAPI_API_KEY`
5. Default model: `openai/gpt-4o` (or any model from the [AnyAPI catalog](https://anyapi.ai/ai-models))
```bash theme={"system"}
hermes chat
```
Your agent now routes requests through AnyAPI.
## Manual Configuration
For full control, edit the config files directly instead of using `hermes model`.
### Set the API key
`~/.hermes/.env`:
```bash theme={"system"}
ANYAPI_API_KEY=your-key-here
```
### Configure provider and model
`~/.hermes/config.yaml`:
```yaml theme={"system"}
model:
default: openai/gpt-4o
provider: custom:anyapi
base_url: https://api.anyapi.ai/v1
custom_providers:
- name: anyapi
base_url: https://api.anyapi.ai/v1
key_env: ANYAPI_API_KEY
api_mode: chat_completions
```
### Verify
```bash theme={"system"}
hermes config show
```
## Model Format
AnyAPI uses the `vendor/model` format. Always include the vendor prefix:
```yaml theme={"system"}
model:
provider: custom:anyapi
default: openai/gpt-4o # correct
# default: gpt-4 # wrong β missing vendor prefix
```
Browse all available models at [anyapi.ai/ai-models](https://anyapi.ai/ai-models) or query the API:
```bash theme={"system"}
curl -sS -H "Authorization: Bearer $ANYAPI_API_KEY" \
https://api.anyapi.ai/v1/models | jq -r '.data[].id'
```
## Advanced Configuration
Set the context length for the default model:
```yaml theme={"system"}
model:
default: openai/gpt-4o
context_length: 128000
```
For per-model overrides:
```yaml theme={"system"}
custom_providers:
- name: anyapi
base_url: https://api.anyapi.ai/v1
key_env: ANYAPI_API_KEY
models:
openai/gpt-4o:
context_length: 128000
google/gemini-2.5-pro:
context_length: 1000000
```
Hermes runs side LLM tasks (vision, compression, title generation, etc.) and lets you override the model per task:
```yaml theme={"system"}
auxiliary:
vision:
provider: custom:anyapi
model: openai/gpt-4o
compression:
provider: custom:anyapi
model: openai/gpt-4o-mini
title_generation:
provider: custom:anyapi
model: openai/gpt-4o-mini
```
Tasks not overridden inherit from the main `model:` block.
A list of backups Hermes tries when the primary model fails:
```yaml theme={"system"}
fallback_providers:
- provider: custom:anyapi
model: openai/gpt-4o
- provider: custom:anyapi
model: openai/gpt-4o-mini
```
AnyAPI also exposes Anthropic's `/v1/messages` format for Claude models:
```yaml theme={"system"}
model:
default: anthropic/claude-sonnet-4.6
provider: custom:anyapi-anthropic
custom_providers:
- name: anyapi-anthropic
base_url: https://api.anyapi.ai/v1
key_env: ANYAPI_API_KEY
api_mode: anthropic_messages
```
## Configuration Examples
```yaml theme={"system"}
model:
default: openai/gpt-4o
provider: custom:anyapi
base_url: https://api.anyapi.ai/v1
custom_providers:
- name: anyapi
base_url: https://api.anyapi.ai/v1
key_env: ANYAPI_API_KEY
```
```yaml theme={"system"}
model:
default: google/gemini-2.5-pro
provider: custom:anyapi
base_url: https://api.anyapi.ai/v1
custom_providers:
- name: anyapi
base_url: https://api.anyapi.ai/v1
key_env: ANYAPI_API_KEY
```
```yaml theme={"system"}
model:
default: openai/gpt-4o
provider: custom:anyapi
base_url: https://api.anyapi.ai/v1
custom_providers:
- name: anyapi
base_url: https://api.anyapi.ai/v1
key_env: ANYAPI_API_KEY
api_mode: chat_completions
auxiliary:
compression:
provider: custom:anyapi
model: openai/gpt-4o-mini
title_generation:
provider: custom:anyapi
model: openai/gpt-4o-mini
fallback_providers:
- provider: custom:anyapi
model: anthropic/claude-sonnet-4.6
- provider: custom:anyapi
model: openai/gpt-4o-mini
```
## Gateway Mode
When running Hermes as a messaging gateway (Telegram, Discord, Slack, etc.), the configuration is identical. Restart the service after changes:
```bash theme={"system"}
hermes gateway restart
```
## Troubleshooting
Hermes can't find your AnyAPI key.
1. Verify the env file: `grep ANYAPI ~/.hermes/.env`
2. Re-run the wizard: `hermes model` β Custom endpoint
3. Confirm YAML uses `key_env: ANYAPI_API_KEY` (or the alias `api_key_env:`)
Your key is invalid, revoked, or has no credits.
1. Validate the key at [dash.anyapi.ai](https://dash.anyapi.ai)
2. Check your balance/quota
3. Reissue the key if needed
The model ID is unknown or missing the vendor prefix.
1. List the live catalog: `curl -H "Authorization: Bearer $ANYAPI_API_KEY" https://api.anyapi.ai/v1/models`
2. Use `vendor/model` form (e.g. `openai/gpt-4o`, not bare `gpt-4o`)
The endpoint must be exactly `https://api.anyapi.ai/v1` β no trailing slash, no `/chat/completions` suffix (Hermes appends that automatically).
If Hermes warns about unknown YAML keys, check that you are using:
* `custom_providers:` (plural list), not `custom_provider:`
* `auxiliary..provider`, not `auxiliary_providers:`
* `model.context_length`, not `context_limits:`
Check usage at [dash.anyapi.ai](https://dash.anyapi.ai), upgrade your plan, or route side tasks to cheaper models via `auxiliary.*`.
## Best Practices
1. **Keep credentials in `.env`, never in `config.yaml`**
2. **Configure fallbacks** β AnyAPI gives you many providers behind one key, use them for resilience
3. **Route side tasks to small models** β use `auxiliary.*` with cheaper models like `openai/gpt-4o-mini`
4. **Always use `vendor/model` format** β bare model names will not resolve
5. **Monitor usage** at [dash.anyapi.ai](https://dash.anyapi.ai)
## Support
* **AnyAPI Dashboard**: [dash.anyapi.ai](https://dash.anyapi.ai)
* **AnyAPI Discord**: [Discord community](https://discord.gg/rJgyzGynHw)
* **AnyAPI Email**: [support@anyapi.ai](mailto:support@anyapi.ai)
* **Hermes Agent Issues**: [github.com/NousResearch/hermes-agent/issues](https://github.com/NousResearch/hermes-agent/issues)
# n8n Integration
Source: https://docs.anyapi.ai/integrations/n8n
Automate AI workflows with AnyAPI and n8n's visual workflow builder
# n8n Integration
n8n is a workflow automation platform that lets you connect AI models with hundreds of apps and services. The AnyAPI community node gives you direct access to 300+ AI models inside n8n β no code required.
## Overview
The AnyAPI node for n8n supports:
Send messages to GPT-4o, Claude, Gemini, Llama, and more
Ground model responses with real-time web search
Create images with DALL-E, Stable Diffusion, and others
Generate text embeddings for semantic search and RAG
### Advanced Capabilities
* **Function Calling** β Let models call external tools and APIs
* **Reasoning** β Extended thinking for complex tasks (o1, Claude with thinking)
* **Prompt Caching** β Reduce latency and cost on repeated prompts
* **Streaming** β Stream responses for real-time output
* **Assistant Prefill** β Guide model output by prefilling the response
* **URL Context** β Fetch and include web page content as context
## Installation
1. Open any workflow in n8n
2. Click **+** to open the nodes panel
3. Search for **AnyAPI**
4. Click **Install** on the community node
For self-hosted n8n, you can also install via **Settings > Community Nodes** and enter `n8n-nodes-anyapi.ai`.
## Setup
### Create Credentials
1. Add an **AnyAPI** node to your workflow
2. Click **Credential to connect with** > **Create New Credential**
3. Enter your API key from the [AnyAPI Dashboard](https://dash.anyapi.ai/?page=api-keys)
4. Base URL defaults to `https://api.anyapi.ai/v1`
5. Click **Test** to verify the connection
## Quick Start
### Chat Completion
The simplest workflow β send a message and get a response:
1. Add a **Manual Trigger** node
2. Add an **AnyAPI** node and connect it
3. Set Resource to **Chat**, Operation to **Complete**
4. Choose a model (e.g. `openai/gpt-4o`)
5. Add a message with Role **User** and your prompt
6. Click **Test workflow**
### Example: Scheduled Summary
Generate a daily summary of your data on a schedule:
```
[Schedule Trigger] β [HTTP Request: fetch data] β [AnyAPI: summarize] β [Slack: post message]
```
### Example: Webhook AI Endpoint
Expose an AI-powered API endpoint:
```
[Webhook] β [AnyAPI: process request] β [Respond to Webhook]
```
## Resources
### Chat Completions
| Parameter | Description |
| --------------------- | ------------------------------------------------------------------------------------------------ |
| **Model** | Model ID (e.g. `openai/gpt-4o`, `anthropic/claude-sonnet-4-20250514`, `google/gemini-2.0-flash`) |
| **Messages** | List of messages with role (System, User, Assistant) and content |
| **Simplified Output** | Return only the message content instead of the full API response |
### Chat Options
Sampling temperature from 0 to 2. Lower values make output more focused and deterministic, higher values make it more creative.
Maximum number of tokens to generate in the response.
Enable web search grounding. The model can search the web for up-to-date information. Configure **Web Search Context Size** (Low, Medium, High) to control how much context is included.
Provide a URL to fetch and include as context. Useful for summarizing web pages or using web content as reference.
Pass a JSON array of tool definitions. The model can decide to call these tools based on the conversation.
```json theme={"system"}
[
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"]
}
}
}
]
```
Control how the model uses tools: **Auto** (model decides), **None** (no tool use), **Required** (must use a tool).
Allow the model to call multiple functions in a single turn. Enabled by default.
Enable extended thinking for complex tasks. Supported by models like `openai/o1`, `anthropic/claude-sonnet-4-20250514` with thinking. Set **Reasoning Budget** to control max tokens for thinking steps.
Cache repeated prompts to reduce latency and cost. Especially useful for system prompts and few-shot examples that don't change between requests.
Stream the response. n8n will collect the full streamed response and return it as a single output.
Start the assistant's response with specific text. The model will continue from where you left off. Supported by Anthropic models.
Force the model to output valid JSON. Useful when you need structured data for downstream nodes.
Custom trace ID for tracking requests in the [AnyAPI Dashboard](https://dash.anyapi.ai). Useful for debugging workflows.
### Embeddings
| Parameter | Description |
| -------------- | --------------------------------------------------------- |
| **Model** | Embedding model ID (e.g. `openai/text-embedding-3-small`) |
| **Input Text** | Text to generate embeddings for |
### Image Generation
| Parameter | Description |
| -------------------- | ----------------------------------------------------- |
| **Model** | Image model ID (e.g. `google/gemini-2.5-flash-image`) |
| **Prompt** | Text description of the image |
| **Size** | Image dimensions (256x256 to 1792x1024) |
| **Quality** | Standard or HD |
| **Number of Images** | How many images to generate (1-10) |
### Model Listing
Returns all available models on AnyAPI. Useful for dynamic model selection in workflows.
## Workflow Examples
### AI Content Pipeline
Automate content creation with multiple AI steps:
```
[Schedule Trigger]
β [HTTP Request: fetch topics from CMS]
β [AnyAPI: generate outline (Claude)]
β [AnyAPI: write draft (GPT-4o)]
β [AnyAPI: review and edit (Claude)]
β [Google Sheets: save results]
β [Slack: notify team]
```
### RAG with Web Search
Answer questions using both your data and the web:
```
[Webhook: receive question]
β [Pinecone: search knowledge base]
β [AnyAPI: answer with context + web search enabled]
β [Respond to Webhook]
```
### Image Generation Bot
Generate images on demand via Slack:
```
[Slack Trigger: new message in #ai-images]
β [AnyAPI: generate image from message text]
β [Slack: upload image to channel]
```
### Multi-Model Comparison
Compare responses from different models:
```
[Manual Trigger]
β [Split In Batches]
β [AnyAPI: GPT-4o]
β [AnyAPI: Claude Sonnet]
β [AnyAPI: Gemini Flash]
β [Merge]
β [Google Sheets: save comparison]
```
## Troubleshooting
### Credential Test Fails
* Verify your API key is correct at [dash.anyapi.ai](https://dash.anyapi.ai/?page=api-keys)
* Check the Base URL is `https://api.anyapi.ai/v1`
* Ensure your account has available credits
### Model Not Found
* Use the full model ID with provider prefix: `openai/gpt-4o`, not just `gpt-4o`
* Use the **Model > List** operation to see all available models
### Unsupported Feature Error
Not all models support all features. If you get an error like "unsupported tool type", try a different model. For example, web search works with OpenAI and Anthropic models but may not work with all providers.
## Next Steps
Autonomous coding agent with 21 messaging adapters
AI coding agent with multi-model fallbacks
Learn more about web search grounding
Deep dive into function calling
# OpenClaw
Source: https://docs.anyapi.ai/integrations/openclaw
Connect AnyAPI as a custom provider in OpenClaw for multi-model AI agent workflows
# OpenClaw
[OpenClaw](https://github.com/openclaw/openclaw) is an AI coding agent with support for custom OpenAI-compatible providers, fallback chains, and messaging gateway mode (Discord, Slack, Telegram, etc.).
AnyAPI is not a bundled provider in OpenClaw. You wire it in as a **custom OpenAI-compatible provider** under `models.providers`. Once registered, OpenClaw addresses models as `anyapi//` (your provider ID prefix + the upstream model ID).
OpenClaw uses **JSON5** at `~/.openclaw/openclaw.json` β unquoted keys, trailing commas, and `//` comments are all valid.
## Quick Start
The fastest path is the non-interactive onboarding wizard:
```bash theme={"system"}
ANYAPI_API_KEY='your-key-here' openclaw onboard --non-interactive \
--auth-choice custom-api-key \
--custom-base-url "https://api.anyapi.ai/v1" \
--custom-model-id "openai/gpt-4o" \
--secret-input-mode ref \
--custom-compatibility openai \
--accept-risk
```
This registers a custom provider with `baseUrl: https://api.anyapi.ai/v1`, stores the API key as an env reference, and sets `openai/gpt-4o` as the default model.
Then start OpenClaw:
```bash theme={"system"}
openclaw
```
Prefer interactive setup? Run `openclaw onboard` without flags to walk through provider, URL, key, and model selection step by step.
## Manual Configuration
Edit `~/.openclaw/openclaw.json` directly for full control:
```json5 theme={"system"}
{
env: {
ANYAPI_API_KEY: "your-key-here",
},
models: {
providers: {
anyapi: {
baseUrl: "https://api.anyapi.ai/v1",
apiKey: {
source: "env",
provider: "default",
id: "ANYAPI_API_KEY",
},
api: "openai-completions",
models: [
{ id: "openai/gpt-4o" },
{ id: "openai/gpt-4o-mini" },
{ id: "anthropic/claude-sonnet-4.6" },
{ id: "google/gemini-2.5-pro" },
],
},
},
},
agents: {
defaults: {
model: {
primary: "anyapi/openai/gpt-4o",
fallbacks: [
"anyapi/anthropic/claude-sonnet-4.6",
"anyapi/openai/gpt-4o-mini",
],
},
},
},
}
```
Restart OpenClaw for changes to take effect:
```bash theme={"system"}
openclaw gateway restart # if running as gateway daemon
```
## Model Reference Format
OpenClaw composes model refs as `/`. With provider ID `anyapi`:
* `anyapi/openai/gpt-4o` β correct
* `anyapi/anthropic/claude-sonnet-4.6` β correct
* `openai/gpt-4o` β won't route through the `anyapi` provider
* `gpt-4o` β missing vendor prefix, AnyAPI returns HTTP 400
The `id` field inside `models.providers.anyapi.models[]` is the **upstream** ID sent to AnyAPI (e.g. `openai/gpt-4o`). The `anyapi/` prefix is added by OpenClaw based on your provider ID.
Browse all available models at [anyapi.ai/ai-models](https://anyapi.ai/ai-models) or query the API:
```bash theme={"system"}
curl -sS -H "Authorization: Bearer $ANYAPI_API_KEY" \
https://api.anyapi.ai/v1/models | jq -r '.data[].id'
```
## Configuration Examples
```json5 theme={"system"}
{
models: {
providers: {
anyapi: {
baseUrl: "https://api.anyapi.ai/v1",
apiKey: { source: "env", provider: "default", id: "ANYAPI_API_KEY" },
models: [{ id: "openai/gpt-4o" }],
},
},
},
agents: {
defaults: {
model: { primary: "anyapi/openai/gpt-4o" },
},
},
}
```
```json5 theme={"system"}
{
models: {
providers: {
anyapi: {
baseUrl: "https://api.anyapi.ai/v1",
apiKey: { source: "env", provider: "default", id: "ANYAPI_API_KEY" },
models: [{ id: "anthropic/claude-sonnet-4.6" }],
},
},
},
agents: {
defaults: {
model: { primary: "anyapi/anthropic/claude-sonnet-4.6" },
},
},
}
```
```json5 theme={"system"}
{
models: {
providers: {
anyapi: {
baseUrl: "https://api.anyapi.ai/v1",
apiKey: { source: "env", provider: "default", id: "ANYAPI_API_KEY" },
models: [
{ id: "openai/gpt-4o" },
{ id: "anthropic/claude-sonnet-4.6" },
{ id: "openai/gpt-4o-mini" },
],
},
},
},
agents: {
defaults: {
model: {
primary: "anyapi/openai/gpt-4o",
fallbacks: [
"anyapi/anthropic/claude-sonnet-4.6",
"anyapi/openai/gpt-4o-mini",
],
timeoutMs: 120000,
},
},
},
}
```
## Advanced Configuration
Set per-model defaults via `agents.defaults.models`:
```json5 theme={"system"}
{
agents: {
defaults: {
models: {
"anyapi/openai/gpt-4o": {
params: {
temperature: 0.7,
max_tokens: 4096,
},
},
"anyapi/openai/o3": {
params: {
max_completion_tokens: 8192, // o-series uses max_completion_tokens
},
},
},
},
},
}
```
Enrich model entries to enable image inputs and set context windows:
```json5 theme={"system"}
models: [
{
id: "openai/gpt-4o",
name: "GPT-4o (via AnyAPI)",
input: ["text", "image"],
contextWindow: 128000,
maxTokens: 16384,
},
{
id: "google/gemini-2.5-pro",
input: ["text", "image"],
contextWindow: 1000000,
reasoning: true,
},
]
```
OpenClaw won't infer image capability for custom providers automatically β declare it explicitly.
Add request headers for tracking:
```json5 theme={"system"}
anyapi: {
baseUrl: "https://api.anyapi.ai/v1",
apiKey: { source: "env", provider: "default", id: "ANYAPI_API_KEY" },
headers: {
"X-My-App": "my-app-v1",
},
models: [{ id: "openai/gpt-4o" }],
}
```
For multi-account setups, OpenClaw supports auth profiles. Store a key in OS keychain:
```bash theme={"system"}
openclaw models auth add --provider anyapi
```
## CLI Reference
```bash theme={"system"}
# Onboarding
openclaw onboard # interactive setup
openclaw onboard --non-interactive ... # scripted setup
# Model management
openclaw models list # list configured models
openclaw models set anyapi/openai/gpt-4o # set default model
# Config
openclaw config validate # validate config
openclaw config get models.providers.anyapi.baseUrl
openclaw config set agents.defaults.model.primary "anyapi/openai/gpt-4o"
# Gateway
openclaw gateway status
openclaw gateway restart
openclaw gateway logs
```
## Troubleshooting
OpenClaw rejects unknown keys. Use the canonical field names:
* `baseUrl` (camelCase, not `baseURL`)
* `apiKey` (object or `${VAR}` template, not `apiKeyEnvVar`)
Validate with `openclaw config validate`.
The `apiKey` reference doesn't resolve.
1. Confirm the env var is set: `echo $ANYAPI_API_KEY`
2. Confirm the `apiKey` block points to it:
```json5 theme={"system"}
apiKey: { source: "env", provider: "default", id: "ANYAPI_API_KEY" }
```
3. Restart OpenClaw β env changes don't apply to a running daemon.
Your key is invalid, expired, or revoked.
1. Validate the key at [dash.anyapi.ai](https://dash.anyapi.ai)
2. Test directly:
```bash theme={"system"}
curl https://api.anyapi.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ANYAPI_API_KEY" \
-d '{"model":"openai/gpt-4o-mini","messages":[{"role":"user","content":"test"}],"max_tokens":10}'
```
The model ID is unknown or missing the vendor prefix.
1. List the catalog: `curl -H "Authorization: Bearer $ANYAPI_API_KEY" https://api.anyapi.ai/v1/models`
2. Use `vendor/model` form (e.g. `openai/gpt-4o`, not bare `gpt-4o`)
3. Confirm the model is listed under `models.providers.anyapi.models[]` in your config
Set `baseUrl` to exactly `https://api.anyapi.ai/v1` β no trailing slash, no `/chat/completions` suffix (OpenClaw appends that).
Check usage at [dash.anyapi.ai](https://dash.anyapi.ai), upgrade your plan, or add fallbacks to spread load.
## Best Practices
1. **Store keys as env refs, not plaintext** β prefer `secret-input-mode ref` during onboarding
2. **Configure fallbacks** across different vendors for cross-provider resilience
3. **Set `timeoutMs` for slow models** β reasoning models like `openai/o3` can take minutes
4. **Pin model capability metadata** β declare `input: ["text", "image"]` explicitly for vision models
5. **Always use `vendor/model` format** β bare names return HTTP 400
6. **Monitor usage** at [dash.anyapi.ai](https://dash.anyapi.ai)
## Support
* **AnyAPI Dashboard**: [dash.anyapi.ai](https://dash.anyapi.ai)
* **AnyAPI Discord**: [Discord community](https://discord.gg/rJgyzGynHw)
* **AnyAPI Email**: [support@anyapi.ai](mailto:support@anyapi.ai)
* **OpenClaw Issues**: [github.com/openclaw/openclaw/issues](https://github.com/openclaw/openclaw/issues)
# Overview
Source: https://docs.anyapi.ai/integrations/overview
Connect AnyAPI with popular AI tools and automation platforms
# Integrations
AnyAPI is OpenAI-compatible, so it works with any tool that supports custom OpenAI endpoints. Point the tool at `https://api.anyapi.ai/v1`, add your API key, and you get access to 300+ models from OpenAI, Anthropic, Google, Meta, Mistral, xAI, DeepSeek, and more β through a single key.
## Available Integrations
Use AnyAPI in VS Code with Cline, Continue.dev, GitHub Copilot BYOK, and more
Visual workflow automation with AI models β no code required
Autonomous coding agent with 21 messaging platform adapters
AI coding agent with multi-model fallback chains
## How It Works
AnyAPI implements the OpenAI API specification. Any tool that lets you set a custom base URL and API key for an OpenAI-compatible provider works with AnyAPI out of the box.
```
Your Tool β https://api.anyapi.ai/v1 β 300+ models (GPT, Claude, Gemini, Llama, etc.)
```
### Setup Pattern
The setup is the same across most tools:
1. Get your API key from the [AnyAPI Dashboard](https://dash.anyapi.ai/?page=api-keys)
2. Set the base URL to `https://api.anyapi.ai/v1`
3. Use the `provider/model` format for model IDs (e.g. `openai/gpt-4o`, `anthropic/claude-sonnet-4.6`)
### Model ID Format
AnyAPI uses an OpenRouter-style `vendor/model` format:
* `openai/gpt-4o`
* `anthropic/claude-sonnet-4.6`
* `google/gemini-2.5-pro`
* `deepseek/deepseek-r1`
Bare model names like `gpt-4o` without the vendor prefix will not work. Browse the full catalog at [anyapi.ai/ai-models](https://anyapi.ai/ai-models).
## Support
* **Dashboard**: [dash.anyapi.ai](https://dash.anyapi.ai)
* **Discord**: [discord.gg/rJgyzGynHw](https://discord.gg/rJgyzGynHw)
* **Email**: [support@anyapi.ai](mailto:support@anyapi.ai)
# VS Code Integration
Source: https://docs.anyapi.ai/integrations/vscode
Use AnyAPI models in VS Code with Cline, Continue.dev, GitHub Copilot BYOK, and other AI plugins
# Use AnyAPI in VS Code
AnyAPI works with any VS Code AI plugin that supports custom OpenAI-compatible endpoints. Point the plugin at `https://api.anyapi.ai/v1`, add your API key, and you get access to 300+ models.
## Prerequisites
1. Get your API key from the [AnyAPI Dashboard](https://dash.anyapi.ai/?page=api-keys)
2. Pick a model from the [model catalog](https://anyapi.ai/ai-models)
All plugins below use the same connection details:
| Parameter | Value |
| ------------ | ------------------------------------------------- |
| **Base URL** | `https://api.anyapi.ai/v1` |
| **API Key** | Your AnyAPI key (`sk-...`) |
| **Model ID** | e.g. `openai/gpt-4o`, `anthropic/claude-sonnet-4` |
AnyAPI uses `provider/model` format for model IDs. Bare names like `gpt-4o` without the vendor prefix won't work. Browse all models at [anyapi.ai/ai-models](https://anyapi.ai/ai-models).
***
## Option 1: Cline (Recommended)
Autonomous AI coding agent. Edits files, runs terminal commands, browses the web. No sign-up required β setup takes 30 seconds.
### Setup
1. Install **Cline** from the Extensions marketplace (`Ctrl+Shift+X` / `Cmd+Shift+X`)
2. Open the Cline panel in the sidebar
3. Click the gear icon (Settings)
4. Set **API Provider** to **OpenAI Compatible**
5. Fill in:
| Field | Value |
| ------------ | -------------------------------- |
| **Base URL** | `https://api.anyapi.ai/v1` |
| **API Key** | Your AnyAPI key |
| **Model ID** | e.g. `anthropic/claude-sonnet-4` |
6. Click **Save** β you're ready to go
### Usage
Type a task in the chat (e.g. "Create a REST API with Express") and Cline will autonomously create files, install dependencies, and write code. It asks for approval before each action.
Use large-context models like `anthropic/claude-sonnet-4` for complex tasks and cheaper models like `openai/gpt-4o-mini` for quick fixes.
***
## Option 2: Continue.dev
Open-source AI assistant with chat, autocomplete, refactoring, and code explanation.
### Setup
1. Install **Continue** from the Extensions marketplace
2. Sign in with a Continue.dev account when prompted
3. Open `~/.continue/config.yaml` (`Cmd+Shift+P` β `Continue: Open configuration file`)
4. Replace the contents with:
```yaml theme={"system"}
name: AnyAPI Config
version: 1.0.0
schema: v1
models:
- name: AnyAPI β Claude Sonnet
provider: openai
model: anthropic/claude-sonnet-4
apiBase: https://api.anyapi.ai/v1
apiKey: sk-your-anyapi-key
roles:
- chat
- edit
- name: AnyAPI β GPT-4o
provider: openai
model: openai/gpt-4o
apiBase: https://api.anyapi.ai/v1
apiKey: sk-your-anyapi-key
roles:
- chat
- edit
- name: AnyAPI Autocomplete
provider: openai
model: openai/gpt-4o-mini
apiBase: https://api.anyapi.ai/v1
apiKey: sk-your-anyapi-key
roles:
- autocomplete
```
5. Reload VS Code (`Cmd+Shift+P` β `Developer: Reload Window`)
Newer versions of Continue require signing in with a Continue.dev account on first launch. If the login hangs, reload VS Code and try again.
### Usage
* **Chat** β Open the Continue panel, select a model, and ask questions
* **Autocomplete** β Start typing code, press `Tab` to accept suggestions
* **Refactoring** β Select code, press `Ctrl+Shift+L` / `Cmd+Shift+L` and describe the change
***
## Option 3: GitHub Copilot BYOK
Use AnyAPI models inside GitHub Copilot Chat with Bring Your Own Key.
Requires **VS Code Insiders** and a GitHub Copilot subscription (Free plan or above). If the `Chat: Manage Language Models` command is not found, download [VS Code Insiders](https://code.visualstudio.com/insiders/).
### Setup
1. Open Command Palette (`Cmd+Shift+P` / `Ctrl+Shift+P`)
2. Type `Chat: Manage Language Models`
3. Click **Add Models** β **Custom Endpoint**
4. Configure the provider:
| Field | Value |
| ------------ | ------------------ |
| **API Key** | Your AnyAPI key |
| **API Type** | `Chat Completions` |
5. Add a model:
| Field | Value |
| -------- | ------------------------------------------- |
| **ID** | `anthropic/claude-sonnet-4` |
| **Name** | `AnyAPI Claude Sonnet` |
| **URL** | `https://api.anyapi.ai/v1/chat/completions` |
6. Save β the model appears in the Copilot Chat model picker
This creates a `chatLanguageModels.json` file:
```json theme={"system"}
[
{
"vendor": "customendpoint",
"apiKey": "sk-your-anyapi-key",
"apiType": "chat-completions",
"models": [
{
"id": "anthropic/claude-sonnet-4",
"name": "AnyAPI Claude Sonnet",
"url": "https://api.anyapi.ai/v1/chat/completions",
"toolCalling": true,
"maxInputTokens": 200000,
"maxOutputTokens": 16000
}
]
}
]
```
Copilot BYOK only applies to chat and agent mode. Inline autocomplete still uses GitHub's own models.
***
## Option 4: VS Code Native Custom Endpoint
Built-in support for custom AI endpoints, no extensions needed.
Currently available in **VS Code Insiders** only.
### Setup
1. Open VS Code Insiders
2. Click the model dropdown in Copilot Chat
3. Select **Manage Language Models...**
4. Click **Add Models** β **Custom Endpoint**
5. Fill in:
| Field | Value |
| ---------------- | ------------------------------------------- |
| **Group Name** | `AnyAPI` |
| **Display Name** | `GPT-4o via AnyAPI` |
| **Endpoint URL** | `https://api.anyapi.ai/v1/chat/completions` |
| **API Key** | Your AnyAPI key |
| **API Type** | `Chat Completions` |
***
## Option 5: LiteLLM Extensions
Two extensions designed for LiteLLM proxies β fully compatible with AnyAPI.
### LiteLLM Connector for Copilot
1. Install [LiteLLM Connector for Copilot](https://marketplace.visualstudio.com/items?itemName=Gethnet.litellm-connector-copilot)
2. Open Settings (`Ctrl+,` / `Cmd+,`) and search for `litellm`
3. Set:
| Setting | Value |
| ----------------- | ----------------------- |
| **Base URL** | `https://api.anyapi.ai` |
| **API Key** | Your AnyAPI key |
| **Default Model** | `openai/gpt-4o` |
### LiteLLM Provider for Copilot Chat
1. Install [LiteLLM Provider for Copilot Chat](https://marketplace.visualstudio.com/items?itemName=vivswan.litellm-vscode-chat)
2. `Cmd+Shift+P` β `LiteLLM: Add Server`
3. Enter label `AnyAPI`, URL `https://api.anyapi.ai`, and your API key
Supports multiple servers, automatic model aggregation, and cheapest/fastest model selection.
***
## Option 6: vscode-openai
Lightweight extension for OpenAI-compatible APIs.
1. Install [vscode-openai](https://marketplace.visualstudio.com/items?itemName=AndrewButson.vscode-openai)
2. `Cmd+Shift+P` β `vscode-openai: Set API URL` β `https://api.anyapi.ai/v1`
3. `Cmd+Shift+P` β `vscode-openai: Set API Key` β your AnyAPI key
***
## Comparison
| Plugin | Chat | Autocomplete | Agent | Free | Requires |
| ---------------------- | ---- | ------------ | ----- | ---- | -------------------------- |
| **Cline** | Yes | No | Yes | Yes | β |
| **Continue.dev** | Yes | Yes | No | Yes | Continue account |
| **Copilot BYOK** | Yes | No | Yes | No | Copilot + VS Code Insiders |
| **VS Code Native** | Yes | No | Yes | Yes | VS Code Insiders |
| **LiteLLM Extensions** | Yes | No | No | No | Copilot |
| **vscode-openai** | Yes | No | No | Yes | β |
### Recommendations
* **Quick start, no sign-up** β Cline
* **Autocomplete + chat** β Continue.dev
* **Already use Copilot** β BYOK or LiteLLM extensions
* **No extensions** β VS Code Native Custom Endpoint (Insiders only)
## Recommended Models for Coding
| Model | Best for |
| --------------------------- | ------------------------------------ |
| `anthropic/claude-sonnet-4` | Fast edits, everyday coding |
| `anthropic/claude-opus-4` | Complex architecture, deep reasoning |
| `openai/gpt-4o` | Multi-modal, broad knowledge |
| `openai/gpt-4o-mini` | Autocomplete, quick tasks |
| `deepseek/deepseek-r1` | Algorithms, math |
## Troubleshooting
### "Model not found"
Use the full `provider/model` format. Check available models:
```bash theme={"system"}
curl https://api.anyapi.ai/v1/models \
-H "Authorization: Bearer sk-your-anyapi-key"
```
### "Unauthorized" (401)
Verify your API key is correct and active in the [AnyAPI Dashboard](https://dash.anyapi.ai/?page=api-keys).
### "Payment Required" (402)
Top up your balance or check the limits on your key.
### Slow responses
* Use a faster model (`openai/gpt-4o-mini`)
* Increase the timeout in plugin settings
* Reduce `maxTokens`
## Next Steps
Quick setup guide for Cline
Quick setup guide for Continue.dev
Browse all supported tools
Browse 300+ available models
# 3D Models
Source: https://docs.anyapi.ai/use-cases/3d-models
Generate, manipulate, and optimize 3D models using AI for games, product design, and visualization
# 3D Model Generation with AI
Create stunning 3D models, environments, and assets using AI-powered generation tools. Perfect for game development, product design, architectural visualization, and creative projects.
## Overview
AI 3D model generation enables:
* **Text-to-3D creation** - Generate models from descriptions
* **Image-to-3D conversion** - Transform 2D images into 3D objects
* **Model optimization** - Reduce complexity while maintaining quality
* **Texture generation** - Create realistic materials and surfaces
* **Animation rigging** - Prepare models for movement and interaction
Generate 3D models from text descriptions
Convert 2D images into 3D objects
Optimize geometry and reduce polygon count
Generate realistic materials and textures
## Quick Start
```python Python theme={"system"}
import requests
import base64
import json
from PIL import Image
import io
class AI3DModelGenerator:
def __init__(self, api_key):
self.api_key = api_key
def text_to_3d(self, description, style="realistic", quality="high"):
"""Generate 3D model from text description"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/text-to-3d",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "shap-e",
"prompt": description,
"style": style,
"quality": quality,
"output_format": "obj",
"include_textures": True,
"polygon_count": "medium"
}
)
result = response.json()
return {
"model_url": result["data"]["model_url"],
"texture_url": result["data"].get("texture_url"),
"format": result["data"]["format"],
"polygon_count": result["data"]["polygon_count"]
}
def image_to_3d(self, image_path, depth_estimation="auto"):
"""Convert 2D image to 3D model"""
# Encode image to base64
with open(image_path, "rb") as image_file:
image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
response = requests.post(
"https://api.anyapi.ai/v1/3d/image-to-3d",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "point-e",
"image": image_base64,
"depth_estimation": depth_estimation,
"output_format": "obj",
"generate_backface": True,
"smooth_normals": True
}
)
result = response.json()
return {
"model_url": result["data"]["model_url"],
"preview_url": result["data"]["preview_url"],
"vertices": result["data"]["vertex_count"],
"faces": result["data"]["face_count"]
}
def optimize_model(self, model_url, target_polygons=None, quality_preset="balanced"):
"""Optimize 3D model for performance"""
optimization_settings = {
"low": {"decimation": 0.3, "texture_size": 512},
"balanced": {"decimation": 0.5, "texture_size": 1024},
"high": {"decimation": 0.7, "texture_size": 2048}
}
settings = optimization_settings.get(quality_preset, optimization_settings["balanced"])
response = requests.post(
"https://api.anyapi.ai/v1/3d/optimize",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": model_url,
"target_polygons": target_polygons,
"decimation_ratio": settings["decimation"],
"preserve_uv": True,
"preserve_boundaries": True,
"max_texture_size": settings["texture_size"]
}
)
result = response.json()
return {
"optimized_model_url": result["data"]["model_url"],
"original_polygons": result["data"]["original_count"],
"optimized_polygons": result["data"]["optimized_count"],
"reduction_ratio": result["data"]["reduction_ratio"]
}
def generate_texture(self, model_url, texture_description, resolution=1024):
"""Generate texture for 3D model"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/texture-generation",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "texture-synthesis",
"input_model": model_url,
"texture_prompt": texture_description,
"resolution": resolution,
"uv_unwrap": True,
"seamless": True,
"include_normal_map": True,
"include_roughness_map": True
}
)
result = response.json()
return {
"diffuse_map": result["data"]["diffuse_url"],
"normal_map": result["data"]["normal_url"],
"roughness_map": result["data"]["roughness_url"],
"uv_layout": result["data"]["uv_layout_url"]
}
def create_variations(self, base_description, variation_count=3):
"""Create multiple variations of a 3D model"""
variations = []
variation_styles = [
"stylized and cartoon-like",
"realistic and detailed",
"minimalist and geometric",
"organic and natural",
"futuristic and sci-fi"
]
for i in range(variation_count):
style = variation_styles[i % len(variation_styles)]
varied_description = f"{base_description}, {style} style"
model_result = self.text_to_3d(varied_description, style="mixed")
variations.append({
"description": varied_description,
"style": style,
"model_url": model_result["model_url"],
"texture_url": model_result.get("texture_url")
})
return variations
def rig_for_animation(self, model_url, rig_type="humanoid"):
"""Add animation rig to 3D model"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/rigging",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": model_url,
"rig_type": rig_type,
"auto_weights": True,
"include_ik": True,
"bone_count": "standard"
}
)
result = response.json()
return {
"rigged_model_url": result["data"]["model_url"],
"armature_url": result["data"]["armature_url"],
"bone_count": result["data"]["bone_count"],
"animation_ready": result["data"]["animation_ready"]
}
# Usage examples
generator = AI3DModelGenerator("your_api_key")
# Generate 3D model from text
model_result = generator.text_to_3d(
"A medieval castle with tall towers and stone walls",
style="realistic",
quality="high"
)
print(f"Generated model: {model_result['model_url']}")
# Convert image to 3D
image_3d = generator.image_to_3d("product_photo.jpg")
print(f"Image to 3D model: {image_3d['model_url']}")
# Optimize for web/mobile use
optimized = generator.optimize_model(
model_result['model_url'],
target_polygons=5000,
quality_preset="balanced"
)
print(f"Optimized model: {optimized['optimized_model_url']}")
# Generate custom texture
texture_result = generator.generate_texture(
model_result['model_url'],
"weathered stone texture with moss and age marks",
resolution=2048
)
print(f"Custom texture: {texture_result['diffuse_map']}")
# Create variations
variations = generator.create_variations(
"A futuristic vehicle",
variation_count=3
)
for i, variation in enumerate(variations):
print(f"Variation {i+1}: {variation['description']} - {variation['model_url']}")
```
```javascript JavaScript theme={"system"}
class AI3DModelGenerator {
constructor(apiKey) {
this.apiKey = apiKey;
}
async textTo3D(description, style = 'realistic', quality = 'high') {
const response = await fetch('https://api.anyapi.ai/v1/3d/text-to-3d', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'shap-e',
prompt: description,
style: style,
quality: quality,
output_format: 'obj',
include_textures: true,
polygon_count: 'medium'
})
});
const result = await response.json();
return {
modelUrl: result.data.model_url,
textureUrl: result.data.texture_url,
format: result.data.format,
polygonCount: result.data.polygon_count
};
}
async imageTo3D(imageFile, depthEstimation = 'auto') {
// Convert file to base64
const imageBase64 = await this.fileToBase64(imageFile);
const response = await fetch('https://api.anyapi.ai/v1/3d/image-to-3d', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'point-e',
image: imageBase64,
depth_estimation: depthEstimation,
output_format: 'obj',
generate_backface: true,
smooth_normals: true
})
});
const result = await response.json();
return {
modelUrl: result.data.model_url,
previewUrl: result.data.preview_url,
vertices: result.data.vertex_count,
faces: result.data.face_count
};
}
async optimizeModel(modelUrl, targetPolygons = null, qualityPreset = 'balanced') {
const optimizationSettings = {
low: { decimation: 0.3, textureSize: 512 },
balanced: { decimation: 0.5, textureSize: 1024 },
high: { decimation: 0.7, textureSize: 2048 }
};
const settings = optimizationSettings[qualityPreset] || optimizationSettings.balanced;
const response = await fetch('https://api.anyapi.ai/v1/3d/optimize', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
input_model: modelUrl,
target_polygons: targetPolygons,
decimation_ratio: settings.decimation,
preserve_uv: true,
preserve_boundaries: true,
max_texture_size: settings.textureSize
})
});
const result = await response.json();
return {
optimizedModelUrl: result.data.model_url,
originalPolygons: result.data.original_count,
optimizedPolygons: result.data.optimized_count,
reductionRatio: result.data.reduction_ratio
};
}
async generateTexture(modelUrl, textureDescription, resolution = 1024) {
const response = await fetch('https://api.anyapi.ai/v1/3d/texture-generation', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'texture-synthesis',
input_model: modelUrl,
texture_prompt: textureDescription,
resolution: resolution,
uv_unwrap: true,
seamless: true,
include_normal_map: true,
include_roughness_map: true
})
});
const result = await response.json();
return {
diffuseMap: result.data.diffuse_url,
normalMap: result.data.normal_url,
roughnessMap: result.data.roughness_url,
uvLayout: result.data.uv_layout_url
};
}
async createVariations(baseDescription, variationCount = 3) {
const variations = [];
const variationStyles = [
'stylized and cartoon-like',
'realistic and detailed',
'minimalist and geometric',
'organic and natural',
'futuristic and sci-fi'
];
for (let i = 0; i < variationCount; i++) {
const style = variationStyles[i % variationStyles.length];
const variedDescription = `${baseDescription}, ${style} style`;
const modelResult = await this.textTo3D(variedDescription, 'mixed');
variations.push({
description: variedDescription,
style: style,
modelUrl: modelResult.modelUrl,
textureUrl: modelResult.textureUrl
});
}
return variations;
}
async rigForAnimation(modelUrl, rigType = 'humanoid') {
const response = await fetch('https://api.anyapi.ai/v1/3d/rigging', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
input_model: modelUrl,
rig_type: rigType,
auto_weights: true,
include_ik: true,
bone_count: 'standard'
})
});
const result = await response.json();
return {
riggedModelUrl: result.data.model_url,
armatureUrl: result.data.armature_url,
boneCount: result.data.bone_count,
animationReady: result.data.animation_ready
};
}
async fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result.split(',')[1]);
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
}
// Usage example
const generator = new AI3DModelGenerator('your_api_key');
// Generate from text description
generator.textTo3D('A steampunk airship with copper details')
.then(result => {
console.log('Generated model:', result.modelUrl);
// Display in Three.js scene
displayModel(result.modelUrl);
});
// Convert uploaded image to 3D
document.getElementById('imageUpload').addEventListener('change', async (event) => {
const file = event.target.files[0];
if (file) {
const result = await generator.imageTo3D(file);
console.log('Image to 3D:', result.modelUrl);
displayModel(result.modelUrl);
}
});
function displayModel(modelUrl) {
// Three.js model loading and display code
const loader = new THREE.OBJLoader();
loader.load(modelUrl, function(object) {
scene.add(object);
renderer.render(scene, camera);
});
}
```
## Advanced 3D Generation Workflows
### Game Asset Pipeline
```python theme={"system"}
class GameAssetPipeline:
def __init__(self, api_key):
self.api_key = api_key
self.asset_database = {}
def create_game_character(self, character_description, game_style="realistic"):
"""Create a complete game character with LOD variations"""
# Generate base character model
base_model = self.generate_character_base(character_description, game_style)
# Create multiple Level of Detail (LOD) versions
lod_models = self.create_lod_variations(base_model["model_url"])
# Generate character textures
textures = self.create_character_textures(base_model["model_url"], character_description)
# Add animation rig
rigged_model = self.add_character_rig(base_model["model_url"])
# Generate animation set
animations = self.create_character_animations(rigged_model["rigged_model_url"])
character_asset = {
"character_id": f"char_{hash(character_description) % 10000}",
"description": character_description,
"models": {
"base": base_model,
"lod_variants": lod_models,
"rigged": rigged_model
},
"textures": textures,
"animations": animations,
"game_ready": True
}
self.asset_database[character_asset["character_id"]] = character_asset
return character_asset
def generate_character_base(self, description, style):
"""Generate base character model"""
style_prompts = {
"realistic": "photorealistic human character with natural proportions",
"stylized": "stylized character with appealing proportions and clean geometry",
"cartoon": "cartoon character with exaggerated features and simple forms",
"fantasy": "fantasy character with unique features and detailed design"
}
full_prompt = f"{description}, {style_prompts.get(style, style)}, game-ready topology"
response = requests.post(
"https://api.anyapi.ai/v1/3d/text-to-3d",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "character-gen",
"prompt": full_prompt,
"style": style,
"topology": "quad-based",
"symmetrical": True,
"output_format": "fbx"
}
)
return response.json()["data"]
def create_lod_variations(self, base_model_url):
"""Create Level of Detail variations for performance"""
lod_configs = [
{"name": "LOD0", "quality": 1.0, "polygons": None}, # Original
{"name": "LOD1", "quality": 0.7, "polygons": 15000},
{"name": "LOD2", "quality": 0.4, "polygons": 5000},
{"name": "LOD3", "quality": 0.2, "polygons": 1000}
]
lod_models = []
for config in lod_configs:
if config["name"] == "LOD0":
lod_models.append({
"level": config["name"],
"model_url": base_model_url,
"polygon_count": "original"
})
else:
optimized = self.optimize_for_lod(
base_model_url,
config["polygons"],
config["quality"]
)
lod_models.append({
"level": config["name"],
"model_url": optimized["model_url"],
"polygon_count": optimized["polygon_count"]
})
return lod_models
def optimize_for_lod(self, model_url, target_polygons, quality_factor):
"""Optimize model for specific LOD level"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/lod-optimization",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": model_url,
"target_polygons": target_polygons,
"quality_factor": quality_factor,
"preserve_silhouette": True,
"maintain_uv_seams": True,
"smart_decimation": True
}
)
return response.json()["data"]
def create_character_textures(self, model_url, description):
"""Generate complete texture set for character"""
texture_types = [
("diffuse", f"realistic skin and clothing texture for {description}"),
("normal", f"normal map with skin details and fabric texture for {description}"),
("specular", f"specular map showing appropriate shine and reflectivity for {description}"),
("emission", f"emission map for glowing elements or magical effects on {description}")
]
textures = {}
for texture_type, prompt in texture_types:
response = requests.post(
"https://api.anyapi.ai/v1/3d/texture-generation",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "character-texture",
"input_model": model_url,
"texture_type": texture_type,
"prompt": prompt,
"resolution": 2048,
"seamless": True
}
)
textures[texture_type] = response.json()["data"]["texture_url"]
return textures
def add_character_rig(self, model_url):
"""Add animation rig to character"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/character-rigging",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": model_url,
"rig_type": "humanoid",
"auto_bone_placement": True,
"include_facial_rig": True,
"finger_bones": True,
"ik_chains": ["arms", "legs"],
"constraint_setup": True
}
)
return response.json()["data"]
def create_character_animations(self, rigged_model_url):
"""Generate basic animation set"""
animation_types = [
"idle_breathing",
"walk_cycle",
"run_cycle",
"jump",
"attack_basic",
"hit_reaction",
"death"
]
animations = {}
for anim_type in animation_types:
response = requests.post(
"https://api.anyapi.ai/v1/3d/animation-generation",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"rigged_model": rigged_model_url,
"animation_type": anim_type,
"duration": self.get_animation_duration(anim_type),
"loop": self.is_looping_animation(anim_type),
"quality": "game_ready"
}
)
animations[anim_type] = {
"animation_url": response.json()["data"]["animation_url"],
"duration": response.json()["data"]["duration"],
"looping": response.json()["data"]["looping"]
}
return animations
def get_animation_duration(self, anim_type):
"""Get appropriate duration for animation type"""
durations = {
"idle_breathing": 3.0,
"walk_cycle": 1.0,
"run_cycle": 0.8,
"jump": 1.5,
"attack_basic": 1.2,
"hit_reaction": 0.5,
"death": 2.0
}
return durations.get(anim_type, 1.0)
def is_looping_animation(self, anim_type):
"""Determine if animation should loop"""
looping_anims = ["idle_breathing", "walk_cycle", "run_cycle"]
return anim_type in looping_anims
def create_environment_set(self, theme, complexity="medium"):
"""Create a set of environment assets"""
environment_elements = [
f"{theme} building architecture",
f"{theme} natural vegetation and plants",
f"{theme} props and decorative objects",
f"{theme} terrain and landscape features",
f"{theme} lighting and atmosphere elements"
]
environment_assets = []
for element in environment_elements:
asset = self.generate_environment_asset(element, complexity)
environment_assets.append(asset)
return {
"theme": theme,
"complexity": complexity,
"assets": environment_assets,
"asset_count": len(environment_assets)
}
def generate_environment_asset(self, description, complexity):
"""Generate individual environment asset"""
complexity_settings = {
"low": {"polygons": 2000, "texture_res": 512},
"medium": {"polygons": 8000, "texture_res": 1024},
"high": {"polygons": 20000, "texture_res": 2048}
}
settings = complexity_settings.get(complexity, complexity_settings["medium"])
response = requests.post(
"https://api.anyapi.ai/v1/3d/environment-generation",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "environment-gen",
"prompt": f"{description}, game environment asset",
"polygon_budget": settings["polygons"],
"texture_resolution": settings["texture_res"],
"modular": True,
"collision_mesh": True
}
)
return response.json()["data"]
# Usage
game_pipeline = GameAssetPipeline("your_api_key")
# Create complete game character
character = game_pipeline.create_game_character(
"Female elf archer with leather armor and mystical bow",
game_style="fantasy"
)
print(f"Character ID: {character['character_id']}")
print(f"LOD models: {len(character['models']['lod_variants'])}")
print(f"Animations: {list(character['animations'].keys())}")
# Create environment set
environment = game_pipeline.create_environment_set("medieval fantasy village", "high")
print(f"Environment assets: {environment['asset_count']}")
```
### Product Design and Prototyping
```python theme={"system"}
class ProductDesignStudio:
def __init__(self, api_key):
self.api_key = api_key
self.design_iterations = {}
def design_product_concept(self, product_description, design_brief):
"""Create initial product concept from description"""
concept_prompt = f"""
Product: {product_description}
Design Brief: {design_brief}
Create a professional product design with:
- Functional form and ergonomics
- Aesthetic appeal and modern styling
- Manufacturing feasibility
- User-centered design principles
"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/product-design",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "product-designer",
"prompt": concept_prompt,
"design_style": "modern",
"output_format": "step",
"include_technical_drawings": True,
"cad_compatible": True
}
)
concept_data = response.json()["data"]
design_id = f"design_{hash(product_description) % 10000}"
self.design_iterations[design_id] = {
"original_concept": concept_data,
"iterations": [],
"description": product_description,
"brief": design_brief
}
return {
"design_id": design_id,
"concept_model": concept_data["model_url"],
"technical_drawings": concept_data["drawings_url"],
"specifications": concept_data["specs"]
}
def iterate_design(self, design_id, feedback, iteration_focus="ergonomics"):
"""Create design iteration based on feedback"""
if design_id not in self.design_iterations:
return {"error": "Design not found"}
original_design = self.design_iterations[design_id]
iteration_prompts = {
"ergonomics": "improve ergonomics and user comfort",
"aesthetics": "enhance visual appeal and styling",
"functionality": "optimize functional features and usability",
"manufacturing": "simplify for easier manufacturing and cost reduction",
"sustainability": "incorporate sustainable materials and eco-friendly design"
}
focus_prompt = iteration_prompts.get(iteration_focus, iteration_focus)
iteration_prompt = f"""
Based on this feedback: {feedback}
Focus on: {focus_prompt}
Original product: {original_design['description']}
Design brief: {original_design['brief']}
Create an improved version that addresses the feedback while maintaining the core design intent.
"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/design-iteration",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"base_model": original_design["original_concept"]["model_url"],
"iteration_prompt": iteration_prompt,
"preserve_proportions": True,
"maintain_functionality": True,
"output_format": "step"
}
)
iteration_data = response.json()["data"]
# Store iteration
self.design_iterations[design_id]["iterations"].append({
"feedback": feedback,
"focus": iteration_focus,
"model_url": iteration_data["model_url"],
"changes": iteration_data["change_summary"],
"timestamp": datetime.now().isoformat()
})
return {
"iteration_model": iteration_data["model_url"],
"changes_made": iteration_data["change_summary"],
"iteration_number": len(self.design_iterations[design_id]["iterations"])
}
def create_material_variants(self, design_id, materials_list):
"""Create variants with different materials"""
if design_id not in self.design_iterations:
return {"error": "Design not found"}
base_model = self.design_iterations[design_id]["original_concept"]["model_url"]
material_variants = []
for material in materials_list:
variant = self.apply_material(base_model, material)
material_variants.append({
"material": material,
"model_url": variant["model_url"],
"cost_estimate": variant["cost_estimate"],
"durability_rating": variant["durability"]
})
return {
"design_id": design_id,
"material_variants": material_variants,
"variant_count": len(material_variants)
}
def apply_material(self, model_url, material_description):
"""Apply specific material to model"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/material-application",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": model_url,
"material": material_description,
"realistic_rendering": True,
"include_cost_analysis": True,
"sustainability_rating": True
}
)
return response.json()["data"]
def generate_technical_documentation(self, design_id):
"""Generate technical documentation for manufacturing"""
if design_id not in self.design_iterations:
return {"error": "Design not found"}
design_data = self.design_iterations[design_id]
latest_model = design_data["original_concept"]["model_url"]
# Use latest iteration if available
if design_data["iterations"]:
latest_model = design_data["iterations"][-1]["model_url"]
response = requests.post(
"https://api.anyapi.ai/v1/3d/technical-docs",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": latest_model,
"documentation_type": "manufacturing",
"include_dimensions": True,
"include_tolerances": True,
"include_assembly": True,
"include_bom": True,
"format": "pdf"
}
)
return response.json()["data"]
def create_color_variants(self, design_id, color_schemes):
"""Create color variants of the design"""
if design_id not in self.design_iterations:
return {"error": "Design not found"}
base_model = self.design_iterations[design_id]["original_concept"]["model_url"]
color_variants = []
for scheme in color_schemes:
variant = self.apply_color_scheme(base_model, scheme)
color_variants.append({
"color_scheme": scheme,
"model_url": variant["model_url"],
"swatch_url": variant["color_swatch"]
})
return {
"design_id": design_id,
"color_variants": color_variants
}
def apply_color_scheme(self, model_url, color_scheme):
"""Apply color scheme to model"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/color-application",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": model_url,
"color_scheme": color_scheme,
"maintain_materials": True,
"generate_swatch": True
}
)
return response.json()["data"]
def export_for_manufacturing(self, design_id, export_format="step"):
"""Export design in manufacturing-ready format"""
if design_id not in self.design_iterations:
return {"error": "Design not found"}
design_data = self.design_iterations[design_id]
model_url = design_data["original_concept"]["model_url"]
# Use latest iteration if available
if design_data["iterations"]:
model_url = design_data["iterations"][-1]["model_url"]
response = requests.post(
"https://api.anyapi.ai/v1/3d/manufacturing-export",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_model": model_url,
"export_format": export_format,
"manufacturing_ready": True,
"include_toolpath": True,
"quality_check": True
}
)
return response.json()["data"]
# Usage
product_studio = ProductDesignStudio("your_api_key")
# Create initial product concept
concept = product_studio.design_product_concept(
"Wireless charging desk lamp with adjustable arm",
"Modern minimalist design for home office, sustainable materials, user-friendly controls"
)
print(f"Design ID: {concept['design_id']}")
print(f"Concept model: {concept['concept_model']}")
# Iterate based on feedback
iteration = product_studio.iterate_design(
concept['design_id'],
"The base seems too small for stability, and the charging pad should be more prominent",
"functionality"
)
print(f"Iteration model: {iteration['iteration_model']}")
print(f"Changes made: {iteration['changes_made']}")
# Create material variants
materials = ["brushed aluminum", "bamboo wood", "recycled plastic", "carbon fiber"]
material_variants = product_studio.create_material_variants(concept['design_id'], materials)
print(f"Material variants: {material_variants['variant_count']}")
# Generate technical documentation
tech_docs = product_studio.generate_technical_documentation(concept['design_id'])
print(f"Technical docs: {tech_docs['documentation_url']}")
```
### Architectural Visualization
```python theme={"system"}
class ArchitecturalVisualizer:
def __init__(self, api_key):
self.api_key = api_key
self.projects = {}
def create_building_design(self, architectural_brief, building_type="residential"):
"""Create architectural design from brief"""
building_prompts = {
"residential": "modern residential architecture with clean lines and natural lighting",
"commercial": "professional commercial building with functional design and accessibility",
"industrial": "efficient industrial building with practical layouts and material flow",
"cultural": "inspiring cultural building with unique architectural features"
}
style_context = building_prompts.get(building_type, building_type)
full_prompt = f"""
Architectural Brief: {architectural_brief}
Building Type: {building_type}
Style Context: {style_context}
Create architectural design with:
- Proper structural elements and realistic proportions
- Environmental context and site integration
- Natural lighting and ventilation considerations
- Accessible design and building code compliance
- Sustainable design principles
"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/architectural-design",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "architect-ai",
"prompt": full_prompt,
"building_type": building_type,
"include_site_context": True,
"structural_analysis": True,
"output_format": "ifc",
"lod": "300" # Level of Detail for construction
}
)
building_data = response.json()["data"]
project_id = f"arch_{hash(architectural_brief) % 10000}"
self.projects[project_id] = {
"brief": architectural_brief,
"building_type": building_type,
"design_data": building_data,
"renderings": [],
"floor_plans": []
}
return {
"project_id": project_id,
"building_model": building_data["model_url"],
"floor_plans": building_data["floor_plans_url"],
"structural_data": building_data["structural_url"]
}
def create_interior_design(self, project_id, room_specifications):
"""Create interior design for building"""
if project_id not in self.projects:
return {"error": "Project not found"}
building_model = self.projects[project_id]["design_data"]["model_url"]
interior_designs = {}
for room_name, room_spec in room_specifications.items():
interior = self.design_room_interior(building_model, room_name, room_spec)
interior_designs[room_name] = interior
self.projects[project_id]["interiors"] = interior_designs
return {
"project_id": project_id,
"interior_designs": interior_designs,
"rooms_designed": list(interior_designs.keys())
}
def design_room_interior(self, building_model, room_name, specifications):
"""Design interior for specific room"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/interior-design",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"building_model": building_model,
"room_name": room_name,
"specifications": specifications,
"style": specifications.get("style", "modern"),
"furniture_layout": True,
"lighting_design": True,
"material_selection": True
}
)
return response.json()["data"]
def create_photorealistic_rendering(self, project_id, view_specifications):
"""Create photorealistic architectural renderings"""
if project_id not in self.projects:
return {"error": "Project not found"}
building_model = self.projects[project_id]["design_data"]["model_url"]
renderings = []
for view_spec in view_specifications:
rendering = self.render_architectural_view(building_model, view_spec)
renderings.append(rendering)
self.projects[project_id]["renderings"] = renderings
return {
"project_id": project_id,
"renderings": renderings,
"rendering_count": len(renderings)
}
def render_architectural_view(self, building_model, view_spec):
"""Create single architectural rendering"""
response = requests.post(
"https://api.anyapi.ai/v1/3d/architectural-render",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"building_model": building_model,
"camera_position": view_spec["camera_position"],
"time_of_day": view_spec.get("time_of_day", "golden_hour"),
"weather": view_spec.get("weather", "clear"),
"season": view_spec.get("season", "spring"),
"resolution": view_spec.get("resolution", "4k"),
"style": view_spec.get("style", "photorealistic"),
"include_people": view_spec.get("include_people", True),
"include_vehicles": view_spec.get("include_vehicles", False)
}
)
return response.json()["data"]
def create_virtual_tour(self, project_id, tour_waypoints):
"""Create virtual reality tour of building"""
if project_id not in self.projects:
return {"error": "Project not found"}
building_model = self.projects[project_id]["design_data"]["model_url"]
response = requests.post(
"https://api.anyapi.ai/v1/3d/virtual-tour",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"building_model": building_model,
"waypoints": tour_waypoints,
"navigation_mode": "smooth",
"include_hotspots": True,
"vr_compatible": True,
"web_compatible": True
}
)
tour_data = response.json()["data"]
self.projects[project_id]["virtual_tour"] = tour_data
return {
"project_id": project_id,
"tour_url": tour_data["tour_url"],
"vr_url": tour_data["vr_url"],
"embed_code": tour_data["embed_code"]
}
def generate_construction_drawings(self, project_id):
"""Generate construction drawings and documentation"""
if project_id not in self.projects:
return {"error": "Project not found"}
building_model = self.projects[project_id]["design_data"]["model_url"]
response = requests.post(
"https://api.anyapi.ai/v1/3d/construction-drawings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"building_model": building_model,
"drawing_types": [
"floor_plans",
"elevations",
"sections",
"details",
"structural_plans"
],
"scale": "1:100",
"include_dimensions": True,
"include_annotations": True,
"format": "dwg"
}
)
return response.json()["data"]
# Usage
arch_visualizer = ArchitecturalVisualizer("your_api_key")
# Create building design
building = arch_visualizer.create_building_design(
"Modern 3-story office building with glass facade, open floor plans, rooftop garden, and sustainable features",
"commercial"
)
print(f"Project ID: {building['project_id']}")
# Create interior designs
room_specs = {
"lobby": {
"style": "modern minimalist",
"features": ["reception desk", "seating area", "art installation"],
"lighting": "natural and accent"
},
"office_floor": {
"style": "collaborative workspace",
"features": ["open desk areas", "meeting rooms", "break areas"],
"lighting": "task and ambient"
},
"conference_room": {
"style": "executive",
"features": ["large conference table", "presentation screen", "video conferencing"],
"lighting": "adjustable"
}
}
interiors = arch_visualizer.create_interior_design(building['project_id'], room_specs)
print(f"Interior designs created for: {interiors['rooms_designed']}")
# Create renderings
view_specs = [
{
"camera_position": "exterior_front",
"time_of_day": "golden_hour",
"weather": "clear",
"include_people": True
},
{
"camera_position": "lobby_interior",
"time_of_day": "midday",
"style": "photorealistic"
},
{
"camera_position": "aerial_view",
"time_of_day": "sunset",
"include_vehicles": True
}
]
renderings = arch_visualizer.create_photorealistic_rendering(building['project_id'], view_specs)
print(f"Created {renderings['rendering_count']} renderings")
# Create virtual tour
tour_waypoints = [
{"name": "Building Entrance", "position": "front_entrance"},
{"name": "Lobby", "position": "lobby_center"},
{"name": "Office Floor", "position": "office_overview"},
{"name": "Conference Room", "position": "conference_center"},
{"name": "Rooftop Garden", "position": "rooftop_view"}
]
virtual_tour = arch_visualizer.create_virtual_tour(building['project_id'], tour_waypoints)
print(f"Virtual tour created: {virtual_tour['tour_url']}")
```
## 3D Model Formats and Optimization
### Format Comparison
| Format | Best For | Features | File Size |
| ------------- | --------------------------- | -------------------------------- | --------- |
| **OBJ** | General use, simple models | Basic geometry, widely supported | Medium |
| **FBX** | Game development, animation | Animations, materials, rigging | Large |
| **GLTF/GLB** | Web applications | Optimized for web, PBR materials | Small |
| **STL** | 3D printing | Solid geometry, manufacturing | Small |
| **STEP/IGES** | CAD/Manufacturing | Precise engineering data | Large |
| **USD** | Film/VFX pipeline | Advanced materials, scene graphs | Variable |
### Optimization Strategies
Reduce triangle count while preserving visual quality
Compress textures and optimize UV layouts
Create multiple detail levels for performance
Use format-specific compression techniques
## Best Practices
### 1. Model Quality
* **Proper topology**: Use quad-based geometry when possible
* **Scale accuracy**: Maintain real-world proportions
* **Clean geometry**: Remove unnecessary vertices and faces
* **UV mapping**: Create efficient texture coordinates
### 2. Performance Optimization
* **Polygon budgets**: Set appropriate limits for target platform
* **Texture resolution**: Balance quality with memory usage
* **Level of Detail**: Create multiple versions for different distances
* **Batching**: Group similar objects to reduce draw calls
### 3. Workflow Integration
* **File organization**: Use consistent naming and folder structure
* **Version control**: Track model iterations and changes
* **Asset pipeline**: Automate import/export processes
* **Quality assurance**: Validate models before deployment
### 4. Platform Considerations
* **Web deployment**: Optimize for fast loading and streaming
* **Mobile devices**: Reduce complexity for limited hardware
* **VR/AR applications**: Maintain high frame rates
* **Print preparation**: Ensure manifold geometry for 3D printing
## Common Use Cases
Characters, environments, props, and interactive objects
Prototyping, visualization, and manufacturing preparation
Building visualization, interior design, and construction planning
Film VFX, animation, and virtual production
Interactive learning, simulations, and demonstrations
Product showcases, brand experiences, and campaigns
Prototyping, tooling design, and quality control
Medical visualization, surgical planning, and training
## Model Recommendations
| Use Case | Recommended Model | Strengths |
| ---------------------- | ------------------------------ | ------------------------------ |
| **Text-to-3D** | Shap-E, Point-E | Fast generation, good geometry |
| **Image-to-3D** | Point-E, TripoSR | Accurate reconstruction |
| **Character Creation** | Custom character models | Anatomical accuracy |
| **Environment Design** | Specialized environment models | Architectural knowledge |
| **Product Design** | CAD-aware models | Manufacturing constraints |
## Getting Started
Generate your first 3D model
Explore 3D generation models
Start with 2D image generation
Add motion to your creations
# Animate Images
Source: https://docs.anyapi.ai/use-cases/animate-images
Create dynamic animations, video content, and motion graphics using AI
# Animate Images with AI
Transform static images into dynamic animations, create video content, and generate motion graphics using advanced AI models. Perfect for marketing content, social media, presentations, and creative projects.
## Overview
AI animation capabilities enable you to:
* **Bring images to life** with realistic motion and effects
* **Create video content** from static images and descriptions
* **Generate smooth transitions** between different scenes
* **Add special effects** like particle systems and lighting
* **Produce marketing animations** for social media and advertising
Convert static images into dynamic video content
Add realistic motion, wind, water, and particle effects
Create smooth transitions between different scenes
Animate characters, faces, and objects naturally
## Quick Start
```python Python theme={"system"}
import requests
import base64
from PIL import Image
import io
class ImageAnimator:
def __init__(self, api_key):
self.api_key = api_key
def animate_image(self, image_path, animation_prompt, duration=3.0, fps=24):
"""Animate a static image based on text description"""
# Convert image to base64
with open(image_path, "rb") as image_file:
image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
response = requests.post(
"https://api.anyapi.ai/v1/video/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "runway-gen2",
"prompt": animation_prompt,
"image": image_base64,
"duration": duration,
"fps": fps,
"resolution": "1280x720"
}
)
result = response.json()
return result["data"][0]["url"]
def create_motion_effect(self, image_path, effect_type="wind"):
"""Add specific motion effects to an image"""
effect_prompts = {
"wind": "gentle wind blowing through the scene, leaves and fabric moving naturally",
"water": "water flowing and rippling naturally, reflections dancing",
"fire": "realistic fire flickering and dancing with natural movement",
"snow": "soft snow falling gently with natural drift patterns",
"clouds": "clouds moving slowly across the sky with natural flow",
"particles": "magical particles floating and glowing with ethereal movement"
}
prompt = effect_prompts.get(effect_type, effect_type)
return self.animate_image(image_path, prompt)
def animate_character(self, image_path, action_description):
"""Animate characters or people in images"""
animation_prompt = f"natural character animation: {action_description}, smooth and realistic movement, maintaining character appearance"
return self.animate_image(image_path, animation_prompt, duration=4.0)
def create_cinemagraph(self, image_path, motion_area, motion_type="subtle"):
"""Create cinemagraph-style animations with selective motion"""
motion_descriptions = {
"subtle": "very subtle, barely noticeable movement",
"gentle": "gentle, smooth, hypnotic movement",
"dramatic": "more pronounced but still elegant movement"
}
motion_desc = motion_descriptions.get(motion_type, motion_type)
prompt = f"cinemagraph style animation focusing on {motion_area} with {motion_desc}, everything else remains perfectly still"
return self.animate_image(image_path, prompt, duration=2.0)
# Usage examples
animator = ImageAnimator("your_api_key")
# Basic image animation
animated_url = animator.animate_image(
"landscape.jpg",
"camera slowly panning across the beautiful landscape, revealing hidden details"
)
print(f"Animated landscape: {animated_url}")
# Add wind effect
wind_animation = animator.create_motion_effect("portrait.jpg", "wind")
print(f"Wind effect: {wind_animation}")
# Animate character
character_animation = animator.animate_character(
"person_standing.jpg",
"person waves hello and smiles warmly"
)
print(f"Character animation: {character_animation}")
# Create cinemagraph
cinemagraph = animator.create_cinemagraph(
"coffee_shop.jpg",
"steam rising from the coffee cup",
"gentle"
)
print(f"Cinemagraph: {cinemagraph}")
```
```javascript JavaScript theme={"system"}
class ImageAnimator {
constructor(apiKey) {
this.apiKey = apiKey;
}
async animateImage(imagePath, animationPrompt, duration = 3.0, fps = 24) {
// Convert image to base64
const imageBase64 = await this.imageToBase64(imagePath);
const response = await fetch('https://api.anyapi.ai/v1/video/generations', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'runway-gen2',
prompt: animationPrompt,
image: imageBase64,
duration: duration,
fps: fps,
resolution: '1280x720'
})
});
const result = await response.json();
return result.data[0].url;
}
async createMotionEffect(imagePath, effectType = 'wind') {
const effectPrompts = {
wind: 'gentle wind blowing through the scene, leaves and fabric moving naturally',
water: 'water flowing and rippling naturally, reflections dancing',
fire: 'realistic fire flickering and dancing with natural movement',
snow: 'soft snow falling gently with natural drift patterns',
clouds: 'clouds moving slowly across the sky with natural flow',
particles: 'magical particles floating and glowing with ethereal movement'
};
const prompt = effectPrompts[effectType] || effectType;
return await this.animateImage(imagePath, prompt);
}
async animateCharacter(imagePath, actionDescription) {
const animationPrompt = `natural character animation: ${actionDescription}, smooth and realistic movement, maintaining character appearance`;
return await this.animateImage(imagePath, animationPrompt, 4.0);
}
async createCinemagraph(imagePath, motionArea, motionType = 'subtle') {
const motionDescriptions = {
subtle: 'very subtle, barely noticeable movement',
gentle: 'gentle, smooth, hypnotic movement',
dramatic: 'more pronounced but still elegant movement'
};
const motionDesc = motionDescriptions[motionType] || motionType;
const prompt = `cinemagraph style animation focusing on ${motionArea} with ${motionDesc}, everything else remains perfectly still`;
return await this.animateImage(imagePath, prompt, 2.0);
}
async imageToBase64(imagePath) {
// For browser environment
if (typeof window !== 'undefined') {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(imagePath);
});
}
// For Node.js environment
const fs = require('fs');
const imageBuffer = fs.readFileSync(imagePath);
return imageBuffer.toString('base64');
}
}
// Usage
const animator = new ImageAnimator('your_api_key');
// Example usage in browser
async function animateUserImage() {
const fileInput = document.getElementById('imageInput');
const file = fileInput.files[0];
if (file) {
const animatedUrl = await animator.animateImage(
file,
'gentle camera movement revealing the beauty of the scene'
);
// Display the animated result
const video = document.createElement('video');
video.src = animatedUrl;
video.controls = true;
video.autoplay = true;
document.body.appendChild(video);
}
}
// Create motion effect
animator.createMotionEffect('nature.jpg', 'water')
.then(url => console.log('Water effect:', url));
```
## Advanced Animation Techniques
### Story-Driven Animation
```python theme={"system"}
class StoryAnimator:
def __init__(self, api_key):
self.api_key = api_key
def create_narrative_sequence(self, images, story_prompts, transition_style="smooth"):
"""Create a narrative sequence from multiple images"""
animations = []
for i, (image_path, story_prompt) in enumerate(zip(images, story_prompts)):
# Create individual scene animation
scene_prompt = f"{story_prompt}, cinematic storytelling, {transition_style} transitions"
animated_scene = self.animate_scene(image_path, scene_prompt, scene_number=i)
animations.append(animated_scene)
# Combine scenes into a cohesive story
return self.combine_scenes(animations, transition_style)
def animate_scene(self, image_path, story_prompt, scene_number=0):
"""Animate a single scene with story context"""
# Add scene-specific timing and pacing
duration = 5.0 if scene_number == 0 else 4.0 # Longer intro
response = requests.post(
"https://api.anyapi.ai/v1/video/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "runway-gen2",
"prompt": story_prompt,
"image": self.encode_image(image_path),
"duration": duration,
"fps": 24,
"resolution": "1920x1080",
"style": "cinematic"
}
)
return response.json()["data"][0]["url"]
def combine_scenes(self, scene_urls, transition_style):
"""Combine multiple animated scenes"""
response = requests.post(
"https://api.anyapi.ai/v1/video/edit",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"operation": "sequence",
"inputs": scene_urls,
"transitions": [transition_style] * (len(scene_urls) - 1),
"output_format": "mp4"
}
)
return response.json()["data"]["url"]
def add_narrative_voice(self, video_url, script, voice_style="narrator"):
"""Add AI-generated voiceover to the animation"""
# Generate voiceover
voice_response = requests.post(
"https://api.anyapi.ai/v1/audio/speech",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "tts-1-hd",
"input": script,
"voice": voice_style,
"response_format": "mp3"
}
)
audio_url = voice_response.json()["url"]
# Combine video with audio
final_response = requests.post(
"https://api.anyapi.ai/v1/video/edit",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"operation": "add_audio",
"video": video_url,
"audio": audio_url,
"mix_level": 0.8
}
)
return final_response.json()["data"]["url"]
def encode_image(self, image_path):
"""Helper to encode image to base64"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Usage
story_animator = StoryAnimator("your_api_key")
# Create a story sequence
images = ["scene1.jpg", "scene2.jpg", "scene3.jpg"]
story_prompts = [
"opening scene: peaceful morning in a small village, gentle introduction",
"rising action: storm clouds gathering, tension building in the atmosphere",
"climax: dramatic lightning illuminating the landscape, powerful moment"
]
narrative_video = story_animator.create_narrative_sequence(images, story_prompts, "cinematic")
# Add voiceover
script = """
A peaceful morning in the village was about to change forever.
Dark clouds gathered on the horizon, bringing with them an storm of unprecedented power.
When lightning finally struck, it illuminated not just the landscape, but the beginning of a new chapter.
"""
final_video = story_animator.add_narrative_voice(narrative_video, script, "narrator")
print(f"Complete narrative: {final_video}")
```
### Marketing Animation Studio
```python theme={"system"}
class MarketingAnimator:
def __init__(self, api_key):
self.api_key = api_key
def create_product_showcase(self, product_image, product_name, key_features):
"""Create animated product showcase for marketing"""
showcase_prompt = f"""
Professional product showcase for {product_name}:
- Smooth 360-degree rotation revealing all angles
- Elegant lighting that highlights key features: {', '.join(key_features)}
- Premium commercial photography style
- Subtle zoom-in to show quality and detail
- Professional studio lighting with soft shadows
"""
return self.animate_with_style(product_image, showcase_prompt, "commercial")
def create_social_media_story(self, image, platform="instagram", mood="energetic"):
"""Create platform-optimized animated stories"""
platform_specs = {
"instagram": {
"aspect_ratio": "9:16",
"duration": 3.0,
"style": "vibrant, engaging, trendy"
},
"tiktok": {
"aspect_ratio": "9:16",
"duration": 2.5,
"style": "dynamic, fast-paced, attention-grabbing"
},
"facebook": {
"aspect_ratio": "16:9",
"duration": 4.0,
"style": "professional, clean, accessible"
}
}
spec = platform_specs.get(platform, platform_specs["instagram"])
mood_styles = {
"energetic": "dynamic movement, vibrant colors, upbeat pacing",
"calm": "smooth, gentle motion, peaceful transitions",
"luxurious": "elegant, sophisticated movement, premium feel",
"playful": "bouncy, fun animations, colorful and lively"
}
style_description = mood_styles.get(mood, mood)
prompt = f"""
{platform} story animation with {style_description}:
- Optimized for {spec['aspect_ratio']} aspect ratio
- {spec['style']} presentation
- Engaging visual flow that holds attention
- Brand-appropriate motion and pacing
"""
response = requests.post(
"https://api.anyapi.ai/v1/video/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "runway-gen2",
"prompt": prompt,
"image": self.encode_image(image),
"duration": spec["duration"],
"aspect_ratio": spec["aspect_ratio"],
"fps": 30
}
)
return response.json()["data"][0]["url"]
def create_explainer_animation(self, concept_image, explanation_text):
"""Create educational explainer animations"""
# Break down explanation into key points
key_points = explanation_text.split('. ')
animations = []
for i, point in enumerate(key_points):
animation_prompt = f"""
Educational explainer animation step {i+1}:
- Clear visual demonstration of: {point}
- Smooth, easy-to-follow motion
- Professional educational style
- Highlight key elements as they're explained
- Maintain visual clarity and focus
"""
step_animation = self.animate_with_style(
concept_image,
animation_prompt,
"educational",
duration=3.0
)
animations.append(step_animation)
# Combine steps into complete explainer
return self.combine_animations(animations, "educational")
def create_before_after_reveal(self, before_image, after_image, reveal_style="wipe"):
"""Create before/after reveal animations"""
reveal_styles = {
"wipe": "smooth horizontal wipe transition revealing the transformation",
"fade": "elegant cross-fade transition showing the change",
"split": "split-screen comparison with synchronized movement",
"morph": "smooth morphing transition between before and after states"
}
style_prompt = reveal_styles.get(reveal_style, reveal_style)
# First animate the before state
before_animation = self.animate_with_style(
before_image,
f"before state: subtle movement preparing for transformation, {style_prompt}",
"transformation"
)
# Then animate the after state
after_animation = self.animate_with_style(
after_image,
f"after state: revealing the final result, {style_prompt}",
"transformation"
)
# Combine with transition
response = requests.post(
"https://api.anyapi.ai/v1/video/edit",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"operation": "before_after_transition",
"before": before_animation,
"after": after_animation,
"transition": reveal_style,
"duration": 4.0
}
)
return response.json()["data"]["url"]
def animate_with_style(self, image, prompt, style, duration=3.0):
"""Helper method to animate with specific style"""
style_modifiers = {
"commercial": "professional commercial quality, studio lighting, premium feel",
"educational": "clear, focused, easy to understand, professional presentation",
"transformation": "dramatic reveal, compelling visual story, engaging pacing",
"social": "trendy, engaging, optimized for social media viewing"
}
full_prompt = f"{prompt}. {style_modifiers.get(style, '')}"
response = requests.post(
"https://api.anyapi.ai/v1/video/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "runway-gen2",
"prompt": full_prompt,
"image": self.encode_image(image),
"duration": duration,
"quality": "high"
}
)
return response.json()["data"][0]["url"]
def combine_animations(self, animation_urls, style):
"""Combine multiple animations with appropriate transitions"""
response = requests.post(
"https://api.anyapi.ai/v1/video/edit",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"operation": "sequence",
"inputs": animation_urls,
"style": style,
"auto_transitions": True
}
)
return response.json()["data"]["url"]
def encode_image(self, image_path):
"""Helper to encode image to base64"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Usage
marketing_animator = MarketingAnimator("your_api_key")
# Product showcase
product_animation = marketing_animator.create_product_showcase(
"wireless_headphones.jpg",
"Premium Wireless Headphones",
["noise cancellation", "premium materials", "comfortable fit"]
)
print(f"Product showcase: {product_animation}")
# Social media story
social_story = marketing_animator.create_social_media_story(
"brand_lifestyle.jpg",
platform="instagram",
mood="energetic"
)
print(f"Instagram story: {social_story}")
# Explainer animation
explainer = marketing_animator.create_explainer_animation(
"app_interface.jpg",
"Our app simplifies your workflow. First, connect your accounts. Then, automate repetitive tasks. Finally, track your productivity gains."
)
print(f"Explainer animation: {explainer}")
# Before/after reveal
transformation = marketing_animator.create_before_after_reveal(
"room_before.jpg",
"room_after.jpg",
"wipe"
)
print(f"Transformation reveal: {transformation}")
```
### Interactive Animation Workflows
```python theme={"system"}
class InteractiveAnimationStudio:
def __init__(self, api_key):
self.api_key = api_key
self.project_assets = {}
self.animation_history = []
def start_animation_project(self, project_name, base_image, project_type="general"):
"""Initialize a new animation project with context"""
self.project_assets = {
"name": project_name,
"base_image": base_image,
"type": project_type,
"animations": [],
"variations": []
}
# Create initial animation
initial_prompt = self.get_initial_prompt(project_type)
first_animation = self.create_base_animation(base_image, initial_prompt)
self.project_assets["animations"].append({
"name": "base_animation",
"url": first_animation,
"prompt": initial_prompt
})
return {
"project_id": project_name,
"base_animation": first_animation,
"suggestions": self.get_next_suggestions(project_type)
}
def add_animation_layer(self, layer_type, layer_description):
"""Add a new animation layer to the current project"""
base_animation = self.project_assets["animations"][-1]["url"]
layer_prompts = {
"effects": f"add visual effects: {layer_description}",
"motion": f"enhance motion: {layer_description}",
"atmosphere": f"add atmospheric elements: {layer_description}",
"lighting": f"adjust lighting and mood: {layer_description}",
"particles": f"add particle effects: {layer_description}"
}
prompt = layer_prompts.get(layer_type, layer_description)
# Apply layer to existing animation
response = requests.post(
"https://api.anyapi.ai/v1/video/edit",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"operation": "add_layer",
"base_video": base_animation,
"layer_prompt": prompt,
"blend_mode": "natural"
}
)
enhanced_animation = response.json()["data"]["url"]
self.project_assets["animations"].append({
"name": f"{layer_type}_layer",
"url": enhanced_animation,
"prompt": prompt
})
return enhanced_animation
def create_animation_variations(self, variation_count=3):
"""Create multiple variations of the current animation"""
base_animation = self.project_assets["animations"][-1]
variations = []
variation_styles = [
"more dramatic and intense version",
"softer, more elegant version",
"faster-paced, more dynamic version",
"cinematic, film-quality version",
"artistic, stylized version"
]
for i in range(variation_count):
style = variation_styles[i % len(variation_styles)]
variation_prompt = f"{base_animation['prompt']}, {style}"
response = requests.post(
"https://api.anyapi.ai/v1/video/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "runway-gen2",
"prompt": variation_prompt,
"reference_video": base_animation["url"],
"variation_strength": 0.6,
"duration": 3.0
}
)
variation_url = response.json()["data"][0]["url"]
variations.append({
"style": style,
"url": variation_url,
"prompt": variation_prompt
})
self.project_assets["variations"] = variations
return variations
def adjust_animation_timing(self, new_duration, pacing="natural"):
"""Adjust the timing and pacing of the current animation"""
current_animation = self.project_assets["animations"][-1]["url"]
pacing_styles = {
"natural": "maintain natural, realistic pacing",
"slow": "slow, contemplative pacing for emphasis",
"fast": "quick, energetic pacing for excitement",
"dramatic": "dramatic pauses and emphasis for impact"
}
pacing_instruction = pacing_styles.get(pacing, pacing)
response = requests.post(
"https://api.anyapi.ai/v1/video/edit",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"operation": "retiming",
"input_video": current_animation,
"new_duration": new_duration,
"pacing_style": pacing_instruction,
"preserve_quality": True
}
)
retimed_animation = response.json()["data"]["url"]
self.project_assets["animations"].append({
"name": "retimed_animation",
"url": retimed_animation,
"prompt": f"retimed to {new_duration}s with {pacing} pacing"
})
return retimed_animation
def export_final_animation(self, format_type="mp4", quality="high"):
"""Export the final animation in the desired format"""
final_animation = self.project_assets["animations"][-1]["url"]
response = requests.post(
"https://api.anyapi.ai/v1/video/export",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"input_video": final_animation,
"output_format": format_type,
"quality": quality,
"optimization": "web" if format_type == "mp4" else "source"
}
)
export_url = response.json()["data"]["download_url"]
return {
"download_url": export_url,
"project_summary": self.get_project_summary(),
"total_animations": len(self.project_assets["animations"])
}
def get_initial_prompt(self, project_type):
"""Get appropriate initial prompt based on project type"""
initial_prompts = {
"marketing": "professional, eye-catching animation suitable for marketing",
"artistic": "creative, artistic animation with beautiful visual flow",
"educational": "clear, informative animation that explains concepts well",
"social": "engaging, trendy animation perfect for social media",
"cinematic": "film-quality animation with dramatic cinematography"
}
return initial_prompts.get(project_type, "high-quality, engaging animation")
def get_next_suggestions(self, project_type):
"""Get contextual suggestions for next steps"""
suggestions = {
"marketing": [
"Add product highlight effects",
"Create brand-colored overlays",
"Add call-to-action elements"
],
"artistic": [
"Experiment with particle effects",
"Try different color grading",
"Add abstract motion elements"
],
"educational": [
"Add explanatory text overlays",
"Highlight key learning points",
"Create step-by-step reveals"
]
}
return suggestions.get(project_type, [
"Add motion effects",
"Adjust lighting and mood",
"Create variations"
])
def get_project_summary(self):
"""Get a summary of the current project"""
return {
"project_name": self.project_assets["name"],
"total_animations": len(self.project_assets["animations"]),
"variations_created": len(self.project_assets.get("variations", [])),
"final_animation": self.project_assets["animations"][-1]["url"] if self.project_assets["animations"] else None
}
# Usage
studio = InteractiveAnimationStudio("your_api_key")
# Start a marketing project
project = studio.start_animation_project(
"Product Launch Campaign",
"new_product.jpg",
"marketing"
)
print(f"Started project: {project['project_id']}")
print(f"Base animation: {project['base_animation']}")
# Add effects layer
enhanced = studio.add_animation_layer(
"effects",
"golden particle effects that highlight the product's premium quality"
)
print(f"Enhanced with effects: {enhanced}")
# Create variations
variations = studio.create_animation_variations(3)
print(f"Created {len(variations)} variations")
# Adjust timing
final_timing = studio.adjust_animation_timing(5.0, "dramatic")
print(f"Final timing adjustment: {final_timing}")
# Export final result
export_result = studio.export_final_animation("mp4", "high")
print(f"Export complete: {export_result['download_url']}")
print(f"Project summary: {export_result['project_summary']}")
```
## Animation Styles and Effects
### Popular Animation Techniques
| Technique | Description | Best For |
| ----------------------- | ------------------------------- | ---------------------------- |
| **Cinemagraph** | Subtle motion in specific areas | Social media, websites |
| **Parallax** | Multi-layer depth movement | Immersive storytelling |
| **Morphing** | Smooth shape transformations | Product reveals, transitions |
| **Particle Effects** | Dynamic particle systems | Magic, energy, atmosphere |
| **Character Animation** | Natural character movement | Marketing, storytelling |
| **Camera Motion** | Virtual camera movements | Cinematic reveals |
### Effect Categories
Wind, water, fire, snow, clouds, organic movement
Particles, energy beams, glowing elements, sparkles
Camera moves, lighting changes, depth of field
Text animations, UI elements, geometric shapes
## Best Practices
### 1. Animation Planning
* **Storyboard first**: Plan the motion before creating
* **Consider context**: Match animation style to purpose
* **Duration matters**: Optimize length for platform and use case
* **Smooth transitions**: Ensure natural, believable movement
### 2. Technical Optimization
* **Frame rate**: Use appropriate FPS for smooth motion
* **Resolution**: Balance quality with file size requirements
* **Compression**: Optimize for web delivery when needed
* **Format selection**: Choose the right format for your platform
### 3. Creative Guidelines
* **Subtle is powerful**: Often less motion is more effective
* **Consistent style**: Maintain visual coherence throughout
* **Physics realism**: Respect natural motion laws
* **Timing variation**: Use varied timing for visual interest
### 4. Platform Optimization
* **Social media**: Short, attention-grabbing animations
* **Marketing**: Professional, brand-consistent motion
* **Education**: Clear, easy-to-follow movements
* **Entertainment**: Dramatic, engaging storytelling
## Common Use Cases
Product showcases, brand animations, social media content
Instagram stories, TikTok videos, engaging posts
Dynamic slides, concept visualizations, data animations
Hero banners, loading animations, interactive elements
Explainer videos, process demonstrations, tutorials
Art installations, experimental videos, personal projects
Product demos, before/after reveals, feature highlights
Short films, music videos, creative storytelling
## Model Recommendations
| Animation Type | Recommended Model | Strengths |
| ----------------------- | ---------------------- | ------------------------------ |
| **Realistic Motion** | Runway Gen-2 | Natural movement, high quality |
| **Artistic Styles** | Stable Video Diffusion | Creative flexibility, styles |
| **Character Animation** | Runway Gen-2 | Facial expressions, gestures |
| **Product Showcases** | Runway Gen-2 | Commercial quality, lighting |
| **Quick Prototypes** | Pika Labs | Fast generation, iterations |
## Getting Started
Create your first animated image
Start with static image generation
Learn about vision capabilities
# Create Assistant
Source: https://docs.anyapi.ai/use-cases/create-assistant
Build intelligent AI assistants with custom personalities, knowledge, and capabilities
# Create AI Assistant
Build powerful, customizable AI assistants with specialized knowledge, unique personalities, and advanced capabilities. Perfect for customer support, personal productivity, education, and business automation.
## Overview
AI assistants can be customized for:
* **Specialized knowledge** - Domain expertise in specific fields
* **Custom personalities** - Unique voice, tone, and communication style
* **Advanced capabilities** - Function calling, web search, file processing
* **Role-specific behavior** - Tailored responses for different use cases
* **Memory and context** - Persistent conversations and learning
Train on specific documents, data, and expertise
Integrate with APIs, databases, and external tools
Create unique voice, tone, and communication style
Handle text, images, audio, and documents
## Quick Start
```python Python theme={"system"}
import requests
import json
from datetime import datetime
class AIAssistant:
def __init__(self, api_key, assistant_config=None):
self.api_key = api_key
self.config = assistant_config or self.get_default_config()
self.conversation_history = []
self.memory = {}
def get_default_config(self):
"""Default assistant configuration"""
return {
"name": "Assistant",
"role": "helpful AI assistant",
"personality": "friendly, professional, and knowledgeable",
"expertise": [],
"capabilities": ["conversation", "analysis", "problem_solving"],
"response_style": "clear and concise",
"model": "openai/gpt-4o"
}
def chat(self, user_message, context=None):
"""Have a conversation with the assistant"""
# Build system prompt from configuration
system_prompt = self.build_system_prompt()
# Add context if provided
if context:
user_message = f"Context: {context}\n\nUser: {user_message}"
# Build conversation with history
messages = [{"role": "system", "content": system_prompt}]
# Add conversation history (last 10 exchanges)
messages.extend(self.conversation_history[-20:])
# Add current message
messages.append({"role": "user", "content": user_message})
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": self.config["model"],
"messages": messages,
"temperature": 0.7,
"max_tokens": 1000
}
)
assistant_response = response.json()["choices"][0]["message"]["content"]
# Update conversation history
self.conversation_history.extend([
{"role": "user", "content": user_message},
{"role": "assistant", "content": assistant_response}
])
return assistant_response
def build_system_prompt(self):
"""Build system prompt from configuration"""
prompt = f"""You are {self.config['name']}, a {self.config['role']}.
Personality: {self.config['personality']}
Response Style: {self.config['response_style']}"""
if self.config['expertise']:
prompt += f"\n\nExpertise: You are knowledgeable in {', '.join(self.config['expertise'])}"
if self.config['capabilities']:
prompt += f"\nCapabilities: {', '.join(self.config['capabilities'])}"
prompt += "\n\nAlways maintain your personality and provide helpful, accurate responses."
return prompt
def update_personality(self, personality_traits):
"""Update assistant personality"""
self.config.update(personality_traits)
def add_expertise(self, domain, knowledge_base=None):
"""Add domain expertise to the assistant"""
if domain not in self.config['expertise']:
self.config['expertise'].append(domain)
if knowledge_base:
self.memory[f"expertise_{domain}"] = knowledge_base
def set_context_memory(self, key, value):
"""Set persistent memory for the assistant"""
self.memory[key] = value
def get_context_memory(self, key):
"""Retrieve from persistent memory"""
return self.memory.get(key)
# Usage examples
# Create a basic assistant
assistant = AIAssistant("your_api_key")
response = assistant.chat("Hello! Can you help me with Python programming?")
print(f"Assistant: {response}")
# Create a specialized customer support assistant
support_config = {
"name": "SupportBot",
"role": "customer support specialist",
"personality": "patient, empathetic, and solution-focused",
"expertise": ["product knowledge", "troubleshooting", "customer service"],
"response_style": "helpful and professional with step-by-step guidance",
"model": "openai/gpt-4o"
}
support_assistant = AIAssistant("your_api_key", support_config)
response = support_assistant.chat("I'm having trouble with my order")
print(f"Support: {response}")
# Create a technical assistant with expertise
tech_config = {
"name": "TechAdvisor",
"role": "senior software engineer and architect",
"personality": "analytical, precise, and thorough",
"expertise": ["software architecture", "system design", "best practices"],
"response_style": "detailed technical explanations with examples",
"model": "openai/gpt-4o"
}
tech_assistant = AIAssistant("your_api_key", tech_config)
response = tech_assistant.chat("How should I design a scalable microservices architecture?")
print(f"Tech Advisor: {response}")
```
```javascript JavaScript theme={"system"}
class AIAssistant {
constructor(apiKey, assistantConfig = null) {
this.apiKey = apiKey;
this.config = assistantConfig || this.getDefaultConfig();
this.conversationHistory = [];
this.memory = {};
}
getDefaultConfig() {
return {
name: 'Assistant',
role: 'helpful AI assistant',
personality: 'friendly, professional, and knowledgeable',
expertise: [],
capabilities: ['conversation', 'analysis', 'problem_solving'],
responseStyle: 'clear and concise',
model: 'openai/gpt-4o'
};
}
async chat(userMessage, context = null) {
// Build system prompt
const systemPrompt = this.buildSystemPrompt();
// Add context if provided
if (context) {
userMessage = `Context: ${context}\n\nUser: ${userMessage}`;
}
// Build messages with history
const messages = [{ role: 'system', content: systemPrompt }];
// Add conversation history (last 20 messages)
messages.push(...this.conversationHistory.slice(-20));
// Add current message
messages.push({ role: 'user', content: userMessage });
const response = await fetch('https://api.anyapi.ai/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: this.config.model,
messages: messages,
temperature: 0.7,
max_tokens: 1000
})
});
const data = await response.json();
const assistantResponse = data.choices[0].message.content;
// Update conversation history
this.conversationHistory.push(
{ role: 'user', content: userMessage },
{ role: 'assistant', content: assistantResponse }
);
return assistantResponse;
}
buildSystemPrompt() {
let prompt = `You are ${this.config.name}, a ${this.config.role}.
Personality: ${this.config.personality}
Response Style: ${this.config.responseStyle}`;
if (this.config.expertise.length > 0) {
prompt += `\n\nExpertise: You are knowledgeable in ${this.config.expertise.join(', ')}`;
}
if (this.config.capabilities.length > 0) {
prompt += `\nCapabilities: ${this.config.capabilities.join(', ')}`;
}
prompt += '\n\nAlways maintain your personality and provide helpful, accurate responses.';
return prompt;
}
updatePersonality(personalityTraits) {
Object.assign(this.config, personalityTraits);
}
addExpertise(domain, knowledgeBase = null) {
if (!this.config.expertise.includes(domain)) {
this.config.expertise.push(domain);
}
if (knowledgeBase) {
this.memory[`expertise_${domain}`] = knowledgeBase;
}
}
setContextMemory(key, value) {
this.memory[key] = value;
}
getContextMemory(key) {
return this.memory[key];
}
}
// Usage examples
// Create a basic assistant
const assistant = new AIAssistant('your_api_key');
assistant.chat('Hello! Can you help me with web development?')
.then(response => console.log('Assistant:', response));
// Create a specialized marketing assistant
const marketingConfig = {
name: 'MarketingGuru',
role: 'digital marketing specialist',
personality: 'creative, data-driven, and enthusiastic',
expertise: ['social media', 'content marketing', 'SEO', 'analytics'],
responseStyle: 'actionable advice with creative ideas',
model: 'openai/gpt-4o'
};
const marketingAssistant = new AIAssistant('your_api_key', marketingConfig);
marketingAssistant.chat('How can I improve my social media engagement?')
.then(response => console.log('Marketing Guru:', response));
```
## Advanced Assistant Features
### Function-Enabled Assistant
```python theme={"system"}
class FunctionEnabledAssistant(AIAssistant):
def __init__(self, api_key, assistant_config=None):
super().__init__(api_key, assistant_config)
self.available_functions = {}
self.setup_default_functions()
def setup_default_functions(self):
"""Setup default function capabilities"""
self.available_functions = {
"get_current_time": {
"function": self.get_current_time,
"description": "Get the current date and time",
"parameters": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "Timezone (optional, defaults to UTC)"
}
}
}
},
"search_web": {
"function": self.search_web,
"description": "Search the web for current information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
},
"num_results": {
"type": "integer",
"description": "Number of results to return",
"default": 5
}
},
"required": ["query"]
}
},
"calculate": {
"function": self.calculate,
"description": "Perform mathematical calculations",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate"
}
},
"required": ["expression"]
}
},
"send_email": {
"function": self.send_email,
"description": "Send an email",
"parameters": {
"type": "object",
"properties": {
"to": {"type": "string", "description": "Recipient email"},
"subject": {"type": "string", "description": "Email subject"},
"body": {"type": "string", "description": "Email body"}
},
"required": ["to", "subject", "body"]
}
}
}
def add_custom_function(self, name, function, description, parameters):
"""Add a custom function to the assistant"""
self.available_functions[name] = {
"function": function,
"description": description,
"parameters": parameters
}
def chat_with_functions(self, user_message, context=None):
"""Chat with function calling capabilities"""
system_prompt = self.build_system_prompt()
system_prompt += "\n\nYou have access to functions. Use them when appropriate to help the user."
if context:
user_message = f"Context: {context}\n\nUser: {user_message}"
# Prepare function definitions for the API
functions = []
for name, func_data in self.available_functions.items():
functions.append({
"name": name,
"description": func_data["description"],
"parameters": func_data["parameters"]
})
messages = [{"role": "system", "content": system_prompt}]
messages.extend(self.conversation_history[-20:])
messages.append({"role": "user", "content": user_message})
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": self.config["model"],
"messages": messages,
"functions": functions,
"function_call": "auto",
"temperature": 0.7
}
)
response_data = response.json()
message = response_data["choices"][0]["message"]
# Handle function calls
if message.get("function_call"):
function_name = message["function_call"]["name"]
function_args = json.loads(message["function_call"]["arguments"])
# Execute the function
if function_name in self.available_functions:
function_result = self.available_functions[function_name]["function"](**function_args)
# Continue conversation with function result
messages.append(message)
messages.append({
"role": "function",
"name": function_name,
"content": json.dumps(function_result)
})
# Get final response
final_response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": self.config["model"],
"messages": messages,
"temperature": 0.7
}
)
assistant_response = final_response.json()["choices"][0]["message"]["content"]
# Update history
self.conversation_history.extend([
{"role": "user", "content": user_message},
{"role": "assistant", "content": assistant_response}
])
return assistant_response
# No function call needed
assistant_response = message["content"]
self.conversation_history.extend([
{"role": "user", "content": user_message},
{"role": "assistant", "content": assistant_response}
])
return assistant_response
def get_current_time(self, timezone="UTC"):
"""Function to get current time"""
from datetime import datetime
import pytz
try:
tz = pytz.timezone(timezone)
current_time = datetime.now(tz)
return {
"current_time": current_time.strftime("%Y-%m-%d %H:%M:%S %Z"),
"timezone": timezone
}
except:
utc_time = datetime.utcnow()
return {
"current_time": utc_time.strftime("%Y-%m-%d %H:%M:%S UTC"),
"timezone": "UTC"
}
def search_web(self, query, num_results=5):
"""Function to search the web"""
# Implement web search (placeholder)
return {
"query": query,
"results": [
{
"title": f"Search result for: {query}",
"url": "https://example.com",
"snippet": "This is a sample search result."
}
],
"note": "This is a placeholder. Integrate with actual search API."
}
def calculate(self, expression):
"""Function to perform calculations"""
try:
# Safe evaluation of mathematical expressions
import ast
import operator
# Supported operations
ops = {
ast.Add: operator.add,
ast.Sub: operator.sub,
ast.Mult: operator.mul,
ast.Div: operator.truediv,
ast.Pow: operator.pow,
ast.USub: operator.neg,
}
def eval_expr(node):
if isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.BinOp):
return ops[type(node.op)](eval_expr(node.left), eval_expr(node.right))
elif isinstance(node, ast.UnaryOp):
return ops[type(node.op)](eval_expr(node.operand))
else:
raise TypeError(node)
result = eval_expr(ast.parse(expression, mode='eval').body)
return {
"expression": expression,
"result": result
}
except Exception as e:
return {
"expression": expression,
"error": f"Calculation error: {str(e)}"
}
def send_email(self, to, subject, body):
"""Function to send email (placeholder)"""
# Implement email sending (placeholder)
return {
"status": "success",
"message": f"Email sent to {to}",
"note": "This is a placeholder. Integrate with actual email service."
}
# Usage
function_assistant = FunctionEnabledAssistant("your_api_key", {
"name": "FunctionBot",
"role": "multi-capability assistant with tools",
"personality": "helpful and resourceful",
"capabilities": ["conversation", "web search", "calculations", "email"],
"model": "openai/gpt-4o"
})
# Example usage
response = function_assistant.chat_with_functions("What time is it in Tokyo?")
print(f"Assistant: {response}")
response = function_assistant.chat_with_functions("Calculate 15% tip on a $67.50 bill")
print(f"Assistant: {response}")
```
### Knowledge-Based Assistant
```python theme={"system"}
class KnowledgeAssistant(AIAssistant):
def __init__(self, api_key, assistant_config=None):
super().__init__(api_key, assistant_config)
self.knowledge_base = {}
self.document_embeddings = {}
def add_knowledge_from_text(self, knowledge_key, text_content, source=None):
"""Add knowledge from text content"""
# Split text into chunks for better processing
chunks = self.chunk_text(text_content, chunk_size=500)
# Generate embeddings for each chunk
embeddings = []
for chunk in chunks:
embedding = self.generate_embedding(chunk)
embeddings.append({
"text": chunk,
"embedding": embedding,
"source": source
})
self.knowledge_base[knowledge_key] = {
"content": text_content,
"chunks": embeddings,
"source": source,
"added_at": datetime.now().isoformat()
}
def add_knowledge_from_file(self, knowledge_key, file_path):
"""Add knowledge from a file"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
self.add_knowledge_from_text(knowledge_key, content, source=file_path)
return f"Knowledge added from {file_path}"
except Exception as e:
return f"Error reading file: {str(e)}"
def search_knowledge(self, query, top_k=3):
"""Search through knowledge base for relevant information"""
query_embedding = self.generate_embedding(query)
# Search through all knowledge chunks
results = []
for knowledge_key, knowledge_data in self.knowledge_base.items():
for chunk_data in knowledge_data["chunks"]:
similarity = self.cosine_similarity(query_embedding, chunk_data["embedding"])
if similarity > 0.3: # Minimum relevance threshold
results.append({
"knowledge_key": knowledge_key,
"text": chunk_data["text"],
"similarity": similarity,
"source": chunk_data["source"]
})
# Sort by similarity and return top results
results.sort(key=lambda x: x["similarity"], reverse=True)
return results[:top_k]
def chat_with_knowledge(self, user_message, context=None):
"""Chat using knowledge base for enhanced responses"""
# Search knowledge base for relevant information
relevant_knowledge = self.search_knowledge(user_message)
# Build enhanced system prompt with knowledge
system_prompt = self.build_system_prompt()
if relevant_knowledge:
knowledge_context = "\n\n".join([
f"Knowledge from {item['knowledge_key']}: {item['text']}"
for item in relevant_knowledge
])
system_prompt += f"\n\nRelevant Knowledge:\n{knowledge_context}\n\nUse this knowledge to provide accurate, informed responses."
if context:
user_message = f"Context: {context}\n\nUser: {user_message}"
messages = [{"role": "system", "content": system_prompt}]
messages.extend(self.conversation_history[-20:])
messages.append({"role": "user", "content": user_message})
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": self.config["model"],
"messages": messages,
"temperature": 0.7,
"max_tokens": 1000
}
)
assistant_response = response.json()["choices"][0]["message"]["content"]
# Update conversation history
self.conversation_history.extend([
{"role": "user", "content": user_message},
{"role": "assistant", "content": assistant_response}
])
return {
"response": assistant_response,
"knowledge_used": [item["knowledge_key"] for item in relevant_knowledge],
"sources": [item["source"] for item in relevant_knowledge if item["source"]]
}
def generate_embedding(self, text):
"""Generate embedding for text"""
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "text-embedding-3-large",
"input": text
}
)
return response.json()["data"][0]["embedding"]
def chunk_text(self, text, chunk_size=500, overlap=50):
"""Split text into overlapping chunks"""
words = text.split()
chunks = []
for i in range(0, len(words), chunk_size - overlap):
chunk_words = words[i:i + chunk_size]
chunk_text = " ".join(chunk_words)
chunks.append(chunk_text)
if i + chunk_size >= len(words):
break
return chunks
def cosine_similarity(self, vec1, vec2):
"""Calculate cosine similarity between two vectors"""
import numpy as np
vec1 = np.array(vec1)
vec2 = np.array(vec2)
dot_product = np.dot(vec1, vec2)
magnitude1 = np.linalg.norm(vec1)
magnitude2 = np.linalg.norm(vec2)
if magnitude1 == 0 or magnitude2 == 0:
return 0
return dot_product / (magnitude1 * magnitude2)
def list_knowledge(self):
"""List all knowledge in the base"""
knowledge_summary = []
for key, data in self.knowledge_base.items():
knowledge_summary.append({
"key": key,
"source": data["source"],
"chunk_count": len(data["chunks"]),
"added_at": data["added_at"]
})
return knowledge_summary
# Usage
knowledge_assistant = KnowledgeAssistant("your_api_key", {
"name": "KnowledgeBot",
"role": "knowledge-enhanced assistant",
"personality": "scholarly, accurate, and helpful",
"capabilities": ["conversation", "knowledge search", "document analysis"],
"model": "openai/gpt-4o"
})
# Add knowledge
knowledge_assistant.add_knowledge_from_text(
"company_policies",
"""
Our company offers flexible work arrangements including remote work options.
Employees can work from home up to 3 days per week with manager approval.
We provide comprehensive health insurance including dental and vision coverage.
Vacation policy allows for 20 days of PTO per year, increasing with tenure.
""",
source="Employee Handbook 2024"
)
# Chat with knowledge
result = knowledge_assistant.chat_with_knowledge(
"What are the work from home policies?"
)
print(f"Response: {result['response']}")
print(f"Knowledge used: {result['knowledge_used']}")
print(f"Sources: {result['sources']}")
```
### Multi-Modal Assistant
```python theme={"system"}
class MultiModalAssistant(AIAssistant):
def __init__(self, api_key, assistant_config=None):
super().__init__(api_key, assistant_config)
self.supported_modes = ["text", "image", "document"]
def process_image(self, image_path, user_question=None):
"""Process and analyze images"""
import base64
# Encode image to base64
with open(image_path, "rb") as image_file:
image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
prompt = user_question or "Analyze this image and describe what you see in detail."
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/gpt-4o",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_base64}"
}
}
]
}
],
"max_tokens": 1000
}
)
return response.json()["choices"][0]["message"]["content"]
def process_document(self, document_path, question=None):
"""Process documents (PDF, DOCX, TXT)"""
# Extract text from document
document_text = self.extract_document_text(document_path)
if question:
response = self.chat(f"Based on this document: {document_text}\n\nQuestion: {question}")
else:
response = self.chat(f"Summarize this document: {document_text}")
return {
"document_content": document_text[:500] + "...", # Preview
"response": response
}
def multi_modal_chat(self, inputs, question=None):
"""Handle multiple input types in one conversation"""
results = {}
combined_context = []
for input_type, input_path in inputs.items():
if input_type == "image":
result = self.process_image(input_path, question)
results["image_analysis"] = result
combined_context.append(f"Image analysis: {result}")
elif input_type == "document":
result = self.process_document(input_path, question)
results["document_analysis"] = result
combined_context.append(f"Document content: {result['document_content']}")
# Synthesize all inputs
if len(combined_context) > 1:
synthesis_prompt = f"""
Based on all the analyzed content:
{chr(10).join(combined_context)}
Question: {question or 'Provide a comprehensive analysis of all the provided content.'}
"""
synthesis = self.chat(synthesis_prompt)
results["synthesis"] = synthesis
return results
def extract_document_text(self, document_path):
"""Extract text from various document formats"""
import os
file_extension = os.path.splitext(document_path)[1].lower()
try:
if file_extension == '.txt':
with open(document_path, 'r', encoding='utf-8') as file:
return file.read()
elif file_extension == '.pdf':
# Placeholder for PDF extraction
return "PDF content extraction would be implemented here."
elif file_extension in ['.docx', '.doc']:
# Placeholder for Word document extraction
return "Word document extraction would be implemented here."
else:
return f"Unsupported document type: {file_extension}"
except Exception as e:
return f"Error extracting document: {str(e)}"
# Usage
multimodal_assistant = MultiModalAssistant("your_api_key", {
"name": "MultiBot",
"role": "multi-modal AI assistant",
"personality": "analytical and comprehensive",
"capabilities": ["text", "image analysis", "document analysis"],
"model": "openai/gpt-4o"
})
# Process single image
image_analysis = multimodal_assistant.process_image(
"chart.jpg",
"What insights can you extract from this business chart?"
)
print(f"Image analysis: {image_analysis}")
# Multi-modal analysis
results = multimodal_assistant.multi_modal_chat({
"image": "presentation_slide.jpg",
"document": "report.pdf"
}, "What are the key takeaways from this presentation?")
print(f"Synthesis: {results['synthesis']}")
```
## Assistant Specialized Roles
### Customer Support Assistant
```python theme={"system"}
def create_support_assistant(api_key, company_info):
"""Create a specialized customer support assistant"""
config = {
"name": "SupportAgent",
"role": "customer support specialist",
"personality": "patient, empathetic, solution-focused, and professional",
"expertise": ["product knowledge", "troubleshooting", "policy information"],
"response_style": "clear step-by-step guidance with empathetic communication",
"model": "openai/gpt-4o"
}
assistant = FunctionEnabledAssistant(api_key, config)
# Add company-specific knowledge
assistant.add_knowledge_from_text("company_info", company_info)
# Add support-specific functions
assistant.add_custom_function(
"create_ticket",
lambda issue, priority: {"ticket_id": f"TICK-{hash(issue) % 10000}", "status": "created"},
"Create a support ticket",
{
"type": "object",
"properties": {
"issue": {"type": "string", "description": "Issue description"},
"priority": {"type": "string", "description": "Priority level"}
},
"required": ["issue", "priority"]
}
)
return assistant
```
### Educational Tutor Assistant
```python theme={"system"}
def create_tutor_assistant(api_key, subject_area):
"""Create an educational tutor assistant"""
config = {
"name": f"{subject_area}Tutor",
"role": f"expert {subject_area} tutor",
"personality": "encouraging, patient, clear, and adaptive to learning styles",
"expertise": [subject_area, "pedagogy", "learning techniques"],
"response_style": "educational with examples, practice questions, and explanations",
"model": "openai/gpt-4o"
}
assistant = AIAssistant(api_key, config)
# Add educational methods
def create_practice_quiz(topic, difficulty):
return {
"quiz_questions": [
f"Question about {topic} at {difficulty} level",
"Sample multiple choice question",
"Practice problem to solve"
],
"note": "This would generate actual quiz questions"
}
assistant.add_custom_function(
"create_practice_quiz",
create_practice_quiz,
"Create practice quiz questions",
{
"type": "object",
"properties": {
"topic": {"type": "string", "description": "Quiz topic"},
"difficulty": {"type": "string", "description": "Difficulty level"}
},
"required": ["topic", "difficulty"]
}
)
return assistant
```
### Business Analyst Assistant
```python theme={"system"}
def create_analyst_assistant(api_key):
"""Create a business analyst assistant"""
config = {
"name": "AnalystBot",
"role": "senior business analyst",
"personality": "analytical, data-driven, strategic, and thorough",
"expertise": ["data analysis", "business strategy", "market research", "KPIs"],
"response_style": "detailed analysis with data-backed recommendations",
"model": "openai/gpt-4o"
}
assistant = FunctionEnabledAssistant(api_key, config)
# Add analysis functions
def analyze_data(data_description, analysis_type):
return {
"analysis_type": analysis_type,
"data_summary": f"Analysis of {data_description}",
"insights": ["Key insight 1", "Key insight 2", "Key insight 3"],
"recommendations": ["Recommendation 1", "Recommendation 2"],
"note": "This would perform actual data analysis"
}
assistant.add_custom_function(
"analyze_data",
analyze_data,
"Analyze business data",
{
"type": "object",
"properties": {
"data_description": {"type": "string", "description": "Description of data to analyze"},
"analysis_type": {"type": "string", "description": "Type of analysis needed"}
},
"required": ["data_description", "analysis_type"]
}
)
return assistant
```
## Best Practices
### 1. Assistant Design
* **Clear purpose**: Define specific role and capabilities
* **Consistent personality**: Maintain voice and tone throughout
* **Appropriate expertise**: Match knowledge to use case
* **User-focused**: Design for specific user needs and workflows
### 2. Knowledge Management
* **Structured data**: Organize knowledge logically
* **Regular updates**: Keep information current and accurate
* **Source tracking**: Maintain clear source attribution
* **Quality control**: Verify accuracy of knowledge base
### 3. Function Integration
* **Essential functions**: Add only necessary capabilities
* **Error handling**: Implement robust error management
* **Security**: Validate inputs and protect sensitive operations
* **Documentation**: Clear function descriptions and parameters
### 4. Conversation Flow
* **Context awareness**: Maintain conversation context
* **Memory management**: Handle long conversations efficiently
* **Response relevance**: Ensure responses match user intent
* **Natural interaction**: Create smooth, human-like exchanges
## Common Use Cases
Help desk automation, FAQ assistance, ticket management
Task management, scheduling, information organization
Tutoring, skill development, knowledge assessment
Data analysis, reporting, workflow automation
Writing assistance, idea generation, content optimization
Information gathering, data interpretation, insights
Code assistance, troubleshooting, documentation
Brainstorming, design feedback, creative direction
## Model Recommendations
| Assistant Type | Recommended Model | Reasoning |
| -------------------- | ----------------- | -------------------------------- |
| **General Purpose** | GPT-4o | Balanced capabilities, reliable |
| **Technical/Code** | Claude 3.5 Sonnet | Strong analytical abilities |
| **Creative Tasks** | GPT-4o | Good creativity and ideation |
| **Data Analysis** | Claude 3.5 Sonnet | Excellent reasoning capabilities |
| **Customer Support** | GPT-4o | Natural conversation flow |
| **Educational** | Claude 3.5 Sonnet | Clear explanations, patient tone |
## Getting Started
Build your first AI assistant
Explore text generation models
# Create Embeddings
Source: https://docs.anyapi.ai/use-cases/create-embeddings
Convert text to numerical vectors for search, similarity, and classification with AnyAPI
# Create Embeddings
Transform text into high-dimensional vector representations for semantic search, similarity matching, content classification, and AI-powered applications. Perfect for search engines, recommendation systems, knowledge bases, and content analysis.
## Overview
Embeddings enable:
* **Semantic search** - Find content by meaning, not just keywords
* **Similarity matching** - Compare and group related content
* **Content classification** - Categorize and organize information automatically
* **Knowledge retrieval** - Build intelligent search and Q\&A systems
* **Recommendation engines** - Suggest relevant content to users
### Key Capabilities
**Multi-Language Support**\
Generate embeddings for text in multiple languages
**Batch Processing**\
Process multiple texts efficiently in single requests
**High Precision**\
Latest embedding models for superior accuracy
**Flexible Integration**\
Easy integration with vector databases and search systems
## Quick Start
```python theme={"system"}
import requests
import numpy as np
from typing import List, Dict, Any
class EmbeddingGenerator:
def __init__(self, api_key: str, model: str = "text-embedding-3-large"):
self.api_key = api_key
self.model = model
self.base_url = "https://api.anyapi.ai/v1"
def create_embedding(self, text: str, **kwargs) -> List[float]:
"""Generate embedding for a single text"""
response = requests.post(
f"{self.base_url}/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": self.model,
"input": text,
**kwargs
}
)
if response.status_code == 200:
return response.json()["data"][0]["embedding"]
else:
raise Exception(f"API Error: {response.status_code} - {response.text}")
def create_embeddings_batch(self, texts: List[str], **kwargs) -> List[List[float]]:
"""Generate embeddings for multiple texts"""
response = requests.post(
f"{self.base_url}/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": self.model,
"input": texts,
**kwargs
}
)
if response.status_code == 200:
return [item["embedding"] for item in response.json()["data"]]
else:
raise Exception(f"API Error: {response.status_code} - {response.text}")
def get_embedding_info(self) -> Dict[str, Any]:
"""Get information about the embedding model"""
# Model dimension information
model_info = {
"text-embedding-3-small": {"dimensions": 1536, "max_tokens": 8191},
"text-embedding-3-large": {"dimensions": 3072, "max_tokens": 8191},
"text-embedding-ada-002": {"dimensions": 1536, "max_tokens": 8191}
}
return model_info.get(self.model, {"dimensions": "unknown", "max_tokens": "unknown"})
# Basic usage
embedding_gen = EmbeddingGenerator("your_api_key")
# Single text embedding
text = "Artificial intelligence is transforming how we work and live."
embedding = embedding_gen.create_embedding(text)
print(f"Embedding dimension: {len(embedding)}")
print(f"First 5 values: {embedding[:5]}")
# Batch processing
texts = [
"Machine learning enables computers to learn without explicit programming.",
"Deep learning uses neural networks with multiple layers.",
"Natural language processing helps computers understand human language.",
"Computer vision allows machines to interpret visual information."
]
embeddings = embedding_gen.create_embeddings_batch(texts)
print(f"Generated {len(embeddings)} embeddings")
print(f"Each embedding has {len(embeddings[0])} dimensions")
# Model information
info = embedding_gen.get_embedding_info()
print(f"Model info: {info}")
```
## Advanced Embedding Operations
### Similarity Calculator
```python theme={"system"}
class SimilarityCalculator:
def __init__(self, embedding_generator: EmbeddingGenerator):
self.embedding_gen = embedding_generator
def cosine_similarity(self, vec1: List[float], vec2: List[float]) -> float:
"""Calculate cosine similarity between two vectors"""
vec1 = np.array(vec1)
vec2 = np.array(vec2)
dot_product = np.dot(vec1, vec2)
magnitude1 = np.linalg.norm(vec1)
magnitude2 = np.linalg.norm(vec2)
if magnitude1 == 0 or magnitude2 == 0:
return 0.0
return dot_product / (magnitude1 * magnitude2)
def euclidean_distance(self, vec1: List[float], vec2: List[float]) -> float:
"""Calculate Euclidean distance between two vectors"""
vec1 = np.array(vec1)
vec2 = np.array(vec2)
return np.linalg.norm(vec1 - vec2)
def find_most_similar(self, query_text: str, candidate_texts: List[str], top_k: int = 5) -> List[Dict]:
"""Find most similar texts to a query"""
# Generate embeddings
query_embedding = self.embedding_gen.create_embedding(query_text)
candidate_embeddings = self.embedding_gen.create_embeddings_batch(candidate_texts)
# Calculate similarities
similarities = []
for i, candidate_embedding in enumerate(candidate_embeddings):
similarity = self.cosine_similarity(query_embedding, candidate_embedding)
similarities.append({
"text": candidate_texts[i],
"similarity": similarity,
"index": i
})
# Sort by similarity and return top_k
similarities.sort(key=lambda x: x["similarity"], reverse=True)
return similarities[:top_k]
def cluster_texts(self, texts: List[str], n_clusters: int = 3) -> Dict[str, List[str]]:
"""Simple clustering of texts based on embeddings"""
from sklearn.cluster import KMeans
# Generate embeddings
embeddings = self.embedding_gen.create_embeddings_batch(texts)
# Perform clustering
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
cluster_labels = kmeans.fit_predict(embeddings)
# Group texts by cluster
clusters = {}
for i, label in enumerate(cluster_labels):
cluster_key = f"cluster_{label}"
if cluster_key not in clusters:
clusters[cluster_key] = []
clusters[cluster_key].append(texts[i])
return clusters
# Usage
embedding_gen = EmbeddingGenerator("your_api_key")
similarity_calc = SimilarityCalculator(embedding_gen)
# Find similar texts
query = "machine learning algorithms"
candidates = [
"Neural networks are a type of machine learning model",
"Supervised learning requires labeled training data",
"The weather is nice today",
"Deep learning is a subset of machine learning",
"I enjoy cooking pasta for dinner",
"Unsupervised learning finds patterns in unlabeled data"
]
similar_texts = similarity_calc.find_most_similar(query, candidates, top_k=3)
for result in similar_texts:
print(f"Similarity: {result['similarity']:.3f} - {result['text']}")
# Cluster texts
tech_texts = [
"Artificial intelligence and machine learning",
"Software development and programming",
"Cooking recipes and food preparation",
"Travel destinations and vacation planning",
"Data science and analytics",
"Restaurant reviews and dining experiences",
"Computer vision and image recognition",
"Adventure sports and outdoor activities"
]
clusters = similarity_calc.cluster_texts(tech_texts, n_clusters=3)
for cluster_name, cluster_texts in clusters.items():
print(f"\n{cluster_name}:")
for text in cluster_texts:
print(f" - {text}")
```
### Vector Database Integration
```python theme={"system"}
class VectorDatabase:
def __init__(self, embedding_generator: EmbeddingGenerator):
self.embedding_gen = embedding_generator
self.documents = []
self.embeddings = []
self.metadata = []
def add_document(self, text: str, metadata: Dict = None):
"""Add a single document to the database"""
embedding = self.embedding_gen.create_embedding(text)
self.documents.append(text)
self.embeddings.append(embedding)
self.metadata.append(metadata or {})
return len(self.documents) - 1 # Return document ID
def add_documents_batch(self, texts: List[str], metadata_list: List[Dict] = None):
"""Add multiple documents efficiently"""
embeddings = self.embedding_gen.create_embeddings_batch(texts)
if metadata_list is None:
metadata_list = [{}] * len(texts)
start_id = len(self.documents)
self.documents.extend(texts)
self.embeddings.extend(embeddings)
self.metadata.extend(metadata_list)
return list(range(start_id, len(self.documents)))
def search(self, query: str, top_k: int = 5, metadata_filter: Dict = None) -> List[Dict]:
"""Search for similar documents"""
query_embedding = self.embedding_gen.create_embedding(query)
results = []
for i, doc_embedding in enumerate(self.embeddings):
# Apply metadata filter if provided
if metadata_filter:
if not all(self.metadata[i].get(k) == v for k, v in metadata_filter.items()):
continue
# Calculate similarity
similarity = self._cosine_similarity(query_embedding, doc_embedding)
results.append({
"id": i,
"text": self.documents[i],
"metadata": self.metadata[i],
"similarity": similarity
})
# Sort by similarity and return top_k
results.sort(key=lambda x: x["similarity"], reverse=True)
return results[:top_k]
def get_document(self, doc_id: int) -> Dict:
"""Retrieve a document by ID"""
if 0 <= doc_id < len(self.documents):
return {
"id": doc_id,
"text": self.documents[doc_id],
"metadata": self.metadata[doc_id],
"embedding": self.embeddings[doc_id]
}
return None
def update_document(self, doc_id: int, new_text: str = None, new_metadata: Dict = None):
"""Update a document"""
if 0 <= doc_id < len(self.documents):
if new_text:
self.documents[doc_id] = new_text
self.embeddings[doc_id] = self.embedding_gen.create_embedding(new_text)
if new_metadata:
self.metadata[doc_id].update(new_metadata)
return True
return False
def delete_document(self, doc_id: int):
"""Delete a document"""
if 0 <= doc_id < len(self.documents):
del self.documents[doc_id]
del self.embeddings[doc_id]
del self.metadata[doc_id]
return True
return False
def get_stats(self) -> Dict:
"""Get database statistics"""
return {
"total_documents": len(self.documents),
"embedding_dimension": len(self.embeddings[0]) if self.embeddings else 0,
"unique_metadata_keys": list(set().union(*(meta.keys() for meta in self.metadata)))
}
def _cosine_similarity(self, vec1: List[float], vec2: List[float]) -> float:
"""Helper method for cosine similarity"""
vec1 = np.array(vec1)
vec2 = np.array(vec2)
dot_product = np.dot(vec1, vec2)
magnitude1 = np.linalg.norm(vec1)
magnitude2 = np.linalg.norm(vec2)
if magnitude1 == 0 or magnitude2 == 0:
return 0.0
return dot_product / (magnitude1 * magnitude2)
def export_to_json(self, filename: str):
"""Export database to JSON file"""
import json
data = {
"documents": self.documents,
"embeddings": self.embeddings,
"metadata": self.metadata
}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
def import_from_json(self, filename: str):
"""Import database from JSON file"""
import json
with open(filename, 'r') as f:
data = json.load(f)
self.documents = data["documents"]
self.embeddings = data["embeddings"]
self.metadata = data["metadata"]
# Usage
embedding_gen = EmbeddingGenerator("your_api_key")
vector_db = VectorDatabase(embedding_gen)
# Add documents with metadata
docs_and_metadata = [
("Python is a versatile programming language", {"category": "programming", "difficulty": "beginner"}),
("Machine learning algorithms can solve complex problems", {"category": "AI", "difficulty": "advanced"}),
("Cooking pasta requires boiling water first", {"category": "cooking", "difficulty": "beginner"}),
("Deep neural networks have multiple hidden layers", {"category": "AI", "difficulty": "advanced"}),
("JavaScript is essential for web development", {"category": "programming", "difficulty": "intermediate"})
]
for text, metadata in docs_and_metadata:
vector_db.add_document(text, metadata)
# Search with different queries
search_results = vector_db.search("programming languages", top_k=3)
print("Search results for 'programming languages':")
for result in search_results:
print(f" {result['similarity']:.3f}: {result['text']} (Category: {result['metadata']['category']})")
# Search with metadata filter
ai_results = vector_db.search("algorithms", metadata_filter={"category": "AI"})
print("\nAI-specific results for 'algorithms':")
for result in ai_results:
print(f" {result['similarity']:.3f}: {result['text']}")
# Database statistics
stats = vector_db.get_stats()
print(f"\nDatabase stats: {stats}")
```
### Content Classification System
```python theme={"system"}
class ContentClassifier:
def __init__(self, embedding_generator: EmbeddingGenerator):
self.embedding_gen = embedding_generator
self.categories = {}
self.is_trained = False
def add_category(self, category_name: str, example_texts: List[str]):
"""Add a category with example texts"""
# Generate embeddings for all examples
embeddings = self.embedding_gen.create_embeddings_batch(example_texts)
# Calculate centroid (average) embedding for the category
centroid = np.mean(embeddings, axis=0).tolist()
self.categories[category_name] = {
"examples": example_texts,
"embeddings": embeddings,
"centroid": centroid
}
def train(self):
"""Mark classifier as trained"""
if len(self.categories) < 2:
raise ValueError("Need at least 2 categories to train classifier")
self.is_trained = True
def classify(self, text: str, confidence_threshold: float = 0.5) -> Dict:
"""Classify a text into categories"""
if not self.is_trained:
raise ValueError("Classifier must be trained before classification")
text_embedding = self.embedding_gen.create_embedding(text)
# Calculate similarity to each category centroid
similarities = {}
for category, data in self.categories.items():
similarity = self._cosine_similarity(text_embedding, data["centroid"])
similarities[category] = similarity
# Find best match
best_category = max(similarities, key=similarities.get)
best_score = similarities[best_category]
# Check confidence threshold
prediction = {
"text": text,
"predicted_category": best_category if best_score >= confidence_threshold else "unknown",
"confidence": best_score,
"all_scores": similarities
}
return prediction
def classify_batch(self, texts: List[str], confidence_threshold: float = 0.5) -> List[Dict]:
"""Classify multiple texts"""
return [self.classify(text, confidence_threshold) for text in texts]
def evaluate_accuracy(self, test_texts: List[str], true_labels: List[str]) -> Dict:
"""Evaluate classifier accuracy on test data"""
predictions = [self.classify(text)["predicted_category"] for text in test_texts]
correct = sum(1 for pred, true in zip(predictions, true_labels) if pred == true)
accuracy = correct / len(true_labels) if true_labels else 0
# Create confusion matrix
unique_labels = list(set(true_labels + predictions))
confusion_matrix = {label: {label2: 0 for label2 in unique_labels} for label in unique_labels}
for true_label, pred_label in zip(true_labels, predictions):
confusion_matrix[true_label][pred_label] += 1
return {
"accuracy": accuracy,
"correct_predictions": correct,
"total_predictions": len(true_labels),
"confusion_matrix": confusion_matrix
}
def get_category_info(self) -> Dict:
"""Get information about all categories"""
info = {}
for category, data in self.categories.items():
info[category] = {
"example_count": len(data["examples"]),
"sample_examples": data["examples"][:3] # Show first 3 examples
}
return info
def _cosine_similarity(self, vec1: List[float], vec2: List[float]) -> float:
"""Helper method for cosine similarity"""
vec1 = np.array(vec1)
vec2 = np.array(vec2)
dot_product = np.dot(vec1, vec2)
magnitude1 = np.linalg.norm(vec1)
magnitude2 = np.linalg.norm(vec2)
if magnitude1 == 0 or magnitude2 == 0:
return 0.0
return dot_product / (magnitude1 * magnitude2)
# Usage
embedding_gen = EmbeddingGenerator("your_api_key")
classifier = ContentClassifier(embedding_gen)
# Define categories with examples
technology_examples = [
"New smartphone features artificial intelligence capabilities",
"Software update improves system performance significantly",
"Cloud computing revolutionizes data storage solutions",
"Machine learning algorithms optimize user experience"
]
sports_examples = [
"Football team wins championship after intense season",
"Basketball player breaks scoring record tonight",
"Olympic swimming competition features world records",
"Tennis tournament showcases incredible athletic skill"
]
food_examples = [
"Delicious pasta recipe with fresh ingredients",
"Restaurant review highlights amazing flavor combinations",
"Cooking techniques for perfect grilled vegetables",
"Traditional bread baking methods explained"
]
# Add categories
classifier.add_category("technology", technology_examples)
classifier.add_category("sports", sports_examples)
classifier.add_category("food", food_examples)
# Train classifier
classifier.train()
# Test classification
test_texts = [
"Latest AI breakthrough improves natural language processing",
"Soccer world cup final draws millions of viewers",
"Homemade pizza dough recipe for crispy crust",
"Virtual reality headset offers immersive gaming experience",
"Marathon runner completes race in record time"
]
for text in test_texts:
result = classifier.classify(text)
print(f"Text: {text}")
print(f"Category: {result['predicted_category']} (Confidence: {result['confidence']:.3f})")
print()
# Evaluate on test data
test_labels = ["technology", "sports", "food", "technology", "sports"]
evaluation = classifier.evaluate_accuracy(test_texts, test_labels)
print(f"Accuracy: {evaluation['accuracy']:.2%}")
```
## Specialized Embedding Applications
### Semantic Search Engine
```python theme={"system"}
def create_semantic_search_engine(api_key: str, documents: List[Dict]):
"""Create a semantic search engine for documents"""
embedding_gen = EmbeddingGenerator(api_key)
vector_db = VectorDatabase(embedding_gen)
# Add documents to the database
for doc in documents:
vector_db.add_document(
text=doc.get("content", ""),
metadata={
"title": doc.get("title", ""),
"author": doc.get("author", ""),
"date": doc.get("date", ""),
"url": doc.get("url", ""),
"tags": doc.get("tags", [])
}
)
class SemanticSearchEngine:
def __init__(self, vector_database):
self.db = vector_database
def search(self, query: str, filters: Dict = None, top_k: int = 10):
"""Search documents semantically"""
results = self.db.search(query, top_k=top_k, metadata_filter=filters)
# Format results for display
formatted_results = []
for result in results:
formatted_results.append({
"title": result["metadata"].get("title", "Untitled"),
"content_preview": result["text"][:200] + "...",
"author": result["metadata"].get("author", "Unknown"),
"relevance_score": result["similarity"],
"url": result["metadata"].get("url", ""),
"tags": result["metadata"].get("tags", [])
})
return formatted_results
def get_recommendations(self, document_id: int, top_k: int = 5):
"""Get documents similar to a specific document"""
doc = self.db.get_document(document_id)
if not doc:
return []
return self.search(doc["text"], top_k=top_k + 1)[1:] # Exclude the document itself
return SemanticSearchEngine(vector_db)
# Example usage
documents = [
{
"title": "Introduction to Machine Learning",
"content": "Machine learning is a subset of artificial intelligence that enables computers to learn and improve from experience without being explicitly programmed.",
"author": "Dr. Smith",
"date": "2024-01-15",
"tags": ["AI", "ML", "programming"]
},
{
"title": "Deep Learning Fundamentals",
"content": "Deep learning uses neural networks with multiple layers to model and understand complex patterns in data, revolutionizing fields like computer vision and natural language processing.",
"author": "Prof. Johnson",
"date": "2024-02-20",
"tags": ["deep learning", "neural networks", "AI"]
},
{
"title": "Cooking Perfect Pasta",
"content": "The secret to perfect pasta lies in using plenty of salted water, timing the cooking precisely, and choosing the right sauce pairing for each pasta shape.",
"author": "Chef Maria",
"date": "2024-03-10",
"tags": ["cooking", "pasta", "recipes"]
}
]
search_engine = create_semantic_search_engine("your_api_key", documents)
# Search examples
results = search_engine.search("artificial intelligence and neural networks")
for result in results:
print(f"Title: {result['title']}")
print(f"Relevance: {result['relevance_score']:.3f}")
print(f"Preview: {result['content_preview']}")
print()
```
### Content Recommendation System
```python theme={"system"}
def create_recommendation_system(api_key: str):
"""Create a content recommendation system"""
embedding_gen = EmbeddingGenerator(api_key)
class RecommendationSystem:
def __init__(self, embedding_generator):
self.embedding_gen = embedding_generator
self.user_profiles = {}
self.content_embeddings = {}
self.content_metadata = {}
def add_content(self, content_id: str, text: str, metadata: Dict = None):
"""Add content to the system"""
embedding = self.embedding_gen.create_embedding(text)
self.content_embeddings[content_id] = embedding
self.content_metadata[content_id] = metadata or {}
def update_user_profile(self, user_id: str, interactions: List[Dict]):
"""Update user profile based on interactions"""
# interactions: [{"content_id": str, "rating": float, "interaction_type": str}]
if user_id not in self.user_profiles:
self.user_profiles[user_id] = {
"preferences": None,
"interaction_history": []
}
# Add new interactions
self.user_profiles[user_id]["interaction_history"].extend(interactions)
# Calculate user preference vector
weighted_embeddings = []
weights = []
for interaction in self.user_profiles[user_id]["interaction_history"]:
content_id = interaction["content_id"]
rating = interaction["rating"]
if content_id in self.content_embeddings:
# Weight by rating and interaction type
weight = rating
if interaction["interaction_type"] == "like":
weight *= 1.5
elif interaction["interaction_type"] == "share":
weight *= 2.0
elif interaction["interaction_type"] == "dislike":
weight *= -0.5
weighted_embeddings.append(np.array(self.content_embeddings[content_id]) * weight)
weights.append(abs(weight))
if weighted_embeddings:
# Calculate weighted average
total_weight = sum(weights)
preference_vector = sum(weighted_embeddings) / total_weight
self.user_profiles[user_id]["preferences"] = preference_vector.tolist()
def get_recommendations(self, user_id: str, exclude_seen: bool = True, top_k: int = 10) -> List[Dict]:
"""Get content recommendations for a user"""
if user_id not in self.user_profiles or self.user_profiles[user_id]["preferences"] is None:
# Return popular content for new users
return self._get_popular_content(top_k)
user_preferences = self.user_profiles[user_id]["preferences"]
seen_content = {interaction["content_id"] for interaction in self.user_profiles[user_id]["interaction_history"]}
recommendations = []
for content_id, content_embedding in self.content_embeddings.items():
if exclude_seen and content_id in seen_content:
continue
# Calculate similarity to user preferences
similarity = self._cosine_similarity(user_preferences, content_embedding)
recommendations.append({
"content_id": content_id,
"similarity_score": similarity,
"metadata": self.content_metadata[content_id]
})
# Sort by similarity and return top_k
recommendations.sort(key=lambda x: x["similarity_score"], reverse=True)
return recommendations[:top_k]
def get_similar_content(self, content_id: str, top_k: int = 5) -> List[Dict]:
"""Get content similar to a specific item"""
if content_id not in self.content_embeddings:
return []
target_embedding = self.content_embeddings[content_id]
similarities = []
for other_id, other_embedding in self.content_embeddings.items():
if other_id != content_id:
similarity = self._cosine_similarity(target_embedding, other_embedding)
similarities.append({
"content_id": other_id,
"similarity_score": similarity,
"metadata": self.content_metadata[other_id]
})
similarities.sort(key=lambda x: x["similarity_score"], reverse=True)
return similarities[:top_k]
def _get_popular_content(self, top_k: int) -> List[Dict]:
"""Get popular content for new users"""
# Placeholder: return random content
import random
content_ids = list(self.content_embeddings.keys())
selected = random.sample(content_ids, min(top_k, len(content_ids)))
return [{
"content_id": content_id,
"similarity_score": 0.0,
"metadata": self.content_metadata[content_id]
} for content_id in selected]
def _cosine_similarity(self, vec1: List[float], vec2: List[float]) -> float:
"""Calculate cosine similarity"""
vec1 = np.array(vec1)
vec2 = np.array(vec2)
dot_product = np.dot(vec1, vec2)
magnitude1 = np.linalg.norm(vec1)
magnitude2 = np.linalg.norm(vec2)
if magnitude1 == 0 or magnitude2 == 0:
return 0.0
return dot_product / (magnitude1 * magnitude2)
return RecommendationSystem(embedding_gen)
# Usage
rec_system = create_recommendation_system("your_api_key")
# Add content
contents = [
("article_1", "Latest advances in artificial intelligence and machine learning", {"category": "technology", "author": "Tech Writer"}),
("article_2", "Deep learning applications in computer vision", {"category": "AI", "author": "AI Researcher"}),
("article_3", "Delicious pasta recipes for home cooking", {"category": "food", "author": "Chef"}),
("article_4", "Understanding neural network architectures", {"category": "AI", "author": "Data Scientist"})
]
for content_id, text, metadata in contents:
rec_system.add_content(content_id, text, metadata)
# Simulate user interactions
user_interactions = [
{"content_id": "article_1", "rating": 4.5, "interaction_type": "like"},
{"content_id": "article_2", "rating": 5.0, "interaction_type": "share"},
{"content_id": "article_3", "rating": 2.0, "interaction_type": "dislike"}
]
rec_system.update_user_profile("user_123", user_interactions)
# Get recommendations
recommendations = rec_system.get_recommendations("user_123", top_k=3)
print("Recommendations for user_123:")
for rec in recommendations:
print(f" Content: {rec['content_id']}")
print(f" Score: {rec['similarity_score']:.3f}")
print(f" Category: {rec['metadata']['category']}")
print()
# Get similar content
similar = rec_system.get_similar_content("article_1", top_k=2)
print("Similar to article_1:")
for item in similar:
print(f" {item['content_id']}: {item['similarity_score']:.3f}")
```
## Best Practices
### 1. Text Preprocessing
* **Clean text**: Remove excessive whitespace, special characters
* **Normalize content**: Consistent formatting and encoding
* **Chunk long documents**: Split into manageable segments
* **Handle multilingual content**: Specify language when needed
### 2. Embedding Management
* **Batch processing**: Use batch API for multiple texts
* **Caching**: Store embeddings to avoid recomputation
* **Version control**: Track embedding model versions
* **Dimension consistency**: Ensure same model across pipeline
### 3. Similarity Calculations
* **Choose right metric**: Cosine similarity for semantic similarity
* **Normalize vectors**: Consider L2 normalization
* **Threshold tuning**: Adjust similarity thresholds per use case
* **Performance optimization**: Use approximate nearest neighbor for large datasets
### 4. Production Deployment
* **Error handling**: Robust API error management
* **Rate limiting**: Respect API rate limits
* **Monitoring**: Track embedding quality and performance
* **Scaling**: Plan for increased embedding volume
## Common Use Cases
**Semantic Search**\
Search documents by meaning, not just keywords
**Content Recommendation**\
Suggest related articles, products, or media
**Document Classification**\
Automatically categorize content by topic
**Duplicate Detection**\
Find similar or duplicate content efficiently
**Knowledge Base Q\&A**\
Build intelligent question-answering systems
**Content Clustering**\
Group related content automatically
**Personalization**\
Create user preference profiles for recommendations
**Language Translation Support**\
Cross-language similarity and matching
# Create Images
Source: https://docs.anyapi.ai/use-cases/create-images
Generate stunning images from text descriptions using AI image models
# Create Images with AI
Generate high-quality images from text descriptions using state-of-the-art AI image generation models. Perfect for creative projects, marketing content, prototyping, and visual storytelling.
## Overview
AI image generation allows you to:
* **Create original artwork** from text descriptions
* **Generate marketing visuals** for campaigns and content
* **Prototype designs** quickly and efficiently
* **Visualize concepts** that are hard to describe
* **Produce variations** of existing ideas
Generate images from detailed text prompts
Create images in specific artistic styles
Generate images up to 2048x2048 pixels
Choose from DALL-E, Flux, Stable Diffusion, and more
## Quick Start
```python Python theme={"system"}
import requests
import base64
from PIL import Image
import io
def generate_image(prompt, model="google/gemini-2.5-flash-image", size="1024x1024"):
"""Generate an image from a text prompt"""
response = requests.post(
"https://api.anyapi.ai/v1/images/generations",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"model": model,
"prompt": prompt,
"size": size,
"quality": "hd",
"n": 1
}
)
result = response.json()
image_url = result["data"][0]["url"]
# Download and save the image
image_response = requests.get(image_url)
image = Image.open(io.BytesIO(image_response.content))
return image, image_url
# Usage examples
image, url = generate_image(
"A serene mountain landscape at sunset with a crystal clear lake reflecting the mountains and sky"
)
# Save the image
image.save("mountain_landscape.png")
print(f"Image generated and saved! URL: {url}")
# Generate with different model
image2, url2 = generate_image(
"A futuristic cityscape with flying cars and neon lights",
model="flux-pro",
size="1792x1024"
)
image2.save("futuristic_city.png")
```
```javascript JavaScript theme={"system"}
async function generateImage(prompt, model = 'google/gemini-2.5-flash-image', size = '1024x1024') {
const response = await fetch('https://api.anyapi.ai/v1/images/generations', {
method: 'POST',
headers: {
'Authorization': 'Bearer your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: model,
prompt: prompt,
size: size,
quality: 'hd',
n: 1
})
});
const result = await response.json();
return result.data[0].url;
}
// Usage
generateImage('A magical forest with glowing mushrooms and fairy lights')
.then(imageUrl => {
console.log('Image generated:', imageUrl);
// Display in HTML
const img = document.createElement('img');
img.src = imageUrl;
img.style.maxWidth = '100%';
document.body.appendChild(img);
});
// Generate multiple variations
async function generateVariations(basePrompt, variations) {
const images = [];
for (const variation of variations) {
const fullPrompt = `${basePrompt}, ${variation}`;
const imageUrl = await generateImage(fullPrompt);
images.push({ prompt: fullPrompt, url: imageUrl });
}
return images;
}
// Example usage
const variations = [
'in watercolor style',
'in digital art style',
'in photorealistic style',
'in minimalist style'
];
generateVariations('A cute robot character', variations)
.then(images => {
images.forEach(img => {
console.log(`${img.prompt}: ${img.url}`);
});
});
```
## Advanced Image Generation
### Creative Prompt Engineering
```python theme={"system"}
class ImagePromptGenerator:
def __init__(self, api_key):
self.api_key = api_key
def enhance_prompt(self, basic_prompt):
"""Enhance a basic prompt with detailed descriptions"""
enhancement_request = f"""
Enhance this basic image prompt with rich, detailed descriptions:
"{basic_prompt}"
Add details about:
- Visual style and artistic technique
- Lighting and atmosphere
- Colors and composition
- Camera angle and perspective
- Mood and emotion
- Technical photography details
Create a detailed prompt that will generate a stunning image.
Keep it under 400 characters for optimal results.
"""
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "You are an expert at writing image generation prompts. Create detailed, visual descriptions that will produce amazing images."
},
{"role": "user", "content": enhancement_request}
],
"temperature": 0.8
}
)
return response.json()["choices"][0]["message"]["content"]
def generate_with_enhanced_prompt(self, basic_prompt, model="google/gemini-2.5-flash-image"):
"""Generate image with AI-enhanced prompt"""
enhanced_prompt = self.enhance_prompt(basic_prompt)
print(f"Enhanced prompt: {enhanced_prompt}")
response = requests.post(
"https://api.anyapi.ai/v1/images/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": model,
"prompt": enhanced_prompt,
"size": "1024x1024",
"quality": "hd"
}
)
result = response.json()
return result["data"][0]["url"], enhanced_prompt
# Usage
prompt_generator = ImagePromptGenerator("your_api_key")
# Basic prompt
basic_prompt = "a cat wearing a hat"
# Generate with enhancement
image_url, enhanced = prompt_generator.generate_with_enhanced_prompt(basic_prompt)
print(f"Generated image: {image_url}")
print(f"Enhanced prompt: {enhanced}")
```
### Style Transfer and Artistic Techniques
```python theme={"system"}
class ArtisticImageGenerator:
def __init__(self, api_key):
self.api_key = api_key
self.art_styles = {
"impressionist": "in the style of Claude Monet, impressionist brushstrokes, soft colors, outdoor lighting",
"surreal": "surrealist art style, dreamlike, impossible geometry, Salvador Dali inspired",
"cyberpunk": "cyberpunk aesthetic, neon colors, dystopian future, high-tech low-life",
"watercolor": "watercolor painting, soft edges, transparent colors, artistic brushstrokes",
"oil_painting": "classical oil painting, rich textures, realistic details, museum quality",
"anime": "anime art style, vibrant colors, expressive characters, Japanese animation",
"pixel_art": "8-bit pixel art style, retro gaming aesthetic, limited color palette",
"photorealistic": "photorealistic, high detail, professional photography, crisp focus",
"minimalist": "minimalist design, clean lines, simple composition, negative space",
"vintage": "vintage photography, aged colors, film grain, nostalgic mood"
}
self.camera_angles = {
"portrait": "portrait orientation, close-up shot",
"landscape": "landscape orientation, wide shot",
"aerial": "aerial view, bird's eye perspective, drone photography",
"macro": "macro photography, extreme close-up, detailed textures",
"wide_angle": "wide-angle lens, expansive view, dramatic perspective",
"telephoto": "telephoto lens, compressed perspective, shallow depth of field"
}
def generate_artistic_image(self, subject, style_key, camera_key=None, additional_details=""):
"""Generate image with specific artistic style"""
style = self.art_styles.get(style_key, "")
camera = self.camera_angles.get(camera_key, "") if camera_key else ""
prompt = f"{subject}, {style}"
if camera:
prompt += f", {camera}"
if additional_details:
prompt += f", {additional_details}"
response = requests.post(
"https://api.anyapi.ai/v1/images/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "google/gemini-2.5-flash-image",
"prompt": prompt,
"size": "1024x1024",
"quality": "hd"
}
)
result = response.json()
return result["data"][0]["url"], prompt
def create_style_variations(self, subject, styles_list):
"""Create multiple style variations of the same subject"""
variations = []
for style in styles_list:
try:
image_url, full_prompt = self.generate_artistic_image(subject, style)
variations.append({
"style": style,
"url": image_url,
"prompt": full_prompt
})
print(f"Generated {style} version: {image_url}")
except Exception as e:
print(f"Failed to generate {style} version: {e}")
return variations
# Usage
art_generator = ArtisticImageGenerator("your_api_key")
# Generate single artistic image
subject = "a majestic eagle soaring over snow-capped mountains"
image_url, prompt = art_generator.generate_artistic_image(
subject,
"impressionist",
"landscape",
"golden hour lighting, dramatic clouds"
)
print(f"Impressionist eagle: {image_url}")
# Generate style variations
styles = ["photorealistic", "watercolor", "cyberpunk", "anime", "oil_painting"]
variations = art_generator.create_style_variations(
"a wise old wizard reading a glowing book",
styles
)
for var in variations:
print(f"{var['style']}: {var['url']}")
```
### Product and Marketing Image Generation
```python theme={"system"}
class MarketingImageGenerator:
def __init__(self, api_key):
self.api_key = api_key
def generate_product_mockup(self, product_description, setting="modern studio"):
"""Generate product mockup images"""
mockup_prompts = {
"modern_studio": "professional product photography, clean white background, studio lighting, high-end commercial style",
"lifestyle": "lifestyle photography, natural setting, people using the product, authentic atmosphere",
"minimalist": "minimalist product shot, simple background, clean aesthetic, negative space",
"luxury": "luxury product photography, elegant setting, premium materials, sophisticated lighting"
}
style = mockup_prompts.get(setting, mockup_prompts["modern_studio"])
prompt = f"{product_description}, {style}, product photography, high resolution, commercial quality"
response = requests.post(
"https://api.anyapi.ai/v1/images/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "google/gemini-2.5-flash-image",
"prompt": prompt,
"size": "1024x1024",
"quality": "hd"
}
)
result = response.json()
return result["data"][0]["url"]
def generate_social_media_content(self, theme, platform="instagram"):
"""Generate social media optimized images"""
platform_specs = {
"instagram": {
"size": "1024x1024",
"style": "Instagram-ready, vibrant colors, eye-catching composition, social media optimized"
},
"facebook": {
"size": "1200x630",
"style": "Facebook cover style, wide composition, engaging visual"
},
"twitter": {
"size": "1200x675",
"style": "Twitter header style, clean design, professional look"
},
"linkedin": {
"size": "1128x640",
"style": "LinkedIn professional style, business appropriate, sophisticated"
}
}
spec = platform_specs.get(platform, platform_specs["instagram"])
prompt = f"{theme}, {spec['style']}, high quality, social media ready"
response = requests.post(
"https://api.anyapi.ai/v1/images/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "google/gemini-2.5-flash-image",
"prompt": prompt,
"size": spec["size"],
"quality": "hd"
}
)
result = response.json()
return result["data"][0]["url"]
def generate_marketing_campaign(self, campaign_theme, variations=3):
"""Generate a complete marketing campaign with multiple image variations"""
campaign_angles = [
"hero shot, dramatic lighting, premium feel",
"lifestyle approach, people enjoying the product/service, authentic",
"close-up detail shot, texture and quality focus",
"wide environmental shot, context and scale",
"creative artistic interpretation, abstract elements"
]
campaign_images = []
for i in range(variations):
angle = campaign_angles[i % len(campaign_angles)]
prompt = f"{campaign_theme}, {angle}, marketing photography, professional quality, brand-focused"
response = requests.post(
"https://api.anyapi.ai/v1/images/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "google/gemini-2.5-flash-image",
"prompt": prompt,
"size": "1024x1024",
"quality": "hd"
}
)
result = response.json()
campaign_images.append({
"angle": angle,
"url": result["data"][0]["url"],
"prompt": prompt
})
return campaign_images
# Usage
marketing_gen = MarketingImageGenerator("your_api_key")
# Product mockup
product_url = marketing_gen.generate_product_mockup(
"sleek wireless headphones with noise cancellation",
"luxury"
)
print(f"Product mockup: {product_url}")
# Social media content
social_url = marketing_gen.generate_social_media_content(
"celebrating innovation in technology",
"instagram"
)
print(f"Social media image: {social_url}")
# Marketing campaign
campaign = marketing_gen.generate_marketing_campaign(
"eco-friendly sustainable fashion brand launch",
3
)
for img in campaign:
print(f"{img['angle']}: {img['url']}")
```
### Creative Workflow Automation
```python theme={"system"}
class CreativeWorkflow:
def __init__(self, api_key):
self.api_key = api_key
self.project_history = []
def brainstorm_concepts(self, project_brief):
"""Generate multiple creative concepts for a project"""
brainstorm_prompt = f"""
Given this creative brief: "{project_brief}"
Generate 5 diverse creative concepts with:
1. A catchy concept name
2. Visual description for image generation
3. Target mood/emotion
4. Color palette suggestion
5. Key visual elements
Make each concept unique and creative.
"""
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a creative director generating innovative visual concepts."
},
{"role": "user", "content": brainstorm_prompt}
],
"temperature": 0.8
}
)
return response.json()["choices"][0]["message"]["content"]
def concept_to_image(self, concept_description):
"""Convert a concept description to an actual image"""
# Extract visual elements for prompt
image_prompt = f"{concept_description}, high quality, creative design, professional artwork"
response = requests.post(
"https://api.anyapi.ai/v1/images/generations",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "google/gemini-2.5-flash-image",
"prompt": image_prompt,
"size": "1024x1024",
"quality": "hd"
}
)
result = response.json()
return result["data"][0]["url"]
def create_mood_board(self, theme, num_images=6):
"""Create a mood board with multiple related images"""
mood_variations = [
f"{theme}, inspirational mood",
f"{theme}, dramatic lighting",
f"{theme}, soft and dreamy",
f"{theme}, bold and vibrant",
f"{theme}, minimalist approach",
f"{theme}, textured and detailed"
]
mood_board = []
for i, variation in enumerate(mood_variations[:num_images]):
try:
image_url = self.concept_to_image(variation)
mood_board.append({
"concept": variation,
"url": image_url
})
print(f"Generated mood board image {i+1}: {image_url}")
except Exception as e:
print(f"Failed to generate mood board image {i+1}: {e}")
return mood_board
def iterate_design(self, base_concept, feedback):
"""Iterate on a design based on feedback"""
iteration_prompt = f"""
Original concept: {base_concept}
Feedback received: {feedback}
Create an improved version that addresses the feedback while maintaining the core concept.
Provide a detailed visual description for image generation.
"""
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a designer iterating on concepts based on feedback."
},
{"role": "user", "content": iteration_prompt}
],
"temperature": 0.6
}
)
improved_concept = response.json()["choices"][0]["message"]["content"]
improved_image = self.concept_to_image(improved_concept)
return improved_image, improved_concept
# Usage
workflow = CreativeWorkflow("your_api_key")
# Project brainstorming
brief = "Design visuals for a new eco-friendly coffee shop that appeals to young professionals"
concepts = workflow.brainstorm_concepts(brief)
print("Generated concepts:")
print(concepts)
# Create mood board
mood_board = workflow.create_mood_board("sustainable coffee culture", 4)
print("\nMood board created:")
for item in mood_board:
print(f"{item['concept']}: {item['url']}")
# Design iteration
base_concept = "modern coffee shop interior with living walls and natural lighting"
feedback = "Make it feel more cozy and less corporate, add warm colors"
improved_url, improved_concept = workflow.iterate_design(base_concept, feedback)
print(f"\nImproved design: {improved_url}")
print(f"Improved concept: {improved_concept}")
```
## Model Selection Guide
### Model Comparison
| Model | Strengths | Best For | Style |
| ----------------------- | -------------------------- | ------------------------- | ------------------- |
| **DALL-E 3** | Prompt adherence, quality | General purpose, concepts | Balanced, realistic |
| **Flux Pro** | Photorealism, details | Commercial, professional | Photographic |
| **Stable Diffusion XL** | Customization, speed | Experimentation, artistic | Artistic, flexible |
| **Midjourney** | Artistic style, creativity | Creative projects, art | Stylized, artistic |
### Choosing the Right Model
```python theme={"system"}
def recommend_model(use_case, style_preference, quality_needs):
"""Recommend the best model for specific needs"""
recommendations = {
("marketing", "photorealistic", "high"): "flux-pro",
("marketing", "artistic", "high"): "google/gemini-2.5-flash-image",
("creative", "artistic", "high"): "midjourney-v6",
("prototype", "any", "medium"): "stable-diffusion-xl",
("social_media", "vibrant", "medium"): "google/gemini-2.5-flash-image",
("product", "photorealistic", "high"): "flux-pro",
("concept_art", "artistic", "high"): "midjourney-v6",
("quick_test", "any", "low"): "stable-diffusion-turbo"
}
key = (use_case, style_preference, quality_needs)
return recommendations.get(key, "google/gemini-2.5-flash-image") # Default to DALL-E 3
# Usage
model = recommend_model("marketing", "photorealistic", "high")
print(f"Recommended model: {model}")
```
## Best Practices
### Prompt Engineering Tips
1. **Be specific and descriptive**
* β "a dog"
* β
"a golden retriever puppy playing in a sunny meadow with wildflowers"
2. **Include style and quality modifiers**
* "professional photography"
* "high resolution"
* "studio lighting"
* "award-winning"
3. **Specify composition and framing**
* "wide shot", "close-up", "aerial view"
* "rule of thirds composition"
* "shallow depth of field"
4. **Add mood and atmosphere**
* "warm and inviting"
* "dramatic and moody"
* "bright and cheerful"
### Quality Optimization
```python theme={"system"}
quality_modifiers = {
"photography": [
"professional photography",
"high resolution",
"sharp focus",
"perfect lighting",
"DSLR camera",
"award-winning"
],
"artwork": [
"masterpiece",
"highly detailed",
"intricate",
"beautiful",
"stunning",
"gallery quality"
],
"commercial": [
"commercial photography",
"product shot",
"clean background",
"professional lighting",
"marketing ready"
]
}
def enhance_prompt_quality(base_prompt, category="photography"):
"""Add quality modifiers to improve image generation"""
modifiers = quality_modifiers.get(category, quality_modifiers["photography"])
selected_modifiers = ", ".join(modifiers[:3]) # Use top 3 modifiers
return f"{base_prompt}, {selected_modifiers}"
# Usage
enhanced = enhance_prompt_quality(
"a modern kitchen with marble countertops",
"commercial"
)
print(enhanced)
# Output: "a modern kitchen with marble countertops, commercial photography, product shot, clean background"
```
## Common Use Cases
Social media content, campaign visuals, product mockups
Concept art, illustrations, artistic exploration
Prototyping, visualization, design iteration
Blog images, thumbnails, visual storytelling
Product photography, lifestyle shots, catalog images
Property visualization, staging concepts, renovation ideas
Clothing designs, lookbooks, trend visualization
Building concepts, interior design, space planning
## Troubleshooting Common Issues
### Content Policy Violations
* **Issue**: Image generation rejected
* **Solution**: Revise prompts to avoid restricted content
* **Prevention**: Review content guidelines
### Poor Quality Results
* **Issue**: Blurry or low-quality images
* **Solution**: Add quality modifiers, use HD setting
* **Example**: Add "high resolution, professional photography"
### Prompt Interpretation Errors
* **Issue**: AI misunderstands the prompt
* **Solution**: Be more specific, add context
* **Example**: "red car" β "bright red sports car, side view, professional photography"
## Getting Started
Generate your first AI image
Explore image generation models
Learn about vision capabilities
Use our official libraries
# Semantic Search
Source: https://docs.anyapi.ai/use-cases/semantic-search
Build intelligent search systems that understand meaning and context using AI embeddings
# Semantic Search Implementation
Build powerful search systems that understand meaning rather than just matching keywords. Semantic search uses AI embeddings to find relevant content based on intent and context.
## Overview
Semantic search enables:
* **Meaning-based search** - Find content by intent, not just keywords
* **Contextual understanding** - Understand synonyms and related concepts
* **Cross-language search** - Search across different languages
* **Fuzzy matching** - Find relevant content even with imprecise queries
* **Intelligent ranking** - Rank results by semantic relevance
Convert text to numerical vectors that capture meaning
Find similar content using vector mathematics
Combine semantic and keyword search for best results
Index new content automatically for immediate search
## Quick Start Implementation
```python Python theme={"system"}
import requests
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import json
class SemanticSearch:
def __init__(self, api_key):
self.api_key = api_key
self.documents = {}
self.embeddings = {}
def add_document(self, doc_id, content, metadata=None):
"""Add a document to the search index"""
# Generate embedding for the document
embedding = self.get_embedding(content)
# Store document and embedding
self.documents[doc_id] = {
"content": content,
"metadata": metadata or {},
"embedding": embedding
}
print(f"Added document {doc_id} to search index")
def get_embedding(self, text):
"""Generate embedding for text using AnyAPI"""
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": text
}
)
return response.json()["data"][0]["embedding"]
def search(self, query, top_k=5, min_similarity=0.3):
"""Search for similar documents"""
# Get query embedding
query_embedding = self.get_embedding(query)
# Calculate similarities
similarities = []
for doc_id, doc_data in self.documents.items():
similarity = cosine_similarity(
[query_embedding],
[doc_data["embedding"]]
)[0][0]
if similarity >= min_similarity:
similarities.append({
"doc_id": doc_id,
"similarity": similarity,
"content": doc_data["content"],
"metadata": doc_data["metadata"]
})
# Sort by similarity
similarities.sort(key=lambda x: x["similarity"], reverse=True)
return similarities[:top_k]
def batch_add_documents(self, documents):
"""Add multiple documents efficiently"""
# Extract texts for batch embedding
texts = [doc["content"] for doc in documents]
# Get embeddings in batch
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": texts
}
)
embeddings = response.json()["data"]
# Store documents with embeddings
for doc, embedding in zip(documents, embeddings):
self.documents[doc["id"]] = {
"content": doc["content"],
"metadata": doc.get("metadata", {}),
"embedding": embedding["embedding"]
}
print(f"Added {len(documents)} documents to search index")
# Usage example
search_engine = SemanticSearch("your_api_key")
# Add sample documents
documents = [
{
"id": "doc1",
"content": "Machine learning is a subset of artificial intelligence that enables computers to learn from data",
"metadata": {"category": "AI", "author": "John Doe"}
},
{
"id": "doc2",
"content": "Python is a popular programming language for data science and web development",
"metadata": {"category": "Programming", "author": "Jane Smith"}
},
{
"id": "doc3",
"content": "Deep learning uses neural networks to solve complex problems like image recognition",
"metadata": {"category": "AI", "author": "Bob Johnson"}
},
{
"id": "doc4",
"content": "JavaScript is the primary language for building interactive web applications",
"metadata": {"category": "Programming", "author": "Alice Wilson"}
}
]
# Add documents to search index
search_engine.batch_add_documents(documents)
# Search examples
results = search_engine.search("AI and computers learning", top_k=3)
print("\nSearch results for 'AI and computers learning':")
for result in results:
print(f"Score: {result['similarity']:.3f} - {result['content']}")
results = search_engine.search("coding languages for websites", top_k=3)
print("\nSearch results for 'coding languages for websites':")
for result in results:
print(f"Score: {result['similarity']:.3f} - {result['content']}")
```
```javascript JavaScript theme={"system"}
class SemanticSearch {
constructor(apiKey) {
this.apiKey = apiKey;
this.documents = new Map();
}
async addDocument(docId, content, metadata = {}) {
const embedding = await this.getEmbedding(content);
this.documents.set(docId, {
content,
metadata,
embedding
});
console.log(`Added document ${docId} to search index`);
}
async getEmbedding(text) {
const response = await fetch('https://api.anyapi.ai/v1/embeddings', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'text-embedding-3-large',
input: text
})
});
const data = await response.json();
return data.data[0].embedding;
}
async search(query, topK = 5, minSimilarity = 0.3) {
const queryEmbedding = await this.getEmbedding(query);
const similarities = [];
for (const [docId, docData] of this.documents.entries()) {
const similarity = this.cosineSimilarity(queryEmbedding, docData.embedding);
if (similarity >= minSimilarity) {
similarities.push({
docId,
similarity,
content: docData.content,
metadata: docData.metadata
});
}
}
return similarities
.sort((a, b) => b.similarity - a.similarity)
.slice(0, topK);
}
cosineSimilarity(vecA, vecB) {
const dotProduct = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0);
const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));
const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0));
return dotProduct / (magnitudeA * magnitudeB);
}
async batchAddDocuments(documents) {
const texts = documents.map(doc => doc.content);
const response = await fetch('https://api.anyapi.ai/v1/embeddings', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'text-embedding-3-large',
input: texts
})
});
const data = await response.json();
const embeddings = data.data;
documents.forEach((doc, index) => {
this.documents.set(doc.id, {
content: doc.content,
metadata: doc.metadata || {},
embedding: embeddings[index].embedding
});
});
console.log(`Added ${documents.length} documents to search index`);
}
}
// Usage
const searchEngine = new SemanticSearch('your_api_key');
async function setupSearchDemo() {
const documents = [
{
id: 'doc1',
content: 'Machine learning algorithms can automatically improve through experience',
metadata: { category: 'AI', author: 'Dr. Smith' }
},
{
id: 'doc2',
content: 'React is a JavaScript library for building user interfaces',
metadata: { category: 'Web Dev', author: 'Jane Doe' }
}
];
await searchEngine.batchAddDocuments(documents);
const results = await searchEngine.search('AI learning systems', 2);
console.log('Search results:', results);
}
setupSearchDemo();
```
## Advanced Search Implementations
### Vector Database Integration
```python Pinecone Integration theme={"system"}
import pinecone
import requests
class PineconeSemanticSearch:
def __init__(self, api_key, pinecone_key, index_name):
self.api_key = api_key
# Initialize Pinecone
pinecone.init(api_key=pinecone_key, environment="us-west1-gcp")
self.index = pinecone.Index(index_name)
def index_documents(self, documents, namespace="default"):
"""Index documents in Pinecone with metadata"""
# Get embeddings for all documents
texts = [doc["content"] for doc in documents]
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": texts
}
)
embeddings = response.json()["data"]
# Prepare vectors for Pinecone
vectors = []
for doc, embedding in zip(documents, embeddings):
vectors.append({
"id": doc["id"],
"values": embedding["embedding"],
"metadata": {
"content": doc["content"][:1000], # Truncate for metadata
"title": doc.get("title", ""),
"category": doc.get("category", ""),
"author": doc.get("author", ""),
"url": doc.get("url", "")
}
})
# Upsert to Pinecone
self.index.upsert(vectors=vectors, namespace=namespace)
print(f"Indexed {len(vectors)} documents in Pinecone")
def search(self, query, top_k=10, filters=None, namespace="default"):
"""Search with optional metadata filtering"""
# Get query embedding
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": query
}
)
query_embedding = response.json()["data"][0]["embedding"]
# Search in Pinecone
search_results = self.index.query(
vector=query_embedding,
top_k=top_k,
include_metadata=True,
filter=filters,
namespace=namespace
)
return search_results["matches"]
def hybrid_search(self, query, keyword_filter=None, top_k=10):
"""Combine semantic search with keyword filtering"""
# Semantic search
semantic_results = self.search(query, top_k=top_k * 2)
# Apply keyword filtering if specified
if keyword_filter:
filtered_results = []
for result in semantic_results:
content = result["metadata"].get("content", "").lower()
if keyword_filter.lower() in content:
filtered_results.append(result)
semantic_results = filtered_results
return semantic_results[:top_k]
# Usage
pinecone_search = PineconeSemanticSearch(
"your_api_key",
"your_pinecone_key",
"semantic-search-index"
)
# Index documents
documents = [
{
"id": "article_1",
"content": "The future of artificial intelligence in healthcare looks promising...",
"title": "AI in Healthcare",
"category": "Technology",
"author": "Dr. Sarah Johnson"
}
# Add more documents...
]
pinecone_search.index_documents(documents)
# Search with filters
results = pinecone_search.search(
"machine learning medical applications",
filters={"category": {"$eq": "Technology"}},
top_k=5
)
for result in results:
print(f"Score: {result['score']:.3f} - {result['metadata']['title']}")
```
```python Weaviate Integration theme={"system"}
import weaviate
import requests
class WeaviateSemanticSearch:
def __init__(self, api_key, weaviate_url):
self.api_key = api_key
self.client = weaviate.Client(weaviate_url)
def create_schema(self):
"""Create Weaviate schema for documents"""
schema = {
"classes": [{
"class": "Document",
"vectorizer": "none", # We provide our own vectors
"properties": [
{"name": "content", "dataType": ["text"]},
{"name": "title", "dataType": ["string"]},
{"name": "category", "dataType": ["string"]},
{"name": "author", "dataType": ["string"]},
{"name": "url", "dataType": ["string"]},
{"name": "timestamp", "dataType": ["date"]}
]
}]
}
self.client.schema.create(schema)
print("Created Weaviate schema")
def index_document(self, document):
"""Index a single document in Weaviate"""
# Get embedding
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": document["content"]
}
)
embedding = response.json()["data"][0]["embedding"]
# Store in Weaviate
self.client.data_object.create(
data_object={
"content": document["content"],
"title": document.get("title", ""),
"category": document.get("category", ""),
"author": document.get("author", ""),
"url": document.get("url", ""),
"timestamp": document.get("timestamp", "")
},
class_name="Document",
vector=embedding
)
def search(self, query, limit=10, where_filter=None):
"""Search documents in Weaviate"""
# Get query embedding
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": query
}
)
query_embedding = response.json()["data"][0]["embedding"]
# Build Weaviate query
query_builder = self.client.query.get("Document", [
"content", "title", "category", "author", "url", "timestamp"
]).with_near_vector({
"vector": query_embedding
}).with_limit(limit)
# Add filters if provided
if where_filter:
query_builder = query_builder.with_where(where_filter)
# Execute query
result = query_builder.do()
return result["data"]["Get"]["Document"]
def search_with_generative(self, query, limit=5):
"""Search and generate summary using Weaviate's generative module"""
query_embedding = self.get_embedding(query)
result = self.client.query.get("Document", [
"content", "title", "author"
]).with_near_vector({
"vector": query_embedding
}).with_generate(
single_prompt="Summarize this document: {content}"
).with_limit(limit).do()
return result["data"]["Get"]["Document"]
# Usage
weaviate_search = WeaviateSemanticSearch("your_api_key", "http://localhost:8080")
# Create schema and index documents
weaviate_search.create_schema()
documents = [
{
"content": "Quantum computing represents a fundamental shift in computational paradigms...",
"title": "Introduction to Quantum Computing",
"category": "Science",
"author": "Prof. Alice Chen"
}
]
for doc in documents:
weaviate_search.index_document(doc)
# Search with filters
results = weaviate_search.search(
"quantum algorithms",
where_filter={
"path": ["category"],
"operator": "Equal",
"valueString": "Science"
}
)
print("Search results:", results)
```
### E-commerce Search Implementation
```python theme={"system"}
class EcommerceSemanticSearch:
def __init__(self, api_key):
self.api_key = api_key
self.products = {}
def index_product(self, product_id, product_data):
"""Index an e-commerce product for semantic search"""
# Create searchable text from product data
searchable_text = self.create_product_text(product_data)
# Get embedding
embedding = self.get_embedding(searchable_text)
# Store product with embedding
self.products[product_id] = {
"data": product_data,
"searchable_text": searchable_text,
"embedding": embedding
}
def create_product_text(self, product_data):
"""Create comprehensive searchable text from product data"""
text_parts = [
product_data.get("title", ""),
product_data.get("description", ""),
product_data.get("brand", ""),
product_data.get("category", ""),
" ".join(product_data.get("tags", [])),
" ".join(product_data.get("features", [])),
product_data.get("color", ""),
product_data.get("material", ""),
product_data.get("size", "")
]
return " ".join(filter(None, text_parts))
def get_embedding(self, text):
"""Generate embedding for text"""
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": text
}
)
return response.json()["data"][0]["embedding"]
def search_products(self, query, filters=None, top_k=20):
"""Search products with optional filters"""
query_embedding = self.get_embedding(query)
# Calculate similarities
similarities = []
for product_id, product_info in self.products.items():
# Check filters first
if filters and not self.matches_filters(product_info["data"], filters):
continue
similarity = cosine_similarity(
[query_embedding],
[product_info["embedding"]]
)[0][0]
similarities.append({
"product_id": product_id,
"similarity": similarity,
"product": product_info["data"]
})
# Sort by similarity
similarities.sort(key=lambda x: x["similarity"], reverse=True)
return similarities[:top_k]
def matches_filters(self, product_data, filters):
"""Check if product matches specified filters"""
for key, value in filters.items():
if key == "price_range":
price = product_data.get("price", 0)
if not (value[0] <= price <= value[1]):
return False
elif key == "categories":
if product_data.get("category") not in value:
return False
elif key == "brands":
if product_data.get("brand") not in value:
return False
elif key == "in_stock":
if product_data.get("in_stock", False) != value:
return False
return True
def get_recommendations(self, product_id, top_k=5):
"""Get similar products (recommendations)"""
if product_id not in self.products:
return []
target_embedding = self.products[product_id]["embedding"]
similarities = []
for pid, product_info in self.products.items():
if pid == product_id: # Skip self
continue
similarity = cosine_similarity(
[target_embedding],
[product_info["embedding"]]
)[0][0]
similarities.append({
"product_id": pid,
"similarity": similarity,
"product": product_info["data"]
})
similarities.sort(key=lambda x: x["similarity"], reverse=True)
return similarities[:top_k]
# Usage example
ecommerce_search = EcommerceSemanticSearch("your_api_key")
# Index sample products
products = [
{
"id": "prod_1",
"data": {
"title": "Wireless Noise-Cancelling Headphones",
"description": "Premium over-ear headphones with active noise cancellation and 30-hour battery life",
"brand": "AudioTech",
"category": "Electronics",
"price": 299.99,
"tags": ["wireless", "bluetooth", "noise-cancelling", "premium"],
"features": ["Active noise cancellation", "30-hour battery", "Quick charge"],
"color": "Black",
"in_stock": True
}
},
{
"id": "prod_2",
"data": {
"title": "Bluetooth Sport Earbuds",
"description": "Waterproof wireless earbuds perfect for workouts and running",
"brand": "FitSound",
"category": "Electronics",
"price": 89.99,
"tags": ["wireless", "sport", "waterproof", "earbuds"],
"features": ["IPX7 waterproof", "Secure fit", "8-hour battery"],
"color": "Blue",
"in_stock": True
}
}
]
# Index products
for product in products:
ecommerce_search.index_product(product["id"], product["data"])
# Search examples
results = ecommerce_search.search_products(
"noise cancelling headphones for music",
top_k=5
)
print("Search results:")
for result in results:
print(f"Score: {result['similarity']:.3f} - {result['product']['title']}")
# Search with filters
filtered_results = ecommerce_search.search_products(
"wireless audio",
filters={
"price_range": [50, 200],
"categories": ["Electronics"],
"in_stock": True
},
top_k=5
)
print("\nFiltered search results:")
for result in filtered_results:
print(f"Score: {result['similarity']:.3f} - {result['product']['title']} - ${result['product']['price']}")
# Get recommendations
recommendations = ecommerce_search.get_recommendations("prod_1", top_k=3)
print("\nRecommendations for product prod_1:")
for rec in recommendations:
print(f"Score: {rec['similarity']:.3f} - {rec['product']['title']}")
```
### Document and Knowledge Base Search
```python theme={"system"}
class KnowledgeBaseSearch:
def __init__(self, api_key):
self.api_key = api_key
self.documents = {}
def index_document(self, doc_id, title, content, metadata=None):
"""Index a document with hierarchical content structure"""
# Split content into chunks for better search
chunks = self.chunk_content(content, chunk_size=500, overlap=50)
# Process each chunk
for i, chunk in enumerate(chunks):
chunk_id = f"{doc_id}_chunk_{i}"
# Create comprehensive text for embedding
searchable_text = f"{title}\n\n{chunk}"
# Get embedding
embedding = self.get_embedding(searchable_text)
# Store chunk with metadata
self.documents[chunk_id] = {
"parent_doc_id": doc_id,
"title": title,
"content": chunk,
"chunk_index": i,
"searchable_text": searchable_text,
"embedding": embedding,
"metadata": metadata or {}
}
def chunk_content(self, content, chunk_size=500, overlap=50):
"""Split content into overlapping chunks"""
words = content.split()
chunks = []
for i in range(0, len(words), chunk_size - overlap):
chunk_words = words[i:i + chunk_size]
chunk_text = " ".join(chunk_words)
chunks.append(chunk_text)
# Break if we've covered all words
if i + chunk_size >= len(words):
break
return chunks
def get_embedding(self, text):
"""Generate embedding for text"""
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": text
}
)
return response.json()["data"][0]["embedding"]
def search(self, query, top_k=10, min_similarity=0.3):
"""Search across all document chunks"""
query_embedding = self.get_embedding(query)
similarities = []
for chunk_id, chunk_data in self.documents.items():
similarity = cosine_similarity(
[query_embedding],
[chunk_data["embedding"]]
)[0][0]
if similarity >= min_similarity:
similarities.append({
"chunk_id": chunk_id,
"doc_id": chunk_data["parent_doc_id"],
"title": chunk_data["title"],
"content": chunk_data["content"],
"similarity": similarity,
"metadata": chunk_data["metadata"]
})
# Sort by similarity
similarities.sort(key=lambda x: x["similarity"], reverse=True)
# Group by document and take best chunks
doc_results = {}
for result in similarities:
doc_id = result["doc_id"]
if doc_id not in doc_results:
doc_results[doc_id] = []
doc_results[doc_id].append(result)
# Return top results with max 2 chunks per document
final_results = []
for doc_id, chunks in doc_results.items():
final_results.extend(chunks[:2]) # Max 2 chunks per doc
return sorted(final_results, key=lambda x: x["similarity"], reverse=True)[:top_k]
def answer_question(self, question, context_chunks=3):
"""Answer a question using relevant document chunks"""
# Search for relevant chunks
search_results = self.search(question, top_k=context_chunks)
if not search_results:
return "I couldn't find relevant information to answer your question."
# Create context from top chunks
context = "\n\n".join([
f"From '{result['title']}':\n{result['content']}"
for result in search_results
])
# Generate answer using AI
response = requests.post(
"https://api.anyapi.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant. Answer the user's question based on the provided context. If the context doesn't contain enough information, say so clearly."
},
{
"role": "user",
"content": f"Context:\n{context}\n\nQuestion: {question}\n\nAnswer:"
}
]
}
)
answer = response.json()["choices"][0]["message"]["content"]
return {
"answer": answer,
"sources": [
{
"title": result["title"],
"relevance": result["similarity"],
"doc_id": result["doc_id"]
}
for result in search_results
]
}
# Usage
kb_search = KnowledgeBaseSearch("your_api_key")
# Index documents
documents = [
{
"id": "doc_ai_basics",
"title": "Introduction to Artificial Intelligence",
"content": """
Artificial Intelligence (AI) is a branch of computer science that aims to create
machines capable of intelligent behavior. AI systems can perform tasks that typically
require human intelligence, such as visual perception, speech recognition,
decision-making, and language translation.
There are several types of AI: narrow AI (designed for specific tasks),
general AI (human-level intelligence across domains), and superintelligence
(exceeding human intelligence). Currently, most AI systems are narrow AI.
Machine learning is a subset of AI that enables systems to learn and improve
from experience without being explicitly programmed. Deep learning, a subset
of machine learning, uses neural networks with multiple layers to model
complex patterns in data.
""",
"metadata": {"category": "Technology", "difficulty": "Beginner"}
}
]
for doc in documents:
kb_search.index_document(doc["id"], doc["title"], doc["content"], doc["metadata"])
# Search and answer questions
result = kb_search.answer_question("What is the difference between narrow AI and general AI?")
print("Answer:", result["answer"])
print("\nSources:")
for source in result["sources"]:
print(f"- {source['title']} (relevance: {source['relevance']:.3f})")
```
## Performance Optimization
### Caching and Indexing
```python theme={"system"}
import redis
import pickle
import hashlib
class CachedSemanticSearch:
def __init__(self, api_key, redis_url=None):
self.api_key = api_key
self.redis_client = redis.from_url(redis_url) if redis_url else None
self.cache_ttl = 86400 # 24 hours
def get_embedding_cached(self, text):
"""Get embedding with Redis caching"""
if not self.redis_client:
return self.get_embedding(text)
# Create cache key
cache_key = f"embedding:{hashlib.md5(text.encode()).hexdigest()}"
# Try to get from cache
cached_embedding = self.redis_client.get(cache_key)
if cached_embedding:
return pickle.loads(cached_embedding)
# Generate new embedding
embedding = self.get_embedding(text)
# Cache the result
self.redis_client.setex(
cache_key,
self.cache_ttl,
pickle.dumps(embedding)
)
return embedding
def get_embedding(self, text):
"""Generate embedding using API"""
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": text
}
)
return response.json()["data"][0]["embedding"]
def batch_process_documents(self, documents, batch_size=100):
"""Process documents in batches for efficiency"""
processed_docs = []
for i in range(0, len(documents), batch_size):
batch = documents[i:i + batch_size]
# Extract texts
texts = [doc["content"] for doc in batch]
# Get embeddings in batch
response = requests.post(
"https://api.anyapi.ai/v1/embeddings",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": "openai/text-embedding-3-large",
"input": texts
}
)
embeddings = response.json()["data"]
# Combine documents with embeddings
for doc, embedding in zip(batch, embeddings):
processed_docs.append({
**doc,
"embedding": embedding["embedding"]
})
print(f"Processed batch {i//batch_size + 1}/{(len(documents)-1)//batch_size + 1}")
return processed_docs
```
## Best Practices
### 1. Embedding Model Selection
* **text-embedding-3-large**: Best quality, higher cost
* **text-embedding-3-small**: Good balance of quality and speed
* **Cohere embeddings**: Strong for specific domains
### 2. Content Preparation
* **Clean text**: Remove unnecessary formatting and noise
* **Chunk appropriately**: Split long documents for better search
* **Include metadata**: Store searchable metadata separately
* **Normalize content**: Consistent formatting and structure
### 3. Search Optimization
* **Hybrid search**: Combine semantic and keyword search
* **Result reranking**: Use AI to rerank semantic results
* **Query expansion**: Enhance queries with synonyms
* **Personalization**: Adapt results to user preferences
### 4. Performance Tuning
* **Batch processing**: Generate embeddings in batches
* **Caching**: Cache embeddings and search results
* **Indexing**: Use vector databases for large datasets
* **Monitoring**: Track search quality and performance
## Common Use Cases
Internal knowledge bases, document repositories, team wikis
Product discovery, recommendation systems, catalog search
FAQ systems, help desk automation, knowledge retrieval
Media libraries, educational content, research databases
Case law search, document analysis, contract review
Clinical decision support, research literature, patient records
Research reports, compliance documents, market analysis
Paper discovery, literature review, citation analysis
## Getting Started
Build your first semantic search
Learn about embedding models
Complete search solution
Use our official libraries