如何获取LUIS Cognitive编程接口中的所有意图(默认仅返回100个)
Got it, I’ve run into this exact issue before when working with LUIS’s programmatic API! The default 100-intent limit is a common gotcha, but you can easily fetch all your intents using the API’s built-in pagination parameters. Here’s how to do it:
How it works
The LUIS Authoring API's "Get All Intents" endpoint supports two query parameters to handle large result sets:
skip: The number of intents to skip before returning results (starts at 0)top: The maximum number of intents to return per request (check the API docs for the upper limit—usually it's 500, which is way better than the default 100)
Step-by-step implementation
- Initialize a counter for
skipstarting at 0, and set atopvalue to the highest allowed by the API. - Make a request to the endpoint with your current
skipandtopvalues. - Add the returned intents to a master list.
- If the number of intents returned is less than your
topvalue, you’ve fetched all intents—stop the loop. Otherwise, incrementskipbytopand repeat the process.
Example code (Python)
Here’s a quick script to demonstrate this logic using the requests library:
import requests def fetch_all_luis_intents(app_id, auth_key, region): # Base URL for LUIS Authoring API (adjust version if needed) api_url = f"https://{region}.api.cognitive.microsoft.com/luis/authoring/v3.0-preview/apps/{app_id}/versions/0.1/intents" headers = {"Ocp-Apim-Subscription-Key": auth_key} all_intents = [] skip = 0 max_per_page = 500 # Use the maximum allowed by your API version while True: params = {"skip": skip, "top": max_per_page} response = requests.get(api_url, headers=headers, params=params) response.raise_for_status() # Throw an error if request fails current_batch = response.json() all_intents.extend(current_batch) # Exit loop if we got fewer results than the max per page if len(current_batch) < max_per_page: break skip += max_per_page return all_intents
Important notes
- API Version Check: Double-check the API version you’re using (v2 vs v3) as parameter names or endpoints might differ slightly.
- Rate Limits: LUIS enforces rate limits on authoring API calls. If you hit a 429 "Too Many Requests" error, add retry logic with exponential backoff to handle it.
- Permissions: Ensure your authentication key has authoring access to the LUIS app—you can’t fetch intents using a prediction key.
内容的提问来源于stack exchange,提问作者tumblewood
相关产品推荐
相关产品推荐

