Download the PHP package xp-forge/openai without Composer
On this page you can find all versions of the php package xp-forge/openai. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xp-forge/openai
More information about xp-forge/openai
Files in xp-forge/openai
Package openai
Short Description OpenAI APIs for XP Framework
License BSD-3-Clause
Homepage http://xp-framework.net/
Informations about the package openai
OpenAI APIs for XP
This library implements OpenAI APIs with a low-level abstraction approach, supporting their REST and realtime APIs, request and response streaming, function calling and TikToken encoding.
Completions
Using the REST API, see https://platform.openai.com/docs/api-reference/making-requests
Streaming
The REST API can use server-sent events to stream responses, see https://platform.openai.com/docs/api-reference/streaming
To access the result object after streaming, use $stream->result()
. It contains the choices list as well as model, filter results and usage information.
TikToken
Encodes text to tokens. Download the vocabularies cl100k_base (used for GPT-3.5 and GPT-4.0) and o200k_base (used for Omni and O1) first!
Instead of encode(), you can use count() to count the number of tokens.
Embeddings
To create an embedding for a given text, use https://platform.openai.com/docs/guides/embeddings/what-are-embeddings
Text to speech
To stream generate audio, use the API's transmit() method, which sends the given payload and returns the response. See https://platform.openai.com/docs/guides/text-to-speech/overview
Speech to text
To convert audio into text, upload files via the API's open() method, which returns an Upload instance. See https://platform.openai.com/docs/guides/speech-to-text/overview
You can also stream uploads from InputStreams as follows:
Tracing the calls
REST API calls can be traced with the logging library:
Tool calls
There are two types of tools: Built-ins like file_search and code_interpreter (available in the assistants API) as well as custom functions, see https://platform.openai.com/docs/guides/function-calling
Defining functions
Custom functions map to instance methods in a class:
The Param annnotation may define a description and a JSON schema type:
#[Param('The name of the city')] $name
#[Param(type: ['type' => 'string', 'enum' => ['C', 'F']])] $unit
Passing custom functions
Custom functions are registered in a Functions
instance and passed via tools inside the payload.
Invoking custom functions
If tool calls are requested by the LLM, invoke them and return to next completion cycle. See https://platform.openai.com/docs/guides/function-calling/configuring-parallel-function-calling
Passing context
Functions can be passed a context as follows by annotating parameters with the Context annotation:
Azure OpenAI
These endpoints differ slightly in how they are invoked, which is handled by the AzureAI implementation. See https://learn.microsoft.com/en-us/azure/ai-services/openai/overview
Distributing requests
The Distributed endpoint allows to distribute requests over multiple endpoints. The ByRemainingRequests class uses the x-ratelimit-remaining-requests
header to determine the target. See https://platform.openai.com/docs/guides/rate-limits
For more complex load balancing, have a look at this blog article using Azure API management
Realtime API
The realtime API allows streaming audio and/or text to and from language models, see https://platform.openai.com/docs/guides/realtime
For Azure AI, the setup code is slightly different:
See also
All versions of openai with dependencies
xp-framework/logging Version ^11.2
xp-framework/reflection Version ^3.0 | ^2.0
xp-forge/marshalling Version ^2.3
xp-forge/rest-client Version ^5.7
xp-forge/websockets Version ^4.0
php Version >=7.4.0