Skip to content

Commit

Permalink
feat(theodo-group#111): add dimensions to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng committed May 17, 2024
1 parent e0f1425 commit a54da80
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ interface EmbeddingGeneratorInterface
/**
* @return float[]
*/
public function embedText(string $text): array;
public function embedText(string $text, ?int $dimensions = null): array;

public function embedDocument(Document $document): Document;
public function embedDocument(Document $document, ?int $dimensions = null): Document;

/**
* @param Document[] $documents
* @return Document[]
*/
public function embedDocuments(array $documents): array;
public function embedDocuments(array $documents, ?int $dimensions = null): array;

public function getEmbeddingLength(): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(?OpenAIConfig $config = null)
*
* @return float[]
*/
public function embedText(string $text): array
public function embedText(string $text, ?int $dimensions = null): array
{
$text = str_replace("\n", ' ', $text);

Expand All @@ -64,10 +64,10 @@ public function embedText(string $text): array
return $searchResults['data'][0]['embedding'];
}

public function embedDocument(Document $document): Document
public function embedDocument(Document $document, ?int $dimensions = null): Document
{
$text = $document->formattedContent ?? $document->content;
$document->embedding = $this->embedText($text);
$document->embedding = $this->embedText($text, $dimensions);

return $document;
}
Expand All @@ -78,11 +78,11 @@ public function embedDocument(Document $document): Document
* @param Document[] $documents
* @return Document[]
*/
public function embedDocuments(array $documents): array
public function embedDocuments(array $documents, ?int $dimensions = null): array
{
$embedDocuments = [];
foreach ($documents as $document) {
$embedDocuments[] = $this->embedDocument($document);
$embedDocuments[] = $this->embedDocument($document, $dimensions);
}

return $embedDocuments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public function __construct(OllamaConfig $config)
* Call out to Ollama embedding endpoint.
*
* @return float[]
*
* @throws \JsonException
* @throws Exception
*/
public function embedText(string $text): array
public function embedText(string $text, ?int $dimensions = null): array
{
$text = str_replace("\n", ' ', $text);

Expand All @@ -57,23 +60,28 @@ public function embedText(string $text): array
return $searchResults['embedding'];
}

public function embedDocument(Document $document): Document
/**
* @throws \JsonException
*/
public function embedDocument(Document $document, ?int $dimensions = null): Document
{
$text = $document->formattedContent ?? $document->content;
$document->embedding = $this->embedText($text);
$document->embedding = $this->embedText($text, $dimensions);

return $document;
}

/**
* @param Document[] $documents
* @return Document[]
*
* @throws \JsonException
*/
public function embedDocuments(array $documents): array
public function embedDocuments(array $documents, ?int $dimensions = null): array
{
$embedDocuments = [];
foreach ($documents as $document) {
$embedDocuments[] = $this->embedDocument($document);
$embedDocuments[] = $this->embedDocument($document, $dimensions);
}

return $embedDocuments;
Expand Down

0 comments on commit a54da80

Please sign in to comment.