Error:Unable to import module 'lambda_function': No module named 'openai'"

0

I created a lambda function calling openai gpt mode. I added an openai layer to the function. In creating the zip file for the layer, I created a virtual environment first, installed the openai 0.27.0 version, and then zipped all content under the site_packages folder. After creating a layer using the zip file, the function test gave me the error message in the title. Not sure where I did wrong. Appreciate your insights in advance!

Below lists the details of the lambda function:

run time: python 3.11 partial snippet of the lambda function: import json import os import boto3 from openai import OpenAI

client=OpenAI()

Lin
질문됨 4달 전288회 조회
4개 답변
2

I'd suggest you to follow this process and comment here, how it goes for you:

  1. Create openai layer by following this re:Post Knowledge Center Article How can I create a layer for my Lambda Python function?. Payload for the lambda function to create openai layer would be as below:

           {
             "dependencies": {
               "openai": "== 0.27.0"
             },
             "layer": {
               "name": "openai-lambda-layer",
               "description": "this layer contains requests, operations libraries",
               "compatible-runtimes": [
                 "python3.11"
               ],
               "license-info": "MIT"
             }
           }
    
  2. With the above payload, run the lambda function, which would create layer.

Enter image description here

  1. Now layer is created and you can use it in your own lambda function, where you wanted to use openai module. Just go to your lambda function -> code -> Scroll down -> Add a layer -> Add newly created openai layer

Enter image description here

  1. Once you add the layer, you can use this module in your lambda function code as I did below(no error reporting that openai module not found):

Enter image description here

Enter image description here

Comment here if you have additional questions, happy to help.

Abhishek

profile pictureAWS
전문가
답변함 4달 전
1
수락된 답변

Terrific! I was able to get rid of the import error. You rock, Abhishek !

I still couldn't execute the function, though. I got another error message: can't imprt name 'OpenAI' from 'openai' (/opt/python/openai/init.py)". Any advice on how I could fix this issue would be great, Abhishek!

Lin

Lin
답변함 4달 전
profile picture
전문가
검토됨 2달 전
0

I did some exploration, and found that if I didn't import OpenAI from openai, the function was fine. Guess it might be an issue between openai and python. I will keep investigating. However, if you have any suggestions, I am all ears.

Lin
답변함 4달 전
  • Hi Lin, I'm not sure, how did I miss the link in my original answer, hence edited it and provided you the link for the process to create layer. To your second question about the error: "can't imprt name 'OpenAI' from 'openai' (/opt/python/openai/init.py)". I'd suggest you to create a new layer with latest version of openai, payload to create the layer with latest version of openai, would look like as below:

       {
         "dependencies": {
           "openai": "latest"
         },
         "layer": {
           "name": "openai-lambda-layer",
           "description": "this layer contains requests, operations libraries",
           "compatible-runtimes": [
             "python3.11"
           ],
           "license-info": "MIT"
         }
       }
    

    Then add this layer to your function and see how it goes.

0

It worked, Abhishek! Appreciate your prompt advice!

Lin

Lin
답변함 4달 전
  • Glad it worked. For community's better experience, it'd be great if you could mark the first answer as accepted one like you did at first place. Thanks.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠