Prompt Caching nke Na-amata Provider
Superdav AI Agent v1.12.0 na-ewebata prompt caching nke na-amata provider, nke na-eme ka ụgwọ API na latency ka mma site n’ịchekwa prompts gafee providers LLM dị iche iche. Provider ọ bụla nwere usoro caching na nhazi dị iche iche.
Nchịkọta
Prompt caching na-enye gị ohere:
- Chekwaa prompts buru ibu, a na-ejikarị eme ihe
- Belata ụgwọ API site n’izere nhazi ugboro ugboro na-enweghị mkpa
- Mee ka latency ka mma maka arịrịọ echekwara na cache
- Jikwaa cache lifecycle n'ụzọ doro anya
Providers dị iche iche na-etinye caching n’ọrụ n’ụzọ dị iche:
- Google Gemini:
cachedContentsAPI - Azure OpenAI: Prompt caching nwere TTL
- OpenRouter: Caching pụrụ iche maka provider
- Vertex Anthropic: Prompt caching nwere cache control
Google Gemini: cachedContents API
Google Gemini na-enye njikwa cache doro anya site na cachedContents API.
Nhazi
$config = [
'provider' => 'google-gemini',
'model' => 'gemini-2.0-flash',
'caching' => [
'enabled' => true,
'ttl' => 3600, // 1 hour in seconds
'max_tokens' => 1000000, // Max tokens to cache
],
];
Ịmepụta Prompt Echekwara na Cache
use Superdav\AI\Providers\GoogleGemini;
$gemini = new GoogleGemini( $config );
$cached_content = $gemini->create_cached_content(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'ttl' => 3600,
]
);
// Returns: ['cache_id' => 'abc123', 'expires_at' => timestamp]
Iji Prompt Echekwara na Cache
$response = $gemini->generate(
[
'cache_id' => 'abc123',
'prompt' => 'User question here',
]
);
Cache Lifecycle
// List cached contents
$caches = $gemini->list_cached_contents();
// Get cache details
$cache = $gemini->get_cached_content( 'abc123' );
// Extend cache TTL
$gemini->update_cached_content(
'abc123',
['ttl' => 7200] // Extend to 2 hours
);
// Delete cache
$gemini->delete_cached_content( 'abc123' );
Omume Kachasị Mma maka Gemini
- Tọọ TTL kwesịrị ekwesị: Hazie n’etiti ịchekwa ụgwọ na ịka nká nke cache
- Chekwaa system prompts na cache: Jiri otu system prompt ọzọ gafee arịrịọ
- Nyochaa ojiji cache: Soro caches a na-eji nke ukwuu
- Hichapụ caches gwụchara oge: Hichapụ caches anaghị eji eme ihe kwa oge
Azure OpenAI: Prompt Caching
Azure OpenAI na-akwado prompt caching nwere njikwa TTL akpaka.
Nhazi
$config = [
'provider' => 'azure-openai',
'model' => 'gpt-4-turbo',
'api_version' => '2024-08-01-preview',
'caching' => [
'enabled' => true,
'cache_control' => 'max_age=3600',
],
];
Ime ka Caching rụọ ọrụ
use Superdav\AI\Providers\AzureOpenAI;
$azure = new AzureOpenAI( $config );
$response = $azure->generate(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'prompt' => 'User question here',
'cache_control' => 'max_age=3600',
]
);
// Response includes cache usage:
// [
// 'content' => '...',
// 'cache_creation_input_tokens' => 1000,
// 'cache_read_input_tokens' => 500,
// ]
Cache Headers
Azure OpenAI na-eji HTTP headers maka cache control:
Cache-Control: max_age=3600
Ụkpụrụ akwadoro:
max_age=<seconds>: Chekwaa na cache ruo oge akọwapụtarano_cache: Echekwala arịrịọ a na cacheno_store: Echekwala na cache ma ejikwala ọzọ
Nyochaa Ojiji Cache
$response = $azure->generate( [...] );
$cache_tokens = $response['cache_creation_input_tokens'] ?? 0;
$cache_hits = $response['cache_read_input_tokens'] ?? 0;
echo "Cache creation: $cache_tokens tokens\n";
echo "Cache hits: $cache_hits tokens\n";
Omume Kachasị Mma maka Azure OpenAI
- Jiri prompts na-agbanwebeghị: Prompts yiri otu na-erite uru na caching
- Tọọ TTL ezi uche dị na ya: Hazie n’etiti ụgwọ na ịdị ọhụrụ
- Nyochaa metrics cache: Soro imepụta cache megide hits
- Kpokọta arịrịọ yiri ibe ha: Kpọkọta arịrịọ iji mee ka cache hits baa ụba
OpenRouter: Caching Pụrụ Iche maka Provider
OpenRouter na-akwado caching site na providers dị n’okpuru (OpenAI, Anthropic, wdg.).
Nhazi
$config = [
'provider' => 'openrouter',
'model' => 'openai/gpt-4-turbo',
'caching' => [
'enabled' => true,
'provider_cache' => 'openai', // Use OpenAI's caching
],
];
Iji OpenRouter Caching
use Superdav\AI\Providers\OpenRouter;
$router = new OpenRouter( $config );
$response = $router->generate(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'prompt' => 'User question here',
'cache_control' => 'max_age=3600',
]
);
Nhọrọ Pụrụ Iche maka Provider
Providers dị iche iche nwere usoro caching dị iche:
// OpenAI-compatible caching
$response = $router->generate(
[
'model' => 'openai/gpt-4-turbo',
'cache_control' => 'max_age=3600',
]
);
// Anthropic-compatible caching
$response = $router->generate(
[
'model' => 'anthropic/claude-3-opus',
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
]
);
Omume Kachasị Mma maka OpenRouter
- Mara caching nke provider gị: Provider ọ bụla nwere usoro dị iche
- Nwale omume caching: Nyochaa na caching na-arụ ọrụ na provider ị họọrọ
- Nyochaa ụgwọ: Soro ego echekwara site na caching
- Jiri models na-agbanwebeghị: Ịgbanwe models na-emebi cache hits
Vertex Anthropic: Prompt Caching nwere Cache Control
Vertex Anthropic (Google Cloud) na-akwado prompt caching nwere cache control doro anya.
Nhazi
$config = [
'provider' => 'vertex-anthropic',
'model' => 'claude-3-opus',
'project_id' => 'your-gcp-project',
'region' => 'us-central1',
'caching' => [
'enabled' => true,
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
],
];
Iji Vertex Anthropic Caching
use Superdav\AI\Providers\VertexAnthropic;
$vertex = new VertexAnthropic( $config );
$response = $vertex->generate(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'prompt' => 'User question here',
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
]
);
// Response includes cache metrics:
// [
// 'content' => '...',
// 'usage' => [
// 'input_tokens' => 1000,
// 'cache_creation_input_tokens' => 500,
// 'cache_read_input_tokens' => 300,
// ],
// ]
Ụdị Cache Control
- ephemeral: Cache maka oge arịrịọ ahụ na-adị (ndabara)
- persistent: Cache gafee ọtụtụ arịrịọ (ma ọ bụrụ na a na-akwado ya)
Nlekọta Ojiji Cache
$response = $vertex->generate( [...] );
$usage = $response['usage'];
$cache_created = $usage['cache_creation_input_tokens'] ?? 0;
$cache_read = $usage['cache_read_input_tokens'] ?? 0;
echo "Cache created: $cache_created tokens\n";
echo "Cache read: $cache_read tokens\n";
Omume Kachasị Mma maka Vertex Anthropic
- Jiri ephemeral caching: Ọ dị mma maka caching nke otu nnọkọ
- Tọọ max_tokens nke ọma: Mee nha cache na ọnụ ahịa kwekọọ
- Nyochaa cache metrics: Soro ịdị irè cache
- Nwale na ibu ọrụ gị: Kwenye na caching bara uru maka ọnọdụ ojiji gị
Atụmatụ Caching Gafee Provider
Nhazi Jikọrọ Ọnụ
$config = [
'caching' => [
'enabled' => true,
'default_ttl' => 3600,
'providers' => [
'google-gemini' => [
'ttl' => 3600,
'max_tokens' => 1000000,
],
'azure-openai' => [
'cache_control' => 'max_age=3600',
],
'vertex-anthropic' => [
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
],
],
],
];
Nchọpụta Provider
$provider = $config['provider'];
$cache_config = $config['caching']['providers'][ $provider ]
?? $config['caching'];
// Use provider-specific caching configuration
Atụmatụ Fallback
try {
// Try caching with primary provider
$response = $primary_provider->generate( $request );
} catch ( CacheException $e ) {
// Fall back to non-cached request
$response = $primary_provider->generate(
array_merge( $request, ['cache_control' => 'no_cache'] )
);
}