Skip to content

Commit eaa2c59

Browse files
committed
Added support for reka.ai
1 parent 200983a commit eaa2c59

File tree

6 files changed

+114
-1
lines changed

6 files changed

+114
-1
lines changed

Model/Anthropic/ChatModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getAnswer(array $messages): string
5151
}
5252

5353
/**
54-
* Send a request to the OpenAI API
54+
* Send a request to the API
5555
*
5656
* @param string $endpoint
5757
* @param array $data Payload to send

Model/Reka/ChatModel.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\aichat\Model\Reka;
4+
5+
use dokuwiki\plugin\aichat\Model\AbstractModel;
6+
use dokuwiki\plugin\aichat\Model\ChatInterface;
7+
8+
class ChatModel extends AbstractModel implements ChatInterface
9+
{
10+
/** @inheritdoc */
11+
public function __construct(string $name, array $config)
12+
{
13+
parent::__construct($name, $config);
14+
15+
if (empty($config['reka_apikey'])) {
16+
throw new \Exception('Reka API key not configured');
17+
}
18+
19+
$this->http->headers['x-api-key'] = $config['reka_apikey'];
20+
}
21+
22+
/** @inheritdoc */
23+
public function getAnswer(array $messages): string
24+
{
25+
$chat = [];
26+
foreach ($messages as $message) {
27+
if ($message['role'] === 'user') {
28+
$chat[] = [
29+
'type' => 'human',
30+
'text' => $message['content'],
31+
];
32+
} elseif ($message['role'] === 'assistant') {
33+
$chat[] = [
34+
'type' => 'model',
35+
'text' => $message['content'],
36+
];
37+
}
38+
// system messages are not supported
39+
}
40+
41+
$data = [
42+
'conversation_history' => $chat,
43+
'model_name' => $this->getModelName(),
44+
'temperature' => 0.0,
45+
];
46+
47+
$response = $this->request('chat', $data);
48+
return $response['text'];
49+
}
50+
51+
/**
52+
* Send a request to the API
53+
*
54+
* @param string $endpoint
55+
* @param array $data Payload to send
56+
* @return array API response
57+
* @throws \Exception
58+
*/
59+
protected function request($endpoint, $data)
60+
{
61+
$url = 'https://api.reka.ai/' . $endpoint;
62+
return $this->sendAPIRequest('POST', $url, $data);
63+
}
64+
65+
/** @inheritdoc */
66+
protected function parseAPIResponse($response)
67+
{
68+
if(((int) $this->http->status) !== 200) {
69+
if(isset($response['detail'])) {
70+
throw new \Exception('Reka API error: ' . $response['detail']);
71+
} else {
72+
throw new \Exception('Reka API error: ' . $this->http->status . ' ' . $this->http->error);
73+
}
74+
}
75+
76+
if (isset($response['metadata'])) {
77+
$this->inputTokensUsed += $response['metadata']['input_tokens'];
78+
$this->outputTokensUsed += $response['metadata']['generated_tokens'];
79+
}
80+
81+
return $response;
82+
}
83+
}

Model/Reka/models.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"chat": {
3+
"reka-flash": {
4+
"description": "Fast and cost-efficient model for most tasks",
5+
"inputTokens": 128000,
6+
"inputTokenPrice": 0.8,
7+
"outputTokens": 8192,
8+
"outputTokenPrice": 2.0
9+
},
10+
"reka-edge": {
11+
"description": "Lightweight model for local (i.e., on-hardware) or latency sensitive applications",
12+
"inputTokens": 64000,
13+
"inputTokenPrice": 0.4,
14+
"outputTokens": 8192,
15+
"outputTokenPrice": 1.0
16+
},
17+
"reka-core": {
18+
"description": "Superior capabilities for complex tasks",
19+
"inputTokens": 128000,
20+
"inputTokenPrice": 10.0,
21+
"outputTokens": 8192,
22+
"outputTokenPrice": 25.0
23+
}
24+
}
25+
}

conf/default.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
$conf['voyageai_apikey'] = '';
2222

23+
$conf['reka_apikey'] = '';
24+
2325
$conf['pinecone_apikey'] = '';
2426
$conf['pinecone_baseurl'] = '';
2527

conf/metadata.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
$meta['voyageai_apikey'] = array('password');
2929

30+
$meta['reka_apikey'] = array('password');
31+
3032
$meta['pinecone_apikey'] = array('password');
3133
$meta['pinecone_baseurl'] = array('string');
3234

lang/en/settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
$lang['anthropic_apikey'] = '🧠 <b>Anthropic</b> API key';
1818
$lang['mistral_apikey'] = '🧠 <b>Mistral</b> API key';
1919
$lang['voyageai_apikey'] = '🧠 <b>Voyage AI</b> API key';
20+
$lang['reka_apikey'] = '🧠 <b>Reka</b> API key';
2021

2122
$lang['pinecone_apikey'] = '📥 <b>Pinecone</b> API key';
2223
$lang['pinecone_baseurl'] = '📥 <b>Pinecone</b> base URL';

0 commit comments

Comments
 (0)