Laravel Semantic Search with OpenAI and Pinecone

Summary

Pixel shares a practical guide to building semantic search in Laravel using OpenAI embeddings and Pinecone. It covers 1536-dimensional vectors, cosine similarity, index setup, environment configuration, and using Guzzle to connect the services—helping developers create search that understands meaning beyond exact keywords.

DEV Community

Using Embeddings and Pinecone for Semantic Search in Laravel: A Production-Ready Guide

If you've ever built a search feature with LIKE '%query%' and watched it completely miss what the user obviously meant, you already understand the problem. Keyword search is brittle. It fails on synonyms, typos, and anything requiring conceptual understanding. Semantic search — powered by embeddings — solves this at the model level, not the application level. And it's more accessible to integrate into Laravel than most developers expect. This guide walks through building a real semantic search pipeline: generating embeddings with OpenAI, storing and querying them in Pinecone, and wiring it all up in a Laravel application. No hand-waving — actual code you can adapt. What Embeddings Actually Are (The 30-Second Version) An embedding is a high-dimensional vector — an array of floats — that represents the meaning of a piece of text. OpenAI's text-embedding-3-small model outputs 1536-dimensional vectors. Texts with similar meaning produce vectors that are geometrically close to each other. That's the whole trick. When a user searches for "affordable places to eat near the marina", the embedding of that query will be close to the embedding of a restaurant listing that says "budget-friendly dining, waterfront location" — even though no keywords match. Setting Up Pinecone Pinecone is a managed vector database. You create an index, specify the dimension (must match your embedding model's output), and choose a similarity metric. For OpenAI embeddings, use cosine. Create a free Pinecone account, make an index called laravel-search with dimension 1536 and metric cosine, then grab your API key. Add these to your .env: OPENAI_API_KEY=sk-...
PINECONE_API_KEY=pcsk_...
PINECONE_HOST=https://laravel-search-xxxx.svc.aped-xxxx.pinecone.io
Pinecone's REST API is straightforward, so we'll use Guzzle directly rather than adding another package dependency. The Embedding Service Create a dedicated service class to generate embeddings: <?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class EmbeddingService
{
public function generate(string $text): array
{
$response = Http::withToken(config('services.openai.key'))
- >post('https://api.openai.com/v1/embeddings', [
'model' => 'text-embedding-3-small',
'input' => $this->sanitize($text),
]);

    if ($response->failed()) {
        throw new \RuntimeException('OpenAI embedding request failed: ' . $response->body());
    }

    return $response->json('data.0.embedding');
}

private function sanitize(string $text): string
{
    // Strip excessive whitespace, truncate to stay within token limits
    return mb_substr(trim(preg_replace('/\s+/', ' ', $text)), 0, 8000);
}

}
Add to config/services.php: 'openai' => [
'key' => env('OPENAI_API_KEY'),
],
'pinecone' => [
'key' => env('PINECONE_API_KEY'),
'host' => env('PINECONE_HOST'),
],
The Pinecone Service <?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class PineconeService
{
private string $baseUrl;

public function __construct()
{
    $this->baseUrl = config('services.pinecone.host');
}

private function client()
{
    return Http::withHeaders([
        'Api-Key' => config('services.pinecone.key'),
        'Content-Type' => 'application/json',
    ])->baseUrl($this->baseUrl);
}

public function upsert(string $id, array $vector, array $metadata = []): void
{
    $this->client()->post('/vectors/upsert', [
        'vectors' => [
            [
                'id' => $id,
                'values' => $vector,
                'metadata' => $metadata,
            ],
        ],
    ])->throw();
}

public function query(array $vector, int $topK = 10, array $filter = []): array
{
    $payload = [
        'vector' => $vector,
        'topK' => $topK,
        'includeMetadata' => true,
...

Laravel Semantic Search with OpenAI and Pinecone
Powered by Readybot.io

The latest from Pixel

Stop Paying for AI Renders in Test Orders

Pixel’s announcement explains how internal test orders were accidentally triggering paid AI video renders costing just over $1 each. It details a server-side dry-run guard …

Sandblasting Grit Guide: 120 vs 220 vs 320

Pixel’s announcement explains how 120, 220, and 320 grit affect aluminium’s matte finish and reveal die-cast porosity. It also covers how pressure, blast distance, and …

NVIDIA Cosmos 3 Edge: Physical AI on Jetson

Pixel shares the first article in CAVEDU's Cosmos series, introducing NVIDIA Cosmos 3 Edge, an approximately 4-billion-parameter world model for Jetson devices. It explores multimodal …