翻譯|行業(yè)資訊|編輯:龔雪|2024-06-13 10:32:42.820|閱讀 80 次
概述:本文主要介紹如何用OpenAI模型為Word和Excel文件生成超鏈接描述,歡迎下載最新版組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
DevExpress Office File API是一個(gè)專為C#, VB.NET 和 ASP.NET等開發(fā)人員提供的非可視化.NET庫(kù)。有了這個(gè)庫(kù),不用安裝Microsoft Office,就可以完全自動(dòng)處理Excel、Word等文檔。開發(fā)人員使用一個(gè)非常易于操作的API就可以生成XLS, XLSx, DOC, DOCx, RTF, CSV 和 Snap Report等企業(yè)級(jí)文件。
在我們上一篇與AI相關(guān)的文章中(),描述了如何使用DevExpress Office File API庫(kù)和Azure AI OpenAI服務(wù)的可訪問性)為圖像和圖表生成AltText(有意義的描述)。在這篇文章中,我們將解釋如何為Word和Excel文件生成超鏈接描述。
當(dāng)您生成可訪問的Word或Excel文檔時(shí),創(chuàng)建帶有有意義的屏幕提示的超鏈接是必不可少的,有意義的屏幕提示可以幫助有視覺障礙的人或使用屏幕閱讀器的人理解超鏈接的目的,增加清晰度,并增強(qiáng)文檔導(dǎo)航。此外,當(dāng)您將文檔導(dǎo)出為可訪問的PDF格式(與可訪問性標(biāo)準(zhǔn)(WCAG)保持一致)時(shí),超鏈接屏幕提示非常重要。在DevExpress Word Processing Document API 和 Spreadsheet Document API庫(kù)的幫助下,您可以定位不提供可訪問描述的文檔超鏈接,并使用OpenAI模型生成這些描述。
獲取DevExpress Office File API正式版下載
DevExpress技術(shù)交流群10:532598169 歡迎一起進(jìn)群討論
我們?cè)贕itHub上的示例項(xiàng)目中為該功能添加了兩個(gè)新的端點(diǎn):,接下來將在下面詳細(xì)介紹實(shí)現(xiàn)步驟……
注意:在您將此解決方案納入應(yīng)用程序之前,請(qǐng)務(wù)必閱讀并理解OpenAI的許可協(xié)議和使用條款。
這一步類似于我們?cè)谥暗腁I金喜正規(guī)買球相關(guān)的文章中演示過得步驟,在您的項(xiàng)目中安裝Azure.AI.OpenAI NuGet包,并使用OpenAI api發(fā)送請(qǐng)求。
這個(gè)代碼片段發(fā)送一個(gè)請(qǐng)求來描述一個(gè)超鏈接URI,并獲得一個(gè)帶有響應(yīng)的字符串。
public class OpenAIClientHyperlinkHelper { OpenAIClient client; internal OpenAIClientHyperlinkHelper(string openAIApiKey) { client = new OpenAIClient(openAIApiKey, new OpenAIClientOptions()); } internal async Task<string> DescribeHyperlinkAsync(string link) { ChatCompletionsOptions chatCompletionsOptions = new() { DeploymentName = "gpt-4-vision-preview", Messages = { new ChatRequestSystemMessage("You are a helpful assistant that describes hyperlinks."), new ChatRequestUserMessage( new ChatMessageTextContentItem("Give a description for this hyperlink URI in 10-20 words"), new ChatMessageTextContentItem(link)) }, MaxTokens = 300 }; Response<ChatCompletions> chatResponse = await client.GetChatCompletionsAsync(chatCompletionsOptions); ChatChoice choice = chatResponse.Value.Choices[0]; return choice.Message.Content; } }
要在DevExpress支持的Word Processing Document API應(yīng)用程序中使用上述API,請(qǐng)?jiān)L問集合來檢索文檔超鏈接。檢查文檔超鏈接是否包含提示( 屬性值不應(yīng)為空或等于值),否則調(diào)用OpenAIClientHyperlinkHelper.DescribeHyperlinkAsync方法生成超鏈接描述信息。
public async Task<IActionResult> GenerateHyperlinkDescriptionForWord(IFormFile documentWithHyperlinks, [FromQuery] RichEditFormat outputFormat) { try { var hyperlinkHelper = new OpenAIClientHyperlinkHelper(openAIApiKey); using (var wordProcessor = new RichEditDocumentServer()) { await RichEditHelper.LoadFile(wordProcessor, documentWithHyperlinks); wordProcessor.IterateSubDocuments(async (document) => { foreach (var hyperlink in document.Hyperlinks) { if (string.IsNullOrEmpty(hyperlink.ToolTip) || hyperlink.ToolTip == hyperlink.NavigateUri) { hyperlink.ToolTip = hyperlinkHelper.DescribeHyperlinkAsync(hyperlink.NavigateUri).Result; } } }); Stream result = RichEditHelper.SaveDocument(wordProcessor, outputFormat); string contentType = RichEditHelper.GetContentType(outputFormat); string outputStringFormat = outputFormat.ToString().ToLower(); return File(result, contentType, $"result.{outputStringFormat}"); } } catch (Exception e) { return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace); } }
下圖顯示了使用此代碼生成的超鏈接描述。
對(duì)于DevExpress支持的Spreadsheet Document API應(yīng)用程序,迭代文檔工作表并使用集合訪問工作表超鏈接,完成后,必須確定 屬性是否滿足條件,并調(diào)用OpenAIClientHyperlinkHelper. DescribeHyperlinkAsync方法,在需要時(shí)生成超鏈接描述。
public async Task<IActionResult> GenerateHyperlinkDescriptionForSpreadsheet(IFormFile documentWithHyperlinks, [FromQuery] SpreadsheetFormat outputFormat) { try { var hyperlinkHelper = new OpenAIClientHyperlinkHelper(openAIApiKey); using (var workbook = new Workbook()) { await SpreadsheetHelper.LoadWorkbook(workbook, documentWithHyperlinks); foreach (var worksheet in workbook.Worksheets) { foreach (var hyperlink in worksheet.Hyperlinks) { if(hyperlink.IsExternal && (string.IsNullOrEmpty(hyperlink.TooltipText) || hyperlink.TooltipText == hyperlink.Uri)) hyperlink.TooltipText = hyperlinkHelper.DescribeHyperlinkAsync(hyperlink.Uri).Result; } } Stream result = SpreadsheetHelper.SaveDocument(workbook, outputFormat); string contentType = SpreadsheetHelper.GetContentType(outputFormat); string outputStringFormat = outputFormat.ToString().ToLower(); return File(result, contentType, $"result.{outputStringFormat}"); } } catch (Exception e) { return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace); } }
更多DevExpress線上公開課、中文教程資訊請(qǐng)上中文網(wǎng)獲取
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)