我开发的函数应用程序在使用 .get() 方法从字典中提取数据时突然出现错误。这是我使用的示例代码:
def sendOpenAIPromptRequest(azureopenai_endpoint, azureopenai_deployment,azureopenai_token, azureopenai_version, workItemDetails):
workItemSummary = []
#Iteration per object in workItemDetails
for item in workItemDetails:
openai_prompt = openAIPrompt(item)
try:
url = f"{azureopenai_endpoint}/openai/deployments/{azureopenai_deployment}/chat/completions?{azureopenai_version}"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {azureopenai_token}"
}
body = {
"max_tokens": 590,
"messages": [
{
"role": "user",
"content": openai_prompt
}
],
"temperature": 0.25
}
response = requests.post(url=url, headers=headers, json=body)
print(response)
responseContent = json.loads(response.text)
except:
return print("An error has occurred.")
container = {
"UserStoryId": item.get("UserStoryId", ""),
"UserStoryTitle": item.get("UserStoryTitle", ""),
"UserStoryState":item.get("UserStoryState", ""),
"UserStoryType": item.get("UserStoryType", ""),
"Summary": responseContent['choices'][0]['message']['content']
}
workItemSummary.append(container)
return json.dumps({"workItemSummaryValue": workItemSummary})
workItemDetails 是一个字典列表,我需要将其数据传输到“容器”变量。
workItemDetails = [
{
"UserStoryId": "id1",
"UserStoryTitle": "title1",
"UserStoryState": "state1",
"UserStoryType": "type1"
"RawUpdates": "updates1",
"RawComments": "comments1"
},
{
"UserStoryId": "id2",
"UserStoryTitle": "title2",
"UserStoryState": "state2",
"UserStoryType": "type2"
"RawUpdates": "updates2",
"RawComments": "comments2"
}]
过去几周工作正常,但当我从 VScode 或已部署的应用程序再次运行它时,它将返回此错误:
[2023-11-09T23:41:43.106Z] Executed 'Functions.MyFunctionApp' (Failed, Id=d91f7dc1-8c42-477d-a506-6128fd5e6898, Duration=10120ms) [2023-11-09T23:41:43.109Z] System.Private.CoreLib: Exception while executing function: Functions.MyFunctionApp. System.Private.CoreLib: Result: Failure Exception: AttributeError: 'str' object has no attribute 'get'
请帮我解决这个问题。
我需要将数据从字典传输到 Azure Function 应用程序中的另一个字典变量。
我假设你的
workItemDetails
是这样的:
workItemDetails = ['{ "UserStoryId": "id1","UserStoryTitle": "title1","UserStoryState": "state1", "UserStoryType": "type1","RawUpdates": "updates1","RawComments": "comments1"}',
'{"UserStoryId": "id2","UserStoryTitle": "title2","UserStoryState": "state2","UserStoryType": "type2","RawUpdates": "updates2","RawComments": "comments2"}']
现在,如果你这样做:
for item in workItemDetails:
container = {
"UserStoryId": item.get("UserStoryId", ""),
"UserStoryTitle": item.get("UserStoryTitle", ""),
"UserStoryState":item.get("UserStoryState", ""),
"UserStoryType": item.get("UserStoryType", ""),
}
你会得到这个错误:
AttributeError Traceback (most recent call last)
<ipython-input-12-a611593be7a6> in <cell line: 1>()
1 for item in workItemDetails:
2 container = {
----> 3 "UserStoryId": item.get("UserStoryId", ""),
4 "UserStoryTitle": item.get("UserStoryTitle", ""),
5 "UserStoryState":item.get("UserStoryState", ""),
AttributeError: 'str' object has no attribute 'get'
解决方法:
from ast import literal_eval
workItemDetails = [literal_eval(item) for item in workItemDetails]
for item in workItemDetails:
container = {
"UserStoryId": item.get("UserStoryId", ""),
"UserStoryTitle": item.get("UserStoryTitle", ""),
"UserStoryState":item.get("UserStoryState", ""),
"UserStoryType": item.get("UserStoryType", ""),
}
print(container)
{'UserStoryId': 'id2',
'UserStoryTitle': 'title2',
'UserStoryState': 'state2',
'UserStoryType': 'type2'}