https://github.com/mongodb-developer/laravel-openai-vector-search
Laravel Tour Planner with Open AI and MongoDB Vector Search
https://github.com/mongodb-developer/laravel-openai-vector-search
database generative-ai laravel mongodb openai
Last synced: 4 months ago
JSON representation
Laravel Tour Planner with Open AI and MongoDB Vector Search
- Host: GitHub
- URL: https://github.com/mongodb-developer/laravel-openai-vector-search
- Owner: mongodb-developer
- Created: 2024-07-31T09:48:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-24T15:40:46.000Z (about 1 year ago)
- Last Synced: 2025-08-01T16:42:25.943Z (5 months ago)
- Topics: database, generative-ai, laravel, mongodb, openai
- Language: PHP
- Homepage: https://www.mongodb.com/developer/code-examples/php/laravel-openai-mongodb-vectorsearch
- Size: 744 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## About Laravel
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
## Learning Laravel
Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
To leverage MongoDB with Laravel see the following [resources](https://www.mongodb.com/resources/products/compatibilities/mongodb-laravel-integration)
## Tour Planner with MongoDB Vector Search
This project demonstrates the use of MongoDB Vector Search in a Laravel backend with a Vue.js frontend to create an intelligent tour planning application. It showcases how to leverage vector search capabilities for enhanced search and recommendation features in travel applications. See the article [Introducing the Tour Planner With MongoDB Vector Search](https://www.mongodb.com/developer/code-examples/php/laravel-openai-mongodb-vectorsearch/)
### Project Structure
- `/tour-planner`: Laravel backend
- `/tour-planner/frontend/tour-planner-frontend`: Vue.js frontend
### Features
- City search with vector-based similarity
- Points of interest recommendation
- Intelligent trip planning using OpenAI integration
## Vector Search and Laravel sdk
The points of interest controller search cities by embeddings generated on city - attraction - desc concat, and gets the searched term as $search
```php
$embedding = $this->generateEmbedding($search);
// Use Atlas Search to search for points of interest within a city
$points = DB::collection('points_of_interest')
->raw(
function ( $collection) use ($embedding) {
return $collection->aggregate([
[
'$vectorSearch' => [
'index' => 'vector_index',
'path' => 'embedding',
'queryVector' => $embedding,
'numCandidates' => 20,
'limit' => 5
],
]
,
['$project' => [
'name' => 1,
'description' => 1,
'rating' => 1,
'location' => 1,
'score' => ['$meta' => 'vectorSearchScore']
]]
]
);
},
)->toArray();
```
## Retrieval Augmented Generation and Laravel with OpenAI
Using the relevant context from MongoDB to augment the trip planning JSON response
```php
protected function generatePossibleTrip($cities,$context, $days){
$result = OpenAI::chat()->create([
'model' => 'gpt-4o-mini',
'temperature' => 0,
'response_format' => ['type' => 'json_object'],
'messages' => [
[
'role' => 'system',
'content' => 'You are a travel agent helping a customer plan a trip to a city. If it will be hard to visit that in the number of days Have a one day plan stating the problem. The customer will provide you with points of interest to visit in json.'
],
[
'role' => 'system',
'content' => 'take this schema, for flights add orig_airport_code and dest_airport_code:
"tripPlan": {
"destination": [{
"city": "string",
"country": "string"
}],
"pointsOfInterest": [
{
"name": "string",
"description": "string",
"location": {
"coordinates": [number, number]
},
"rating": number
}
],
// only in relevant direction
"flights" : [ "src_airport_code": "string",
"dest_airport_code": "string"
],
"itinerary": [
{
"day": number,
"destination": "string",
"activities": [
{
"time": "string",
"activity": "string",
"duration": "string",
// if flight
"src_airport_code": "string",
dest_airport_code: "string"
}
...
]
}
...
]
}
'
],
[
'role' => 'user',
'content' => 'For cities: ' . json_encode($cities) . '| Take this POIs: ' . $context . ' and build a plan for the a trip of ' . $days . 'days. '
]
]
]);
return $result->choices[0]->message->content;
}
```
## Prerequisites
- PHP 8.1+
- MongoDB Extension and prereqisites.
- Composer
- Node.js and npm
- MongoDB Atlas cluster
- OpenAI API key
## Backend Setup (Laravel)
Navigate to the cloned directory:
```
cd laravel-openai-vector-search
```
Install PHP dependencies:
```
composer install
```
Copy the `.env.example` file to `.env` and configure your environment variables:
```
OPENAI_API_KEY=your_openai_api_key
DB_URI=your_atlas_uri
```
Generate an application key:
```
php artisan key:generate
```
Run database migrations and seeders (if any):
```
php artisan db:seed
```
Create Atlas vector search index on database: `trip_planner` collection: `points_of_interest`:
** Index name : vector_index **
```
{
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 1536,
"similarity": "cosine"
}
]
}
```
Start the Laravel development server:
```
php artisan serve
```
## Frontend Setup (Vue.js)
Navigate to the frontend directory:
```
cd frontend/trip-planner-frontend
npm install
npm run serve
```
The application should be visible at http://localhost:8080 .
## Disclaimer
**Not a MongoDB official product. Use at your own risk**
