-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add semantic caching, update model and locations
- Loading branch information
1 parent
a532a8d
commit 2987fb5
Showing
3 changed files
with
375 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<policies> | ||
<inbound> | ||
<base /> | ||
<!--Policy 4 - Semantic Caching--> | ||
<!-- Check the embeddings in the Redis cache for a cached prompt response according to the score threshold. If successful, the policy automatically moves past the azure-openai-semantic-cache-store policy. --> | ||
<azure-openai-semantic-cache-lookup score-threshold="0.8" embeddings-backend-id ="embeddings-backend" embeddings-backend-auth ="system-assigned" /> | ||
<!-- Authenticate to Azure OpenAI with API Management's managed identity --> | ||
<authentication-managed-identity resource="https://cognitiveservices.azure.com" output-token-variable-name="managed-id-access-token" ignore-error="false" /> | ||
<set-header name="Authorization" exists-action="override"> | ||
<value>@("Bearer " + (string)context.Variables["managed-id-access-token"])</value> | ||
</set-header> | ||
<set-backend-service backend-id="{backend-id}" /> | ||
<!--Policy 3 - Limit the tokens per subscription--> | ||
<azure-openai-token-limit counter-key="@(context.Subscription.Id)" tokens-per-minute="{tpm}" estimate-prompt-tokens="false" remaining-tokens-variable-name="remainingTokens" /> | ||
<!--Policy 2 - Emit the Azure OpenAI Token Metrics --> | ||
<azure-openai-emit-token-metric namespace="openai"> | ||
<dimension name="Subscription ID" value="@(context.Subscription.Id)" /> | ||
<dimension name="Client IP" value="@(context.Request.IpAddress)" /> | ||
<dimension name="API ID" value="@(context.Api.Id)" /> | ||
<dimension name="User ID" value="@(context.Request.Headers.GetValueOrDefault("x-user-id", "N/A"))" /> | ||
</azure-openai-emit-token-metric> | ||
</inbound> | ||
<backend> | ||
<!--Policy 1 - Apply load-balancing and retry mechanisms --> | ||
<!--Set count to one less than the number of backends in the pool to try all backends until the backend pool is temporarily unavailable.--> | ||
<retry count="{retry-count}" interval="0" first-fast-retry="true" condition="@(context.Response.StatusCode == 429 || (context.Response.StatusCode == 503 && !context.Response.StatusReason.Contains("Backend pool") && !context.Response.StatusReason.Contains("is temporarily unavailable")))"> | ||
<forward-request buffer-request-body="true" /> | ||
</retry> | ||
</backend> | ||
<outbound> | ||
<!-- Cache the Gen AI response in Redis for 2 minutes --> | ||
<azure-openai-semantic-cache-store duration="120" /> | ||
<base /> | ||
</outbound> | ||
<on-error> | ||
<base /> | ||
<choose> | ||
<!--Return a generic error that does not reveal backend pool details.--> | ||
<when condition="@(context.Response.StatusCode == 503)"> | ||
<return-response> | ||
<set-status code="503" reason="Service Unavailable" /> | ||
</return-response> | ||
</when> | ||
</choose> | ||
</on-error> | ||
</policies> |
Oops, something went wrong.