Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified SQLite vector store creation #123

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace LangChain.Databases;

public class SQLIteVectorStoreOptions
{
public string Filename { get; set; } = "vectors.db";
public string TableName { get; set; } = "vectors";
public int ChunkSize { get; set; } = 200;
public int ChunkOverlap { get; set; } = 50;
}
30 changes: 30 additions & 0 deletions src/libs/Databases/LangChain.Databases.Sqlite/SQLiteVectorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ public static async Task<VectorStoreIndexWrapper> CreateIndexFromDocuments(
return index;
}

public static SQLIteVectorStoreOptions DefaultOptions = new SQLIteVectorStoreOptions();

/// <summary>
/// If database does not exists, it loads documents from the documentsSource, creates an index from these documents and returns the created index.
/// If database exists, it loads the index from the database.
/// documentsSource is used only if the database does not exist. If the database exists, documentsSource is ignored.
/// </summary>
/// <param name="embeddings">An object implementing the <see cref="IEmbeddings"/> interface. This object is used to generate embeddings for the documents.</param>
/// <param name="documentsSource">An optional object implementing the <see cref="ISource"/> interface. This object is used to load documents if the vector store database file does not exist.</param>
/// <param name="options">An optional <see cref="SQLIteVectorStoreOptions"/> object. This object provides configuration options for the SQLite vector store</param>
public static async Task<VectorStoreIndexWrapper> GetIndex(
IEmbeddings embeddings, ISource? documentsSource=null, SQLIteVectorStoreOptions? options=null)
{
options ??= DefaultOptions;


TextSplitter textSplitter = new RecursiveCharacterTextSplitter(chunkSize: options.ChunkSize, chunkOverlap: options.ChunkOverlap);

if (!System.IO.File.Exists("vectors.db"))
{
var documents = await documentsSource.LoadAsync();
return await SQLiteVectorStore.CreateIndexFromDocuments(embeddings, documents, options.Filename, options.TableName, textSplitter: textSplitter);
}


var vectorStore = new SQLiteVectorStore(options.Filename, options.TableName, embeddings);
var index = new VectorStoreIndexWrapper(vectorStore);
return index;
}

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace LangChain.Providers;

public static class WithDebugExtensions
{
public static T UseConsoleForDebug<T>(this T model) where T : IWithDebug
{
model.PromptSent += Console.Write;
model.TokenGenerated += Console.Write;
return model;
}
}
14 changes: 14 additions & 0 deletions src/libs/LangChain.Core/Providers/IWithDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace LangChain.Providers;

public interface IWithDebug
{
/// <summary>
/// Occurs when token generated.
/// </summary>
public event Action<string> TokenGenerated;

/// <summary>
/// Occurs before prompt is sent to the model.
/// </summary>
public event Action<string> PromptSent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LangChain.Providers.LLamaSharp;
///
/// </summary>
[CLSCompliant(false)]
public class LLamaSharpModelInstruction : LLamaSharpModelBase
public class LLamaSharpModelInstruction : LLamaSharpModelBase, IWithDebug
{


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace LangChain.Providers;
/// <summary>
///
/// </summary>
public class OllamaLanguageModelInstruction : IChatModel
public class OllamaLanguageModelInstruction : IChatModel, IWithDebug
{
private readonly string _modelName;
private readonly OllamaApiClient _api;
Expand Down
Loading