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

Added files for Cosmos ingestion AI demo #1218

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions Git-SK/C-SK.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySemanticKernelApp", "MySemanticKernelApp\MySemanticKernelApp.csproj", "{38BEADDD-1C1B-4F0F-8AD9-A898E06C1F0F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{38BEADDD-1C1B-4F0F-8AD9-A898E06C1F0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38BEADDD-1C1B-4F0F-8AD9-A898E06C1F0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38BEADDD-1C1B-4F0F-8AD9-A898E06C1F0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38BEADDD-1C1B-4F0F-8AD9-A898E06C1F0F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F20DEA80-BC1A-45A4-9517-D4AB341B71FA}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions Git-SK/MySemanticKernelApp/MySemanticKernelApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.61.3" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.01" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.OpenApi" Version="1.6.3-alphaclear" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" />
</ItemGroup>



</Project>
127 changes: 127 additions & 0 deletions Git-SK/MySemanticKernelApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Plugins.OpenApi;
using System.Net.Http.Headers;
using static System.Environment;
using Microsoft.Identity.Client;


var builder = Kernel.CreateBuilder();

// // Add logging
// builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Error));

//Add Permission Filter
#pragma warning disable SKEXP0001
builder.Services.AddSingleton<IFunctionInvocationFilter, PermissionFilter>();



builder.Services.AddAzureOpenAIChatCompletion(
deploymentName: "",
modelId: "",// like gpt-4o
endpoint: "https://{your_openai-resource}.openai.azure.com/",
apiKey: ""
);
var kernel = builder.Build();



string ClientId = "";
string Authority = $"https://login.microsoftonline.com/{tenant_id}";
string[] Scopes = new string[] { "" };

var app = PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority(Authority)
.WithDefaultRedirectUri() // Uses http://localhost for a console app
.Build();

AuthenticationResult authResult = null;
try
{
authResult = await app.AcquireTokenInteractive(Scopes).ExecuteAsync();
}
catch (MsalException ex)
{
Console.WriteLine("An error occurred acquiring the token: " + ex.Message);
}

//import Logic App as a function
#pragma warning disable SKEXP0040
#pragma warning disable SKEXP0042 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
await kernel.ImportPluginFromOpenApiAsync(
pluginName: "pstest",
uri: new Uri("https://{logic-app-name}.azurewebsites.net/swagger.json"),
executionParameters: new OpenApiFunctionExecutionParameters()
{
HttpClient = new HttpClient()
{
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken)
}
}


}
);
#pragma warning restore SKEXP0042 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.



// Enable planning
//tell OpenAI it is ok to import the function
var settings = new OpenAIPromptExecutionSettings(){ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions};


//Get chat completion service
var chatService = kernel.GetRequiredService<IChatCompletionService>();

//Create chat history
ChatHistory chat = new();
ChatHistory chatMessages = new ChatHistory("""
You are a friendly assistant who likes to follow the rules. You will complete required steps
and request approval before taking any consequential actions. If the user doesn't provide
enough information for you to complete a task, you will keep asking questions until you have
enough information to complete the task.
""");

while (true){
Console.Write("Q:");

chat.AddUserMessage(Console.ReadLine());

//add the kernel and settings to the chat service
var r = await chatService.GetChatMessageContentAsync(chat, settings, kernel);
Console.WriteLine("A:" + r);
chat.Add(r); // add response to chat history


}

#pragma warning disable SKEXP0001
class PermissionFilter : IFunctionInvocationFilter
{

public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
Console.WriteLine($"Allow {context.Function.Name} to be invoked? If so, answer with 'y'");

if (Console.ReadLine() == "y")
{
// Perform some actions before function invocation
await next(context);
}
else{
throw new Exception("Function invocation not allowed");
}


}
}



46 changes: 46 additions & 0 deletions pdfToCosmos/connections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"serviceProviderConnections": {
"AzureBlob_pdf_ingestion": {
"parameterValues": {
"connectionString": "@appsetting('AzureBlob__pdf_ingestion_connectionString')"
},
"parameterSetName": "connectionString",
"serviceProvider": {
"id": "/serviceProviders/AzureBlob"
},
"displayName": "blob"
},
"AzureCosmosDB": {
"parameterValues": {
"connectionString": "@appsetting('AzureCosmosDB_connectionString')"
},
"serviceProvider": {
"id": "/serviceProviders/AzureCosmosDB"
},
"displayName": "docsDb"
},
"AzureBlob": {
"parameterValues": {
"connectionString": "@appsetting('AzureBlob_connectionString')"
},
"parameterSetName": "connectionString",
"serviceProvider": {
"id": "/serviceProviders/AzureBlob"
},
"displayName": "blob1"
},
"openai": {
"parameterValues": {
"openAIEndpoint": "@appsetting('openai_openAIEndpoint')",
"openAIKey": "@appsetting('openai_openAIKey')"
},
"parameterSetName": "KeyAndEndpointConnection",
"serviceProvider": {
"id": "/serviceProviders/openai"
},
"displayName": "openAI"
}

},
"managedApiConnections": {}
}
7 changes: 7 additions & 0 deletions pdfToCosmos/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle.Workflows",
"version": "[1.*, 2.0.0)"
}
}
30 changes: 30 additions & 0 deletions pdfToCosmos/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_EXTENSION_VERSION": "~4",
"WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "",
"WEBSITE_SITE_NAME": "pdfToCosmos",
"FUNCTIONS_WORKER_RUNTIME": "node",
"APP_KIND": "workflowApp",
"WEBSITE_AUTH_ENABLED": "False",
"openai_openAIKey": "",
"AzureWebJobsStorage": "",
"AzureFunctionsJobHost__extensionBundle__id": "Microsoft.Azure.Functions.ExtensionBundle.Workflows",
"ScmType": "None",
"openai_openAIEndpoint": "",
"WEBSITE_CONTENTSHARE": "",
"FUNCTIONS_RUNTIME_SCALE_MONITORING_ENABLED": "1",
"AzureFunctionsJobHost__extensionBundle__version": "[1.*, 2.0.0)",
"AzureBlob__pdf_ingestion_connectionString": "",
"WEBSITE_SLOT_NAME": "Production",
"AzureCosmosDB_connectionString": "",
"AzureBlob_connectionString": "",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "",
"WEBSITE_NODE_DEFAULT_VERSION": "18.20.4",
"WORKFLOWS_TENANT_ID": "",
"WORKFLOWS_SUBSCRIPTION_ID": "",
"WORKFLOWS_RESOURCE_GROUP_NAME": "",
"WORKFLOWS_LOCATION_NAME": "",
"WORKFLOWS_MANAGEMENT_BASE_URI": "https://management.azure.com/"
}
}
10 changes: 10 additions & 0 deletions pdfToCosmos/parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Blob_Path_pdf_ingestion": {
"type": "String",
"value": "docs"
},
"OpenAI_TextEmbedding_Deployment_Identifier_pdf_ingestion": {
"type": "String",
"value": "text-embedding"
}
}
Loading