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.