Skip to content

Commit 353b662

Browse files
committed
Always use Unix new line ending with StringBuilder
1 parent a490102 commit 353b662

12 files changed

Lines changed: 72 additions & 21 deletions

File tree

applications/evaluation/Evaluators/Faithfulness/FaithfulnessEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal async Task<float> Evaluate(MemoryAnswer answer, Dictionary<string, obje
5959
{
6060
var evaluation = await this.FaithfulnessEvaluation.InvokeAsync(this._kernel, new KernelArguments
6161
{
62-
{ "context", string.Join(Environment.NewLine, answer.RelevantSources.SelectMany(c => c.Partitions.Select(p => p.Text))) },
62+
{ "context", string.Join('\n', answer.RelevantSources.SelectMany(c => c.Partitions.Select(p => p.Text))) },
6363
{ "answer", answer.Result },
6464
{ "statements", JsonSerializer.Serialize(extraction) }
6565
}).ConfigureAwait(false);

applications/evaluation/Evaluators/Relevance/RelevanceEvaluator.cs

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

3-
using System;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Numerics.Tensors;
@@ -68,7 +67,7 @@ private async IAsyncEnumerable<RelevanceEvaluation> GetEvaluations(MemoryAnswer
6867
{
6968
var extraction = await this.ExtractQuestion.InvokeAsync(this._kernel, new KernelArguments
7069
{
71-
{ "context", string.Join(Environment.NewLine, answer.RelevantSources.SelectMany(c => c.Partitions.Select(p => p.Text))) },
70+
{ "context", string.Join('\n', answer.RelevantSources.SelectMany(c => c.Partitions.Select(p => p.Text))) },
7271
{ "answer", answer.Result }
7372
}).ConfigureAwait(false);
7473

extensions/Postgres/Postgres/PostgresMemory.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.KernelMemory.AI;
1414
using Microsoft.KernelMemory.Diagnostics;
1515
using Microsoft.KernelMemory.MemoryStorage;
16+
using Microsoft.KernelMemory.Text;
1617
using Pgvector;
1718

1819
namespace Microsoft.KernelMemory.Postgres;
@@ -272,7 +273,7 @@ private static string NormalizeTableNamePrefix(string? name)
272273
foreach (MemoryFilter filter in filters.Where(f => !f.IsEmpty()))
273274
{
274275
var andSql = new StringBuilder();
275-
andSql.AppendLine("(");
276+
andSql.AppendLineNix("(");
276277

277278
if (filter is PostgresMemoryFilter)
278279
{
@@ -298,10 +299,10 @@ private static string NormalizeTableNamePrefix(string? name)
298299
// $"{PostgresSchema.PlaceholdersTags} @> " + safeSqlPlaceholder
299300
// $"{PostgresSchema.PlaceholdersTags} @> " + safeSqlPlaceholder + "::text[]"
300301
// $"{PostgresSchema.PlaceholdersTags} @> ARRAY[" + safeSqlPlaceholder + "]::text[]"
301-
andSql.AppendLine($"{PostgresSchema.PlaceholdersTags} @> " + safeSqlPlaceholder);
302+
andSql.AppendLineNix($"{PostgresSchema.PlaceholdersTags} @> " + safeSqlPlaceholder);
302303
}
303304

304-
andSql.AppendLine(")");
305+
andSql.AppendLineNix(")");
305306
orConditions.Add(andSql.ToString());
306307
}
307308

service/Abstractions/HTTP/SSE.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using System.Text.Json;
1010
using System.Threading;
11+
using Microsoft.KernelMemory.Text;
1112

1213
namespace Microsoft.KernelMemory.HTTP;
1314

@@ -39,7 +40,7 @@ public async static IAsyncEnumerable<T> ParseStreamAsync<T>(
3940
}
4041
else
4142
{
42-
buffer.AppendLine(line);
43+
buffer.AppendLineNix(line);
4344
}
4445
}
4546

service/Abstractions/Models/MemoryAnswer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using System.Text.Json;
88
using System.Text.Json.Serialization;
9+
using Microsoft.KernelMemory.Text;
910

1011
namespace Microsoft.KernelMemory;
1112

@@ -92,7 +93,7 @@ public string ToJson(bool optimizeForStream)
9293
public override string ToString()
9394
{
9495
var result = new StringBuilder();
95-
result.AppendLine(this.Result);
96+
result.AppendLineNix(this.Result);
9697

9798
if (!this.NoResult && this.RelevantSources is { Count: > 0 })
9899
{
@@ -103,8 +104,8 @@ public override string ToString()
103104
sources[x.Index + x.Link] = $" - {x.SourceName} [{date}]";
104105
}
105106

106-
result.AppendLine("- Sources:");
107-
result.AppendLine(string.Join("\n", sources.Values));
107+
result.AppendLineNix("- Sources:");
108+
result.AppendLineNix(string.Join("\n", sources.Values));
108109
}
109110

110111
return result.ToString();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Text;
4+
5+
namespace Microsoft.KernelMemory.Text;
6+
7+
public static class StringBuilderExtensions
8+
{
9+
/// <summary>
10+
/// Append line using Unix line ending "\n"
11+
/// </summary>
12+
public static void AppendLineNix(this StringBuilder sb)
13+
{
14+
sb.Append('\n');
15+
}
16+
17+
/// <summary>
18+
/// Append line using Unix line ending "\n"
19+
/// </summary>
20+
public static void AppendLineNix(this StringBuilder sb, string value)
21+
{
22+
sb.Append(value);
23+
sb.Append('\n');
24+
}
25+
26+
/// <summary>
27+
/// Append line using Unix line ending "\n"
28+
/// </summary>
29+
public static void AppendLineNix(this StringBuilder sb, char value)
30+
{
31+
sb.Append(value);
32+
sb.Append('\n');
33+
}
34+
35+
/// <summary>
36+
/// Append line using Unix line ending "\n"
37+
/// </summary>
38+
public static void AppendLineNix(this StringBuilder sb, StringBuilder value)
39+
{
40+
sb.Append(value);
41+
sb.Append('\n');
42+
}
43+
}

service/Core/DataFormats/Office/MsExcelDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Extensions.Logging;
1212
using Microsoft.KernelMemory.Diagnostics;
1313
using Microsoft.KernelMemory.Pipeline;
14+
using Microsoft.KernelMemory.Text;
1415

1516
namespace Microsoft.KernelMemory.DataFormats.Office;
1617

@@ -61,7 +62,7 @@ public Task<FileContent> DecodeAsync(Stream data, CancellationToken cancellation
6162
worksheetNumber++;
6263
if (this._config.WithWorksheetNumber)
6364
{
64-
sb.AppendLine(this._config.WorksheetNumberTemplate.Replace("{number}", $"{worksheetNumber}", StringComparison.OrdinalIgnoreCase));
65+
sb.AppendLineNix(this._config.WorksheetNumberTemplate.Replace("{number}", $"{worksheetNumber}", StringComparison.OrdinalIgnoreCase));
6566
}
6667

6768
var rowsUsed = worksheet.RangeUsed()?.RowsUsed();
@@ -142,12 +143,12 @@ public Task<FileContent> DecodeAsync(Stream data, CancellationToken cancellation
142143
}
143144
}
144145

145-
sb.AppendLine(this._config.RowSuffix);
146+
sb.AppendLineNix(this._config.RowSuffix);
146147
}
147148

148149
if (this._config.WithEndOfWorksheetMarker)
149150
{
150-
sb.AppendLine(this._config.EndOfWorksheetMarkerTemplate.Replace("{number}", $"{worksheetNumber}", StringComparison.OrdinalIgnoreCase));
151+
sb.AppendLineNix(this._config.EndOfWorksheetMarkerTemplate.Replace("{number}", $"{worksheetNumber}", StringComparison.OrdinalIgnoreCase));
151152
}
152153

153154
string worksheetContent = sb.ToString().Trim();

service/Core/DataFormats/Office/MsPowerPointDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.Logging;
1414
using Microsoft.KernelMemory.Diagnostics;
1515
using Microsoft.KernelMemory.Pipeline;
16+
using Microsoft.KernelMemory.Text;
1617

1718
namespace Microsoft.KernelMemory.DataFormats.Office;
1819

@@ -99,16 +100,16 @@ public Task<FileContent> DecodeAsync(Stream data, CancellationToken cancellation
99100
// Prepend slide number before the slide text
100101
if (this._config.WithSlideNumber)
101102
{
102-
sb.AppendLine(this._config.SlideNumberTemplate.Replace("{number}", $"{slideNumber}", StringComparison.OrdinalIgnoreCase));
103+
sb.AppendLineNix(this._config.SlideNumberTemplate.Replace("{number}", $"{slideNumber}", StringComparison.OrdinalIgnoreCase));
103104
}
104105

105106
sb.Append(currentSlideContent);
106-
sb.AppendLine();
107+
sb.AppendLineNix();
107108

108109
// Append the end of slide marker
109110
if (this._config.WithEndOfSlideMarker)
110111
{
111-
sb.AppendLine(this._config.EndOfSlideMarkerTemplate.Replace("{number}", $"{slideNumber}", StringComparison.OrdinalIgnoreCase));
112+
sb.AppendLineNix(this._config.EndOfSlideMarkerTemplate.Replace("{number}", $"{slideNumber}", StringComparison.OrdinalIgnoreCase));
112113
}
113114
}
114115

service/Core/DataFormats/Office/MsWordDecoder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.Extensions.Logging;
1313
using Microsoft.KernelMemory.Diagnostics;
1414
using Microsoft.KernelMemory.Pipeline;
15+
using Microsoft.KernelMemory.Text;
1516

1617
namespace Microsoft.KernelMemory.DataFormats.Office;
1718

@@ -85,7 +86,7 @@ public Task<FileContent> DecodeAsync(Stream data, CancellationToken cancellation
8586
pageNumber++;
8687
}
8788

88-
sb.AppendLine(p.InnerText);
89+
sb.AppendLineNix(p.InnerText);
8990
}
9091
}
9192

service/Core/Handlers/SummarizationHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.KernelMemory.Extensions;
1414
using Microsoft.KernelMemory.Pipeline;
1515
using Microsoft.KernelMemory.Prompts;
16+
using Microsoft.KernelMemory.Text;
1617

1718
namespace Microsoft.KernelMemory.Handlers;
1819

@@ -214,7 +215,7 @@ public SummarizationHandler(
214215
newContent.Append(token);
215216
}
216217

217-
newContent.AppendLine();
218+
newContent.AppendLineNix();
218219
}
219220

220221
content = newContent.ToString();

0 commit comments

Comments
 (0)