Skip to content

Commit 6b6d2d2

Browse files
committed
feat: allow setting workspace id per request
1 parent 27396d8 commit 6b6d2d2

10 files changed

+112
-38
lines changed

src/Cnblogs.DashScope.Core/ApplicationRequest.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
namespace Cnblogs.DashScope.Core;
1+
using System.Text.Json.Serialization;
2+
using Cnblogs.DashScope.Core.Internals;
3+
4+
namespace Cnblogs.DashScope.Core;
25

36
/// <summary>
47
/// Request body for an application all.
58
/// </summary>
69
/// <typeparam name="TBizParams">Type of the biz_content</typeparam>
7-
public class ApplicationRequest<TBizParams>
10+
public class ApplicationRequest<TBizParams> : IDashScopeWorkspaceConfig
811
where TBizParams : class
912
{
1013
/// <summary>
1114
/// Content of this call.
1215
/// </summary>
13-
public required ApplicationInput<TBizParams> Input { get; init; }
16+
public required ApplicationInput<TBizParams> Input { get; set; }
1417

1518
/// <summary>
1619
/// Optional configurations.
1720
/// </summary>
18-
public ApplicationParameters? Parameters { get; init; }
21+
public ApplicationParameters? Parameters { get; set; }
22+
23+
/// <summary>
24+
/// Optional workspace id.
25+
/// </summary>
26+
[JsonIgnore]
27+
public string? WorkspaceId { get; set; }
1928
}
2029

2130
/// <summary>

src/Cnblogs.DashScope.Core/DashScopeClientCore.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ public async Task<DashScopeFileList> ListFilesAsync(CancellationToken cancellati
275275
}
276276

277277
/// <inheritdoc />
278-
public async Task<DashScopeDeleteFileResult> DeleteFileAsync(DashScopeFileId id, CancellationToken cancellationToken = default)
278+
public async Task<DashScopeDeleteFileResult> DeleteFileAsync(
279+
DashScopeFileId id,
280+
CancellationToken cancellationToken = default)
279281
{
280282
var request = BuildRequest(HttpMethod.Delete, ApiLinks.Files + $"/{id}");
281283
return (await SendCompatibleAsync<DashScopeDeleteFileResult>(request, cancellationToken))!;
@@ -297,8 +299,7 @@ private static HttpRequestMessage BuildRequest<TPayload>(
297299
string url,
298300
TPayload? payload = null,
299301
bool sse = false,
300-
bool isTask = false,
301-
string? workspaceId = null)
302+
bool isTask = false)
302303
where TPayload : class
303304
{
304305
var message = new HttpRequestMessage(method, url)
@@ -316,9 +317,9 @@ private static HttpRequestMessage BuildRequest<TPayload>(
316317
message.Headers.Add("X-DashScope-Async", "enable");
317318
}
318319

319-
if (string.IsNullOrWhiteSpace(workspaceId) == false)
320+
if (payload is IDashScopeWorkspaceConfig config && string.IsNullOrWhiteSpace(config.WorkspaceId) == false)
320321
{
321-
message.Headers.Add("X-DashScope-WorkspaceId", workspaceId);
322+
message.Headers.Add("X-DashScope-WorkSpace", config.WorkspaceId);
322323
}
323324

324325
return message;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Cnblogs.DashScope.Core.Internals;
2+
3+
/// <summary>
4+
/// Workspace configuration.
5+
/// </summary>
6+
internal interface IDashScopeWorkspaceConfig
7+
{
8+
/// <summary>
9+
/// Unique id of workspace to use.
10+
/// </summary>
11+
public string? WorkspaceId { get; }
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.DashScope.Core;
2+
3+
/// <summary>
4+
/// Token usage details.
5+
/// </summary>
6+
/// <param name="CachedTokens">Token count of cached input tokens</param>
7+
public record TextGenerationTokenDetails(int CachedTokens);

src/Cnblogs.DashScope.Core/TextGenerationTokenUsage.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public class TextGenerationTokenUsage
1111
/// <remarks>This number may larger than input if internet search is enabled.</remarks>
1212
public int InputTokens { get; set; }
1313

14+
/// <summary>
15+
/// Input token details.
16+
/// </summary>
17+
public TextGenerationTokenDetails? PromptTokensDetails { get; set; }
18+
1419
/// <summary>
1520
/// The number of output token.
1621
/// </summary>
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1 @@
1-
{
2-
"output": {
3-
"finish_reason": "stop",
4-
"text": "1+1 等于 2。这是最基本的数学加法之一,在十进制计数体系中,任何情况下两个一相加的结果都是二。"
5-
},
6-
"usage": {
7-
"total_tokens": 43,
8-
"output_tokens": 35,
9-
"input_tokens": 8
10-
},
11-
"request_id": "4ef2ed16-4dc3-9083-a723-fb2e80c84d3b"
12-
}
1+
{"output":{"finish_reason":"stop","text":"1+1等于2。这是最基本的数学加法运算之一。"},"usage":{"prompt_tokens_details":{"cached_tokens":0},"total_tokens":30,"output_tokens":14,"input_tokens":16},"request_id":"7e3d5586-cb70-98ce-97bf-8a2ac0091c3f"}
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
HTTP/1.1 200 OK
2-
eagleeye-traceid: 5bc840127c9dd7ed1de5586c0865cf01
3-
content-type: application/json
1+
HTTP/1.1 200 OK
2+
Vary: Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Accept-Encoding
3+
X-Request-ID: 7e3d5586-cb70-98ce-97bf-8a2ac0091c3f
4+
x-dashscope-timeout: 180
45
x-dashscope-call-gateway: true
5-
req-cost-time: 6004
6-
req-arrive-time: 1708924139239
7-
resp-start-time: 1708924145244
8-
x-envoy-upstream-service-time: 5998
9-
content-encoding: gzip
10-
vary: Accept-Encoding
11-
date: Mon, 26 Feb 2024 05:09:05 GMT
12-
server: istio-envoy
13-
transfer-encoding: chunked
6+
x-dashscope-finished: true
7+
req-cost-time: 974
8+
req-arrive-time: 1742222245417
9+
resp-start-time: 1742222246391
10+
x-envoy-upstream-service-time: 964
11+
Set-Cookie: acw_tc=7e3d5586-cb70-98ce-97bf-8a2ac0091c3f7d8fa666bbce18dfce67d4b0ced27ae0;path=/;HttpOnly;Max-Age=1800
12+
Date: Mon, 17 Mar 2025 14:37:26 GMT
13+
Server: istio-envoy
14+
Transfer-Encoding: chunked

test/Cnblogs.DashScope.Sdk.UnitTests/Utils/Snapshots.Application.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,30 @@ public static readonly RequestSnapshot<ApplicationRequest<TestApplicationBizPara
273273
new ApplicationOutput("code", "stop", "5a20b47dac2f43a7b1cbb8924ca66c47", null, null),
274274
new ApplicationUsage(null)));
275275

276+
public static readonly RequestSnapshot<ApplicationRequest<TestApplicationBizParam>, ApplicationResponse>
277+
WorkflowInDifferentWorkSpaceNoSse =
278+
new(
279+
"application-workflow",
280+
new ApplicationRequest<TestApplicationBizParam>()
281+
{
282+
Input = new ApplicationInput<TestApplicationBizParam>()
283+
{
284+
BizParams = new TestApplicationBizParam("code"), Prompt = "请你跟我这样说"
285+
},
286+
Parameters = new ApplicationParameters()
287+
{
288+
TopK = 100,
289+
TopP = 0.8f,
290+
Seed = 1234,
291+
Temperature = 0.85f,
292+
},
293+
WorkspaceId = "workspaceId"
294+
},
295+
new ApplicationResponse(
296+
"10990f51-e2d0-9338-9c52-319af5f4858b",
297+
new ApplicationOutput("code", "stop", "5a20b47dac2f43a7b1cbb8924ca66c47", null, null),
298+
new ApplicationUsage(null)));
299+
276300
public static readonly RequestSnapshot<ApplicationRequest, ApplicationResponse> ConversationSessionIdNoSse =
277301
new(
278302
"application-conversation-generation-session-id",

test/Cnblogs.DashScope.Sdk.UnitTests/Utils/Snapshots.TextGeneration.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public static class TextFormat
3636
{
3737
Output = new TextGenerationOutput
3838
{
39-
FinishReason = "stop", Text = "1+1 等于 2。这是最基本的数学加法之一,在十进制计数体系中,任何情况下两个一相加的结果都是二。"
39+
FinishReason = "stop", Text = "1+1等于2。这是最基本的数学加法运算之一。"
4040
},
41-
RequestId = "4ef2ed16-4dc3-9083-a723-fb2e80c84d3b",
41+
RequestId = "7e3d5586-cb70-98ce-97bf-8a2ac0091c3f",
4242
Usage = new TextGenerationTokenUsage
4343
{
44-
InputTokens = 8,
45-
OutputTokens = 35,
46-
TotalTokens = 43
44+
InputTokens = 16,
45+
OutputTokens = 14,
46+
TotalTokens = 30,
47+
PromptTokensDetails = new TextGenerationTokenDetails(0)
4748
}
4849
});
4950

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Cnblogs.DashScope.Sdk.UnitTests.Utils;
2+
using NSubstitute;
3+
4+
namespace Cnblogs.DashScope.Sdk.UnitTests;
5+
6+
public class WorkspaceIdTests
7+
{
8+
[Fact]
9+
public async Task ApplicationCall_WithWorkspaceId_ApplyAsync()
10+
{
11+
// Arrange
12+
const bool sse = false;
13+
var testCase = Snapshots.Application.WorkflowInDifferentWorkSpaceNoSse;
14+
var (client, handler) = await Sut.GetTestClientAsync(sse, testCase);
15+
16+
// Act
17+
await client.GetApplicationResponseAsync("anyId", testCase.RequestModel);
18+
19+
// Assert
20+
handler.Received().MockSend(
21+
Arg.Is<HttpRequestMessage>(
22+
m => m.Headers.GetValues("X-DashScope-WorkSpace").First() == testCase.RequestModel.WorkspaceId),
23+
Arg.Any<CancellationToken>());
24+
}
25+
}

0 commit comments

Comments
 (0)