forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into update-modelid
- Loading branch information
Showing
236 changed files
with
8,260 additions
and
2,434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[*.cs] | ||
dotnet_diagnostic.CA1016.severity = none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/bin | ||
**/obj | ||
local.settings.json | ||
azure-function/appsettings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Semantic Kernel OpenAI plugin starter | ||
|
||
This project provides starter code to create a OpenAI plugin. It includes the following components: | ||
|
||
- An endpoint that serves up an ai-plugin.json file for ChatGPT to discover the plugin | ||
- A generator that automatically converts prompts into prompt endpoints | ||
- The ability to add additional native functions as endpoints to the plugin | ||
|
||
## Prerequisites | ||
|
||
- [.NET 6](https://dotnet.microsoft.com/download/dotnet/8.0) is required to run this starter. | ||
- [Azure Functions Core Tools](https://www.npmjs.com/package/azure-functions-core-tools) is required to run this starter. | ||
- Install the recommended extensions | ||
- [C#](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) | ||
- [Semantic Kernel Tools](https://marketplace.visualstudio.com/items?itemName=ms-semantic-kernel.semantic-kernel) (optional) | ||
|
||
## Configuring the starter | ||
|
||
To configure the starter, you need to provide the following information: | ||
|
||
- Define the properties of the plugin in the appsettings.json file. | ||
- Enter the API key for your AI endpoint in the [local.settings.json](./azure-function/local.settings.json.example) file. | ||
|
||
### Using appsettings.json | ||
|
||
Configure an OpenAI endpoint | ||
|
||
1. Copy [settings.json.openai-example](./azure-function/config-samples/appsettings.json.openai-example) to `./appsettings.json` | ||
1. Edit the `kernel` object to add your OpenAI endpoint configuration | ||
1. Edit the `aiPlugin` object to define the properties that get exposed in the ai-plugin.json file | ||
|
||
Configure an Azure OpenAI endpoint | ||
|
||
1. Copy [settings.json.azure-example](./azure-function/config-samples/appsettings.json.azure-example) to `./appsettings.json` | ||
1. Edit the `kernel` object to add your Azure OpenAI endpoint configuration | ||
1. Edit the `aiPlugin` object to define the properties that get exposed in the ai-plugin.json file | ||
|
||
### Using local.settings.json | ||
|
||
1. Copy [local.settings.json.example](./azure-function/local.settings.json.example) to `./azure-function/local.settings.json` | ||
1. Edit the `Values` object to add your OpenAI endpoint configuration in the `apiKey` property | ||
|
||
## Running the starter | ||
|
||
To run the Azure Functions application just hit `F5`. | ||
|
||
To build and run the Azure Functions application from a terminal use the following commands: | ||
|
||
```powershell | ||
cd azure-function | ||
dotnet build | ||
cd bin/Debug/net8.0 | ||
func host start | ||
``` |
30 changes: 30 additions & 0 deletions
30
dotnet/samples/CreateChatGptPlugin/MathPlugin/azure-function/AIPluginJson.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Models; | ||
|
||
public class AIPluginJson | ||
{ | ||
[Function("GetAIPluginJson")] | ||
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = ".well-known/ai-plugin.json")] HttpRequestData req) | ||
{ | ||
var currentDomain = $"{req.Url.Scheme}://{req.Url.Host}:{req.Url.Port}"; | ||
|
||
HttpResponseData response = req.CreateResponse(HttpStatusCode.OK); | ||
response.Headers.Add("Content-Type", "application/json"); | ||
|
||
var appSettings = AppSettings.LoadSettings(); | ||
|
||
// serialize app settings to json using System.Text.Json | ||
var json = System.Text.Json.JsonSerializer.Serialize(appSettings.AIPlugin); | ||
|
||
// replace {url} with the current domain | ||
json = json.Replace("{url}", currentDomain, StringComparison.OrdinalIgnoreCase); | ||
|
||
response.WriteString(json); | ||
|
||
return response; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
dotnet/samples/CreateChatGptPlugin/MathPlugin/azure-function/Directory.Build.props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<!-- Default properties inherited by all projects. Projects can override. --> | ||
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild> | ||
<EnableNETAnalyzers>true</EnableNETAnalyzers> | ||
<AnalysisMode>AllEnabledByDefault</AnalysisMode> | ||
<AnalysisLevel>latest</AnalysisLevel> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<LangVersion>11</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
<NoWarn>CS1591,CA1852,CA1050</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<!-- Disable NuGet packaging by default. Projects can override. --> | ||
<IsPackable>disable</IsPackable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DebugType>portable</DebugType> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<RepoRoot>$([System.IO.Path]::GetDirectoryName($([MSBuild]::GetPathOfFileAbove('.gitignore', '$(MSBuildThisFileDirectory)'))))</RepoRoot> | ||
</PropertyGroup> | ||
|
||
</Project> |
25 changes: 25 additions & 0 deletions
25
dotnet/samples/CreateChatGptPlugin/MathPlugin/azure-function/Directory.Build.targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project> | ||
<!-- Direct all packages under 'dotnet' to get versions from Directory.Packages.props --> | ||
<!-- using Central Package Management feature --> | ||
<!-- https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management --> | ||
<Sdk Name="Microsoft.Build.CentralPackageVersions" Version="2.1.3" /> | ||
|
||
<!-- Only run 'dotnet format' on dev machines, Release builds. Skip on GitHub Actions --> | ||
<!-- as this runs in its own Actions job. --> | ||
<Target Name="DotnetFormatOnBuild" BeforeTargets="Build" | ||
Condition=" '$(Configuration)' == 'Release' AND '$(GITHUB_ACTIONS)' == '' "> | ||
<Message Text="Running dotnet format" Importance="high" /> | ||
<Exec Command="dotnet format --no-restore -v diag $(ProjectFileName)" /> | ||
</Target> | ||
|
||
<Target Name="AddInternalsVisibleTo" BeforeTargets="BeforeCompile"> | ||
<!-- Handle Add InternalsVisibleTo to any targets that don't support it. --> | ||
<ItemGroup Condition="'@(InternalsVisibleTo->Count())' > 0 AND $([MSBuild]::VersionLessThan($(NETCoreSdkVersion), '5.0.0'))"> | ||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo"> | ||
<_Parameter1 Condition="'%(InternalsVisibleTo.PublicKey)' != ''">%(InternalsVisibleTo.Identity), PublicKey="%(InternalsVisibleTo.PublicKey)</_Parameter1> | ||
<_Parameter1 Condition="'%(InternalsVisibleTo.PublicKey)' == ''">%(InternalsVisibleTo.Identity)</_Parameter1> | ||
<_Parameter1_TypeName>System.String</_Parameter1_TypeName> | ||
</AssemblyAttribute> | ||
</ItemGroup> | ||
</Target> | ||
</Project> |
56 changes: 56 additions & 0 deletions
56
dotnet/samples/CreateChatGptPlugin/MathPlugin/azure-function/Extensions/AIPluginRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel; | ||
|
||
namespace Plugins.AzureFunctions.Extensions; | ||
|
||
public class AIPluginRunner | ||
{ | ||
private readonly ILogger<AIPluginRunner> _logger; | ||
private readonly Kernel _kernel; | ||
|
||
public AIPluginRunner(Kernel kernel, ILoggerFactory loggerFactory) | ||
{ | ||
this._kernel = kernel; | ||
this._logger = loggerFactory.CreateLogger<AIPluginRunner>(); | ||
} | ||
|
||
/// <summary> | ||
/// Runs a prompt using the operationID and returns back an HTTP response. | ||
/// </summary> | ||
/// <param name="req"></param> | ||
/// <param name="pluginName"></param> | ||
/// <param name="functionName"></param> | ||
public async Task<HttpResponseData> RunAIPluginOperationAsync<T>(HttpRequestData req, string pluginName, string functionName) | ||
{ | ||
KernelArguments arguments = ConvertToKernelArguments((await JsonSerializer.DeserializeAsync<T>(req.Body).ConfigureAwait(true))!); | ||
|
||
var response = req.CreateResponse(HttpStatusCode.OK); | ||
response.Headers.Add("Content-Type", "text/plain;charset=utf-8"); | ||
await response.WriteStringAsync( | ||
(await this._kernel.InvokeAsync(pluginName, functionName, arguments).ConfigureAwait(false)).ToString() | ||
).ConfigureAwait(false); | ||
return response; | ||
} | ||
|
||
// Method to convert model to dictionary | ||
private static KernelArguments ConvertToKernelArguments<T>(T model) | ||
{ | ||
{ | ||
var arguments = new KernelArguments(); | ||
foreach (PropertyInfo property in typeof(T).GetProperties()) | ||
{ | ||
if (property.GetValue(model) != null) | ||
{ | ||
arguments.Add(property.Name, property.GetValue(model)); | ||
} | ||
} | ||
return arguments; | ||
} | ||
} | ||
} |
Oops, something went wrong.