Skip to content

Commit ae74151

Browse files
committed
zai image provider
1 parent bdaed00 commit ae74151

2 files changed

Lines changed: 128 additions & 3 deletions

File tree

‎src/Providers/ZAI/Audio/ZAITranscription.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ protected function addFile(array &$body, AudioContent $audio): void
159159

160160
public function structured(array|Message $messages, string $class, array $response_schema): Message
161161
{
162-
throw new ProviderException('Structured output is not supported by OpenAI Text to Speech.');
162+
throw new ProviderException('Structured output is not supported for transcription.');
163163
}
164164

165165
public function messageMapper(): MessageMapperInterface
166166
{
167-
throw new ProviderException('Messages are not supported by OpenAI Text to Speech.');
167+
throw new ProviderException('Messages are not supported for transcription.');
168168
}
169169

170170
public function toolPayloadMapper(): ToolMapperInterface
171171
{
172-
throw new ProviderException('Tools are not supported by OpenAI Text to Speech.');
172+
throw new ProviderException('Tools are not supported for transcription.');
173173
}
174174

175175
public function setTools(array $tools): AIProviderInterface
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NeuronAI\Providers\ZAI\Image;
6+
7+
use Generator;
8+
use NeuronAI\Chat\Enums\SourceType;
9+
use NeuronAI\Chat\Messages\AssistantMessage;
10+
use NeuronAI\Chat\Messages\ContentBlocks\ImageContent;
11+
use NeuronAI\Chat\Messages\ContentBlocks\TextContent;
12+
use NeuronAI\Chat\Messages\Message;
13+
use NeuronAI\Chat\Messages\Usage;
14+
use NeuronAI\Exceptions\HttpException;
15+
use NeuronAI\Exceptions\ProviderException;
16+
use NeuronAI\HttpClient\GuzzleHttpClient;
17+
use NeuronAI\HttpClient\HasHttpClient;
18+
use NeuronAI\HttpClient\HttpClientInterface;
19+
use NeuronAI\HttpClient\HttpRequest;
20+
use NeuronAI\Providers\AIProviderInterface;
21+
use NeuronAI\Providers\MessageMapperInterface;
22+
use NeuronAI\Providers\ToolMapperInterface;
23+
24+
use function end;
25+
26+
class ZAIImage implements AIProviderInterface
27+
{
28+
use HasHttpClient;
29+
30+
/**
31+
* The main URL of the provider API.
32+
*/
33+
protected string $baseUri = 'https://api.z.ai/api/paas/v4';
34+
35+
/**
36+
* System instructions.
37+
*/
38+
protected ?string $system = null;
39+
40+
public function __construct(
41+
protected string $key,
42+
protected string $model,
43+
protected array $parameters = [],
44+
?HttpClientInterface $httpClient = null
45+
) {
46+
$this->httpClient = ($httpClient ?? new GuzzleHttpClient())
47+
->withBaseUri($this->baseUri)
48+
->withHeaders([
49+
'Accept' => 'application/json',
50+
'Content-Type' => 'application/json',
51+
'Authorization' => 'Bearer ' . $this->key,
52+
]);
53+
}
54+
55+
public function systemPrompt(?string $prompt): AIProviderInterface
56+
{
57+
$this->system = $prompt;
58+
return $this;
59+
}
60+
61+
/**
62+
* @throws HttpException
63+
*/
64+
public function chat(Message ...$messages): Message
65+
{
66+
$message = end($messages);
67+
68+
if ($this->system ?? false) {
69+
$message->addContent(new TextContent($this->system));
70+
}
71+
72+
$body = [
73+
'model' => $this->model,
74+
'prompt' => $message->getContent(),
75+
...$this->parameters
76+
];
77+
78+
$response = $this->httpClient->request(
79+
HttpRequest::post(
80+
uri: 'images/generations',
81+
body: $body
82+
)
83+
)->json();
84+
85+
$result = new AssistantMessage(
86+
new ImageContent($response['data'][0]['url'], SourceType::URL)
87+
);
88+
89+
if ($response['usage']) {
90+
$result->setUsage(
91+
new Usage(
92+
$response['usage']['prompt_tokens'],
93+
$response['usage']['completion_tokens']
94+
)
95+
);
96+
}
97+
98+
return $result;
99+
}
100+
101+
public function stream(Message ...$messages): Generator
102+
{
103+
throw new ProviderException('Streaming not supported for image generation. Use chat() instead.');
104+
}
105+
106+
public function structured(array|Message $messages, string $class, array $response_schema): Message
107+
{
108+
throw new ProviderException('Structured output not supported for image generation. Use chat() instead.');
109+
}
110+
111+
public function messageMapper(): MessageMapperInterface
112+
{
113+
throw new ProviderException('Messages are not supported for image generation.');
114+
}
115+
116+
public function toolPayloadMapper(): ToolMapperInterface
117+
{
118+
throw new ProviderException('Tools are not supported for image generation.');
119+
}
120+
121+
public function setTools(array $tools): AIProviderInterface
122+
{
123+
return $this;
124+
}
125+
}

0 commit comments

Comments
 (0)