# frozen_string_literal: true # Build the wiki RAG index: fetch knowledge pages (Wiki.js GraphQL) -> chunk -> embed (OpenRouter) # -> save. Run: WIKI_JWT=… OPENROUTER_API_KEY=… ruby build_index.rb [out.json] # Keys can also be read from the standard local key files (the paths in the defaults below). require_relative "wiki_client" require_relative "embedder" require_relative "wiki_index" def key_from(file, pattern) return nil unless File.exist?(file) File.readlines(file).map(&:strip).find { |l| l =~ pattern } end jwt = ENV["WIKI_JWT"] || key_from("/home/lucy/Downloads/apikeyvodafonewiki", /\Aey[A-Za-z0-9_-]/) orkey = ENV["OPENROUTER_API_KEY"] || key_from("/home/lucy/Downloads/openrouterApiKlic2", /\Ask-or-v1-/) abort "no WIKI_JWT" if jwt.to_s.empty? abort "no OPENROUTER_API_KEY" if orkey.to_s.empty? out = ARGV[0] || File.join(__dir__, "wiki_index.json") warn "fetching knowledge pages from the wiki…" pages = Helpdesk::WikiClient.new(jwt: jwt).knowledge_pages chars = pages.sum { |p| p[:text].length } warn " #{pages.size} pages, #{chars} chars (~#{chars / 4} tokens) to index" warn "embedding chunks via OpenRouter…" idx = Helpdesk::WikiIndex.new(embedder: Helpdesk::Embedder.new(api_key: orkey)) idx.build(pages) idx.save(out) warn "DONE: #{idx.size} chunks -> #{out}"