Skip to content

Commit

Permalink
feat: add support for deepseek ai
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng committed Feb 11, 2025
1 parent 9506324 commit f47e1c6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ for Chatbot, RAG (Retrieval-Augmented Generation) based applications with integr

*supported providers:*

| Provider | Link | Features |
|---------------|-------------------------------------------------------|------------------------------|
| OpenAI | [openai.com](https://openai.com) | Completion, Embeddings |
| Mistral | [mistral.ai](https://mistral.ai/) | Completion, Embeddings |
| Google | [ai.google](https://ai.google/) | Completion, Embeddings |
| GitHub | [github.com](https://github.com/marketplace/models) | Completion, Embeddings |
| Anthropic | [anthropic.com](https://www.anthropic.com/) | Completion |
| Voyager.ai | [voyageai.com](https://www.voyageai.com/) | Embeddings, Reranking |
| Ollama | [ollama.com](https://ollama.com/) | Completion, Embeddings |
| Provider | Link | Features |
|------------|-----------------------------------------------------|------------------------|
| OpenAI | [openai.com](https://openai.com) | Completion, Embeddings |
| Mistral | [mistral.ai](https://mistral.ai/) | Completion, Embeddings |
| Google | [ai.google](https://ai.google/) | Completion, Embeddings |
| GitHub | [github.com](https://github.com/marketplace/models) | Completion, Embeddings |
| Anthropic | [anthropic.com](https://www.anthropic.com/) | Completion |
| Voyager.ai | [voyageai.com](https://www.voyageai.com/) | Embeddings, Reranking |
| Ollama | [ollama.com](https://ollama.com/) | Completion, Embeddings |
| Deepseek | [deepseek.com](https://deepseek.com) | completion |



## Installation
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
final class FileNotFoundException extends \RuntimeException
{
public function __construct(string $file, int $code = 0, \Throwable $previous = null)
public function __construct(string $file, int $code = 0, ?\Throwable $previous = null)
{
$message = \sprintf('Failed to open stream: No such file %s', $file);
parent::__construct($message, $code, $previous);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/IOException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
final class IOException extends \RuntimeException
{
public function __construct(string $file, int $code = 0, \Throwable $previous = null)
public function __construct(string $file, int $code = 0, ?\Throwable $previous = null)
{
$message = \sprintf('Unable to read or write the content of %s', $file);
parent::__construct($message, $code, $previous);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/UnsupportedFileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class UnsupportedFileException extends \RuntimeException
public function __construct(
array $extensions = [],
int $code = 0,
\Throwable $previous = null
?\Throwable $previous = null
) {
$message = \vsprintf('The given %s file is not supported, this reader supports %s files', $extensions);
parent::__construct($message, $code, $previous);
Expand Down
1 change: 1 addition & 0 deletions src/Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum Provider: string
case GITHUB = 'GITHUB';
case ANTHROPIC = 'ANTHROPIC';
case VOYAGER = 'VOYAGER';
case DEEPSEEK = 'DEEPSEEK';

public function getEnvName(): string
{
Expand Down
68 changes: 68 additions & 0 deletions src/Provider/Service/Client/DeepSeekClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Devscast\Lugha\Provider\Service\Client;

use Devscast\Lugha\Assert;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Completion\Chat\History;
use Devscast\Lugha\Model\Completion\CompletionConfig;
use Devscast\Lugha\Provider\Provider;
use Devscast\Lugha\Provider\Response\CompletionResponse;
use Devscast\Lugha\Provider\Service\Client;
use Devscast\Lugha\Provider\Service\Common\OpenAICompatibilitySupport;
use Devscast\Lugha\Provider\Service\Common\ToolCallingSupport;
use Devscast\Lugha\Provider\Service\HasCompletionSupport;

/**
* Class DeepSeekClient.
*
* @see https://api-docs.deepseek.com/api/deepseek-api
* @see https://api-docs.deepseek.com/api/create-chat-completion
*
* @author bernard-ng <[email protected]>
*/
final class DeepSeekClient extends Client implements HasCompletionSupport
{
use OpenAICompatibilitySupport;
use ToolCallingSupport;

protected const string BASE_URI = 'https://api.deepseek.com';

protected Provider $provider = Provider::DEEPSEEK;

#[\Override]
public function completion(History|string $input, CompletionConfig $config, array $tools = []): CompletionResponse
{
Assert::notEmpty($input);
$this->buildReferences($tools);

try {
$response = $this->http->request('POST', 'chat/completions', [
'json' => [
'model' => $config->model,
'messages' => match (true) {
$input instanceof History => $input->getHistory(),
default => [[
'content' => $input,
'role' => 'user',
]],
},
'tools' => $this->getToolDefinitions(),
'max_completion_tokens' => $config->maxTokens,
'temperature' => $config->temperature,
'top_p' => $config->topP,
'stop' => $config->stopSequences,
'presence_penalty' => $config->presencePenalty,
'frequency_penalty' => $config->frequencyPenalty,
...$config->additionalParameters,
],
])->toArray();

return $this->handleToolCalls($response, $config);
} catch (\Throwable $e) {
throw new ServiceIntegrationException('Unable to generate completion.', previous: $e);
}
}
}
1 change: 1 addition & 0 deletions src/Provider/Service/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static function create(Provider $provider, ?ProviderConfig $config = null
Provider::ANTHROPIC => new Client\AnthropicClient($config),
Provider::VOYAGER => new Client\VoyagerClient($config),
Provider::MISTRAL => new Client\MistralClient($config),
Provider::DEEPSEEK => new Client\DeepSeekClient($config)
};
}

Expand Down

0 comments on commit f47e1c6

Please sign in to comment.