PHP code example of srcker / openai

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

    

srcker / openai example snippets


       $chatCompletionRequest = new ChatCompletionRequest();

        $chatCompletionRequest
            ->setModel("ep-20241220124901-pc5gs")
            ->addMessage(new ChatMessage(Role::USER, '介绍下自己'));

        $result = $this->factory
            ->withApiKey("****-6904-***-ba03-**")
            ->withBaseUrl("https://ark.cn-beijing.volces.com/api/v3/chat/completions")
            ->withHttpHeader("Content-Type", "application/json")
            ->withChatCompletionRequest($chatCompletionRequest)
            ->make();

        print_r($result);



        $chatCompletionRequest = new ChatCompletionRequest();

        $chatCompletionRequest
            ->setModel("ep-20241220124901-pc5gs")
            ->addMessage(new ChatMessage(Role::USER, '介绍下自己'));

        $this->factory
            ->withApiKey("****-333-****-ba03-***")
            ->withBaseUrl("https://ark.cn-beijing.volces.com/api/v3/chat/completions")
            ->withHttpHeader("Content-Type", "application/json")
            ->withChatCompletionRequest($chatCompletionRequest)
            ->withStreamHandler(function ($chunk) {
                echo $chunk;
            })
            ->makeStreamHandler();