Create a OpenAI LLM interface to custom information. In this example we use LlamaIndex to provide a ChatGPT interface to Alice in Wonderland.
Large language models, such as ChatGPT, can do amazing things but are limited to the data upon which they are trained. To unlock further potential of these tools we need to be able to integrate custom information, often proprietary or context-specific data. This allows us to apply these high-powered tools in new and exciting ways.
Overview
In this example we will use the LlamaIndex (formerly GPT Index) framework to ingest our own data, a copy of Alice in Wonderland, and provide a query-response interface to discuss the book. This is an extension of the LlamaIndex Starter Tutorial with additional implementation details and integration of new user data. The extensions include the following:
- Using a Python virtual environment to cleanly manage dependencies.
- Use environment variable to protect your personal API key
- Demonstrate how to integrate new data – beyond what comes with the LlamaIndex example.
- Illustrate the results with some example queries and responses
The full working example is in our companion github repository: https://github.com/bsb808/ai-escapades/tree/main/llamaindex
Tutorial
Installation – Python Virtual Environment
For a number of great reasons, mostly related to dependency management, it makes sense to use Python virtual environments, especially for experimentation and exploration
If you don’t have the venv
package, install:
sudo apt install python3.10-venv
Clone this blog’s companion repository:
git clone https://github.com/bsb808/ai-escapades.git
Within the source code directory, create and activate a new virtual environment. Then install the requirements (including the llama-index package) with pip:
cd ai-escapades/llamaindex python3 -m venv venv . venv/bin/activate pip install -r requirements.txt
Setup OpenAI API Key Using Python dotenv
It is important to follow best practices for API key safety to avoid racking up large fees from stolen keys. The way we do this is this example is as follows:
- Copy the included skeleton file: cp .env.example .env
- Edit the .env file to add your private API key (and make sure that you do not add the .env file to your git repo, or, better yet, include it in the .gitignore list).
- In the Python program us the dotenv module to read the environmental variable from the .env file, e.g.,
from dotenv import load_dotenv # Load the .env variables and set API key. load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY")
Include New Data
For this example, we downloaded Alice’s Adventures in Wonderland by Lewis Caroll in plain text and include it in the data_alice directory. The data is already there for you from cloning the repositoy.
Run the Example
We use iPython to run the example, but you can also call it directly. Either way, you will execute it from within the virtual environment.
Start iPython: ipython3
Run the alice_example.py within iPython: run -i alice_example.py
The example illustrates the following steps:
# Load the .env variables and set API key. load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") # Index / embed the custom data, in the case the text of Alice in Wonderland documents = SimpleDirectoryReader('data_alice').load_data() index = VectorStoreIndex.from_documents(documents) # Example query the LLM interface to the Alice data query_engine = index.as_query_engine() query = "What does Alice look like?" response = query_engine.query(query)
Representative Results
If all goes well, you should have a ChatGPT interface with which to discuss Alice in Wonderland. Here are some examples of queries and responses:
- Query: What does Alice look like?
- Response: Alice is described as a young girl with blonde hair that does not go in ringlets. She is wearing a white kid glove and is about two feet tall.
- Query: How does Alice get down the hole?
- Response: Alice gets down the hole by falling.
- Query: Who is at the tea party?
- Response: At the tea party, Alice finds the Hatter, the Dormouse, and the Queen of Hearts.
- Query: Write a short paragraph to describe the tea party.
- Response:
The tea party Alice attended was a strange and chaotic affair. The Hatter was rude to Alice, and the Dormouse fell asleep instantly. The other two guests, Five and Seven, were busy painting roses red, and when the Queen arrived, they all bowed low. Alice asked why they were painting the roses, and Five and Seven said it was because they had put a white rose-tree in by mistake and would be beheaded if the Queen found out. The March Hare then suggested Alice tell a story, and the Dormouse began a tale about three little sisters who lived at the bottom of a well. Alice was offered more tea, but she had not yet had any. It was clear that this was not a typical tea party.
- Query: Write a riddle about the cat.
- Response: What has a tail that wags when it’s angry and growls when it’s pleased?