ข้ามไปที่เนื้อหาหลัก

สร้าง Open AI ใน Laravel อย่างไร #อ่านบทควทม


 1. สร้างตัวอย่าง chatbot จาก Telegram และ botman

2. สมัครใช้งาน bot 

3. ระบบหลังบ้าน

4. ทดสอบและใช้งาน

  1. ติดตั้ง composer require openai-php/laravel
  2. แก้ไขและเพิ่ท vendor php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
  3. ใส่ open ai keyในไฟล์ config/openai.php OPENAI_API_KEY=sk-...
  4. เรียกใช้งาน 
 use OpenAI\Laravel\Facades\OpenAI;
$result = OpenAI::completions()->create([

        'model' => 'text-davinci-003',
          'prompt' => 'PHP is',
      ]);
        echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.

        ส่วนของ Client

        ติดตั้ง
        composer require openai-php/client
        composer require guzzlehttp/guzzle

        เรียกใช่
        $yourApiKey = getenv('YOUR_API_KEY');
        $client = OpenAI::client($yourApiKey);

        $result = $client->completions()->create([
            'model' => 'text-davinci-003',
            'prompt' => 'PHP is',
        ]);

        echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.

        ส่วนเรียกใช้ client


        $yourApiKey = getenv('YOUR_API_KEY');

        $client = OpenAI::factory()
            ->withApiKey($yourApiKey)
            ->withOrganization('your-organization') // default: null
            ->withBaseUri('openai.example.com/v1') // default: api.openai.com/v1
            ->withHttpClient($client = new \GuzzleHttp\Client([])) // default: HTTP client found using PSR-18 HTTP Client Discovery
            ->withHttpHeader('X-My-Header', 'foo')
            ->withQueryParam('my-param', 'bar')
            ->withStreamHandler(fn (RequestInterface $request): ResponseInterface => $client->send($request, [
                'stream' => true // Allows to provide a custom stream handler for the http client.
            ]))
            ->make();

        เรียกใช้ แสดงรายการ Model


        $response = $client->models()->list();

        $response->object; // 'list'

        foreach ($response->data as $result) {
            $result->id; // 'text-davinci-003'
            $result->object; // 'model'
            // ...
        }

        $response->toArray(); // ['object' => 'list', 'data' => [...]]

        ค้นคืน retrieve
        $response = $client->models()->retrieve('text-davinci-003');

        $response->id; // 'text-davinci-003'
        $response->object; // 'model'
        $response->created; // 1642018370
        $response->ownedBy; // 'openai'
        $response->root; // 'text-davinci-003'
        $response->parent; // null

        foreach ($response->permission as $result) {
            $result->id; // 'modelperm-7E53j9OtnMZggjqlwMxW4QG7' 
            $result->object; // 'model_permission' 
            $result->created; // 1664307523 
            $result->allowCreateEngine; // false 
            $result->allowSampling; // true 
            $result->allowLogprobs; // true 
            $result->allowSearchIndices; // false 
            $result->allowView; // true 
            $result->allowFineTuning; // false 
            $result->organization; // '*' 
            $result->group; // null 
            $result->isBlocking; // false 
        }

        $response->toArray(); // ['id' => 'text-davinci-003', ...]

        ลบ model
        $response = $client->models()->delete('curie:ft-acmeco-2021-03-03-21-44-20');

        $response->id; // 'curie:ft-acmeco-2021-03-03-21-44-20'
        $response->object; // 'model'
        $response->deleted; // true

        $response->toArray(); // ['id' => 'curie:ft-acmeco-2021-03-03-21-44-20', ...]

        ความคิดเห็น

        โพสต์ยอดนิยมจากบล็อกนี้

        Interactive เทคโนโลยีเชิงโต้ตอบเปลี่ยนโลกเข้าสู่ยุค Metaverse

        เทคโนโลยี Interactive คืออะไร คำนิยามของ เทคโนโลยี Interactive ที่สร้างและพัฒนาขึ้นสำหรับโปรแกรมหรือแอพลิเคชั่นที่เป็น Real-Time เรียกง่ายๆว่า Real-Time Programming (RTP) โดยเน้นไปยังผู้ใช้หรือมนุษย์นั้นเอง จะประกอบไปด้วย 2 องค์ประกอบ คือ ส่วนแรก เทคโนโลยี Interactive เข้าทำการเปลี่ยนแปลรูปร่าง ขนาด และรูปแบบ ซึ่งมาจาก web service อุปกรณ์ sensor ผ่านคอมพิวเตอร์ และมือถือ เป็นผสมผสานระหว่างระบบดิจิทัลและแอนนาล๊อกเข้าด้วยกัน ผ่านปุ่ม สไลด์เดอร์ หรือสวิทซ์ เพื่อการควบคุมในส่วนควบคุมทั้งหมด ที่เรียกว่า Control panel  ส่วนที่ 2  แอพลิเคชั่นที่ทำงานแบบ Real Time Application ถูกออกแบบในแนวคิดหลักของเทคโนโลยี Real Time  อ้างอิง https://interactiveimmersive.io/blog/beginner/02-interactive-technology/ เทคโนโลยีเชิงโต้ตอบที่เน้นการสร้างต้นแบบที่เร็วขึ้น มีประโยชน์อย่างมากในแอปพลิเคชันและประสบการณ์การสร้างต้นแบบ  ประเภทของเทคโนโลยี Interactive  มี 6 ประเภท 1. IoT เป็นแนวคิดจะเปลี่ยนบริการทุกบริการให้อยู่บนเครือข่ายอินเทอร์เน็ต และสามารถเชื่อมต่อเข้ากับแอพลิเคชั่นทุกแอ...

        Routing Protocol คืออะไร มีประโยชน์อย่างไร ทำหน้าที่อะไร

         Routing Protocol คืออะไร มีประโยชน์อย่างไร ทำหน้าที่อะไร         ในกระบวนการเชื่อมต่อระบบเครือข่ายจะมีกระบวนการหรือกฏหนึ่งที่ทำหน้าที่ในการเชื่อมต่อระบบเครือข่ายตั้งแต่ 2 ระบบขึ้นไป เพื่อให้สามารถติดต่อสื่อสารข้อมูลระหว่างกันได้ กระบวนการนี้เรียกว่า Routing Protocol            Routing Protocol  คือ Protocol มีหน้าที่สำหรับการแลกเปลี่ยนข้อมูลเส้นทางสำหรับอุปกรณ์ค้นหาเส้นทาง หรือ Router ข้อมูลที่ส่งไปเป็น routing information ระหว่างอุปกรณ์เครือข่ายต่างๆที่ทำงานในระดับ Network Layer (Layer 3) เพื่อส่งข้อมูลแพ็กเก็จจาก ผ่านตัว Router ไปยัง IP ปลายทางได้ หรือผ่าน Router ตัวไหน โดยตรวจสอบจากได้จาก routing table หรือตารางเส้นทางนั้นเอง  ประเภทของ Routing Protocol   Interior gateway Protocol  มี 2 Type คือ   Type 1  Link State Routing Protocols Open Shortest Path Frist ( OSPF) Intermediate System to Intermediate System (IS-IS) Type 2 Distance-Vector Routing Protocols Routing Information Pro...