Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions extensions/Elasticsearch/Elasticsearch/ConfigurationException.cs

This file was deleted.

51 changes: 51 additions & 0 deletions service/Core/AI/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.KernelMemory.AI;

// ReSharper disable once CheckNamespace
namespace Microsoft.KernelMemory;

public static partial class KernelMemoryBuilderExtensions
{
/// <summary>
/// Inject a fake embedding generator that will throw an exception if used
/// </summary>
/// <param name="builder">KM builder</param>
public static IKernelMemoryBuilder WithoutEmbeddingGenerator(this IKernelMemoryBuilder builder)
{
builder.Services.AddNoEmbeddingGenerator();
return builder;
}

/// <summary>
/// Inject a fake embedding generator that will throw an exception if used
/// </summary>
/// <param name="builder">KM builder</param>
public static IKernelMemoryBuilder WithoutTextGenerator(this IKernelMemoryBuilder builder)
{
builder.Services.AddNoTextGenerator();
return builder;
}
}

public static partial class DependencyInjection
{
/// <summary>
/// Inject a fake embedding generator that will throw an exception if used
/// </summary>
/// <param name="services">.NET services</param>
public static IServiceCollection AddNoEmbeddingGenerator(this IServiceCollection services)
{
return services.AddSingleton<ITextEmbeddingGenerator, NoEmbeddingGenerator>();
}

/// <summary>
/// Inject a fake text generator that will throw an exception if used
/// </summary>
/// <param name="services">.NET services</param>
public static IServiceCollection AddNoTextGenerator(this IServiceCollection services)
{
return services.AddSingleton<ITextGenerator, NoTextGenerator>();
}
}
43 changes: 43 additions & 0 deletions service/Core/AI/NoEmbeddingGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Microsoft.KernelMemory.AI;

/// <summary>
/// Disabled embedding generator used when using KM without embeddings,
/// e.g. when using the internal orchestration to run jobs that don't require AI.
/// </summary>
public class NoEmbeddingGenerator : ITextEmbeddingGenerator
{
private readonly ILogger<ITextEmbeddingGenerator> _log;

public NoEmbeddingGenerator(ILoggerFactory loggerFactory)
{
this._log = loggerFactory.CreateLogger<ITextEmbeddingGenerator>();
}

/// <inheritdoc />
public int MaxTokens => int.MaxValue;

/// <inheritdoc />
public int CountTokens(string text)
{
throw this.Error();
}

/// <inheritdoc />
public Task<Embedding> GenerateEmbeddingAsync(string text, CancellationToken cancellationToken = default)
{
throw this.Error();
}

private NotImplementedException Error()
{
this._log.LogCritical("The application is attempting to generate embeddings even if embedding generation has been disabled");
return new NotImplementedException("Embedding generation has been disabled");
}
}
43 changes: 43 additions & 0 deletions service/Core/AI/NoTextGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;

namespace Microsoft.KernelMemory.AI;

/// <summary>
/// Disabled text generator used when using KM without AI queries and summaries,
/// e.g. when using the internal orchestration to run jobs that don't require AI.
/// </summary>
public class NoTextGenerator : ITextGenerator
{
private readonly ILogger<ITextGenerator> _log;

public NoTextGenerator(ILoggerFactory loggerFactory)
{
this._log = loggerFactory.CreateLogger<ITextGenerator>();
}

/// <inheritdoc />
public int MaxTokenTotal => int.MaxValue;

/// <inheritdoc />
public int CountTokens(string text)
{
throw this.Error();
}

/// <inheritdoc />
public IAsyncEnumerable<string> GenerateTextAsync(string prompt, TextGenerationOptions options, CancellationToken cancellationToken = default)
{
throw this.Error();
}

private NotImplementedException Error()
{
this._log.LogCritical("The application is attempting to generate text even if text generation has been disabled");
return new NotImplementedException("Text generation has been disabled");
}
}