-
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
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 <bernard@devscast.tech> | ||
*/ | ||
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 <bernard@devscast.tech> | ||
* | ||
* 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 <bernard@devscast.tech> | ||
*/ | ||
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 <bernard@devscast.tech> | ||
*/ | ||
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
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 <bernard@devscast.tech> | ||
*/ | ||
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
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,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <bernard@devscast.tech> | ||
* | ||
* 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 <bernard@devscast.tech> | ||
*/ | ||
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.