Skip to content

Commit 7ded100

Browse files
authored
Merge pull request #389 from SciSharp/merge-code
merge latest code
2 parents 4e9c311 + 451ec99 commit 7ded100

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+523
-218
lines changed

Directory.Build.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<Project>
22
<PropertyGroup>
33
<TargetFramework>net8.0</TargetFramework>
4-
<LangVersion>12.0</LangVersion>
5-
<BotSharpVersion>1.0.1</BotSharpVersion>
6-
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
4+
<LangVersion>10.0</LangVersion>
5+
<BotSharpVersion>1.2.1</BotSharpVersion>
6+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<GenerateDocumentationFile>false</GenerateDocumentationFile>
88
</PropertyGroup>
99
</Project>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
[![Discord](https://img.shields.io/discord/1106946823282761851?label=Discord)](https://discord.com/channels/1106946823282761851/1106947212459642991)
55
[![QQ群聊](https://img.shields.io/static/v1?label=QQ&message=群聊&color=brightgreen)](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=sN9VVMwbWjs5L0ATpizKKxOcZdEPMrp8&authKey=RLDw41bLTrEyEgZZi%2FzT4pYk%2BwmEFgFcrhs8ZbkiVY7a4JFckzJefaYNW6Lk4yPX&noverify=0&group_code=985366726)
6-
[![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community)
76
[![Apache 2.0](https://img.shields.io/hexpm/l/plug.svg)](https://raw.githubusercontent.com/Oceania2018/BotSharp/master/LICENSE)
87
[![NuGet](https://img.shields.io/nuget/dt/BotSharp.Core.svg)](https://www.nuget.org/packages/BotSharp.Core)
98
[![Build status](https://ci.appveyor.com/api/projects/status/qx2dx5ca5hjqodm5?svg=true)](https://ci.appveyor.com/project/Haiping-Chen/botsharp)
@@ -88,6 +87,7 @@ BotSharp uses component design, the kernel is kept to a minimum, and business fu
8887
- BotSharp.Plugin.HuggingFace
8988
- BotSharp.Plugin.LLamaSharp
9089
- BotSharp.Plugin.SemanticKernel
90+
- BotSharp.Plugin.SparkDesk
9191

9292
#### Messaging / Channel
9393
- BotSharp.OpenAPI

docs/llm/function.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,32 @@
22

33
A **calling function** is a function that is passed as an argument to another function and is executed after a specific event or action occurs. In the context of **large language models (LLMs)**, calling functions can be used to hook into various stages of an LLM application. They are useful for tasks such as logging, monitoring, streaming, and more. For example, in the **BotSharp** framework, calling functions can be used to log information, monitor the progress of an LLM application, or perform other tasks. The BotSharp provides a `callbacks` argument that allows developers to interactive with external systems.
44

5-
The use of calling functions in LLM applications provides flexibility and extensibility. Developers can customize the behavior of their applications by defining callback handlers that implement specific methods. These handlers can be used for tasks like logging, error handling, or interacting with external systems. The function will be triggered by LLM based on the conversation context.
5+
The use of calling functions in LLM applications provides flexibility and extensibility. Developers can customize the behavior of their applications by defining callback handlers that implement specific methods. These handlers can be used for tasks like logging, error handling, or interacting with external systems. The function will be triggered by LLM based on the conversation context.
6+
7+
## Hide Function
8+
9+
In order to more flexibly control whether the Agent is allowed to use a certain function, there is a Visibility Expression property in the function definition that can be used to control display or hiding. When we input prompt into LLM, although we can use state variables in the system instruction file to control the rendering content, LLM will still take the definition of the function into consideration. If the related functions are not hidden at the same time, LLM will still be It is possible to call related functions, bringing unexpected results. Because we need to control system instruction and function definition at the same time to make them consistent.
10+
11+
```json
12+
{
13+
"name": "make_payment",
14+
"description": "call this function to make payment",
15+
"visibility_expression": "{% if states.order_number != empty %}visible{% endif %}",
16+
"parameters": {
17+
"type": "object",
18+
"properties": {
19+
"order_number": {
20+
"type": "string",
21+
"description": "order number."
22+
},
23+
"total_amount": {
24+
"type": "string",
25+
"description": "total amount."
26+
}
27+
},
28+
"required": ["order_number", "total_amount"]
29+
}
30+
}
31+
```
32+
33+
The above is an example. The system will parse the liquid template of Visibility Expression `{% if states.order_number != empty %}visible{% endif %}`. When "visible" is returned, the system will allow the Agent to use this function. In liquid In expressions, we can use `states.name` to reference the state value in the conversation.

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Functions.Models;
12
using BotSharp.Abstraction.Plugins.Models;
23
using BotSharp.Abstraction.Repositories.Filters;
34

@@ -23,6 +24,8 @@ public interface IAgentService
2324

2425
string RenderedTemplate(Agent agent, string templateName);
2526

27+
bool RenderFunction(Agent agent, FunctionDef def);
28+
2629
/// <summary>
2730
/// Get agent detail without trigger any hook.
2831
/// </summary>

src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public IConversationHook SetConversation(Conversation conversation)
2929
public virtual Task OnStateLoaded(ConversationState state)
3030
=> Task.CompletedTask;
3131

32-
public virtual Task OnStateChanged(string name, string preValue, string currentValue)
32+
public virtual Task OnStateChanged(StateChangeModel stateChange)
3333
=> Task.CompletedTask;
3434

3535
public virtual Task OnDialogRecordLoaded(RoleDialogModel dialog)

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public interface IConversationHook
4343
Task OnDialogRecordLoaded(RoleDialogModel dialog);
4444

4545
Task OnStateLoaded(ConversationState state);
46-
Task OnStateChanged(string name, string preValue, string currentValue);
46+
Task OnStateChanged(StateChangeModel stateChange);
4747

4848
Task OnMessageReceived(RoleDialogModel message);
4949
Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace BotSharp.Abstraction.Conversations.Models;
2+
3+
public class StateChangeModel
4+
{
5+
[JsonPropertyName("conversation_id")]
6+
public string ConversationId { get; set; }
7+
8+
[JsonPropertyName("message_id")]
9+
public string MessageId { get; set; }
10+
11+
[JsonPropertyName("name")]
12+
public string Name { get; set; }
13+
14+
[JsonPropertyName("before_value")]
15+
public string BeforeValue { get; set; }
16+
17+
[JsonPropertyName("before_active_rounds")]
18+
public int? BeforeActiveRounds { get; set; }
19+
20+
[JsonPropertyName("after_value")]
21+
public string AfterValue { get; set; }
22+
23+
[JsonPropertyName("after_active_rounds")]
24+
public int? AfterActiveRounds { get; set; }
25+
}

src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionCallingResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public class FunctionCallingResponse
1818

1919
[JsonPropertyName("args")]
2020
public JsonDocument? Args { get; set; }
21+
22+
public override string ToString()
23+
{
24+
return $"{FunctionName}({JsonSerializer.Serialize(Args)}) => {Content}";
25+
}
2126
}

src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionDef.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ public class FunctionDef
55
public string Name { get; set; }
66
public string Description { get; set; }
77

8+
[JsonPropertyName("visibility_expression")]
9+
public string? VisibilityExpression { get; set; }
10+
811
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
912
public string? Impact { get; set; }
1013

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Infrastructures.Enums;
2+
3+
public class StateConst
4+
{
5+
public const string EXPECTED_ACTION_AGENT = "expected_next_action_agent";
6+
public const string EXPECTED_GOAL_AGENT = "expected_user_goal_agent";
7+
public const string NEXT_ACTION_AGENT = "next_action_agent";
8+
public const string USER_GOAL_AGENT = "user_goal_agent";
9+
}

0 commit comments

Comments
 (0)