Query Details

APIM AI Gateway - Model and tool inventory from request bodies

APIM Model Tool Inventory

Query

AppRequests
| where SDKVersion startswith "apim:" or tostring(Properties["Service Type"]) =~ "API Management"
| extend RequestBody=tostring(Properties["Request-Body"])
| where isnotempty(RequestBody)
| extend RequestJson=parse_json(RequestBody)
| extend Model=tostring(RequestJson.model), Tools=tostring(RequestJson.tools), ToolChoice=tostring(RequestJson.tool_choice), Stream=tostring(RequestJson.stream), APIName=tostring(Properties["API Name"])
| summarize Requests=count(), Successful=countif(Success==true), FirstSeen=min(TimeGenerated), LastSeen=max(TimeGenerated), SampleRequestBody=take_any(RequestBody) by APIName, Model, ToolChoice, Stream, Tools
| order by Requests desc

Explanation

This query is designed to analyze and summarize data from application requests related to API Management (APIM). Here's a simple breakdown of what it does:

  1. Data Source: It starts by filtering records from AppRequests where the SDK version begins with "apim:" or the service type is "API Management". This ensures that only relevant API Management requests are considered.

  2. Extract Request Body: It extracts the "Request-Body" from the properties of each request and ensures that this body is not empty.

  3. Parse JSON: The request body is parsed as JSON to extract specific fields: model, tools, tool_choice, and stream. It also captures the API name from the request properties.

  4. Summarize Data: The query then summarizes the data by counting the total number of requests, the number of successful requests, and noting the first and last time each combination of API name, model, tool choice, stream, and tools was seen. It also provides a sample request body for each combination.

  5. Order Results: Finally, the results are ordered by the number of requests in descending order, highlighting the most frequently seen combinations.

Overall, this query inventories models, tool settings, and other configurations seen in API Management requests, providing insights into their usage patterns.