The code in this repository is a combination of the following:
- Based on the Petal Pro boilerplate code (Petal Pro 1.6.2)
- This article on how to use
pgvector, python and OpenAI - This example OpenAI code written in Elixir
Requires the pgvector Postgres extension. I had to follow the instructions for Windows to build and install the extension.
Once the pgvector extension is installed, run mix setup. This will download dependencies, compile the code and run the migrations. There are two migrations that are relevant to pgvector:
create_vector_extensionenables thepgvectorextension for the databasecreate_embeddingscreates a table calledembeddingsthat includes a:vectorfield
priv/repo/seeds.exs has been updated to parse priv/repo/blog_data_and_embeddings.csv which is used to update the embeddings table.
blog_data_and_embeddings.csv was generated from the Jupyter Notebook that's used in the blog article. It contains data about Timescale and vector data (that was created by OpenAI). To see what this data looks like, go to:
http://localhost:4000/embeddings
Now your ready to interact with the chat bot:
http://localhost:4000/recipes/open-ai/examples/open-ai-prompt
Type in a question. It's just like using ChatGPT. If you ask a question about Timescale, then it will include custom data from the embedded knowledge base.
To see more detail about the process, see the original article. But here's the gist of how it works:
- Starting with your knowledge base, you make an OpenAI call to generate vector data for each document
- Store vector data in a vector database (in our case postgres with the
pgvectorextension) - Use OpenAI to generate vector data for a search phrase
- Use the vector database to compare the search phrase vector data with the knowledge base vector data
- Return the top 3 documents
- Generate a LLM response by generating a prompt that includes your search phrase and the contents of the top three documents
