-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for embeddings and reranking models
- Loading branch information
1 parent
1cf0ba5
commit 01e2fb4
Showing
32 changed files
with
561 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,23 +11,25 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Model\Chat; | ||
namespace Devscast\Lugha\Model\Completion; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Class ChatConfig. | ||
* Class CompletionConfig. | ||
* | ||
* @see https://platform.openai.com/docs/api-reference/chat/object | ||
* @see https://ai.google.dev/gemini-api/docs/text-generation?lang=rest#configure | ||
* @see https://docs.mistral.ai/api/#tag/chat | ||
* @see https://docs.anthropic.com/en/api/messages | ||
* @see https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final readonly class ChatConfig | ||
final readonly class CompletionConfig | ||
{ | ||
/** | ||
* @param string $model The model to use for generating the text. | ||
* @param float|null $temperature The value used to control the randomness of the generated text. | ||
* @param int|null $maxTokens The maximum number of tokens to generate. | ||
* @param float|null $topP The cumulative probability of the top tokens to keep. | ||
|
@@ -37,6 +39,7 @@ | |
* @param array|null $stopSequences A list of sequences where the model should stop generating the text. | ||
*/ | ||
public function __construct( | ||
public string $model, | ||
public ?float $temperature = null, | ||
public ?int $maxTokens = null, | ||
public ?float $topP = null, | ||
|
@@ -45,6 +48,7 @@ public function __construct( | |
public ?float $presencePenalty = null, | ||
public ?array $stopSequences = null, | ||
) { | ||
Assert::notEmpty($this->model); | ||
Assert::nullOrRange($this->temperature, 0, 2); | ||
Assert::nullOrPositiveInteger($this->maxTokens); | ||
Assert::nullOrRange($this->topP, 0, 1); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Model\Reranking; | ||
|
||
/** | ||
* Class RankedDocument. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class RankedDocument | ||
{ | ||
public function __construct( | ||
public string $content, | ||
public float $score | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Model\Reranking; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Class RerankingConfig. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final readonly class RerankingConfig | ||
{ | ||
/** | ||
* @param string $model The model to use for generating the embeddings. | ||
* @param int $topK The number of top results to return. | ||
*/ | ||
public function __construct( | ||
public string $model, | ||
public int $topK | ||
) { | ||
Assert::notEmpty($this->model); | ||
Assert::positiveInteger($this->topK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Provider\Response; | ||
|
||
/** | ||
* Class CompletionResponse. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final readonly class CompletionResponse | ||
{ | ||
public function __construct( | ||
public string $model, | ||
public string $completion, | ||
public array $providerResponse = [] | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,19 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Provider\Service\Ollama; | ||
|
||
use Devscast\Lugha\Provider\Service\AbstractClient; | ||
namespace Devscast\Lugha\Provider\Response; | ||
|
||
/** | ||
* Class Client. | ||
* Class EmbeddingResponse. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class Client extends AbstractClient | ||
final readonly class EmbeddingResponse | ||
{ | ||
protected const string BASE_URI = 'http://localhost:11434/api/'; | ||
public function __construct( | ||
public string $model, | ||
public array $embedding, | ||
public array $providerResponse = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Provider\Response; | ||
|
||
use Devscast\Lugha\Model\Reranking\RankedDocument; | ||
|
||
/** | ||
* Class RerankingResponse. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final readonly class RerankingResponse | ||
{ | ||
/** | ||
* @param array<RankedDocument> $documents | ||
*/ | ||
public function __construct( | ||
public string $model, | ||
public array $documents, | ||
public array $providerResponse = [] | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,18 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Provider\Service\Anthropic; | ||
namespace Devscast\Lugha\Provider\Service\Client; | ||
|
||
use Devscast\Lugha\Provider\Service\AbstractClient; | ||
|
||
/** | ||
* Class Client. | ||
* Class OllamaClient. | ||
* | ||
* @see https://docs.anthropic.com/en/api/getting-started | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class Client extends AbstractClient | ||
final class AnthropicClient extends AbstractClient | ||
{ | ||
protected const string BASE_URI = 'https://api.anthropic.com/v1/'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,18 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Provider\Service\Github; | ||
namespace Devscast\Lugha\Provider\Service\Client; | ||
|
||
use Devscast\Lugha\Provider\Service\AbstractClient; | ||
|
||
/** | ||
* Class Client. | ||
* Class OllamaClient. | ||
* | ||
* @see https://github.com/marketplace/models/ | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class Client extends AbstractClient | ||
final class GithubClient extends AbstractClient | ||
{ | ||
protected const string BASE_URI = 'https://models.inference.ai.azure.com/'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Provider\Service\Client; | ||
|
||
use Devscast\Lugha\Model\Embedding\EmbeddingConfig; | ||
use Devscast\Lugha\Provider\Response\EmbeddingResponse; | ||
use Devscast\Lugha\Provider\Service\AbstractClient; | ||
use Devscast\Lugha\Provider\Service\HasEmbeddingSupport; | ||
use Devscast\Lugha\Provider\Service\IntegrationException; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Class OllamaClient. | ||
* | ||
* @see https://ai.google.dev/api | ||
* @see https://ai.google.dev/gemini-api/docs/embeddings#curl | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class GoogleClient extends AbstractClient implements HasEmbeddingSupport | ||
{ | ||
protected const string BASE_URI = 'https://generativelanguage.googleapis.com/v1beta/'; | ||
|
||
#[\Override] | ||
public function embeddings(string $prompt, EmbeddingConfig $config): EmbeddingResponse | ||
{ | ||
Assert::notEmpty($prompt); | ||
|
||
try { | ||
$response = $this->http->request('POST', "models/{$config->model}:embedContent?key={$this->config->apiKey}", [ | ||
'json' => [ | ||
'model' => "models/{$config->model}", | ||
'content' => [ | ||
'parts' => [ | ||
[ | ||
'text' => $prompt, | ||
], | ||
], | ||
], | ||
], | ||
])->toArray(); | ||
|
||
return new EmbeddingResponse( | ||
model: $config->model, | ||
embedding: $response['embedding']['values'], | ||
providerResponse: [] // no special information to pass | ||
); | ||
} catch (\Throwable $e) { | ||
throw new IntegrationException('Unable to generate embeddings.', previous: $e); | ||
} | ||
} | ||
} |
Oops, something went wrong.