Skip to content

Commit

Permalink
Add Service Lifetime to cache registration (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva authored Sep 19, 2023
2 parents fdda099 + fc42efb commit b3cd8e5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/ChatGptNet/IChatGptBuilderExtensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class IChatGptBuilderExtensions

| name | description |
| --- | --- |
| static [WithCache<TImplementation>](IChatGptBuilderExtensions/WithCache.md)(…) | Uses a custom cache implementation for conversation handling. |
| static [WithCache<TImplementation>](IChatGptBuilderExtensions/WithCache.md)(…) | Defines a custom cache implementation for conversation handling. |

## See Also

Expand Down
10 changes: 4 additions & 6 deletions docs/ChatGptNet/IChatGptBuilderExtensions/WithCache.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
# IChatGptBuilderExtensions.WithCache<TImplementation> method

Uses a custom cache implementation for conversation handling.
Defines a custom cache implementation for conversation handling.

```csharp
public static IChatGptBuilder WithCache<TImplementation>(this IChatGptBuilder builder)
public static IChatGptBuilder WithCache<TImplementation>(this IChatGptBuilder builder,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TImplementation : class, IChatGptCache
```

| parameter | description |
| --- | --- |
| TImplementation | The implementation of [`IChatGptCache`](../IChatGptCache.md) to use. |
| builder | The [`IChatGptBuilder`](../IChatGptBuilder.md) object to configure. |
| lifetime | The ServiceLifetime of the service. |

## Return Value

The [`IChatGptBuilder`](../IChatGptBuilder.md) to further customize ChatGPT.

## Remarks

*TImplementation* is registered as singleton.

## See Also

* interface [IChatGptBuilder](../IChatGptBuilder.md)
Expand Down
6 changes: 3 additions & 3 deletions src/ChatGptNet/ChatGptServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class ChatGptServiceCollectionExtensions
/// <param name="builder">The <see cref="ChatGptOptionsBuilder"/> to configure options.</param>
/// <returns>A <see cref="IChatGptBuilder"/> that can be used to further customize ChatGPT.</returns>
/// <remarks>This method automatically adds a <see cref="MemoryCache"/> that is used to save conversation history for chat completion.
/// It is possibile to use <see cref="IChatGptBuilderExtensions.WithCache{TImplementation}(IChatGptBuilder)"/> to specify another cache implementation.
/// It is possibile to use <see cref="IChatGptBuilderExtensions.WithCache{TImplementation}(IChatGptBuilder, ServiceLifetime)"/> to specify another cache implementation.
/// </remarks>
/// <seealso cref="ChatGptOptionsBuilder"/>
/// <seealso cref="MemoryCacheServiceCollectionExtensions.AddMemoryCache(IServiceCollection)"/>
Expand Down Expand Up @@ -47,7 +47,7 @@ public static IChatGptBuilder AddChatGpt(this IServiceCollection services, Actio
/// <param name="sectionName">The name of the configuration section that holds ChatGPT settings (default: ChatGPT).</param>
/// <returns>A <see cref="IChatGptBuilder"/> that can be used to further customize ChatGPT.</returns>
/// <remarks>This method automatically adds a <see cref="MemoryCache"/> that is used to save conversation history for chat completion.
/// It is possibile to use <see cref="IChatGptBuilderExtensions.WithCache{TImplementation}(IChatGptBuilder)"/> to specify another cache implementation.
/// It is possibile to use <see cref="IChatGptBuilderExtensions.WithCache{TImplementation}(IChatGptBuilder, ServiceLifetime)"/> to specify another cache implementation.
/// </remarks>
/// <seealso cref="ChatGptOptions"/>
/// <seealso cref="IConfiguration"/>
Expand Down Expand Up @@ -75,7 +75,7 @@ public static IChatGptBuilder AddChatGpt(this IServiceCollection services, IConf
/// <returns>A <see cref="IChatGptBuilder"/> that can be used to further customize ChatGPT.</returns>
/// <remarks>Use this this method if it is necessary to dynamically set options (for example, using other services via dependency injection).
/// This method automatically adds a <see cref="MemoryCache"/> that is used to save conversation history for chat completion.
/// It is possibile to use <see cref="IChatGptBuilderExtensions.WithCache{TImplementation}(IChatGptBuilder)"/> to specify another cache implementation.
/// It is possibile to use <see cref="IChatGptBuilderExtensions.WithCache{TImplementation}(IChatGptBuilder, ServiceLifetime)"/> to specify another cache implementation.
/// </remarks>
/// <seealso cref="ChatGptOptions"/>
/// <seealso cref="IServiceProvider"/>
Expand Down
8 changes: 4 additions & 4 deletions src/ChatGptNet/IChatGptBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ namespace ChatGptNet;
public static class IChatGptBuilderExtensions
{
/// <summary>
/// Uses a custom cache implementation for conversation handling.
/// Defines a custom cache implementation for conversation handling.
/// </summary>
/// <typeparam name="TImplementation">The implementation of <see cref="IChatGptCache"/> to use.</typeparam>
/// <param name="builder">The <see cref="IChatGptBuilder"/> object to configure.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the service.</param>
/// <returns>The <see cref="IChatGptBuilder"/> to further customize ChatGPT.</returns>
/// <remarks><typeparamref name="TImplementation"/> is registered as singleton.</remarks>
/// <seealso cref="IChatGptBuilder"/>
/// <seealso cref="IChatGptCache"/>
public static IChatGptBuilder WithCache<TImplementation>(this IChatGptBuilder builder)
public static IChatGptBuilder WithCache<TImplementation>(this IChatGptBuilder builder, ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TImplementation : class, IChatGptCache
{
ArgumentNullException.ThrowIfNull(builder);

builder.Services.Replace(ServiceDescriptor.Singleton<IChatGptCache, TImplementation>());
builder.Services.Replace(new ServiceDescriptor(typeof(IChatGptCache), typeof(TImplementation), lifetime));

return builder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ChatGptNet/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "2.4",
"version": "2.5",
"publicReleaseRefSpec": [
"^refs/heads/master$" // we release out of master
],
Expand Down

0 comments on commit b3cd8e5

Please sign in to comment.