PHP code example of edgaras / azurellm

1. Go to this page and download the library: Download edgaras/azurellm 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/ */

    

edgaras / azurellm example snippets




use Edgaras\AzureLLM\LLM;

$config = new LLM([
    'apiKey' => '<YOUR-API-KEY>',
    'endpoint' => 'https://<DEPLOYMENT>.openai.azure.com',
    'deployment' => '<MODEL-DEPLOYMENT-ID>',
    'apiVersion' => '<API-VERSION>'
]); 
 


use Edgaras\AzureLLM\LLM;
use Edgaras\AzureLLM\AzureOpenAI;

$config = new LLM([
    'apiKey' => '<YOUR-API-KEY>',
    'endpoint' => 'https://<DEPLOYMENT>.openai.azure.com',
    'deployment' => '<MODEL-DEPLOYMENT-ID>',
    'apiVersion' => '<API-VERSION>'
]); 

$azureLLM = new AzureOpenAI($config);

$inputMessages = [
    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
    ['role' => 'user', 'content' => 'What is the capital of Lithuania?']
];

$options = [
    "temperature" => 0.7,
    "top_p" => 0.95,
    "max_tokens" => 150 
];

$response = $azureLLM->chatCompletions($inputMessages, $options);



use Edgaras\AzureLLM\LLM;
use Edgaras\AzureLLM\AzureOpenAI;

$config = new LLM([
    'apiKey' => '<YOUR-API-KEY>',
    'endpoint' => 'https://<DEPLOYMENT>.openai.azure.com',
    'deployment' => '<MODEL-DEPLOYMENT-ID>',
    'apiVersion' => '<API-VERSION>'
]); 

$azureLLM = new AzureOpenAI($config);

$inputMessages = [
    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
    ['role' => 'user', 'content' => 'Summarize your knowledgebase']
];

$options = [
    "temperature" => 0.7,
    "top_p" => 0.95,
    "max_tokens" => 150 
];

$data_sources = [[
    "type" => "azure_search",
    "parameters" => [
        "filter" => null,
        "endpoint" => 'https://<SEARCH-DEPLOYMENT>.search.windows.net',
        "index_name" => '<SEARCH-INDEX-NAME>',
        "authentication" => [
            "type" => "api_key",
            "key" => '<SEARCH-API-KEY>'
        ],
    ],
]];

$response = $azureLLM->chatCompletions($inputMessages, $options, $data_sources);



use Edgaras\AzureLLM\AISearch\Auth;

$config = new Auth([
    'apiKey' => '<YOUR-API-KEY>',
    'endpoint' => 'https://<YOUR-SEARCH-SERVICE>.search.windows.net',
    'apiVersion' => '2023-07-01-Preview'
]); 


use Edgaras\AzureLLM\AISearch\Index;

$indexService = new Index($config);

// Define Index Fields
$fields = [
    ['name' => 'id', 'type' => 'Edm.String', 'key' => true],
    ['name' => 'content', 'type' => 'Edm.String', 'searchable' => true, 'retrievable' => true]
];

// Create Index
$indexService->createIndex('test-index', $fields);

// List all indexes
$indexes = $indexService->listIndexes();
print_r($indexes);


use Edgaras\AzureLLM\AISearch\Indexer;

$indexerService = new Indexer($config);

// Define Indexer
$indexerConfig = [
    'dataSourceName' => 'test-data-source',
    'targetIndexName' => 'test-index',
    'schedule' => ['interval' => 'P1D']
];

// Create Indexer
$indexerService->createIndexer('test-indexer', $indexerConfig);

// Run Indexer Manually
$indexerService->runIndexer('test-indexer');


use Edgaras\AzureLLM\AISearch\DataSource;

$dataSourceService = new DataSource($config);

// Define Data Source Configuration
$dataSourceConfig = [
    'type' => 'azureblob',
    'credentials' => ['connectionString' => '<YOUR-STORAGE-CONNECTION-STRING>'],
    'container' => ['name' => 'your-container']
];

// Create Data Source
$dataSourceService->createDataSource('test-data-source', $dataSourceConfig);

use Edgaras\AzureLLM\LLM; 
use Edgaras\AzureLLM\Agents\Agent;
use Edgaras\AzureLLM\Agents\Thread;

// Initialize
$config = new LLM([
    'apiKey' => '<API-KEY>',
    'endpoint' => 'https://<DEPLOYMENT-NAME>.openai.azure.com',
    'deployment' => '<MODEL>',
    'apiVersion' => '2024-05-01-preview'
]);

$agent = new Agent($config);
$thread = new Thread($config);

// Create an Agent
$agentResponse = $agent->createAgent("SupportBot", "Assist users with support queries.");
$agentId = $agentResponse['id'];

// Start a conversation thread
$threadResponse = $thread->createThread();
$threadId = $threadResponse['id'];

// Send a message
$thread->addMessageToThread($threadId, "user", "How do I reset my password?");

// Run the AI Assistant on the thread
$thread->runThread($threadId, $agentId);