Skip to content

Commit 1af879f

Browse files
authored
Merge pull request #28 from FullStackWithLawrence/next
Langchain integration
2 parents db78131 + d1b05cb commit 1af879f

32 files changed

+492
-157
lines changed

.mergify.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# see:
2+
# - https://github.com/FullStackWithLawrence/aws-openai/settings/installations
3+
# - https://docs.mergify.com/getting-started/
14
pull_request_rules:
25
- name: automatic approve dependabot pull requests
36
conditions:

.pre-commit-config.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ default_language_version:
33
python: python3.11
44
repos:
55
- repo: https://github.com/psf/black
6-
rev: 23.7.0
6+
rev: 23.10.1
77
hooks:
88
- id: black
99
- repo: https://github.com/pre-commit/pre-commit-hooks
10-
rev: v4.4.0
10+
rev: v4.5.0
1111
hooks:
1212
# See https://pre-commit.com/hooks.html for more hooks
1313
#- id: check-added-large-files
@@ -35,3 +35,9 @@ repos:
3535
# - id: shellcheck
3636
# - id: yapf
3737
# - id: markdown-link-check
38+
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
39+
rev: v9.5.0
40+
hooks:
41+
- id: commitlint
42+
stages: [commit-msg]
43+
additional_dependencies: ['@commitlint/config-angular']

CHANGELOG.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
# [0.4.0](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.3.1...v0.4.0) (2023-11-03)
1+
# CHANGE LOG
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
9+
## [0.4.0](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.3.1...v0.4.0) (2023-11-03)
210

311

412
### Bug Fixes
@@ -22,13 +30,6 @@
2230

2331
* revert to secrets.PAT ([7342489](https://github.com/FullStackWithLawrence/aws-openai/commit/7342489ef7b7537419cc12732c1739a9fc3b42a8))
2432

25-
# CHANGE LOG
26-
27-
All notable changes to this project will be documented in this file.
28-
29-
The format is based on [Keep a Changelog](http://keepachangelog.com/)
30-
and this project adheres to [Semantic Versioning](http://semver.org/).
31-
3233
## [0.3.0] (2023-11-01)
3334

3435
The YouTube video for this release: [AWS Lamba Layers: When and How to use them](https://youtu.be/5Jf34t_UlZA)

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ A [React](https://react.dev/) + [AWS Serverless](https://aws.amazon.com/serverle
99

1010
![React front end](https://github.com/FullStackWithLawrence/aws-openai/blob/main/doc/front-end.png)
1111

12-
1312
**IMPORTANT DISCLAIMER: AWS' Lambda service has a hard 29-second timeout. OpenAI API calls often take longer than this, in which case the AWS API Gateway endpoint will return a 504 "Gateway timeout error" response to the React client. This happens frequently with apps created using chatgpt-4. Each of the 30 OpenAI API example applications are nonetheless implemented exactly as they are specified in the official documentation.**
1413

1514
## ReactJS chat application
@@ -18,7 +17,7 @@ Complete documentation is located [here](./client/).
1817

1918
React app that leverages [Vite.js](https://github.com/FullStackWithLawrence/aws-openai), [@chatscope/chat-ui-kit-react](https://www.npmjs.com/package/@chatscope/chat-ui-kit-react), and [react-pro-sidebar](https://www.npmjs.com/package/react-pro-sidebar).
2019

21-
### Key features
20+
### Webapp Key features
2221

2322
- robust, highly customizable chat features
2423
- A component model for implementing your own highly personalized OpenAI apps
@@ -36,7 +35,7 @@ Complete documentation is located [here](./api/).
3635

3736
A REST API implementing each of the [30 example applications](https://platform.openai.com/examples) from the official [OpenAI API Documentation](https://platform.openai.com/docs/api-reference/making-requests?lang=python) using a modularized Terraform approach. Leverages OpenAI's suite of AI models, including [GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5), [GPT-4](https://platform.openai.com/docs/models/gpt-4), [DALL·E](https://platform.openai.com/docs/models/dall-e), [Whisper](https://platform.openai.com/docs/models/whisper), [Embeddings](https://platform.openai.com/docs/models/embeddings), and [Moderation](https://platform.openai.com/docs/models/moderation).
3837

39-
### Key features
38+
### API Key features
4039

4140
- Built on the [OpenAI API Python Library](https://pypi.org/project/openai/)
4241
- Customizable. [Modularized endpoints](./terraform/apigateway_endpoints.tf) that only take a few lines of code each.
@@ -66,6 +65,27 @@ A REST API implementing each of the [30 example applications](https://platform.o
6665

6766
Detailed documentation for each endpoint is available here: [Documentation](./doc/examples/)
6867

68+
## Examples of Code Management Best Practices
69+
70+
This repo is referenced by multiple YouTube videos, including various tutorials about good coding practices and good code management. Of note:
71+
72+
- [python-dotenv](https://pypi.org/project/python-dotenv/) for storing sensitive data for local development
73+
- [.gitignore](./.gitignore) ensures that no sensitive nor useless data accidentally gets pushed to GitHub.
74+
- [tox.ini](./tox.ini) file for configuring behaviors of Python testing tools
75+
- [GitHub Actions](https://github.com/features/actions) automates unit testing, semantic release rule checking, and dependabot actions.
76+
- [GitHub Secrets](https://github.com/FullStackWithLawrence/aws-openai/settings/secrets/actions) to provide sensitive data to Github Actions workflows
77+
- [GitHub Issues](https://github.com/features/issues)
78+
- [Makefile](./Makefile) automates procedures like init, build, test, release and linting for Python, ReactJS and Terraform.
79+
- [pre-commit](https://pre-commit.com/) automatically enforces a multitude of code quality, coding style and security policies.
80+
- [Dependabot](https://github.com/dependabot) automatically updates the version pins of code library dependencies for Python, ReactJS and Terraform.
81+
- [Unit Tests](https://docs.pytest.org/) are automated and can be invoked
82+
- manually from the command line
83+
- manually from GitHub Actions
84+
- automatically by Dependabot.
85+
- [Mergify](https://mergify.com/) automates processing of bot-created pull requests
86+
- [Semantic Release](https://github.com/semantic-release/semantic-release) automates version releases as well as maintains the [CHANGELOG](./CHANGELOG.md) for the repo.
87+
- [Change Log](http://keepachangelog.com/)
88+
6989
## Support
7090

7191
To get community support, go to the official [Issues Page](https://github.com/FullStackWithLawrence/aws-openai/issues) for this project.

api/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,15 @@ Example valid request body:
122122
{
123123
"role": "user",
124124
"content": "what is quantum computing?"
125-
}
125+
},
126+
{
127+
"role": "assistant",
128+
"content": "Quantum computing involves teeny tiny itsy bitsy atomic stuff"
129+
},
130+
{
131+
"role": "user",
132+
"content": "What??? I don't understand. Please provide a better explanation."
133+
},
126134
]
127135
}
128136
```

api/terraform/apigateway_endpoints.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ module "default_vr_fitness" {
350350
###############################################################################
351351
# 15. see https://platform.openai.com/examples/default-marv-sarcastic-chat
352352
# Marv is a factual chatbot that is also sarcastic.
353+
#
354+
# Note: migrated to Langchain in v0.5.0
353355
###############################################################################
354356
module "default_marv_sarcastic_chat" {
355357
source = "./endpoint"
@@ -366,8 +368,8 @@ module "default_marv_sarcastic_chat" {
366368
aws_region = var.aws_region
367369
aws_api_gateway_rest_api_parent_id = aws_api_gateway_resource.examples.id
368370
aws_api_gateway_rest_api_id = aws_api_gateway_rest_api.openai.id
369-
aws_lambda_function_openai_text_invoke_arn = aws_lambda_function.lambda_openai.invoke_arn
370-
aws_lambda_function_openai_text = aws_lambda_function.lambda_openai.function_name
371+
aws_lambda_function_openai_text_invoke_arn = aws_lambda_function.lambda_langchain.invoke_arn
372+
aws_lambda_function_openai_text = aws_lambda_function.lambda_langchain.function_name
371373
aws_iam_role_arn = aws_iam_role.apigateway.arn
372374
}
373375

api/terraform/lambda_langchain.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ resource "aws_lambda_function" "lambda_langchain" {
7979
memory_size = var.lambda_memory_size
8080
timeout = var.lambda_timeout
8181
handler = "lambda_handler.handler"
82+
architectures = var.compatible_architectures
8283
filename = data.archive_file.lambda_langchain.output_path
8384
source_code_hash = data.archive_file.lambda_langchain.output_base64sha256
8485
layers = [aws_lambda_layer_version.genai.arn]

api/terraform/lambda_layer.tf

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ locals {
1111
layer_slug = "genai"
1212
layer_name = "layer_${local.layer_slug}"
1313
layer_source_directory = "${path.module}/python/${local.layer_name}"
14-
layer_packaging_script = "${local.layer_source_directory}/create_pkg.sh"
14+
layer_packaging_script = "${local.layer_source_directory}/create_container.sh"
1515
layer_package_folder = local.layer_slug
16-
layer_dist_package_name = "${local.layer_name}_dst"
16+
layer_dist_package_name = "${local.layer_name}_dst.zip"
1717
}
1818

1919
###############################################################################
@@ -25,11 +25,14 @@ resource "null_resource" "package_layer_genai" {
2525
redeployment = sha1(jsonencode([
2626
"${path.module}/lambda_layer.tf",
2727
file("${local.layer_packaging_script}"),
28+
file("${local.layer_source_directory}/Dockerfile"),
29+
file("${local.layer_source_directory}/create_container.sh"),
2830
file("${local.layer_source_directory}/requirements.txt"),
2931
file("${local.layer_source_directory}/openai_utils/__init__.py"),
3032
file("${local.layer_source_directory}/openai_utils/const.py"),
3133
file("${local.layer_source_directory}/openai_utils/utils.py"),
3234
file("${local.layer_source_directory}/openai_utils/validators.py"),
35+
fileexists("${local.layer_source_directory}/${local.layer_dist_package_name}") ? filebase64("${local.layer_source_directory}/${local.layer_dist_package_name}") : null
3336
]))
3437
}
3538

@@ -38,28 +41,23 @@ resource "null_resource" "package_layer_genai" {
3841
command = local.layer_packaging_script
3942

4043
environment = {
41-
LAYER_NAME = local.layer_slug
4244
SOURCE_CODE_PATH = local.layer_source_directory
43-
PACKAGE_FOLDER = "python"
4445
RUNTIME = var.lambda_python_runtime
46+
CONTAINER_NAME = local.layer_name
47+
PACKAGE_NAME = local.layer_dist_package_name
4548
}
4649
}
4750
}
4851

49-
data "archive_file" "layer_genai" {
50-
# see https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file
51-
source_dir = "${local.layer_source_directory}/archive/"
52-
output_path = "${local.layer_source_directory}/${local.layer_dist_package_name}.zip"
53-
type = "zip"
54-
depends_on = [null_resource.package_layer_genai]
55-
}
56-
5752
resource "aws_lambda_layer_version" "genai" {
58-
filename = data.archive_file.layer_genai.output_path
59-
source_code_hash = fileexists(data.archive_file.layer_genai.output_path) ? filebase64sha256(data.archive_file.layer_genai.output_path) : null
53+
filename = "${local.layer_source_directory}/${local.layer_dist_package_name}"
54+
source_code_hash = fileexists("${local.layer_source_directory}/${local.layer_dist_package_name}") ? filebase64sha256("${local.layer_source_directory}/${local.layer_dist_package_name}") : null
6055
layer_name = local.layer_slug
61-
compatible_architectures = ["x86_64", "arm64"]
56+
compatible_architectures = var.compatible_architectures
6257
compatible_runtimes = [var.lambda_python_runtime]
58+
lifecycle {
59+
create_before_destroy = true
60+
}
6361
depends_on = [
6462
null_resource.package_layer_genai
6563
]

api/terraform/lambda_openai_v2.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ resource "aws_lambda_function" "lambda_openai_v2_" {
7979
memory_size = var.lambda_memory_size
8080
timeout = var.lambda_timeout
8181
handler = "lambda_handler.handler"
82+
architectures = var.compatible_architectures
8283
filename = data.archive_file.lambda_openai_v2_.output_path
8384
source_code_hash = data.archive_file.lambda_openai_v2_.output_base64sha256
8485
layers = [aws_lambda_layer_version.genai.arn]

0 commit comments

Comments
 (0)