-
Notifications
You must be signed in to change notification settings - Fork 47
Add embedding generation support #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| use WordPress\AiClient\Builders\PromptBuilder; | ||
| use WordPress\AiClient\Common\Exception\InvalidArgumentException; | ||
| use WordPress\AiClient\Common\Exception\RuntimeException; | ||
| use WordPress\AiClient\Operations\DTO\EmbeddingOperation; | ||
| use WordPress\AiClient\ProviderImplementations\Anthropic\AnthropicProvider; | ||
| use WordPress\AiClient\ProviderImplementations\Google\GoogleProvider; | ||
| use WordPress\AiClient\ProviderImplementations\OpenAi\OpenAiProvider; | ||
|
|
@@ -16,6 +17,7 @@ | |
| use WordPress\AiClient\Providers\Models\Contracts\ModelInterface; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelConfig; | ||
| use WordPress\AiClient\Providers\ProviderRegistry; | ||
| use WordPress\AiClient\Results\DTO\EmbeddingResult; | ||
| use WordPress\AiClient\Results\DTO\GenerativeAiResult; | ||
|
|
||
| /** | ||
|
|
@@ -298,6 +300,60 @@ public static function generateSpeechResult( | |
| return self::getConfiguredPromptBuilder($prompt, $modelOrConfig, $registry)->generateSpeechResult(); | ||
| } | ||
|
|
||
| /** | ||
| * Generates embeddings for the given input using the traditional API. | ||
| * | ||
| * @since 0.2.0 | ||
| * | ||
| * @param Prompt $input The input to embed. Accepts strings, messages, or arrays of those types. | ||
| * @param ModelInterface|ModelConfig|null $modelOrConfig Optional model specification. | ||
| * @param ProviderRegistry|null $registry Optional custom registry. | ||
| * @return EmbeddingResult The embeddings result. | ||
| */ | ||
| public static function generateEmbeddingsResult( | ||
| $input, | ||
| $modelOrConfig = null, | ||
| ?ProviderRegistry $registry = null | ||
| ): EmbeddingResult { | ||
| self::validateModelOrConfigParameter($modelOrConfig); | ||
| $builder = self::prompt(null, $registry); | ||
| if ($modelOrConfig instanceof ModelInterface) { | ||
| $builder->usingModel($modelOrConfig); | ||
| } elseif ($modelOrConfig instanceof ModelConfig) { | ||
| $builder->usingModelConfig($modelOrConfig); | ||
| } | ||
|
Comment on lines
+319
to
+324
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the |
||
|
|
||
| $builder->withEmbeddingInputs($input); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comments on |
||
| return $builder->generateEmbeddingsResult(); | ||
| } | ||
|
|
||
| /** | ||
| * Generates an embeddings operation for asynchronous processing. | ||
| * | ||
| * @since 0.2.0 | ||
| * | ||
| * @param Prompt $input The input to embed. | ||
| * @param ModelInterface|ModelConfig|null $modelOrConfig Optional model specification. | ||
| * @param ProviderRegistry|null $registry Optional custom registry. | ||
| * @return EmbeddingOperation The embeddings operation. | ||
| */ | ||
| public static function generateEmbeddingsOperation( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not implement this yet - we don't support long-running operations for anything yet, so doing that here would be preliminary. |
||
| $input, | ||
| $modelOrConfig = null, | ||
| ?ProviderRegistry $registry = null | ||
| ): EmbeddingOperation { | ||
| self::validateModelOrConfigParameter($modelOrConfig); | ||
| $builder = self::prompt(null, $registry); | ||
| if ($modelOrConfig instanceof ModelInterface) { | ||
| $builder->usingModel($modelOrConfig); | ||
| } elseif ($modelOrConfig instanceof ModelConfig) { | ||
| $builder->usingModelConfig($modelOrConfig); | ||
| } | ||
|
|
||
| $builder->withEmbeddingInputs($input); | ||
| return $builder->generateEmbeddingsOperation(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new message builder for fluent API usage. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make these
@since n.e.x.tfor all new code? We use this to keep things flexible before release, and they will be replaced with a concrete version in a pre-release step.