Implementing an AI Travel Agent using OpenAI's text API and LangChain framework to track information about future events with the help of duckduckgo search engine, wikipedia and other websites.
-
The API Works receiving a
question
query/prompt with instructions to an AI travel agent that returns an answer with a complete travel itinerary based on the informations provided by the user:- User could input the trip destination and dates and ask for a complete travel itinerary, events that will happen at the trip's time and flight tickets price, for example.
- The AI agent then look up for the answer with the help of duckduckgo search engine, wikipedia, and other trip-related websites to ensure the most complete anwser possible.
1. Create a `requirements.txt` file:
- 1.1. Make sure to create a
requirements.txt
file containing all the python packages you used in the project.
2. Create a image of your project with Docker:
- 2.1. Install Docker Desktop;
- 2.2. Create the Dockerfile;
- 2.3. Build the image:
docker build --platform linux/x86_64 -t agent .
3. Setting up AWS ECR Repository:
- 3.1. Login at AWS Website;
- 3.2. Create a Private Repository at Amazon Elastic Container Registry (ECR) with the name
travelagent
, for example.
4. Setting up AWS CLI:
- 4.1. Install AWS Command Line Interface (CLI), copying the commands to your terminal, according to your Operational System;
- 4.2. Verify installation with the following commands:
which aws aws --version
- 4.3. AWS Configure: requires AWS Access Key ID and AWS Secret Access Key.
- To find those keys, go to AWS Identity and Access Management (IAM);
- Then, find Users > your_user > Security credentials;
- Create an Access Key, then copy paste your keys in the terminal, after typing the following command:
aws configure
- Lastly, confirm your region at AWS Home Console, next to your username, at the top right corner.
5. Push your API Image to your AWS ECR Repository via AWS CLI:
- 5.1. Head to the ECR Repository > View Push Commands.
- Copy and paste the push commands into your terminal.
- At this point, make sure to build, tag and push your image everytime there's a change to the code. Also, don't forget to deploy the new image to the Lambda function aswell.
6. Create Lambda Function:
- 6.1. Inside AWS Panel, search for Lambda;
- 6.2. Create a new function with a Container image:
- Browse for the ECR Repository Image;
- Select Architecture x86_64.
- 6.3. Edit function's configuration:
- Increase Memory to 512 MB;
- Increase timeout (e.g. 10 minutes).
- 6.4. Insert Environment Variable:
- Inside the Lambda Function, go to Configuration > Environment variables;
- fill the 'Key' field with
OPENAI_API_KEY
and the 'Value' field with the actual API Key.
7. Create Lambda Test:
- 7.1. Inside the Lambda function, go to Test:
- Insert an Event name;
- In this example, create a 'question' tag in the JSON Event with an example of an actual user prompt;
- Save and Run the Test;
- Check for execution status and details and see if corresponds to what's expected.
8. Exposing the application to the internet via AWS EC2 Load Balancers:
- 8.1. Inside AWS Panel, search for Elastic Compute Cloud (EC2);
- 8.2. At the left side Panel, look for Load Balancing > Load Balancers > Create Load Balancer > Application Load Balancer:
- Scheme: Internet-facing;
- Load balancer IP address type: IPv4;
- Select the existing VPC and all Subnets;
- Security groups: Default;
- Protocol: HTTP, Port: 80, Create target group.
- 8.3. Creating a target group:
- Target type: Lambda Function;
- Provide a Target group name (e.g. target-travelagent);
- Select the recently created Lambda Function, with Version set to
$LATEST
- 8.4. Create Load Balancer (after adding the target group);
- 8.5. Edit Security configuration:
- Head to Security > Open security group ID > Edit inbound rules:
- Type: HTTP;
- Port: 80;
- Source: Custom ->
0.0.0.0/0
.
- Head to Security > Open security group ID > Edit inbound rules:
- 8.6. Make sure to copy the DNS Name from the Load Balancer to call the API.
curl -X POST "http://api-travelagent-232686593.us-east-2.elb.amazonaws.com" \
-H "Content-Type: application/json" \
-d '{
"question": "Vou viajar para Londres em agosto de 2024. Quero que faça um roteiro de viagem para mim com eventos que irão ocorrer na data da viagem e com o preço da passagem de São Paulo para Londres ida e volta em reais."
}'
The output should be printed in your terminal.
-
After cloning this repository to your directory, install
cors-anywhere
as following:1.1. Clone cors-anywhere repository inside this repository's directory:
git clone https://github.com/Rob--W/cors-anywhere.git
1.2. Navigate to the directory and install dependencies with
npm
:cd cors-anywhere npm install
1.3. Start the server at Port 8080:
node server.js -p 8080
-
Now just open
index.html
file through Live Server VSCode Extension, or by just opening the file on your browser, and have fun 😄.
This project is part of the AI track at the 16th edition of the Next Level Week (NLW Journey) held by Rocketseat from 8-11 of July/2024.