From f90024c3a77cd167905ef41a7c6cf9b9f562cd14 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Wed, 17 Jan 2024 11:44:56 +0100 Subject: [PATCH] Documentation update #139 --- README.md | 23 ++++++ .../ChatGptServiceCollectionExtensions.md | 2 +- .../AddChatGpt.md | 75 +++++++++++++++++-- docs/README.md | 2 +- samples/ChatGptConsole/ChatGptConsole.csproj | 2 +- 5 files changed, 94 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 571c5c6..e2c7c6c 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,29 @@ builder.Services.AddChatGpt((services, options) => }); ``` +### Configuring HTTP Client + +**ChatGptNet** uses an [HttpClient](https://docs.microsoft.com/dotnet/api/system.net.http.httpclient) to call the chat completion and embedding APIs. If you need to customize it, you can use the overload of the **AddChatGpt** method that accepts an [Action<IHttpClientBuiler>](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.ihttpclientbuilder) as argument. For example, if you want to add resiliency to the HTTP client (let's say a retry policy), you can use [Polly](https://github.com/App-vNext/Polly): + +```csharp +// using Microsoft.Extensions.DependencyInjection; +// Requires: Microsoft.Extensions.Http.Resilience + +builder.Services.AddChatGpt(context.Configuration, + httpClient => + { + // Configures retry policy on the inner HttpClient using Polly. + httpClient.AddStandardResilienceHandler(options => + { + options.AttemptTimeout.Timeout = TimeSpan.FromMinutes(1); + options.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(3); + options.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(3); + }); + }) +``` + +More information about this topic is available on the [official documentation](https://learn.microsoft.com/dotnet/core/resilience/http-resilience). + ## Usage The library can be used in any .NET application built with .NET 6.0 or later. For example, we can create a Minimal API in this way: diff --git a/docs/ChatGptNet/ChatGptServiceCollectionExtensions.md b/docs/ChatGptNet/ChatGptServiceCollectionExtensions.md index 084e54f..32538dd 100644 --- a/docs/ChatGptNet/ChatGptServiceCollectionExtensions.md +++ b/docs/ChatGptNet/ChatGptServiceCollectionExtensions.md @@ -10,7 +10,7 @@ public static class ChatGptServiceCollectionExtensions | name | description | | --- | --- | -| static [AddChatGpt](ChatGptServiceCollectionExtensions/AddChatGpt.md)(…) | Registers a ChatGptClient instance with the specified options. (3 methods) | +| static [AddChatGpt](ChatGptServiceCollectionExtensions/AddChatGpt.md)(…) | Registers a ChatGptClient instance with the specified options. (5 methods) | ## See Also diff --git a/docs/ChatGptNet/ChatGptServiceCollectionExtensions/AddChatGpt.md b/docs/ChatGptNet/ChatGptServiceCollectionExtensions/AddChatGpt.md index 390063b..200e272 100644 --- a/docs/ChatGptNet/ChatGptServiceCollectionExtensions/AddChatGpt.md +++ b/docs/ChatGptNet/ChatGptServiceCollectionExtensions/AddChatGpt.md @@ -1,16 +1,17 @@ -# ChatGptServiceCollectionExtensions.AddChatGpt method (1 of 3) +# ChatGptServiceCollectionExtensions.AddChatGpt method (1 of 5) Registers a ChatGptClient instance with the specified options. ```csharp public static IChatGptBuilder AddChatGpt(this IServiceCollection services, - Action builder) + Action builder, Action? httpClientBuilder = null) ``` | parameter | description | | --- | --- | | services | The IServiceCollection to add services to. | | builder | The [`ChatGptOptionsBuilder`](../ChatGptOptionsBuilder.md) to configure options. | +| httpClientBuilder | The IHttpClientBuilder to configure the HTTP client used to make HTTP requests. | ## Return Value @@ -29,19 +30,21 @@ This method automatically adds a MemoryCache that is used to save conversation h --- -# ChatGptServiceCollectionExtensions.AddChatGpt method (2 of 3) +# ChatGptServiceCollectionExtensions.AddChatGpt method (2 of 5) Registers a ChatGptClient instance using dynamic options. ```csharp public static IChatGptBuilder AddChatGpt(this IServiceCollection services, - Action builder) + Action builder, + Action? httpClientBuilder = null) ``` | parameter | description | | --- | --- | | services | The IServiceCollection to add services to. | | builder | The [`ChatGptOptionsBuilder`](../ChatGptOptionsBuilder.md) to configure options. | +| httpClientBuilder | The IHttpClientBuilder to configure the HTTP client used to make HTTP requests. | ## Return Value @@ -61,20 +64,78 @@ Use this this method if it is necessary to dynamically set options (for example, --- -# ChatGptServiceCollectionExtensions.AddChatGpt method (3 of 3) +# ChatGptServiceCollectionExtensions.AddChatGpt method (3 of 5) + +Registers a ChatGptClient instance reading configuration from the specified IConfiguration source, searching for the ChatGPT section. + +```csharp +public static IChatGptBuilder AddChatGpt(this IServiceCollection services, + IConfiguration configuration, Action? httpClientBuilder = null) +``` + +| parameter | description | +| --- | --- | +| services | The IServiceCollection to add services to. | +| configuration | The IConfiguration being bound. | +| httpClientBuilder | The IHttpClientBuilder to configure the HTTP client used to make HTTP requests. | + +## Remarks + +This method automatically adds a MemoryCache that is used to save conversation history for chat completion. It is possibile to use [`WithCache`](../IChatGptBuilderExtensions/WithCache.md) to specify another cache implementation. + +## See Also + +* class [ChatGptOptions](../ChatGptOptions.md) +* interface [IChatGptBuilder](../IChatGptBuilder.md) +* class [ChatGptServiceCollectionExtensions](../ChatGptServiceCollectionExtensions.md) +* namespace [ChatGptNet](../../ChatGptNet.md) + +--- + +# ChatGptServiceCollectionExtensions.AddChatGpt method (4 of 5) + +Registers a ChatGptClient instance reading configuration from the specified IConfiguration source. + +```csharp +public static IChatGptBuilder AddChatGpt(this IServiceCollection services, + IConfiguration configuration, string sectionName) +``` + +| parameter | description | +| --- | --- | +| services | The IServiceCollection to add services to. | +| configuration | The IConfiguration being bound. | +| sectionName | The name of the configuration section that holds ChatGPT settings. | + +## Remarks + +This method automatically adds a MemoryCache that is used to save conversation history for chat completion. It is possibile to use [`WithCache`](../IChatGptBuilderExtensions/WithCache.md) to specify another cache implementation. + +## See Also + +* class [ChatGptOptions](../ChatGptOptions.md) +* interface [IChatGptBuilder](../IChatGptBuilder.md) +* class [ChatGptServiceCollectionExtensions](../ChatGptServiceCollectionExtensions.md) +* namespace [ChatGptNet](../../ChatGptNet.md) + +--- + +# ChatGptServiceCollectionExtensions.AddChatGpt method (5 of 5) Registers a ChatGptClient instance reading configuration from the specified IConfiguration source. ```csharp public static IChatGptBuilder AddChatGpt(this IServiceCollection services, - IConfiguration configuration, string sectionName = "ChatGPT") + IConfiguration configuration, string sectionName, + Action? httpClientBuilder = null) ``` | parameter | description | | --- | --- | | services | The IServiceCollection to add services to. | | configuration | The IConfiguration being bound. | -| sectionName | The name of the configuration section that holds ChatGPT settings (default: ChatGPT). | +| sectionName | The name of the configuration section that holds ChatGPT settings. | +| httpClientBuilder | The IHttpClientBuilder to configure the HTTP client used to make HTTP requests. | ## Return Value diff --git a/docs/README.md b/docs/README.md index 8b26f5e..e2a9ae3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -55,7 +55,7 @@ | class [ChatGptTool](./ChatGptNet.Models/ChatGptTool.md) | Represents a tool that the model may call. | | class [ChatGptToolCall](./ChatGptNet.Models/ChatGptToolCall.md) | A tool call generated by the model, such as a function call. | | static class [ChatGptToolChoices](./ChatGptNet.Models/ChatGptToolChoices.md) | Contains constants for ChatGPT function call types. | -| class [ChatGptToolParameters](./ChatGptNet.Models/ChatGptToolParameters.md) | Contains parameters about the tools calls that are available for ChatGPT. | +| class [ChatGptToolParameters](./ChatGptNet.Models/ChatGptToolParameters.md) | Contains parameters about the tool calls that are available for ChatGPT. | | static class [ChatGptToolTypes](./ChatGptNet.Models/ChatGptToolTypes.md) | Contains constants for ChatGPT tool types. | | class [ChatGptUsage](./ChatGptNet.Models/ChatGptUsage.md) | Contains information about the API usage. | | static class [OpenAIChatGptModels](./ChatGptNet.Models/OpenAIChatGptModels.md) | Contains all the chat completion models that are currently supported by OpenAI. | diff --git a/samples/ChatGptConsole/ChatGptConsole.csproj b/samples/ChatGptConsole/ChatGptConsole.csproj index d74f6bc..3d6f605 100644 --- a/samples/ChatGptConsole/ChatGptConsole.csproj +++ b/samples/ChatGptConsole/ChatGptConsole.csproj @@ -1,4 +1,4 @@ - + Exe