Skip to content

Commit

Permalink
Many new features (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva authored Mar 29, 2023
2 parents 15f04e1 + e08b9b2 commit 9abbe9f
Show file tree
Hide file tree
Showing 66 changed files with 366 additions and 1,855 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: CodeQL
on:
push:
branches-ignore: [ master ]
paths: [ 'src/**' ]
paths: [ 'src/**', 'samples/**' ]
pull_request:
branches: [ master, develop]
paths: [ 'src/**' ]
paths: [ 'src/**', 'samples/**' ]
workflow_dispatch:
schedule:
- cron: '0 0 1,15 * *'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ on:
push:
branches-ignore: [ master ]
# Remove the line above to run when pushing to master
paths: [ 'src/**' ]
paths: [ 'src/**', 'samples/**' ]
pull_request:
branches: [ master, develop ]
paths: [ 'src/**' ]
paths: [ 'src/**', 'samples/**' ]
workflow_dispatch:

###############
Expand Down
9 changes: 8 additions & 1 deletion ChatGptNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatGptConsole", "samples\C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatGptApi", "samples\ChatGptApi\ChatGptApi.csproj", "{435F71F0-D250-4726-A5C1-EFF5CD3A05F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatGptStreamConsole", "samples\ChatGptStreamConsole\ChatGptStreamConsole.csproj", "{E8254DE1-D0EC-4858-81BB-656089F287CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatGptStreamConsole", "samples\ChatGptStreamConsole\ChatGptStreamConsole.csproj", "{E8254DE1-D0EC-4858-81BB-656089F287CD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatGptBlazor.Wasm", "samples\ChatGptBlazor.Wasm\ChatGptBlazor.Wasm.csproj", "{7C629CAB-98E5-48AF-A884-7662A3D063F7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -41,6 +43,10 @@ Global
{E8254DE1-D0EC-4858-81BB-656089F287CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8254DE1-D0EC-4858-81BB-656089F287CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8254DE1-D0EC-4858-81BB-656089F287CD}.Release|Any CPU.Build.0 = Release|Any CPU
{7C629CAB-98E5-48AF-A884-7662A3D063F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C629CAB-98E5-48AF-A884-7662A3D063F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C629CAB-98E5-48AF-A884-7662A3D063F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C629CAB-98E5-48AF-A884-7662A3D063F7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -49,6 +55,7 @@ Global
{77843D2C-16C4-4020-A69F-4253CB0FABC1} = {74E21338-5D64-4CA4-87E9-0621F93FE6A5}
{435F71F0-D250-4726-A5C1-EFF5CD3A05F9} = {74E21338-5D64-4CA4-87E9-0621F93FE6A5}
{E8254DE1-D0EC-4858-81BB-656089F287CD} = {74E21338-5D64-4CA4-87E9-0621F93FE6A5}
{7C629CAB-98E5-48AF-A884-7662A3D063F7} = {74E21338-5D64-4CA4-87E9-0621F93FE6A5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C0C7E910-6AB2-450F-8C88-810B320AA514}
Expand Down
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,43 @@ ChatGPT is aimed to support conversational scenarios: user can talk to ChatGPT w
* *MessageLimit*: specifies how many messages for each conversation must be saved. When this limit is reached, oldest messages are automatically removed.
* *MessageExpiration*: specifies the time interval used to maintain messages in cache, regardless their count.

The **AddChatGpt** method has an overload that accepts an [IServiceProvider](https://learn.microsoft.com/en-us/dotnet/api/system.iserviceprovider) as argument. It can be used, for example, if we're in a Web API and we need to support scenarios in which every user has a different API key that can be retrieved accessing a database via Dependency Injection.
The configuration can be automatically read from [IConfiguration](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration), using for example a _ChatGPT_ section in the _appsettings.json_ file:

"ChatGPT": {
"ApiKey": "",
"MessageLimit": 20,
"MessageExpiration": "00:30:00",
"DefaultModel": "gpt-3.5-turbo",
"ThrowExceptionOnError": true,
"User": "UserName"
"Organization": "My-Organization",
"DefaultParameters": {
"Temperature": 0.8,
"TopP": 1,
"Choices": 1,
"MaxTokens": 500,
"PresencePenalty": 0,
"FrequencyPenalty": 0
}
}

And then use the corresponding overload of che **AddChatGpt** method:

// Adds ChatGPT service using settings from IConfiguration.
services.AddChatGpt(context.Configuration);

The **AddChatGpt** method has also an overload that accepts an [IServiceProvider](https://learn.microsoft.com/en-us/dotnet/api/system.iserviceprovider) as argument. It can be used, for example, if we're in a Web API and we need to support scenarios in which every user has a different API key that can be retrieved accessing a database via Dependency Injection:

builder.Services.AddChatGpt((services, options) =>
{
var accountService = services.GetRequiredService<IAccountService>();

// Dynamically gets the API key from the service.
var apiKey = "..."

options.ApiKey = apiKey;
});


**Usage**

Expand Down Expand Up @@ -130,3 +166,6 @@ Conversation history is automatically deleted when expiration time (specified by
**Contribute**

The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.

> **Warning**
Remember to work on the **develop** branch, don't use the **master** branch directly. Create Pull Requests targeting **develop**.
28 changes: 19 additions & 9 deletions samples/ChatGptApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});

// Adds ChatGPT service.
builder.Services.AddChatGpt(options =>
{
options.ApiKey = "";
options.MessageLimit = 16; // Default: 10
options.MessageExpiration = TimeSpan.FromMinutes(5); // Default: 1 hour
});
// Adds ChatGPT service with hard-coded settings.
//builder.Services.AddChatGpt(options =>
//{
// options.ApiKey = "";
// options.MessageLimit = 16; // Default: 10
// options.MessageExpiration = TimeSpan.FromMinutes(5); // Default: 1 hour
//});

// Adds ChatGPT service using settings from IConfiguration.
builder.Services.AddChatGpt(builder.Configuration);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
Expand Down Expand Up @@ -99,7 +102,7 @@ await problemDetailsService.WriteAsync(new()
})
.WithOpenApi();

app.MapPost("/api/chat/ask", async (Request request, IChatGptClient chatGptClient) =>
app.MapPost("/api/chat", async (Request request, IChatGptClient chatGptClient) =>
{
var response = await chatGptClient.AskAsync(request.ConversationId, request.Message);
return TypedResults.Ok(response);
Expand All @@ -125,13 +128,20 @@ async IAsyncEnumerable<string> Stream()
})
.WithOpenApi();

app.MapDelete("/api/chat/delete", async (Guid conversationId, IChatGptClient chatGptClient) =>
app.MapDelete("/api/chat/{conversationId:guid}", async (Guid conversationId, IChatGptClient chatGptClient) =>
{
await chatGptClient.DeleteConversationAsync(conversationId);
return TypedResults.NoContent();
})
.WithOpenApi();

app.MapGet("/api/chat/{conversationId:guid}", async (Guid conversationId, IChatGptClient chatGptClient) =>
{
var messagges = await chatGptClient.GetConversationAsync(conversationId);
return TypedResults.Ok(messagges);
})
.WithOpenApi();

app.Run();

public record class Request(Guid ConversationId, string Message);
31 changes: 24 additions & 7 deletions samples/ChatGptApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"ChatGPT": {
"ApiKey": "",
"MessageLimit": 20,
"MessageExpiration": "00:30:00",
"ThrowExceptionOnError": true,
"DefaultModel": "gpt-3.5-turbo"
//"Organization": "My-Organization"
//"User": "UserName",
//"DefaultParameters": {
// "Temperature": 0.8,
// "TopP": 1,
// "Choices": 1,
// "MaxTokens": 500,
// "PresencePenalty": 0,
// "FrequencyPenalty": 0
//}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
12 changes: 0 additions & 12 deletions samples/ChatGptBlazor.Server/App.razor

This file was deleted.

14 changes: 0 additions & 14 deletions samples/ChatGptBlazor.Server/ChatGptBlazor.Server.csproj

This file was deleted.

25 changes: 0 additions & 25 deletions samples/ChatGptBlazor.Server/ChatGptBlazor.Server.sln

This file was deleted.

31 changes: 0 additions & 31 deletions samples/ChatGptBlazor.Server/Components/ChatGpt.razor

This file was deleted.

44 changes: 0 additions & 44 deletions samples/ChatGptBlazor.Server/Components/ChatGptStream.razor

This file was deleted.

12 changes: 0 additions & 12 deletions samples/ChatGptBlazor.Server/Data/WeatherForecast.cs

This file was deleted.

19 changes: 0 additions & 19 deletions samples/ChatGptBlazor.Server/Data/WeatherForecastService.cs

This file was deleted.

18 changes: 0 additions & 18 deletions samples/ChatGptBlazor.Server/Pages/Counter.razor

This file was deleted.

Loading

0 comments on commit 9abbe9f

Please sign in to comment.