Caching ny Prompt Mahafantatra Provider
Superdav AI Agent v1.12.0 dia mampiditra caching ny prompt mahafantatra provider, izay manatsara ny vidin’ny API sy ny latency amin’ny alalan’ny caching ny prompts manerana ireo provider LLM samihafa. Samy manana mekanisma sy fikirakirana caching samihafa ny provider tsirairay.
Topimaso
Ny caching ny prompt dia mamela anao:
- Hanaovana cache ireo prompts lehibe sy ampiasaina matetika
- Hampihenana ny vidin’ny API amin’ny fisorohana fanodinana miverimberina
- Hanatsarana ny latency ho an’ireo fangatahana voatahiry anaty cache
- Hitantana mazava ny tsingerim-piainan’ny cache
Samy hafa ny fomba anatanterahan’ny provider samihafa ny caching:
- Google Gemini:
cachedContentsAPI - Azure OpenAI: Caching ny prompt miaraka amin’ny TTL
- OpenRouter: Caching manokana arakaraka ny provider
- Vertex Anthropic: Caching ny prompt miaraka amin’ny fanaraha-maso cache
Google Gemini: cachedContents API
Google Gemini dia manome fitantanana cache mazava amin’ny alalan’ny cachedContents API.
Fikirakirana
$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
],
];
Famoronana Prompt Voatahiry anaty 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]
Fampiasana Prompt Voatahiry anaty Cache
$response = $gemini->generate(
[
'cache_id' => 'abc123',
'prompt' => 'User question here',
]
);
Tsingerim-piainan’ny 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' );
Fomba Fanao Tsara Indrindra ho an’ny Gemini
- Mametraha TTL mety: Ampifandanjao ny fitsitsiana vola sy ny fahalasanan’ny cache
- Ataovy cache ny system prompts: Ampiasao indray ilay system prompt mitovy manerana ny fangatahana
- Araho maso ny fampiasana cache: Araho hoe iza amin’ireo cache no ampiasaina indrindra
- Diovy ireo cache lany daty: Fafao tsindraindray ireo cache tsy ampiasaina
Azure OpenAI: Caching ny Prompt
Azure OpenAI dia manohana caching ny prompt miaraka amin’ny fitantanana TTL mandeha ho azy.
Fikirakirana
$config = [
'provider' => 'azure-openai',
'model' => 'gpt-4-turbo',
'api_version' => '2024-08-01-preview',
'caching' => [
'enabled' => true,
'cache_control' => 'max_age=3600',
],
];
Fampandehanana ny 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,
// ]
Lohateny Cache
Azure OpenAI dia mampiasa lohateny HTTP ho an’ny fanaraha-maso cache:
Cache-Control: max_age=3600
Sanda tohanana:
max_age=<seconds>: Ataovy cache mandritra ny faharetana voafaritrano_cache: Aza atao cache ity fangatahana ityno_store: Aza atao cache ary aza ampiasaina indray
Fanaraha-maso ny Fampiasana 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";
Fomba Fanao Tsara Indrindra ho an’ny Azure OpenAI
- Mampiasà prompts tsy miovaova: Mahazo tombony amin’ny caching ny prompts mitovy tanteraka
- Mametraha TTL antonony: Ampifandanjao ny vidiny sy ny maha-vaovao
- Araho maso ny metrika cache: Araho ny famoronana cache raha oharina amin’ny hits
- Ataovy andiany ireo fangatahana mitovy: Vondroy ny fangatahana mba hampitomboana ny cache hits
OpenRouter: Caching Manokana arakaraka ny Provider
OpenRouter dia manohana caching amin’ny alalan’ireo provider fototra (OpenAI, Anthropic, sns.).
Fikirakirana
$config = [
'provider' => 'openrouter',
'model' => 'openai/gpt-4-turbo',
'caching' => [
'enabled' => true,
'provider_cache' => 'openai', // Use OpenAI's caching
],
];
Fampiasana Caching OpenRouter
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',
]
);
Safidy Manokana arakaraka ny Provider
Samy manana mekanisma caching samihafa ny provider samihafa:
// 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,
],
]
);
Fomba Fanao Tsara Indrindra ho an’ny OpenRouter
- Fantaro ny caching an’ny provider-nao: Samy manana mekanisma samihafa ny provider tsirairay
- Andramo ny fitondran-tenan’ny caching: Hamarino fa mandeha amin’ilay provider nofidinao ny caching
- Araho maso ny vola lany: Araho ny fitsitsiana azo avy amin’ny caching
- Mampiasà models tsy miovaova: Manapaka ny cache hits ny fifindrana models
Vertex Anthropic: Caching ny Prompt miaraka amin’ny Fanaraha-maso Cache
Vertex Anthropic (Google Cloud) dia manohana caching ny prompt miaraka amin’ny fanaraha-maso cache mazava.
Fikirakirana
$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,
],
],
];
Fampiasana 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,
// ],
// ]
Karazana Cache Control
- ephemeral: Cache mandritra ny fangatahana (default)
- persistent: Cache manerana fangatahana maro (raha tohanana)
Fanaraha-maso ny Fampiasana 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";