Download the PHP package focusbp/openai-response-php without Composer
On this page you can find all versions of the php package focusbp/openai-response-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download focusbp/openai-response-php
More information about focusbp/openai-response-php
Files in focusbp/openai-response-php
Package openai-response-php
Short Description OpenAI Responses API helper for PHP with function calling and vector store sync.
License MIT
Informations about the package openai-response-php
OpenAI Response PHP
A lightweight PHP library that wraps the OpenAI Responses API to provide:
- Vector Store integration (sync local text files into an OpenAI Vector Store and use them as context)
- Function Calling support (tool invocation with typed arguments)
This library is intended to be easy to drop into a simple PHP app, even on older environments.
Install
You can install it via Composer:
Features
🧠 Vector Store Integration
- Syncs a local directory of text files into an OpenAI Vector Store.
- Lets the model answer questions using that knowledge.
🔧 Function Calling
- Define tools (functions) in PHP and expose them to the model.
- The model can call your tools with structured arguments.
- Supports dependency injection via a controller object.
💬 Conversation Recording
- Conversation history (messages) can be persisted via
Recorderimplementations:SessionRecorder(stores in$_SESSION)FileRecorder(stores in local JSON files)
📡 Status Polling
- Includes a simple async-style status polling example using
status_poll.phpto report "what the AI is doing right now" to the frontend.
Requirements
- PHP: 7.3+ (no typed properties required)
- Extensions:
cURL,json - Web server: Any server that can run PHP (Apache, nginx + PHP-FPM, etc.)
- OpenAI API Key
Quick Start
This is the fastest way to see it working in a browser.
-
Set your API key
Open
tests/config/config.phpand set your OpenAI API key: -
Deploy
Upload the entire
tests/directory (and the project library) to a PHP-capable web server. -
Access the demo UI
Open
index.phpin your browser.
The sample UI can answer:- questions about upcoming events (from the Vector Store)
- weather questions (via Function Calling)
Vector Store Usage
1. Add source files
Put plain text files (or other supported text content) into the vector_store/ directory.
For example:
These files act as your "knowledge base."
2. Sync with OpenAI
From the sample UI (the test app in tests/), click:
"Sync Vector Store"
When you click that:
- The library will upload/sync the contents of
vector_store/to the configured Vector Store in OpenAI. - The Vector Store will then be used to ground answers.
If a Vector Store does not exist yet, the library can create one.
If it already exists, the library can update it.
Function Calling
The library exposes a Function Calling interface so the model can decide to call your PHP functions as tools.
1. Create a tool
Create a class in the function_tools/ directory that implements \focusbp\OpenAIResponsePhp\FunctionTool.
Example (simplified):
2. Dependency injection via Controller
If your tool needs shared services like:
- database connections
- HTTP clients
- config
…you can create your own class that extends \focusbp\OpenAIResponsePhp\Controller and put those dependencies there.
Then, when you instantiate the OpenAI client, pass that controller instance into the constructor.
Inside execute(), you’ll receive that same $ctl instance, so you can do things like $ctl->db->query(...) or $ctl->weatherApi->fetch(...).
3. Auto-loading
Any class you put in function_tools/ that implements FunctionTool will be auto-discovered and made available to the model.
Conversation & Status
Message history
The library keeps a running conversation history (system / user / assistant / tool messages).
This can be persisted using a Recorder:
SessionRecorderkeeps messages in$_SESSION.FileRecordercan write JSON logs to disk.
Both provide:
read()write()append()
Status polling
The sample UI calls status_poll.php on an interval and displays a short status string (e.g. “fetching vector store…”).
This status string is stored via a StatusManager implementation.
Example implementation:
SessionStatusManagerstores a_status_msgvalue in$_SESSION.
Logging
All request/response logs and debug output can be written to the log/ directory.
If you’re debugging or auditing model behavior:
- Check the generated files in
log/. - You can also wire up a
FileRecorderso you have a full transcript.
Project Structure (reference)
Below is a typical layout to help you navigate:
Demo Flow (What the sample UI does)
- You open
tests/index.phpin your browser. - You see a chat-like interface.
- You ask something like:
- “What upcoming events are there?”
- “What’s the weather tomorrow in Tokyo?”
- Under the hood:
- For event questions, it retrieves knowledge from the synced Vector Store.
- For weather questions, it calls the
Weathertool via Function Calling.
- The UI polls
status_poll.phpso you can see status text like “Start AI Processing...” or other internal messages.
Security Notes
- Do not expose your API key publicly.
tests/index.phpis for local or protected testing. - Vector Store content is sent to OpenAI.
Do not sync files that contain secrets, PII, or anything you’re not comfortable sending to an API. - Logs may contain both user input and model output.
License
Add your preferred license here (MIT, Apache-2.0, etc.).
Contributing
Pull requests and issues are welcome.
If you add new tools or storage backends (e.g. RedisRecorder, DatabaseStatusManager), please include minimal usage docs in your PR so others can learn from it.
All versions of openai-response-php with dependencies
ext-curl Version *
ext-json Version *