Provider-सचेत Prompt Caching
Superdav AI Agent v1.12.0 ले provider-सचेत prompt caching परिचय गराउँछ, जसले विभिन्न LLM providers भरि prompts cache गरेर API लागत र विलम्बता अनुकूलित गर्छ। प्रत्येक provider का caching संयन्त्र र configurations फरक हुन्छन्।
सिंहावलोकन
Prompt caching ले तपाईंलाई यी गर्न दिन्छ:
- ठूला, बारम्बार प्रयोग हुने prompts cache गर्नु
- दोहोरिने processing जोगाएर API लागत घटाउनु
- cached requests का लागि विलम्बता सुधार गर्नु
- cache lifecycle स्पष्ट रूपमा व्यवस्थापन गर्नु
विभिन्न providers ले caching फरक तरिकाले कार्यान्वयन गर्छन्:
- Google Gemini:
cachedContentsAPI - Azure OpenAI: TTL सहितको Prompt caching
- OpenRouter: Provider-specific caching
- Vertex Anthropic: cache control सहितको Prompt caching
Google Gemini: cachedContents API
Google Gemini ले cachedContents API मार्फत स्पष्ट cache management प्रदान गर्छ।
Configuration
$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
],
];
Cached Prompt सिर्जना गर्दै
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]
Cached Prompt प्रयोग गर्दै
$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' );
Gemini का लागि उत्तम अभ्यासहरू
- उपयुक्त TTL सेट गर्नुहोस्: लागत बचत र cache staleness बीच सन्तुलन मिलाउनुहोस्
- system prompts cache गर्नुहोस्: requests भरि एउटै system prompt पुनः प्रयोग गर्नुहोस्
- cache usage निगरानी गर्नुहोस्: कुन caches सबैभन्दा बढी प्रयोग हुन्छन् track गर्नुहोस्
- expired caches सफा गर्नुहोस्: प्रयोग नभएका caches आवधिक रूपमा delete गर्नुहोस्
Azure OpenAI: Prompt Caching
Azure OpenAI ले automatic TTL management सहित prompt caching समर्थन गर्छ।
Configuration
$config = [
'provider' => 'azure-openai',
'model' => 'gpt-4-turbo',
'api_version' => '2024-08-01-preview',
'caching' => [
'enabled' => true,
'cache_control' => 'max_age=3600',
],
];
Caching सक्षम गर्दै
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 ले cache control का लागि HTTP headers प्रयोग गर्छ:
Cache-Control: max_age=3600
समर्थित मानहरू:
max_age=<seconds>: तोकिएको अवधिका लागि cache गर्नुहोस्no_cache: यो request cache नगर्नुहोस्no_store: cache नगर्नुहोस् र पुनः प्रयोग नगर्नुहोस्
Cache Usage निगरानी
$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";
Azure OpenAI का लागि उत्तम अभ्यासहरू
- consistent prompts प्रयोग गर्नुहोस्: समान prompts ले caching बाट लाभ लिन्छन्
- reasonable TTL सेट गर्नुहोस्: लागत र freshness बीच सन्तुलन मिलाउनुहोस्
- cache metrics निगरानी गर्नुहोस्: cache creation र hits track गर्नुहोस्
- similar requests batch गर्नुहोस्: cache hits अधिकतम बनाउन requests group गर्नुहोस्
OpenRouter: Provider-Specific Caching
OpenRouter ले underlying providers (OpenAI, Anthropic, आदि) मार्फत caching समर्थन गर्छ।
Configuration
$config = [
'provider' => 'openrouter',
'model' => 'openai/gpt-4-turbo',
'caching' => [
'enabled' => true,
'provider_cache' => 'openai', // Use OpenAI's caching
],
];