$part ) { $text = $part instanceof MessagePart ? $part->getText() : null; if ( null === $text || '' === trim( $text ) ) { throw new InvalidArgumentException( sprintf( 'Embedding input %d must be non-empty text.', (int) $index ) ); } $texts[] = $text; } if ( array() === $texts ) { throw new InvalidArgumentException( 'At least one embedding input is required.' ); } $params = array( 'model' => $this->metadata()->getId(), 'input' => $texts, ); if ( null !== $this->getConfig()->getDimensions() ) { $params['dimensions'] = $this->getConfig()->getDimensions(); } $data = $this->post( 'embeddings', $this->withCustomOptions( $params ) )->getData(); $embeddings = array(); foreach ( (array) ( $data['data'] ?? array() ) as $node ) { $vector = is_array( $node ) ? ( $node['embedding'] ?? null ) : null; if ( ! is_array( $vector ) || array() === $vector ) { throw ResponseException::fromInvalidData( 'amazee.ai', 'data.embedding', 'Expected an embedding vector.' ); } $embeddings[] = new Embedding( array_map( 'floatval', array_values( $vector ) ), count( $vector ) ); } if ( array() === $embeddings ) { throw ResponseException::fromMissingData( 'amazee.ai', 'data' ); } $usage = is_array( $data['usage'] ?? null ) ? $data['usage'] : array(); $prompt = (int) ( $usage['prompt_tokens'] ?? 0 ); return new EmbeddingResult( is_string( $data['id'] ?? null ) ? $data['id'] : uniqid( 'amazeeio-embedding-', true ), $embeddings, $embeddings[0]->getDimensions(), new TokenUsage( $prompt, 0, (int) ( $usage['total_tokens'] ?? $prompt ) ), $this->providerMetadata(), $this->metadata() ); } }