Skip to content

Commit

Permalink
1st Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
offlineprogrammer committed Oct 1, 2024
1 parent 3a89a02 commit 15732ba
Show file tree
Hide file tree
Showing 24 changed files with 24,915 additions and 5 deletions.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# amplify
.amplify
amplify_outputs*
amplifyconfiguration*
42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
## My Project
## Creating a Generative AI Travel Assistant App with Amazon Bedrock and AWS Amplify

TODO: Fill this README out!
This is an example shows how to build a travel assistant app. The app will provide a personalized experience by suggesting popular attractions, local experiences, and hidden gems for the user's desired destination. We will build this app using AWS mplify and Amazon Bedrock.

Be sure to:
![travel-planner-ai](images/amplify_travel_planner.gif)

## Getting Started
### Clone repo

```
git clone https://github.com/aws-samples/travel-personal-assistant.git
cd travel-personal-assistant
```

### Install the packages

```
npm i
```

### Initiate a cloud sandbox environment

```
npx ampx sandbox
```

### Run the App

```
npm run dev
```

* Change the title in this README
* Edit your repository description on GitHub

## Security

Expand Down
11 changes: 11 additions & 0 deletions amplify/auth/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineAuth } from '@aws-amplify/backend';

/**
* Define and configure your auth resource
* @see https://docs.amplify.aws/gen2/build-a-backend/auth
*/
export const auth = defineAuth({
loginWith: {
email: true,
},
});
21 changes: 21 additions & 0 deletions amplify/backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineBackend } from "@aws-amplify/backend";
import { auth } from "./auth/resource";
import { data } from "./data/resource";
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
import { personalAssistantFunction, MODEL_ID } from "./functions/personal-assistant/resource";

export const backend = defineBackend({
auth,
data,
personalAssistantFunction,
});

backend.personalAssistantFunction.resources.lambda.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["bedrock:InvokeModel"],
resources: [
`arn:aws:bedrock:*::foundation-model/${MODEL_ID}`,
],
})
);
22 changes: 22 additions & 0 deletions amplify/data/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";
import { personalAssistantFunction } from "../functions/personal-assistant/resource";

const schema = a.schema({
chat: a
.query()
.arguments({
conversation: a.json().required(),
})
.returns(a.string())
.authorization((allow) => [allow.authenticated()])
.handler(a.handler.function(personalAssistantFunction)),
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: "userPool",
},
});
56 changes: 56 additions & 0 deletions amplify/functions/personal-assistant/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
BedrockRuntimeClient,
ConverseCommandInput,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
import type { Handler } from "aws-lambda";

// Constants
const AWS_REGION = process.env.AWS_REGION;
const MODEL_ID = process.env.MODEL_ID;

// Configuration
const INFERENCE_CONFIG = {
maxTokens: 1000,
temperature: 0.5,
};

// Initialize Bedrock Runtime Client
const client = new BedrockRuntimeClient({ region: AWS_REGION });

export const handler: Handler = async (event) => {
const { conversation } = event.arguments;

const SYSTEM_PROMPT = `
To create a personalized travel planning experience, greet users warmly and inquire about their travel preferences
such as destination, dates, budget, and interests. Based on their input, suggest tailored itineraries that include
popular attractions, local experiences, and hidden gems, along with accommodation options across various price
ranges and styles. Provide transportation recommendations, including flights and car rentals, along with estimated
costs and travel times. Recommend dining experiences that align with dietary needs, and share insights on local
customs, necessary travel documents, and packing essentials. Highlight the importance of travel insurance, offer
real-time updates on weather and events, and allow users to save and modify their itineraries. Additionally,
provide a budget tracking feature and the option to book flights and accommodations directly or through trusted
platforms, all while maintaining a warm and approachable tone to enhance the excitement of trip planning.
`;

const input = {
modelId: MODEL_ID,
system: [{ text: SYSTEM_PROMPT }],
messages: conversation,
inferenceConfig: INFERENCE_CONFIG,
} as ConverseCommandInput;

try {
const command = new ConverseCommand(input);
const response = await client.send(command);

if (!response.output?.message) {
throw new Error("No message in the response output");
}

return JSON.stringify(response.output.message);
} catch (error) {
console.error("Error in chat handler:", error);
throw error; // Re-throw to be handled by AWS Lambda
}
};
12 changes: 12 additions & 0 deletions amplify/functions/personal-assistant/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineFunction } from "@aws-amplify/backend";

export const MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0";

export const personalAssistantFunction = defineFunction({
entry: "./handler.ts",
environment: {
MODEL_ID,
},
timeoutSeconds: 30,
runtime: 20,
});
3 changes: 3 additions & 0 deletions amplify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
17 changes: 17 additions & 0 deletions amplify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"paths": {
"$amplify/*": [
"../.amplify/generated/*"
]
}
}
}
Binary file added images/amplify_travel_planner.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
Loading

0 comments on commit 15732ba

Please sign in to comment.