Skip to main content

Overview

RAG Chat offers several configuration options to customize its behavior, from API keys to text processing parameters and system prompts.

Environment Configuration

OpenAI API Key Setup

RAG Chat requires an OpenAI API key to function:
app.py
from dotenv import load_dotenv

load_dotenv()
1

Create .env File

Create a .env file in the root directory of your project:
touch .env
2

Add Your API Key

Add your OpenAI API key to the .env file:
.env
OPENAI_API_KEY=sk-your-api-key-here
3

Restart Application

Restart the Streamlit application to load the new environment variables:
streamlit run app.py
Never commit your .env file to version control. Add it to .gitignore to keep your API key secure.

Getting an API Key

  1. Visit OpenAI’s platform
  2. Sign up or log in to your account
  3. Navigate to API Keys section
  4. Create a new secret key
  5. Copy the key to your .env file
You’ll need billing set up on your OpenAI account to use the API. Check OpenAI’s pricing for current rates.

Vector Store Configuration

Persistent Directory

Documents are stored in a persistent directory:
app.py
persistant_directory = 'db'
By default, the vector store is saved to a db directory in your project root.
persistant_directory = 'db'

Vector Store Initialization

app.py
def load_existing_vector_store():
    if os.path.exists(persistant_directory):
        vector_store = Chroma(
            persist_directory=persistant_directory,
            embedding_function=OpenAIEmbeddings()
        )
        return vector_store
    return None
The application checks for existing data on startup and loads it automatically.

Text Processing Configuration

Chunk Size and Overlap

These parameters control how documents are split into chunks:
app.py
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size = 1000,
    chunk_overlap = 400
)
chunk_size
int
default:"1000"
Maximum number of characters per chunk
  • Larger values: More context per chunk, but less granular retrieval
  • Smaller values: More granular retrieval, but less context
  • Recommended range: 500-2000 characters
chunk_overlap
int
default:"400"
Number of characters to overlap between chunks
  • Larger values: Better context preservation across chunk boundaries
  • Smaller values: Less redundancy, faster processing
  • Recommended: 20-40% of chunk_size

Customizing Chunking Parameters

For documents where you want more context per chunk:
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size = 2000,    # Larger chunks
    chunk_overlap = 500   # Proportional overlap
)
Use when:
  • Documents have long, connected narratives
  • You need more context in each retrieval
  • Working with complex technical content
Experiment with different chunk sizes to find what works best for your specific documents and use case.

System Prompt Customization

The system prompt controls how the AI responds to questions:
app.py
system_prompt = '''
Use o contexto para responder as perguntas.
Se não encontrar uma resposta no contexto,
explique que não há informações disponíveis.
Responda em formato de markdown e com visualizações
elaboradas e interativas.
Contexto: {context}
'''

Custom System Prompts

Translate the system prompt to English:
system_prompt = '''
Use the context to answer the questions.
If you cannot find an answer in the context,
explain that the information is not available.
Respond in markdown format with elaborate
and interactive visualizations.
Context: {context}
'''
Always keep the {context} placeholder in your system prompt - this is where retrieved document chunks are inserted.

Streamlit Configuration

Page Configuration

app.py
st.set_page_config(
    page_title='RAG Chat',
    page_icon='🤖'
)
Customize the browser tab title and icon:
st.set_page_config(
    page_title='My Custom RAG App',
    page_icon='📚'
)

Header Customization

app.py
st.header('🤖 Como posso te ajudar hoje?')
Customize the main header:
st.header('📚 Ask me anything about your documents')

Current Configuration

Currently, the .env file only requires:
.env
# Required: OpenAI Configuration
OPENAI_API_KEY=sk-your-api-key-here
This is the only environment variable currently used by RAG Chat. Other configuration options require code modification (see Advanced Configuration below).

Advanced Configuration

The following customizations require modifying the source code in app.py. These are not built-in features but examples of how you can extend RAG Chat.

Using Environment Variables

You can modify app.py to use environment variables for configuration:
import os
from dotenv import load_dotenv

load_dotenv()

# Configurable chunk size
chunk_size = int(os.getenv('CHUNK_SIZE', 1000))
chunk_overlap = int(os.getenv('CHUNK_OVERLAP', 400))

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size = chunk_size,
    chunk_overlap = chunk_overlap
)

# Configurable default model
default_model = os.getenv('DEFAULT_MODEL', 'gpt-3.5-turbo')

Embedding Configuration

Customize the embedding model:
# Use a specific embedding model
embedding_function = OpenAIEmbeddings(
    model="text-embedding-3-small"
)

vector_store = Chroma(
    persist_directory=persistant_directory,
    embedding_function=embedding_function
)

Configuration Checklist

1

API Key

✅ Set up .env file with OPENAI_API_KEY
2

Storage

✅ Configure persistant_directory for vector store
3

Chunking

✅ Adjust chunk_size and chunk_overlap if needed
4

System Prompt

✅ Customize system_prompt for your use case
5

UI

✅ Configure Streamlit page settings

Troubleshooting

API Key Issues

  • Verify the key in your .env file
  • Ensure no extra spaces or quotes
  • Check that the key starts with sk-
  • Verify the key is active in your OpenAI account
  • Ensure .env is in the root directory
  • Restart the application after creating .env
  • Check that python-dotenv is installed

Storage Issues

  • Check directory permissions
  • Ensure sufficient disk space
  • Verify the path is valid
  • Confirm persist_directory is set correctly
  • Check that the directory is not being deleted
  • Verify write permissions

Next Steps

Uploading Documents

Learn how to upload and process documents

API Reference

Explore the complete API reference

Build docs developers (and LLMs) love