From 3e571e968447c87b134a76a6debf6c8c61136dd1 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Thu, 16 Mar 2023 19:55:56 -0400 Subject: [PATCH] OpenAI-DotNet 6.3.0 (#48) - Removed `OpenAI-DotNet-Proxy` and put it directly in package on its own --- .github/actions/build-and-publish/action.yaml | 41 --------- .github/workflows/Publish-Nuget.yml | 70 +++++++--------- Directory.Build.props | 5 -- .../OpenAI-DotNet-Proxy.csproj | 41 --------- OpenAI-DotNet-Proxy/Readme.md | 84 ------------------- .../OpenAI-DotNet-Tests-Proxy.csproj | 1 - OpenAI-DotNet.sln | 6 -- OpenAI-DotNet/OpenAI-DotNet.csproj | 7 +- .../Proxy/AbstractAuthenticationFilter.cs | 0 .../Proxy/IAuthenticationFilter.cs | 0 .../Proxy/OpenAIProxyStartup.cs | 0 README.md | 15 ++-- 12 files changed, 42 insertions(+), 228 deletions(-) delete mode 100644 .github/actions/build-and-publish/action.yaml delete mode 100644 Directory.Build.props delete mode 100644 OpenAI-DotNet-Proxy/OpenAI-DotNet-Proxy.csproj delete mode 100644 OpenAI-DotNet-Proxy/Readme.md rename {OpenAI-DotNet-Proxy => OpenAI-DotNet}/Proxy/AbstractAuthenticationFilter.cs (100%) rename {OpenAI-DotNet-Proxy => OpenAI-DotNet}/Proxy/IAuthenticationFilter.cs (100%) rename {OpenAI-DotNet-Proxy => OpenAI-DotNet}/Proxy/OpenAIProxyStartup.cs (100%) diff --git a/.github/actions/build-and-publish/action.yaml b/.github/actions/build-and-publish/action.yaml deleted file mode 100644 index 1948a375..00000000 --- a/.github/actions/build-and-publish/action.yaml +++ /dev/null @@ -1,41 +0,0 @@ -# .github/actions/build-and-publish/action.yml -name: "Build and Publish NuGet Package" -description: "Build and publish the specified NuGet package" -inputs: - project_name: - description: "The name of the project" - required: true - nuget_api_key: - description: "NuGet API Key" - required: true -runs: - using: "composite" - steps: - - name: Build Pack and Publish NuGet Package - run: | - $projectPath = "${{ github.workspace }}\${{ inputs.project_name }}" - dotnet build $projectPath --configuration Release - $out = "$projectPath\bin\Release" - $packagePath = Get-ChildItem -Path $out -File -Include '*.nupkg' -Recurse -ErrorAction SilentlyContinue - - if ($packagePath) { - Write-Host Package path: $packagePath - } else { - Write-Host Failed to find package at $out - exit 1 - } - - $isRelease = "${{ github.ref == 'refs/heads/main' }}" - - if ($isRelease -eq 'true') { - dotnet nuget push $packagePath --api-key ${{ inputs.nuget_api_key }} --source https://api.nuget.org/v3/index.json - } - - $version = $packagePath.Name -replace "^${{ inputs.project_name }}.(.*).nupkg$",'$1' - echo "PACKAGE_VERSION=$version" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - shell: pwsh - - - uses: actions/upload-artifact@v3 - with: - name: ${{ inputs.project_name }}.${{ env.PACKAGE_VERSION }} - path: ${{ github.workspace }}/${{ inputs.project_name }}/bin/Release/${{ inputs.project_name }}.${{ env.PACKAGE_VERSION }}.nupkg diff --git a/.github/workflows/Publish-Nuget.yml b/.github/workflows/Publish-Nuget.yml index a9975eac..761fd493 100644 --- a/.github/workflows/Publish-Nuget.yml +++ b/.github/workflows/Publish-Nuget.yml @@ -4,9 +4,13 @@ on: push: branches: - main + paths: + - OpenAI-DotNet/** pull_request: branches: - '*' + paths: + - OpenAI-DotNet/** workflow_dispatch: inputs: @@ -19,17 +23,17 @@ env: DOTNET_VERSION: ${{ github.event.inputs.dotnet-version || '6.0.x' }} jobs: - test: + build: runs-on: windows-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - - uses: actions/setup-dotnet@v3 with: dotnet-version: ${{ env.DOTNET_VERSION }} + - uses: microsoft/setup-msbuild@v1 - name: Test Packages run: dotnet test --configuration Release @@ -37,45 +41,33 @@ jobs: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_ORGANIZATION_ID: ${{ secrets.OPENAI_ORGANIZATION_ID }} - - name: No tests needed - run: echo "No tests needed as no files in the specified paths were modified." + - name: Build Pack and Publish NuGet Package + run: | + $projectPath = "${{ github.workspace }}\OpenAI-DotNet" + dotnet build $projectPath --configuration Release + $out = "$projectPath\bin\Release" + $packagePath = Get-ChildItem -Path $out -File -Include '*.nupkg' -Recurse -ErrorAction SilentlyContinue - build-openai-dotnet: - needs: test - runs-on: windows-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 + if ($packagePath) { + Write-Host Package path: $packagePath + } else { + Write-Host Failed to find package at $out + exit 1 + } - - uses: actions/setup-dotnet@v3 - with: - dotnet-version: ${{ env.DOTNET_VERSION }} + $isRelease = "${{ github.ref == 'refs/heads/main' }}" - - uses: microsoft/setup-msbuild@v1 + if ($isRelease -eq 'true') { + dotnet nuget push $packagePath --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate + } - - name: Build and Publish OpenAI-DotNet NuGet Package - uses: ./.github/actions/build-and-publish - with: - project_name: OpenAI-DotNet - nuget_api_key: ${{ secrets.NUGET_API_KEY }} + $version = $packagePath.Name -replace "^OpenAI-DotNet.(.*).nupkg$",'$1' + echo "PACKAGE_VERSION=$version" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + shell: pwsh - build-openai-dotnet-proxy: - needs: test - runs-on: windows-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - - uses: actions/setup-dotnet@v3 - with: - dotnet-version: ${{ env.DOTNET_VERSION }} - - - uses: microsoft/setup-msbuild@v1 - - - name: Build and Publish OpenAI-DotNet-Proxy NuGet Package - uses: ./.github/actions/build-and-publish - with: - project_name: OpenAI-DotNet-Proxy - nuget_api_key: ${{ secrets.NUGET_API_KEY }} + - uses: actions/upload-artifact@v3 + if: always() + with: + name: OpenAI-DotNet.${{ env.PACKAGE_VERSION }} + path: ${{ github.workspace }}/OpenAI-DotNet/bin/Release/OpenAI-DotNet.${{ env.PACKAGE_VERSION }}.nupkg + if-no-files-found: ignore diff --git a/Directory.Build.props b/Directory.Build.props deleted file mode 100644 index 0bb05894..00000000 --- a/Directory.Build.props +++ /dev/null @@ -1,5 +0,0 @@ - - - 6.2.0 - - diff --git a/OpenAI-DotNet-Proxy/OpenAI-DotNet-Proxy.csproj b/OpenAI-DotNet-Proxy/OpenAI-DotNet-Proxy.csproj deleted file mode 100644 index 5f3ed708..00000000 --- a/OpenAI-DotNet-Proxy/OpenAI-DotNet-Proxy.csproj +++ /dev/null @@ -1,41 +0,0 @@ - - - net6.0 - true - false - disable - false - Stephen Hodgson - OpenAI-DotNet-Proxy - A simple Proxy API gateway for OpenAI-DotNet to make authenticated requests from a front end application without exposing your API keys. - 2023 - CC0-1.0 - https://github.com/RageAgainstThePixel/OpenAI-DotNet - https://github.com/RageAgainstThePixel/OpenAI-DotNet - OpenAI, AI, ML, API, gpt, gpt-4, gpt-3.5-turbo, gpt-3, chatGPT, api-proxy, proxy, gateway - OpenAI API Proxy - OpenAI-DotNet-Proxy - $(OpenAIDotNetVersion) - RageAgainstThePixel - OpenAI.Proxy - OpenAI-DotNet-Icon.png - Initial Release! - - - True - - - True - - - - - - - - True - \ - - - - diff --git a/OpenAI-DotNet-Proxy/Readme.md b/OpenAI-DotNet-Proxy/Readme.md deleted file mode 100644 index 5ecc3402..00000000 --- a/OpenAI-DotNet-Proxy/Readme.md +++ /dev/null @@ -1,84 +0,0 @@ - -# OpenAI-DotNet-Proxy - -[![NuGet version (OpenAI-DotNet-Proxy)](https://img.shields.io/nuget/v/OpenAI-DotNet-Proxy.svg?label=OpenAI-DotNet-Proxy&logo=nuget)](https://www.nuget.org/packages/OpenAI-DotNet-Proxy/) - -A simple Proxy API gateway for [OpenAI-DotNet](https://github.com/RageAgainstThePixel/OpenAI-DotNet) to make authenticated requests from a front end application without exposing your API keys. - -## Getting started - -### Install from NuGet - -Install package [`OpenAI-DotNet-Proxy` from Nuget](https://www.nuget.org/packages/OpenAI-DotNet-Proxy/). Here's how via command line: - -```powershell -Install-Package OpenAI-DotNet-Proxy -``` - -## Documentation - -Using either the [OpenAI-DotNet](https://github.com/RageAgainstThePixel/OpenAI-DotNet) or [com.openai.unity](https://github.com/RageAgainstThePixel/com.openai.unity) packages directly in your front-end app may expose your API keys and other sensitive information. To mitigate this risk, it is recommended to set up an intermediate API that makes requests to OpenAI on behalf of your front-end app. This library can be utilized for both front-end and intermediary host configurations, ensuring secure communication with the OpenAI API. - -### Font End Example - -In the front end example, you will need to securely authenticate your users using your preferred OAuth provider. Once the user is authenticated, exchange your custom auth token with your API key on the backend. - -Follow these steps: - -1. Setup a new project using either the [OpenAI-DotNet](https://github.com/RageAgainstThePixel/OpenAI-DotNet) or [com.openai.unity](https://github.com/RageAgainstThePixel/com.openai.unity) packages. -2. Authenticate users with your OAuth provider. -3. After successful authentication, create a new `OpenAIAuthentication` object and pass in the custom token with the prefix `sess-`. -4. Create a new `OpenAIClientSettings` object and specify the domain where your intermediate API is located. -5. Pass your new `auth` and `settings` objects to the `OpenAIClient` constructor when you create the client instance. - -Here's an example of how to set up the front end: - -```csharp -var authToken = await LoginAsync(); -var auth = new OpenAIAuthentication($"sess-{authToken}"); -var settings = new OpenAIClientSettings(domain: "api.your-custom-domain.com"); -var api = new OpenAIClient(auth, settings); -``` - -This setup allows your front end application to securely communicate with your backend that will be using the OpenAI-DotNet-Proxy, which then forwards requests to the OpenAI API. This ensures that your OpenAI API keys and other sensitive information remain secure throughout the process. - -### Back End Example - -In this example, we demonstrate how to set up and use `OpenAIProxyStartup` in a new ASP.NET Core web app. The proxy server will handle authentication and forward requests to the OpenAI API, ensuring that your API keys and other sensitive information remain secure. - -1. Create a new [ASP.NET Core minimal web API](https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0) project. -2. Add the OpenAI-DotNet-Proxy nuget package to your project. - a. Manually editing .csproj: `` - b. Powershell install: `Install-Package OpenAI-DotNet-Proxy` -3. Create a new class that inherits from `AbstractAuthenticationFilter` and override the `ValidateAuthentication` method. This will implement the `IAuthenticationFilter` that you will use to check user session token against your internal server. -4. In `Program.cs`, create a new proxy web application by calling `OpenAIProxyStartup.CreateDefaultHost` method, passing your custom `AuthenticationFilter` as a type argument. -5. Create `OpenAIAuthentication` and `OpenAIClientSettings` as you would normally with your API keys, org id, or Azure settings. - -```csharp -public partial class Program -{ - private class AuthenticationFilter : AbstractAuthenticationFilter - { - public override void ValidateAuthentication(IHeaderDictionary request) - { - // You will need to implement your own class to properly test - // custom issued tokens you've setup for your end users. - if (!request.Authorization.ToString().Contains(userToken)) - { - throw new AuthenticationException("User is not authorized"); - } - } - } - - public static void Main(string[] args) - { - var auth = OpenAIAuthentication.LoadFromEnv(); - var settings = new OpenAIClientSettings(/* your custom settings if using Azure OpenAI */); - var openAIClient = new OpenAIClient(auth, settings); - var proxy = OpenAIProxyStartup.CreateDefaultHost(args, openAIClient); - proxy.Run(); - } -} -``` - -Once you have set up your proxy server, your end users can now make authenticated requests to your proxy api instead of directly to the OpenAI API. The proxy server will handle authentication and forward requests to the OpenAI API, ensuring that your API keys and other sensitive information remain secure. diff --git a/OpenAI-DotNet-Tests-Proxy/OpenAI-DotNet-Tests-Proxy.csproj b/OpenAI-DotNet-Tests-Proxy/OpenAI-DotNet-Tests-Proxy.csproj index 82c86dc5..8c7ad825 100644 --- a/OpenAI-DotNet-Tests-Proxy/OpenAI-DotNet-Tests-Proxy.csproj +++ b/OpenAI-DotNet-Tests-Proxy/OpenAI-DotNet-Tests-Proxy.csproj @@ -11,7 +11,6 @@ - diff --git a/OpenAI-DotNet.sln b/OpenAI-DotNet.sln index 0331efb9..9c575111 100644 --- a/OpenAI-DotNet.sln +++ b/OpenAI-DotNet.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 17.5.33424.131 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI-DotNet", "OpenAI-DotNet\OpenAI-DotNet.csproj", "{76B6480E-154A-4341-B530-AFE04B254360}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI-DotNet-Proxy", "OpenAI-DotNet-Proxy\OpenAI-DotNet-Proxy.csproj", "{DEF7D9FF-A356-48C7-937F-C13FF59DF68A}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI-DotNet-Tests", "OpenAI-DotNet-Tests\OpenAI-DotNet-Tests.csproj", "{066EC5A5-47CE-4B91-B924-F236644037C1}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI-DotNet-Tests-Proxy", "OpenAI-DotNet-Tests-Proxy\OpenAI-DotNet-Tests-Proxy.csproj", "{F8B5D079-FD33-4A9E-92C4-CC88C911CFF3}" @@ -21,10 +19,6 @@ Global {76B6480E-154A-4341-B530-AFE04B254360}.Debug|Any CPU.Build.0 = Debug|Any CPU {76B6480E-154A-4341-B530-AFE04B254360}.Release|Any CPU.ActiveCfg = Release|Any CPU {76B6480E-154A-4341-B530-AFE04B254360}.Release|Any CPU.Build.0 = Release|Any CPU - {DEF7D9FF-A356-48C7-937F-C13FF59DF68A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DEF7D9FF-A356-48C7-937F-C13FF59DF68A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DEF7D9FF-A356-48C7-937F-C13FF59DF68A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DEF7D9FF-A356-48C7-937F-C13FF59DF68A}.Release|Any CPU.Build.0 = Release|Any CPU {066EC5A5-47CE-4B91-B924-F236644037C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {066EC5A5-47CE-4B91-B924-F236644037C1}.Debug|Any CPU.Build.0 = Debug|Any CPU {066EC5A5-47CE-4B91-B924-F236644037C1}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/OpenAI-DotNet/OpenAI-DotNet.csproj b/OpenAI-DotNet/OpenAI-DotNet.csproj index efbdbfed..cabaf057 100644 --- a/OpenAI-DotNet/OpenAI-DotNet.csproj +++ b/OpenAI-DotNet/OpenAI-DotNet.csproj @@ -17,7 +17,9 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet- https://github.com/RageAgainstThePixel/OpenAI-DotNet OpenAI, AI, ML, API, gpt-4, gpt-3.5-tubo, gpt-3, chatGPT, chat-gpt, gpt-2, gpt OpenAI API - Bump version to 6.2.0 + Bump version to 6.3.0 +- Removed OpenAI-DotNet-Proxy and put it directly in packge on its own +Bump version to 6.2.0 - Added OpenAI-DotNet-Proxy project and package. - Added support for custom domains - Updated unit tests @@ -75,7 +77,7 @@ Bump version to 4.4.0 OpenAI-DotNet.pfx true OpenAI-DotNet - $(OpenAIDotNetVersion) + 6.3.0 RageAgainstThePixel README.md Assets\OpenAI-DotNet-Icon.png @@ -95,5 +97,6 @@ Bump version to 4.4.0 True \ + diff --git a/OpenAI-DotNet-Proxy/Proxy/AbstractAuthenticationFilter.cs b/OpenAI-DotNet/Proxy/AbstractAuthenticationFilter.cs similarity index 100% rename from OpenAI-DotNet-Proxy/Proxy/AbstractAuthenticationFilter.cs rename to OpenAI-DotNet/Proxy/AbstractAuthenticationFilter.cs diff --git a/OpenAI-DotNet-Proxy/Proxy/IAuthenticationFilter.cs b/OpenAI-DotNet/Proxy/IAuthenticationFilter.cs similarity index 100% rename from OpenAI-DotNet-Proxy/Proxy/IAuthenticationFilter.cs rename to OpenAI-DotNet/Proxy/IAuthenticationFilter.cs diff --git a/OpenAI-DotNet-Proxy/Proxy/OpenAIProxyStartup.cs b/OpenAI-DotNet/Proxy/OpenAIProxyStartup.cs similarity index 100% rename from OpenAI-DotNet-Proxy/Proxy/OpenAIProxyStartup.cs rename to OpenAI-DotNet/Proxy/OpenAIProxyStartup.cs diff --git a/README.md b/README.md index 9440dbaa..ffaa114b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![Discord](https://img.shields.io/discord/855294214065487932.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/xQgMW9ufN4) [![NuGet version (OpenAI-DotNet)](https://img.shields.io/nuget/v/OpenAI-DotNet.svg?label=OpenAI-DotNet&logo=nuget)](https://www.nuget.org/packages/OpenAI-DotNet/) -[![NuGet version (OpenAI-DotNet-Proxy)](https://img.shields.io/nuget/v/OpenAI-DotNet-Proxy.svg?label=OpenAI-DotNet-Proxy&logo=nuget)](https://www.nuget.org/packages/OpenAI-DotNet-Proxy/) [![Nuget Publish](https://github.com/RageAgainstThePixel/OpenAI-DotNet/actions/workflows/Publish-Nuget.yml/badge.svg)](https://github.com/RageAgainstThePixel/OpenAI-DotNet/actions/workflows/Publish-Nuget.yml) A simple C# .NET client library for [OpenAI](https://openai.com/) to use use chat-gpt, GPT-4, GPT-3.5-Turbo and Dall-E though their RESTful API (currently in beta). Independently developed, this is not an official library and I am not affiliated with OpenAI. An OpenAI API account is required. @@ -150,13 +149,11 @@ var settings = new OpenAIClientSettings(resourceName: "your-resource", deploymen var api = new OpenAIClient(auth, settings); ``` -### :new: [OpenAI API Proxy](OpenAI-DotNet-Proxy/Readme.md) - -[![NuGet version (OpenAI-DotNet-Proxy)](https://img.shields.io/nuget/v/OpenAI-DotNet-Proxy.svg?label=OpenAI-DotNet-Proxy&logo=nuget)](https://www.nuget.org/packages/OpenAI-DotNet-Proxy/) +### :new: OpenAI API Proxy Using either the [OpenAI-DotNet](https://github.com/RageAgainstThePixel/OpenAI-DotNet) or [com.openai.unity](https://github.com/RageAgainstThePixel/com.openai.unity) packages directly in your front-end app may expose your API keys and other sensitive information. To mitigate this risk, it is recommended to set up an intermediate API that makes requests to OpenAI on behalf of your front-end app. This library can be utilized for both front-end and intermediary host configurations, ensuring secure communication with the OpenAI API. -#### Font End Example +#### Front End Example In the front end example, you will need to securely authenticate your users using your preferred OAuth provider. Once the user is authenticated, exchange your custom auth token with your API key on the backend. @@ -177,16 +174,16 @@ var settings = new OpenAIClientSettings(domain: "api.your-custom-domain.com"); var api = new OpenAIClient(auth, settings); ``` -This setup allows your front end application to securely communicate with your backend that will be using the OpenAI-DotNet-Proxy, which then forwards requests to the OpenAI API. This ensures that your OpenAI API keys and other sensitive information remain secure throughout the process. +This setup allows your front end application to securely communicate with your backend that will be using the proxy service, which then forwards requests to the OpenAI API. This ensures that your OpenAI API keys and other sensitive information remain secure throughout the process. #### Back End Example In this example, we demonstrate how to set up and use `OpenAIProxyStartup` in a new ASP.NET Core web app. The proxy server will handle authentication and forward requests to the OpenAI API, ensuring that your API keys and other sensitive information remain secure. 1. Create a new [ASP.NET Core minimal web API](https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0) project. -2. Add the OpenAI-DotNet-Proxy nuget package to your project. - a. Manually editing .csproj: `` - b. Powershell install: `Install-Package OpenAI-DotNet-Proxy` +2. Add the OpenAI-DotNet nuget package to your project. + - Powershell install: `Install-Package OpenAI-DotNet` + - Manually editing .csproj: `` 3. Create a new class that inherits from `AbstractAuthenticationFilter` and override the `ValidateAuthentication` method. This will implement the `IAuthenticationFilter` that you will use to check user session token against your internal server. 4. In `Program.cs`, create a new proxy web application by calling `OpenAIProxyStartup.CreateDefaultHost` method, passing your custom `AuthenticationFilter` as a type argument. 5. Create `OpenAIAuthentication` and `OpenAIClientSettings` as you would normally with your API keys, org id, or Azure settings.