Environment:Iamhankai Forest of Thought OpenAI API Credentials
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, LLMs |
| Last Updated | 2026-02-14 03:30 GMT |
Overview
OpenAI API credentials environment providing `OPENAI_API_KEY` and optional `OPENAI_API_BASE` for GPT-based reasoning in the Forest-of-Thought framework.
Description
This environment defines the API credentials required when using the OpenAI GPT backend (GPT-4, GPT-3.5-turbo, GPT-4o) instead of locally loaded models. The `models/models.py` module reads the API key from the `OPENAI_API_KEY` environment variable and optionally overrides the API base URL via `OPENAI_API_BASE`. The code defaults to a third-party proxy endpoint (`https://api.chatanywhere.tech/v1`) if no custom base URL is provided.
This environment is optional — the primary workflow uses locally loaded HuggingFace models. The OpenAI path is used only when the `--backend` argument contains `"gpt"` (e.g., in `methods/bfs.py:L99` and `scripts/game24/run.py`).
Usage
Use this environment when running Forest-of-Thought experiments with GPT-4 or GPT-3.5-turbo as the backend model. This is primarily used in the Game24 BFS workflow when `--backend gpt-4` is specified, and in the MCTS task when `propose_method='gpt'`.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| Network | Internet access | Required to reach OpenAI API endpoints |
| Python | `openai` == 0.27.7 | Pinned version in requirements.txt |
| Python | `backoff` == 2.2.1 | For exponential backoff on API errors (commented out but available) |
Dependencies
Python Packages
- `openai` == 0.27.7
- `backoff` == 2.2.1
Credentials
The following environment variables must be set:
- `OPENAI_API_KEY`: OpenAI API key for authentication. The code prints a warning if unset but does not raise an error immediately.
- `OPENAI_API_BASE` (optional): Custom API base URL. If unset, defaults to `https://api.chatanywhere.tech/v1`.
Quick Install
pip install openai==0.27.7 backoff==2.2.1
# Set credentials
export OPENAI_API_KEY="your-api-key-here"
# Optional: override API endpoint
export OPENAI_API_BASE="https://api.openai.com/v1"
Code Evidence
API key loading from `models/models.py:L9-13`:
api_key = os.getenv("OPENAI_API_KEY", "")
if api_key != "":
openai.api_key = api_key
else:
print("Warning: OPENAI_API_KEY is not set")
Custom base URL override from `models/models.py:L15-18`:
api_base = os.getenv("OPENAI_API_BASE", "")
if api_base != "":
print("Warning: OPENAI_API_BASE is set to {}".format(api_base))
openai.api_base = api_base
Hardcoded proxy endpoint in `models/models.py:L21-25`:
client = OpenAI(
api_key=api_key,
base_url="https://api.chatanywhere.tech/v1"
)
GPT backend detection in `methods/bfs.py:L99`:
if 'gpt' in args.backend:
response = llm(propose_prompt, n=1, stop=None)[0].split('\n')
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `Warning: OPENAI_API_KEY is not set` | `OPENAI_API_KEY` environment variable not configured | Set `export OPENAI_API_KEY="sk-..."` before running |
| `openai.error.AuthenticationError` | Invalid or expired API key | Verify your API key at https://platform.openai.com/api-keys |
| `openai.error.RateLimitError` | API rate limit exceeded | The `backoff` library is available for retry logic; reduce concurrent requests |
Compatibility Notes
- API Version: Uses `openai` == 0.27.7 (legacy SDK). The code also imports `from openai import OpenAI` (v1.x style), creating a version mismatch. The pinned version in `requirements.txt` should be used.
- Proxy Endpoint: The hardcoded base URL `https://api.chatanywhere.tech/v1` is a third-party proxy. Set `OPENAI_API_BASE` to use the official OpenAI endpoint instead.
- Cost Tracking: The `gpt_usage()` function in `models/models.py` tracks token costs for GPT-4, GPT-3.5-turbo, and GPT-4o.