Getting started
Get the OpenAI API key
Before using the ChatGPT API Framework, you'll need to obtain an API key from OpenAI. You can get your API key by registering at
OpenAI.
Initialize the OpenAI API
The ChatGPT API Framework provides a convenient way to initialize the OpenAI API. To get started, you can use the following code:
PHP:
/** \Orhanerday\OpenAi\OpenAi $api */
$api = \XF::app()->container('chatGPT');
This code initializes the OpenAI API and assigns it to the $api variable.
Get a reply from ChatGPT
To get a reply from ChatGPT, you can use the Response::getReply() function provided by the ChatGPT API Framework. Here's an example code snippet:
Code:
use BS\ChatGPTBots\Response;
$messages = [
['role' => 'user', 'content' => 'Hello!']
];
$reply = Response::getReply(
$api->chat([
'model' => 'gpt-3.5-turbo',
'messages' => $messages,
'temperature' => 1.0,
'max_tokens' => 420,
'frequency_penalty' => 0,
'presence_penalty' => 0,
])
);
This code initializes an array of messages to send to ChatGPT and uses the
chat()
function to get a response. The response is returned as the
$reply
variable.
Get a reply from ChatGPT with logging errors
The method attempts to get a reply from OpenAI's Chat API using the provided parameters, and logs any errors that occur during the process. It returns a reply if successful, or a default error message if not.
Code:
use BS\ChatGPTBots\Response;
$messages = [
['role' => 'user', 'content' => 'Hello!']
];
$reply = Response::getReplyWithLogErrors(
$api->chat([
'model' => 'gpt-3.5-turbo',
'messages' => $messages,
'temperature' => 1.0,
'max_tokens' => 420,
'frequency_penalty' => 0,
'presence_penalty' => 0,
])
);
Message Repository \BS\ChatGPTBots\Repository\Message
The ChatGPT API Framework provides a message repository to manage messages for your bots. The repository has several useful functions, including:
fetchMessagesFromThread()
Loads the context for the bot from the topic. Bot quotes are transformed into his messages for the correct context.
PHP:
public function fetchMessagesFromThread(
Thread $thread,
int $stopPosition = null,
?User $assistant = null,
bool $transformAssistantQuotesToMessages = true,
int $startPosition = null,
bool $removeQuotesFromAssistantMessages = true
)
fetchMessagesFromConversation()
This function loads the context for a bot from a conversation. Bot quotes are transformed into his messages for the correct context.
PHP:
public function fetchMessagesFromConversation(
ConversationMaster $conversation,
?ConversationMessage $beforeMessage = null,
?User $assistant = null,
int $limit = 0,
bool $reverseLoad = false,
bool $transformAssistantQuotesToMessages = true,
bool $removeQuotesFromAssistantMessages = true
)
fetchMessagesFromConversation()
This function loads the context for a bot from a conversation. Bot quotes are transformed into his messages for the correct context.
PHP:
public function fetchMessagesFromConversation(
ConversationMaster $conversation,
?ConversationMessage $beforeMessage = null,
?User $assistant = null,
int $limit = 0,
bool $reverseLoad = false,
bool $transformAssistantQuotesToMessages = true,
bool $removeQuotesFromAssistantMessages = true
)
fetchCommentsFromProfilePost()
This function loads the context for a bot from a profile post.
PHP:
public function fetchCommentsFromProfilePost(
ProfilePost $profilePost,
?ProfilePostComment $beforeComment = null,
?User $assistant = null,
int $limit = 0,
bool $reverseLoad = false
)
wrapMessage()
Generates a message array, preparing content for the bot (removes unnecessary BB codes).
PHP:
public function wrapMessage(string $content, string $role = 'user'): array
prepareContent()
Prepare message content for the bot (removes unnecessary BB codes).
PHP:
public function prepareContent(string $content, bool $stripQuotes = true): string
getQuotes()
Parses quotes from the text, bringing it to a convenient form.
PHP:
public function getQuotes(string $text, int $userId = null, int $postId = null, string $postType = 'post'): array
removeQuotes()
Remove quotes from the text. Can be remove quotes for specific posts or users.
PHP:
public function removeQuotes(string $text, int $userId = null, int $postId = null, string $postType = 'post'): string