Skip to content

Commit 0f248ca

Browse files
committed
doc: add key information extraction sample
1 parent 5f41113 commit 0f248ca

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

README.zh-Hans.md

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ messages.Add(TextChatMessage.File(file2.Id));
11891189
messages.Add(TextChatMessage.User("这两篇文章分别讲了什么?"));
11901190
```
11911191

1192-
最后向模型发送请求,注意这个接口获得的文件 ID 只有 `qwen-long``qwen-doc-turbo` 模型可以访问,其他模型是访问不到的。
1192+
向模型发送请求,注意这个接口获得的文件 ID 只有 `qwen-long``qwen-doc-turbo` 模型可以访问,其他模型是访问不到的。
11931193

11941194
```csharp
11951195
var completion = client.GetTextCompletionStreamAsync(
@@ -2549,9 +2549,11 @@ Usage: in(2434)/out(155)/image(2410)/total(2589)
25492549
25502550
#### 调用内置任务
25512551
2552+
调用内置任务时,不建议开启流式增量传输。(将不会提供 `OcrResult` 里的额外内容,只能从文字内容中手动读取)
2553+
25522554
##### 高精识别
25532555
2554-
使用这个任务时,不要开启流式传输,否则 `completion.Output.Choices[0].Message.Content[0].OcrResult.WordsInfo` 将为 `null`
2556+
使用内置任务时,不建议开启流式传输,否则 `completion.Output.Choices[0].Message.Content[0].OcrResult.WordsInfo` 将为 `null`
25552557
25562558
除了常规的返回文字内容外,该任务还会返回文字的坐标。
25572559
@@ -2633,6 +2635,108 @@ Location: [356,175,401,175,401,207,356,207]
26332635
RotateRect: [378,191,32,45,90]
26342636
````
26352637
2638+
##### 信息抽取
2639+
2640+
使用内置任务时,不建议开启流式传输,否则 `completion.Output.Choices[0].Message.Content[0].OcrResult.KvResult` 将为 `null`
2641+
2642+
设置 `Parameters.OcrOptions.Task``key_information_extraction` 即可调用该内置任务,不需要传入额外的文字信息。
2643+
2644+
通过 `Parameters.OcrOptions.TaskConfig.ResultSchema` 可以自定义输出的 JSON 格式(至多 3 层嵌套),留空则默认输出全部字段。
2645+
2646+
例如我们希望从图片中抽取如下类型的对象(JSON 属性名尽可能采用图片上存在的文字):
2647+
2648+
```csharp
2649+
internal class ReceiptModel()
2650+
{
2651+
[JsonPropertyName("乘车日期")]
2652+
public string? Date { get; init; }
2653+
2654+
[JsonPropertyName("发票")]
2655+
public ReceiptSerials? Serials { get; init; }
2656+
}
2657+
2658+
internal class ReceiptSerials
2659+
{
2660+
[JsonPropertyName("发票代码")]
2661+
public string? Code { get; init; }
2662+
2663+
[JsonPropertyName("发票号码")]
2664+
public string? Serial { get; init; }
2665+
}
2666+
```
2667+
2668+
当模型识别失败时,对应字段将被设置为 `null`,需要确保代码里能够正确处理这种情况。
2669+
2670+
示例请求:
2671+
2672+
```csharp
2673+
await using var file = File.OpenRead("receipt.jpg");
2674+
var ossLink = await client.UploadTemporaryFileAsync("qwen-vl-ocr-latest", file, "receipt.jpg");
2675+
Console.WriteLine($"File uploaded: {ossLink}");
2676+
var messages =
2677+
new List<MultimodalMessage> { MultimodalMessage.User([MultimodalMessageContent.ImageContent(ossLink)]) };
2678+
var completion = await client.GetMultimodalGenerationAsync(
2679+
new ModelRequest<MultimodalInput, IMultimodalParameters>()
2680+
{
2681+
Model = "qwen-vl-ocr-latest",
2682+
Input = new MultimodalInput() { Messages = messages },
2683+
Parameters = new MultimodalParameters()
2684+
{
2685+
OcrOptions = new MultimodalOcrOptions()
2686+
{
2687+
Task = "key_information_extraction",
2688+
TaskConfig = new MultimodalOcrTaskConfig()
2689+
{
2690+
ResultSchema = new Dictionary<string, object>()
2691+
{
2692+
{
2693+
"发票",
2694+
new Dictionary<string, string>()
2695+
{
2696+
{ "发票代码", "提取图中的发票代码,通常为一组数字或字母组合" },
2697+
{ "发票号码", "提取发票上的号码,通常由纯数字组成。" }
2698+
}
2699+
},
2700+
{ "乘车日期", "对应图中乘车日期时间,格式为年-月-日,比如2025-03-05" }
2701+
}
2702+
}
2703+
}
2704+
}
2705+
});
2706+
```
2707+
2708+
返回的 `KvResult` 是一个 `JsonElement`,可以直接反序列化到指定的类型,或者直接转换为 `Dictionary<string, object?>?`
2709+
2710+
使用示例:
2711+
2712+
````csharp
2713+
Console.WriteLine("Text:");
2714+
Console.WriteLine(completion.Output.Choices[0].Message.Content[0].Text);
2715+
Console.WriteLine("KvResults:");
2716+
var model = completion.Output.Choices[0].Message.Content[0].OcrResult!.KvResult?.Deserialize<ReceiptModel>();
2717+
Console.WriteLine($"Date: {model?.Date}");
2718+
Console.WriteLine($"Code: {model?.Serials?.Code}");
2719+
Console.WriteLine($"Serial: {model?.Serials?.Serial}");
2720+
2721+
/*
2722+
Text:
2723+
```json
2724+
{
2725+
"乘车日期": "2013-06-29",
2726+
"发票": {
2727+
"发票代码": "221021325353",
2728+
"发票号码": "10283819"
2729+
}
2730+
}
2731+
```
2732+
KvResults:
2733+
Date: 2013-06-29
2734+
Code: 221021325353
2735+
Serial: 10283819
2736+
Usage: in(524)/out(65)/image(310)/total(589)
2737+
*/
2738+
````
2739+
26362740
26372741
26382742
## 语音合成

0 commit comments

Comments
 (0)