Comments (7)
Thank you so much the change worked. I am closing this issue.
from aws-sam-cli.
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.
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:
- 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.
Hi, I see have you ran sam build
before running sam start local-api
?
from aws-sam-cli.
Hi, thank you for the response. Yes I ran sam build
before running sam start local-api
from aws-sam-cli.
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.
⚠️ 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)
- SAM Does not build the application, returns "PythonPipBuilder:ResolveDependencies failed HOT 6
- Bug: Missing DockerContext doesn't fail validation HOT 3
- `sam init` fails to deploy a standard sample on Windows platform HOT 3
- Bug: sam local start-api not working in a docker-out-of-docker setup, due to an empty /var/task directory. HOT 3
- Bug: TITLE"Invalid API identifier specified" when attaching resources to an existing AWS::ApiGatewayV2::Api HOT 2
- Bug: sam build - UnsupportedBuilderException HOT 3
- SAM build crashes using terraform HOT 9
- Bug: sam local start-api - AttributeError HOT 9
- SAM local invocation with CDK generated template with typescript not able to find module index (index.mjs) HOT 5
- How to add tags with sam deploy HOT 12
- Bug: Got issue when using 1.116.0_1 for deployment HOT 2
- Bug: [WinError 3] The system cannot find the path specified HOT 4
- Feature request: Deploy Lambdas with concurrency HOT 3
- Bug: sam sync with python causes endless cpu saturation HOT 11
- Bug: sam build - EmptyKeyError HOT 1
- Bug: Unable to run sam local invoke on Apple Silicon HOT 16
- Cannot sam local start-api without specifying an image HOT 3
- Bug: sam local start-api - ReadTimeout HOT 3
- Bug: "Timed out while attempting to establish a connection to the container" error after fresh install of Docker and AWS SAM HOT 7
- Bug: sam build - JSONDecodeError HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-sam-cli.