PHP code example of r5dy1n / straico-php
1. Go to this page and download the library: Download r5dy1n/straico-php 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/ */
r5dy1n / straico-php example snippets
$straico = new \Straico\Straico('your-api-key-here');
traico\Straico;
use Straico\Exceptions\StraicoException;
// Using environment variable
$straico = new Straico();
// Or with API key parameter
$straico = new Straico('your-api-key-here');
try {
$user = $straico->user();
print_r($user);
} catch (StraicoException $e) {
echo "Error: " . $e->getMessage();
}
// Get models (v0)
$models = $straico->models();
// Get models (v1)
$models = $straico->modelsV1();
// Get models (v2) - OpenAI-Compatible format
$models = $straico->modelsV2();
foreach ($models['data'] as $model) {
echo $model['name'] . " - " . $model['pricing']['coins'] . " coins\n";
}
$model = $straico->model('openai/gpt-4');
print_r($model);
// Using v0 (default)
$response = $straico->promptCompletion(
model: 'openai/gpt-4',
message: 'Explain quantum computing in simple terms',
options: []
);
// Using v1
$response = $straico->promptCompletionV1(
model: 'openai/gpt-4',
message: 'Explain quantum computing in simple terms',
options: []
);
echo $response['choices'][0]['message']['content'];
$response = $straico->promptCompletion(
model: 'openai/gpt-4',
message: 'Analyze this image and describe what you see',
options: [
'images' => ['path/to/image.jpg'],
'display_transcripts' => true
]
);
$response = $straico->promptCompletion(
model: 'anthropic/claude-3-opus',
message: 'Summarize this video',
options: [
'youtube_urls' => ['https://youtube.com/watch?v=...'],
'display_transcripts' => true
]
);
// Using v0 (default)
$response = $straico->chatCompletion(
model: 'openai/gpt-4',
messages: [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => 'What is the capital of France?']
]
);
// Using v1
$response = $straico->chatCompletionV1(
model: 'openai/gpt-4',
messages: [
['role' => 'user', 'content' => 'Hello!']
]
);
// Using v2
$response = $straico->chatCompletionV2(
model: 'openai/gpt-4',
messages: [
['role' => 'user', 'content' => 'Hello!']
],
options: [
'temperature' => 0.7,
'max_tokens' => 150
]
);
echo $response['choices'][0]['message']['content'];
$uploadResult = $straico->uploadFile('/path/to/document.pdf');
$fileId = $uploadResult['file_id'];
echo "File uploaded: $fileId\n";
$result = $straico->imageGeneration(
model: 'openai/dall-e-3',
description: 'A serene landscape with mountains and a lake at sunset',
size: '1024x1024',
variations: 1
);
$imageUrl = $result['data'][0]['url'];
echo "Generated image: $imageUrl\n";
$rag = $straico->createRag(
name: 'My Knowledge Base',
description: 'Documentation and guides',
files: [
[
'name' => 'document.pdf',
'content' => base64_encode(file_get_contents('document.pdf'))
]
]
);
$ragId = $rag['id'];
$rags = $straico->rags();
foreach ($rags as $rag) {
echo $rag['name'] . " (ID: " . $rag['id'] . ")\n";
}
$rag = $straico->rag('rag-id-here');
print_r($rag);
$result = $straico->updateRag(
ragId: 'rag-id-here',
filePaths: [
'/path/to/new-document.pdf',
'/path/to/another-file.txt'
]
);
echo "RAG updated with " . $result['total_words'] . " words\n";
// Basic query
$response = $straico->ragPromptCompletion(
ragId: 'rag-id-here',
model: 'openai/gpt-4',
prompt: 'What does the documentation say about authentication?'
);
// With optional retriever parameters
$response = $straico->ragPromptCompletion(
ragId: 'rag-id-here',
model: 'openai/gpt-4',
prompt: 'What does the documentation say about authentication?',
options: [
'search_type' => 'similarity', // similarity, mmr, or similarity_score_threshold
'k' => 4, // number of documents to return
'fetch_k' => 20, // documents to pass to MMR algorithm
'lambda_mult' => 0.5, // diversity (0=max diversity, 1=min diversity)
'score_threshold' => 0.7 // minimum relevance threshold
]
);
echo $response['data']['answer'];
// View references
foreach ($response['data']['references'] as $ref) {
echo "Page {$ref['page']}: {$ref['page_content']}\n";
}
$result = $straico->deleteRag('rag-id-here');
$agent = $straico->createAgent([
'name' => 'Research Assistant',
'description' => 'Helps with research tasks',
'model' => 'openai/gpt-4',
// Additional configuration...
]);
$agentId = $agent['_id'];
// Basic query
$response = $straico->agentPromptCompletion(
agentId: 'agent-id-here',
prompt: 'Research the latest developments in AI'
);
// With optional parameters
$response = $straico->agentPromptCompletion(
agentId: 'agent-id-here',
prompt: 'Research the latest developments in AI',
options: [
'search_type' => 'mmr',
'k' => 5
]
);
echo $response['data']['answer'];
use Straico\Exceptions\StraicoException;
try {
$response = $straico->promptCompletion('openai/gpt-4', 'Hello');
} catch (StraicoException $e) {
echo "Error: " . $e->getMessage() . "\n";
echo "Code: " . $e->getCode() . "\n";
}
bash
composer bash
export STRAICO_API_KEY="your-api-key-here"