c# Azure.AI.OpenAI 在启动时出现错误

问题描述 投票:0回答:1

我正在尝试遵循 Microsoft 的操作方法(以 C# 方式):
https://learn.microsoft.com/en-us/azure/ai-services/openai/use-your-data-quickstart?tabs=command-line%2Cpython&pivots=programming-language-csharp

按照说明使用模板创建代码:

dotnet new console -n azure-openai-quickstart

添加请求的包后:

dotnet add package Azure.AI.OpenAI --prerelease

它添加了包
Azure.AI.OpenAI 1.0.0-beta.12

使用 Visual Studio 打开代码,出现几个错误。

ChatMessage
开始,然后是
SearchKey
类中的
AzureCognitiveSearchChatExtensionConfiguration
属性:

您知道如何解决这个问题并遵循操作方法吗?

为什么微软留下了一个不正确且不实用的操作指南来“帮助”人们开始使用一项技术?

这是评论中要求的

.csproj
文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <RootNamespace>azure_openai_quickstart</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.12" />
  </ItemGroup>

</Project>
c# azure azure-openai
1个回答
0
投票

问题出在

1.0.0-beta.12
Nuget 包的
Azure.AI.OpenAI
版本上。该代码与版本
1.0.0-beta.8
兼容。有两种可能的解决方案:

  1. 降级到版本
    1.0.0-beta.8
    。您需要卸载当前版本并使用
    1.0.0-beta.8
    安装
    dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.8
    。虽然我不建议这样做。
  2. 更改代码,使其与版本兼容
    1.0.0-beta.12

我对代码做了一些更改,以便至少可以编译(但尚未尝试运行它)。这是编译的代码:

using Azure;
using Azure.AI.OpenAI;
using System.Text.Json;
using static System.Environment;

string azureOpenAIEndpoint = GetEnvironmentVariable("AOAIEndpoint");
string azureOpenAIKey = GetEnvironmentVariable("AOAIKey");
string searchEndpoint = GetEnvironmentVariable("SearchEndpoint");
string searchKey = GetEnvironmentVariable("SearchKey");
string searchIndex = GetEnvironmentVariable("SearchIndex");
string deploymentName = GetEnvironmentVariable("AOAIDeploymentId");

var client = new OpenAIClient(new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey));

var chatCompletionsOptions = new ChatCompletionsOptions()
{
    Messages =
    {
        new ChatRequestUserMessage("What are the differences between Azure Machine Learning and Azure AI services?"),
    },
    AzureExtensionsOptions = new AzureChatExtensionsOptions()
    {
        Extensions =
        {
            new AzureCognitiveSearchChatExtensionConfiguration()
            {
                SearchEndpoint = new Uri(searchEndpoint),
                Key = searchKey,
                IndexName = searchIndex,
            },
        }
    },
    DeploymentName = deploymentName
};

Response<ChatCompletions> response = client.GetChatCompletions(chatCompletionsOptions);

ChatResponseMessage responseMessage = response.Value.Choices[0].Message;

Console.WriteLine($"Message from {responseMessage.Role}:");
Console.WriteLine("===");
Console.WriteLine(responseMessage.Content);
Console.WriteLine("===");

Console.WriteLine($"Context information (e.g. citations) from chat extensions:");
Console.WriteLine("===");
foreach (ChatResponseMessage contextMessage in responseMessage.AzureExtensionsContext.Messages)
{
    string contextContent = contextMessage.Content;
    try
    {
        var contextMessageJson = JsonDocument.Parse(contextMessage.Content);
        contextContent = JsonSerializer.Serialize(contextMessageJson, new JsonSerializerOptions()
        {
            WriteIndented = true,
        });
    }
    catch (JsonException)
    {}
    Console.WriteLine($"{contextMessage.Role}: {contextContent}");
}
Console.WriteLine("===");
© www.soinside.com 2019 - 2024. All rights reserved.