Build5Nines.SharpVector
เป็นไลบรารีฐานข้อมูลเวกเตอร์ในหน่วยความจำที่ออกแบบมาสำหรับแอปพลิเคชัน .NET ช่วยให้คุณสามารถจัดเก็บ ค้นหา และจัดการข้อมูลข้อความโดยใช้การแสดงเวกเตอร์ ไลบรารีสามารถปรับแต่งและขยายได้ ช่วยให้รองรับวิธีการเปรียบเทียบเวกเตอร์ เทคนิคการเตรียมการประมวลผลล่วงหน้า และกลยุทธ์การทำเวกเตอร์ที่แตกต่างกัน
ฐานข้อมูลเวกเตอร์ใช้กับโซลูชัน Generative AI ที่เสริม LLM (โมเดลภาษาขนาดใหญ่) พร้อมความสามารถในการโหลดข้อมูลบริบทเพิ่มเติมด้วยพร้อมต์ AI โดยใช้รูปแบบการออกแบบ RAG (Retriieval-Augmented Generation)
แม้ว่าจะมีฐานข้อมูลขนาดใหญ่จำนวนมากที่สามารถใช้ในการสร้างฐานข้อมูลเวกเตอร์ (เช่น Azure CosmosDB, PostgreSQL w/ pgvector, Azure AI Search, Elasticsearch และอื่นๆ) แต่ก็ไม่มีตัวเลือกมากมายสำหรับฐานข้อมูลเวกเตอร์แบบน้ำหนักเบาที่สามารถฝังลงใน แอปพลิเคชัน .NET ใด ๆ
โปรเจ็กต์ Build5Nines SharpVector มอบฐานข้อมูลเวกเตอร์ในหน่วยความจำน้ำหนักเบาสำหรับใช้ในแอปพลิเคชัน .NET ใดๆ
"สำหรับฐานข้อมูลเวกเตอร์ในหน่วยความจำ เราใช้ Build5Nines.SharpVector ซึ่งเป็นโปรเจ็กต์โอเพ่นซอร์สที่ยอดเยี่ยมโดย Chris Pietschmann 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
ต่อไปนี้ คุณสามารถฝังฐานข้อมูล Vector ในหน่วยความจำลงในแอปพลิเคชัน .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
ยอมรับฟังก์ชัน lambda ที่สามารถใช้เพื่อกำหนดข้อมูลเมตาสำหรับ chucks ขณะโหลดได้อย่างง่ายดาย
แอปคอนโซลตัวอย่างใน repo นี้แสดงตัวอย่างการใช้งาน Build5Nines.SharpVector.dll
โดยจะโหลดรายชื่อภาพยนตร์และคำอธิบายจากไฟล์ JSON จากนั้นให้ผู้ใช้สามารถพิมพ์ข้อความแจ้งเพื่อค้นหาฐานข้อมูลและส่งกลับผลลัพธ์ที่ตรงกันที่สุด
นี่คือภาพหน้าจอของแอปคอนโซลทดสอบที่ทำงานอยู่:
BasicMemoryVectorDatabase
รองรับการดำเนินการทั้งแบบซิงโครนัสและอะซิงโครนัสAsync
จะทำงานได้ดีAsync
เพื่อรองรับมัลติเธรด.AddText()
และ .AddTextAsync()
IVectorSimilarityCalculator
กันเครื่องคิดเลขเป็น IVectorComparer
และ CosineVectorSimilarityCalculatorAsync
to CosineSimilarityVectorComparerAsync
EuclideanDistanceVectorComparerAsync
ใหม่MemoryVectorDatabase
เพื่อไม่ต้องใช้ประเภททั่วไป TId
ที่ไม่ได้ใช้อีกต่อไปVectorSimilarity
และ Similarity
เป็น VectorComparison
TextDataLoader
เพื่อรองรับวิธีการต่างๆ ของการรวมข้อความเมื่อโหลดเอกสารลงในฐานข้อมูลเวกเตอร์BasicMemoryVectorDatabase
เป็นการใช้งานฐานข้อมูลเวกเตอร์ขั้นพื้นฐานที่ใช้กลยุทธ์การทำเวกเตอร์แบบ Bag of Words ที่มีความคล้ายคลึงกันของโคไซน์ ที่เก็บคำศัพท์ในพจนานุกรม และตัวประมวลผลล่วงหน้าข้อความพื้นฐานVectorTextResultItem.Similarity
เพื่อให้การใช้โค้ดสามารถตรวจสอบความคล้ายคลึงกันของข้อความในผลการค้นหาเวกเตอร์ได้.Search
เพื่อรองรับการเพจผลการค้นหาและการสนับสนุนเกณฑ์สำหรับการเปรียบเทียบความคล้ายคลึงกันโครงการ Build5Nines SharpVector ได้รับการดูแลโดย Chris Pietschmann, Microsoft MVP, HashiCorp Ambassador