Skip to content

Commit

Permalink
feat: go deps, README, provided.al2023 runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmcguire1 committed Jan 14, 2024
1 parent d2963e4 commit 67977e3
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 61 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ on:

env:
ARCH: GOARCH=arm64

LAMBDA_RUNTIME: provided.al2023
LAMBDA_HANDLER: bootstrap
LAMBDA_ARCH: arm64
jobs:
build-deploy:
runs-on: ubuntu-latest
Expand All @@ -28,5 +30,5 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
- run: sam build --parameter-overrides Runtime=provided.al2 Handler=bootstrap Architecture=arm64 ApiKey=${{ secrets.OPENAI_API_KEY }}
- run: sam deploy --stack-name chat-gpt --s3-bucket ${{ secrets.S3_BUCKET }} --parameter-overrides Runtime=provided.al2 Handler=bootstrap Architecture=arm64 GptApiKey=${{ secrets.OPENAI_API_KEY }} GeminiApiKey=${{ secrets.GEMINI_API_KEY }} --capabilities CAPABILITY_IAM
- run: sam build --parameter-overrides Runtime=${{ env.LAMBDA_RUNTIME }} Handler=${{ env.LAMBDA_HANDLER }} Architecture=${{ env.LAMBDA_ARCH }}
- run: sam deploy --stack-name chat-gpt --s3-bucket ${{ secrets.S3_BUCKET }} --parameter-overrides Runtime=${{ env.LAMBDA_RUNTIME }} Handler=${{ env.LAMBDA_HANDLER }} Architecture=${{ env.LAMBDA_ARCH }} OpenAIApiKey=${{ secrets.OPENAI_API_KEY }} GeminiApiKey=${{ secrets.GEMINI_API_KEY }} --capabilities CAPABILITY_IAM
73 changes: 44 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Alexa-ChatGPT

> This repository contains the Alexa skill serverless backend to prompt generative ai LLM models
[git]: https://git-scm.com/
[git]: https://git-scm.com/
[golang]: https://golang.org/
[modules]: https://github.com/golang/go/wiki/Modules
[golint]: https://github.com/golangci/golangci-lint
[aws-cli]: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html
[aws-cli-config]: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html
[aws-sam-cli]: https://github.com/awslabs/aws-sam-cli


[![Go Report Card](https://goreportcard.com/badge/github.com/jackmcguire1/alexa-chatgpt)](https://goreportcard.com/report/github.com/jackmcguire1/alexa-chatgpt)
[![codecov](https://codecov.io/gh/jackmcguire1/alexa-chatgpt/branch/main/graph/badge.svg)](https://codecov.io/gh/jackmcguire1/alexa-chatgpt)

# Logic

- A user prompts the Alexa skill.
- The Alexa skill will invoke the assigned Lambda with an 'AutoComplete' Intent.
- The Lambda will push the user prompt to a SQS.
Expand All @@ -23,8 +24,7 @@
> [!CAUTION]
> Due to the Alexa skill idle lambda response constraint of ~8 seconds, the following logic has been applied.
- If the Alexa skill does not poll a SQS message within ~7 seconds, users will be given a response of 'your response will be available shortly', this is too avoid the Alexa skill session expiring.

- If the Alexa skill does not poll a message from the queue within ~7 seconds, users will be given a direct response of 'your response will be available shortly!', this is too avoid the Alexa skill session from expiring.

- Querying the Alexa skill with 'Last Response', the lambda will immediately poll the response SQS to retrieve the delayed response and output the prompt with the timestamp of response time

Expand All @@ -34,39 +34,42 @@
> Users are able to change which chat model is in use
### OpenAI ChatGPT

- user's can select this by prompting 'use gpt'

### Google's GenerativeAI Gemini

- user's can select this by prompting 'use gemini'

## Alexa Intents
> The Alexa Intents or phrases to interact with the Alexa Skill

> The Alexa Intents or phrases to interact with the Alexa Skill
- AutoComplete
> the intent used to prompt the LLM models

> the intent used to prompt the LLM models
- Model
> Allows users to select LLM model to use

> Allows users to select LLM model to use
- Last Response
> Fetch delayed LLM response to user's prompt

> Fetch delayed LLM response to user's prompt
- Cancel
> Force Alexa to await for next intent

> Force Alexa to await for next intent
- Stop
> Terminate Alexa skill session

> Terminate Alexa skill session
- Help
> List all avalible interactions or intents
> List all avalible interactions or intents
# Infrastructure

<img src="./images/infra.png">

# Examples
Expand All @@ -76,9 +79,11 @@
</p>

## SETUP

> How to configure your Alexa Skill
### Environment

> we use handler env var to name the go binary either 'main' or 'bootstrap' for AL2.Provided purposes, devs should use 'main'
```shell
Expand All @@ -96,72 +101,82 @@
- [AWS SAM CLI][aws-sam-cli]

### [AWS CLI Configuration][aws-cli-config]

> Make sure you configure the AWS CLI
- AWS Access Key ID
- AWS Secret Access Key
- Default region 'us-east-1'

```shell
aws configure
```

### Requirements

- <b>OPENAI API KEY</b>
* please set environment variables for your OPENAI API key
> export API_KEY=123456

- please set environment variables for your OPENAI API key
> export API_KEY=123456
- <b>Create a S3 Bucket on your AWS Account</b>
* Set envrionment variable of the S3 Bucket name you have created [this is where AWS SAM]
- Set envrionment variable of the S3 Bucket name you have created [this is where AWS SAM]
> export S3_BUCKET_NAME=bucket_name

### Deployment Steps
1. Create a new Alexa skill with a name of your choice

1. Create a new Alexa skill with a name of your choice

2. Set the Alexa skill invocation with a phrase i.e. 'My question'


3. Set built-in invent invocations to their relevant phrases i.e. 'help', 'stop', 'cancel', etc.


4. Create a new Intent named 'AutoCompleteIntent'


5. Add a new Alexa slot to this Intent and name it 'prompt' with type AMAZON.SearchQuery'


6. Add invocation phrase for the 'AutoCompleteIntent' with value 'question {prompt}'


7. Deploy the stack to your AWS account.
> sam build && sam deploy --stack-name chat-gpt --s3-bucket $S3_BUCKET_NAME --parameter-overrides 'ApiKey=$API_KEY' --capabilities CAPABILITY_IAM

```
export ARCH=GOARCH=arm64
export LAMBDA_RUNTIME=provided.al2023
export LAMBDA_HANDLER=bootstrap
export LAMBDA_ARCH=arm64
```

```
sam build --parameter-overrides Runtime=$LAMBDA_RUNTIME Handler=$LAMBDA_HANDLER Architecture=$LAMBDA_ARCH
```

9. Once the stack has deployed, make note of lambda ARN from the 'ChatGPTLambdaArn' field, from the the output of
> sam list stack-outputs --stack-name chat-gpt
```
sam deploy --stack-name chat-gpt --s3-bucket $S3_BUCKET --parameter-overrides Runtime=$LAMBDA_RUNTIME Handler=$LAMBDA_HANDLER Architecture=$LAMBDA_ARCH OpenAIApiKey=$OPENAI_API_KEY GeminiApiKey=$GEMINI_API_KEY --capabilities CAPABILITY_IAM
```

10. Apply this lambda ARN to your 'Default Endpoint' configuration within your Alexa skill, i.e. 'arn:aws:lambda:us-east-1:123456789:function:chatGPT'
9. Once the stack has deployed, make note of lambda ARN from the 'ChatGPTLambdaArn' field, from the the output of

> sam list stack-outputs --stack-name chat-gpt
11. Begin testing your Alexa skill by querying for 'My question' or your chosen invocation phrase, Alexa should respond with "Hi, let's begin our conversation!"
10. Apply this lambda ARN to your 'Default Endpoint' configuration within your Alexa skill, i.e. 'arn:aws:lambda:us-east-1:123456789:function:chatGPT'

11. Begin testing your Alexa skill by querying for 'My question' or your chosen invocation phrase, Alexa should respond with "Hi, let's begin our conversation!"

12. Query Alexa 'question {your sentence here}'
> Note the OpenAI API may take longer than 8 seconds to respond, in this scenario Alexa will tell you your answer will be ready momentarily, simply then ask Alexa 'last response'

> Note the OpenAI API may take longer than 8 seconds to respond, in this scenario Alexa will tell you your answer will be ready momentarily, simply then ask Alexa 'last response'
13. Tell Alexa to 'stop'


14. <b>Testing complete!</b>

## Contributors

This project exists thanks to **all** the people who contribute.

## Donations

All donations are appreciated!

[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](http://paypal.me/crazyjack12)
54 changes: 27 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,37 @@ module github.com/jackmcguire1/alexa-chatgpt
go 1.21

require (
github.com/aws/aws-lambda-go v1.42.0
github.com/aws/aws-sdk-go-v2/config v1.26.1
github.com/aws/aws-sdk-go-v2/service/sqs v1.29.5
github.com/google/generative-ai-go v0.3.0
github.com/sashabaranov/go-openai v1.17.9
github.com/aws/aws-lambda-go v1.44.0
github.com/aws/aws-sdk-go-v2/config v1.26.3
github.com/aws/aws-sdk-go-v2/service/sqs v1.29.7
github.com/google/generative-ai-go v0.5.0
github.com/sashabaranov/go-openai v1.17.11
github.com/stretchr/testify v1.8.4
google.golang.org/api v0.154.0
golang.org/x/oauth2 v0.16.0
google.golang.org/api v0.156.0
)

require (
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/ai v0.3.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
github.com/aws/aws-sdk-go-v2 v1.24.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.12 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9 // indirect
github.com/aws/aws-sdk-go-v2 v1.24.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.14 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 // indirect
github.com/aws/smithy-go v1.19.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -47,18 +48,17 @@ require (
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.60.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 67977e3

Please sign in to comment.