NLU Watson API - ApiException: Error: invalid request: content is empty, Code: 400

NLU Watson API - ApiException: Error: invalid request: content is empty, Code: 400

我正在尝试使用 Python 和 IBM Watson API 进行名称实体识别。我以前做过,但现在遇到了一个奇怪的问题。这是一个.

ApiException: Error: invalid request: content is empty, Code: 400 , X-global-transaction-id: 9ba464a0-310b-4106-8044-0362ba1d5850

我只在非常小的样本(10 篇文章)上得到了结果,但如果我增加它,就会出现上面的错误。

我的代码:

# Pass credentials
authenticator = IAMAuthenticator('#######################')
natural_language_understanding = NaturalLanguageUnderstandingV1(version = '2020-08-01', authenticator = authenticator)

# Create a function for extracting information, using Nick's code
def get_company_list(text):
    response = natural_language_understanding.analyze(
           language = 'en', text = text,
           features = Features(
                        entities = EntitiesOptions(sentiment = False, emotion = False)
                        # keywords = KeywordsOptions(sentiment = True, emotion = True),
                        # concepts = ConceptsOptions(limit = 50),
                        # relations = RelationsOptions(),
                        # sentiment = SentimentOptions(),
                        # semantic_roles = SemanticRolesOptions(entities = True, keywords = True),
                        # emotion = EmotionOptions(),
                        # categories = CategoriesOptions()
            )).get_result()
  company_list = [entity['text'] for entity in response['entities'] if entity['type'] == 'Company']
  time.sleep(0.1)
  return(company_list)

# Apply the function and get a list of companies and titles
company_list_text = [get_company_list(text) for text in data1000['text']]
company_list_title = [get_company_list(text) for text in data1000['title']]

然后出现错误。我也用大学服务器来运行它,但我不确定它是否有问题。之前我是在 Colab 上成功的。

将空字符串或无效字符作为输入传递给 Watson NLU 时,会发生 ApiException: Error: invalid request: content is empty, Code: 400 错误。

在发送 API 请求之前检查输入数据是否存在缺失或无效的文本值。