Skip to content

Commit d0144a6

Browse files
authored
Merge pull request #436 from hchen2020/master
Anthropic AI
2 parents 9915823 + 2a3e50a commit d0144a6

File tree

16 files changed

+437
-23
lines changed

16 files changed

+437
-23
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class RoleDialogModel : ITrackableMessage
4242
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
4343
public string? FunctionName { get; set; }
4444

45+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
46+
public string? ToolCallId { get; set; }
47+
4548
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
4649
public string? PostbackFunctionName { get; set; }
4750

@@ -108,6 +111,7 @@ public static RoleDialogModel From(RoleDialogModel source,
108111
MessageId = source.MessageId,
109112
FunctionArgs = source.FunctionArgs,
110113
FunctionName = source.FunctionName,
114+
ToolCallId = source.ToolCallId,
111115
PostbackFunctionName = source.PostbackFunctionName,
112116
RichContent = source.RichContent,
113117
StopCompletion = source.StopCompletion,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class FunctionCallingResponse
1313
[JsonPropertyName("content")]
1414
public string? Content { get; set; }
1515

16-
[JsonPropertyName("function_name")]
16+
[JsonPropertyName("function")]
1717
public string? FunctionName { get; set; }
1818

1919
[JsonPropertyName("args")]

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ namespace BotSharp.Abstraction.Functions.Models;
22

33
public class FunctionDef
44
{
5-
public string Name { get; set; }
6-
public string Description { get; set; }
5+
[JsonPropertyName("name")]
6+
public string Name { get; set; } = null!;
7+
8+
[JsonPropertyName("description")]
9+
public string Description { get; set; } = null!;
710

811
[JsonPropertyName("visibility_expression")]
912
public string? VisibilityExpression { get; set; }
1013

1114
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1215
public string? Impact { get; set; }
1316

17+
[JsonPropertyName("parameters")]
1418
public FunctionParametersDef Parameters { get; set; } = new FunctionParametersDef();
1519

1620
public override string ToString()

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class FunctionParametersDef
1919
[JsonPropertyName("required")]
2020
public List<string> Required { get; set; } = new List<string>();
2121

22+
public override string ToString()
23+
{
24+
return $"{{\"type\":\"{Type}\", \"properties\":{JsonSerializer.Serialize(Properties)}, \"required\":[{string.Join(",", Required.Select(x => "\"" + x + "\""))}]}}";
25+
}
26+
2227
public FunctionParametersDef()
2328
{
2429

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
@@ -150,7 +150,7 @@
150150
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
151151
<PackageReference Include="Colorful.Console" Version="1.2.15" />
152152
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.2.1" />
153-
<PackageReference Include="Fluid.Core" Version="2.8.0" />
153+
<PackageReference Include="Fluid.Core" Version="2.9.0" />
154154
<PackageReference Include="Nanoid" Version="3.0.0" />
155155
<PackageReference Include="RedLock.net" Version="2.3.2" />
156156
</ItemGroup>

src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialog
1717
return false;
1818
}
1919

20+
var provide = agent.LlmConfig.Provider;
21+
var model = agent.LlmConfig.Model;
22+
23+
if (provide == null || model == null)
24+
{
25+
var agentSettings = _services.GetRequiredService<AgentSettings>();
26+
provide = agentSettings.LlmConfig.Provider;
27+
model = agentSettings.LlmConfig.Model;
28+
}
29+
2030
var chatCompletion = CompletionProvider.GetChatCompletion(_services,
21-
agentConfig: agent.LlmConfig);
31+
provider: provide,
32+
model: model);
2233

2334
var message = dialogs.Last();
2435
var response = await chatCompletion.GetChatCompletions(agent, dialogs);
@@ -31,6 +42,7 @@ public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialog
3142
{
3243
response.FunctionName = response.FunctionName.Split("/").Last();
3344
}
45+
message.ToolCallId = response.ToolCallId;
3446
message.FunctionName = response.FunctionName;
3547
message.FunctionArgs = response.FunctionArgs;
3648
message.CurrentAgentId = agent.Id;

src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public TemplateRender(IServiceProvider services, ILogger<TemplateRender> logger)
2727
_options.MemberAccessStrategy.Register<Agent>();
2828
_options.MemberAccessStrategy.Register<RoutableAgent>();
2929
_options.MemberAccessStrategy.Register<RoutingHandlerDef>();
30+
_options.MemberAccessStrategy.Register<FunctionDef>();
31+
_options.MemberAccessStrategy.Register<FunctionParametersDef>();
3032
_options.MemberAccessStrategy.Register<UserIdentity>();
3133
}
3234

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
[Output Requirements]
2-
1. Read the [Functions] definition, you can utilize the function to retrieve data or execute actions.
3-
2. Think step by step, check if specific function will provider data to help complete user request based on the conversation.
4-
3. If you need to call a function to decide how to response user,
5-
response in format: {"role": "function", "reason":"why choose this function", "function_name": "", "args": {}},
6-
otherwise response in format: {"role": "assistant", "reason":"why response to user", "content":"next step question"}.
7-
4. If the conversation already contains the function execution result, don't need to call it again.
8-
5. If user mentioned some specific requirment, don't ask this question in your response.
9-
6. Don't repeat the same question in your response.
1+
{% if functions and functions != empty %}
2+
[FUNCTIONS]
3+
{% for fn in functions -%}
4+
{{ fn.name }}: {{ fn.description }}
5+
{{ fn.parameters }}
6+
{{ "\r\n" }}
7+
{%- endfor %}
8+
response_to_user: response to user directly without using any function.
9+
{"type": "object", "properties": {"content":{"type": "string", "description": "The content responsed to user"}}, "required":["content"]}
10+
11+
[RESPONSE OUTPUT REQUIREMENTS]
12+
* Pick the appropriate function and populate the arguments defined in properties.
13+
* Output the JSON {"function": "", "args":{}} without other text
14+
{% endif %}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using BotSharp.Abstraction.MLTasks;
2+
using BotSharp.Abstraction.Plugins;
3+
using BotSharp.Plugin.AnthropicAI.Providers;
4+
using BotSharp.Plugin.AnthropicAI.Settings;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace BotSharp.Plugin.AnthropicAI;
9+
10+
public class AnthropicPlugin : IBotSharpPlugin
11+
{
12+
public string Id => "012119da-8367-4be8-9a75-ab6ae55071e6";
13+
14+
public void RegisterDI(IServiceCollection services, IConfiguration config)
15+
{
16+
var settings = new AnthropicSettings();
17+
config.Bind("Anthropic", settings);
18+
services.AddSingleton(x =>
19+
{
20+
// Console.WriteLine($"Loaded Anthropic settings: {settings.Claude.ApiKey.SubstringMax(4)}");
21+
return settings;
22+
});
23+
24+
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
25+
// services.AddScoped<ITextCompletion, TextCompletionProvider>();
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Anthropic.SDK" Version="3.2.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)