refine
This commit is contained in:
parent
6308bdea5f
commit
9c14caede9
|
|
@ -23,7 +23,12 @@ public class SideCarAttribute : AsyncMoAttribute
|
||||||
var instance = context.Target;
|
var instance = context.Target;
|
||||||
var retType = context.ReturnType;
|
var retType = context.ReturnType;
|
||||||
|
|
||||||
var serviceProvider = ((IHaveServiceProvider)instance).ServiceProvider;
|
var serviceProvider = (instance as IHaveServiceProvider)?.ServiceProvider;
|
||||||
|
if (serviceProvider == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var (sidecar, sidecarMethod) = GetSideCarMethod(serviceProvider, methodName, retType, methodArgs);
|
var (sidecar, sidecarMethod) = GetSideCarMethod(serviceProvider, methodName, retType, methodArgs);
|
||||||
if (sidecar == null || sidecarMethod == null)
|
if (sidecar == null || sidecarMethod == null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,22 @@ public class SideCarOptions
|
||||||
{
|
{
|
||||||
public bool IsInheritStates { get; set; }
|
public bool IsInheritStates { get; set; }
|
||||||
public IEnumerable<string>? InheritStateKeys { get; set; }
|
public IEnumerable<string>? InheritStateKeys { get; set; }
|
||||||
|
public IEnumerable<string>? ExcludedStateKeys { get; set; }
|
||||||
|
|
||||||
public static SideCarOptions Empty()
|
public static SideCarOptions Empty()
|
||||||
{
|
{
|
||||||
return new();
|
return new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SideCarOptions InheritStates(IEnumerable<string>? targetStates = null)
|
public static SideCarOptions InheritStates(
|
||||||
|
IEnumerable<string>? includedStates = null,
|
||||||
|
IEnumerable<string>? excludedStates = null)
|
||||||
{
|
{
|
||||||
return new()
|
return new()
|
||||||
{
|
{
|
||||||
IsInheritStates = true,
|
IsInheritStates = true,
|
||||||
InheritStateKeys = targetStates
|
InheritStateKeys = includedStates,
|
||||||
|
ExcludedStateKeys = excludedStates
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -194,14 +194,17 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
||||||
|
|
||||||
if (_sideCarOptions?.IsInheritStates == true)
|
if (_sideCarOptions?.IsInheritStates == true)
|
||||||
{
|
{
|
||||||
|
var hasIncludedStates = _sideCarOptions?.InheritStateKeys?.Any() == true;
|
||||||
|
var hasExcludedStates = _sideCarOptions?.ExcludedStateKeys?.Any() == true;
|
||||||
var curStates = state.GetCurrentState();
|
var curStates = state.GetCurrentState();
|
||||||
|
|
||||||
foreach (var pair in curStates)
|
foreach (var pair in curStates)
|
||||||
{
|
{
|
||||||
var endNode = pair.Value.Values.LastOrDefault();
|
var endNode = pair.Value.Values.LastOrDefault();
|
||||||
if (endNode == null) continue;
|
if (endNode == null) continue;
|
||||||
|
|
||||||
if (_sideCarOptions?.InheritStateKeys?.Any() == true
|
if ((hasIncludedStates && !_sideCarOptions.InheritStateKeys.Contains(pair.Key))
|
||||||
&& !_sideCarOptions.InheritStateKeys.Contains(pair.Key))
|
|| (hasExcludedStates && _sideCarOptions.ExcludedStateKeys.Contains(pair.Key)))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -241,10 +241,9 @@ public partial class FileRepository
|
||||||
if (!string.IsNullOrEmpty(convDir))
|
if (!string.IsNullOrEmpty(convDir))
|
||||||
{
|
{
|
||||||
var breakpointFile = Path.Combine(convDir, BREAKPOINT_FILE);
|
var breakpointFile = Path.Combine(convDir, BREAKPOINT_FILE);
|
||||||
|
|
||||||
if (!File.Exists(breakpointFile))
|
if (!File.Exists(breakpointFile))
|
||||||
{
|
{
|
||||||
File.Create(breakpointFile);
|
File.WriteAllText(breakpointFile, "[]");
|
||||||
}
|
}
|
||||||
|
|
||||||
var content = File.ReadAllText(breakpointFile);
|
var content = File.ReadAllText(breakpointFile);
|
||||||
|
|
@ -285,7 +284,7 @@ public partial class FileRepository
|
||||||
var breakpointFile = Path.Combine(convDir, BREAKPOINT_FILE);
|
var breakpointFile = Path.Combine(convDir, BREAKPOINT_FILE);
|
||||||
if (!File.Exists(breakpointFile))
|
if (!File.Exists(breakpointFile))
|
||||||
{
|
{
|
||||||
File.Create(breakpointFile);
|
File.WriteAllText(breakpointFile, "[]");
|
||||||
}
|
}
|
||||||
|
|
||||||
var content = File.ReadAllText(breakpointFile);
|
var content = File.ReadAllText(breakpointFile);
|
||||||
|
|
@ -920,7 +919,6 @@ public partial class FileRepository
|
||||||
private bool SaveTruncatedDialogs(string dialogDir, List<DialogElement> dialogs)
|
private bool SaveTruncatedDialogs(string dialogDir, List<DialogElement> dialogs)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(dialogDir) || dialogs == null) return false;
|
if (string.IsNullOrEmpty(dialogDir) || dialogs == null) return false;
|
||||||
if (!File.Exists(dialogDir)) File.Create(dialogDir);
|
|
||||||
|
|
||||||
var texts = ParseDialogElements(dialogs);
|
var texts = ParseDialogElements(dialogs);
|
||||||
File.WriteAllText(dialogDir, texts);
|
File.WriteAllText(dialogDir, texts);
|
||||||
|
|
@ -930,7 +928,6 @@ public partial class FileRepository
|
||||||
private bool SaveTruncatedStates(string stateDir, List<StateKeyValue> states)
|
private bool SaveTruncatedStates(string stateDir, List<StateKeyValue> states)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(stateDir) || states == null) return false;
|
if (string.IsNullOrEmpty(stateDir) || states == null) return false;
|
||||||
if (!File.Exists(stateDir)) File.Create(stateDir);
|
|
||||||
|
|
||||||
var stateStr = JsonSerializer.Serialize(states, _options);
|
var stateStr = JsonSerializer.Serialize(states, _options);
|
||||||
File.WriteAllText(stateDir, stateStr);
|
File.WriteAllText(stateDir, stateStr);
|
||||||
|
|
@ -940,7 +937,6 @@ public partial class FileRepository
|
||||||
private bool SaveTruncatedLatestStates(string latestStateDir, List<StateKeyValue> states)
|
private bool SaveTruncatedLatestStates(string latestStateDir, List<StateKeyValue> states)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(latestStateDir) || states == null) return false;
|
if (string.IsNullOrEmpty(latestStateDir) || states == null) return false;
|
||||||
if (!File.Exists(latestStateDir)) File.Create(latestStateDir);
|
|
||||||
|
|
||||||
var latestStates = BuildLatestStates(states);
|
var latestStates = BuildLatestStates(states);
|
||||||
var stateStr = JsonSerializer.Serialize(latestStates, _options);
|
var stateStr = JsonSerializer.Serialize(latestStates, _options);
|
||||||
|
|
@ -951,7 +947,6 @@ public partial class FileRepository
|
||||||
private bool SaveTruncatedBreakpoints(string breakpointDir, List<ConversationBreakpoint> breakpoints)
|
private bool SaveTruncatedBreakpoints(string breakpointDir, List<ConversationBreakpoint> breakpoints)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(breakpointDir) || breakpoints == null) return false;
|
if (string.IsNullOrEmpty(breakpointDir) || breakpoints == null) return false;
|
||||||
if (!File.Exists(breakpointDir)) File.Create(breakpointDir);
|
|
||||||
|
|
||||||
var breakpointStr = JsonSerializer.Serialize(breakpoints, _options);
|
var breakpointStr = JsonSerializer.Serialize(breakpoints, _options);
|
||||||
File.WriteAllText(breakpointDir, breakpointStr);
|
File.WriteAllText(breakpointDir, breakpointStr);
|
||||||
|
|
|
||||||
|
|
@ -15,17 +15,17 @@ public partial class FileRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
var configFile = Path.Combine(vectorDir, COLLECTION_CONFIG_FILE);
|
var configFile = Path.Combine(vectorDir, COLLECTION_CONFIG_FILE);
|
||||||
|
if (!File.Exists(configFile))
|
||||||
|
{
|
||||||
|
File.WriteAllText(configFile, "[]");
|
||||||
|
}
|
||||||
|
|
||||||
if (reset)
|
if (reset)
|
||||||
{
|
{
|
||||||
File.WriteAllText(configFile, JsonSerializer.Serialize(configs ?? new(), _options));
|
File.WriteAllText(configFile, JsonSerializer.Serialize(configs ?? new(), _options));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!File.Exists(configFile))
|
|
||||||
{
|
|
||||||
File.Create(configFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
var str = File.ReadAllText(configFile);
|
var str = File.ReadAllText(configFile);
|
||||||
var savedConfigs = JsonSerializer.Deserialize<List<VectorCollectionConfig>>(str, _options) ?? new();
|
var savedConfigs = JsonSerializer.Deserialize<List<VectorCollectionConfig>>(str, _options) ?? new();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,31 +33,30 @@ public partial class KnowledgeService
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var vectorDb = GetVectorDb();
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||||
var created = await vectorDb.CreateCollection(collectionName, dimension);
|
var created = db.AddKnowledgeCollectionConfigs(new List<VectorCollectionConfig>
|
||||||
|
{
|
||||||
|
new VectorCollectionConfig
|
||||||
|
{
|
||||||
|
Name = collectionName,
|
||||||
|
Type = collectionType,
|
||||||
|
VectorStore = new VectorStoreConfig
|
||||||
|
{
|
||||||
|
Provider = _settings.VectorDb.Provider
|
||||||
|
},
|
||||||
|
TextEmbedding = new KnowledgeEmbeddingConfig
|
||||||
|
{
|
||||||
|
Provider = provider,
|
||||||
|
Model = model,
|
||||||
|
Dimension = dimension
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (created)
|
if (created)
|
||||||
{
|
{
|
||||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
var vectorDb = GetVectorDb();
|
||||||
var userId = await GetUserId();
|
created = await vectorDb.CreateCollection(collectionName, dimension);
|
||||||
|
|
||||||
db.AddKnowledgeCollectionConfigs(new List<VectorCollectionConfig>
|
|
||||||
{
|
|
||||||
new VectorCollectionConfig
|
|
||||||
{
|
|
||||||
Name = collectionName,
|
|
||||||
Type = collectionType,
|
|
||||||
VectorStore = new VectorStoreConfig
|
|
||||||
{
|
|
||||||
Provider = _settings.VectorDb.Provider
|
|
||||||
},
|
|
||||||
TextEmbedding = new KnowledgeEmbeddingConfig
|
|
||||||
{
|
|
||||||
Provider = provider,
|
|
||||||
Model = model,
|
|
||||||
Dimension = dimension
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return created;
|
return created;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue