PHP code example of fholbrook / php-openrouter-client
1. Go to this page and download the library: Download fholbrook/php-openrouter-client 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/ */
fholbrook / php-openrouter-client example snippets
$client = new OpenRouter(
new OpenRouterConfig(
apiKey: 'your_api_key'
// see source for other optional configuration options
)
);
$chatData = new ChatRequest(
prompt: 'Tell me a story about a rogue AI that falls in love with its creator.',
model: 'mistralai/mistral-7b-instruct:free',
// all properties below are optional
response_format: new ResponseFormatData(
type: 'json_object',
),
stop: ['stop_token'],
stream: true,
include_reasoning: true,
max_tokens: 1024,
temperature: 0.7,
top_p: 0.9,
top_k: 50,
frequency_penalty: 0.5,
presence_penalty: 0.2,
repetition_penalty: 1.2,
seed: 42,
tool_choice: 'auto',
tools: [
// ToolCallData instances
],
logit_bias: [
'50256' => -100,
],
transforms: ['middle-out'],
models: ['model1', 'model2'],
route: 'fallback',
provider: new ProviderPreferencesData(
allow_fallbacks: true,
$chatData = new ChatRequest(
chat: new Chat(
messages: [
new Message(
role: RoleType::USER,
content: 'Tell me a story about a rogue AI that falls in love with its creator.',
),
],
),
model: 'mistralai/mistral-7b-instruct:free',
);
$response = $client->chat->chat(
new ChatRequest(
chat: new Chat(
id: 'your_conversation_id',
messages: [
new Message(
role: RoleType::USER,
content: 'What is the capital of France?',
),
]
),
model: 'mistralai/mistral-7b-instruct:free',
)
));
// $chat now contains the conversation history
$chat = $response->getChat();
// {role:'user', content:'What is the capital of France?'}
// {role:'assistant', content:'Paris'}
// Now you can append another message
$chat->messages[] = new Message(
role: RoleType::USER,
content: 'What is the capital of Germany?',
);
//and repeat the call
$response = $client->chat->chat(
new ChatRequest(
chat: $chat,
model: 'mistralai/mistral-7b-instruct:free',
)
));
$chat = $response->getChat();
//now contains:
// {role:'user', content:'What is the capital of France?'}
// {role:'assistant', content:'Paris'}
// {role:'user', content:'What is the capital of Germany?'}
// {role:'assistant', content:'Berlin'}
$chatData = new ChatData(
messages: [
new MessageData(
role: RoleType::USER,
content: 'Tell me a story about a rogue AI that falls in love with its creator.',
),
],
model: 'mistralai/mistral-7b-instruct:free',
response_format: new ResponseFormatData(
type: 'json_object',
),
provider: new ProviderPreferencesData(
$chatData = new ChatData(
messages: [
new MessageData(
role : RoleType::USER,
content: 'Tell me a story about a rogue AI that falls in love with its creator.',
),
],
model: 'mistralai/mistral-7b-instruct:free',
response_format: new ResponseFormatData(
type: 'json_schema',
json_schema: new Schema(
type: 'object',
properties: [
new Property(
name: 'name',
type: 'string',
)
],
)
),
provider: new ProviderPreferencesData(
$chatRequest = new ChatRequest(
prompt: 'What is the weather in Dallas?',
model: 'anthropic/claude-3.5-sonnet',
tool_choice: 'auto',
tools: [
new ToolCall(
type: 'function',
function: new FunctionData(
name: 'weather',
description: 'Get the weather for a specific zip code.',
parameters: new Schema(
type: 'object',
properties: [
new Property(
name: 'zip_code',
type: 'string',
description: 'The zip code to get weather for'
),
]
)
)
)
]
);
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.