xref: /plugin/aichat/Model/OpenAI/AbstractOpenAIModel.php (revision dce0dee5ef27bcbbc5570fc278f3e75f426c19c5)
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 */
15*dce0dee5SAndreas Gohr    public function __construct(string $name, array $config)
16294a9eafSAndreas Gohr    {
17*dce0dee5SAndreas Gohr        parent::__construct($name, $config);
18294a9eafSAndreas Gohr
19d02b7935SAndreas Gohr        $openAIKey = $config['openaikey'] ?? '';
20d02b7935SAndreas 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'])) {
4634a1c478SAndreas Gohr            $this->inputTokensUsed += $response['usage']['prompt_tokens'];
4734a1c478SAndreas Gohr            $this->outputTokensUsed += $response['usage']['completion_tokens'] ?? 0;
48294a9eafSAndreas Gohr        }
49294a9eafSAndreas Gohr
50294a9eafSAndreas Gohr        if (isset($response['error'])) {
51294a9eafSAndreas Gohr            throw new \Exception('OpenAI API error: ' . $response['error']['message']);
52294a9eafSAndreas Gohr        }
53294a9eafSAndreas Gohr
54294a9eafSAndreas Gohr        return $response;
55294a9eafSAndreas Gohr    }
56294a9eafSAndreas Gohr
57294a9eafSAndreas Gohr    /**
58294a9eafSAndreas Gohr     * @internal for checking available models
59294a9eafSAndreas Gohr     */
60294a9eafSAndreas Gohr    public function listUpstreamModels()
61294a9eafSAndreas Gohr    {
62294a9eafSAndreas Gohr        $url = 'https://api.openai.com/v1/models';
63294a9eafSAndreas Gohr        return $this->http->get($url);
64294a9eafSAndreas Gohr    }
65294a9eafSAndreas Gohr}
66