Skip to content

Commit 074b770

Browse files
committed
added support for groq
1 parent 8be48eb commit 074b770

File tree

9 files changed

+231
-0
lines changed

9 files changed

+231
-0
lines changed

Model/Groq/ChatModel.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\aichat\Model\Groq;
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['groq_apikey'])) {
16+
throw new \Exception('Groq API key not configured');
17+
}
18+
19+
$this->http->headers['Authorization'] = 'Bearer '.$config['groq_apikey'];
20+
}
21+
22+
/** @inheritdoc */
23+
public function getAnswer(array $messages): string
24+
{
25+
$data = [
26+
'messages' => $messages,
27+
'model' => $this->getModelName(),
28+
'max_tokens' => null,
29+
'stream' => false,
30+
'n' => 1, // number of completions
31+
'temperature' => 0.0,
32+
];
33+
$response = $this->request('chat/completions', $data);
34+
return $response['choices'][0]['message']['content'];
35+
}
36+
37+
/**
38+
* Send a request to the API
39+
*
40+
* @param string $endpoint
41+
* @param array $data Payload to send
42+
* @return array API response
43+
* @throws \Exception
44+
*/
45+
protected function request($endpoint, $data)
46+
{
47+
$url = 'https://api.groq.com/openai/v1/' . $endpoint;
48+
return $this->sendAPIRequest('POST', $url, $data);
49+
}
50+
51+
/** @inheritdoc */
52+
protected function parseAPIResponse($response)
53+
{
54+
if (isset($response['usage'])) {
55+
$this->inputTokensUsed += $response['usage']['prompt_tokens'];
56+
$this->outputTokensUsed += $response['usage']['completion_tokens'] ?? 0;
57+
}
58+
59+
if (isset($response['error'])) {
60+
throw new \Exception('Groq API error: ' . $response['error']['message']);
61+
}
62+
63+
return $response;
64+
}
65+
}

Model/Groq/models.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"chat": {
3+
"llama3-8b-8192": {
4+
"description": "LLaMA3 8b",
5+
"inputTokens": 8192,
6+
"inputTokenPrice": 0,
7+
"outputTokens": 8192,
8+
"outputTokenPrice": 0
9+
},
10+
"llama3-70b-8192": {
11+
"description": "LLaMA3 70b",
12+
"inputTokens": 8192,
13+
"inputTokenPrice": 0,
14+
"outputTokens": 8192,
15+
"outputTokenPrice": 0
16+
},
17+
"llama2-70b-4096": {
18+
"description": "LLaMA2 70b",
19+
"inputTokens": 4096,
20+
"inputTokenPrice": 0,
21+
"outputTokens": 4096,
22+
"outputTokenPrice": 0
23+
},
24+
"mixtral-8x7b-32768": {
25+
"description": "Mixtral 8x7b",
26+
"inputTokens": 32768,
27+
"inputTokenPrice": 0,
28+
"outputTokens": 32768,
29+
"outputTokenPrice": 0
30+
},
31+
"gemma-7b-it": {
32+
"description": "Gemma is a family of lightweight, state-of-the-art open models from Google, built from the same research and technology used to create the Gemini models. They are text-to-text, decoder-only large language models, available in English, with open weights, pre-trained variants, and instruction-tuned variants. Gemma models are well-suited for a variety of text generation tasks, including question answering, summarization, and reasoning. Their relatively small size makes it possible to deploy them in environments with limited resources such as a laptop, desktop or your own cloud infrastructure, democratizing access to state of the art AI models and helping foster innovation for everyone.",
33+
"inputTokens": 8192,
34+
"inputTokenPrice": 0,
35+
"outputTokens": 8182,
36+
"outputTokenPrice": 0
37+
}
38+
}
39+
}

Model/Ollama/AbstractOllama.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\aichat\Model\Ollama;
4+
5+
use dokuwiki\plugin\aichat\Model\AbstractModel;
6+
7+
/**
8+
* Abstract OpenAI Model
9+
*
10+
* This class provides a basic interface to the OpenAI API
11+
*/
12+
abstract class AbstractOllama extends AbstractModel
13+
{
14+
protected $apiurl = 'http://localhost:11434/api/';
15+
16+
/** @inheritdoc */
17+
public function __construct(string $name, array $config)
18+
{
19+
parent::__construct($name, $config);
20+
$this->apiurl = rtrim($config['ollama_baseurl'] ?? '', '/');
21+
if ($this->apiurl === '') {
22+
throw new \Exception('Ollama base URL not configured');
23+
}
24+
}
25+
26+
/**
27+
* Send a request to the OpenAI API
28+
*
29+
* @param string $endpoint
30+
* @param array $data Payload to send
31+
* @return array API response
32+
* @throws \Exception
33+
*/
34+
protected function request($endpoint, $data)
35+
{
36+
$url = $this->apiurl . '/' . ltrim($endpoint, '/');
37+
return $this->sendAPIRequest('POST', $url, $data);
38+
}
39+
40+
/** @inheritdoc */
41+
protected function parseAPIResponse($response)
42+
{
43+
if (isset($response['eval_count'])) {
44+
$this->inputTokensUsed += $response['eval_count'];
45+
}
46+
47+
if (isset($response['error'])) {
48+
$error = is_array($response['error']) ? $response['error']['message'] : $response['error'];
49+
throw new \Exception('Ollama API error: ' . $error);
50+
}
51+
52+
return $response;
53+
}
54+
55+
}

Model/Ollama/ChatModel.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\aichat\Model\Ollama;
4+
5+
use dokuwiki\plugin\aichat\Model\ChatInterface;
6+
7+
class ChatModel extends AbstractOllama implements ChatInterface
8+
{
9+
/** @inheritdoc */
10+
public function getAnswer(array $messages): string
11+
{
12+
$data = [
13+
'messages' => $messages,
14+
'model' => $this->getModelName(),
15+
'stream' => false,
16+
];
17+
$response = $this->request('chat', $data);
18+
return $response['message']['content'];
19+
}
20+
}

Model/Ollama/EmbeddingModel.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\aichat\Model\Ollama;
4+
5+
use dokuwiki\plugin\aichat\Model\EmbeddingInterface;
6+
7+
class EmbeddingModel extends AbstractOllama implements EmbeddingInterface
8+
{
9+
/** @inheritdoc */
10+
public function getEmbedding($text): array
11+
{
12+
$data = [
13+
'model' => $this->getModelName(),
14+
'prompt' => $text,
15+
];
16+
$response = $this->request('embeddings', $data);
17+
18+
return $response['embedding'];
19+
}
20+
}

Model/Ollama/models.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"chat": {
3+
"llama2": {
4+
"description": "Llama 2 is released by Meta Platforms, Inc. This model is trained on 2 trillion tokens, and by default supports a context length of 4096. Llama 2 Chat models are fine-tuned on over 1 million human annotations, and are made for chat.",
5+
"inputTokens": 4096,
6+
"inputTokenPrice": 0,
7+
"outputTokens": 4096,
8+
"outputTokenPrice": 0
9+
},
10+
11+
"gemma": {
12+
"description": "Llama 2 is released by Meta Platforms, Inc. This model is trained on 2 trillion tokens, and by default supports a context length of 4096. Llama 2 Chat models are fine-tuned on over 1 million human annotations, and are made for chat.",
13+
"inputTokens": 4096,
14+
"inputTokenPrice": 0,
15+
"outputTokens": 4096,
16+
"outputTokenPrice": 0
17+
}
18+
},
19+
"embedding": {
20+
"text-embedding-ada-002": {
21+
"description": "Most capable 2nd generation embedding model",
22+
"inputTokens": 8192,
23+
"inputTokenPrice": 0.10,
24+
"dimensions": 1536
25+
}
26+
}
27+
}

conf/default.php

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

2323
$conf['reka_apikey'] = '';
2424

25+
$conf['groq_apikey'] = '';
26+
2527
$conf['pinecone_apikey'] = '';
2628
$conf['pinecone_baseurl'] = '';
2729

conf/metadata.php

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

3030
$meta['reka_apikey'] = array('password');
3131

32+
$meta['groq_apikey'] = array('password');
33+
3234
$meta['pinecone_apikey'] = array('password');
3335
$meta['pinecone_baseurl'] = array('string');
3436

lang/en/settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
$lang['mistral_apikey'] = '🧠 <b>Mistral</b> API key';
1919
$lang['voyageai_apikey'] = '🧠 <b>Voyage AI</b> API key';
2020
$lang['reka_apikey'] = '🧠 <b>Reka</b> API key';
21+
$lang['groq_apikey'] = '🧠 <b>Groq</b> API key';
2122

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

0 commit comments

Comments
 (0)