Giter VIP home page Giter VIP logo

Comments (7)

mikes-hobbies avatar mikes-hobbies commented on May 24, 2024 1

Thank you so much the change worked. I am closing this issue.

from aws-sam-cli.

jysheng123 avatar jysheng123 commented on May 24, 2024

Hi, in a normal project are you able to import that module in the first place? This does not seem like a SAM specific issue, it looks like that your project normally can not find that module in the first place. Could you help verify that the package is installed and that you are able to use it normally without SAM? If so, then that would indicate that something in SAM may be the issue. Thanks

from aws-sam-cli.

mikes-hobbies avatar mikes-hobbies commented on May 24, 2024

Thank you for responding to this issue. I believe this is a SAM related issue. I am using the same code in a Flask app as I am in the SAM app (shown below). The response in the Flask app is a string (proving that its not a code related issue). In the SAM app I am receiving an error shown below.

Flask app:

from flask import Flask
import json
import boto3
from langchain_openai import OpenAI

app = Flask(__name__)

@app.route("/")
def hello_world():
    ssh_key = get_ssh_key() 
    if ssh_key:
        print("Retrieved SSH key:", ssh_key)
    response_body = {
        "OpenAISSHKey": ssh_key
    }
    llm = OpenAI()
    name = llm.invoke("What are some theories about the relationship between unemployment and inflation?")
    return name

def get_ssh_key():
    openai_sshkey = 'open-ai-ssh-key'
    ssm_client = boto3.client('ssm')
    try:
        response = ssm_client.get_parameter(
            Name=openai_sshkey,
            WithDecryption=True 
        )
        return response['Parameter']['Value']
    except ssm_client.exceptions.ParameterNotFound as e:
        print("SSH key parameter not found:", e)
        return None
    except Exception as e:
        print("Error retrieving SSH key:", e)
        return None

Flask Endpoint output:

  1. Phillips Curve: This theory suggests an inverse relationship between unemployment and inflation. As unemployment decreases, wages increase, leading to higher demand for goods and services and thus, higher prices. 2. Demand-Pull Inflation: This theory states that when unemployment is low, consumer demand for goods and services increases, leading to an increase in prices. 3. Cost-Push Inflation: This theory suggests that when unemployment is high, businesses are unable to increase prices due to weak demand. As a result, they may reduce production costs by cutting wages, leading to a decrease in consumer purchasing power and lower prices. 4. Modern Monetary Theory: This theory argues that there is no direct relationship between unemployment and inflation and that inflation is determined by the government's spending and taxation policies. 5. Structural Unemployment: This theory suggests that unemployment and inflation are not directly related. Instead, structural unemployment, caused by changes in the economy or technological advancements, can lead to inflation if businesses need to increase prices to cover the costs of adapting to these changes. 6. Rational Expectations Theory: This theory argues that people's expectations about future inflation can influence their behavior and lead to changes in the current inflation rate. 7. Natural Rate of Unemployment: According to this theory, there is a

SAM app:

import json
import boto3
from langchain_openai import OpenAI

def first_time_users(event, context):
    ssh_key = get_ssh_key() 
    if ssh_key:
        print("Retrieved SSH key:", ssh_key)
    response_body = {
        "OpenAISSHKey": ssh_key
    }
    llm = OpenAI()
    name = llm.invoke("What are some theories about the relationship between unemployment and inflation?")
    return {
        "statusCode": 200,
        "body": name,
    }

def get_ssh_key():
    openai_sshkey = 'open-ai-ssh-key'
    ssm_client = boto3.client('ssm')
    try:
        response = ssm_client.get_parameter(
            Name=openai_sshkey,
            WithDecryption=True 
        )
        return response['Parameter']['Value']
    except ssm_client.exceptions.ParameterNotFound as e:
        print("SSH key parameter not found:", e)
        return None
    except Exception as e:
        print("Error retrieving SSH key:", e)
        return None

SAM Endpoint Output:
{"message":"Internal server error"}

from aws-sam-cli.

jysheng123 avatar jysheng123 commented on May 24, 2024

Hi, I see have you ran sam build before running sam start local-api?

from aws-sam-cli.

mikes-hobbies avatar mikes-hobbies commented on May 24, 2024

Hi, thank you for the response. Yes I ran sam build before running sam start local-api

from aws-sam-cli.

mndeveci avatar mndeveci commented on May 24, 2024

Hi there,

I've tried to re-produce this issue. I've the following files in my project;

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: sam-app

Globals:
  Function:
    Timeout: 3
    MemorySize: 128

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
hello_world/requirements.txt
langchain==0.1.1
langchain-openai==0.0.3
hello_world/app.py
import json
import boto3
from langchain_openai import OpenAI
import requests


def lambda_handler(event, context):
    ssh_key = get_ssh_key()
    if ssh_key:
        print("Retrieved SSH key:", ssh_key)
    response_body = {
        "OpenAISSHKey": ssh_key
    }
    llm = OpenAI()
    name = llm.invoke(
        "What are some theories about the relationship between unemployment and inflation?"
    )
    return {
        "statusCode": 200,
        "body": name,
    }

def get_ssh_key():
    openai_sshkey = 'open-ai-ssh-key'
    ssm_client = boto3.client('ssm')
    try:
        response = ssm_client.get_parameter(
            Name=openai_sshkey,
            WithDecryption=True
        )
        return response['Parameter']['Value']
    except ssm_client.exceptions.ParameterNotFound as e:
        print("SSH key parameter not found:", e)
        return None
    except Exception as e:
        print("Error retrieving SSH key:", e)
        return None

With this setup, if I run sam build && sam local start-api and then curl the endpoint, I've got the following error.

Invalid lambda response received: Invalid API Gateway Response Keys: {'errorMessage', 'stackTrace', 'requestId', 'errorType'} in {'errorMessage': "Unable to import module 'app': cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_'
(/var/task/urllib3/util/ssl_.py)", 'errorType': 'Runtime.ImportModuleError', 'requestId': '749d3de4-9e5b-4ade-bdce-6582a53297c3', 'stackTrace': []}

boto3 library still requires urllib3 v1, so I've added that into my requirements.txt file;

urllib3<2
langchain==0.1.1
langchain-openai==0.0.3

And after adding this I was able to invoke lambda function with no import errors. It now fails since I didn't provide OpenAI API Key but I assume you already set it in your application.

  Did not find openai_api_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass `openai_api_key` as a named parameter. (type=value_error)

Can you add this urllib3<2 to your requirements file and see if it works?

from aws-sam-cli.

github-actions avatar github-actions commented on May 24, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

from aws-sam-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.