namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\FileVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
return new FileVectorStore(
directory: storage_path(),
topK: 4
);
}
}
composer require neuron-core/php-vector
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\PHPVector\PHPVector;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyRAG extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
return new PHPVector(
path: '/var/data/mydb',
topK: 5,
);
}
}
CREATE TABLE IF NOT EXISTS rag_documents (
id UUID NOT NULL PRIMARY KEY,
content TEXT,
sourceType VARCHAR(255),
sourceName VARCHAR(255),
metadata JSON,
embedding VECTOR(1536) NOT NULL,
VECTOR INDEX (embedding)
)
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\MariaDBVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
return new MariaDBVectorStore(
new \PDO(...), // 或者从 ORM 中获取 PDO 实例
);
}
}
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\PineconeVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
return new PineconeVectorStore(
key: 'PINECONE_API_KEY',
indexUrl: 'PINECONE_INDEX_URL'
);
}
}
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\PineconeVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
protected array $vectorStoreFilters = [];
...
protected function vectorStore(): VectorStoreInterface
{
$store = new PineconeVectorStore(
key: 'PINECONE_API_KEY',
indexUrl: 'PINECONE_INDEX_URL'
);
return $store->withFilters($this->vectorStoreFilters);
}
public function addVectorStoreFilters(array $filters): self
{
$this->vectorStoreFilters = $filters;
return $this;
}
}
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\OpenSearchVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
use OpenSearch\GuzzleClientFactory;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
$opensearch = new GuzzleClientFactory()->create([
'base_uri' => 'http://localhost:9200',
]);
return new OpenSearchVectorStore(
client: $opensearch,
index: 'neuron-ai',
);
}
}
composer require typesense/typesense-php
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\TypesenseVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
$typesense = new \Typesense\Client([
'api_key' => 'TYPESENSE_API_KEY',
'nodes' => [
[
'host' => 'TYPESENSE_NODE_HOST',
'port' => 'TYPESENSE_NODE_PORT',
'protocol' => 'TYPESENSE_NODE_PROTOCOL'
],
]
]);
return new TypesenseVectorStore(
client: $typesense,
collection: 'neuron-ai',
vectorDimension: 1024
);
}
}
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\QdrantVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
return new QdrantVectorStore(
collectionUrl: 'http://localhost:6333/collections/neuron-ai/',
key: 'QDRANT_API_KEY'
);
}
}
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\ChromaVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
return new ChromaVectorStore(
collection: 'neuron-ai',
//host: 'http://localhost:8000', <-- 这是默认值
topK: 5
);
}
}
namespace App\Neuron;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\MeilisearchVectorStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyChatBot extends RAG
{
...
protected function vectorStore(): VectorStoreInterface
{
return new MeilisearchVectorStore(
indexUid: 'MEILISEARCH_INDEXUID',
host: 'http://localhost:8000', // 或使用云端 URL
key: 'MEILISEARCH_API_KEY',
embedder: 'default',
topK: 5
);
}
}
namespace NeuronAI\RAG\VectorStore;
use NeuronAI\RAG\Document;
interface VectorStoreInterface
{
public function addDocument(Document $document): void;
/**
* @param Document[] $documents
*/
public function addDocuments(array $documents): void;
public function deleteBySource(string $sourceName, string $sourceType): void;
/**
* 返回与嵌入最相似的文档。
*
* @param float[] $embedding
* @return Document[]
*/
public function similaritySearch(array $embedding, int $k = 4): iterable;
}
namespace App\Neuron\VectorStore;
use NeuronAI\RAG\Document;
use NeuronAI\RAG\VectorSimilarity;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyVectorStore implements VectorStoreInterface
{
...
/**
* @param float[] $embeddings
*/
public function similaritySearch(array $embedding): iterable
{
$documents = // 从向量存储中获取文档
return \array_map(function (Document $document) {
return $document->setScore(
VectorSimilarity::similarityFromDistance($similarity)
);
}, $documents);
}
}
namespace App\Neuron\VectorStore;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use NeuronAI\RAG\Document;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyVectorStore implements VectorStoreInterface
{
protected Client $client;
public function __construct(
string $key,
protected string $index,
protected int $topK = 5
) {
$this->client = new Client([
'base_uri' => 'https://api.vector-store.com',
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$key}",
]
]);
}
public function addDocument(Document $document): void
{
$this->addDocuments([$document]);
}
/**
* @param Document[] $documents
*/
public function addDocuments(array $documents): void
{
$this->client->post("indexes/{$this->index}", [
RequestOptions::JSON => \array_map(function (Document $document) {
return [
'vector' => $document->embedding,
];
}, $documents)
]);
}
/**
* @return Document[]
*/
public function similaritySearch(array $embedding): iterable
{
// 执行相似度搜索并返回 Document 对象数组
}
}
namespace App\Neuron;
use App\Neuron\VectorStore\MyVectorStore;
use NeuronAI\Agent;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
class MyAgent extends Agent
{
protected function vectorStore(): VectorStoreInterface
{
return new MyVectorStore(
key: 'VECTORSTORE_API_KEY',
index: 'neuron-ai',
);
}
}