PHP code example of moe-mizrak / laravel-openrouter
1. Go to this page and download the library: Download moe-mizrak/laravel-openrouter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$content = 'Tell me a story about a rogue AI that falls in love with its creator.'; // Your desired prompt or content
$model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models)
$messageData = new MessageData([
'content' => $content,
'role' => RoleType::USER,
]);
$chatData = new ChatData([
'messages' => [
$messageData,
],
'model' => $model,
'max_tokens' => 100, // Adjust this value as needed
]);
$chatResponse = LaravelOpenRouter::chatRequest($chatData);
$content = 'Tell me a story about a rogue AI that falls in love with its creator.'; // Your desired prompt or content
$model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models)
$messageData = new MessageData([
'content' => $content,
'role' => RoleType::USER,
]);
$chatData = new ChatData([
'messages' => [
$messageData,
],
'model' => $model,
'max_tokens' => 100, // Adjust this value as needed
]);
/*
* Calls chatStreamRequest ($promise is type of PromiseInterface)
*/
$promise = LaravelOpenRouter::chatStreamRequest($chatData);
// Waits until the promise completes if possible.
$stream = $promise->wait(); // $stream is type of GuzzleHttp\Psr7\Stream
/*
* 1) You can retrieve whole raw response as: - Choose 1) or 2) depending on your case.
*/
$rawResponseAll = $stream->getContents(); // Instead of chunking streamed response as below - while (! $stream->eof()), it waits and gets raw response all together.
$response = LaravelOpenRouter::filterStreamingResponse($rawResponseAll); // Optionally you can use filterStreamingResponse to filter raw streamed response, and map it into array of responseData DTO same as chatRequest response format.
// 2) Or Retrieve streamed raw response as it becomes available:
while (! $stream->eof()) {
$rawResponse = $stream->read(1024); // readByte can be set as desired, for better performance 4096 byte (4kB) can be used.
/*
* Optionally you can use filterStreamingResponse to filter raw streamed response, and map it into array of responseData DTO same as chatRequest response format.
*/
$response = LaravelOpenRouter::filterStreamingResponse($rawResponse);
}
$model = 'mistralai/mistral-7b-instruct:free';
$firstMessage = new MessageData([
'role' => RoleType::USER,
'content' => 'My name is Moe, the AI necromancer.',
]);
$chatData = new ChatData([
'messages' => [
$firstMessage,
],
'model' => $model,
]);
// This is the chat which you want LLM to remember
$oldResponse = LaravelOpenRouter::chatRequest($chatData);
/*
* You can skip part above and just create your historical message below (maybe you retrieve historical messages from DB etc.)
*/
// Here adding historical response to new message
$historicalMessage = new MessageData([
'role' => RoleType::ASSISTANT, // set as assistant since it is a historical message retrieved previously
'content' => Arr::get($oldResponse->choices[0],'message.content'), // Historical response content retrieved from previous chat request
]);
// This is your new message
$newMessage = new MessageData([
'role' => RoleType::USER,
'content' => 'Who am I?',
]);
$chatData = new ChatData([
'messages' => [
$historicalMessage,
$newMessage,
],
'model' => $model,
]);
$response = LaravelOpenRouter::chatRequest($chatData);
$content = Arr::get($response->choices[0], 'message.content');
// content = You are Moe, a fictional character and AI Necromancer, as per the context of the conversation we've established. In reality, you are the user interacting with me, an assistant designed to help answer questions and engage in friendly conversation.
$content = 'Tell me a story about a rogue AI that falls in love with its creator.'; // Your desired prompt or content
$model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models)
$messageData = new MessageData([
'content' => $content,
'role' => RoleType::USER,
]);
$chatData = new ChatData([
'messages' => [
$messageData,
],
'model' => $model,
'max_tokens' => 100, // Adjust this value as needed
]);
$chatResponse = LaravelOpenRouter::chatRequest($chatData);
$generationId = $chatResponse->id; // generation id which will be passed to costRequest
$costResponse = LaravelOpenRouter::costRequest($generationId);
public function __construct(protected OpenRouterRequest $openRouterRequest) {}
$content = 'Tell me a story about a rogue AI that falls in love with its creator.'; // Your desired prompt or content
$model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models)
$messageData = new MessageData([
'content' => $content,
'role' => RoleType::USER,
]);
$chatData = new ChatData([
'messages' => [
$messageData,
],
'model' => $model,
'max_tokens' => 100, // Adjust this value as needed
]);
$response = $this->openRouterRequest->chatRequest($chatData);
$content = 'Tell me a story about a rogue AI that falls in love with its creator.';
$model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models)
$messageData = new MessageData([
'content' => $content,
'role' => RoleType::USER,
]);
$chatData = new ChatData([
'messages' => [
$messageData,
],
'model' => $model,
'max_tokens' => 100, // Adjust this value as needed
]);
$chatResponse = $this->openRouterRequest->chatRequest($chatData);
$generationId = $chatResponse->id; // generation id which will be passed to costRequest
$costResponse = $this->openRouterRequest->costRequest($generationId);