k4l1sh / alexa-gpt

A tutorial on how to use ChatGPT in Alexa
MIT License
204 stars 44 forks source link

Error generating response: 'choices'? #13

Closed hashnag closed 7 months ago

hashnag commented 7 months ago

I'm not very familiar with Amazon developer console or a frequent user of OpenAI API. So there is a 99.9% chance I'm doing something stupid. However:

I was able to import the project using the Import Skill feature. I was able to input my key into the api_key variable in lambda_function.py as api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" I was able to verify the requirements.txt, lambda_function.py, and JSON content all match the project here. I am able to Build and Deploy the Skill without any errors. I did get an error complaining chat was too generic an invocation and had to select an alternate invocation. I settled on "chat g p t" and from within test when I type "open chat g p t" I get the expected "Chat G.P.T. mode activated".

However once Chat G.P.T. mode is activated the only responses I get are Error generating response: 'choices'

I did generate a new API key from OpenAI with All permissions. I have OpenAI API funds available from a previous Tasker project.

Any help would be appreciated.

hashnag commented 7 months ago
{
    "version": "1.0",
    "session": {
        "new": false,
        "sessionId": "amzn1.echo-api.session.XXXX",
        "application": {
            "applicationId": "amzn1.ask.skill.XXXX"
        },
        "attributes": {
            "chat_history": []
        },
        "user": {
            "userId": "amzn1.ask.account.XXXX"
        },
        "affiliatedResources": []
    },
    "context": {
        "Viewports": [
            {
                "type": "APL",
                "id": "main",
                "shape": "RECTANGLE",
                "dpi": 213,
                "presentationType": "STANDARD",
                "canRotate": false,
                "configuration": {
                    "current": {
                        "mode": "HUB",
                        "video": {
                            "codecs": [
                                "H_264_42",
                                "H_264_41"
                            ]
                        },
                        "size": {
                            "type": "DISCRETE",
                            "pixelWidth": 1280,
                            "pixelHeight": 800
                        }
                    }
                }
            }
        ],
        "Viewport": {
            "experiences": [
                {
                    "arcMinuteWidth": 346,
                    "arcMinuteHeight": 216,
                    "canRotate": false,
                    "canResize": false
                }
            ],
            "mode": "HUB",
            "shape": "RECTANGLE",
            "pixelWidth": 1280,
            "pixelHeight": 800,
            "dpi": 213,
            "currentPixelWidth": 1280,
            "currentPixelHeight": 800,
            "touch": [
                "SINGLE"
            ],
            "video": {
                "codecs": [
                    "H_264_42",
                    "H_264_41"
                ]
            }
        },
        "Extensions": {
            "available": {
                "aplext:backstack:10": {}
            }
        },
        "Advertising": {
            "advertisingId": "00000000-0000-0000-0000-000000000000",
            "limitAdTracking": true
        },
        "System": {
            "application": {
                "applicationId": "amzn1.ask.skill.XXXX"
            },
            "user": {
                "userId": "amzn1.ask.account.XXXX"
            },
            "device": {
                "deviceId": "amzn1.ask.device.XXXX",
                "supportedInterfaces": {}
            },
            "apiEndpoint": "https://api.amazonalexa.com",
            "apiAccessToken": "XXXX"
        }
    },
    "request": {
        "type": "IntentRequest",
        "requestId": "amzn1.echo-api.request.XXXX",
        "locale": "en-US",
        "timestamp": "2024-02-27T05:51:27Z",
        "intent": {
            "name": "GptQueryIntent",
            "confirmationStatus": "NONE",
            "slots": {
                "query": {
                    "name": "query",
                    "value": "who are you",
                    "resolutions": {
                        "resolutionsPerAuthority": [
                            {
                                "authority": "AlexaEntities",
                                "status": {
                                    "code": "ER_SUCCESS_NO_MATCH"
                                }
                            }
                        ]
                    },
                    "confirmationStatus": "NONE",
                    "source": "USER",
                    "slotValue": {
                        "type": "Simple",
                        "value": "who are you",
                        "resolutions": {
                            "resolutionsPerAuthority": [
                                {
                                    "authority": "AlexaEntities",
                                    "status": {
                                        "code": "ER_SUCCESS_NO_MATCH"
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    }
}
{
    "body": {
        "version": "1.0",
        "response": {
            "outputSpeech": {
                "type": "SSML",
                "ssml": "<speak>Error generating response: 'choices'</speak>"
            },
            "reprompt": {
                "outputSpeech": {
                    "type": "SSML",
                    "ssml": "<speak>Any other questions?</speak>"
                }
            },
            "shouldEndSession": false,
            "type": "_DEFAULT_RESPONSE"
        },
        "sessionAttributes": {
            "chat_history": [
                [
                    "who are you",
                    "Error generating response: 'choices'"
                ]
            ]
        },
        "userAgent": "ask-python/1.11.0 Python/3.8.18"
    }
}
k4l1sh commented 7 months ago

This 'choices' error was just an Exception due to lack of error handling in the request, as we had to change the OpenAI client to Requests to fix issue #12 Could be any error, like invalid API key, invalid payment method, maximum quota of requests reached, or any other error. I just commited 65945ae to improve the error handling, so if you update the lambda_function.py you should see the real error in the response, or just change generate_gpt_response() to this code:

def generate_gpt_response(chat_history, new_question):
    """Generates a GPT response to a new question"""
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    url = "https://api.openai.com/v1/chat/completions"
    messages = [{"role": "system", "content": "You are a helpful assistant. Answer in 50 words or less."}]
    for question, answer in chat_history[-10:]:
        messages.append({"role": "user", "content": question})
        messages.append({"role": "assistant", "content": answer})
    messages.append({"role": "user", "content": new_question})

    data = {
        "model": "gpt-3.5-turbo-0125",
        "messages": messages,
        "max_tokens": 300,
        "temperature": 0.5
    }
    try:
        response = requests.post(url, headers=headers, data=json.dumps(data))
        response_data = response.json()
        if response.ok:
            return response_data['choices'][0]['message']['content']
        else:
            return f"Error {response.status_code}: {response_data['error']['message']}"
    except Exception as e:
        return f"Error generating response: {str(e)}"
hashnag commented 7 months ago

I still appear to be getting the same thing.

{
    "header": {
        "namespace": "SkillDebugger",
        "name": "CaptureDebuggingInfo",
        "messageId": "98b3eece-7e7f-425a-9a3e-7d23f6cef62e"
    },
    "payload": {
        "skillId": "amzn1.ask.skill.xxxx",
        "timestamp": "2024-02-28T01:19:36.528Z",
        "dialogRequestId": "692f068d-7a46-4607-8626-6657c25d8eac",
        "skillRequestId": "amzn1.echo-api.request.xxxx",
        "type": "SkillExecutionInfo",
        "content": {
            "invocationRequest": {
                "endpoint": "arn:aws:lambda:us-east-1:795224922580:function:7c8c7ddc-7575-4d70-9408-4b4fad6d1716:Release_0",
                "body": {
                    "version": "1.0",
                    "session": {
                        "new": false,
                        "sessionId": "amzn1.echo-api.session.xxxx",
                        "application": {
                            "applicationId": "amzn1.ask.skill.xxxx"
                        },
                        "attributes": {
                            "chat_history": []
                        },
                        "user": {
                            "userId": "amzn1.ask.account.xxxx"
                        },
                        "affiliatedResources": []
                    },
                    "context": {
                        "Viewports": [
                            {
                                "type": "APL",
                                "id": "main",
                                "shape": "RECTANGLE",
                                "dpi": 213,
                                "presentationType": "STANDARD",
                                "canRotate": false,
                                "configuration": {
                                    "current": {
                                        "mode": "HUB",
                                        "video": {
                                            "codecs": [
                                                "H_264_42",
                                                "H_264_41"
                                            ]
                                        },
                                        "size": {
                                            "type": "DISCRETE",
                                            "pixelWidth": 1280,
                                            "pixelHeight": 800
                                        }
                                    }
                                }
                            }
                        ],
                        "Viewport": {
                            "experiences": [
                                {
                                    "arcMinuteWidth": 346,
                                    "arcMinuteHeight": 216,
                                    "canRotate": false,
                                    "canResize": false
                                }
                            ],
                            "mode": "HUB",
                            "shape": "RECTANGLE",
                            "pixelWidth": 1280,
                            "pixelHeight": 800,
                            "dpi": 213,
                            "currentPixelWidth": 1280,
                            "currentPixelHeight": 800,
                            "touch": [
                                "SINGLE"
                            ],
                            "video": {
                                "codecs": [
                                    "H_264_42",
                                    "H_264_41"
                                ]
                            }
                        },
                        "Extensions": {
                            "available": {
                                "aplext:backstack:10": {}
                            }
                        },
                        "Advertising": {
                            "advertisingId": "00000000-0000-0000-0000-000000000000",
                            "limitAdTracking": true
                        },
                        "System": {
                            "application": {
                                "applicationId": "amzn1.ask.skill.xxxx"
                            },
                            "user": {
                                "userId": "amzn1.ask.account.xxxx"
                            },
                            "device": {
                                "deviceId": "amzn1.ask.device.xxxx",
                                "supportedInterfaces": {}
                            },
                            "apiEndpoint": "https://api.amazonalexa.com",
                            "apiAccessToken": "xxxx"
                        }
                    },
                    "request": {
                        "type": "IntentRequest",
                        "requestId": "amzn1.echo-api.request.xxxx",
                        "locale": "en-US",
                        "timestamp": "2024-02-28T01:19:36Z",
                        "intent": {
                            "name": "GptQueryIntent",
                            "confirmationStatus": "NONE",
                            "slots": {
                                "query": {
                                    "name": "query",
                                    "value": "who are you",
                                    "resolutions": {
                                        "resolutionsPerAuthority": [
                                            {
                                                "authority": "AlexaEntities",
                                                "status": {
                                                    "code": "ER_SUCCESS_NO_MATCH"
                                                }
                                            }
                                        ]
                                    },
                                    "confirmationStatus": "NONE",
                                    "source": "USER",
                                    "slotValue": {
                                        "type": "Simple",
                                        "value": "who are you",
                                        "resolutions": {
                                            "resolutionsPerAuthority": [
                                                {
                                                    "authority": "AlexaEntities",
                                                    "status": {
                                                        "code": "ER_SUCCESS_NO_MATCH"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "invocationResponse": {
                "body": {
                    "version": "1.0",
                    "response": {
                        "outputSpeech": {
                            "type": "SSML",
                            "ssml": "<speak>Error generating response: 'choices'</speak>"
                        },
                        "reprompt": {
                            "outputSpeech": {
                                "type": "SSML",
                                "ssml": "<speak>Any other questions?</speak>"
                            }
                        },
                        "shouldEndSession": false,
                        "type": "_DEFAULT_RESPONSE"
                    },
                    "sessionAttributes": {
                        "chat_history": [
                            [
                                "who are you",
                                "Error generating response: 'choices'"
                            ]
                        ]
                    },
                    "userAgent": "ask-python/1.11.0 Python/3.8.18"
                }
            },
            "metrics": {
                "skillExecutionTimeInMilliseconds": 138
            }
        }
    }
}

If this is just me being a brick, don't spend too much time on it.

k4l1sh commented 7 months ago

This error "Error generating response: 'choices'" occurs because in the part of the code where you there is response_data['choices'][0]['message']['content'] it is not recognizing the "choices" key in the response. In the code I provided, this error should not appear. Make sure you clicked on "Save" and "Deploy"

If it is still happening, you could try debugging making it by making it only show the response_data, like replacing response_data['choices'][0]['message']['content'] with json.dumps(response_data)

A successful response in chatgpt api should look like like this:

{
    "id": "chatcmpl-abc123",
    "object": "chat.completion",
    "created": 1677858242,
    "model": "gpt-3.5-turbo-0613",
    "usage": {
        "prompt_tokens": 13,
        "completion_tokens": 7,
        "total_tokens": 20
    },
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "\n\nThis is a test!"
            },
            "logprobs": null,
            "finish_reason": "stop",
            "index": 0
        }
    ]
}

But if there is an error, it should be something like this:

{
    "error": {
        "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

That is why it didn't recognized 'choices'

hashnag commented 7 months ago

Huh. I updated the code and it worked great. I'm not sure what was originally wrong but thanks for your help and patience!