You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This adds required dependencies to your Gemfile, creates the initializer file `config/initializers/langchainrb_rails.rb`, database migrations to support vectorsearch, and adds the necessary code to the ActiveRecord model to enable vectorsearch.
40
-
41
-
2. Bundle && Migrate
40
+
This adds required dependencies to your Gemfile, creates the `config/initializers/langchainrb_rails.rb` initializer file, database migrations, and adds the necessary code to the ActiveRecord model to enable vectorsearch.
42
41
42
+
2. Bundle and migrate
43
43
```bash
44
44
bundle install && rails db:migrate
45
45
```
46
46
47
-
3. Set the env var `OPENAI_API_KEY` to your OpenAI API key.
48
-
49
-
4. Generate embeddings for your model
47
+
3. Set the env var `OPENAI_API_KEY` to your OpenAI API key: https://platform.openai.com/account/api-keys
48
+
```ruby
49
+
ENV["OPENAI_API_KEY"]=
50
+
```
50
51
52
+
5. Generate embeddings for your model
51
53
```ruby
52
-
[YOURMODEL].embed!
54
+
Product.embed!
53
55
```
54
56
55
57
This can take a while depending on the number of database records.
56
58
57
59
## Usage
58
60
59
61
### Question and Answering
60
-
61
62
```ruby
62
63
Product.ask("list the brands of shoes that are in stock")
63
64
```
64
65
65
66
Returns a `String` with a natural language answer. The answer is assembled using the following steps:
66
67
67
-
1.Turn the `question` into an embedding using the selected LLM.
68
-
2.Find records that most closely match the question using Postgres vector similarity search (#similarity_search).
69
-
3.Create a prompt using the question and insert the records (via`#as_vector`) into the prompt as context.
70
-
4.Generate a completion using the prompt and the selected LLM.
68
+
1.An embedding is generated for the passed in `question` using the selected LLM.
69
+
2.We calculate a [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity) to find records that most closely match your question's embedding.
70
+
3.A prompt is created using the question and the above records (their`#as_vector` representation )are added as context.
71
+
4.This prompt is passed to the LLM to generate an answer
71
72
72
73
### Similarity Search
73
-
74
74
```ruby
75
75
Product.similarity_search("t-shirt")
76
76
```
77
77
78
-
Returns ActiveRecord records that most closely match the `query` using Postgres vector similarity search.
78
+
Returns ActiveRecord relation that most closely matches the `query` using vector search.
79
79
80
80
## Customization
81
81
82
-
## Changing the vector representation of a record
83
-
84
-
By default, embeddings are generated by calling the following within your model:
82
+
### Changing the vector representation of a record
85
83
84
+
By default, embeddings are generated by calling the following method on your model instance:
86
85
```ruby
87
86
to_json(except::embedding)
88
87
```
89
88
90
89
You can override this by defining an `#as_vector` method in your model:
91
-
92
90
```ruby
93
91
defas_vector
94
-
res = to_json(except::embedding, :owner_id, :user_id, :category_id)
0 commit comments