Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better support for using OpenAI subscriptions #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,26 @@ To better understand skUnit, Check these documents:

## Requirements
- .NET 7.0 or higher
- An OpenAI API key
- An OpenAI API key or an AzureOpenAI API key

## Installation
You can easily add **skUnit** to your project as it is available as a [NuGet](https://www.nuget.org/packages/skUnit) package. To install it, execute the following command in your terminal:
```bash
dotnet add package skUnit
```

Afterwards, you'll need to instantiate the `SemanticKernelAssert` class in your test constructor. This requires passing your OpenAI subscription details as parameters:
Afterwards, you'll need to instantiate the `SemanticKernelAssert` class in your test constructor. This requires passing your OpenAI or AzureOpenAI subscription details as parameters:
```csharp
public class MyTest
{
SemanticKernelAssert SemanticKernelAssert { get; set; }
MyTest(ITestOutputHelper output)
{
// For AzureOpenAI
SemanticKernelAssert = new SemanticKernelAssert(_deploymentName, _endpoint, _apiKey, message => output.WriteLine(message));

// For OpenAI
SemanticKernelAssert = new SemanticKernelAssert(_apiKey, message => output.WriteLine(message));
}

[Fact]
Expand Down
27 changes: 26 additions & 1 deletion src/skUnit.Tests/SemanticAssertTests/SemanticAssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ public SemanticAssertTests(ITestOutputHelper output)
SemanticAssert = new SemanticAssert(deploymentName, endpoint, apiKey);
}

public SemanticAssertTests(ITestOutputHelper output, bool useOpenAI)
{
Output = output;

var apiKey =
Environment.GetEnvironmentVariable("openai-api-key", EnvironmentVariableTarget.User) ??
throw new Exception("No ApiKey in environment variables.");

if (useOpenAI)
{
SemanticAssert = new SemanticAssert(apiKey);
}
else
{
var endpoint =
Environment.GetEnvironmentVariable("openai-endpoint", EnvironmentVariableTarget.User) ??
throw new Exception("No Endpoint in environment variables.");
var deploymentName =
Environment.GetEnvironmentVariable("openai-deployment-name", EnvironmentVariableTarget.User) ??
throw new Exception("No DeploymentName in environment variables.");

SemanticAssert = new SemanticAssert(deploymentName, endpoint, apiKey);
}
}

[Theory]
[MemberData(nameof(GetSimilarData))]
public void Similar_True_MustWork(string first, string second)
Expand Down Expand Up @@ -89,4 +114,4 @@ public static IEnumerable<object[]> GetSimilarData()
};
}
}
}
}
12 changes: 12 additions & 0 deletions src/skUnit/Asserts/SemanticAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@

}

/// <summary>
/// This class needs a SemanticKernel kernel to work.
/// Using this constructor you can use an OpenAI subscription to configure it.
/// </summary>
/// <param name="apiKey"></param>
public SemanticAssert(string apiKey)
{
var kernel = Kernel.Builder.Build();

Check failure on line 51 in src/skUnit/Asserts/SemanticAssert.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Builder'

Check failure on line 51 in src/skUnit/Asserts/SemanticAssert.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Builder'
kernel.Config.AddOpenAITextCompletionService("text-davinci-003", apiKey);

Check failure on line 52 in src/skUnit/Asserts/SemanticAssert.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Config' and no accessible extension method 'Config' accepting a first argument of type 'Kernel' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 52 in src/skUnit/Asserts/SemanticAssert.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Config' and no accessible extension method 'Config' accepting a first argument of type 'Kernel' could be found (are you missing a using directive or an assembly reference?)
Semantic = new Semantic(kernel);
}

/// <summary>
/// Checks whether <paramref name="first"/> and <paramref name="second"/> string are semantically similar.
/// It uses the kernel and OpenAI to check this semantically.
Expand Down
23 changes: 22 additions & 1 deletion src/skUnit/Asserts/SemanticKernelAssert_Initialize.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -55,11 +55,32 @@

}

/// <summary>
/// This class needs a SemanticKernel kernel to work.
/// Using this constructor you can use an OpenAI subscription to configure it.
/// </summary>
/// <param name="apiKey"></param>
/// <param name="onLog">If you're using xUnit, do this in the constructor:
/// <code>
/// MyTest(ITestOutputHelper output)
/// {
/// SemanticKernelAssert = new SemanticKernelAssert(_apiKey, output.WriteLine);
/// }
/// </code>
/// </param>
public SemanticKernelAssert(string apiKey, Action<string> onLog)
{
var kernel = Kernel.Builder.Build();

Check failure on line 73 in src/skUnit/Asserts/SemanticKernelAssert_Initialize.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Builder'

Check failure on line 73 in src/skUnit/Asserts/SemanticKernelAssert_Initialize.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Builder'
kernel.Config.AddOpenAITextCompletionService("text-davinci-003", apiKey);

Check failure on line 74 in src/skUnit/Asserts/SemanticKernelAssert_Initialize.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Config' and no accessible extension method 'Config' accepting a first argument of type 'Kernel' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 74 in src/skUnit/Asserts/SemanticKernelAssert_Initialize.cs

View workflow job for this annotation

GitHub Actions / build

'Kernel' does not contain a definition for 'Config' and no accessible extension method 'Config' accepting a first argument of type 'Kernel' could be found (are you missing a using directive or an assembly reference?)
Semantic = new Semantic(kernel);
OnLog = onLog;
}

private void Log(string? message = "")
{
if (OnLog is not null)
{
OnLog(message);

Check warning on line 83 in src/skUnit/Asserts/SemanticKernelAssert_Initialize.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'obj' in 'void Action<string>.Invoke(string obj)'.
}
}

Expand Down
Loading