Skip to content

Commit 4758096

Browse files
Update README.md
1 parent dad27ef commit 4758096

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

README.md

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,90 +21,87 @@ The fastest way to sprinkle AI ✨ on top of your Rails app. Add OpenAI-powered
2121
## Installation
2222

2323
Install the gem and add to the application's Gemfile by executing:
24-
25-
bundle add langchainrb_rails
24+
```bash
25+
bundle add langchainrb_rails
26+
```
2627

2728
If bundler is not being used to manage dependencies, install the gem by executing:
29+
```bash
30+
gem install langchainrb_rails
31+
```
2832

29-
gem install langchainrb_rails
30-
31-
## Configuration w/PgVector (requires Postgres 11+)
32-
33-
1. Generate changes to support vectorsearch in your chosen ActiveRecord model
33+
## Configuration w/ [Pgvector](https://github.com/pgvector/pgvector) (requires Postgres 11+)
3434

35+
1. Run the Rails generator to add vectorsearch to your ActiveRecord model
3536
```bash
3637
rails generate langchainrb_rails:pgvector --model=Product --llm=openai
3738
```
3839

39-
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.
4241

42+
2. Bundle and migrate
4343
```bash
4444
bundle install && rails db:migrate
4545
```
4646

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+
```
5051

52+
5. Generate embeddings for your model
5153
```ruby
52-
[YOUR MODEL].embed!
54+
Product.embed!
5355
```
5456

5557
This can take a while depending on the number of database records.
5658

5759
## Usage
5860

5961
### Question and Answering
60-
6162
```ruby
6263
Product.ask("list the brands of shoes that are in stock")
6364
```
6465

6566
Returns a `String` with a natural language answer. The answer is assembled using the following steps:
6667

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
7172

7273
### Similarity Search
73-
7474
```ruby
7575
Product.similarity_search("t-shirt")
7676
```
7777

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.
7979

8080
## Customization
8181

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
8583

84+
By default, embeddings are generated by calling the following method on your model instance:
8685
```ruby
8786
to_json(except: :embedding)
8887
```
8988

9089
You can override this by defining an `#as_vector` method in your model:
91-
9290
```ruby
9391
def as_vector
94-
res = to_json(except: :embedding, :owner_id, :user_id, :category_id)
95-
res.merge({ "owner" => owner.name, "user" => user.name, "category" => category.name })
92+
{ name: name, description: description, category: category.name, ... }.to_json
9693
end
9794
```
9895

9996
Re-generate embeddings after modifying this method:
10097

10198
```ruby
102-
[YOUR MODEL].embed!
99+
Product.embed!
103100
```
104101

105102
## Rails Generators
106103

107-
### PgVector Generator
104+
### Pgvector Generator
108105

109106
```bash
110107
rails generate langchainrb_rails:pgvector --model=Product --llm=openai
@@ -124,7 +121,3 @@ Pinecone Generator does the following:
124121
2. Adds necessary code to the ActiveRecord model to enable vectorsearch
125122
3. Adds `pinecone` gem to the Gemfile
126123

127-
### Chroma Generator - adds vectorsearch to your ActiveRecord model
128-
```bash
129-
rails generate langchainrb_rails:chroma --model=Product --llm=openai
130-
```

0 commit comments

Comments
 (0)