...

/

Hands-On Application: Movie Recommendation Service

Hands-On Application: Movie Recommendation Service

Walk through a movie recommendation service that uses Pinecone for vector search.

We will walk through how to use Pinecone to implement a service that provides movie recommendations based on user-provided search criteria.

This will be split into three important steps, which will be executed in order:

  1. Create a serverless index.

  2. Load the data into the index.

  3. Use the movie recommendation service.

Press + to interact
Key steps in the application
Key steps in the application

Below is the high-level architecture of the solution. In response to a user query for movie recommendations, the application executes a similarity search in the Pinecone database. The movie data is converted into vector embeddings and loaded into the database as a separate process.

Press + to interact
High-level architecture
High-level architecture

Set up a free Pinecone account

  1. Sign up for a free Pinecone account.

  2. You need an API key to make API calls to the Pinecone project. To get the key, open the Pinecone console, select the project, go to "API Keys", and copy the API key.

  3. Note down the API key because it will be used in subsequent steps.

Create a serverless index

The serverless index will be used to execute vector searches across movie data.

Sample code

Go through the example below.

  • Provide the value for PINECONE_AI_API_KEY API key in the widget.

  • Click the "Run" button to execute the code.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"
	"net/http"
)

const indexName = "movies"
const createIndexURL = "https://api.pinecone.io/indexes"

func main() {

	pineconeAPIKey := os.Getenv("PINECONE_API_KEY")
		
	r := CreateIndexRequest{
		Name:      indexName,
		Metric:    "cosine", //do not change
		Dimension: 768,
		Spec: Spec{
			Serverless: Serverless{
				Cloud:  "aws",       //do not change
				Region: "us-east-1", //do not change
			},
		},
	}

	b, _ := json.Marshal(r)

	req, _ := http.NewRequest("POST", createIndexURL, bytes.NewReader(b))

	req.Header.Add("accept", "text/plain")
	req.Header.Add("content-type", "application/json")
	req.Header.Add("Api-Key", pineconeAPIKey)

	res, err := http.DefaultClient.Do(req)

	if err != nil {
		log.Fatal(err)
	}

	defer res.Body.Close()
	body, err := io.ReadAll(res.Body)

	if err != nil {
		log.Fatal(err)
	}

	if res.Status == "201 Created" {
		fmt.Println("index created -", indexName)
	} else {
		fmt.Println("response status", res.Status)
		fmt.Println("index creation failed due to -", string(body))
	}
}

type CreateIndexRequest struct {
	Name      string `json:"name"`
	Dimension int    `json:"dimension"`
	Metric    string `json:"metric"`
	Spec      Spec   `json:"spec"`
}

type Spec struct {
	Serverless Serverless `json:"serverless"`
}

type Serverless struct {
	Cloud  string `json:"cloud"`
	Region string `json:"region"`
}
Create search index

Output ...