Skip to content

Commit 17bfcb2

Browse files
committed
Set model in conversation state.
1 parent d911b8d commit 17bfcb2

File tree

19 files changed

+59
-101
lines changed

19 files changed

+59
-101
lines changed

BotSharp.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Global
241241
{631D9C12-86C4-44F0-99C3-D32C0754BF37} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
242242
{4F346DCE-087F-4368-AF88-EE9C720D0E69} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
243243
{298AC787-A104-414C-B114-82BE764FBD9C} = {4F346DCE-087F-4368-AF88-EE9C720D0E69}
244+
{5CD330E1-9E5A-4112-8346-6E31CA98EF78} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
244245
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C} = {5CD330E1-9E5A-4112-8346-6E31CA98EF78}
245246
EndGlobalSection
246247
GlobalSection(ExtensibilityGlobals) = postSolution

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<LangVersion>10.0</LangVersion>
44
<OutputPath>..\..\..\packages</OutputPath>
5-
<PackageVersion>0.12.0</PackageVersion>
5+
<PackageVersion>0.12.1</PackageVersion>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
</PropertyGroup>
88
</Project>

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public interface IConversationService
44
{
55
IConversationStateService States { get; }
66
Task<Conversation> NewConversation(Conversation conversation);
7-
void SetConversationId(string conversationId, string channel);
7+
void SetConversationId(string conversationId, List<string> states);
88
Task<Conversation> GetConversation(string id);
99
Task<List<Conversation>> GetConversations();
1010
Task DeleteConversation(string id);

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace BotSharp.Abstraction.Conversations;
66
public interface IConversationStateService
77
{
88
ConversationState Load(string conversationId);
9-
string GetState(string name);
9+
string GetState(string name, string defaultValue = "");
1010
ConversationState GetStates();
1111
void SetState(string name, string value);
1212
void CleanState();

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ public class RoleDialogModel
1111
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
1212
public string Content { get; set; }
1313
public string CurrentAgentId { get; set; }
14-
public string ModelName { get; set; } = "gpt-3.5-turbo";
15-
public float Temperature { get; set; } = 0.5f;
16-
public float SamplingFactor { get; set; } = 0.5f;
1714

1815
/// <summary>
1916
/// Function name if LLM response function call
@@ -43,11 +40,6 @@ public class RoleDialogModel
4340
/// </summary>
4441
public bool StopCompletion { get; set; }
4542

46-
/// <summary>
47-
/// Channel name
48-
/// </summary>
49-
public string Channel { get; set; }
50-
5143
public RoleDialogModel(string role, string text)
5244
{
5345
Role = role;

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using BotSharp.Abstraction.Agents.Enums;
22
using BotSharp.Abstraction.Agents.Models;
3-
using BotSharp.Abstraction.Conversations.Models;
4-
using BotSharp.Abstraction.MLTasks;
53
using BotSharp.Abstraction.Templating;
64

75
namespace BotSharp.Core.Conversations.Services;
@@ -16,7 +14,7 @@ private async Task<bool> GetChatCompletionsAsyncRecursively(Agent agent,
1614
Func<RoleDialogModel, Task> onFunctionExecuting,
1715
Func<RoleDialogModel, Task> onFunctionExecuted)
1816
{
19-
var chatCompletion = CompletionProvider.GetChatCompletion(_services, wholeDialogs.Last().ModelName);
17+
var chatCompletion = CompletionProvider.GetChatCompletion(_services);
2018

2119
currentRecursiveDepth++;
2220
if (currentRecursiveDepth > _settings.MaxRecursiveDepth)
@@ -32,10 +30,7 @@ private async Task<bool> GetChatCompletionsAsyncRecursively(Agent agent,
3230

3331
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, text)
3432
{
35-
CurrentAgentId = agent.Id,
36-
Channel = wholeDialogs.Last().Channel,
37-
Temperature = wholeDialogs.Last().Temperature,
38-
SamplingFactor = wholeDialogs.Last().SamplingFactor
33+
CurrentAgentId = agent.Id
3934
}, onMessageReceived);
4035

4136
return false;
@@ -55,10 +50,7 @@ private async Task<bool> GetChatCompletionsAsyncRecursively(Agent agent,
5550
{
5651
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, fn.Content)
5752
{
58-
CurrentAgentId = fn.CurrentAgentId,
59-
Channel = fn.Channel,
60-
Temperature = fn.Temperature,
61-
SamplingFactor = fn.SamplingFactor
53+
CurrentAgentId = fn.CurrentAgentId
6254
}, onMessageReceived);
6355

6456
return;
@@ -68,11 +60,8 @@ private async Task<bool> GetChatCompletionsAsyncRecursively(Agent agent,
6860
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, fn.Content)
6961
{
7062
CurrentAgentId = fn.CurrentAgentId,
71-
Channel = fn.Channel,
7263
ExecutionData = fn.ExecutionData,
73-
ExecutionResult = fn.ExecutionResult,
74-
Temperature = fn.Temperature,
75-
SamplingFactor = fn.SamplingFactor
64+
ExecutionResult = fn.ExecutionResult
7665
}, onMessageReceived);
7766

7867
return;
@@ -106,10 +95,7 @@ await GetChatCompletionsAsyncRecursively(agent,
10695
{
10796
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, response)
10897
{
109-
CurrentAgentId = agent.Id,
110-
Channel = wholeDialogs.Last().Channel,
111-
Temperature = wholeDialogs.Last().Temperature,
112-
SamplingFactor = wholeDialogs.Last().SamplingFactor
98+
CurrentAgentId = agent.Id
11399
}, onMessageReceived);
114100

115101
return;

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ public async Task<bool> SendMessage(string agentId,
4343
{
4444
var message = new RoleDialogModel(AgentRole.Assistant, lastDialog.Content)
4545
{
46-
CurrentAgentId = agent.Id,
47-
Channel = lastDialog.Channel,
48-
Temperature = lastDialog.Temperature,
49-
SamplingFactor = lastDialog.SamplingFactor
46+
CurrentAgentId = agent.Id
5047
};
5148
await onMessageReceived(message);
5249
_storage.Append(_conversationId, message);
@@ -65,10 +62,7 @@ public async Task<bool> SendMessage(string agentId,
6562
{
6663
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, reasonedContext.Content)
6764
{
68-
CurrentAgentId = agent.Id,
69-
Channel = lastDialog.Channel,
70-
Temperature = lastDialog.Temperature,
71-
SamplingFactor = lastDialog.SamplingFactor
65+
CurrentAgentId = agent.Id
7266
}, onMessageReceived);
7367

7468
return true;
@@ -77,10 +71,7 @@ public async Task<bool> SendMessage(string agentId,
7771
{
7872
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, reasonedContext.Content)
7973
{
80-
CurrentAgentId = agent.Id,
81-
Channel = lastDialog.Channel,
82-
Temperature = lastDialog.Temperature,
83-
SamplingFactor = lastDialog.SamplingFactor
74+
CurrentAgentId = agent.Id
8475
}, onMessageReceived);
8576

8677
return true;

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Repositories;
2-
using BotSharp.Abstraction.Repositories.Records;
32

43
namespace BotSharp.Core.Conversations.Services;
54

@@ -82,10 +81,10 @@ public List<RoleDialogModel> GetDialogHistory(int lastCount = 20)
8281
.ToList();
8382
}
8483

85-
public void SetConversationId(string conversationId, string channel)
84+
public void SetConversationId(string conversationId, List<string> states)
8685
{
8786
_conversationId = conversationId;
8887
_state.Load(_conversationId);
89-
_state.SetState("channel", channel);
88+
states.ForEach(x => _state.SetState(x.Split('=')[0], x.Split('=')[1]));
9089
}
9190
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ private string GetStorageFile(string conversationId)
112112
public ConversationState GetStates()
113113
=> _states;
114114

115-
public string GetState(string name)
115+
public string GetState(string name, string defaultValue = "")
116116
{
117117
if (!_states.ContainsKey(name))
118118
{
119-
_states[name] = "";
119+
_states[name] = defaultValue ?? "";
120120
}
121121
return _states[name];
122122
}

src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ namespace BotSharp.Core.Infrastructures;
44

55
public class CompletionProvider
66
{
7-
public static IChatCompletion GetChatCompletion(IServiceProvider services, string modelName = "gpt-3.5-turbo")
7+
public static IChatCompletion GetChatCompletion(IServiceProvider services)
88
{
99
var completions = services.GetServices<IChatCompletion>();
10-
var settings = services.GetRequiredService<ConversationSetting>();
10+
// var settings = services.GetRequiredService<ConversationSetting>();
1111
// completions.FirstOrDefault(x => x.GetType().FullName.EndsWith(settings.ChatCompletion));
12-
return completions.FirstOrDefault(x => x.ModelName == modelName);
12+
var state = services.GetRequiredService<IConversationStateService>();
13+
var model = state.GetState("model", "gpt-3.5-turbo");
14+
return completions.FirstOrDefault(x => x.ModelName == model);
1315
}
1416
}

0 commit comments

Comments
 (0)