@@ -1173,13 +1173,46 @@ Usage: in(31)/out(34)/reasoning()/total(65)
11731173
11741174尽管 QWen-Long 支持直接传入字符串,但还是推荐先将文件上传后再通过 FileId 的形式传入 ` message ` 数组中。
11751175
1176- 上传文件,使用 ` UploadFileAsync ()` 方法传入文件(注意不是 ` UploadTemporaryFileAsync ` , 后者是用于上传媒体文件的) :
1176+ 上传文件,使用 ` OpenAiCompatibleUploadFileAsync ()` 方法传入文件:
11771177
11781178``` csharp
1179- var file1 = await client .UploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
1179+ var file1 = await client .OpenAiCompatibleUploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
11801180```
11811181
1182- 然后将文件作为 ` system ` 消息传入消息数组中,注意第一条 ` system ` 消息不能省略,否则模型可能会将文件里的内容当作 System prompt 。
1182+ 如果文件比较大,服务端可能需要几秒的时间进行解析。根据返回的 ` file.Status ` 属性是否为 ` processed ` 可以判断是否解析完成。未解析完成的文件无法被模型使用,需要等待解析完成。以下是一个示例方法,自动等待文档解析完毕或超时抛出异常:
1183+
1184+ ``` csharp
1185+ private static async Task EnsureFileProcessedAsync (
1186+ IDashScopeClient client ,
1187+ DashScopeFileId id ,
1188+ int timeoutInSeconds = 5 )
1189+ {
1190+ var timeout = Task .Delay (TimeSpan .FromSeconds (timeoutInSeconds ));
1191+ while (timeout .IsCompleted == false )
1192+ {
1193+ var file = await client .GetFileAsync (id );
1194+ if (file .Status == " processed" )
1195+ {
1196+ return ;
1197+ }
1198+
1199+ await Task .Delay (1000 );
1200+ }
1201+
1202+ throw new InvalidOperationException ($" File not processed within timeout, fileId: {id }" );
1203+ }
1204+ ```
1205+
1206+ 调用方式:
1207+
1208+ ``` csharp
1209+ if (file .Status != " processed" )
1210+ {
1211+ await EnsureFileProcessedAsync (client , file .Id , 3 ); // 最多等待 3 秒
1212+ }
1213+ ```
1214+
1215+ 待文档解析完成后,将文件作为 ` system ` 消息传入消息数组中,注意第一条 ` system ` 消息不能省略,否则模型可能会将文件里的内容当作 ` System prompt ` 。
11831216
11841217``` csharp
11851218var messages = new List <TextChatMessage >();
@@ -1211,22 +1244,25 @@ var completion = client.GetTextCompletionStreamAsync(
12111244 });
12121245```
12131246
1214- 最后可以通过 ` DeleteFileAsync ()` 方法删除上传的文件
1247+ 最后可以通过 ` OpenAiCompatibleDeleteFileAsync ()` 方法删除上传的文件。
12151248
12161249``` csharp
1217- var result = await client .DeleteFileAsync (file1 .Id );
1250+ var result = await client .OpenAiCompatibleDeleteFileAsync (file1 .Id );
12181251Console .WriteLine (result .Deleted ? " Success" : " Failed" );
12191252```
12201253
12211254完整示例
12221255
12231256``` csharp
12241257Console .WriteLine (" Uploading file1..." );
1225- var file1 = await client .UploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
1258+ var file1 = await client .OpenAiCompatibleUploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
12261259Console .WriteLine (" Uploading file2..." );
1227- var file2 = await client .UploadFileAsync (File .OpenRead (" 1024-2.txt" ), " file2.txt" );
1260+ var file2 = await client .OpenAiCompatibleUploadFileAsync (File .OpenRead (" 1024-2.txt" ), " file2.txt" );
12281261Console .WriteLine ($" Uploaded, file1 id: {file1 .Id .ToUrl ()}, file2 id: {file2 .Id .ToUrl ()}" );
12291262
1263+ await EnsureFileProcessedAsync (client , file1 );
1264+ await EnsureFileProcessedAsync (client , file2 );
1265+
12301266var messages = new List <TextChatMessage >();
12311267messages .Add (TextChatMessage .System (" You are a helpful assistant" ));
12321268messages .Add (TextChatMessage .File (file1 .Id ));
@@ -1292,6 +1328,31 @@ Console.Write("Deleting file2...");
12921328result = await client .DeleteFileAsync (file2 .Id );
12931329Console .WriteLine (result .Deleted ? " Success" : " Failed" );
12941330
1331+ private static async Task EnsureFileProcessedAsync (
1332+ IDashScopeClient client ,
1333+ DashScopeFile file ,
1334+ int timeoutInSeconds = 5 )
1335+ {
1336+ if (file .Status == " processed" )
1337+ {
1338+ return ;
1339+ }
1340+
1341+ var timeout = Task .Delay (TimeSpan .FromSeconds (timeoutInSeconds ));
1342+ while (timeout .IsCompleted == false )
1343+ {
1344+ var realtime = await client .GetFileAsync (file .Id );
1345+ if (realtime .Status == " processed" )
1346+ {
1347+ return ;
1348+ }
1349+
1350+ await Task .Delay (1000 );
1351+ }
1352+
1353+ throw new InvalidOperationException ($" File not processed within timeout, fileId: {file .Id }" );
1354+ }
1355+
12951356/*
12961357Uploading file1...
12971358Uploading file2...
@@ -1324,6 +1385,14 @@ Deleting file2...Success
13241385*/
13251386```
13261387
1388+ ** 注意及时删除上传的文件,这个接口有文件总数(1万)和文件总量(100GB)限制。**
1389+
1390+ 你可以使用 ` ListFileAsync ` 获取完整的文件列表并删除不再需要使用的文件
1391+
1392+ 示例:
1393+
1394+
1395+
13271396### 翻译能力(Qwen-MT)
13281397
13291398翻译能力主要通过 ` Parameters ` 里的 ` TranslationOptions ` 进行配置。
@@ -1507,10 +1576,10 @@ Usage: in(147)/out(130)/total(277)
15071576
15081577### 数据挖掘(Qwen-doc-turbo)
15091578
1510- 上传文件,使用 ` UploadFileAsync ()` 方法传入文件(注意不是 ` UploadTemporaryFileAsync ` , 后者是用于上传媒体文件的):
1579+ 上传文件,使用 ` OpenAiCompatibleUploadFileAsync ()` 方法传入文件(注意不是 ` UploadTemporaryFileAsync ` , 后者是用于上传媒体文件的):
15111580
15121581``` csharp
1513- var file1 = await client .UploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
1582+ var file1 = await client .OpenAiCompatibleUploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
15141583```
15151584
15161585然后将文件作为 ` system ` 消息传入消息数组中,注意第一条 ` system ` 消息不能省略,否则模型可能会将文件里的内容当作 System prompt 。
@@ -1549,7 +1618,7 @@ var completion = client.GetTextCompletionStreamAsync(
15491618
15501619```` csharp
15511620Console .WriteLine (" Uploading file1..." );
1552- var file1 = await client .UploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
1621+ var file1 = await client .OpenAiCompatibleUploadFileAsync (File .OpenRead (" 1024-1.txt" ), " file1.txt" );
15531622var messages = new List <TextChatMessage >();
15541623messages .Add (TextChatMessage .System (" You are a helpful assistant" ));
15551624messages .Add (TextChatMessage .File (file1 .Id ));
@@ -1593,7 +1662,7 @@ if (usage != null)
15931662
15941663// Deleting files
15951664Console .Write (" Deleting file1..." );
1596- var result = await client .DeleteFileAsync (file1 .Id );
1665+ var result = await client .OpenAiCompatibleDeleteFileAsync (file1 .Id );
15971666Console .WriteLine (result .Deleted ? " Success" : " Failed" );
15981667
15991668/*
0 commit comments