-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
318 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
import json | ||
import traceback | ||
import openai | ||
import boto3 | ||
from typing import Any, Dict | ||
from dotenv import dotenv_values | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
config = { | ||
**dotenv_values(os.path.join(os.path.dirname(__file__), '..', '.env')), | ||
**os.environ, | ||
} | ||
|
||
openai.api_key = config.get('OPENAI_API_KEY') | ||
BUCKET_NAME = config.get('BUCKET_NAME') | ||
|
||
|
||
def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]: | ||
try: | ||
uid = event.get('requestContext', {}).get( | ||
'authorizer', {}).get('principalId') | ||
|
||
body = event.get('body') | ||
|
||
if body is None: | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({'message': 'please provide POST body'}), | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
|
||
body = json.loads(body) | ||
key = body.get('key') | ||
|
||
s3_resource = boto3.resource('s3') | ||
|
||
s3_object = s3_resource.Object(BUCKET_NAME, key) | ||
s3_response = s3_object.get() | ||
file_response = openai.File.create(file=s3_response['Body'], purpose='fine-tune') | ||
file_id = file_response.get('id', '') | ||
finetune_response = openai.FineTune.create(training_file=file_id) | ||
new_s3_object = s3_resource.Object(BUCKET_NAME, f'{uid}/{file_id}') | ||
new_s3_object.copy_from(CopySource=key) | ||
s3_object.delete() | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(finetune_response), | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
except Exception: | ||
traceback.print_exc() | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({'message': 'error occurred, please check logs'}), | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
print(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import json | ||
import uuid | ||
import traceback | ||
import boto3 | ||
from typing import Any, Dict | ||
from dotenv import dotenv_values | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
config = { | ||
**dotenv_values(os.path.join(os.path.dirname(__file__), '..', '.env')), | ||
**os.environ, | ||
} | ||
|
||
BUCKET_NAME = config.get('BUCKET_NAME') | ||
|
||
|
||
def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]: | ||
try: | ||
uid = event.get('requestContext', {}).get( | ||
'authorizer', {}).get('principalId') | ||
|
||
s3_client = boto3.client('s3') | ||
|
||
object_key = f'{uid}/{str(uuid.uuid4())}.jsonl' | ||
|
||
response = s3_client.generate_presigned_post(BUCKET_NAME, object_key) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(response), | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
except Exception: | ||
traceback.print_exc() | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({'message': 'error occurred, please check logs'}), | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
print(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
{ | ||
"name": "openai-serverless", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "OpenAI Serverless Functions", | ||
"main": "index.js", | ||
"author": "Xanthous Tech <hi@x-tech.io>", | ||
"license": "ISC", | ||
"private": true, | ||
"scripts": { | ||
"postinstall": "patch-package" | ||
}, | ||
"devDependencies": { | ||
"dotenv-cli": "^4.0.0", | ||
"patch-package": "^6.4.7", | ||
"serverless": "^2.44.0", | ||
"serverless-domain-manager": "^5.1.0", | ||
"serverless-offline": "^7.0.0", | ||
"serverless-plugin-additional-stacks": "^1.5.0", | ||
"serverless-python-requirements": "^5.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
diff --git a/node_modules/serverless-plugin-additional-stacks/index.js b/node_modules/serverless-plugin-additional-stacks/index.js | ||
index d5f1379..0b1eff9 100644 | ||
--- a/node_modules/serverless-plugin-additional-stacks/index.js | ||
+++ b/node_modules/serverless-plugin-additional-stacks/index.js | ||
@@ -58,6 +58,7 @@ class AdditionalStacksPlugin { | ||
usage: 'Additional stack name to deploy', | ||
shortcut: 'k', | ||
required: false, | ||
+ type: 'string', | ||
}, | ||
}, | ||
}, | ||
@@ -66,6 +67,7 @@ class AdditionalStacksPlugin { | ||
'skip-additionalstacks': { | ||
usage: 'Skip deploying additional stacks', | ||
required: false, | ||
+ type: 'boolean', | ||
}, | ||
}, | ||
}, | ||
@@ -81,6 +83,7 @@ class AdditionalStacksPlugin { | ||
usage: 'Additional stack name to remove', | ||
shortcut: 'k', | ||
required: false, | ||
+ type: 'string', | ||
}, | ||
}, | ||
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
stack: | ||
StackName: ${self:custom.prefix}-dynamodb | ||
Resources: | ||
UsageTable: | ||
Type: AWS::DynamoDB::Table | ||
Properties: | ||
TableName: ${self:custom.prefix}-usage | ||
AttributeDefinitions: | ||
- AttributeName: uid | ||
AttributeType: S | ||
- AttributeName: timestamp | ||
AttributeType: N | ||
KeySchema: | ||
- AttributeName: uid | ||
KeyType: HASH | ||
- AttributeName: timestamp | ||
KeyType: RANGE | ||
ProvisionedThroughput: | ||
ReadCapacityUnits: 5 | ||
WriteCapacityUnits: 5 | ||
Outputs: | ||
UsageTableName: | ||
Value: | ||
Ref: UsageTable | ||
Export: | ||
Name: ${self:custom.prefix}-usage-table-name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# TODO: make these permissions more granular, this is too broad | ||
Resources: | ||
lambdaRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
Description: Role for running proper actions in lambda functions. | ||
RoleName: ${self:custom.prefix}-lambda-role | ||
AssumeRolePolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: 'Allow' | ||
Principal: | ||
Service: | ||
- 'lambda.amazonaws.com' | ||
Action: | ||
- 'sts:AssumeRole' | ||
ManagedPolicyArns: | ||
- 'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess' | ||
- 'arn:aws:iam::aws:policy/AmazonSNSFullAccess' | ||
- 'arn:aws:iam::aws:policy/CloudWatchLogsFullAccess' | ||
- 'arn:aws:iam::aws:policy/AmazonS3FullAccess' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
stack: | ||
StackName: ${self:custom.prefix}-s3 | ||
Resources: | ||
FinetuneDataBucket: | ||
Type: AWS::S3::Bucket | ||
Properties: | ||
BucketName: ${self:custom.prefix}-finetune-data-set | ||
Outputs: | ||
FinetuneDataBucketName: | ||
Value: | ||
Ref: FinetuneDataBucket | ||
Export: | ||
Name: ${self:custom.prefix}-finetune-data-bucket-name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.