gromit 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bb63635be5d413e6221b47e7f726df939363b49601d67b5fde8657520b3c7b6
4
- data.tar.gz: '009cb195bd61e73709dc26e4d11abcecc0fccc3ec774414bfab21426ba3522a5'
3
+ metadata.gz: 9932004042ffb0cc538889e78933675898b22305fa251e56cff964d3d79e46b0
4
+ data.tar.gz: 021d95202589af85ac778f35be87004ab36fafcd4c636aaedda0c0d9096f54e6
5
5
  SHA512:
6
- metadata.gz: 79e8650c8531ab4bfa31ddca7af92c69d3dc160d6f4b66bf2f0c900f6a7ea451c21a00f64adb1394b813de92be33b7a7b28df7c40adf7864cc12543f66da049c
7
- data.tar.gz: deb314cec627935551926ad76718c7c49623423c2d8f08a4b03ea38d1dade24750ee86f9592df3dc249c0e9639706efe104ab6d5336b3bd9c5e56123c5c25c91
6
+ metadata.gz: 7f2c28d1f9549c9271124eae5386b5e8b2b4aa79b03c31786f071c09471ecef517c0ac33301d5590ce03aac3eb6e0b2f615f1af50f28b646c7f8ff1807c4fe33
7
+ data.tar.gz: 716276f9c72f67a31d451f7ddbf4344dfd1a7d05d3e4ebea55e0c65da6b1ec018cbb53e163c1845ec7ed8433c48a0f5221144320578a47b1f5e7d41aa18b63b0
data/README.md CHANGED
@@ -1,8 +1,40 @@
1
1
  # Gromit
2
- Short description and motivation.
2
+ Your documentation search assitant
3
3
 
4
4
  ## Usage
5
- How to use my plugin.
5
+
6
+ Make sure to provide your OpenAI key in a `.env` file
7
+ ```
8
+ OPENAPI_ACCESS_TOKEN=your-openai-token
9
+ ```
10
+
11
+ To mount the Gromit engine in Rails 7 add this line to your routes:
12
+ ```ruby
13
+ Rails.application.routes.draw do
14
+ mount Gromit::Engine => "/"
15
+ end
16
+ ```
17
+
18
+ Gromit provides the following routes:
19
+ ```
20
+ Routes for Gromit::Engine:
21
+ healthcheck GET /healthcheck(.:format) gromit/gromit#healthcheck {:format=>:json}
22
+ search POST /search(.:format) gromit/gromit#search {:format=>:json}
23
+ upsert POST /upsert(.:format) gromit/gromit#upsert {:format=>:json}
24
+ ```
25
+
26
+ To index your documentation locally you can use `gromit-reindexer`. This will update `redis-stack-server` running on your machine.
27
+ ```bash
28
+ bundle exec gromit-reindexer -s /path/to/your/docs
29
+ ```
30
+
31
+ To remotely upsert your documentation you can use `gromit-uploader`.
32
+ ```bash
33
+ BASE_URL=https://gromit-rails.example.com bundle exec gromit-uploader -s /path/to/your/docs
34
+ ```
35
+
36
+ For a working example of a Rails 7 application using Gromit check out:
37
+ https://github.com/releasehub-com/gromit-example
6
38
 
7
39
  ## Installation
8
40
  Add this line to your application's Gemfile:
@@ -1,5 +1,6 @@
1
1
  module Gromit
2
2
  class GromitController < ApplicationController
3
+ skip_before_action :verify_authenticity_token
3
4
 
4
5
  def healthcheck
5
6
  gromit = Gromit::Search.new
@@ -1,6 +1,6 @@
1
1
 
2
2
  class Gromit::MarkdownParser
3
- attr_reader :sections
3
+ attr_reader :sections, :file_path
4
4
 
5
5
  class << self
6
6
 
@@ -58,7 +58,6 @@ class Gromit::MarkdownParser
58
58
  data = redis.get(section_id)
59
59
 
60
60
  if data.nil?
61
-
62
61
  # OpenAI recommends replacing newlines with spaces for best results (specific to embeddings)
63
62
  input = section.gsub(/\n/m, ' ')
64
63
  response = openai.embeddings(parameters: { input: input, model: "text-embedding-ada-002"})
@@ -121,7 +120,7 @@ class Gromit::MarkdownParser
121
120
 
122
121
  def parse_file
123
122
  current_section = []
124
- File.read(@file_path).lines do |line|
123
+ File.read(file_path).lines do |line|
125
124
  # Check if the line is a header (starts with one or more '#' characters)
126
125
  if header?(line)
127
126
  # Save the previous section if it's not empty
@@ -25,6 +25,7 @@ class Gromit::Uploader
25
25
  end.parse!
26
26
 
27
27
  path = Pathname.new(options.fetch(:source_dir, ''))
28
+
28
29
  unless path.exist?
29
30
  puts "Error: The source directory (-s or --source) doesn't exist or is not specified."
30
31
  exit 1
@@ -33,7 +34,7 @@ class Gromit::Uploader
33
34
  sections = Gromit::MarkdownParser.process(path.to_s)
34
35
  sections.each do |section|
35
36
  puts "uploading: #{section[:file]} section: #{section[:section_title]}"
36
- Uploader::Partay.post('/upsert', { headers: {"Content-Type": "application/json"}, body: section.to_json })
37
+ Partay.post('/upsert', { headers: {"Content-Type" => "application/json"}, body: section.to_json })
37
38
  end
38
39
  end
39
40
  end
@@ -1,3 +1,3 @@
1
1
  module Gromit
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/gromit.rb CHANGED
@@ -1,9 +1,24 @@
1
1
  require "gromit/version"
2
2
  require "gromit/engine"
3
+ require "gromit/markdown_parser"
3
4
 
4
5
  module Gromit
5
6
  class Search
6
7
 
8
+ EMBEDDINGS_SCHEMA = {
9
+ id: "TAG",
10
+ page_id: "TAG",
11
+ section_id: "TAG",
12
+ file: "TEXT",
13
+ title: "TEXT",
14
+ content: "TEXT",
15
+ checksum: "TEXT",
16
+ token_count: "NUMERIC",
17
+ embedding: "VECTOR FLAT 6 DIM 1536 DISTANCE_METRIC COSINE TYPE FLOAT64",
18
+ }
19
+
20
+ EMBEDDINGS_PREAMBLE = "FT.CREATE index ON JSON PREFIX 1 item: SCHEMA "
21
+
7
22
  def find_by_embedding(embedding)
8
23
  results = redis.call([
9
24
  "FT.SEARCH", "index", "@embedding:[VECTOR_RANGE $r $BLOB]=>{$YIELD_DISTANCE_AS: my_scores}",
@@ -29,24 +44,12 @@ module Gromit
29
44
  end
30
45
 
31
46
  def create_index
32
- schema = {
33
- id: "TAG",
34
- page_id: "TAG",
35
- section_id: "TAG",
36
- file: "TEXT",
37
- title: "TEXT",
38
- content: "TEXT",
39
- checksum: "TEXT",
40
- token_count: "NUMERIC",
41
- embedding: "VECTOR FLAT 6 DIM 1536 DISTANCE_METRIC COSINE TYPE FLOAT64",
42
- }
43
- preamble = "FT.CREATE index ON JSON PREFIX 1 item: SCHEMA "
44
- command = (preamble + schema.map{|name,type| "$.#{name} AS #{name} #{type}"}.join(" ")).split(" ")
47
+ command = (EMBEDDINGS_PREAMBLE + EMBEDDINGS_SCHEMA.map{|name,type| "$.#{name} AS #{name} #{type}"}.join(" ")).split(" ")
45
48
  redis.call(command)
46
49
  end
47
50
 
48
51
  def redis
49
- @redis ||= Redis.new(host: ENV.fetch("REDIS_HOST") { "127.0.0.1" }, port: ENV.fetch("REDIS_PORT") { "6379" }.to_i)
52
+ @redis ||= Gromit::MarkdownParser.redis
50
53
  end
51
54
 
52
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gromit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Giffin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-07 00:00:00.000000000 Z
11
+ date: 2023-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv