Skip to content

Commit b76f9bb

Browse files
committed
add question to GetRelevantKnowledges.
1 parent d9996fd commit b76f9bb

File tree

11 files changed

+27
-49
lines changed

11 files changed

+27
-49
lines changed

src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public interface IKnowledgeHook
55
Task<List<KnowledgeChunk>> CollectChunkedKnowledge()
66
=> Task.FromResult(new List<KnowledgeChunk>());
77

8-
Task<List<string>> GetRelevantKnowledges()
8+
Task<List<string>> GetRelevantKnowledges(string text)
99
=> Task.FromResult(new List<string>());
1010

1111
Task<List<string>> GetGlobalKnowledges()

src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/GenericTemplateMessage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ public class GenericTemplateMessage<T> : IRichMessage, ITemplateMessage
55
[JsonPropertyName("rich_type")]
66
public string RichType => RichTypeEnum.GenericTemplate;
77

8+
/// <summary>
9+
/// Use model refined content if leaving blank
10+
/// </summary>
811
[JsonPropertyName("text")]
912
[Translate]
1013
public string Text { get; set; } = string.Empty;

src/Infrastructure/BotSharp.Abstraction/Planning/IPlanningHook.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public interface IPlanningHook
44
{
55
Task<string> GetSummaryAdditionalRequirements(string planner)
66
=> Task.FromResult(string.Empty);
7+
78
Task OnPlanningCompleted(string planner, RoleDialogModel msg)
89
=> Task.CompletedTask;
910
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ response.RichContent is RichContent<IRichMessage> template &&
123123
Message = new TextMessage(response.SecondaryContent ?? response.Content)
124124
};
125125

126+
// Use model refined response
127+
if (string.IsNullOrEmpty(response.RichContent.Message.Text))
128+
{
129+
response.RichContent.Message.Text = response.Content;
130+
}
131+
126132
// Patch return function name
127133
if (response.PostbackFunctionName != null)
128134
{

src/Infrastructure/BotSharp.OpenAPI/Filters/UserSingleLoginFilter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ public void OnAuthorization(AuthorizationFilterContext context)
3131
return;
3232
}
3333

34-
if (token.ValidTo.ToLongTimeString() != GetUserExpires().ToLongTimeString())
34+
var validTo = token.ValidTo.ToLongTimeString();
35+
var currentExpires = GetUserExpires().ToLongTimeString();
36+
37+
if (validTo != currentExpires)
3538
{
39+
Serilog.Log.Warning($"Token expired. Token expires at {validTo}, current expires at {currentExpires}");
3640
context.Result = new UnauthorizedResult();
3741
}
3842
}

src/Plugins/BotSharp.Plugin.Planner/Functions/PrimaryStagePlanFn.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ public async Task<bool> Execute(RoleDialogModel message)
2727
var collectionName = knowledgeSettings.Default.CollectionName ?? KnowledgeCollectionName.BotSharp;
2828

2929
// Get knowledge from vectordb
30+
var hooks = _services.GetServices<IKnowledgeHook>();
3031
var knowledges = new List<string>();
3132
foreach (var question in task.Questions)
3233
{
3334
var list = await knowledgeService.SearchVectorKnowledge(question, collectionName, new VectorSearchOptions
3435
{
3536
Confidence = 0.2f
3637
});
37-
3838
knowledges.Add(string.Join("\r\n\r\n=====\r\n", list.Select(x => x.ToQuestionAnswer())));
39+
40+
foreach (var hook in hooks)
41+
{
42+
var k = await hook.GetRelevantKnowledges(question);
43+
knowledges.AddRange(k);
44+
}
3945
}
4046

4147
// Get first stage planning prompt
@@ -92,7 +98,7 @@ private async Task<RoleDialogModel> GetAiResponse(Agent plannerAgent)
9298
var wholeDialogs = conv.GetDialogHistory();
9399

94100
// Append text
95-
wholeDialogs.Last().Content += "\n\nYou must analyze the table description to infer the table relations.";
101+
wholeDialogs.Last().Content += "\n\nYou must analyze the table description to infer the table relations. Only output the JSON result.";
96102

97103
var completion = CompletionProvider.GetChatCompletion(_services,
98104
provider: plannerAgent.LlmConfig.Provider,

src/Plugins/BotSharp.Plugin.Planner/Functions/SecondaryStagePlanFn.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ private async Task<string> GetSecondStagePlanPrompt(string taskDescription, Role
7575
var template = agent.Templates.FirstOrDefault(x => x.Name == "two_stage.2nd.plan")?.Content ?? string.Empty;
7676
var responseFormat = JsonSerializer.Serialize(new SecondStagePlan
7777
{
78-
Tool = "tool name if task solution provided",
7978
Parameters = [ JsonDocument.Parse("{}") ],
8079
Results = [ string.Empty ]
8180
});

src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Models/SecondStagePlan.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ public class SecondStagePlan
88
[JsonPropertyName("description")]
99
public string Description { get; set; } = "";
1010

11-
[JsonPropertyName("tool_name")]
12-
public string Tool { get; set; } = "";
13-
1411
[JsonPropertyName("input_args")]
1512
public JsonDocument[] Parameters { get; set; } = [];
1613

src/Plugins/BotSharp.Plugin.Planner/TwoStaging/TwoStageTaskPlanner.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,6 @@ public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, Ro
9696
return true;
9797
}
9898

99-
private async Task<string> GetFirstStagePlanPrompt(Agent router)
100-
{
101-
var template = router.Templates.First(x => x.Name == "two_stage.1st.plan").Content;
102-
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
103-
{
104-
Parameters = new JsonDocument[] { JsonDocument.Parse("{}") },
105-
Results = new string[] { "" }
106-
});
107-
108-
var relevantKnowledges = new List<string>();
109-
var hooks = _services.GetServices<IKnowledgeHook>();
110-
foreach (var hook in hooks)
111-
{
112-
var k = await hook.GetRelevantKnowledges();
113-
relevantKnowledges.AddRange(k);
114-
}
115-
116-
var render = _services.GetRequiredService<ITemplateRender>();
117-
return render.Render(template, new Dictionary<string, object>
118-
{
119-
{ "response_format", responseFormat },
120-
{ "relevant_knowledges", relevantKnowledges.ToArray() }
121-
});
122-
}
123-
12499
private async Task<string> GetNextStepPrompt(Agent router)
125100
{
126101
var agentService = _services.GetRequiredService<IAgentService>();
@@ -134,17 +109,4 @@ private async Task<string> GetNextStepPrompt(Agent router)
134109
{ StateConst.EXPECTED_GOAL_AGENT, states.GetState(StateConst.EXPECTED_GOAL_AGENT) }
135110
});
136111
}
137-
138-
private string GetSecondStageTaskPrompt(Agent router, SecondStagePlan plan)
139-
{
140-
var template = router.Templates.First(x => x.Name == "planner_prompt.two_stage.2nd.task").Content;
141-
var render = _services.GetRequiredService<ITemplateRender>();
142-
return render.Render(template, new Dictionary<string, object>
143-
{
144-
{ "task_description", plan.Description },
145-
{ "related_tables", plan.Tables },
146-
{ "input_arguments", JsonSerializer.Serialize(plan.Parameters) },
147-
{ "output_results", JsonSerializer.Serialize(plan.Results) },
148-
});
149-
}
150112
}

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/instructions/instruction.liquid

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Use the TwoStagePlanner approach to plan the overall implementation steps, follow the below steps strictly.
2-
1. call plan_primary_stage to generate the primary plan.
2+
1. Call plan_primary_stage to generate the primary plan.
33
2. If need_additional_information is true, call plan_secondary_stage for the specific primary stage.
4-
3. You must call plan_summary for you final planned output.
4+
3. You must call plan_summary to generate final planned steps.
5+
4. If you can't generate the final accurate planning steps due to missing some specific informations, please ask user for more information.
56

67
*** IMPORTANT ***
78
Don't run the planning process repeatedly if you have already got the result of user's request.

0 commit comments

Comments
 (0)