Skip to content

refine conversation states #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace BotSharp.Abstraction.Conversations;
public interface IConversationStateService
{
string GetConversationId();
ConversationState Load(string conversationId);
Dictionary<string, string> Load(string conversationId);
string GetState(string name, string defaultValue = "");
bool ContainsState(string name);
ConversationState GetStates();
IConversationStateService SetState<T>(string name, T value);
Dictionary<string, string> GetStates();
IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true);
void SaveStateByArgs(JsonDocument args);
void CleanState();
void Save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Conversation
public List<DialogElement> Dialogs { get; set; } = new List<DialogElement>();

[JsonIgnore]
public ConversationState States { get; set; } = new ConversationState();
public Dictionary<string, string> States { get; set; } = new Dictionary<string, string>();

public string Status { get; set; } = ConversationStatus.Open;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
namespace BotSharp.Abstraction.Conversations.Models;

public class ConversationState : Dictionary<string, string>
public class ConversationState : Dictionary<string, List<StateValue>>
{
public ConversationState()
{

}

public ConversationState(List<StateKeyValue> pairs)
{
foreach (var pair in pairs)
{
this[pair.Key] = pair.Value;
this[pair.Key] = pair.Values;
}
}

public List<StateKeyValue> ToKeyValueList()
{
return this.Select(x => new StateKeyValue(x.Key, x.Value)).ToList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ namespace BotSharp.Abstraction.Conversations.Models;
public class StateKeyValue
{
public string Key { get; set; }
public string Value { get; set; }
public List<StateValue> Values { get; set; } = new List<StateValue>();

public StateKeyValue()
{

}

public StateKeyValue(string key, string value)
public StateKeyValue(string key, List<StateValue> values)
{
Key = key;
Value = value;
Values = values;
}
}

public class StateValue
{
public string Data { get; set; }
public DateTime UpdateTime { get; set; }

public StateValue()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class InstructResult : ITrackableMessage
public string MessageId { get; set; }
public string Text { get; set; }
public object Data { get; set; }
public ConversationState States { get; set; }
public Dictionary<string, string> States { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IBotSharpRepository
List<DialogElement> GetConversationDialogs(string conversationId);
void UpdateConversationDialogElements(string conversationId, List<DialogContentUpdateModel> updateElements);
void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs);
List<StateKeyValue> GetConversationStates(string conversationId);
ConversationState GetConversationStates(string conversationId);
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
void UpdateConversationStatus(string conversationId, string status);
Conversation GetConversation(string conversationId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Text.Json.Serialization;

namespace BotSharp.Abstraction.Repositories.Records;

public class ConversationRecord : RecordBase
Expand All @@ -18,9 +16,6 @@ public class ConversationRecord : RecordBase
[JsonIgnore]
public string Dialog { get; set; }

[JsonIgnore]
public List<StateKeyValue> States { get; set; }

[Required]
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BotSharp.Abstraction.Repositories;
using System.IO;
using System.Linq;

namespace BotSharp.Core.Conversations.Services;

Expand All @@ -11,60 +11,87 @@ public class ConversationStateService : IConversationStateService, IDisposable
private readonly ILogger _logger;
private readonly IServiceProvider _services;
private ConversationState _states;
private BotSharpDatabaseSettings _dbSettings;
private string _conversationId;
private readonly IBotSharpRepository _db;
private List<StateKeyValue> _savedStates;

public ConversationStateService(ILogger<ConversationStateService> logger,
IServiceProvider services,
BotSharpDatabaseSettings dbSettings,
IServiceProvider services,
IBotSharpRepository db)
{
_logger = logger;
_services = services;
_dbSettings = dbSettings;
_db = db;
_states = new ConversationState();
}

public string GetConversationId() => _conversationId;

public IConversationStateService SetState<T>(string name, T value)

/// <summary>
/// Set conversation state
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="isNeedVersion">whether the state is related to message or not</param>
/// <returns></returns>
public IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true)
{
if (value == null)
{
return this;
}

var preValue = string.Empty;
var currentValue = value.ToString();
var hooks = _services.GetServices<IConversationHook>();
string preValue = _states.ContainsKey(name) ? _states[name] : "";
if (!_states.ContainsKey(name) || _states[name] != currentValue)

if (_states.TryGetValue(name, out var values))
{
preValue = values?.LastOrDefault()?.Data ?? string.Empty;
}

if (!_states.ContainsKey(name) || preValue != currentValue)
{
_states[name] = currentValue;
_logger.LogInformation($"[STATE] {name} = {value}");
foreach (var hook in hooks)
{
hook.OnStateChanged(name, preValue, currentValue).Wait();
}

var stateValue = new StateValue
{
Data = currentValue,
UpdateTime = DateTime.UtcNow
};

if (!_states.ContainsKey(name) || !isNeedVersion)
{
_states[name] = new List<StateValue> { stateValue };
}
else
{
_states[name].Add(stateValue);
}
}

return this;
}

public ConversationState Load(string conversationId)
public Dictionary<string, string> Load(string conversationId)
{
_conversationId = conversationId;

_savedStates = _db.GetConversationStates(_conversationId).ToList();
_states = _db.GetConversationStates(_conversationId);
var curStates = new Dictionary<string, string>();

if (!_savedStates.IsNullOrEmpty())
if (!_states.IsNullOrEmpty())
{
foreach (var data in _savedStates)
foreach (var state in _states)
{
_states[data.Key] = data.Value;
_logger.LogInformation($"[STATE] {data.Key} : {data.Value}");
var value = state.Value?.LastOrDefault()?.Data ?? string.Empty;
curStates[state.Key] = value;
_logger.LogInformation($"[STATE] {state.Key} : {value}");
}
}

Expand All @@ -75,7 +102,7 @@ public ConversationState Load(string conversationId)
hook.OnStateLoaded(_states).Wait();
}

return _states;
return curStates;
}

public void Save()
Expand All @@ -93,25 +120,32 @@ public void Save()
}

_db.UpdateConversationStates(_conversationId, states);
_logger.LogInformation($"Saved state {_conversationId}");
_logger.LogInformation($"Saved states of conversation {_conversationId}");
}

public void CleanState()
{
//File.Delete(_file);
_states.Clear();
}

public ConversationState GetStates()
=> _states;
public Dictionary<string, string> GetStates()
{
var curStates = new Dictionary<string, string>();
foreach (var state in _states)
{
curStates[state.Key] = state.Value?.LastOrDefault()?.Data ?? string.Empty;
}
return curStates;
}

public string GetState(string name, string defaultValue = "")
{
if (!_states.ContainsKey(name))
if (!_states.ContainsKey(name) || _states[name].IsNullOrEmpty())
{
return defaultValue;
}

return _states[name];
return _states[name].Last().Data;
}

public void Dispose()
Expand All @@ -121,7 +155,9 @@ public void Dispose()

public bool ContainsState(string name)
{
return _states.ContainsKey(name) && !string.IsNullOrEmpty(_states[name]);
return _states.ContainsKey(name)
&& !_states[name].IsNullOrEmpty()
&& !string.IsNullOrEmpty(_states[name].Last().Data);
}

public void SaveStateByArgs(JsonDocument args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public void AddToken(TokenStatsModel stats)
// Accumulated Token
var stat = _services.GetRequiredService<IConversationStateService>();
var inputCount = int.Parse(stat.GetState("prompt_total", "0"));
stat.SetState("prompt_total", stats.PromptCount + inputCount);
stat.SetState("prompt_total", stats.PromptCount + inputCount, false);
var outputCount = int.Parse(stat.GetState("completion_total", "0"));
stat.SetState("completion_total", stats.CompletionCount + outputCount);
stat.SetState("completion_total", stats.CompletionCount + outputCount, false);

// Total cost
var total_cost = float.Parse(stat.GetState("llm_total_cost", "0"));
total_cost += Cost;
stat.SetState("llm_total_cost", total_cost);
stat.SetState("llm_total_cost", total_cost, false);
}

public void PrintStatistics()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void UpdateConversationDialogElements(string conversationId, List<DialogC
throw new NotImplementedException();
}

public List<StateKeyValue> GetConversationStates(string conversationId)
public ConversationState GetConversationStates(string conversationId)
{
throw new NotImplementedException();
}
Expand Down
Loading