-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathfrag-openai-usage-streaming.xml
64 lines (59 loc) · 3.52 KB
/
frag-openai-usage-streaming.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<fragment>
<!-- Usage logs for "streaming" requests only -->
<choose>
<!-- Usage logs for streaming requests -->
<when condition="@(context.Variables.GetValueOrDefault<string>("isStream","false").Equals("true", StringComparison.OrdinalIgnoreCase))">
<set-variable name="potentialRouteIndex" value="@{
JArray routes = (JArray)context.Variables["routes"];
int selectedPriority = Int32.MaxValue;
List<int> availableRoutesIndexes = new List<int>();
for (int i = 0; i < routes.Count; i++)
{
JObject route = (JObject)routes[i];
if (!route.Value<bool>("isThrottling"))
{
int routePriority = route.Value<int>("priority");
if (routePriority < selectedPriority)
{
selectedPriority = routePriority;
availableRoutesIndexes.Clear();
availableRoutesIndexes.Add(i);
}
else if (routePriority == selectedPriority)
{
availableRoutesIndexes.Add(i);
}
}
}
if (availableRoutesIndexes.Count == 1)
{
return availableRoutesIndexes[0];
}
if (availableRoutesIndexes.Count > 0)
{
//Returns a random route from the list if we have more than one available with the same priority
return availableRoutesIndexes[new Random().Next(0, availableRoutesIndexes.Count)];
}
else
{
//If there are no available routes, the request will be sent to the first one
return 0;
}
}" />
<set-variable name="backendId" value="@(((JObject)((JArray)context.Variables["routes"])[(Int32)context.Variables["potentialRouteIndex"]]).Value<string>("backend-id"))" />
<set-variable name="routeLocation" value="@(((JObject)((JArray)context.Variables["routes"])[(Int32)context.Variables["potentialRouteIndex"]]).Value<string>("location"))" />
<set-variable name="routeName" value="@(((JObject)((JArray)context.Variables["routes"])[(Int32)context.Variables["potentialRouteIndex"]]).Value<string>("name"))" />
<set-variable name="deploymentName" value="@((string)context.Variables["deployment-id"])" />
<azure-openai-emit-token-metric namespace="ai-streaming">
<dimension name="SubscriptionId" value="@(context.Subscription.Id)" />
<dimension name="productName" value="@(context.Product?.Name?.ToString() ?? "Portal-Admin")" />
<dimension name="gatewayName" value="@(context.Deployment?.ServiceName ?? "NA")" />
<dimension name="routeName" value="@((string)context.Variables.GetValueOrDefault<string>("routeName", "Streaming"))" />
<dimension name="deploymentName" value="@((string)context.Variables.GetValueOrDefault<string>("deploymentName", "Streaming"))" />
<dimension name="operationName" value="@(context.Operation?.Id ?? "Streaming")" />
<dimension name="backendId" value="@((string)context.Variables.GetValueOrDefault<string>("backendId", "DefaultStreaming"))" />
<dimension name="routeLocation" value="@((string)context.Variables.GetValueOrDefault<string>("routeLocation", "DefaultStreaming"))" />
</azure-openai-emit-token-metric>
</when>
</choose>
</fragment>