File size: 5,412 Bytes
ae8c95b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

declare(strict_types=1);

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
 * Class OpenAI
 */
class OpenAI {
	/**
	 * @var string
	 */
	private $api_key;

	/**
	 * @var Client
	 */
	private $client;

	/**
	 * @var LoggerInterface
	 */
	private $log;

	/**
	 * OpenAI constructor.
	 *
	 * @param string $api_key
	 * @param Client|null $client
	 * @param LoggerInterface|null $log
	 */
	public function __construct(string $api_key, Client $client = null, LoggerInterface $log = null) {
		$this->api_key = $api_key;
		$this->client = $client ?: new Client([
			'base_uri' => 'https://api.openai.com/v1/',
			'headers' => [
				'Content-Type' => 'application/json',
				'Authorization' => 'Bearer ' . $this->api_key
			]
		]);
		$this->log = $log ?: new NullLogger();
		if (empty($api_key)) {
			throw new RuntimeException('No valid API key was provided.');
		}
	}

	/**
	 * Generate an image using DALL-E API and save it locally.
	 *
	 * @param string $prompt
	 * @param string $localDirectory
	 * @param string $size
	 * @param int $n
	 * @return array|null
	 */
	public function generateImage(string $prompt, string $localDirectory, string $size = '1024x1024', int $n = 4): ?array {
		$data = [
			'prompt' => $prompt,
			'n' => $n,
			'size' => $size
		];

		try {
			$response = $this->client->post('images/generations', ['json' => $data]);
			$json = json_decode((string) $response->getBody(), true);

			if (json_last_error() === JSON_ERROR_NONE) {
				$savedImages = [];
				foreach ($json['data'] as $imageData) {
					$imageUrl = $imageData['url'];
					$localFilePath = $this->saveImage($imageUrl, $localDirectory);
					$savedImages[] = $localFilePath;
				}

				return $savedImages;
			} else {
				$this->log->error('Failed to decode JSON response', ['json_error' => json_last_error_msg()]);

				return null;
			}
		} catch (RequestException $e) {
			$this->log->error('RequestException encountered', ['message' => $e->getMessage()]);

			return null;
		}
	}

	/**
	 * Save an image from a URL to a local file path.
	 *
	 * @param string $imageUrl
	 * @param string $localDirectory
	 * @return string
	 */
	private function saveImage(string $imageUrl, string $localDirectory): string {
		$imageFileName = basename(parse_url($imageUrl, PHP_URL_PATH));
		$localFilePath = $localDirectory . '/' . $imageFileName;

		$client = new Client();
		$response = $client->get($imageUrl, ['sink' => $localFilePath]);

		if ($response->getStatusCode() == 200) {
			$this->log->info('Image saved successfully', ['path' => $localFilePath]);

			return $localFilePath;
		} else {
			$this->log->error('Failed to save the image', ['status_code' => $response->getStatusCode()]);

			throw new RuntimeException('Failed to save the image: ' . $response->getStatusCode());
		}
	}

	/**
	 * Generate a script using the OpenAI GPT-3.5 Turbo model.
	 *
	 * @param string $role
	 * @param string $prompt
	 * @param int $maxTokens
	 * @param float $temperature
	 * @return string|null
	 */
	public function generateScript(string $role, string $prompt, int $maxTokens = 3600, float $temperature = 1.0): ?string {
		$data = [
			'model' => 'gpt-3.5-turbo',
			'messages' => [
				['role' => 'system', 'content' => $role],
				['role' => 'user', 'content' => $prompt],
			],
			'max_tokens' => $maxTokens,
			'temperature' => $temperature
		];

		try {
			$response = $this->client->post('chat/completions', ['json' => $data]);
			$json = json_decode((string) $response->getBody(), true);

			if (json_last_error() === JSON_ERROR_NONE) {
				$assistantResponse = $json['choices'][0]['message']['content'];

				return $assistantResponse;
			} else {
				$this->log->error('Failed to decode JSON response', ['json_error' => json_last_error_msg()]);

				return null;
			}
		} catch (RequestException $e) {
			$this->log->error('RequestException encountered', ['message' => $e->getMessage()]);

			return null;
		}
	}
    /**
     * Generate image variations using DALL-E API and save them locally.
     *
     * @param string $imagePath
     * @param string $localDirectory
     * @param int $n
     * @param string $size
     * @return array|null
     */
    public function generateImageVariations(string $imagePath, string $localDirectory, int $n = 4, string $size = '1024x1024'): ?array {
        $data = [
            'n' => $n,
            'size' => $size,
            'image' => curl_file_create($imagePath)
        ];

        try {
            $response = $this->client->post('images/variations', ['multipart' => $data]);
            $json = json_decode((string) $response->getBody(), true);

            if (json_last_error() === JSON_ERROR_NONE) {
                $savedImages = [];
                foreach ($json['data'] as $imageData) {
                    $imageUrl = $imageData['url'];
                    $localFilePath = $this->saveImage($imageUrl, $localDirectory);
                    $savedImages[] = $localFilePath;
                }

                return $savedImages;
            } else {
                $this->log->error('Failed to decode JSON response', ['json_error' => json_last_error_msg()]);

                return null;
            }
        } catch (RequestException $e) {
            $this->log->error('RequestException encountered', ['message' => $e->getMessage()]);

            return null;
        }
    }
}