> For the complete documentation index, see [llms.txt](https://docs.neuron-ai.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.neuron-ai.dev/neuron-v3-zh/rag/embeddings-provider.md).

# 向量嵌入提供方

将您的文本转换为向量表示！嵌入可让您将检索增强生成（[RAG](/neuron-v3-zh/rag/rag.md)) 集成到您的 AI 应用中。

## 可用的嵌入提供方

该框架已包含以下嵌入提供方。

### Ollama

使用 Ollama，您可以在本地运行嵌入模型。文档 - <https://ollama.com/blog/embedding-models>

```php
namespace App\Neuron;

use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\OllamaEmbeddingsProvider;
use NeuronAI\RAG\RAG;

class MyRAG extends RAG
{
    ...
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        return new OllamaEmbeddingsProvider(
            model: 'OLLAMA_EMBEDDINGS_MODEL'
        );
    }
}
```

### Voyage AI

文档 - <https://www.voyageai.com/>

```php
namespace App\Neuron;

use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\VoyageEmbeddingsProvider;
use NeuronAI\RAG\RAG;

class MyRAG extends RAG
{
    ...
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        return new VoyageEmbeddingsProvider(
            key: 'VOYAGE_API_KEY',
            model: 'VOYAGE_EMBEDDINGS_MODEL' // voyage-3-large
        );
    }
}
```

### OpenAI

文档 - <https://platform.openai.com/docs/guides/embeddings>

```php
namespace App\Neuron;

use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\OpenAIEmbeddingsProvider;
use NeuronAI\RAG\RAG;

class MyRAG extends RAG
{
    ...
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        return new OpenAIEmbeddingsProvider(
            key: 'OPENAI_API_KEY',
            model: 'OPENAI_EMBEDDINGS_MODEL' // text-embedding-3-small
        );
    }
}
```

### OpenAILikeEmbeddings

您可以使用任何与 OpenAI API 格式兼容的提供方：

```php
namespace App\Neuron;

use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\OpenAILikeEmbeddings;
use NeuronAI\RAG\RAG;

class MyRAG extends RAG
{
    ...
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        return new OpenAILikeEmbeddings(
            baseUri: 'PRODIDER_URL',
            key: 'PROVIDER_API_KEY',
            model: 'PROVIDER_EMBEDDING_MODEL'
        );
    }
}
```

### Gemini

```php
namespace App\Neuron;

use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\GeminiEmbeddingsProvider;
use NeuronAI\RAG\RAG;

class MyRAG extends RAG
{
    ...
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        return new GeminiEmbeddingsProvider(
            key: 'GEMINI_API_KEY',
            model: 'GEMINI_EMBEDDINGS_MODEL' // gemini-embedding-001
        );
    }
}
```

### Cohere

```php
namespace App\Neuron;

use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\CohereEmbeddingsProvider;
use NeuronAI\RAG\RAG;

class MyRAG extends RAG
{
    ...
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        return new CohereEmbeddingsProvider(
            key: 'COHERE_API_KEY',
            model: 'COHERE_EMBEDDINGS_MODEL' // embed-v4.0
        );
    }
}
```

### AWS Bedrock

```php
namespace App\Neuron;

use Aws\BedrockRuntime\BedrockRuntimeClient;
use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\AwsBedrockEmbeddingsProvider;
use NeuronAI\RAG\RAG;

class MyRAG extends RAG
{
    ...
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        $client = new BedrockRuntimeClient([
            'version' => 'latest',
            'region' => 'us-east-1',
            'credentials' => [
                'key' => 'AWS_BEDROCK_KEY',
                'secret' => 'AWS_BEDROCK_SECRET',
            ],
        ]);
        
        return new AwsBedrockEmbeddingsProvider(
            client: $client,
            model: 'AWS_EMBEDDINGS_MODEL'
        );
    }
}
```

## 实现一个新的提供方

要创建自定义提供方，您只需扩展 `AbstractEmbeddingsProvider` 类。此类已经实现了框架特定的方法，并让您可以自由地在其中实现唯一的、提供方特定的 HTTP 调用到 `embedText()` 方法中传入你的偏好来定制获取搜索结果的默认选项：

```php
namespace App\Neuron\Embeddings;

use GuzzleHttp\Client;

class CustomEmbeddingsProvider extends AbstractEmbeddingsProvider
{
    protected Client $client;

    protected string $baseUri = 'HTTP-ENDPOINT';

    public function __construct(
        protected string $key,
        protected string $model
    ) {
        $this->client = new Client([
            'base_uri' => trim($this->baseUri, '/').'/',
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer ' . $this->key,
            ]
        ]);
    }

    public function embedText(string $text): array
    {
        $response = $this->client->post('', [
            'json' => [
                'model' => $this->model,
                'input' => $text,
            ]
        ]);

        $response = \json_decode($response->getBody()->getContents(), true);

        return $response['data'][0]['embedding'];
    }
}
```

您应根据自定义提供方的 API 调整 HTTP 请求。
