xref: /plugin/aichat/Model/OpenAI/AbstractOpenAIModel.php (revision d02b793578c15c86b482725d129996df393f1890)
1294a9eafSAndreas Gohr<?php
2294a9eafSAndreas Gohr
3294a9eafSAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\OpenAI;
4294a9eafSAndreas Gohr
5294a9eafSAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
6294a9eafSAndreas Gohr
7294a9eafSAndreas Gohr/**
8294a9eafSAndreas Gohr * Abstract OpenAI Model
9294a9eafSAndreas Gohr *
10294a9eafSAndreas Gohr * This class provides a basic interface to the OpenAI API
11294a9eafSAndreas Gohr */
12294a9eafSAndreas Gohrabstract class AbstractOpenAIModel extends AbstractModel
13294a9eafSAndreas Gohr{
14294a9eafSAndreas Gohr    /** @inheritdoc */
15294a9eafSAndreas Gohr    public function __construct($config)
16294a9eafSAndreas Gohr    {
17294a9eafSAndreas Gohr        parent::__construct($config);
18294a9eafSAndreas Gohr
19*d02b7935SAndreas Gohr        $openAIKey = $config['openaikey'] ?? '';
20*d02b7935SAndreas Gohr        $openAIOrg = $config['openaiorg'] ?? '';
21294a9eafSAndreas Gohr
22294a9eafSAndreas Gohr        $this->http->headers['Authorization'] = 'Bearer ' . $openAIKey;
23294a9eafSAndreas Gohr        if ($openAIOrg) {
24294a9eafSAndreas Gohr            $this->http->headers['OpenAI-Organization'] = $openAIOrg;
25294a9eafSAndreas Gohr        }
26294a9eafSAndreas Gohr    }
27294a9eafSAndreas Gohr
28294a9eafSAndreas Gohr    /**
29294a9eafSAndreas Gohr     * Send a request to the OpenAI API
30294a9eafSAndreas Gohr     *
31294a9eafSAndreas Gohr     * @param string $endpoint
32294a9eafSAndreas Gohr     * @param array $data Payload to send
33294a9eafSAndreas Gohr     * @return array API response
34294a9eafSAndreas Gohr     * @throws \Exception
35294a9eafSAndreas Gohr     */
36294a9eafSAndreas Gohr    protected function request($endpoint, $data)
37294a9eafSAndreas Gohr    {
38294a9eafSAndreas Gohr        $url = 'https://api.openai.com/v1/' . $endpoint;
39294a9eafSAndreas Gohr        return $this->sendAPIRequest('POST', $url, $data);
40294a9eafSAndreas Gohr    }
41294a9eafSAndreas Gohr
42294a9eafSAndreas Gohr    /** @inheritdoc */
43294a9eafSAndreas Gohr    protected function parseAPIResponse($response)
44294a9eafSAndreas Gohr    {
45294a9eafSAndreas Gohr        if (isset($response['usage'])) {
46294a9eafSAndreas Gohr            $this->tokensUsed += $response['usage']['total_tokens'];
47294a9eafSAndreas Gohr        }
48294a9eafSAndreas Gohr
49294a9eafSAndreas Gohr        if (isset($response['error'])) {
50294a9eafSAndreas Gohr            throw new \Exception('OpenAI API error: ' . $response['error']['message']);
51294a9eafSAndreas Gohr        }
52294a9eafSAndreas Gohr
53294a9eafSAndreas Gohr        return $response;
54294a9eafSAndreas Gohr    }
55294a9eafSAndreas Gohr
56294a9eafSAndreas Gohr    /**
57294a9eafSAndreas Gohr     * @internal for checking available models
58294a9eafSAndreas Gohr     */
59294a9eafSAndreas Gohr    public function listUpstreamModels()
60294a9eafSAndreas Gohr    {
61294a9eafSAndreas Gohr        $url = 'https://api.openai.com/v1/models';
62294a9eafSAndreas Gohr        return $this->http->get($url);
63294a9eafSAndreas Gohr    }
64294a9eafSAndreas Gohr}
65