Skip to content

Commit

Permalink
Rename TextContent to GeneratedTextContent; Undo package upgrades; Fi…
Browse files Browse the repository at this point in the history
…x typos
  • Loading branch information
dluc committed Dec 19, 2024
1 parent 3410d1c commit 25222c1
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 68 deletions.
5 changes: 2 additions & 3 deletions examples/104-dotnet-custom-LLM/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Runtime.CompilerServices;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.AI;
using Microsoft.KernelMemory.Models;

public static class Program
{
Expand Down Expand Up @@ -69,7 +68,7 @@ public IReadOnlyList<string> GetTokens(string text)
}

/// <inheritdoc />
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand All @@ -79,6 +78,6 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
// Remove this
await Task.Delay(0, cancellationToken).ConfigureAwait(false);

yield return new("some text");
yield return "some text";
}
}
5 changes: 2 additions & 3 deletions extensions/Anthropic/AnthropicTextGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.KernelMemory.AI.Anthropic.Client;
using Microsoft.KernelMemory.Context;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;

namespace Microsoft.KernelMemory.AI.Anthropic;

Expand Down Expand Up @@ -98,7 +97,7 @@ public IReadOnlyList<string> GetTokens(string text)
}

/// <inheritdoc />
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand All @@ -121,7 +120,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
switch (response)
{
case ContentBlockDelta blockDelta:
yield return new(blockDelta.Delta.Text);
yield return blockDelta.Delta.Text;
break;

default:
Expand Down
13 changes: 6 additions & 7 deletions extensions/AzureOpenAI/AzureOpenAI/AzureOpenAITextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.AI.AzureOpenAI.Internals;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using OpenAI.Chat;
Expand Down Expand Up @@ -120,7 +119,7 @@ public IReadOnlyList<string> GetTokens(string text)
}

/// <inheritdoc/>
public async IAsyncEnumerable<Models.TextContent> GenerateTextAsync(
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -157,15 +156,15 @@ public IReadOnlyList<string> GetTokens(string text)
throw new AzureOpenAIException(e.Message, e, isTransient: e.StatusCode.IsTransientError());
}

await foreach (var x in result.WithCancellation(cancellationToken))
await foreach (StreamingTextContent x in result.WithCancellation(cancellationToken))
{
TokenUsage? tokenUsage = null;

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

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

// NOTE: as stated at https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices,
// 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
// 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
// x.Text is null, but tokenUsage is not (token usage statistics for the entire request are included in the last chunk).
if (x.Text is null && tokenUsage is null) { continue; }

Expand Down
16 changes: 4 additions & 12 deletions extensions/LlamaSharp/LlamaSharp/LlamaSharpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using LLama;
using LLama.Common;
using LLama.Native;
using LLama.Sampling;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;

namespace Microsoft.KernelMemory.AI.LlamaSharp;

Expand Down Expand Up @@ -77,18 +74,18 @@ public IReadOnlyList<string> GetTokens(string text)
}

/// <inheritdoc/>
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
public IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{
var executor = new InteractiveExecutor(this._context);

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

var samplingPipeline = new DefaultSamplingPipeline()
var samplingPipeline = new DefaultSamplingPipeline
{
Temperature = (float)options.Temperature,
TopP = (float)options.NucleusSampling,
Expand All @@ -106,12 +103,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
};

this._log.LogTrace("Generating text, temperature {0}, max tokens {1}", samplingPipeline.Temperature, settings.MaxTokens);

IAsyncEnumerable<string> streamingResponse = executor.InferAsync(prompt, settings, cancellationToken);
await foreach (var x in streamingResponse.ConfigureAwait(false))
{
yield return new(x);
}
return executor.InferAsync(prompt, settings, cancellationToken).Select(x => new GeneratedTextContent(x));
}

/// <inheritdoc/>
Expand Down
5 changes: 2 additions & 3 deletions extensions/ONNX/Onnx/OnnxTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;
using Microsoft.ML.OnnxRuntimeGenAI;
using static Microsoft.KernelMemory.OnnxConfig;

Expand Down Expand Up @@ -86,7 +85,7 @@ public OnnxTextGenerator(
}

/// <inheritdoc/>
public async IAsyncEnumerable<TextContent> GenerateTextAsync(
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -163,7 +162,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
if (outputTokens.Count > 0 && this._tokenizer != null)
{
var newToken = outputTokens[^1];
yield return new(this._tokenizer.Decode([newToken]));
yield return this._tokenizer.Decode([newToken]);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions extensions/Ollama/Ollama/OllamaTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.Context;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;
using OllamaSharp;
using OllamaSharp.Models;

Expand Down Expand Up @@ -92,7 +91,7 @@ public IReadOnlyList<string> GetTokens(string text)
return this._textTokenizer.GetTokens(text);
}

public async IAsyncEnumerable<TextContent> GenerateTextAsync(
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -147,7 +146,7 @@ public async IAsyncEnumerable<TextContent> GenerateTextAsync(
IAsyncEnumerable<string?> stream = chat.SendAsync(prompt, cancellationToken);
await foreach (string? token in stream.ConfigureAwait(false))
{
if (token != null) { yield return new(token); }
if (token != null) { yield return token; }
}
}
}
3 changes: 1 addition & 2 deletions extensions/OpenAI/OpenAI/OpenAITextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.AI.OpenAI.Internals;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using OpenAI;
Expand Down Expand Up @@ -125,7 +124,7 @@ public IReadOnlyList<string> GetTokens(string text)
}

/// <inheritdoc/>
public async IAsyncEnumerable<Models.TextContent> GenerateTextAsync(
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand Down
3 changes: 1 addition & 2 deletions service/Abstractions/AI/ITextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Collections.Generic;
using System.Threading;
using Microsoft.KernelMemory.Models;

namespace Microsoft.KernelMemory.AI;

Expand All @@ -20,7 +19,7 @@ public interface ITextGenerator : ITextTokenizer
/// <param name="options">Options for the LLM request</param>
/// <param name="cancellationToken">Async task cancellation token</param>
/// <returns>Text generated, returned as a stream of strings/tokens</returns>
public IAsyncEnumerable<TextContent> GenerateTextAsync(
public IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
CancellationToken cancellationToken = default);
Expand Down
31 changes: 31 additions & 0 deletions service/Abstractions/Models/GeneratedTextContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.KernelMemory;

public class GeneratedTextContent
{
public string Text { get; set; }

public TokenUsage? TokenUsage { get; set; }

public GeneratedTextContent(string text, TokenUsage? tokenUsage = null)
{
this.Text = text;
this.TokenUsage = tokenUsage;
}

/// <inheritdoc/>
public override string ToString()
{
return this.Text;
}

/// <summary>
/// Convert a string to an instance of GeneratedTextContent
/// </summary>
/// <param name="text">Text content</param>
public static implicit operator GeneratedTextContent(string text)

Check failure on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Build (8.0.x, ubuntu-latest, Debug)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check failure on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Build (8.0.x, ubuntu-latest, Debug)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check failure on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Build (8.0.x, ubuntu-latest, Release)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check failure on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Build (8.0.x, ubuntu-latest, Release)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check warning on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (8.0.x, ubuntu-latest)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check warning on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (8.0.x, ubuntu-latest)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check warning on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (8.0.x, windows-latest)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check warning on line 27 in service/Abstractions/Models/GeneratedTextContent.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (8.0.x, windows-latest)

Provide a method named 'ToGeneratedTextContent' or 'FromString' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)
{
return new GeneratedTextContent(text);
}
}
3 changes: 1 addition & 2 deletions service/Abstractions/Models/MemoryAnswer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.KernelMemory.Models;

namespace Microsoft.KernelMemory;

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

/// <summary>
/// List of the relevant sources used to produce the answer.
Expand Down
16 changes: 0 additions & 16 deletions service/Abstractions/Models/TextContent.cs

This file was deleted.

6 changes: 3 additions & 3 deletions service/Abstractions/Models/TokenUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Text.Json.Serialization;

namespace Microsoft.KernelMemory.Models;
namespace Microsoft.KernelMemory;

/// <summary>
/// Represents the usage of tokens in a request and response cycle.
Expand All @@ -22,13 +22,13 @@ public class TokenUsage
/// The number of tokens in the request message input, spanning all message content items, measured by the tokenizer.
/// </summary>
[JsonPropertyName("tokenizer_tokens_in")]
public int TokeninzerTokensIn { get; set; }
public int TokenizerTokensIn { get; set; }

/// <summary>
/// The combined number of output tokens in the generated completion, measured by the tokenizer.
/// </summary>
[JsonPropertyName("tokenizer_tokens_out")]
public int TokeninzerTokensOut { get; set; }
public int TokenizerTokensOut { get; set; }

/// <summary>
/// The number of tokens in the request message input, spanning all message content items, measured by the service.
Expand Down
3 changes: 1 addition & 2 deletions service/Core/AI/NoTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;

namespace Microsoft.KernelMemory.AI;

Expand Down Expand Up @@ -38,7 +37,7 @@ public IReadOnlyList<string> GetTokens(string text)
}

/// <inheritdoc />
public IAsyncEnumerable<TextContent> GenerateTextAsync(string prompt, TextGenerationOptions options, CancellationToken cancellationToken = default)
public IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(string prompt, TextGenerationOptions options, CancellationToken cancellationToken = default)
{
throw this.Error();
}
Expand Down
7 changes: 3 additions & 4 deletions service/Core/Search/AnswerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.KernelMemory.AI;
using Microsoft.KernelMemory.Context;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.Models;
using Microsoft.KernelMemory.Prompts;

namespace Microsoft.KernelMemory.Search;
Expand Down Expand Up @@ -76,7 +75,7 @@ internal async IAsyncEnumerable<MemoryAnswer> GenerateAnswerAsync(
{
Timestamp = DateTimeOffset.UtcNow,
ModelType = Constants.ModelType.TextGeneration,
TokeninzerTokensIn = this._textGenerator.CountTokens(prompt)
TokenizerTokensIn = this._textGenerator.CountTokens(prompt)
};

result.AskResult.TokenUsage.Add(tokenUsage);
Expand All @@ -100,7 +99,7 @@ internal async IAsyncEnumerable<MemoryAnswer> GenerateAnswerAsync(

// Finalize the answer, checking if it's empty
result.AskResult.Result = completeAnswer.ToString();
tokenUsage.TokeninzerTokensOut = this._textGenerator.CountTokens(result.AskResult.Result);
tokenUsage.TokenizerTokensOut = this._textGenerator.CountTokens(result.AskResult.Result);

if (string.IsNullOrWhiteSpace(result.AskResult.Result)
|| ValueIsEquivalentTo(result.AskResult.Result, this._config.EmptyAnswer))
Expand Down Expand Up @@ -138,7 +137,7 @@ private string CreatePrompt(string question, string facts, IContext? context)
return prompt;
}

private IAsyncEnumerable<TextContent> GenerateAnswerTokensAsync(string prompt, IContext? context, CancellationToken cancellationToken)
private IAsyncEnumerable<GeneratedTextContent> GenerateAnswerTokensAsync(string prompt, IContext? context, CancellationToken cancellationToken)
{
int maxTokens = context.GetCustomRagMaxTokensOrDefault(this._config.AnswerTokens);
double temperature = context.GetCustomRagTemperatureOrDefault(this._config.Temperature);
Expand Down
2 changes: 1 addition & 1 deletion service/Core/SemanticKernel/SemanticKernelTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SemanticKernelTextGenerator(
}

/// <inheritdoc />
public async IAsyncEnumerable<Models.TextContent> GenerateTextAsync(
public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand Down
Loading

0 comments on commit 25222c1

Please sign in to comment.