/logseq_rag_chatbot

A chatbot that utilizes the RAG technique can generate responses to user questions based on selected Loqseq notes.

Primary LanguageTypeScript

Logseq RAG Chatbot Plugin 🤖

A chatbot that utilizes the RAG technique can generate responses to user questions based on selected Loqseq notes.

Features

  • Chat with your notes.
  • Answer sources citation with links to original note blocks.
  • Contextualizing user question with chat history.
  • Persistent vector storage on Supabase.
  • Prompt tracing with Langsmith

How to get started

  1. Clone the repository

  2. Make sure you have pnpm installed, install if necessary

  3. Execute pnpm install and pnpm build to build the plugin

  4. Enable developer-mode in Logseq, go to plugins, select "Load unpacked plugin"

  5. Select the directory of your plugin (not the /dist-directory, but the directory which includes your package.json)

  6. Setup a Supabase project and execute following SQL in SQL Editor.

    -- Enable the pgvector extension to work with embedding vectors
    create extension vector;
    
    -- Create a table to store page info
    create table pages(
    id bigint generated by default as identity,
    uuid uuid not null primary key,
    name character varying null,
    original_name character varying null,
    updated_at bigint not null,
    );
    
    -- Create a table to store your documents
    create table documents (
    id bigserial primary key,
    content text, -- corresponds to Document.pageContent
    metadata jsonb, -- corresponds to Document.metadata
    embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
    );
    
    -- Create a function to search for documents
    create function match_documents (
    query_embedding vector(1536),
    match_count int default null,
    filter jsonb DEFAULT '{}'
    ) returns table (
    id bigint,
    content text,
    metadata jsonb,
    similarity float
    )
    language plpgsql
    as $$
    #variable_conflict use_column
    begin
    return query
    select
        id,
        content,
        metadata,
        1 - (documents.embedding <=> query_embedding) as similarity
    from documents
    where metadata @> filter
    order by documents.embedding <=> query_embedding
    limit match_count;
    end;
    $$;
  7. Setup Supabase project url, service key, and OpenAI API key in Plugin Settings.

  8. Start Chat with your notes.