تەمىنلىگۈچىدىن خەۋەردار Prompt Caching
Superdav AI Agent v1.12.0 تەمىنلىگۈچىدىن خەۋەردار prompt caching نى تونۇشتۇرىدۇ؛ بۇ ئوخشىمىغان LLM تەمىنلىگۈچىلەر ئارىسىدا prompt لارنى cache قىلىش ئارقىلىق API خىراجىتى ۋە كېچىكىشنى ئەلالاشتۇرىدۇ. ھەر بىر تەمىنلىگۈچىنىڭ cache مېخانىزمى ۋە سەپلىمىلىرى ئوخشىمايدۇ.
ئومۇمىي چۈشەنچە
Prompt caching سىزگە تۆۋەندىكىلەرنى قىلىش ئىمكانىنى بېرىدۇ:
- چوڭ، دائىم ئىشلىتىلىدىغان prompt لارنى cache قىلىش
- تەكرار بىر تەرەپ قىلىشتىن ساقلىنىپ API خىراجىتىنى ئازايتىش
- cache قىلىنغان تەلەپلەرنىڭ كېچىكىشىنى ياخشىلاش
- cache ھايات دەۋرىنى ئېنىق باشقۇرۇش
ئوخشىمىغان تەمىنلىگۈچىلەر cache قىلىشنى ئوخشىمىغان ئۇسۇلدا ئەمەلگە ئاشۇرىدۇ:
- Google Gemini:
cachedContentsAPI - Azure OpenAI: TTL بىلەن prompt caching
- OpenRouter: تەمىنلىگۈچىگە خاس caching
- Vertex Anthropic: cache control بىلەن prompt caching
Google Gemini: cachedContents API
Google Gemini cachedContents API ئارقىلىق ئېنىق cache باشقۇرۇشنى تەمىنلەيدۇ.
سەپلىمە
$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
],
];
Cache قىلىنغان 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]
Cache قىلىنغان Prompt نى ئىشلىتىش
$response = $gemini->generate(
[
'cache_id' => 'abc123',
'prompt' => 'User question here',
]
);
Cache ھايات دەۋرى
// 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 نىڭ كونىرىشى ئوتتۇرىسىدا تەڭپۇڭلۇق ساقلاڭ
- سىستېما prompt لارنى cache قىلىڭ: ئوخشاش سىستېما prompt نى تەلەپلەر ئارىسىدا قايتا ئىشلىتىڭ
- cache ئىشلىتىلىشىنى كۆزىتىڭ: قايسى cache لارنىڭ ئەڭ كۆپ ئىشلىتىلىدىغانلىقىنى ئىز قوغلاڭ
- مۇددىتى ئۆتكەن cache لارنى تازىلاڭ: ئىشلىتىلمەيدىغان cache لارنى قەرەللىك ئۆچۈرۈڭ
Azure OpenAI: Prompt Caching
Azure OpenAI ئاپتوماتىك TTL باشقۇرۇشى بىلەن prompt caching نى قوللايدۇ.
سەپلىمە
$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: بۇ تەلەپنى cache قىلماسلىقno_store: cache قىلماسلىق ۋە قايتا ئىشلەتمەسلىك
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";
Azure OpenAI ئۈچۈن ئەڭ ياخشى ئەمەلىيەتلەر
- ئىزچىل prompt لارنى ئىشلىتىڭ: ئوخشاش prompt لار caching دىن پايدا ئالىدۇ
- مۇۋاپىق TTL بەلگىلەڭ: خىراجەت بىلەن يېڭىلىق ئوتتۇرىسىدا تەڭپۇڭلۇق ساقلاڭ
- cache ئۆلچەملىرىنى كۆزىتىڭ: cache قۇرۇلۇشى بىلەن hit لارنى ئىز قوغلاڭ
- ئوخشاش تەلەپلەرنى توپلاڭ: cache hit لارنى ئەڭ يۇقىرى چەككە يەتكۈزۈش ئۈچۈن تەلەپلەرنى گۇرۇپپىلاڭ
OpenRouter: تەمىنلىگۈچىگە خاس Caching
OpenRouter ئاستىدىكى تەمىنلىگۈچىلەر (OpenAI، Anthropic قاتارلىقلار) ئارقىلىق caching نى قوللايدۇ.
سەپلىمە
$config = [
'provider' => 'openrouter',
'model' => 'openai/gpt-4-turbo',
'caching' => [
'enabled' => true,
'provider_cache' => 'openai', // Use OpenAI's caching
],
];
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',
]
);
تەمىنلىگۈچىگە خاس تاللانمىلار
ئوخشىمىغان تەمىنلىگۈچىلەرنىڭ cache مېخانىزملىرى ئوخشىمايدۇ:
// 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,
],
]
);
OpenRouter ئۈچۈن ئەڭ ياخشى ئەمەلىيەتلەر
- تەمىنلىگۈچىڭىزنىڭ caching نى بىلىڭ: ھەر بىر تەمىنلىگۈچىنىڭ مېخانىزمى ئوخشىمايدۇ
- caching ھەرىكىتىنى سىناڭ: تاللىغان تەمىنلىگۈچىڭىز بىلەن caching نىڭ ئىشلەيدىغانلىقىنى دەلىللەڭ
- خىراجەتنى كۆزىتىڭ: caching ئارقىلىق تېجەلگەن مىقدارنى ئىز قوغلاڭ
- ئىزچىل model لارنى ئىشلىتىڭ: model ئالماشتۇرۇش cache hit لارنى بۇزىدۇ
Vertex Anthropic: Cache Control بىلەن Prompt Caching
Vertex Anthropic (Google Cloud) ئېنىق cache control بىلەن prompt caching نى قوللايدۇ.
سەپلىمە
$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,
],
],
];
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,
// ],
// ]
Cache كونترول تۈرلىرى
- ephemeral: ئىلتىماس داۋامىدا Cache قىلىش (كۆڭۈلدىكى)
- persistent: كۆپ ئىلتىماس ئارىسىدا Cache قىلىش (قوللىسا)
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";
Vertex Anthropic ئۈچۈن ئەڭ ياخشى ئەمەلىيەتلەر
- ephemeral caching نى ئىشلىتىڭ: يەككە session Cache قىلىشقا ماس كېلىدۇ
- max_tokens نى مۇۋاپىق تەڭشەڭ: Cache چوڭلۇقى بىلەن خىراجەت ئارىسىدا تەڭپۇڭلۇق ساقلاڭ
- Cache ئۆلچەملىرىنى كۆزىتىڭ: Cache ئۈنۈمىنى ئىز قوغلاڭ
- ئۆز workloadىڭىز بىلەن سىناڭ: Cache قىلىشنىڭ سىزنىڭ ئىشلىتىش ئەھۋالىڭىزغا پايدا ئېلىپ كېلىدىغانلىقىنى دەلىللەڭ
تەمىنلىگۈچىلەر ئارا Cache ئىستراتېگىيەسى
بىرلەشمە تەڭشەك
$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,
],
],
],
],
];