Monday, May 29, 2023

OpenAI Q&A using Redis VSS for Context

Summary

I'll be covering the use case of providing supplemental context to OpenAI in a question/answer scenario (ChatGPT).  Various news articles will be vectorized and stored in Redis.  For a given question that lies outside of ChatGPT's knowledge, additional context will be fetched from Redis via Vector Similarity Search (VSS).   That context will aid ChatGPT in providing a more accurate answer.

Architecture


Code Snippets

OpenAI Prompt/Collect Helper Function


The code below is a simple function for sending a prompt into ChatGPT and then extracting the resulting response.

OpenAI QnA Example 1


The prompt below is on a topic (FTX meltdown) that is outside of ChatGPT's training cut-off date. As a result, the response is of poor quality (wrong).

Redis Context Index Build


The code below uses Redis-py client lib to build an index for business article content in Redis. The index has two fields in its schema: the text content itself and a vector representing the embedding of that text content.

Context Storage as Redis JSON


The code below loads up a dozen different business articles into Redis as JSON objects.

RedisInsight



Redis Vector Search (KNN)


A vector search in Redis is depicted below. This particular query picks the #1 article as far as vector distance to a given question (prompt).

Reprompt ChatGPT with Redis-fetched Context


The context fetched in the previous step is now added as supplemental info to ChatGPT for the same FTX-related question. The response is now in line with expectations.

Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.

OpenAI + Redis VSS w/JSON

Summary

This post will cover an example of how to use Redis Vector Similarity Search (VSS) capabilities with OpenAI as the embedding engine.  Documents will be stored as JSON objects within Redis and then searched via VSS via KNN and Hybrid queries.

Architecture

Code Snippets

OpenAI Embedding


Redis Index Creation


Redis JSON Document Insertion


RedisInsight



Redis Semantic Search (KNN)


Redis Hybrid Search (Full-text + KNN)


Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.

Saturday, May 27, 2023

Redis Polygon Search

Summary

This post will demonstrate the usage of a new search feature within Redis - geospatial search with polygons.  This search feature is part of the 7.2.0-M01 Redis Stack release.  This initial release supports the WITHIN and CONTAINS query types for polygons, only.  Additional geospatial search types will be forthcoming in future releases.  

Architecture


Code Snippets

Point Generation

I use the Shapely module to generate the geometries for this demo.  The code snippet below will generate a random point, optionally within a bounding box.

Polygon Generation

Random polygons can be generated using the random point function above.  By passing a polygon as an input parameter, the generated polygon can be placed inside that input polygon.

Redis Polygon Search Index

The command below creates an index on the polygons with the new keyword 'GEOMETRY' for their associated WKT-formatted points.  Note this code is sending a raw CLI command to Redis.  The redis-py lib does not support the new geospatial command sets at the time of this writing.

Redis Polygon Load as JSON

The code below inserts 4 polygons into Redis as JSON objects.  Those objects are indexed within Redis by the code above.
  

Redis Polygon Search

Redis Polygon search (contains or within) code below. Again, this is the raw CLI command.

Results

Plot






Results


Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.