Я понял, что ваш SDK изменился, модели устарели и были заменены новыми, и все же я не могу заставить его работать.
Вот код, который использует ваш новый API:
Когда я использую новую модель: gpt-4o, вызов CompleteChatAsync никогда не возвращает результат, если я не использую CancellationToken, в противном случае он отменяется после 30. сек.
То есть получаю исключение:
Изображение
Код: Выделить всё
public async Task GenerateResponseAsync(CompletionModel model, string prompt,
List contextList, CancellationToken cancellationToken)
{
_logger.LogInformation("GenerateResponseAsync entered");
ArgumentNullException.ThrowIfNull(model, nameof(model));
ArgumentNullException.ThrowIfNullOrWhiteSpace(prompt, nameof(prompt));
string respMessage = "";
try
{
Uri azureOpenAIResourceUri = new Uri(_azureSettings.AzureOpenAiEndpoint);
AzureKeyCredential azureOpenAiApiKey = new AzureKeyCredential(_azureSettings.AzureOpenAiApiKey);
AzureOpenAIClient azureClient = new(azureOpenAIResourceUri, azureOpenAiApiKey);
ChatClient chatClient = azureClient.GetChatClient(model.DeploymentName);
if (chatClient == null)
{
throw new Exception("Failed to create AzureOpenAIClient");
}
List? chatMessages = ConstructMessages(model, prompt, contextList);
if (chatMessages == null)
{
throw new Exception("Failed to construct messages");
}
_logger.LogInformation("Creating ChatCompletionOptions...");
ChatCompletionOptions? chatCompletionOptions = new ChatCompletionOptions()
{
FrequencyPenalty = 0f,
PresencePenalty = 0f,
MaxOutputTokenCount = model.TokenLimit - 1000,
TopP = 0,
Temperature = 1.0f,
};
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
_logger.LogInformation("Calling CompleteChatAsync...");
ChatCompletion chatCompletion = await chatClient.CompleteChatAsync(chatMessages.ToArray(), chatCompletionOptions, linkedTokenSource.Token);
if (chatCompletion != null)
{
ChatMessageContent msgContent = chatCompletion.Content;
if (msgContent != null)
{
respMessage = (msgContent.Count > 0) ? msgContent[0].Text : "";
}
}
_logger.LogInformation($"CONTENT: {respMessage}");
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
throw;
}
finally
{
_logger.LogInformation("GenerateResponse exiting");
}
return respMessage;
}
public List? ConstructMessages(CompletionModel model, string prompt, List contextList)
{
_logger.LogInformation("ConstructMessages entered");
try
{
StringBuilder sb = new StringBuilder($"QUESTION: {prompt}");
sb.AppendLine(Environment.NewLine);
sb.AppendLine("CONTEXT:");
sb.AppendLine(Environment.NewLine);
if (contextList != null)
{
for (int i = 0; i < contextList.Count; i++)
{
sb.AppendLine("## " + contextList[i].Trim('\n').Trim('\r'));
sb.AppendLine(Environment.NewLine);
}
}
List chatMessages = new List()
{
// The system message represents instructions or other guidance about how the assistant should behave
new SystemChatMessage(_azureSettings.AzureOpenAiCompletionSystemInstruction),
new UserChatMessage(sb.ToString()),
};
_logger.LogInformation($"SystemInstruction: {chatMessages[0]}");
_logger.LogInformation($"User Message: {chatMessages[1]}");
string totPayloadStr = _azureSettings.AzureOpenAiCompletionSystemInstruction + sb.ToString();
if (IsExceedsModelTokenLimit(totPayloadStr, model) == true)
{
throw new Exception("Total prompt + system + context message size exceeds the model limit");
}
_logger.LogInformation("Exiting exiting...");
return chatMessages;
}
catch(Exception ex)
{
_logger.LogError($"ConstructMessages exception: {ex.Message}");
}
return null;
}
Группа ресурсов и Ресурс находятся в «eastus»
Модель gpt-4o с именем развертывания/идентификатором модели «gpt-4o-2024-08-06» успешно развернута некоторое время назад.
имя развертывания модели: gpt-40-24-08-06
конечная точка целевого URI:
https://azureopenaiexperiment.openai.az ... 8-06/chat/ завершения?api-version=2024-08-01-preview
Итак, почему это не работает? Что мне не хватает?
Я получаю исключения: либо «Задача отменена», либо «Ресурс не найден».
Среда
Windows 11, Visual Studio 2022.NET 9
Microsoft.NETCore.APP 9.0.0
Microsoft.Windows.SDK.NET.Ref.Windows 10.0.26100.54
Подробнее здесь: https://stackoverflow.com/questions/793 ... uest-fails
Мобильная версия