Skip to content

Commit

Permalink
Improve registration of IDistributedCache in READMEs (#5480)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Oct 8, 2024
1 parent a7e4f3b commit 331ddb5
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 18 deletions.
7 changes: 1 addition & 6 deletions src/Libraries/Microsoft.Extensions.AI.Abstractions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,12 @@ await client.CompleteAsync("Hello, world!");

```csharp
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Runtime.CompilerServices;

// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddSingleton<IDistributedCache>(
new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())));
builder.Services.AddDistributedMemoryCache();
builder.Services.AddChatClient(b => b
.UseDistributedCache()
.Use(new SampleChatClient(new Uri("http://coolsite.ai"), "my-custom-model")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,17 @@ static int GetPersonAge(string personName) =>
using Azure;
using Azure.AI.Inference;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddSingleton(
new ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!)));
builder.Services.AddSingleton<IDistributedCache>(new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())));
builder.Services.AddDistributedMemoryCache();
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));

builder.Services.AddChatClient(b => b
Expand Down
5 changes: 1 addition & 4 deletions src/Libraries/Microsoft.Extensions.AI.Ollama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,13 @@ foreach (var prompt in new[] { "What is AI?", "What is .NET?", "What is AI?" })

```csharp
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddSingleton<IDistributedCache>(new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())));
builder.Services.AddDistributedMemoryCache();
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));

builder.Services.AddChatClient(b => b
Expand Down
5 changes: 1 addition & 4 deletions src/Libraries/Microsoft.Extensions.AI.OpenAI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,15 @@ foreach (var prompt in new[] { "What is AI?", "What is .NET?", "What is AI?" })

```csharp
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenAI;

// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddSingleton(new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")));
builder.Services.AddSingleton<IDistributedCache>(new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())));
builder.Services.AddDistributedMemoryCache();
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));

builder.Services.AddChatClient(b => b
Expand Down

0 comments on commit 331ddb5

Please sign in to comment.