Skip to content

Add DotNet 8.0 Support #443

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

Merged
merged 8 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 11 additions & 5 deletions OpenAI.Playground/OpenAI.Playground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net7.0;net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0</TargetFrameworks>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -25,24 +25,30 @@
<PackageReference Include="LaserCatEyes.HttpClientListener" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="LaserCatEyes.HttpClientListener" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net7.0'">
<ItemGroup Condition="'$(TargetFramework)' != 'net8.0' And '$(TargetFramework)' != 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions OpenAI.SDK/ObjectModels/Models.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;

#pragma warning disable CS1591
namespace OpenAI.ObjectModels;
Expand Down Expand Up @@ -430,4 +430,4 @@ public static string EnumToString(this Subject subject, string baseModel)
_ => throw new ArgumentOutOfRangeException(nameof(subject), subject, null)
}, baseModel);
}
}
}
13 changes: 8 additions & 5 deletions OpenAI.SDK/OpenAI.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>Latest</LangVersion>

<Copyright>Betalgo Up Ltd.</Copyright>
<PackageProjectUrl>https://openai.com/</PackageProjectUrl>
<PackageIcon>OpenAI-Betalgo.png</PackageIcon>
Expand Down Expand Up @@ -53,18 +52,22 @@
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net7.0'">
<ItemGroup Condition="'$(TargetFramework)' != 'net8.0' And '$(TargetFramework)' != 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>
</Project>
5 changes: 2 additions & 3 deletions OpenAI.Utilities.Tests/FunctionCallingHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void VerifyGetFunctionDefinition()
stringParameter.Description.ShouldBe("String Parameter");
stringParameter.Type.ShouldBe("string");

var enumValues = new List<string> {"Value1", "Value2", "Value3"};
var enumValues = new List<string> { "Value1", "Value2", "Value3" };

var enumParameter = functionDefinition.Parameters.Properties["enumParameter"];
enumParameter.Description.ShouldBe("Enum Parameter");
Expand Down Expand Up @@ -62,7 +62,6 @@ public void VerifyTypeOverride()
public void VerifyGetFunctionDefinitions()
{
var functionDefinitions = FunctionCallingHelper.GetFunctionDefinitions<FunctionCallingTestClass>();

functionDefinitions.Count.ShouldBe(3);

var functionDefinition = functionDefinitions.First(x => x.Name == "TestFunction");
Expand Down Expand Up @@ -275,4 +274,4 @@ public enum TestEnum
Value1,
Value2,
Value3
}
}
14 changes: 7 additions & 7 deletions OpenAI.Utilities.Tests/OpenAI.Utilities.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<LangVersion>Latest</LangVersion>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
18 changes: 11 additions & 7 deletions OpenAI.Utilities/Embedding/EmbeddingTools.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections;
#if NET6_0_OR_GREATER
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This utility will not run with .NetStandard 2.0 without more fixes so excluding it from utilities for now.


using System.Collections;
using System.Globalization;
using System.Text;
using CsvHelper;
Expand Down Expand Up @@ -109,7 +111,7 @@ public EmbeddingTools(IOpenAIService sdk, int maxToken, string embeddingModel)

public async Task<DataFrame> ReadFilesAndCreateEmbeddingDataAsCsv(string pathToDirectoryOrFile, string outputFileName)
{
return await ReadFilesAndCreateEmbeddingDataAsCsv(new[] {pathToDirectoryOrFile}, outputFileName);
return await ReadFilesAndCreateEmbeddingDataAsCsv(new[] { pathToDirectoryOrFile }, outputFileName);
}

public async Task<DataFrame> ReadFilesAndCreateEmbeddingDataAsCsv(IEnumerable<string> pathsToDirectoriesOrFiles, string outputFileName)
Expand Down Expand Up @@ -180,7 +182,7 @@ public string CreateContext(string question, DataFrame df, int maxLen = 1800)
}

// Else add it to the text that is being returned
returns.Add((string) row[textIndex]);
returns.Add((string)row[textIndex]);
}

// Return the context
Expand All @@ -200,7 +202,7 @@ public DataFrame LoadEmbeddedDataFromCsv(string path)
/// <returns></returns>
public IEnumerable<TextEmbeddingData> LoadFilesFromDirectory(string pathToDirectory)
{
return !Path.Exists(pathToDirectory)
return !File.Exists(pathToDirectory)
? new List<TextEmbeddingData>()
: Directory.EnumerateFiles(pathToDirectory).Select(LoadFile).Where(r => r != null).ToList()!;
}
Expand All @@ -214,7 +216,7 @@ public async Task WriteToTempCsv(IEnumerable<TextEmbeddingData> textEmbeddingDat
Directory.CreateDirectory(Path.GetDirectoryName(outputFilePath) ?? string.Empty);
await using var writer = new StreamWriter(outputFilePath);
await using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
await csv.WriteRecordsAsync((IEnumerable) textEmbeddingData);
await csv.WriteRecordsAsync((IEnumerable)textEmbeddingData);
await csv.DisposeAsync();
}

Expand Down Expand Up @@ -394,7 +396,7 @@ public PrimitiveDataFrameColumn<double> DistancesFromEmbeddings(List<double> qEm
var distances = new PrimitiveDataFrameColumn<double>(EmbedStaticValues.Distances, embeddingsColumn.Length);
for (var i = 0; i < embeddingsColumn.Length; i++)
{
var rowEmbeddings = embeddingsColumn[i].ToString()!.Split(",").Select(Convert.ToDouble);
var rowEmbeddings = embeddingsColumn[i].ToString()!.Split(',').Select(Convert.ToDouble);
distances[i] = Distance.Cosine(qEmbeddings.Select(x => x).ToArray(), Array.ConvertAll(rowEmbeddings.ToArray(), x => x));
}

Expand Down Expand Up @@ -439,4 +441,6 @@ public async Task<DataFrame> ReadAllDataInFolderAndCreateEmbeddingData(string pa
var files = LoadFilesFromDirectory(pathToDirectory);
return await PerformTextEmbedding(files, outputFileName);
}
}
}

#endif
12 changes: 3 additions & 9 deletions OpenAI.Utilities/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace OpenAI.Utilities;

public static partial class StringExtensions
{
private static readonly Regex NewlineToSpaceRegex = NewlineToSpace();
private static readonly Regex MultipleSpacesToSingleSpaceRegex = MultipleSpacesToSingleSpace();
private static readonly Regex NewlineToSpaceRegex = new Regex("\\r?\\n");
private static readonly Regex MultipleSpacesToSingleSpaceRegex = new Regex(" {2,}");

public static string RemoveNewlines(this string input)
{
Expand All @@ -18,10 +18,4 @@ public static string RemoveNewlines(this string input)
input = MultipleSpacesToSingleSpaceRegex.Replace(input, " ");
return input;
}

[GeneratedRegex("\\r?\\n")]
private static partial Regex NewlineToSpace();

[GeneratedRegex(" {2,}")]
private static partial Regex MultipleSpacesToSingleSpace();
}
}
12 changes: 6 additions & 6 deletions OpenAI.Utilities/FunctionCalling/FunctionCallingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public static FunctionDefinition GetFunctionDefinition(MethodInfo methodInfo)
case ({ } t, _) when t.IsAssignableFrom(typeof(string)):
definition = PropertyDefinition.DefineString(description);
break;
case ({IsEnum: true}, _):
case ({ IsEnum: true }, _):

var enumValues = string.IsNullOrEmpty(parameterDescriptionAttribute?.Enum)
? Enum.GetNames(parameter.ParameterType).ToList()
: parameterDescriptionAttribute.Enum.Split(",").Select(x => x.Trim()).ToList();
: parameterDescriptionAttribute.Enum.Split(',').Select(x => x.Trim()).ToList();

definition =
PropertyDefinition.DefineEnum(enumValues, description);
Expand Down Expand Up @@ -153,7 +153,7 @@ public static List<FunctionDefinition> GetFunctionDefinitions(Type type)
throw new InvalidFunctionCallException($"Method '{functionCall.Name}' on type '{obj.GetType()}' not found");
}

if (!methodInfo.ReturnType.IsAssignableTo(typeof(T)))
if (!typeof(T).IsAssignableFrom(methodInfo.ReturnType))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsAssignableTo not supported in .NetStandard 2.0 so reversed it to IsAssignableFrom for support.

{
throw new InvalidFunctionCallException(
$"Method '{functionCall.Name}' on type '{obj.GetType()}' has return type '{methodInfo.ReturnType}' but expected '{typeof(T)}'");
Expand All @@ -176,12 +176,12 @@ public static List<FunctionDefinition> GetFunctionDefinitions(Type type)
throw new Exception($"Argument '{name}' not found");
}

var value = parameter.ParameterType.IsEnum ? Enum.Parse(parameter.ParameterType, argument.Value.ToString()!) : ((JsonElement) argument.Value).Deserialize(parameter.ParameterType);
var value = parameter.ParameterType.IsEnum ? Enum.Parse(parameter.ParameterType, argument.Value.ToString()!) : ((JsonElement)argument.Value).Deserialize(parameter.ParameterType);

args.Add(value);
}

var result = (T?) methodInfo.Invoke(obj, args.ToArray());
var result = (T?)methodInfo.Invoke(obj, args.ToArray());
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ public FunctionDescriptionAttribute(string? description = null)
/// Description of the function
/// </summary>
public string? Description { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public class InvalidFunctionCallException : Exception
public InvalidFunctionCallException(string message) : base(message)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public ParameterDescriptionAttribute(string? description = null)
/// Whether the parameter is required. If not provided, the parameter will be required. Default is true
/// </summary>
public bool Required { get; set; } = true;
}
}
10 changes: 6 additions & 4 deletions OpenAI.Utilities/OpenAI.Utilities.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>Latest</LangVersion>

<Copyright>Betalgo Up Ltd.</Copyright>
<PackageProjectUrl>https://openai.com/</PackageProjectUrl>
<PackageIcon>OpenAI-Betalgo.png</PackageIcon>
Expand Down Expand Up @@ -41,9 +40,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Betalgo.OpenAI" Version="7.2.0" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Microsoft.Data.Analysis" Version="0.20.1" />
<PackageReference Include="Microsoft.Data.Analysis" Version="0.21.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenAI.SDK\OpenAI.csproj" />
</ItemGroup>
</Project>
17 changes: 15 additions & 2 deletions OpenAI.UtilitiesPlayground/OpenAI.UtilitiesPlayground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net8.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>acb2421b-1517-4212-93a4-e4eb182b4626</UserSecretsId>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
<PackageReference Include="LaserCatEyes.HttpClientListener" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="7.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static async Task ExerciseFunctionCalling(IOpenAIService openAIService)
{
//Functions = FunctionCallingHelper.GetFunctionDefinitions(calculator),
//Functions = FunctionCallingHelper.GetFunctionDefinitions(typeof(Calculator)),
Functions = FunctionCallingHelper.GetFunctionDefinitions<Calculator>(),
//Functions = FunctionCallingHelper.GetFunctionDefinitions<Calculator>(),
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem("You are a helpful assistant."),
Expand Down Expand Up @@ -88,4 +88,4 @@ public float AdvancedMath(float a, float b, AdvancedOperators advancedOperator)
};
}
}
}
}