Giter Club home page Giter Club logo

supershaneski / mytrip-hokkaido-app Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 5.0 4.58 MB

This sample project is a customizable regional travel planning app that uses artificial intelligence to generate itineraries based on user text, powered by OpenAI Chat Completions API, built using Next.js 13.

License: MIT License

JavaScript 89.12% CSS 10.88%
hokkaido itinerary openai openai-api trip-planner chat-completion-api function-calling itinerary-generator travel travel-app

mytrip-hokkaido-app's Introduction

mytrip-hokkaido-app

v0.0.2

This sample project is a regional travel planning app that uses artificial intelligence to generate itineraries based on user text, powered by OpenAI Chat Completions API, built using Next.js 13.


このサンプルプロジェクトは、ユーザーテキストに基づいて旅行の計画を立てるために人工知能を使用する地域旅行計画アプリです。OpenAI Chat Completions APIによりパワードされ、Next.js 13を使用して構築されています。

Updated:

Application

Itinerary screen

Simply input your desired activities and specify a location within the current region. All generated itineraries are saved in localStorage and will be suggested during keyword searches.

Home screen

Customize Location

You can now change the default location by editing the APP_LOCATION environment variable in the .env file.

APP_LOCATION=Hokkaido, Japan

I recommend using a broader region rather than a specific city for more itinerary options. For instance, consider using Maharashtra, India, instead of Mumbai, India.

Sample Location - Murmansk, Russia

Here’s a demonstration of what happens when we set the APP_LOCATION to Murmansk, Russia.

Murmansk, Russia

Itinerary screen

Kola Bay

Language Support

After enabling location customization, it becomes necessary to let the language of user input to dictate the output.

Here are several examples of the screens in different languages:

  • Japanese: 札幌、一泊、ジンギスカン、カラオケ (Sapporo, Overnight, Jingisukan, Karaoke)

Japanese - Main Japanese - Itinerary

  • Korean: 삿포로 쇼핑여행 (Shopping trip in Sapporo)

Korean - Main Korean - Itinerary

  • Russian: Поездка на горячие источники в Отару (Hot spring trip in Otaru)

Russian - Main Russian - Itinerary

Chat Completions API

I have refactored and optimized the code from the previous version of this app, particularly focusing on improving the efficiency of API calls.

First, I implemented function calling to verify whether the given location falls within the specified region.

{
    "name": "get_place",
    "description": "Extract name of place and theme or activity for the trip in the given texts and check if it is located in Hokkaido, Japan.",
    "parameters": {
        "type": "object",
        "properties": {
            "place": {
                "type": "string",
                "description": "The name of the actual geographical location for the trip. This should be a recognized place name such as a town, city, region, country, or state. Abstract or generic locations like 'countryside' are not accepted."
            },
            "itinerary": {
                "type": "string",
                "description": "The theme or activity that the user is interested"
            },
            "language": {
                "type": "string",
                "description": "The primary language used in the user textin ISO 639-1 code"
            },
            "isLocationValid": {
                "type": "boolean",
                "description": "Flag that tells whether the place is located in Hokkaido or not."
            }
        },
        "required": [ "place", "itinerary", "language", "isLocationValid" ]
    }
}

This function serves as the primary workhorse for the application. It's worth noting the various parameters it accepts, aside from place and language. These additional parameters play a crucial role in generating the output. For instance, the language parameter determines the language of the output.

If isLocationValid is TRUE, I proceed to submit either the exact user query or utilize the itinerary and place from the result of the function call in the next API call. Unlike the initial function call, this next API call does not involve any additional function calls. Instead, we have prepared a JSON output and enabled response_format.

const response = await openai.chat({
    response_format: { type: "json_object" },
    messages
})

When configuring response_format using JSON, it is important to explicitly instruct the AI to output JSON in the system prompt. Failure to do so may result in unexpected outcomes.

const system_prompt = `You are a helpful travel planner specializing in ${APP_PLACE_NAME} and designed to output json.\n` +
    `Your output is to an API.\n` +
    `Create valid json complying to the schema.\n\n` +
    `# language\n` +
    `Please write reply in ${language_text} language.\n\n` +
    `Today is ${new Date()}.\n\n` +
    `# json output schema\n` +
    JSON.stringify(output_schema, null, 2)

We also included the JSON schema for the output to provide clarity and structure.

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "title": {
            "type": "string",
            "description": "main title for the whole itinerary"
        },
        "welcome": {
            "type": "object",
            "description": "welcome panel",
            "properties": {
                "title": {
                    "type": "string",
                    "description": "title for introduction message"
                },
                "text": {
                    "type": "string",
                    "description": "introduction message about the itinerary"
                },
                "image": {
                    "type": "string",
                    "description": "image keyword for welcome panel"
                }
            },
            "required": ["title", "text", "image"]
        },
        "itineraries": {
            "type": "array",
            "description": "itinerary item",
            "items": {
                "type": "object",
                "properties": {
                    "title": {
                        "type": "string",
                        "description": "title for itinerary"
                    },
                    "text": {
                        "type": "string",
                        "description": "description of itinerary"
                    },
                    "image": {
                        "type": "string",
                        "description": "image keyword for itinerary panel"
                    },
                    "places": {
                        "type": "array",
                        "description": "notable places to included in this itinerary",
                        "items": {
                            "type": "string"
                        }
                    }
                },
                "required": ["title", "text", "image", "places"]
            }
        },
        "closing": {
            "type": "object",
            "description": "closing panel",
            "properties": {
                "title": {
                    "type": "string",
                    "description": "title for closing message"
                },
                "text": {
                    "type": "string",
                    "description": "closing message about the itinerary"
                },
                "image": {
                    "type": "string",
                    "description": "image keyword for closing panel"
                },
                "places": {
                    "type": "array",
                    "description": "notable places to from all itineraries",
                    "items": {
                        "type": "string"
                    }
                }
            },
            "required": ["title", "text", "image", "places"]
        }
    },
    "required": [
        "title",
        "welcome",
        "itineraries",
        "closing"
    ]
}

Images

To generate the images for the itinerary, I will be using Pexels API. You can use Pexels Node.js library to access the API.

npm install pexels

I will be using Pexels API photo search function

import { createClient } from "pexels"

const client = createClient(process.env.PEXELS_API_KEY)

client.photos.search({ query, per_page: 3 }).then((photos) => {
    console.log(photos)
})

Please be aware of the rate-limit of Pexels API.

Setup

Clone the repository and install the dependencies

git clone https://github.com/supershaneski/mytrip-hokkaido-app.git myproject

cd myproject

npm install

Copy .env.example and rename it to .env then edit the OPENAI_API_KEY and PEXELS_API_KEY use your own API keys. If you want to change the region, please also edit APP_LOCATION.

OPENAI_API_KEY=YOUR-OPENAI-API-KEY
PEXELS_API_KEY=YOUR-PEXELS-API-KEY
APP_LOCATION=Hokkaido, Japan

Then run the app

npm run dev

Open your browser to http://localhost:4000/ to load the application page.

mytrip-hokkaido-app's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.