Skip to content

Commit 6b68eb3

Browse files
committed
Rename TextContent to GeneratedTextContent; Undo package upgrades; Fix typos
1 parent 3410d1c commit 6b68eb3

17 files changed

Lines changed: 62 additions & 67 deletions

File tree

examples/104-dotnet-custom-LLM/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Runtime.CompilerServices;
44
using Microsoft.KernelMemory;
55
using Microsoft.KernelMemory.AI;
6-
using Microsoft.KernelMemory.Models;
76

87
public static class Program
98
{
@@ -69,7 +68,7 @@ public IReadOnlyList<string> GetTokens(string text)
6968
}
7069

7170
/// <inheritdoc />
72-
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
71+
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
7372
string prompt,
7473
TextGenerationOptions options,
7574
[EnumeratorCancellation] CancellationToken cancellationToken = default)
@@ -79,6 +78,6 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
7978
// Remove this
8079
await Task.Delay(0, cancellationToken).ConfigureAwait(false);
8180

82-
yield return new("some text");
81+
yield return "some text";
8382
}
8483
}

extensions/Anthropic/AnthropicTextGeneration.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.KernelMemory.AI.Anthropic.Client;
1111
using Microsoft.KernelMemory.Context;
1212
using Microsoft.KernelMemory.Diagnostics;
13-
using Microsoft.KernelMemory.Models;
1413

1514
namespace Microsoft.KernelMemory.AI.Anthropic;
1615

@@ -98,7 +97,7 @@ public IReadOnlyList<string> GetTokens(string text)
9897
}
9998

10099
/// <inheritdoc />
101-
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
100+
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
102101
string prompt,
103102
TextGenerationOptions options,
104103
[EnumeratorCancellation] CancellationToken cancellationToken = default)
@@ -121,7 +120,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
121120
switch (response)
122121
{
123122
case ContentBlockDelta blockDelta:
124-
yield return new(blockDelta.Delta.Text);
123+
yield return blockDelta.Delta.Text;
125124
break;
126125

127126
default:

extensions/AzureOpenAI/AzureOpenAI/AzureOpenAITextGenerator.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.Extensions.Logging;
1212
using Microsoft.KernelMemory.AI.AzureOpenAI.Internals;
1313
using Microsoft.KernelMemory.Diagnostics;
14-
using Microsoft.KernelMemory.Models;
1514
using Microsoft.SemanticKernel;
1615
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
1716
using OpenAI.Chat;
@@ -120,7 +119,7 @@ public IReadOnlyList<string> GetTokens(string text)
120119
}
121120

122121
/// <inheritdoc/>
123-
public async IAsyncEnumerable<Models.TextContent> GenerateTextAsync(
122+
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
124123
string prompt,
125124
TextGenerationOptions options,
126125
[EnumeratorCancellation] CancellationToken cancellationToken = default)
@@ -161,11 +160,11 @@ public IReadOnlyList<string> GetTokens(string text)
161160
{
162161
TokenUsage? tokenUsage = null;
163162

164-
// The last message in the chunk has the usage metadata.
163+
// The last message includes tokens usage metadata.
165164
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options
166-
if (x.Metadata?["Usage"] is ChatTokenUsage { } usage)
165+
if (x.Metadata?["Usage"] is ChatTokenUsage usage)
167166
{
168-
this._log.LogTrace("Usage report: input tokens {0}, output tokens {1}, output reasoning tokens {2}",
167+
this._log.LogTrace("Usage report: input tokens: {InputTokenCount}, output tokens: {OutputTokenCount}, output reasoning tokens: {ReasoningTokenCount}",
169168
usage.InputTokenCount, usage.OutputTokenCount, usage.OutputTokenDetails?.ReasoningTokenCount ?? 0);
170169

171170
tokenUsage = new TokenUsage
@@ -181,7 +180,7 @@ public IReadOnlyList<string> GetTokens(string text)
181180
}
182181

183182
// NOTE: as stated at https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices,
184-
// The Choice can also be empty for the last chunk if we set stream_options: { "include_usage": true} to get token counts, so it is possible that
183+
// the Choice can also be empty for the last chunk if we set stream_options: { "include_usage": true} to get token counts, so it is possible that
185184
// x.Text is null, but tokenUsage is not (token usage statistics for the entire request are included in the last chunk).
186185
if (x.Text is null && tokenUsage is null) { continue; }
187186

extensions/LlamaSharp/LlamaSharp/LlamaSharpTextGenerator.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
using System.Collections.Generic;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Linq;
7-
using System.Runtime.CompilerServices;
87
using System.Threading;
9-
using System.Threading.Tasks;
108
using LLama;
119
using LLama.Common;
1210
using LLama.Native;
1311
using LLama.Sampling;
1412
using Microsoft.Extensions.Logging;
1513
using Microsoft.KernelMemory.Diagnostics;
16-
using Microsoft.KernelMemory.Models;
1714

1815
namespace Microsoft.KernelMemory.AI.LlamaSharp;
1916

@@ -77,18 +74,18 @@ public IReadOnlyList<string> GetTokens(string text)
7774
}
7875

7976
/// <inheritdoc/>
80-
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
77+
public IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
8178
string prompt,
8279
TextGenerationOptions options,
83-
[EnumeratorCancellation] CancellationToken cancellationToken = default)
80+
CancellationToken cancellationToken = default)
8481
{
8582
var executor = new InteractiveExecutor(this._context);
8683

8784
var logitBias = options.TokenSelectionBiases.Count > 0
8885
? options.TokenSelectionBiases.ToDictionary(pair => (LLamaToken)pair.Key, pair => pair.Value)
8986
: [];
9087

91-
var samplingPipeline = new DefaultSamplingPipeline()
88+
var samplingPipeline = new DefaultSamplingPipeline
9289
{
9390
Temperature = (float)options.Temperature,
9491
TopP = (float)options.NucleusSampling,
@@ -106,12 +103,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
106103
};
107104

108105
this._log.LogTrace("Generating text, temperature {0}, max tokens {1}", samplingPipeline.Temperature, settings.MaxTokens);
109-
110-
IAsyncEnumerable<string> streamingResponse = executor.InferAsync(prompt, settings, cancellationToken);
111-
await foreach (var x in streamingResponse.ConfigureAwait(false))
112-
{
113-
yield return new(x);
114-
}
106+
return executor.InferAsync(prompt, settings, cancellationToken).Select(x => new GeneratedTextContent(x));
115107
}
116108

117109
/// <inheritdoc/>

extensions/ONNX/Onnx/OnnxTextGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Threading.Tasks;
1111
using Microsoft.Extensions.Logging;
1212
using Microsoft.KernelMemory.Diagnostics;
13-
using Microsoft.KernelMemory.Models;
1413
using Microsoft.ML.OnnxRuntimeGenAI;
1514
using static Microsoft.KernelMemory.OnnxConfig;
1615

@@ -86,7 +85,7 @@ public OnnxTextGenerator(
8685
}
8786

8887
/// <inheritdoc/>
89-
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
88+
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
9089
string prompt,
9190
TextGenerationOptions? options = null,
9291
[EnumeratorCancellation] CancellationToken cancellationToken = default)
@@ -163,7 +162,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
163162
if (outputTokens.Count > 0 && this._tokenizer != null)
164163
{
165164
var newToken = outputTokens[^1];
166-
yield return new(this._tokenizer.Decode([newToken]));
165+
yield return this._tokenizer.Decode([newToken]);
167166
}
168167
}
169168
}

extensions/Ollama/Ollama/OllamaTextGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.Extensions.Logging;
1010
using Microsoft.KernelMemory.Context;
1111
using Microsoft.KernelMemory.Diagnostics;
12-
using Microsoft.KernelMemory.Models;
1312
using OllamaSharp;
1413
using OllamaSharp.Models;
1514

@@ -92,7 +91,7 @@ public IReadOnlyList<string> GetTokens(string text)
9291
return this._textTokenizer.GetTokens(text);
9392
}
9493

95-
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
94+
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
9695
string prompt,
9796
TextGenerationOptions options,
9897
[EnumeratorCancellation] CancellationToken cancellationToken = default)
@@ -147,7 +146,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
147146
IAsyncEnumerable<string?> stream = chat.SendAsync(prompt, cancellationToken);
148147
await foreach (string? token in stream.ConfigureAwait(false))
149148
{
150-
if (token != null) { yield return new(token); }
149+
if (token != null) { yield return token; }
151150
}
152151
}
153152
}

extensions/OpenAI/OpenAI/OpenAITextGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.KernelMemory.AI.OpenAI.Internals;
1212
using Microsoft.KernelMemory.Diagnostics;
13-
using Microsoft.KernelMemory.Models;
1413
using Microsoft.SemanticKernel;
1514
using Microsoft.SemanticKernel.Connectors.OpenAI;
1615
using OpenAI;
@@ -125,7 +124,7 @@ public IReadOnlyList<string> GetTokens(string text)
125124
}
126125

127126
/// <inheritdoc/>
128-
public async IAsyncEnumerable<Models.TextContent> GenerateTextAsync(
127+
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
129128
string prompt,
130129
TextGenerationOptions options,
131130
[EnumeratorCancellation] CancellationToken cancellationToken = default)

service/Abstractions/AI/ITextGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using System.Collections.Generic;
44
using System.Threading;
5-
using Microsoft.KernelMemory.Models;
65

76
namespace Microsoft.KernelMemory.AI;
87

@@ -20,7 +19,7 @@ public interface ITextGenerator : ITextTokenizer
2019
/// <param name="options">Options for the LLM request</param>
2120
/// <param name="cancellationToken">Async task cancellation token</param>
2221
/// <returns>Text generated, returned as a stream of strings/tokens</returns>
23-
public IAsyncEnumerable<TextContent> GenerateTextAsync(
22+
public IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
2423
string prompt,
2524
TextGenerationOptions options,
2625
CancellationToken cancellationToken = default);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
namespace Microsoft.KernelMemory;
4+
5+
public class GeneratedTextContent
6+
{
7+
public string Text { get; set; }
8+
9+
public TokenUsage? TokenUsage { get; set; }
10+
11+
public GeneratedTextContent(string text, TokenUsage? tokenUsage = null)
12+
{
13+
this.Text = text;
14+
this.TokenUsage = tokenUsage;
15+
}
16+
17+
/// <inheritdoc/>
18+
public override string ToString()
19+
{
20+
return this.Text;
21+
}
22+
23+
/// <summary>
24+
/// Convert a string to an instance of GeneratedTextContent
25+
/// </summary>
26+
/// <param name="text">Text content</param>
27+
public static implicit operator GeneratedTextContent(string text)
28+
{
29+
return new GeneratedTextContent(text);
30+
}
31+
}

service/Abstractions/Models/MemoryAnswer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Text;
77
using System.Text.Json;
88
using System.Text.Json.Serialization;
9-
using Microsoft.KernelMemory.Models;
109

1110
namespace Microsoft.KernelMemory;
1211

@@ -53,7 +52,7 @@ public class MemoryAnswer
5352
/// <remarks>Not all the models and text generators return token usage information.</remarks>
5453
[JsonPropertyName("tokenUsage")]
5554
[JsonPropertyOrder(11)]
56-
public IList<TokenUsage> TokenUsage { get; set; } = [];
55+
public List<TokenUsage> TokenUsage { get; set; } = [];
5756

5857
/// <summary>
5958
/// List of the relevant sources used to produce the answer.

0 commit comments

Comments
 (0)