langchainrb_rails 0.1.0
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 +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +114 -0
- data/LICENSE.txt +21 -0
- data/README.md +32 -0
- data/Rakefile +8 -0
- data/lib/langchainrb_rails/active_record/hooks.rb +112 -0
- data/lib/langchainrb_rails/railtie.rb +11 -0
- data/lib/langchainrb_rails/version.rb +5 -0
- data/lib/langchainrb_rails.rb +13 -0
- data/sig/langchainrb_rails.rbs +4 -0
- metadata +101 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9c1e0233e3c6a9b90521be85e3ce43bb0eafab6f148cc5e7ce3183493d0be423
|
|
4
|
+
data.tar.gz: 20e683575cbc95f361df5c642fdb922638d541ffbcb8018907a76305243b852b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 310d63172807b8cb6fcb58056fce57800a6c074423b5cd880b6ca4fd9900b44f657b8bd7dfd39de8aefc30085a257c8467708a3afd4d6cbfafe3949f5970e5d6
|
|
7
|
+
data.tar.gz: 5b721ba6d1801760779d94e66718f6de0e0e3b242814419fd4fc664b2ba335aa585b53de706d6e44176b1609f6ff7e5be8dc3c73b114bedd34cb7b99aa4f2fd7
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
langchainrb_rails (0.1.0)
|
|
5
|
+
langchainrb (~> 0.6.19)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
addressable (2.8.5)
|
|
11
|
+
public_suffix (>= 2.0.2, < 6.0)
|
|
12
|
+
ast (2.4.2)
|
|
13
|
+
baran (0.1.9)
|
|
14
|
+
base64 (0.1.1)
|
|
15
|
+
byebug (11.1.3)
|
|
16
|
+
coderay (1.1.3)
|
|
17
|
+
colorize (0.8.1)
|
|
18
|
+
diff-lcs (1.5.0)
|
|
19
|
+
json (2.6.3)
|
|
20
|
+
json-schema (4.0.0)
|
|
21
|
+
addressable (>= 2.8)
|
|
22
|
+
langchainrb (0.6.19)
|
|
23
|
+
baran (~> 0.1.9)
|
|
24
|
+
colorize (~> 0.8.1)
|
|
25
|
+
json-schema (~> 4.0.0)
|
|
26
|
+
pragmatic_segmenter (~> 0.3.0)
|
|
27
|
+
tiktoken_ruby (~> 0.0.5)
|
|
28
|
+
zeitwerk (~> 2.5)
|
|
29
|
+
language_server-protocol (3.17.0.3)
|
|
30
|
+
lint_roller (1.1.0)
|
|
31
|
+
method_source (1.0.0)
|
|
32
|
+
parallel (1.23.0)
|
|
33
|
+
parser (3.2.2.4)
|
|
34
|
+
ast (~> 2.4.1)
|
|
35
|
+
racc
|
|
36
|
+
pragmatic_segmenter (0.3.23)
|
|
37
|
+
unicode
|
|
38
|
+
pry (0.14.2)
|
|
39
|
+
coderay (~> 1.1)
|
|
40
|
+
method_source (~> 1.0)
|
|
41
|
+
pry-byebug (3.10.1)
|
|
42
|
+
byebug (~> 11.0)
|
|
43
|
+
pry (>= 0.13, < 0.15)
|
|
44
|
+
public_suffix (5.0.3)
|
|
45
|
+
racc (1.7.1)
|
|
46
|
+
rainbow (3.1.1)
|
|
47
|
+
rake (13.0.6)
|
|
48
|
+
regexp_parser (2.8.2)
|
|
49
|
+
rexml (3.2.6)
|
|
50
|
+
rspec (3.12.0)
|
|
51
|
+
rspec-core (~> 3.12.0)
|
|
52
|
+
rspec-expectations (~> 3.12.0)
|
|
53
|
+
rspec-mocks (~> 3.12.0)
|
|
54
|
+
rspec-core (3.12.2)
|
|
55
|
+
rspec-support (~> 3.12.0)
|
|
56
|
+
rspec-expectations (3.12.3)
|
|
57
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
58
|
+
rspec-support (~> 3.12.0)
|
|
59
|
+
rspec-mocks (3.12.6)
|
|
60
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
61
|
+
rspec-support (~> 3.12.0)
|
|
62
|
+
rspec-support (3.12.1)
|
|
63
|
+
rubocop (1.56.4)
|
|
64
|
+
base64 (~> 0.1.1)
|
|
65
|
+
json (~> 2.3)
|
|
66
|
+
language_server-protocol (>= 3.17.0)
|
|
67
|
+
parallel (~> 1.10)
|
|
68
|
+
parser (>= 3.2.2.3)
|
|
69
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
70
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
71
|
+
rexml (>= 3.2.5, < 4.0)
|
|
72
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
|
73
|
+
ruby-progressbar (~> 1.7)
|
|
74
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
|
75
|
+
rubocop-ast (1.29.0)
|
|
76
|
+
parser (>= 3.2.1.0)
|
|
77
|
+
rubocop-performance (1.19.1)
|
|
78
|
+
rubocop (>= 1.7.0, < 2.0)
|
|
79
|
+
rubocop-ast (>= 0.4.0)
|
|
80
|
+
ruby-progressbar (1.13.0)
|
|
81
|
+
standard (1.31.2)
|
|
82
|
+
language_server-protocol (~> 3.17.0.2)
|
|
83
|
+
lint_roller (~> 1.0)
|
|
84
|
+
rubocop (~> 1.56.4)
|
|
85
|
+
standard-custom (~> 1.0.0)
|
|
86
|
+
standard-performance (~> 1.2)
|
|
87
|
+
standard-custom (1.0.2)
|
|
88
|
+
lint_roller (~> 1.0)
|
|
89
|
+
rubocop (~> 1.50)
|
|
90
|
+
standard-performance (1.2.1)
|
|
91
|
+
lint_roller (~> 1.1)
|
|
92
|
+
rubocop-performance (~> 1.19.1)
|
|
93
|
+
standardrb (1.0.1)
|
|
94
|
+
standard
|
|
95
|
+
tiktoken_ruby (0.0.6-x86_64-darwin)
|
|
96
|
+
unicode (0.4.4.4)
|
|
97
|
+
unicode-display_width (2.5.0)
|
|
98
|
+
yard (0.9.34)
|
|
99
|
+
zeitwerk (2.6.11)
|
|
100
|
+
|
|
101
|
+
PLATFORMS
|
|
102
|
+
x86_64-darwin-19
|
|
103
|
+
|
|
104
|
+
DEPENDENCIES
|
|
105
|
+
langchainrb
|
|
106
|
+
langchainrb_rails!
|
|
107
|
+
pry-byebug (~> 3.10.0)
|
|
108
|
+
rake (~> 13.0)
|
|
109
|
+
rspec (~> 3.0)
|
|
110
|
+
standardrb
|
|
111
|
+
yard (~> 0.9.34)
|
|
112
|
+
|
|
113
|
+
BUNDLED WITH
|
|
114
|
+
2.4.0
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Andrei Bondarev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
💎🔗 Langchain.rb for Rails
|
|
2
|
+
---
|
|
3
|
+
⚡ Building applications with LLMs through composability ⚡
|
|
4
|
+
|
|
5
|
+
👨💻👩💻 CURRENTLY SEEKING PEOPLE TO FORM THE CORE GROUP OF MAINTAINERS WITH
|
|
6
|
+
|
|
7
|
+
:warning: UNDER ACTIVE AND RAPID DEVELOPMENT (MAY BE BUGGY AND UNTESTED)
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
[](https://badge.fury.io/rb/langchainrb_rails)
|
|
11
|
+
[](http://rubydoc.info/gems/langchainrb_rails)
|
|
12
|
+
[](https://github.com/andreibondarev/langchainrb_rails/blob/main/LICENSE.txt)
|
|
13
|
+
[](https://discord.gg/WDARp7J2n8)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
Langchain.rb is a library that's an abstraction layer on top many emergent AI, ML and other DS tools. The goal is to abstract complexity and difficult concepts to make building AI/ML-supercharged applications approachable for traditional software engineers.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
21
|
+
|
|
22
|
+
bundle add langchainrb_rails
|
|
23
|
+
|
|
24
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
25
|
+
|
|
26
|
+
gem install langchainrb_rails
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
require "langchainrb_rails"
|
|
32
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LangchainrbRails
|
|
4
|
+
module ActiveRecord
|
|
5
|
+
# This module adds the following functionality to your ActiveRecord models:
|
|
6
|
+
# * `vectorsearch` class method to set the vector search provider
|
|
7
|
+
# * `similarity_search` class method to search for similar texts
|
|
8
|
+
# * `upsert_to_vectorsearch` instance method to upsert the record to the vector search provider
|
|
9
|
+
#
|
|
10
|
+
# Usage:
|
|
11
|
+
# class Recipe < ActiveRecord::Base
|
|
12
|
+
# vectorsearch provider: Langchain::Vectorsearch::Weaviate.new(
|
|
13
|
+
# api_key: ENV["WEAVIATE_API_KEY"],
|
|
14
|
+
# url: ENV["WEAVIATE_URL"],
|
|
15
|
+
# index_name: "Recipes",
|
|
16
|
+
# llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
|
|
17
|
+
# )
|
|
18
|
+
#
|
|
19
|
+
# after_save :upsert_to_vectorsearch
|
|
20
|
+
#
|
|
21
|
+
# # Overwriting how the model is serialized before it's indexed
|
|
22
|
+
# def as_vector
|
|
23
|
+
# [
|
|
24
|
+
# "Title: #{title}",
|
|
25
|
+
# "Description: #{description}",
|
|
26
|
+
# ...
|
|
27
|
+
# ]
|
|
28
|
+
# .compact
|
|
29
|
+
# .join("\n")
|
|
30
|
+
# end
|
|
31
|
+
# end
|
|
32
|
+
#
|
|
33
|
+
# Create the default schema
|
|
34
|
+
# Recipe.class_variable_get(:@@provider).create_default_schema
|
|
35
|
+
# Query the vector search provider
|
|
36
|
+
# Recipe.similarity_search("carnivore dish")
|
|
37
|
+
# Delete the default schema to start over
|
|
38
|
+
# Recipe.class_variable_get(:@@provider).destroy_default_schema
|
|
39
|
+
#
|
|
40
|
+
module Hooks
|
|
41
|
+
def self.included(base)
|
|
42
|
+
base.extend ClassMethods
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Index the text to the vector search provider
|
|
46
|
+
# You'd typically call this method in an ActiveRecord callback
|
|
47
|
+
#
|
|
48
|
+
# @return [Boolean] true
|
|
49
|
+
# @raise [Error] Indexing to vector search DB failed
|
|
50
|
+
def upsert_to_vectorsearch
|
|
51
|
+
if previously_new_record?
|
|
52
|
+
self.class.class_variable_get(:@@provider).add_texts(
|
|
53
|
+
texts: [as_vector],
|
|
54
|
+
ids: [id]
|
|
55
|
+
)
|
|
56
|
+
else
|
|
57
|
+
self.class.class_variable_get(:@@provider).update_texts(
|
|
58
|
+
texts: [as_vector],
|
|
59
|
+
ids: [id]
|
|
60
|
+
)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Used to serialize the DB record to an indexable vector text
|
|
65
|
+
# Overwrite this method in your model to customize
|
|
66
|
+
#
|
|
67
|
+
# @return [String] the text representation of the model
|
|
68
|
+
def as_vector
|
|
69
|
+
to_json
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
module ClassMethods
|
|
73
|
+
# Set the vector search provider
|
|
74
|
+
#
|
|
75
|
+
# @param provider [Object] The `Langchain::Vectorsearch::*` instance
|
|
76
|
+
def vectorsearch(provider:)
|
|
77
|
+
class_variable_set(:@@provider, provider)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Search for similar texts
|
|
81
|
+
#
|
|
82
|
+
# @param query [String] The query to search for
|
|
83
|
+
# @param k [Integer] The number of results to return
|
|
84
|
+
# @return [ActiveRecord::Relation] The ActiveRecord relation
|
|
85
|
+
def similarity_search(query, k: 1)
|
|
86
|
+
records = class_variable_get(:@@provider).similarity_search(
|
|
87
|
+
query: query,
|
|
88
|
+
k: k
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
# We use "__id" when Weaviate is the provider
|
|
92
|
+
ids = records.map { |record| record.dig("id") || record.dig("__id") }
|
|
93
|
+
where(id: ids)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Ask a question and return the answer
|
|
97
|
+
#
|
|
98
|
+
# @param question [String] The question to ask
|
|
99
|
+
# @param k [Integer] The number of results to have in context
|
|
100
|
+
# @yield [String] Stream responses back one String at a time
|
|
101
|
+
# @return [String] The answer to the question
|
|
102
|
+
def ask(question:, k: 4, &block)
|
|
103
|
+
class_variable_get(:@@provider).ask(
|
|
104
|
+
question: question,
|
|
105
|
+
k: k,
|
|
106
|
+
&block
|
|
107
|
+
)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "langchain"
|
|
4
|
+
require_relative "langchainrb_rails/version"
|
|
5
|
+
require "langchainrb_rails/railtie"
|
|
6
|
+
|
|
7
|
+
module LangchainrbRails
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
|
|
10
|
+
module ActiveRecord
|
|
11
|
+
autoload :Hooks, "langchainrb_rails/active_record/hooks"
|
|
12
|
+
end
|
|
13
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: langchainrb_rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andrei Bondarev
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-10-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: langchainrb
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.6.19
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.6.19
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: pry-byebug
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 3.10.0
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 3.10.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: yard
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 0.9.34
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.9.34
|
|
55
|
+
description: Rails wrapper for langchainrb gem
|
|
56
|
+
email:
|
|
57
|
+
- andrei.bondarev13@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".rspec"
|
|
63
|
+
- CHANGELOG.md
|
|
64
|
+
- Gemfile
|
|
65
|
+
- Gemfile.lock
|
|
66
|
+
- LICENSE.txt
|
|
67
|
+
- README.md
|
|
68
|
+
- Rakefile
|
|
69
|
+
- lib/langchainrb_rails.rb
|
|
70
|
+
- lib/langchainrb_rails/active_record/hooks.rb
|
|
71
|
+
- lib/langchainrb_rails/railtie.rb
|
|
72
|
+
- lib/langchainrb_rails/version.rb
|
|
73
|
+
- sig/langchainrb_rails.rbs
|
|
74
|
+
homepage: https://rubygems.org/gems/langchainrb_rails
|
|
75
|
+
licenses:
|
|
76
|
+
- MIT
|
|
77
|
+
metadata:
|
|
78
|
+
homepage_uri: https://rubygems.org/gems/langchainrb_rails
|
|
79
|
+
source_code_uri: https://github.com/andreibondarev/langchainrb_rails
|
|
80
|
+
changelog_uri: https://github.com/andreibondarev/langchainrb_rails/CHANGELOG.md
|
|
81
|
+
documentation_uri: https://rubydoc.info/gems/langchainrb_rails
|
|
82
|
+
post_install_message:
|
|
83
|
+
rdoc_options: []
|
|
84
|
+
require_paths:
|
|
85
|
+
- lib
|
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: 3.0.0
|
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
requirements: []
|
|
97
|
+
rubygems_version: 3.2.3
|
|
98
|
+
signing_key:
|
|
99
|
+
specification_version: 4
|
|
100
|
+
summary: Rails wrapper for langchainrb gem
|
|
101
|
+
test_files: []
|