Class: SimpleRag::Engine
- Inherits:
-
Object
- Object
- SimpleRag::Engine
- Defined in:
- lib/simple_rag.rb
Constant Summary collapse
- DEFAULT_URL =
"https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt"
Instance Method Summary collapse
- #get_url ⇒ Object
- #prompt_user_for_url ⇒ Object
- #run ⇒ Object
- #run_mistral(client, user_message, model: "mistral-medium-latest") ⇒ Object
- #valid_url?(url) ⇒ Boolean
Instance Method Details
#get_url ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/simple_rag.rb', line 43 def get_url url = prompt_user_for_url until valid_url?(url) puts "The URL provided is invalid. Please try again." url = prompt_user_for_url end url end |
#prompt_user_for_url ⇒ Object
30 31 32 33 34 |
# File 'lib/simple_rag.rb', line 30 def prompt_user_for_url print "Specify a URL to an HTML document you would like to ask questions of (Default: What I Worked On by Paul Graham): " input_url = gets.chomp input_url.empty? ? DEFAULT_URL : input_url end |
#run ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/simple_rag.rb', line 52 def run url = get_url puts "Document Downloaded" # Setup LLM of choice api_key = ENV["MISTRAL_AI_KEY"] || STDIN.getpass("Type your API Key: ") raise "Missing API Key" unless api_key client = Mistral.new( credentials: {api_key: api_key}, options: {server_sent_events: true} ) # Indexing puts "Initialize indexing" index_instance = SimpleRag::Index.new(client) puts "Loading url" text = index_instance.load(url) puts "Chunk text" chunks = index_instance.chunk(text) puts "Embed chunks" = index_instance.(chunks) index = index_instance.save() retrieve_instance = SimpleRag::Retrieve.new(client) retrieve_instance.save_index(index) retrieve_instance.save_chunks(chunks) loop do print "Enter your query (or type 'exit' to quit): " query = gets.chomp break if query.downcase == "exit" puts # Retrieval/Search = retrieve_instance.(query) retrieved_chunks = retrieve_instance.similarity_search(, 2) # Generation prompt = SimpleRag::Generate.new.prompt(query, retrieved_chunks) puts run_mistral(client, prompt) puts end end |
#run_mistral(client, user_message, model: "mistral-medium-latest") ⇒ Object
24 25 26 27 28 |
# File 'lib/simple_rag.rb', line 24 def run_mistral(client, , model: "mistral-medium-latest") = [{role: "user", content: }] chat_response = client.chat_completions({model: model, messages: }) chat_response.dig("choices", 0, "message", "content") end |
#valid_url?(url) ⇒ Boolean
36 37 38 39 40 41 |
# File 'lib/simple_rag.rb', line 36 def valid_url?(url) uri = URI.parse(url) uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) rescue URI::InvalidURIError false end |