Switching to GPT-4 in AWS Lambda Integration

Hi everyone,

I’m reaching out for some guidance and apologize in advance if I’m overlooking something obvious. I’ve been working on integrating ChatGPT into an e-learning course and have encountered an issue that’s left me a bit stumped.

So far, I’ve successfully created an AWS Lambda function that integrates with GPT-3.5-turbo. This setup is working well, but I’m looking to enhance the capabilities by switching to GPT-4. However, when I try to change the model to GPT-4 or adjusting the max-tokens parameter in my Python code (keeping everything else constant) results in a 500 Internal Server Error.

I haven’t made any other changes to the code, which leaves me puzzled about the source of this issue. Has anyone else encountered a similar problem, or does anyone have insights into what might be going wrong?

Any advice or suggestions would be greatly appreciated!

Without any code it’s really hard. Share at least the part of the code which does the actual API talking.

Of course:

import os
import json
import logging
import traceback
from openai import OpenAI

# Initialize the OpenAI client with your API key
client = OpenAI(api_key=os.getenv('GPT_API_KEY'))

def lambda_handler(event, context):
    try:
        # Parse the incoming JSON payload
        body = json.loads(event['body'])
        system_role = body.get('systemRole')  # Use .get() to avoid KeyError
        sl_question = body.get('SLQuestion')  # Use .get() to avoid KeyError

        if system_role is None or sl_question is None:
            # Return a 400 Bad Request response if input is missing
            return {
                'statusCode': 400,
                'body': json.dumps({
                    'error': 'Missing systemRole or SLQuestion in the request',
                    'AnswerRetrieved': False
                }),
                'headers': {
                    'Content-Type': 'application/json',
                    'Access-Control-Allow-Origin': '*'
                }
            }

        # Construct the chat messages with roles
        messages = [
            {"role": "system", "content": system_role},
            {"role": "user", "content": sl_question}
        ]

        # Create the chat completion
        completion = client.chat.completions.create(
            model="gpt-4",
            messages=messages,
            max_tokens=800
        )

        # Use model_dump() to access the content of the response
        chat_gpt_response = completion.choices[0].message.content

        # Return the response in JSON format
        return {
            'statusCode': 200,
            'body': json.dumps({
                'GPTAnswer': chat_gpt_response,
                'AnswerRetrieved': True
            }),
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            }
        }

   except Exception as e:
        # Log the error with stack trace to CloudWatch Logs
        logging.error(f"Error in ChatGPT Function: {str(e)}")
        logging.error("Stack Trace:", exc_info=True)
        
        # Return a 500 server error response
        return {
            'statusCode': 500,
            'body': json.dumps({
                'error': 'Error processing your request',
                'AnswerRetrieved': False
            }),
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*' 
            }
        }

Your code does look correct to me at least.

I’m starting to think you’re trying to process more token that it has available, and thus it errors out.
Also, stupid question, but do you have access to gpt-4? Also, take a look at the gpt-4-1106-preview model as it’s cheaper and sometimes faster.

Also, what’s the output of this line:

logging.error(f"Error in ChatGPT Function: {str(e)}")

Thank you @Fusseldieb. I’ve increased my max-tokens value and changed the model When I looked at the logs, there is this error:

[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module ‘index’: unindent does not match any outer indentation level (index.py, line 60)
Traceback (most recent call last):
File “/var/task/index.py” Line 60
except Exception as e:

However, I can’t find an indent problem with the code, so I’m not sure what’s causing this. Here’s the updated code:

import os
import json
import logging
import traceback
from openai import OpenAI

# Initialize the OpenAI client with your API key
client = OpenAI(api_key=os.getenv('GPT_API_KEY'))

def lambda_handler(event, context):
    try:
        # Parse the incoming JSON payload
        body = json.loads(event['body'])
        system_role = body.get('systemRole')  # Use .get() to avoid KeyError
        sl_question = body.get('SLQuestion')  # Use .get() to avoid KeyError

        if system_role is None or sl_question is None:
            # Return a 400 Bad Request response if input is missing
            return {
                'statusCode': 400,
                'body': json.dumps({
                    'error': 'Missing systemRole or SLQuestion in the request',
                    'AnswerRetrieved': False
                }),
                'headers': {
                    'Content-Type': 'application/json',
                    'Access-Control-Allow-Origin': '*'
                }
            }

        # Construct the chat messages with roles
        messages = [
            {"role": "system", "content": system_role},
            {"role": "user", "content": sl_question}
        ]

        # Create the chat completion
        completion = client.chat.completions.create(
            model="gpt-4-1106-preview model",
            messages=messages,
            max_tokens=1200
        )

        # Use model_dump() to access the content of the response
        chat_gpt_response = completion.choices[0].message.content

        # Return the response in JSON format
        return {
            'statusCode': 200,
            'body': json.dumps({
                'GPTAnswer': chat_gpt_response,
                'AnswerRetrieved': True
            }),
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            }
        }

   except Exception as e:
        # Log the error with stack trace to CloudWatch Logs
        logging.error(f"Error in ChatGPT Function: {str(e)}")
        logging.error("Stack Trace:", exc_info=True)
        
        # Return a 500 server error response
        return {
            'statusCode': 500,
            'body': json.dumps({
                'error': 'Error processing your request',
                'AnswerRetrieved': False
            }),
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*' 
            }
        }

PS: Unless I’m missing something, I should have access to GPT-4. I’m a Plus subscriber, and have loaded $10 worth of credits for testing in the API.

Aha! Now I am seeing it. Pasting it into an actual code editor reveals the true error:

image

You’re indenting your code wrong. Indentation is crucial in Python.

EDIT: Looking at the code, it seems that the only thing wrong is the “except” clause. Indent it with 1 more space, so it is at the same vertical “level” as “try”.
Also, pro tip: When indenting, don’t try to mix tabs with spaces, since this will give errors, too! Stick to one!

1 Like

Thank you so much!! The 4-space indent was the issue. I’ve added the spaced throughout and now it’s working.

1 Like

As this topic has a selected solution, closing topic.