How to Build a ChatGPT Embedding‑Powered Similarity Search with Redis Vector DB in PHP
Learn how to generate text embeddings with OpenAI's ChatGPT API, store them in Redis' vector database, and perform efficient similarity searches using PHP, covering installation, API calls, index creation, vector storage, KNN querying, and practical code examples.
What Are ChatGPT Embeddings?
Embeddings convert text into high‑dimensional numeric vectors, preserving semantic, syntactic, and emotional information so that similar meanings are close in vector space. ChatGPT embeddings are vectors produced by OpenAI’s language models (e.g., text-embedding-ada-002) and can be used for similarity comparison, as features for machine‑learning models, or other NLP tasks.
Calling the OpenAI Embeddings API
A typical request uses a curl command:
curl https://api.openai.com/v1/embeddings \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"input": "The food was delicious and the waiter...",
"model": "text-embedding-ada-002",
"encoding_format": "float"
}'The response contains a JSON object with a data array; each entry holds an embedding field that is a list of 1536 floating‑point numbers.
Redis Vector Database Overview
Redis Vector DB is an extension of the in‑memory Redis engine designed for storing and retrieving high‑dimensional vectors. It offers high performance, horizontal scalability, and a simple API for vector operations. Typical use cases include text similarity analysis, image similarity search, recommendation systems, and fraud detection.
PHP Implementation Steps
1. Install Required Packages
Ensure PHP ≥ 8.1 is installed, then add the OpenAI client via Composer:
composer require openai-php/client2. Create an OpenAI Client and Generate an Embedding
$apiKey = 'xxxxxxxxxxxxxxxxxx';
$client = \OpenAI::factory()
->withApiKey($apiKey)
->withBaseUri('api.openai.com/v1')
->withHttpClient(new \GuzzleHttp\Client([]))
->withStreamHandler(fn($request) => $client->send($request, ['stream' => true]))
->make();
$input = 'Hi,我是Tinywan,开源技术小栈公众号作者。';
$response = $client->embeddings()->create([
'model' => 'text-embedding-ada-002',
'input' => $input,
'encoding_format' => 'float'
]);
$textEmbeddingVector = $response['data'][0]['embedding'];3. Create a Redis Index (if it does not exist)
$indexName = 'tinywan:embedding:2024';
try {
$indexExist = Redis::rawCommand('FT.INFO', $indexName);
} catch (\Throwable $e) {
$indexExist = false;
}
if (!$indexExist) {
Redis::rawCommand('FT.CREATE', $indexName, 'on', 'JSON', 'PREFIX', '1', "$indexName:",
'SCHEMA',
'$.text_embedding', 'AS', 'text_embedding', 'VECTOR', 'FLAT', '6',
'DIM', '1536', 'DISTANCE_METRIC', 'COSINE', 'TYPE', 'FLOAT32');
}4. Store the Embedding in RedisJSON
$embeddingKey = 'tinywan:embedding:2024:' . time();
$embeddingValue = [
'key' => $embeddingKey,
'content' => $input,
'text_embedding' => $textEmbeddingVector
];
Redis::rawCommand('JSON.SET', $embeddingKey, '$', json_encode($embeddingValue, JSON_UNESCAPED_UNICODE));5. Perform a KNN Search
$count = 10;
$blob = '';
foreach ($textEmbeddingVector as $value) {
$blob .= pack('f', $value);
}
$redisResult = Redis::rawCommand('FT.SEARCH', $indexName,
'*=>[KNN ' . $count . ' @text_embedding $blob]',
'PARAMS', '2', 'blob', $blob,
'SORTBY', '__text_embedding_score',
'DIALECT', '2');
if (!isset($redisResult[2][3])) {
return json(['content' => []]);
}
$resultArr = json_decode($redisResult[2][3], true);
return json(['content' => $resultArr['content']]);Result
After inserting several documents, a query such as searching for the term 程序猿 returns the most relevant entry at the top of the result list, demonstrating successful vector‑based similarity retrieval.
Conclusion
This guide shows how to combine OpenAI’s ChatGPT embeddings with Redis’ vector search capabilities in PHP, covering API interaction, index setup, vector storage, and KNN querying, enabling fast and accurate text similarity applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
