in reference-implementations/semantic-search-for-images/src/ui-backend/Services/ImageService.cs [69:92]
private async Task<float []> GenerateImageEmbeddings(HttpContent requestContent, string embeddingType) {
// Obtain a token from the token credential before making the call to the Vision endpoint so that appropriate token refresh can take place, if necessary.
var tokenResult = await _tokenCredential.GetTokenAsync(new TokenRequestContext(["https://cognitiveservices.azure.com/"]), CancellationToken.None);
_aiServicesHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.Token);
using HttpResponseMessage response = await _aiServicesHttpClient.PostAsync(
$"{_appSettings.AiServices.Uri}/computervision/retrieval:{embeddingType}?api-version={_appSettings.AiServices.ApiVersion}&model-version={_appSettings.AiServices.ModelVersion}",
requestContent);
if(!response.IsSuccessStatusCode) {
if(requestContent is StreamContent streamContent)
throw new Exception($"Error generating embeddings for stream content. Status code: {response.StatusCode}");
else if(requestContent is StringContent stringContent)
throw new Exception($"Error generating embeddings for {await stringContent.ReadAsStringAsync()}. Status code: {response.StatusCode}; Reason: {response.ReasonPhrase}");
else
throw new Exception($"Error generating embeddings. Status code: {response.StatusCode}");
}
var embeddingsResponse = await response.Content.ReadFromJsonAsync<MultimodalEmbeddingsApiResponse>()
?? throw new Exception("Response not received or could not be serialized as expected.");
return embeddingsResponse.Vector;
}