OpenAI Configuration¶
Use OpenAI's GPT models for high-quality documentation search.
Prerequisites¶
- OpenAI API account
- API key from platform.openai.com
Configuration¶
Create a .env file:
# LLM Configuration
LLM_PROVIDER="openai"
LLM_MODEL="gpt-4o"
LLM_API_KEY="sk-your-api-key-here"
# Embedding Configuration
EMBEDDING_PROVIDER="openai"
EMBEDDING_MODEL="text-embedding-3-small"
Full Configuration¶
# LLM Configuration
LLM_PROVIDER="openai"
LLM_MODEL="gpt-4o"
LLM_API_KEY="sk-your-api-key-here"
# Embedding Configuration
EMBEDDING_PROVIDER="openai"
EMBEDDING_MODEL="text-embedding-3-small"
EMBEDDING_DIMENSIONS="1536"
# Optional: Use Azure OpenAI
# LLM_ENDPOINT="https://your-resource.openai.azure.com/"
# AZURE_OPENAI_API_KEY="your-azure-key"
Verify Setup¶
import asyncio
import pygrad as pg
async def test_openai():
print("Testing OpenAI configuration...")
# Index a repository
await pg.add("https://github.com/encode/httpx")
print("Repository indexed!")
# Search
result = await pg.search(
"https://github.com/encode/httpx",
"How to make HTTP requests?"
)
print(result)
asyncio.run(test_openai())
Cost Optimization¶
Use Smaller Models¶
For development and testing:
Batch Processing¶
Index repositories in batches to reduce API calls:
Cache Results¶
Pygrad caches indexed repositories, so subsequent searches don't require re-indexing.
Azure OpenAI¶
For Azure OpenAI deployments:
LLM_PROVIDER="azure"
LLM_ENDPOINT="https://your-resource.openai.azure.com/"
LLM_MODEL="your-deployment-name"
AZURE_OPENAI_API_KEY="your-azure-key"
AZURE_OPENAI_API_VERSION="2024-02-01"
EMBEDDING_PROVIDER="azure"
EMBEDDING_MODEL="your-embedding-deployment-name"
Troubleshooting¶
Authentication Error¶
Verify your API key:
Rate Limits¶
If you hit rate limits:
- Use a smaller model
- Add delays between requests
- Upgrade your OpenAI plan
Timeout Errors¶
For large repositories, increase timeout:
# Handle timeouts gracefully
import asyncio
try:
await asyncio.wait_for(pg.add(url), timeout=300)
except asyncio.TimeoutError:
print("Processing took too long, try a smaller repository")