Skip to content

Commit

Permalink
Fix code style (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc authored Apr 25, 2023
1 parent 09a96c2 commit 41f35fb
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ protected async Task<string> InternalCompleteTextAsync(
}
}

Response<Completions>? response = await RunRequestAsync<Response<Completions>?>(() =>
this.Client.GetCompletionsAsync(this.ModelId, options, cancellationToken)
).ConfigureAwait(false);
Response<Completions>? response = await RunRequestAsync<Response<Completions>?>(
() => this.Client.GetCompletionsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);

if (response == null || response.Value.Choices.Count < 1)
{
Expand All @@ -99,9 +98,8 @@ protected async Task<IList<Embedding<float>>> InternalGenerateTextEmbeddingsAsyn
{
var options = new EmbeddingsOptions(text);

Response<Embeddings>? response = await RunRequestAsync<Response<Embeddings>?>(() =>
this.Client.GetEmbeddingsAsync(this.ModelId, options, cancellationToken)
).ConfigureAwait(false);
Response<Embeddings>? response = await RunRequestAsync<Response<Embeddings>?>(
() => this.Client.GetEmbeddingsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);

if (response == null || response.Value.Data.Count < 1)
{
Expand Down Expand Up @@ -169,9 +167,8 @@ protected async Task<string> InternalGenerateChatMessageAsync(
options.Messages.Add(new ChatMessage(role, message.Content));
}

Response<ChatCompletions>? response = await RunRequestAsync<Response<ChatCompletions>?>(() =>
this.Client.GetChatCompletionsAsync(this.ModelId, options, cancellationToken)
).ConfigureAwait(false);
Response<ChatCompletions>? response = await RunRequestAsync<Response<ChatCompletions>?>(
() => this.Client.GetChatCompletionsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);

if (response == null || response.Value.Choices.Count < 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -100,9 +98,9 @@ public static List<int> Encode(string text)
// for every 1 UTF8 byte. If we can reasonably stack-allocate the space, we do, otherwise
// we temporarily rent a pooled array.
char[]? arrayPoolArray = null;
Span<char> chars = maxUtf8Length <= 256 ?
stackalloc char[maxUtf8Length] :
(arrayPoolArray = ArrayPool<char>.Shared.Rent(maxUtf8Length));
Span<char> chars = maxUtf8Length <= 256
? stackalloc char[maxUtf8Length]
: (arrayPoolArray = ArrayPool<char>.Shared.Rent(maxUtf8Length));

// Rather than using separate space for the UTF8 bytes, we just reinterpret the Span<char>
// as a Span<byte>. Since our mapping is 1:1, the space required for the bytes will always
Expand Down Expand Up @@ -156,24 +154,29 @@ static unsafe int EncodingUtf8GetByteCount(ReadOnlySpan<char> chars)
static unsafe int EncodingUtf8GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes)
{
fixed (char* charPtr = chars)
fixed (byte* bytesPtr = bytes)
{
return Encoding.UTF8.GetBytes(charPtr, chars.Length, bytesPtr, bytes.Length);
fixed (byte* bytesPtr = bytes)
{
return Encoding.UTF8.GetBytes(charPtr, chars.Length, bytesPtr, bytes.Length);
}
}
}
}

public static List<int> Encode(StringBuilder? stringBuilder) =>
stringBuilder is not null ? Encode(stringBuilder.ToString()) :
new List<int>();
stringBuilder is not null
? Encode(stringBuilder.ToString())
: new List<int>();

public static List<int> Encode(char[]? chars) =>
chars is not null ? Encode(new string(chars)) :
new List<int>();
chars is not null
? Encode(new string(chars))
: new List<int>();

public static List<int> Encode(IEnumerable<char>? chars) =>
chars is not null ? Encode(string.Concat(chars)) :
new List<int>();
chars is not null
? Encode(string.Concat(chars))
: new List<int>();

private static List<string> BytePairEncoding(string token)
{
Expand Down Expand Up @@ -237,6 +240,7 @@ private static List<string> BytePairEncoding(string token)
{
break;
}

i = j;

if (i < (word.Count - 1) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ComponentModel.DataAnnotations;
using Microsoft.Identity.Web;

namespace SemanticKernel.Service.Config;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.

using static SemanticKernel.Service.Config.AuthorizationOptions;

namespace SemanticKernel.Service.Config;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace SemanticKernel.Service.Config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public async Task<ActionResult> UploadAsync(
this._logger.LogDebug("Received call to upload a bot");

if (!this.IsBotCompatible(
externalBotSchema: bot.Schema,
externalBotEmbeddingConfig: bot.EmbeddingConfigurations,
embeddingOptions: aiServiceOptions.Get(AIServiceOptions.EmbeddingPropertyName),
botSchemaOptions: botSchemaOptions.Value))
externalBotSchema: bot.Schema,
externalBotEmbeddingConfig: bot.EmbeddingConfigurations,
embeddingOptions: aiServiceOptions.Get(AIServiceOptions.EmbeddingPropertyName),
botSchemaOptions: botSchemaOptions.Value))
{
return this.BadRequest("Incompatible schema");
}
Expand Down Expand Up @@ -160,9 +160,9 @@ private bool IsBotCompatible(
{
// The app can define what schema/version it supports before the community comes out with an open schema.
return externalBotSchema.Name.Equals(botSchemaOptions.Name, StringComparison.OrdinalIgnoreCase)
&& externalBotSchema.Version == botSchemaOptions.Version
&& externalBotEmbeddingConfig.AIService == embeddingOptions.AIService
&& externalBotEmbeddingConfig.DeploymentOrModelId.Equals(embeddingOptions.DeploymentOrModelId, StringComparison.OrdinalIgnoreCase);
&& externalBotSchema.Version == botSchemaOptions.Version
&& externalBotEmbeddingConfig.AIService == embeddingOptions.AIService
&& externalBotEmbeddingConfig.DeploymentOrModelId.Equals(embeddingOptions.DeploymentOrModelId, StringComparison.OrdinalIgnoreCase);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SemanticKernelController(
/// <param name="kernel">Semantic kernel obtained through dependency injection</param>
/// <param name="chatRepository">Storage repository to store chat sessions</param>
/// <param name="chatMessageRepository">Storage repository to store chat messages</param>
/// <param name="documentMemoryOptions">Options for document memory handline.</param>
/// <param name="documentMemoryOptions">Options for document memory handling.</param>
/// <param name="ask">Prompt along with its parameters</param>
/// <param name="skillName">Skill in which function to invoke resides</param>
/// <param name="functionName">Name of function to invoke</param>
Expand Down
6 changes: 2 additions & 4 deletions samples/apps/copilot-chat-app/webapi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Options;

namespace SemanticKernel.Service;

Expand All @@ -17,6 +14,7 @@ public sealed class Program
/// Entry point
/// </summary>
/// <param name="args">Web application command-line arguments.</param>
// ReSharper disable once InconsistentNaming
public static async Task Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
Expand All @@ -27,7 +25,7 @@ public static async Task Main(string[] args)

// Add in configuration options and Semantic Kernel services.
builder.Services
.AddSingleton<ILogger>(sp => sp.GetRequiredService<ILogger<Program>>()) // some services require an untemplated ILogger
.AddSingleton<ILogger>(sp => sp.GetRequiredService<ILogger<Program>>()) // some services require an un-templated ILogger
.AddOptions(builder.Configuration)
.AddSemanticKernelServices()
.AddPersistentChatStore();
Expand Down
17 changes: 8 additions & 9 deletions samples/apps/copilot-chat-app/webapi/SemanticKernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
{
string promptsConfigPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "prompts.json");
PromptsConfig promptsConfig = JsonSerializer.Deserialize<PromptsConfig>(File.ReadAllText(promptsConfigPath)) ??
throw new InvalidOperationException($"Failed to load '{promptsConfigPath}'.");
throw new InvalidOperationException($"Failed to load '{promptsConfigPath}'.");
promptsConfig.Validate();
return promptsConfig;
});
Expand All @@ -46,7 +46,8 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
case MemoriesStoreOptions.MemoriesStoreType.Qdrant:
if (config.Qdrant is null)
{
throw new InvalidOperationException($"MemoriesStore:Qdrant is required when MemoriesStore:Type is '{MemoriesStoreOptions.MemoriesStoreType.Qdrant}'");
throw new InvalidOperationException(
$"MemoriesStore:Qdrant is required when MemoriesStore:Type is '{MemoriesStoreOptions.MemoriesStoreType.Qdrant}'");
}

return new QdrantMemoryStore(
Expand All @@ -61,17 +62,16 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
});

services.AddScoped<ISemanticTextMemory>(serviceProvider => new SemanticTextMemory(
serviceProvider.GetRequiredService<IMemoryStore>(),
serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>().Get(AIServiceOptions.EmbeddingPropertyName)
.ToTextEmbeddingsService(serviceProvider.GetRequiredService<ILogger<AIServiceOptions>>())));

serviceProvider.GetRequiredService<IMemoryStore>(),
serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>().Get(AIServiceOptions.EmbeddingPropertyName)
.ToTextEmbeddingsService(serviceProvider.GetRequiredService<ILogger<AIServiceOptions>>())));

// Add the Semantic Kernel
services.AddSingleton<IPromptTemplateEngine, PromptTemplateEngine>();
services.AddScoped<ISkillCollection, SkillCollection>();
services.AddScoped<KernelConfig>(serviceProvider => new KernelConfig()
.AddCompletionBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>())
.AddEmbeddingBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>()));
.AddCompletionBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>())
.AddEmbeddingBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>()));
services.AddScoped<IKernel, Kernel>();

return services;
Expand Down Expand Up @@ -148,7 +148,6 @@ internal static IEmbeddingGeneration<string, float> ToTextEmbeddingsService(this
ILogger? logger = null,
IDelegatingHandlerFactory? handlerFactory = null)
{

return serviceConfig.AIService switch
{
AIServiceOptions.AIServiceType.AzureOpenAI => new AzureTextEmbeddingGeneration(
Expand Down
26 changes: 8 additions & 18 deletions samples/apps/copilot-chat-app/webapi/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Reflection;
using System.Text.Json;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Web;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.Embeddings;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.TextEmbedding;
using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Reliability;
using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.TemplateEngine;
using SemanticKernel.Service.Auth;
using SemanticKernel.Service.Config;
using SemanticKernel.Service.Skills;
Expand All @@ -26,7 +16,7 @@ internal static class ServicesExtensions
/// <summary>
/// Parse configuration into options.
/// </summary>
internal static IServiceCollection AddOptions(this IServiceCollection services, Microsoft.Extensions.Configuration.ConfigurationManager configuration)
internal static IServiceCollection AddOptions(this IServiceCollection services, ConfigurationManager configuration)
{
// General configuration
services.AddOptions<ServiceOptions>()
Expand Down Expand Up @@ -102,21 +92,21 @@ internal static IServiceCollection AddAuthorization(this IServiceCollection serv
{
case AuthorizationOptions.AuthorizationType.AzureAd:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(configuration.GetSection($"{AuthorizationOptions.PropertyName}:AzureAd"));
.AddMicrosoftIdentityWebApi(configuration.GetSection($"{AuthorizationOptions.PropertyName}:AzureAd"));
break;

case AuthorizationOptions.AuthorizationType.ApiKey:
services.AddAuthentication(ApiKeyAuthenticationHandler.AuthenticationScheme)
.AddScheme<ApiKeyAuthenticationSchemeOptions, ApiKeyAuthenticationHandler>(
ApiKeyAuthenticationHandler.AuthenticationScheme,
options => options.ApiKey = config.ApiKey);
.AddScheme<ApiKeyAuthenticationSchemeOptions, ApiKeyAuthenticationHandler>(
ApiKeyAuthenticationHandler.AuthenticationScheme,
options => options.ApiKey = config.ApiKey);
break;

case AuthorizationOptions.AuthorizationType.None:
services.AddAuthentication(PassThroughAuthenticationHandler.AuthenticationScheme)
.AddScheme<AuthenticationSchemeOptions, PassThroughAuthenticationHandler>(
authenticationScheme: PassThroughAuthenticationHandler.AuthenticationScheme,
configureOptions: null);
.AddScheme<AuthenticationSchemeOptions, PassThroughAuthenticationHandler>(
authenticationScheme: PassThroughAuthenticationHandler.AuthenticationScheme,
configureOptions: null);
break;

default:
Expand Down
2 changes: 0 additions & 2 deletions samples/apps/copilot-chat-app/webapi/Skills/ChatSkill.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Globalization;
using System.Numerics;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.TextCompletion;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Planning.Planners;
using Microsoft.SemanticKernel.SkillDefinition;
using SemanticKernel.Service.Storage;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Globalization;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.TextCompletion;
Expand All @@ -15,7 +16,11 @@ internal static class SemanticMemoryExtractor
/// <param name="memoryName">Name of the memory category</param>
internal static string MemoryCollectionName(string chatId, string memoryName) => $"{chatId}-{memoryName}";

internal static async Task<SemanticChatMemory> ExtractCognitiveMemoryAsync(string memoryName, IKernel kernel, SKContext context, PromptSettings promptSettings)
internal static async Task<SemanticChatMemory> ExtractCognitiveMemoryAsync(
string memoryName,
IKernel kernel,
SKContext context,
PromptSettings promptSettings)
{
if (!promptSettings.MemoryMap.TryGetValue(memoryName, out var memoryPrompt))
{
Expand Down
2 changes: 1 addition & 1 deletion samples/apps/copilot-chat-app/webapi/Skills/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel.Connectors.AI.OpenAI.Tokenizers;
using Microsoft.SemanticKernel.Orchestration;

Expand Down Expand Up @@ -36,4 +37,3 @@ internal static int TokenCount(string text)
return tokens.Count;
}
}

0 comments on commit 41f35fb

Please sign in to comment.