resolve conflict

This commit is contained in:
Jicheng Lu 2023-12-13 14:33:27 -06:00
commit e7d962edf8
12 changed files with 113 additions and 13 deletions

View file

@ -14,5 +14,6 @@ public enum AgentField
Function,
Template,
Response,
Sample
Sample,
LlmConfig
}

View file

@ -71,6 +71,12 @@ public class Agent
public List<RoutingRule> RoutingRules { get; set; }
= new List<RoutingRule>();
/// <summary>
/// Agent LLM Config, i.e., provider & model
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public AgentLlmConfig? LlmConfig { get; set; }
/// <summary>
/// For rendering deferral
/// </summary>
@ -181,4 +187,10 @@ public class Agent
RoutingRules = rules ?? new List<RoutingRule>();
return this;
}
public Agent SetLlmConfig(AgentLlmConfig? llmConfig)
{
LlmConfig = llmConfig;
return this;
}
}

View file

@ -39,7 +39,8 @@ public partial class AgentService
.SetInstruction(foundAgent.Instruction)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
.SetResponses(foundAgent.Responses)
.SetLlmConfig(foundAgent.LlmConfig);
}
var user = _db.GetUserById(_user.Id);

View file

@ -27,6 +27,7 @@ public partial class AgentService
record.Templates = agent.Templates ?? new List<AgentTemplate>();
record.Responses = agent.Responses ?? new List<AgentResponse>();
record.Samples = agent.Samples ?? new List<string>();
record.LlmConfig = agent.LlmConfig;
_db.UpdateAgent(record, updateField);
await Task.CompletedTask;
@ -58,7 +59,8 @@ public partial class AgentService
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples);
.SetSamples(foundAgent.Samples)
.SetLlmConfig(foundAgent.LlmConfig);
_db.UpdateAgent(clonedAgent, AgentField.All);
}

View file

@ -246,6 +246,9 @@ public class FileRepository : IBotSharpRepository
case AgentField.Sample:
UpdateAgentSamples(agent.Id, agent.Samples);
break;
case AgentField.LlmConfig:
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
break;
@ -431,6 +434,17 @@ public class FileRepository : IBotSharpRepository
File.WriteAllLines(file, samples);
}
private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config)
{
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
agent.LlmConfig = config;
agent.UpdatedDateTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
private void UpdateAgentAllFields(Agent inputAgent)
{
var (agent, agentFile) = GetAgentFromFile(inputAgent.Id);
@ -493,10 +507,10 @@ public class FileRepository : IBotSharpRepository
var templates = FetchTemplates(dir);
var responses = FetchResponses(dir);
return record.SetInstruction(instruction)
.SetFunctions(functions)
.SetSamples(samples)
.SetTemplates(templates)
.SetResponses(responses);
.SetFunctions(functions)
.SetSamples(samples)
.SetTemplates(templates)
.SetResponses(responses);
}
return null;

View file

@ -149,4 +149,12 @@ public class AgentController : ControllerBase, IApiAdapter
model.Id = agentId;
await _agentService.UpdateAgent(model, AgentField.Sample);
}
[HttpPut("/agent/{agentId}/llm-config")]
public async Task UpdateAgentLlmConfig([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
{
var model = agent.ToAgent();
model.Id = agentId;
await _agentService.UpdateAgent(model, AgentField.LlmConfig);
}
}

View file

@ -43,6 +43,7 @@ public class AgentCreationModel
/// </summary>
public List<string> Profiles { get; set; } = new List<string>();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new List<RoutingRuleUpdateModel>();
public AgentLlmConfig? LlmConfig { get; set; }
public Agent ToAgent()
{
@ -61,7 +62,8 @@ public class AgentCreationModel
Profiles = Profiles,
RoutingRules = RoutingRules?
.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?
.ToList() ?? new List<RoutingRule>()
.ToList() ?? new List<RoutingRule>(),
LlmConfig = LlmConfig
};
}
}

View file

@ -47,6 +47,8 @@ public class AgentUpdateModel
public List<RoutingRuleUpdateModel>? RoutingRules { get; set; }
public AgentLlmConfig? LlmConfig { get; set; }
public Agent ToAgent()
{
var agent = new Agent()
@ -63,7 +65,8 @@ public class AgentUpdateModel
Instruction = Instruction ?? string.Empty,
Templates = Templates ?? new List<AgentTemplate>(),
Functions = Functions ?? new List<FunctionDef>(),
Responses = Responses ?? new List<AgentResponse>()
Responses = Responses ?? new List<AgentResponse>(),
LlmConfig = LlmConfig
};
return agent;

View file

@ -26,6 +26,10 @@ public class AgentViewModel
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<RoutingRule> RoutingRules { get; set; }
[JsonPropertyName("llmConfig")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public AgentLlmConfig? LlmConfig { get; set; }
[JsonPropertyName("created_datetime")]
public DateTime CreatedDateTime { get; set; }
@ -49,6 +53,7 @@ public class AgentViewModel
AllowRouting = agent.AllowRouting,
Profiles = agent.Profiles,
RoutingRules = agent.RoutingRules,
LlmConfig = agent.LlmConfig,
CreatedDateTime = agent.CreatedDateTime,
UpdatedDateTime = agent.UpdatedDateTime
};

View file

@ -16,6 +16,7 @@ public class AgentDocument : MongoBase
public bool Disabled { get; set; }
public List<string> Profiles { get; set; }
public List<RoutingRuleMongoElement> RoutingRules { get; set; }
public AgentLlmConfigMongoElement? LlmConfig { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }

View file

@ -0,0 +1,31 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Plugin.MongoStorage.Models;
public class AgentLlmConfigMongoElement
{
public string? Provider { get; set; }
public string Model { get; set; }
public static AgentLlmConfigMongoElement? ToMongoElement(AgentLlmConfig? config)
{
if (config == null) return null;
return new AgentLlmConfigMongoElement
{
Provider = config.Provider,
Model = config.Model
};
}
public static AgentLlmConfig? ToDomainElement(AgentLlmConfigMongoElement? config)
{
if (config == null) return null;
return new AgentLlmConfig
{
Provider = config.Provider,
Model = config.Model
};
}
}

View file

@ -82,6 +82,7 @@ public class MongoRepository : IBotSharpRepository
RoutingRules = x.RoutingRules?
.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?
.ToList() ?? new List<RoutingRuleMongoElement>(),
LlmConfig = AgentLlmConfigMongoElement.ToMongoElement(x.LlmConfig),
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
}).ToList();
@ -102,6 +103,7 @@ public class MongoRepository : IBotSharpRepository
.Set(x => x.Disabled, agent.Disabled)
.Set(x => x.Profiles, agent.Profiles)
.Set(x => x.RoutingRules, agent.RoutingRules)
.Set(x => x.LlmConfig, agent.LlmConfig)
.Set(x => x.CreatedTime, agent.CreatedTime)
.Set(x => x.UpdatedTime, agent.UpdatedTime);
_dc.Agents.UpdateOne(filter, update, _options);
@ -211,6 +213,9 @@ public class MongoRepository : IBotSharpRepository
case AgentField.Sample:
UpdateAgentSamples(agent.Id, agent.Samples);
break;
case AgentField.LlmConfig:
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
break;
@ -362,6 +367,17 @@ public class MongoRepository : IBotSharpRepository
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config)
{
var llmConfig = AgentLlmConfigMongoElement.ToMongoElement(config);
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
var update = Builders<AgentDocument>.Update
.Set(x => x.LlmConfig, llmConfig)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentAllFields(Agent agent)
{
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agent.Id);
@ -377,6 +393,7 @@ public class MongoRepository : IBotSharpRepository
.Set(x => x.Functions, agent.Functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList())
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
.Set(x => x.Samples, agent.Samples)
.Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig))
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
@ -413,7 +430,8 @@ public class MongoRepository : IBotSharpRepository
Profiles = agent.Profiles,
RoutingRules = !agent.RoutingRules.IsNullOrEmpty() ? agent.RoutingRules
.Select(r => RoutingRuleMongoElement.ToDomainElement(agent.Id, agent.Name, r))
.ToList() : new List<RoutingRule>()
.ToList() : new List<RoutingRule>(),
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agent.LlmConfig)
};
}
@ -469,7 +487,8 @@ public class MongoRepository : IBotSharpRepository
Profiles = x.Profiles,
RoutingRules = !x.RoutingRules.IsNullOrEmpty() ? x.RoutingRules
.Select(r => RoutingRuleMongoElement.ToDomainElement(x.Id, x.Name, r))
.ToList() : new List<RoutingRule>()
.ToList() : new List<RoutingRule>(),
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(x.LlmConfig)
}).ToList();
}
@ -482,8 +501,8 @@ public class MongoRepository : IBotSharpRepository
var filter = new AgentFilter
{
IsPublic = true,
AgentIds = agentIds
AgentIds = agentIds,
IsPublic = true
};
var agents = GetAgents(filter);
return agents;
@ -533,6 +552,7 @@ public class MongoRepository : IBotSharpRepository
RoutingRules = x.RoutingRules?
.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?
.ToList() ?? new List<RoutingRuleMongoElement>(),
LlmConfig = AgentLlmConfigMongoElement.ToMongoElement(x.LlmConfig),
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
}).ToList();