Skip to content

Commit 41f35fb

Browse files
authored
Fix code style (#633)
1 parent 09a96c2 commit 41f35fb

File tree

13 files changed

+56
-69
lines changed

13 files changed

+56
-69
lines changed

dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ protected async Task<string> InternalCompleteTextAsync(
7272
}
7373
}
7474

75-
Response<Completions>? response = await RunRequestAsync<Response<Completions>?>(() =>
76-
this.Client.GetCompletionsAsync(this.ModelId, options, cancellationToken)
77-
).ConfigureAwait(false);
75+
Response<Completions>? response = await RunRequestAsync<Response<Completions>?>(
76+
() => this.Client.GetCompletionsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);
7877

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

102-
Response<Embeddings>? response = await RunRequestAsync<Response<Embeddings>?>(() =>
103-
this.Client.GetEmbeddingsAsync(this.ModelId, options, cancellationToken)
104-
).ConfigureAwait(false);
101+
Response<Embeddings>? response = await RunRequestAsync<Response<Embeddings>?>(
102+
() => this.Client.GetEmbeddingsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);
105103

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

172-
Response<ChatCompletions>? response = await RunRequestAsync<Response<ChatCompletions>?>(() =>
173-
this.Client.GetChatCompletionsAsync(this.ModelId, options, cancellationToken)
174-
).ConfigureAwait(false);
170+
Response<ChatCompletions>? response = await RunRequestAsync<Response<ChatCompletions>?>(
171+
() => this.Client.GetChatCompletionsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);
175172

176173
if (response == null || response.Value.Choices.Count < 1)
177174
{

dotnet/src/Connectors/Connectors.AI.OpenAI/Tokenizers/GPT3Tokenizer.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System.Buffers;
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
7-
using System.Diagnostics;
8-
using System.IO;
97
using System.Linq;
108
using System.Runtime.InteropServices;
119
using System.Text;
@@ -100,9 +98,9 @@ public static List<int> Encode(string text)
10098
// for every 1 UTF8 byte. If we can reasonably stack-allocate the space, we do, otherwise
10199
// we temporarily rent a pooled array.
102100
char[]? arrayPoolArray = null;
103-
Span<char> chars = maxUtf8Length <= 256 ?
104-
stackalloc char[maxUtf8Length] :
105-
(arrayPoolArray = ArrayPool<char>.Shared.Rent(maxUtf8Length));
101+
Span<char> chars = maxUtf8Length <= 256
102+
? stackalloc char[maxUtf8Length]
103+
: (arrayPoolArray = ArrayPool<char>.Shared.Rent(maxUtf8Length));
106104

107105
// Rather than using separate space for the UTF8 bytes, we just reinterpret the Span<char>
108106
// as a Span<byte>. Since our mapping is 1:1, the space required for the bytes will always
@@ -156,24 +154,29 @@ static unsafe int EncodingUtf8GetByteCount(ReadOnlySpan<char> chars)
156154
static unsafe int EncodingUtf8GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes)
157155
{
158156
fixed (char* charPtr = chars)
159-
fixed (byte* bytesPtr = bytes)
160157
{
161-
return Encoding.UTF8.GetBytes(charPtr, chars.Length, bytesPtr, bytes.Length);
158+
fixed (byte* bytesPtr = bytes)
159+
{
160+
return Encoding.UTF8.GetBytes(charPtr, chars.Length, bytesPtr, bytes.Length);
161+
}
162162
}
163163
}
164164
}
165165

166166
public static List<int> Encode(StringBuilder? stringBuilder) =>
167-
stringBuilder is not null ? Encode(stringBuilder.ToString()) :
168-
new List<int>();
167+
stringBuilder is not null
168+
? Encode(stringBuilder.ToString())
169+
: new List<int>();
169170

170171
public static List<int> Encode(char[]? chars) =>
171-
chars is not null ? Encode(new string(chars)) :
172-
new List<int>();
172+
chars is not null
173+
? Encode(new string(chars))
174+
: new List<int>();
173175

174176
public static List<int> Encode(IEnumerable<char>? chars) =>
175-
chars is not null ? Encode(string.Concat(chars)) :
176-
new List<int>();
177+
chars is not null
178+
? Encode(string.Concat(chars))
179+
: new List<int>();
177180

178181
private static List<string> BytePairEncoding(string token)
179182
{
@@ -237,6 +240,7 @@ private static List<string> BytePairEncoding(string token)
237240
{
238241
break;
239242
}
243+
240244
i = j;
241245

242246
if (i < (word.Count - 1) &&

samples/apps/copilot-chat-app/webapi/Config/AuthorizationOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System.ComponentModel.DataAnnotations;
4-
using Microsoft.Identity.Web;
54

65
namespace SemanticKernel.Service.Config;
76

samples/apps/copilot-chat-app/webapi/Config/ChatStoreOptions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3-
using static SemanticKernel.Service.Config.AuthorizationOptions;
4-
53
namespace SemanticKernel.Service.Config;
64

75
/// <summary>

samples/apps/copilot-chat-app/webapi/Config/NotEmptyOrWhitespaceAttribute.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System.ComponentModel.DataAnnotations;
4-
using System.Reflection;
54

65
namespace SemanticKernel.Service.Config;
76

samples/apps/copilot-chat-app/webapi/Controllers/BotController.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public async Task<ActionResult> UploadAsync(
6666
this._logger.LogDebug("Received call to upload a bot");
6767

6868
if (!this.IsBotCompatible(
69-
externalBotSchema: bot.Schema,
70-
externalBotEmbeddingConfig: bot.EmbeddingConfigurations,
71-
embeddingOptions: aiServiceOptions.Get(AIServiceOptions.EmbeddingPropertyName),
72-
botSchemaOptions: botSchemaOptions.Value))
69+
externalBotSchema: bot.Schema,
70+
externalBotEmbeddingConfig: bot.EmbeddingConfigurations,
71+
embeddingOptions: aiServiceOptions.Get(AIServiceOptions.EmbeddingPropertyName),
72+
botSchemaOptions: botSchemaOptions.Value))
7373
{
7474
return this.BadRequest("Incompatible schema");
7575
}
@@ -160,9 +160,9 @@ private bool IsBotCompatible(
160160
{
161161
// The app can define what schema/version it supports before the community comes out with an open schema.
162162
return externalBotSchema.Name.Equals(botSchemaOptions.Name, StringComparison.OrdinalIgnoreCase)
163-
&& externalBotSchema.Version == botSchemaOptions.Version
164-
&& externalBotEmbeddingConfig.AIService == embeddingOptions.AIService
165-
&& externalBotEmbeddingConfig.DeploymentOrModelId.Equals(embeddingOptions.DeploymentOrModelId, StringComparison.OrdinalIgnoreCase);
163+
&& externalBotSchema.Version == botSchemaOptions.Version
164+
&& externalBotEmbeddingConfig.AIService == embeddingOptions.AIService
165+
&& externalBotEmbeddingConfig.DeploymentOrModelId.Equals(embeddingOptions.DeploymentOrModelId, StringComparison.OrdinalIgnoreCase);
166166
}
167167

168168
/// <summary>

samples/apps/copilot-chat-app/webapi/Controllers/SemanticKernelController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public SemanticKernelController(
4242
/// <param name="kernel">Semantic kernel obtained through dependency injection</param>
4343
/// <param name="chatRepository">Storage repository to store chat sessions</param>
4444
/// <param name="chatMessageRepository">Storage repository to store chat messages</param>
45-
/// <param name="documentMemoryOptions">Options for document memory handline.</param>
45+
/// <param name="documentMemoryOptions">Options for document memory handling.</param>
4646
/// <param name="ask">Prompt along with its parameters</param>
4747
/// <param name="skillName">Skill in which function to invoke resides</param>
4848
/// <param name="functionName">Name of function to invoke</param>

samples/apps/copilot-chat-app/webapi/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3-
using System.Net;
43
using Microsoft.AspNetCore.Hosting.Server;
54
using Microsoft.AspNetCore.Hosting.Server.Features;
6-
using Microsoft.AspNetCore.Server.Kestrel.Core;
7-
using Microsoft.Extensions.Options;
85

96
namespace SemanticKernel.Service;
107

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

2826
// Add in configuration options and Semantic Kernel services.
2927
builder.Services
30-
.AddSingleton<ILogger>(sp => sp.GetRequiredService<ILogger<Program>>()) // some services require an untemplated ILogger
28+
.AddSingleton<ILogger>(sp => sp.GetRequiredService<ILogger<Program>>()) // some services require an un-templated ILogger
3129
.AddOptions(builder.Configuration)
3230
.AddSemanticKernelServices()
3331
.AddPersistentChatStore();

samples/apps/copilot-chat-app/webapi/SemanticKernelExtensions.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
2828
{
2929
string promptsConfigPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "prompts.json");
3030
PromptsConfig promptsConfig = JsonSerializer.Deserialize<PromptsConfig>(File.ReadAllText(promptsConfigPath)) ??
31-
throw new InvalidOperationException($"Failed to load '{promptsConfigPath}'.");
31+
throw new InvalidOperationException($"Failed to load '{promptsConfigPath}'.");
3232
promptsConfig.Validate();
3333
return promptsConfig;
3434
});
@@ -46,7 +46,8 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
4646
case MemoriesStoreOptions.MemoriesStoreType.Qdrant:
4747
if (config.Qdrant is null)
4848
{
49-
throw new InvalidOperationException($"MemoriesStore:Qdrant is required when MemoriesStore:Type is '{MemoriesStoreOptions.MemoriesStoreType.Qdrant}'");
49+
throw new InvalidOperationException(
50+
$"MemoriesStore:Qdrant is required when MemoriesStore:Type is '{MemoriesStoreOptions.MemoriesStoreType.Qdrant}'");
5051
}
5152

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

6364
services.AddScoped<ISemanticTextMemory>(serviceProvider => new SemanticTextMemory(
64-
serviceProvider.GetRequiredService<IMemoryStore>(),
65-
serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>().Get(AIServiceOptions.EmbeddingPropertyName)
66-
.ToTextEmbeddingsService(serviceProvider.GetRequiredService<ILogger<AIServiceOptions>>())));
67-
65+
serviceProvider.GetRequiredService<IMemoryStore>(),
66+
serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>().Get(AIServiceOptions.EmbeddingPropertyName)
67+
.ToTextEmbeddingsService(serviceProvider.GetRequiredService<ILogger<AIServiceOptions>>())));
6868

6969
// Add the Semantic Kernel
7070
services.AddSingleton<IPromptTemplateEngine, PromptTemplateEngine>();
7171
services.AddScoped<ISkillCollection, SkillCollection>();
7272
services.AddScoped<KernelConfig>(serviceProvider => new KernelConfig()
73-
.AddCompletionBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>())
74-
.AddEmbeddingBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>()));
73+
.AddCompletionBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>())
74+
.AddEmbeddingBackend(serviceProvider.GetRequiredService<IOptionsSnapshot<AIServiceOptions>>()));
7575
services.AddScoped<IKernel, Kernel>();
7676

7777
return services;
@@ -148,7 +148,6 @@ internal static IEmbeddingGeneration<string, float> ToTextEmbeddingsService(this
148148
ILogger? logger = null,
149149
IDelegatingHandlerFactory? handlerFactory = null)
150150
{
151-
152151
return serviceConfig.AIService switch
153152
{
154153
AIServiceOptions.AIServiceType.AzureOpenAI => new AzureTextEmbeddingGeneration(

samples/apps/copilot-chat-app/webapi/ServiceExtensions.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3-
using System.Reflection;
4-
using System.Text.Json;
53
using Microsoft.AspNetCore.Authentication;
64
using Microsoft.AspNetCore.Authentication.JwtBearer;
75
using Microsoft.Extensions.Options;
86
using Microsoft.Identity.Web;
9-
using Microsoft.SemanticKernel;
10-
using Microsoft.SemanticKernel.AI.Embeddings;
11-
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.TextEmbedding;
12-
using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;
13-
using Microsoft.SemanticKernel.Memory;
14-
using Microsoft.SemanticKernel.Reliability;
15-
using Microsoft.SemanticKernel.SkillDefinition;
16-
using Microsoft.SemanticKernel.TemplateEngine;
177
using SemanticKernel.Service.Auth;
188
using SemanticKernel.Service.Config;
199
using SemanticKernel.Service.Skills;
@@ -26,7 +16,7 @@ internal static class ServicesExtensions
2616
/// <summary>
2717
/// Parse configuration into options.
2818
/// </summary>
29-
internal static IServiceCollection AddOptions(this IServiceCollection services, Microsoft.Extensions.Configuration.ConfigurationManager configuration)
19+
internal static IServiceCollection AddOptions(this IServiceCollection services, ConfigurationManager configuration)
3020
{
3121
// General configuration
3222
services.AddOptions<ServiceOptions>()
@@ -102,21 +92,21 @@ internal static IServiceCollection AddAuthorization(this IServiceCollection serv
10292
{
10393
case AuthorizationOptions.AuthorizationType.AzureAd:
10494
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
105-
.AddMicrosoftIdentityWebApi(configuration.GetSection($"{AuthorizationOptions.PropertyName}:AzureAd"));
95+
.AddMicrosoftIdentityWebApi(configuration.GetSection($"{AuthorizationOptions.PropertyName}:AzureAd"));
10696
break;
10797

10898
case AuthorizationOptions.AuthorizationType.ApiKey:
10999
services.AddAuthentication(ApiKeyAuthenticationHandler.AuthenticationScheme)
110-
.AddScheme<ApiKeyAuthenticationSchemeOptions, ApiKeyAuthenticationHandler>(
111-
ApiKeyAuthenticationHandler.AuthenticationScheme,
112-
options => options.ApiKey = config.ApiKey);
100+
.AddScheme<ApiKeyAuthenticationSchemeOptions, ApiKeyAuthenticationHandler>(
101+
ApiKeyAuthenticationHandler.AuthenticationScheme,
102+
options => options.ApiKey = config.ApiKey);
113103
break;
114104

115105
case AuthorizationOptions.AuthorizationType.None:
116106
services.AddAuthentication(PassThroughAuthenticationHandler.AuthenticationScheme)
117-
.AddScheme<AuthenticationSchemeOptions, PassThroughAuthenticationHandler>(
118-
authenticationScheme: PassThroughAuthenticationHandler.AuthenticationScheme,
119-
configureOptions: null);
107+
.AddScheme<AuthenticationSchemeOptions, PassThroughAuthenticationHandler>(
108+
authenticationScheme: PassThroughAuthenticationHandler.AuthenticationScheme,
109+
configureOptions: null);
120110
break;
121111

122112
default:

0 commit comments

Comments
 (0)