gpt4 book ai didi

node.js - 什么参数对于 Node js 的 Google Document AI 客户端库无效?

转载 作者:行者123 更新时间:2023-12-05 02:03:49 24 4
gpt4 key购买 nike

我正在尝试使用 Node js 应用程序从 Google 运行文档 OCR。所以我使用了 Node js 的客户端库 @google-cloud/documentai

我做了文档中的所有操作 sample

这是我的代码

const projectId = '*******';
const location = 'eu'; // Format is 'us' or 'eu'
const processor = '******'; // Create processor in Cloud Console
const keyFilename = './secret/******.json';

const { DocumentProcessorServiceClient } = require('@google-cloud/documentai').v1beta3;

const client = new DocumentProcessorServiceClient({projectId, keyFilename});

async function start(encodedImage) {

console.log("Google AI Started")
const name = `projects/${projectId}/locations/${location}/processors/${processor}`;

const request = {
name,
document: {
content: encodedImage,
mimeType: 'application/pdf',
},
}

try {
const [result] = await client.processDocument(request);

const { document } = result;

const { text } = document;

const getText = textAnchor => {
if (!textAnchor.textSegments || textAnchor.textSegments.length === 0) {
return '';
}

// First shard in document doesn't have startIndex property
const startIndex = textAnchor.textSegments[0].startIndex || 0;
const endIndex = textAnchor.textSegments[0].endIndex;

return text.substring(startIndex, endIndex);
};

const [page1] = document;
const { paragraphs } = page1;

for (const paragraph of paragraphs) {
const paragraphText = getText(paragraph.layout.textAnchor);
console.log(`Paragraph text:\n${paragraphText}`);
}
return paragraphs;
}
catch (error) {
console.error(error);
}
}

module.exports = {
start
}

图片编码在这里

const {start: google} = require('./document-ai/index')

if (mimeType === 'application/pdf') {
pdf = true;
encoded = Buffer.from(file).toString('base64');
}

await google(encoded);

结果我得到这个错误

Google AI Started
Error: 3 INVALID_ARGUMENT: Request contains an invalid argument.
at Object.callErrorFromStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client.js:176:52)
at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:342:141)
at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:305:181)
at C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\call-stream.js:124:78
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
code: 3,
details: 'Request contains an invalid argument.',
metadata: Metadata {
internalRepr: Map { 'grpc-server-stats-bin' => [Array] },
options: {}
},
note: 'Exception occurred in retry method that was not classified as transient'
}

我的请求中有哪些无效参数?

环境细节

  • 操作系统:Windows 10
  • Node.js 版本:12.18.3
  • npm 版本:6.14.8
  • @google-cloud/documentai 版本:2.2.1

最佳答案

我也为此苦苦挣扎,结果证明解决方案非常简单:当您的位置不是“us”时,您必须设置参数apiEndpoint >.

这是位置 “eu” 的示例:

const client = new DocumentProcessorServiceClient({ 
keyFilename,
apiEndpoint: 'eu-documentai.googleapis.com'
});

更多信息在这里:GitHub: googleapis /nodejs-document-ai

关于node.js - 什么参数对于 Node js 的 Google Document AI 客户端库无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64888612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com