2 minutes to read - Mar 30, 2023

GPT-3 tutorial: Trip Scheduler

VISIT
GPT-3 tutorial: Trip Scheduler
In this tutoial we will create simple GPT-3 powered Streamlit service for trip scheduling. We will use GPT-3 to generate trip schedule based on user input. We will use Streamlit to create simple UI for our app.
Table of Contents
1🚀 Prerequisites
2📝 Creating Streamlit app
3🚀 Running Streamlit app
4🏆 Result

🚀 Prerequisites

First, we need to install the necessary libraries:

Now we can create .env file and add our OpenAI API key:

Last thing we need to do is to create main.py file, import all necessary libraries and load our API key from .env file:

📝 Creating Streamlit app

Now we can create our Streamlit app. First, we need to create a functions that will generate prompt and trip schedule based on user input:

example_destinations = ['Paris', 'London', 'New York', 'Tokyo', 'Sydney', 'Hong Kong', 'Singapore', 'Warsaw', 'Mexico City', 'Palermo']

random_destination = random.choice(example_destinations)

now_date = datetime.now()

# round to nearest 15 minutes

now_date = now_date.replace(minute=now_date.minute // 15 * 15, second=0, microsecond=0)

# split into date and time objects

now_time = now_date.time()

now_date = now_date.date() + timedelta(days=1)

def generate_prompt(destination, arrival_to, arrival_date, arrival_time, departure_from, departure_date, departure_time, additional_information, **kwargs):

    return f'''

Prepare trip schedule for {destination}, based on the following information:

* Arrival To: {arrival_to}

* Arrival Date: {arrival_date}

* Arrival Time: {arrival_time}

* Departure From: {departure_from}

* Departure Date: {departure_date}

* Departure Time: {departure_time}

* Additional Notes: {additional_information}

'''.strip()

def submit():    

    prompt = generate_prompt(**st.session_state)

    # generate output

    output = openai.Completion.create(

        engine='text-davinci-003',

        prompt=prompt,

        temperature=0.45,

        top_p=1,

        frequency_penalty=2,

        presence_penalty=0,

        max_tokens=1024

    )

    st.session_state['output'] = output['choices'][0]['text']

Now we can create UI for our app with Streamlit:

Let's create form for user input:

And finally, we can display generated trip schedule:

🚀 Running Streamlit app

Now we can run our app:

🏆 Result

And let's check results!

Article source
loading...