我的日常开发记录日志
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)

我的日常开发记录日志

never give up
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)
  • laravel

  • thinkphp

  • swoole

  • workman

  • php

    • php
    • phpstorm常用快捷键
    • new关键字和依赖注入对比
    • windows上php多版本存在
    • 封装一个wkhtmltopdf的扩展类
    • 请求第三方接口绕过白名单操作
    • 封装一个基础的openai请求类
    • 封装一个基础的请求第三方类
    • 支付宝支付使用证书方式进行支付的类
    • cookie通俗讲解
    • php-fpm服务的重启
    • guzzle中cookie使用
  • gpt

  • java

  • 后端
  • php
窝窝侠
2024-10-18

封装一个基础的openai请求类

<?php
/**
 * Created by PhpStorm
 * @Author: wangwin
 * @Date: 2024/9/5
 * @Time: 15:40
 * @Version: 1.0
 */

namespace App\Service\Gpt;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class ChatService
{
    protected $client;
    protected $apiKey;
    protected $baseUri;
    protected $model;

    /**
     * Constructor to initialize the API service.
     *
     * @param string $apiKey
     * @param string $baseUri
     */
    public function __construct(string $apiKey, string $baseUri)
    {
        $this->apiKey = $apiKey;
        $this->baseUri = $baseUri;
        $this->client = new Client([
            'base_uri' => $this->baseUri,
        ]);
    }

    /**
     * Perform the chat completion request.
     *
     * @param array $data
     * @return array
     * @throws \Exception
     */
    public function chat(array $data)
    {
        try {
            if ($this->model) {
                $data['model'] = $this->model;
            }
            $response = $this->client->post('chat/completions', [
                'headers' => [
                    'Authorization' => 'Bearer '.$this->apiKey,
                    'Content-Type'  => 'application/json',
                ],
                'json'    => $data,
            ]);

            return json_decode($response->getBody()->getContents(), true);
        } catch (RequestException $e) {
            throw new \Exception('Request failed: '.$e->getMessage());
        }
    }

    /**
     * Set the API key dynamically.
     *
     * @param string $apiKey
     * @return $this
     */
    public function setApiKey(string $apiKey): self
    {
        $this->apiKey = $apiKey;

        return $this;
    }


    /**
     * Set a new base URI for the API client.
     *
     * @param string $baseUri
     * @return $this
     */
    public function setBaseUri(string $baseUri): self
    {
        $this->baseUri = $baseUri;
        $this->client = new Client([
            'base_uri' => $this->baseUri,
        ]);

        return $this;
    }


    /**
     * Send a post request using cURL as a fallback.
     *
     * @param string $url
     * @param array $postData
     * @param array $headers
     * @return mixed
     * @throws \Exception
     */
    public function sendPostRequest($url, $postData, $headers = [])
    {
        $ch = curl_init();
        $jsonData = json_encode($postData);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error = curl_error($ch);
            curl_close($ch);
            throw new \Exception("cURL Error: $error");
        }

        curl_close($ch);

        return json_decode($response, true);
    }

    /*php artisan make:command ProcessTransaction
*
     * 实例化
     * @param string $apiKey
     * @param string $baseUri
     * @return static
     */
    public static function create(string $apiKey = '7dcae406-5eea-4ab8-b6aa-191d50288c57', string $baseUri = 'https://ark.cn-beijing.volces.com/api/v3/'): self
    {
        return new self($apiKey, $baseUri);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
在线编辑 (opens new window)
上次更新: 2025/02/25, 18:30:54
请求第三方接口绕过白名单操作
封装一个基础的请求第三方类

← 请求第三方接口绕过白名单操作 封装一个基础的请求第三方类→

最近更新
01
showprocess用法
04-29
02
vue3中尖括号和冒号的使用细则
04-29
03
sd使用
02-22
更多文章>
🖥️

© 2025窝窝侠 💌 豫ICP备20005263号-2 🛀 Theme by 💝 Vdoing && 小胖墩er

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×