2.3 KiB
2.3 KiB
OpenAI Embeddings with PocketFlow
This example demonstrates how to properly integrate OpenAI's text embeddings API with PocketFlow, focusing on:
-
Clean code organization with separation of concerns:
- Tools layer for API interactions (
tools/embeddings.py) - Node implementation for PocketFlow integration (
nodes.py) - Flow configuration (
flow.py) - Centralized environment configuration (
utils/call_llm.py)
- Tools layer for API interactions (
-
Best practices for API key management:
- Using environment variables
- Supporting both
.envfiles and system environment variables - Secure configuration handling
-
Proper project structure:
- Modular code organization
- Clear separation between tools and PocketFlow components
- Reusable OpenAI client configuration
Project Structure
pocketflow-tool-embeddings/
├── tools/
│ └── embeddings.py # OpenAI embeddings API wrapper
├── utils/
│ └── call_llm.py # Centralized OpenAI client configuration
├── nodes.py # PocketFlow node implementation
├── flow.py # Flow configuration
└── main.py # Example usage
Setup
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
-
Set up your OpenAI API key in one of two ways:
a. Using a
.envfile:OPENAI_API_KEY=your_api_key_hereb. Or as a system environment variable:
export OPENAI_API_KEY=your_api_key_here
Usage
Run the example:
python main.py
This will:
- Load the OpenAI API key from environment
- Create a PocketFlow node to handle embedding generation
- Process a sample text and generate its embedding
- Display the embedding dimension and first few values
Key Concepts Demonstrated
-
Environment Configuration
- Secure API key handling
- Flexible configuration options
-
Code Organization
- Clear separation between tools and PocketFlow components
- Reusable OpenAI client configuration
- Modular project structure
-
PocketFlow Integration
- Node implementation with prep->exec->post lifecycle
- Flow configuration
- Shared store usage for data passing