Skip to content
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 @@ -5,9 +5,6 @@ public class RoutingArgs
[JsonPropertyName("function")]
public string Function { get; set; } = "route_to_agent";

[JsonPropertyName("function_args")]
public string? FunctionArgs { get; set; }

/// <summary>
/// The reason why you select this function or agent
/// </summary>
Expand Down
23 changes: 2 additions & 21 deletions src/Infrastructure/BotSharp.Core/Routing/Reasoning/HFReasoner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,8 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
}
};
var response = await completion.GetChatCompletions(router, dialogs);
var inst = response.Content.JsonContent<FunctionCallFromLlm>();

if (inst != null)
{
if (!string.IsNullOrEmpty(response.FunctionName))
{
inst.Function = response.FunctionName;
}

if (!string.IsNullOrEmpty(response.FunctionArgs))
{
inst.FunctionArgs = response.FunctionArgs;
}
}
var inst = response.Content.JsonContent<FunctionCallFromLlm>();

// Fix LLM malformed response
await ReasonerHelper.FixMalformedResponse(_services, inst);
Expand All @@ -85,14 +73,7 @@ public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, R

// Set user content as Planner's question
message.FunctionName = inst.Function;
if (!string.IsNullOrEmpty(inst.FunctionArgs))
{
message.FunctionArgs = inst.FunctionArgs;
}
else
{
message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments);
}
message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,10 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
}

message.FunctionArgs = JsonSerializer.Serialize(inst);
if (!string.IsNullOrEmpty(inst.FunctionArgs))
{
message.FunctionArgs = inst.FunctionArgs;
}

if (!string.IsNullOrEmpty(message.FunctionName))
{
var msg = RoleDialogModel.From(message, role: AgentRole.Function);
await routing.InvokeFunction(message.FunctionName, msg, options: new() { From = InvokeSource.Routing });
if (msg.StopCompletion)
{
message = RoleDialogModel.From(msg, role: AgentRole.Assistant);
}
}

var agentId = routing.Context.GetCurrentAgentId();
Expand Down Expand Up @@ -85,8 +76,8 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
}

var response = dialogs.Last();

response.Instruction = inst;
response.StopCompletion = message.StopCompletion;

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
var response = await completion.GetChatCompletions(router, dialogs);

var inst = (response.FunctionArgs ?? response.Content).JsonContent<FunctionCallFromLlm>();
if (inst != null)
{
if (!string.IsNullOrEmpty(response.FunctionName))
{
inst.Function = response.FunctionName;
}

if (!string.IsNullOrEmpty(response.FunctionArgs))
{
inst.FunctionArgs = response.FunctionArgs;
}
}

// Fix LLM malformed response
await ReasonerHelper.FixMalformedResponse(_services, inst);
Expand All @@ -80,14 +68,7 @@ public Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDia
{
// Set user content as Planner's question
message.FunctionName = inst.Function;
if (!string.IsNullOrEmpty(inst.FunctionArgs))
{
message.FunctionArgs = inst.FunctionArgs;
}
else
{
message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments);
}
message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments);

return Task.FromResult(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,8 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
// eliminating format drift where the LLM completes with finishReason=stop and returns
// free text or JSON in Content instead of a structured function call.
var response = await GetChatCompletionsWithScopedState(completion, router, dialogs, "tool_choice", "required");
var inst = response.FunctionArgs?.JsonContent<FunctionCallFromLlm>();

if (inst != null)
{
if (!string.IsNullOrEmpty(response.FunctionName))
{
inst.Function = response.FunctionName;
}

if (!string.IsNullOrEmpty(response.FunctionArgs))
{
inst.FunctionArgs = response.FunctionArgs;
}
}


var inst = response.FunctionArgs?.JsonContent<FunctionCallFromLlm>();
_logger.LogInformation("[OneStepForwardReasoner] ConversationId: {ConversationId}, MessageId: {MessageId}, Next instruction: {Instruction}",
_services.GetRequiredService<IRoutingContext>().ConversationId, messageId, response.FunctionArgs);

Expand All @@ -96,14 +82,7 @@ public Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDia
{
// Set user content as Planner's question
message.FunctionName = inst.Function;
if (!string.IsNullOrEmpty(inst.FunctionArgs))
{
message.FunctionArgs = inst.FunctionArgs;
}
else
{
message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments);
}
message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments);

return Task.FromResult(true);
}
Expand Down
Loading