Skip to content

Commit a9f788e

Browse files
authored
Merge pull request #9 from cnblogs/supports-tools-in-text-completion
feat: support tools in text generation
2 parents 4f32693 + 14a853d commit a9f788e

17 files changed

+263
-7
lines changed

src/Cnblogs.DashScope.Sdk/ChatMessage.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ namespace Cnblogs.DashScope.Sdk;
55
/// <summary>
66
/// Represents a chat message between the user and the model.
77
/// </summary>
8-
public record ChatMessage(string Role, string Content) : IMessage<string>;
8+
/// <param name="Role">The role of this message.</param>
9+
/// <param name="Content">The content of this message.</param>
10+
/// <param name="ToolCalls">Calls to the function.</param>
11+
public record ChatMessage(string Role, string Content, List<ToolCall>? ToolCalls = null) : IMessage<string>;

src/Cnblogs.DashScope.Sdk/Cnblogs.DashScope.Sdk.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
<GenerateDocumentationFile>true</GenerateDocumentationFile>
55
<PackageTags>Cnblogs;Dashscope;AI;Sdk;Embedding;</PackageTags>
66
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="JsonSchema.Net.Generation" Version="4.1.1" />
9+
</ItemGroup>
710
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Represents a call to function.
5+
/// </summary>
6+
/// <param name="Name">Name of the function to call.</param>
7+
/// <param name="Arguments">Arguments of this call, usually a json string.</param>
8+
public record FunctionCall(string Name, string? Arguments);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Json.Schema;
2+
3+
namespace Cnblogs.DashScope.Sdk;
4+
5+
/// <summary>
6+
/// Definition of function that can be called by model.
7+
/// </summary>
8+
/// <param name="Name">The name of the function.</param>
9+
/// <param name="Description">Descriptions about this function that help model to decide when to call this function.</param>
10+
/// <param name="Parameters">The parameters JSON schema.</param>
11+
public record FunctionDefinition(string Name, string Description, JsonSchema? Parameters);

src/Cnblogs.DashScope.Sdk/ITextGenerationParameters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ public interface ITextGenerationParameters : IIncrementalOutputParameter, ISeedP
4242
/// Enable internet search when generation. Defaults to false.
4343
/// </summary>
4444
public bool? EnableSearch { get; }
45+
46+
/// <summary>
47+
/// Available tools for model to call.
48+
/// </summary>
49+
public List<ToolDefinition>? Tools { get; }
4550
}

src/Cnblogs.DashScope.Sdk/TextGenerationInput.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public class TextGenerationInput
1414
/// The collection of context messages associated with this chat completions request.
1515
/// </summary>
1616
public IEnumerable<ChatMessage>? Messages { get; set; }
17+
18+
/// <summary>
19+
/// Available tools for model to use.
20+
/// </summary>
21+
public IEnumerable<ToolDefinition>? Tools { get; set; }
1722
}

src/Cnblogs.DashScope.Sdk/TextGenerationParameters.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class TextGenerationParameters : ITextGenerationParameters
3232
/// <inheritdoc />
3333
public bool? EnableSearch { get; set; }
3434

35+
/// <inheritdoc />
36+
public List<ToolDefinition>? Tools { get; set; }
37+
3538
/// <inheritdoc />
3639
public bool? IncrementalOutput { get; set; }
3740
}

src/Cnblogs.DashScope.Sdk/ToolCall.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Represents a call to tool.
5+
/// </summary>
6+
/// <param name="Id">Id of this tool call.</param>
7+
/// <param name="Type">Type of the tool.</param>
8+
/// <param name="Function">Not null if type is function.</param>
9+
public record ToolCall(string? Id, string Type, FunctionCall? Function);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Definition of a tool that model can call during generation.
5+
/// </summary>
6+
/// <param name="Type">The type of this tool. Use <see cref="ToolTypes"/> to get all available options.</param>
7+
/// <param name="Function">Not null when <paramref name="Type"/> is tool.</param>
8+
public record ToolDefinition(string Type, FunctionDefinition? Function);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Available tool types for <see cref="ToolDefinition"/>.
5+
/// </summary>
6+
public static class ToolTypes
7+
{
8+
/// <summary>
9+
/// Function type.
10+
/// </summary>
11+
public const string Function = "function";
12+
}

0 commit comments

Comments
 (0)