This is a TypeScript implementation of a Retrieval-Augmented Generation (RAG) system that uses Galileo for logging. The system retrieves relevant documents based on a query and uses them to generate an answer.
- Simulated document retrieval (can be replaced with actual vector database retrieval)
- LLM-based answer generation using retrieved documents
- Galileo logging for retriever and LLM spans
- Interactive command-line interface
- Install dependencies:
npm install- Create a
.envfile based on the.env.examplefile:
cp .env.example .env- Add your OpenAI API key and Galileo API key to the
.envfile:
# Galileo Environment Variables
GALILEO_API_KEY=your-galileo-api-key # Your Galileo API key.
GALILEO_PROJECT=your-galileo-project-name # Your Galileo project name.
GALILEO_LOG_STREAM=your-galileo-log-stream # The name of the log stream you want to use for logging.
# Provide the console url below if you are using a custom deployment, and not using app.galileo.ai
# GALILEO_CONSOLE_URL=your-galileo-console-url # Optional if you are using a hosted version of Galileo
OPENAI_API_KEY=your_openai_api_key_here
To run the RAG system:
npm startOr with TypeScript directly:
npx ts-node app.tsFor development with auto-reload:
npm run devTo build the TypeScript code:
npm run build- The system uses the
@logdecorator from Galileo to log retriever and LLM spans. - When a query is received, the system:
- Retrieves relevant documents (currently simulated)
- Formats the documents into a prompt
- Sends the prompt to the LLM to generate an answer
- Returns the answer to the user
- The entire process is logged in Galileo for observability.
The span hierarchy created by the RAG system looks like:
retrieveDocuments(retriever span)- LLM call (automatically logged by the wrapped OpenAI client)
This hierarchy accurately represents the flow of operations in the RAG system.
To extend this system with a real vector database:
- Add a vector database client (e.g., Pinecone, Weaviate, Chroma) to the dependencies
- Modify the
retrieveDocumentsfunction to query the vector database - Add document embedding and indexing functionality