-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support tools in text generation
- Loading branch information
Showing
17 changed files
with
263 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Cnblogs.DashScope.Sdk; | ||
|
||
/// <summary> | ||
/// Represents a call to function. | ||
/// </summary> | ||
/// <param name="Name">Name of the function to call.</param> | ||
/// <param name="Arguments">Arguments of this call, usually a json string.</param> | ||
public record FunctionCall(string Name, string? Arguments); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Json.Schema; | ||
|
||
namespace Cnblogs.DashScope.Sdk; | ||
|
||
/// <summary> | ||
/// Definition of function that can be called by model. | ||
/// </summary> | ||
/// <param name="Name">The name of the function.</param> | ||
/// <param name="Description">Descriptions about this function that help model to decide when to call this function.</param> | ||
/// <param name="Parameters">The parameters JSON schema.</param> | ||
public record FunctionDefinition(string Name, string Description, JsonSchema? Parameters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Cnblogs.DashScope.Sdk; | ||
|
||
/// <summary> | ||
/// Represents a call to tool. | ||
/// </summary> | ||
/// <param name="Id">Id of this tool call.</param> | ||
/// <param name="Type">Type of the tool.</param> | ||
/// <param name="Function">Not null if type is function.</param> | ||
public record ToolCall(string? Id, string Type, FunctionCall? Function); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Cnblogs.DashScope.Sdk; | ||
|
||
/// <summary> | ||
/// Definition of a tool that model can call during generation. | ||
/// </summary> | ||
/// <param name="Type">The type of this tool. Use <see cref="ToolTypes"/> to get all available options.</param> | ||
/// <param name="Function">Not null when <paramref name="Type"/> is tool.</param> | ||
public record ToolDefinition(string Type, FunctionDefinition? Function); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Cnblogs.DashScope.Sdk; | ||
|
||
/// <summary> | ||
/// Available tool types for <see cref="ToolDefinition"/>. | ||
/// </summary> | ||
public static class ToolTypes | ||
{ | ||
/// <summary> | ||
/// Function type. | ||
/// </summary> | ||
public const string Function = "function"; | ||
} |
51 changes: 51 additions & 0 deletions
51
...shScope.Sdk.UnitTests/RawHttpData/single-generation-message-with-tools-nosse.request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"model": "qwen-max", | ||
"input": { | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": "杭州现在的天气如何?" | ||
} | ||
] | ||
}, | ||
"parameters": { | ||
"result_format": "message", | ||
"seed": 1234, | ||
"max_tokens": 1500, | ||
"top_p": 0.8, | ||
"top_k": 100, | ||
"repetition_penalty": 1.1, | ||
"temperature": 0.85, | ||
"stop": [[37763, 367]], | ||
"enable_search": false, | ||
"incremental_output": false, | ||
"tools": [ | ||
{ | ||
"type": "function", | ||
"function": { | ||
"name": "get_current_weather", | ||
"description": "获取现在的天气", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"location": { | ||
"type": "string", | ||
"description": "要获取天气的省市名称,例如浙江省杭州市" | ||
}, | ||
"unit": { | ||
"description": "温度单位", | ||
"enum": [ | ||
"Celsius", | ||
"Fahrenheit" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"location" | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...pe.Sdk.UnitTests/RawHttpData/single-generation-message-with-tools-nosse.response.body.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"output":{"choices":[{"finish_reason":"tool_calls","message":{"role":"assistant","tool_calls":[{"function":{"name":"get_current_weather","arguments":"{\"location\": \"浙江省杭州市\", \"unit\": \"Celsius\"}"},"id":"","type":"function"}],"content":""}}]},"usage":{"total_tokens":36,"output_tokens":31,"input_tokens":5},"request_id":"40b4361e-e936-91b5-879d-355a45d670f8"} |
14 changes: 14 additions & 0 deletions
14
....Sdk.UnitTests/RawHttpData/single-generation-message-with-tools-nosse.response.header.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
HTTP/1.1 200 OK | ||
eagleeye-traceid: 7328e5207abf69133abfe3a68446fc2d | ||
content-type: application/json | ||
x-dashscope-call-gateway: true | ||
x-dashscope-experiments: 33e6d810-qwen-max-base-default-imbalance-fix-lua | ||
req-cost-time: 3898 | ||
req-arrive-time: 1710324737299 | ||
resp-start-time: 1710324741198 | ||
x-envoy-upstream-service-time: 3893 | ||
content-encoding: gzip | ||
vary: Accept-Encoding | ||
date: Wed, 13 Mar 2024 10:12:21 GMT | ||
server: istio-envoy | ||
transfer-encoding: chunked |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
test/Cnblogs.DashScope.Sdk.UnitTests/Utils/GetCurrentWeatherParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Text.Json.Serialization; | ||
using Json.More; | ||
using Json.Schema.Generation; | ||
|
||
namespace Cnblogs.DashScope.Sdk.UnitTests.Utils; | ||
|
||
public record GetCurrentWeatherParameters( | ||
[property: Required] | ||
[property: Description("要获取天气的省市名称,例如浙江省杭州市")] | ||
string Location, | ||
[property: JsonConverter(typeof(EnumStringConverter<TemperatureUnit>))] | ||
[property: Description("温度单位")] | ||
TemperatureUnit Unit = TemperatureUnit.Celsius); | ||
|
||
public enum TemperatureUnit | ||
{ | ||
Celsius, | ||
Fahrenheit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters