-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added OpenAI.Plugin files and configurations
- Loading branch information
Showing
27 changed files
with
537 additions
and
22 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,74 @@ | ||
resource "azurerm_storage_account" "sa" { | ||
name = "stfunc${var.func_name}" | ||
location = var.location | ||
resource_group_name = var.resource_group_name | ||
account_tier = "Standard" | ||
account_replication_type = "LRS" | ||
enable_https_traffic_only = true | ||
} | ||
|
||
resource "azapi_resource" "ca_function" { | ||
schema_validation_enabled = false | ||
name = "func-${var.func_name}" | ||
location = var.location | ||
parent_id = var.resource_group_id | ||
type = "Microsoft.Web/sites@2023-01-01" | ||
body = jsonencode({ | ||
kind = "functionapp,linux,container,azurecontainerapps" | ||
properties : { | ||
language = "dotnet-isolated" | ||
managedEnvironmentId = "${var.cae_id}" | ||
siteConfig = { | ||
linuxFxVersion = "DOCKER|cmendibl3/aoai-plugin:0.8.0" | ||
appSettings = [ | ||
{ | ||
name = "AzureWebJobsStorage" | ||
value = azurerm_storage_account.sa.primary_connection_string | ||
}, | ||
{ | ||
name = "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING" | ||
value = azurerm_storage_account.sa.primary_connection_string | ||
}, | ||
{ | ||
name = "APPINSIGHTS_INSTRUMENTATIONKEY" | ||
value = var.appi_instrumentation_key | ||
}, | ||
{ | ||
name = "APPLICATIONINSIGHTS_CONNECTION_STRING" | ||
value = "InstrumentationKey=${var.appi_instrumentation_key}" | ||
}, | ||
{ | ||
name = "FUNCTIONS_WORKER_RUNTIME" | ||
value = "dotnet-isolated" | ||
}, | ||
{ | ||
name = "FUNCTIONS_EXTENSION_VERSION" | ||
value = "~4" | ||
}, | ||
{ | ||
name = "MODEL_ID" | ||
value = var.openai_model | ||
}, | ||
{ | ||
name = "API_KEY" | ||
value = var.openai_key | ||
}, | ||
{ | ||
name = "ENDPOINT" | ||
value = var.openai_endpoint | ||
}, | ||
{ | ||
name = "OpenApi__HostNames" | ||
value = "https://func-${var.func_name}.${var.cae_default_domain}/api" | ||
} | ||
] | ||
} | ||
workloadProfileName = "Consumption" | ||
resourceConfig = { | ||
cpu = 1 | ||
memory = "2Gi" | ||
} | ||
httpsOnly = false | ||
} | ||
}) | ||
} |
Empty file.
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,20 @@ | ||
terraform { | ||
required_version = ">= 1.1.8" | ||
required_providers { | ||
azurerm = { | ||
source = "hashicorp/azurerm" | ||
version = "3.87.0" | ||
} | ||
azapi = { | ||
source = "Azure/azapi" | ||
} | ||
} | ||
} | ||
|
||
provider "azurerm" { | ||
features { | ||
cognitive_account { | ||
purge_soft_delete_on_destroy = true | ||
} | ||
} | ||
} |
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,11 @@ | ||
variable "resource_group_name" {} | ||
variable "resource_group_id" {} | ||
variable "location" {} | ||
variable "func_name" {} | ||
variable "image_name" {} | ||
variable "cae_id" {} | ||
variable "cae_default_domain" {} | ||
variable "appi_instrumentation_key" {} | ||
variable "openai_key" {} | ||
variable "openai_model" {} | ||
variable "openai_endpoint" {} |
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 @@ | ||
local.settings.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,23 @@ | ||
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}/api"; | ||
|
||
HttpResponseData response = req.CreateResponse(HttpStatusCode.OK); | ||
response.Headers.Add("Content-Type", "application/json"); | ||
|
||
var settings = AIPluginSettings.FromFile(); | ||
|
||
// serialize app settings to json using System.Text.Json | ||
var json = System.Text.Json.JsonSerializer.Serialize(settings); | ||
|
||
// replace {url} with the current domain | ||
json = json.Replace("{url}", currentDomain, StringComparison.OrdinalIgnoreCase); | ||
|
||
response.WriteString(json); | ||
|
||
return response; | ||
} | ||
} |
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,73 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; | ||
using Microsoft.Extensions.Logging; | ||
using Models; | ||
|
||
namespace OpenAI.Plugin | ||
{ | ||
public class CallTranscriptPlugin | ||
{ | ||
private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; | ||
private readonly ILogger _logger; | ||
private readonly Kernel _kernel; | ||
private readonly string _endpoint = Environment.GetEnvironmentVariable("ENDPOINT")!; | ||
private readonly string _deploymentName = Environment.GetEnvironmentVariable("MODEL_ID")!; | ||
private readonly string _subscriptionKey = Environment.GetEnvironmentVariable("API_KEY")!; | ||
|
||
public CallTranscriptPlugin(ILoggerFactory loggerFactory, Kernel kernel) | ||
{ | ||
_logger = loggerFactory.CreateLogger<CallTranscriptPlugin>(); | ||
_kernel = kernel; | ||
} | ||
|
||
[Function("Call Transcript Plugin")] | ||
[OpenApiOperation(operationId: "CallTranscriptPlugin", tags: new[] { "CallTranscriptPlugin" }, Description = "Used to analyze a call given the transcript and a prompt")] | ||
[OpenApiRequestBody("application/json", typeof(ExecuteFunctionRequest), Description = "Variables to use when executing the specified function.", Required = true)] | ||
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(ExecuteFunctionResponse), Description = "Returns the response from the AI.")] | ||
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(ErrorResponse), Description = "Returned if the request body is invalid.")] | ||
[OpenApiResponseWithBody(statusCode: HttpStatusCode.NotFound, contentType: "application/json", bodyType: typeof(ErrorResponse), Description = "Returned if the semantic function could not be found.")] | ||
public async Task<HttpResponseData> Run( | ||
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "plugins/transcript")] HttpRequestData req, | ||
FunctionContext executionContext) | ||
{ | ||
_logger.LogInformation("C# HTTP trigger function processed a request."); | ||
|
||
#pragma warning disable CA1062 | ||
var functionRequest = await JsonSerializer.DeserializeAsync<ExecuteFunctionRequest>(req.Body, _jsonOptions).ConfigureAwait(false); | ||
#pragma warning disable CA1062 | ||
if (functionRequest == null) | ||
{ | ||
return await CreateResponseAsync(req, HttpStatusCode.BadRequest, new ErrorResponse() { Message = $"Invalid request body {functionRequest}" }).ConfigureAwait(false); | ||
} | ||
|
||
try | ||
{ | ||
var context = new KernelArguments | ||
{ | ||
{ "transcript", functionRequest.Transcript } | ||
}; | ||
|
||
var result = await _kernel.InvokeAsync("Prompts", "CallAnalyzer", context).ConfigureAwait(false); | ||
|
||
return await CreateResponseAsync( | ||
req, | ||
HttpStatusCode.OK, | ||
new ExecuteFunctionResponse() { Response = result.ToString() }).ConfigureAwait(false); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return await CreateResponseAsync(req, HttpStatusCode.BadRequest, new ErrorResponse() { Message = ex.Message }).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
private static async Task<HttpResponseData> CreateResponseAsync(HttpRequestData requestData, HttpStatusCode statusCode, object responseBody) | ||
{ | ||
var responseData = requestData.CreateResponse(statusCode); | ||
await responseData.WriteAsJsonAsync(responseBody).ConfigureAwait(false); | ||
return responseData; | ||
} | ||
} | ||
} |
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,12 @@ | ||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS installer-env | ||
|
||
COPY . /src/dotnet-function-app | ||
RUN cd /src/dotnet-function-app && \ | ||
mkdir -p /home/site/wwwroot && \ | ||
dotnet publish *.csproj --output /home/site/wwwroot | ||
|
||
FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4.0-dotnet-isolated8.0 | ||
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ | ||
AzureFunctionsJobHost__Logging__Console__IsEnabled=true | ||
|
||
COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"] |
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,19 @@ | ||
global using Microsoft.SemanticKernel; | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.Extensions.Logging; | ||
global using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; | ||
global using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; | ||
global using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums; | ||
global using Microsoft.Extensions.Hosting; | ||
global using Microsoft.OpenApi.Models; | ||
global using System.Text.Json.Serialization; | ||
global using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; | ||
global using Microsoft.Extensions.Configuration; | ||
global using System.Net; | ||
global using System.Reflection; | ||
global using Microsoft.Azure.Functions.Worker; | ||
global using Microsoft.Azure.Functions.Worker.Http; | ||
global using Azure.AI.OpenAI; | ||
global using Azure; | ||
global using Azure.Identity; | ||
global using Models; |
Oops, something went wrong.