microsoft / semantic-kernel-starters

Starter Projects for Semantic Kernel
MIT License
330 stars 117 forks source link

MethodNotFound Exception when executing IChatCompletion.GetChatCompletionsAsync() #88

Open heftif opened 9 months ago

heftif commented 9 months ago

Describe the bug I'm getting a "MethodNotFound" Exception when executing IChatCompletion.GetChatCompletionsAsync(). The Error: Method not found: 'System.Threading.Tasks.Task1<Azure.Response1> Azure.AI.OpenAI.OpenAIClient.GetChatCompletionsAsync(System.String, Azure.AI.OpenAI.ChatCompletionsOptions, System.Threading.CancellationToken)'.

To Reproduce Steps to reproduce the behavior: Starting a Blazor Project with the following setup. I put a test for AzureOpenAI chat access in (which works without issues) and below I'm trying the same with the semantic kernel approach. I just put them in the Program.cs file to keep the example short.

using GaGSemanticMap.Components;
using DotNetEnv;
using Azure.AI.OpenAI;
using Azure;
using GaGSemanticMap.Services;
using GaGSemanticMap.Skills;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;

var builder = WebApplication.CreateBuilder(args);

Env.Load();

string key = Environment.GetEnvironmentVariable("KEY");
string endPoint = Environment.GetEnvironmentVariable("ENDPOINT");
string model = Environment.GetEnvironmentVariable("MODEL");
string embeddingModel = Environment.GetEnvironmentVariable("EMBEDDING");

//todo: implement a logger

//add configs
/*builder.Configuration.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", true, true)
    .AddEnvironmentVariables()
    .AddUserSecrets<Program>(); */

// register services
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddLogging();
builder.Logging.SetMinimumLevel(LogLevel.Warning);

//test for azure openai client
var client = new OpenAIClient(new Uri(endPoint!), new AzureKeyCredential(key));

var chatCompletionsOptions = new ChatCompletionsOptions()
{
    Messages =
            {
                //new ChatMessage(ChatRole.System, "You are an unhelpful assistant, getting sassy when you have to answer a question"),
                new Azure.AI.OpenAI.ChatMessage(ChatRole.System, "You speak like Yoda and give wise advice"),
                new Azure.AI.OpenAI.ChatMessage(ChatRole.User, "this is the way!")
            },
    MaxTokens = 400,
    DeploymentName = model

};
Response<ChatCompletions> response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
var botResponse = response.Value.Choices.First().Message.Content;

//initiate kernel
var kernelBuilder = new KernelBuilder();
kernelBuilder.WithAzureOpenAIChatCompletionService(model, client);
//kernelBuilder.WithAzureOpenAITextEmbeddingGenerationService(embeddingModel, endPoint!, key);
IKernel kernel = kernelBuilder.Build();
builder.Services.AddSingleton(kernel);

//testing the kernel
var chatRequestSettings = new OpenAIRequestSettings()
{
    MaxTokens = 500,
    Temperature = 0.0f,
    FrequencyPenalty = 0.0f,
    PresencePenalty = 0.0f,
    TopP = 0.0f
};
var chatCompletion = kernel.GetService<IChatCompletion>();

var prompt = "You are a helpful AI";

var chatHistory = chatCompletion.CreateNewChat(prompt);

chatHistory.AddUserMessage("Hey how are you?");

//this line throws the error
var result = chatCompletion.GetChatCompletionsAsync(chatHistory, chatRequestSettings).GetAwaiter().GetResult();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

app.Run();

Expected behavior Return of the message of the configured chat.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

ProjectInfo

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.9" />
    <PackageReference Include="DotNetEnv" Version="2.5.0" />
    <PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta8" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="Pgvector" Version="0.2.0-rc.2" />
    <PackageReference Include="Pgvector.EntityFrameworkCore" Version="0.2.0-rc.1" />
  </ItemGroup>

</Project>
heftif commented 9 months ago

The issue is that Azure.AI.OpenAI, Version 1.0.0-beta9 does not provide the needed function anymore. Working with version 1.0.0-beta.8