Files
CodeAnalizer/CodeBase/Repositories/DatabaseInitializer.cs

46 lines
1.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Neo4j.Driver;
namespace CodeBase.Repositories
{
public class DatabaseInitializer
{
private readonly IDriver _neo4jDriver;
public DatabaseInitializer(IDriver neo4jDriver)
{
_neo4jDriver = neo4jDriver;
}
public async Task InitializeAsync()
{
try
{
await using var session = _neo4jDriver.AsyncSession();
// IF NOT EXISTS гарантирует, что запрос не упадет с ошибкой,
// если индекс уже был создан при предыдущем запуске
var query = @"
CREATE VECTOR INDEX code_embeddings IF NOT EXISTS
FOR (m:CodeEntity) ON (m.embedding)
OPTIONS {
indexConfig: {
`vector.dimensions`: 768, // <-- УКАЖИ ТУТ РАЗМЕРНОСТЬ ТВОЕЙ МОДЕЛИ
`vector.similarity_function`: 'cosine'
}
}";
await session.ExecuteWriteAsync(async tx =>
{
await tx.RunAsync(query);
});
Console.WriteLine("[БД] Векторный индекс успешно инициализирован.");
}
catch (Exception ex)
{
Console.WriteLine($"[БД ОШИБКА] Ошибка при создании индекса: {ex.Message}");
}
}
}
}