chore: skip unnecessary key checks prior to accessing a dictionary (#4497)

This commit is contained in:
Bowen Liang
2024-05-19 18:30:45 +08:00
committed by GitHub
parent aa13d14019
commit 04ad46dd31
30 changed files with 45 additions and 44 deletions

View File

@@ -70,7 +70,7 @@ class StableDiffusionTool(BuiltinTool):
if not base_url:
return self.create_text_message('Please input base_url')
if 'model' in tool_parameters and tool_parameters['model']:
if tool_parameters.get('model'):
self.runtime.credentials['model'] = tool_parameters['model']
model = self.runtime.credentials.get('model', None)

View File

@@ -48,7 +48,7 @@ class WolframAlphaTool(BuiltinTool):
if 'success' not in response_data['queryresult'] or response_data['queryresult']['success'] != True:
query_result = response_data.get('queryresult', {})
if 'error' in query_result and query_result['error']:
if query_result.get('error'):
if 'msg' in query_result['error']:
if query_result['error']['msg'] == 'Invalid appid':
raise ToolProviderCredentialValidationError('Invalid appid')

View File

@@ -79,7 +79,7 @@ class DatasetMultiRetrieverTool(DatasetRetrieverBaseTool):
document_score_list = {}
for item in all_documents:
if 'score' in item.metadata and item.metadata['score']:
if item.metadata.get('score'):
document_score_list[item.metadata['doc_id']] = item.metadata['score']
document_context_list = []

View File

@@ -87,7 +87,7 @@ class DatasetRetrieverTool(DatasetRetrieverBaseTool):
document_score_list = {}
if dataset.indexing_technique != "economy":
for item in documents:
if 'score' in item.metadata and item.metadata['score']:
if item.metadata.get('score'):
document_score_list[item.metadata['doc_id']] = item.metadata['score']
document_context_list = []
index_node_ids = [document.metadata['doc_id'] for document in documents]

View File

@@ -132,17 +132,17 @@ def extract_using_readabilipy(html):
}
# Populate article fields from readability fields where present
if input_json:
if "title" in input_json and input_json["title"]:
if input_json.get("title"):
article_json["title"] = input_json["title"]
if "byline" in input_json and input_json["byline"]:
if input_json.get("byline"):
article_json["byline"] = input_json["byline"]
if "date" in input_json and input_json["date"]:
if input_json.get("date"):
article_json["date"] = input_json["date"]
if "content" in input_json and input_json["content"]:
if input_json.get("content"):
article_json["content"] = input_json["content"]
article_json["plain_content"] = plain_content(article_json["content"], False, False)
article_json["plain_text"] = extract_text_blocks_as_plain_text(article_json["plain_content"])
if "textContent" in input_json and input_json["textContent"]:
if input_json.get("textContent"):
article_json["plain_text"] = input_json["textContent"]
article_json["plain_text"] = re.sub(r'\n\s*\n', '\n', article_json["plain_text"])