Build5Nines.SharpVector
는 .NET 애플리케이션용으로 설계된 메모리 내 벡터 데이터베이스 라이브러리입니다. 벡터 표현을 사용하여 텍스트 데이터를 저장, 검색 및 관리할 수 있습니다. 라이브러리는 사용자 정의 및 확장이 가능하므로 다양한 벡터 비교 방법, 전처리 기술 및 벡터화 전략을 지원할 수 있습니다.
벡터 데이터베이스는 RAG(Retrieval-Augmented Generation) 디자인 패턴을 사용하여 AI 프롬프트로 추가 컨텍스트 데이터를 로드하는 기능으로 LLM(Large Language Model)을 강화하는 Generative AI 솔루션과 함께 사용됩니다.
벡터 데이터베이스(예: Azure CosmosDB, PostgreSQL(pgVector 포함), Azure AI Search, Elasticsearch 등)를 구축하는 데 사용할 수 있는 대규모 데이터베이스가 많이 있지만, 벡터 데이터베이스에 포함될 수 있는 경량 벡터 데이터베이스에 대한 옵션은 많지 않습니다. 모든 .NET 애플리케이션.
Build5Nines SharpVector 프로젝트는 모든 .NET 애플리케이션에서 사용할 수 있는 경량 인메모리 벡터 데이터베이스를 제공합니다.
"인메모리 벡터 데이터베이스의 경우 Chris Pietschmann의 뛰어난 오픈 소스 프로젝트인 Build5Nines.SharpVector를 사용하고 있습니다. SharpVector를 사용하면 벡터화된 데이터를 쉽게 저장하고 검색할 수 있으므로 샘플 RAG 구현에 이상적인 선택입니다."
— Tulika Chaudharie, Microsoft Azure App Service 수석 제품 관리자
Build5Nines.SharpVector
와 같은 인메모리 벡터 데이터베이스는 특히 고성능, 짧은 대기 시간 및 효율적인 리소스 사용이 요구되는 시나리오에서 기존 벡터 데이터베이스 서버에 비해 여러 가지 이점을 제공합니다.
다음은 Build5Nines.SharpVector
유용할 수 있는 여러 사용 시나리오 목록입니다.
다음은 자신의 프로젝트에서 Build5Nines.SharpVector
사용하는 방법에 대한 추가 문서 및 예제가 포함된 몇 가지 유용한 튜토리얼 링크입니다.
Build5Nines.SharpVector
라이브러리는 Nuget 패키지로 제공되어 .NET 프로젝트에 쉽게 포함할 수 있습니다.
dotnet add package Build5Nines.SharpVector
Nuget.org(https://www.nuget.org/packages/Build5Nines.SharpVector/)에서 볼 수 있습니다.
호환성을 극대화하기 위해 Build5Nines.SharpVector
라이브러리는 .NET에서 사용할 수 있는 것 이외의 외부 종속성을 사용하지 않고 빌드되었으며 .NET 6 이상을 대상으로 빌드되었습니다.
Build5Nines.SharpVector
라이브러리의 다음 예제 사용에서 볼 수 있듯이 단 몇 줄의 코드만으로 메모리 내 벡터 데이터베이스를 모든 .NET 애플리케이션에 포함할 수 있습니다.
// Create a Vector Database with metadata of type string
var vdb = new BasicMemoryVectorDatabase ( ) ;
// The Metadata is declared using generics, so you can store whatever data you need there.
// Load Vector Database with some sample text data
// Text is the movie description, and Metadata is the movie title with release year in this example
vdb . AddText ( " Iron Man (2008) is a Marvel Studios action, adventure, and sci-fi movie about Tony Stark (Robert Downey Jr.), a billionaire inventor and weapons developer who is kidnapped by terrorists and forced to build a weapon. Instead, Tony uses his ingenuity to build a high-tech suit of armor and escape, becoming the superhero Iron Man. He then returns to the United States to refine the suit and use it to fight crime and terrorism. " , " Iron Man (2008) " ) ;
vdb . AddText ( " The Lion King is a 1994 Disney animated film about a young lion cub named Simba who is the heir to the throne of an African savanna. " , " The Lion King (1994) " ) ;
vdb . AddText ( " Aladdin is a 2019 live-action Disney adaptation of the 1992 animated classic of the same name about a street urchin who finds a magic lamp and uses a genie's wishes to become a prince so he can marry Princess Jasmine. " , " Alladin (2019) " ) ;
vdb . AddText ( " The Little Mermaid is a 2023 live-action adaptation of Disney's 1989 animated film of the same name. The movie is about Ariel, the youngest of King Triton's daughters, who is fascinated by the human world and falls in love with Prince Eric. " , " The Little Mermaid " ) ;
vdb . AddText ( " Frozen is a 2013 Disney movie about a fearless optimist named Anna who sets off on a journey to find her sister Elsa, whose icy powers have trapped their kingdom in eternal winter. " , " Frozen (2013) " ) ;
// Perform a Vector Search
var result = vdb . Search ( newPrompt , pageCount : 5 ) ; // return the first 5 results
if ( result . HasResults )
{
Console . WriteLine ( " Similar Text Found: " ) ;
foreach ( var item in result . Texts )
{
Console . WriteLine ( item . Metadata ) ;
Console . WriteLine ( item . Text ) ;
}
}
Build5Nines.SharpVector.BasicMemoryVectorDatabase
클래스는 코사인 유사성, 사전 어휘 저장소 및 기본 텍스트 전처리기를 갖춘 Bag of Words 벡터화 전략을 사용합니다. 라이브러리에는 필요한 경우 이를 사용하여 사용자 정의된 벡터 데이터베이스 구현을 생성할 수 있는 일반 클래스와 많은 확장 지점이 포함되어 있습니다.
또한 TextDataLoader
를 사용하면 다양한 텍스트 청킹 방법을 지원하여 텍스트 문서를 벡터 데이터베이스로 로드할 수 있습니다.
/// Paragraph Chunking
var loader = new TextDataLoader < int , string > ( vdb ) ;
loader . AddDocument ( document , new TextChunkingOptions < string >
{
Method = TextChunkingMethod . Paragraph ,
RetrieveMetadata = ( chunk ) => {
// add some basic metadata since this can't be null
return " { chuckSize: " " + chunk . Length + " " } " ;
}
} ) ;
/// Sentence Chunking
var loader = new TextDataLoader < int , string > ( vdb ) ;
loader . AddDocument ( document , new TextChunkingOptions < string >
{
Method = TextChunkingMethod . Sentence ,
RetrieveMetadata = ( chunk ) => {
// add some basic metadata since this can't be null
return " { chuckSize: " " + chunk . Length + " " } " ;
}
} ) ;
/// Fixed Length Chunking
var loader = new TextDataLoader < int , string > ( vdb ) ;
loader . AddDocument ( document , new TextChunkingOptions < string >
{
Method = TextChunkingMethod . FixedLength ,
ChunkSize = 150 ,
RetrieveMetadata = ( chunk ) => {
// add some basic metadata since this can't be null
return " { chuckSize: " " + chunk . Length + " " } " ;
}
} ) ;
RetrieveMetadata
척이 로드될 때 척에 대한 메타데이터를 쉽게 정의하는 데 사용할 수 있는 람다 함수를 허용합니다.
이 리포지토리의 샘플 콘솔 앱은 Build5Nines.SharpVector.dll의 사용 예를 보여줍니다.
JSON 파일에서 영화 제목과 설명 목록을 로드한 다음 사용자가 프롬프트를 입력하여 데이터베이스를 검색하고 가장 일치하는 항목을 반환할 수 있습니다.
다음은 실행 중인 테스트 콘솔 앱의 스크린샷입니다.
BasicMemoryVectorDatabase
이제 동기 및 비동기 작업을 모두 지원합니다.Async
버전이 제대로 작동하는 불필요한 클래스를 제거하도록 리팩터링되었습니다.Async
버전의 클래스를 추가했습니다..AddText()
및 .AddTextAsync()
호출할 때 더 이상 메타데이터가 필요하지 않습니다.IVectorSimilarityCalculator
IVectorComparer
및 CosineVectorSimilarityCalculatorAsync
에서 CosineSimilarityVectorComparerAsync
로 리팩터링EuclideanDistanceVectorComparerAsync
추가TId
일반 유형이 더 이상 필요하지 않도록 MemoryVectorDatabase
수정했습니다.VectorSimilarity
및 Similarity
속성의 이름을 VectorComparison
으로 바꿉니다.TextDataLoader
클래스를 추가하세요.BasicMemoryVectorDatabase
클래스를 도입했습니다.VectorTextResultItem.Similarity
추가했습니다..Search
메서드 업데이트Build5Nines SharpVector 프로젝트는 HashiCorp 홍보대사인 Microsoft MVP인 Chris Pietschmann이 관리합니다.