We attempted to implement Knowledge Agents using the official documentation provided here: 🔗 https://learn.microsoft.com/en-us/azure/search/search-agentic-retrieval-how-to-create
We are currently facing issues with the Retrieve API. Below is the step-by-step implementation and the problems encountered.
Step 1: Create Knowledge Agent
$searchUrl = 'test.search.windows.net';
$agentName = 'agentName';
$indexName = 'indexName';
$modelProviderUrl = 'https://test.openai.azure.com';
$searchApiKey = '*********';
$modelApiKey = '************';
$endpoint = "https://{$searchUrl}/agents/{$agentName}?api-version=2025-05-01-preview";
$data = [
'name' => $agentName,
'targetIndexes' => [[
'indexName' => $indexName,
'defaultRerankerThreshold' => 2.5,
'defaultIncludeReferenceSourceData' => true,
'defaultMaxDocsForReranker' => 200
]],
'models' => [[
'kind' => 'azureOpenAI',
'azureOpenAIParameters' => [
'resourceUri' => $modelProviderUrl,
'apiKey' => $modelApiKey,
'deploymentId' => 'text-embedding-3-large',
'modelName' => 'gpt-4o'
]
]],
'requestLimits' => [
'maxOutputSize' => 5000,
'maxRuntimeInSeconds' => 60
],
'encryptionKey' => null
];
$ch = curl_init($endpoint);
$headers = [
'Content-Type: application/json',
'api-key: ' . $searchApiKey
];
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch) . "\n";
} else {
echo "HTTP Status Code: " . $httpCode . "\n";
echo "Response: " . $response . "\n";
}
curl_close($ch);
Step 2: Retrieve API Implementation
$searchUrl = 'test.search.windows.net';
$agentName = 'agentName';
$indexName = 'indexName';
$searchApiKey = '***********************';
$endpoint = "https://{$searchUrl}/agents/{$agentName}/retrieve?api-version=2025-05-01-preview";
$data = [
'messages' => [
[
'role' => 'assistant',
'content' => [[
'type' => 'text',
'text' => "You are a helpful assistant for Contoso Human Resources. You have access to a search index containing guidelines about health care coverage for Washington state. If you can't find the answer in the search, say you don't know."
]]
],
[
'role' => 'user',
'content' => [[
'type' => 'text',
'text' => 'What are my vision benefits?'
]]
]
],
'targetIndexParams' => [[
'indexName' => $indexName,
'filterAddOn' => "State eq 'WA'",
'IncludeReferenceSourceData' => true,
'rerankerThreshold' => 2.5,
'maxDocsForReranker' => 250
]]
];
$ch = curl_init($endpoint);
$headers = [
'Content-Type: application/json',
// Note: We tried both of the following in different tests
// 'api-key: ' . $searchApiKey
'Authorization: Bearer ' . $searchApiKey
];
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch) . "\n";
} else {
echo "HTTP Status Code: " . $httpCode . "\n";
echo "<pre>";
print_r(json_decode($response));
echo "</pre>";
}
curl_close($ch);
Step 3: Generate Access Token
$tenant = '*************';
$client = '*************';
$secret = '*************';
$url = "https://login.microsoftonline.com/$tenant/oauth2/v2.0/token";
$data = http_build_query([
'grant_type' => 'client_credentials',
'client_id' => $client,
'client_secret' => $secret,
'scope' => 'https://search.azure.com/.default'
]);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
CURLOPT_POSTFIELDS => $data
]);
$resp = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r(json_decode($resp)->access_token);
echo "</pre>";
Issues Encountered with Retrieve API
- Using Access Token:
- Status Code: 403 Forbidden
- Issue: No response returned.
- Using Search API Key as Bearer Token:
- Status Code: 401 Unauthorized
- Issue: No response returned.
- Using Search API Key with api-key Header:
- Status Code: 404 Not Found
- Response:
Could not complete model action. The model endpoint returned status code '404' (NotFound). Resource not found