Skip to content

Commit 5df6982

Browse files
committed
Add ChatBot module and modify HttpClientExtensions
Added a new project "OpenAI.ChatGPT.Modules.ChatBot" to the solution, by modifying the OpenAI_DotNet.sln file. Introduced in this module a new model, 'ChatGptBot.cs', as a simple Bot API, which helps users to manage chat responses without needing to construct or manage sessions or history of chat. Changes involved in 'HttpClientExtensions.cs' related to generator breaking down after completion. It ensures on the fly HTTP call does not stick, if the collection completed enumeration. Also, made a slight adjustment in module 'ChatGPT.cs' by changing comments and interface, now sending userId as parameter to the 'CreateInMemoryChat' method was needed for reference in InMemoryChat History. This change guarantees that user interaction is referenced and regulated correctly. Lastly, file named 'OpenAiClientExtensions.cs' has been renamed to 'OpenAiClientExtensions.Translations.cs' to more correctly represent its functionality.
1 parent 6710473 commit 5df6982

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using OpenAI.ChatGpt;
2+
using OpenAI.ChatGpt.Interfaces;
3+
using OpenAI.ChatGpt.Internal;
4+
using OpenAI.ChatGpt.Models;
5+
6+
namespace OpenAI.ChatGPT.Modules.ChatBot;
7+
8+
internal class ChatGptBot
9+
{
10+
private readonly string? _initialMessage;
11+
private readonly int? _maxTokens;
12+
private readonly string? _model;
13+
private readonly float? _temperature;
14+
private readonly ITimeProvider _timeProvider;
15+
private readonly IChatHistoryStorage _chatHistoryStorage;
16+
private readonly OpenAiClient _client;
17+
18+
public ChatGptBot(
19+
string openAiKey,
20+
string? initialMessage = null,
21+
int? maxTokens = null,
22+
string? model = null,
23+
float? temperature = null,
24+
string? host = null,
25+
IChatHistoryStorage? chatHistoryStorage = null,
26+
ITimeProvider? timeProvider = null)
27+
{
28+
_initialMessage = initialMessage;
29+
_maxTokens = maxTokens;
30+
_model = model;
31+
_temperature = temperature;
32+
_timeProvider = timeProvider ?? new TimeProviderUtc();
33+
_chatHistoryStorage = chatHistoryStorage ?? new InMemoryChatHistoryStorage();
34+
_client = new OpenAiClient(openAiKey, host);
35+
}
36+
37+
public async Task<string> GetResponse(string message, string userId, CancellationToken cancellationToken = default)
38+
{
39+
ArgumentNullException.ThrowIfNull(message);
40+
ArgumentNullException.ThrowIfNull(userId);
41+
var chatGpt = new ChatGpt.ChatGPT(_client, _chatHistoryStorage, _timeProvider, userId, new ChatGPTConfig()
42+
{
43+
InitialSystemMessage = _initialMessage,
44+
MaxTokens = _maxTokens,
45+
Model = _model,
46+
Temperature = _temperature
47+
});
48+
49+
var service = await chatGpt.ContinueOrStartNewTopic(cancellationToken);
50+
//var messages = await service.GetMessages(cancellationToken);
51+
//messages.First().CalculateApproxTotalTokenCount();
52+
var response = await service.GetNextMessageResponse(message, cancellationToken);
53+
return response;
54+
}
55+
}

OpenAI_DotNet.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Stru
3333
EndProject
3434
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "modules", "modules", "{068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8}"
3535
EndProject
36+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGPT.Modules.ChatBot", "OpenAI.ChatGPT.Modules.ChatBot\OpenAI.ChatGPT.Modules.ChatBot.csproj", "{18DF620B-CEE4-4DDB-8C70-A2BF95026E14}"
37+
EndProject
3638
Global
3739
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3840
Debug|Any CPU = Debug|Any CPU
@@ -87,6 +89,10 @@ Global
8789
{F2968A66-5672-439E-823E-D35100CA067D}.Debug|Any CPU.Build.0 = Debug|Any CPU
8890
{F2968A66-5672-439E-823E-D35100CA067D}.Release|Any CPU.ActiveCfg = Release|Any CPU
8991
{F2968A66-5672-439E-823E-D35100CA067D}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Release|Any CPU.Build.0 = Release|Any CPU
9096
EndGlobalSection
9197
GlobalSection(SolutionProperties) = preSolution
9298
HideSolutionNode = FALSE
@@ -101,5 +107,6 @@ Global
101107
{E303F270-6091-47DE-9260-DAD6122005A7} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB}
102108
{F2968A66-5672-439E-823E-D35100CA067D} = {068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8}
103109
{E155D31C-0061-40A3-AD54-93B5DD08836B} = {068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8}
110+
{18DF620B-CEE4-4DDB-8C70-A2BF95026E14} = {068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8}
104111
EndGlobalSection
105112
EndGlobal

src/OpenAI.ChatGpt/ChatGPT.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,20 @@ public ChatGPT(
7272
}
7373

7474
/// <summary>
75-
/// If you don't have users and don't want to save messages into database use this method.
75+
/// If you don't want to save messages into database use this method.
7676
/// </summary>
7777
public static Task<ChatService> CreateInMemoryChat(
7878
string apiKey,
7979
ChatGPTConfig? config = null,
8080
UserOrSystemMessage? initialDialog = null,
8181
ITimeProvider? clock = null,
8282
string? host = null,
83+
string? userId = null,
8384
CancellationToken cancellationToken = default)
8485
{
8586
ArgumentNullException.ThrowIfNull(apiKey);
8687
var chatGpt = new ChatGPT(
87-
apiKey, new InMemoryChatHistoryStorage(), clock ?? new TimeProviderUtc(), null, config, host);
88+
apiKey, new InMemoryChatHistoryStorage(), clock ?? new TimeProviderUtc(), userId, config, host);
8889
return chatGpt.StartNewTopic(initialDialog: initialDialog, cancellationToken: cancellationToken);
8990
}
9091

src/OpenAI.ChatGpt/HttpClientExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ internal static async IAsyncEnumerable<TResponse>
6262
}
6363
}
6464

65+
yield break;
66+
6567

6668
Task<HttpResponseMessage> SendAsync()
6769
{
File renamed without changes.

0 commit comments

Comments
 (0)