bx_builder_chain 0.1.12 → 0.1.13
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 +4 -4
- data/Gemfile +1 -0
- data/README.md +1 -1
- data/bx_builder_chain.gemspec +1 -0
- data/lib/bx_builder_chain/vectorsearch/pgvector.rb +6 -3
- data/lib/bx_builder_chain/version.rb +1 -1
- data/lib/bx_builder_chain.rb +1 -0
- data/lib/generators/bx_builder_chain/templates/app/services/bx_builder_chain/question_asking_service.rb +43 -8
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38f4062b8cf5eb13c2ed44821bff90b6a29dbfbc3a39d5c38c052f19b8df7fb6
|
4
|
+
data.tar.gz: e8b167d59f1764d3c2f8aff7edac1d51a0990b9971e281b3d42ce531129762cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57cc603b6f20f8d66270680d2243583ce1cac57d3b36f849412fdb4c8ec77dd8830579954256aa02f9693753e088ab63c1f6e3b64aed35719f66eb9fa68c5430
|
7
|
+
data.tar.gz: 607857292395ca97abbe6754ac5a896df79a4dfb2333e269a03be5dcdcdacfcbf9188c02d4eb09f387e66aebcfb6cce0792c39d5cbc5cff9625b9663681efa85
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -71,7 +71,7 @@ create the llm and client
|
|
71
71
|
```ruby
|
72
72
|
client = BxBuilderChain::Vectorsearch::Pgvector.new(
|
73
73
|
llm: BxBuilderChain::Llm::OpenAi.new,
|
74
|
-
namespace: user_id # default is nil, nil
|
74
|
+
namespace: user_id # default is nil, nil assumes global public documents
|
75
75
|
)
|
76
76
|
```
|
77
77
|
|
data/bx_builder_chain.gemspec
CHANGED
@@ -69,10 +69,13 @@ module BxBuilderChain::Vectorsearch
|
|
69
69
|
# @return [Array<Integer>] The the ids of the added texts.
|
70
70
|
def add_texts(texts:, ids: nil)
|
71
71
|
if ids.nil? || ids.empty?
|
72
|
-
|
73
|
-
|
72
|
+
Async do
|
73
|
+
data = texts.map do |text|
|
74
|
+
Async do
|
75
|
+
{content: text, vectors: llm.embed(text: text).to_s, namespace: namespaces[0]}
|
76
|
+
end
|
77
|
+
end
|
74
78
|
end
|
75
|
-
|
76
79
|
@db[@table_name.to_sym].multi_insert(data, return: :primary_key)
|
77
80
|
else
|
78
81
|
upsert_texts(texts: texts, ids: ids)
|
data/lib/bx_builder_chain.rb
CHANGED
@@ -2,12 +2,13 @@ module BxBuilderChain
|
|
2
2
|
class QuestionAskingService
|
3
3
|
attr_reader :question, :user_groups, :llm_class_name, :client_class_name, :context_results
|
4
4
|
|
5
|
-
def initialize(question:, user_groups: ['public'], client_class_name: 'BxBuilderChain::Vectorsearch::Pgvector', llm_class_name: 'BxBuilderChain::Llm::OpenAi', context_results: 6)
|
5
|
+
def initialize(question:, user_groups: ['public'], client_class_name: 'BxBuilderChain::Vectorsearch::Pgvector', llm_class_name: 'BxBuilderChain::Llm::OpenAi', context_results: 6, model: "gpt-3.5-turbo-16k")
|
6
6
|
@question = question
|
7
7
|
@user_groups = user_groups
|
8
8
|
@client_class_name = client_class_name
|
9
9
|
@llm_class_name = llm_class_name
|
10
10
|
@context_results = context_results
|
11
|
+
@model = model
|
11
12
|
end
|
12
13
|
|
13
14
|
def ask
|
@@ -16,20 +17,54 @@ module BxBuilderChain
|
|
16
17
|
response = client.ask(question: question, context_results: context_results)
|
17
18
|
{ answer: response }
|
18
19
|
end
|
20
|
+
|
21
|
+
# history in the format of
|
22
|
+
# [{role:'human', message:'user message', {role:'ai',message:'some text'}, .....]
|
23
|
+
def chat(history = [])
|
24
|
+
search_results = client.similarity_search(query: @question, k: @context_results)
|
25
|
+
|
26
|
+
context = search_results.map do |result|
|
27
|
+
result.content.to_s
|
28
|
+
end
|
29
|
+
context = context.join("\n---\n")
|
19
30
|
|
20
|
-
|
31
|
+
prompt = client.generate_prompt(question: @question, context: context, prompt_template: nil)
|
21
32
|
|
33
|
+
llm.chat(prompt:prompt, messages: reduce_history(history))
|
34
|
+
end
|
35
|
+
|
36
|
+
def reduce_history(history)
|
37
|
+
current_length = 0
|
38
|
+
limit = tokenizer.class::TOKEN_LIMITS["gpt-3.5-turbo-16k"] * 0.75
|
39
|
+
history_overflow, @reduced_history = @history.partition do |msg|
|
40
|
+
current_length += tokenizer.token_length(msg[:message], @current_model)
|
41
|
+
current_length > limit
|
42
|
+
end
|
43
|
+
|
44
|
+
reduced_history
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def tokenizer
|
50
|
+
@tokenizer ||= llm.class::LENGTH_VALIDATOR
|
51
|
+
end
|
52
|
+
|
22
53
|
def client
|
23
54
|
@client ||= client_class_name.constantize.new(
|
24
|
-
llm:
|
25
|
-
default_options: {
|
26
|
-
chat_completion_model_name: "gpt-3.5-turbo",
|
27
|
-
temperature: 0.2
|
28
|
-
}
|
29
|
-
),
|
55
|
+
llm: llm,
|
30
56
|
namespaces: user_groups
|
31
57
|
)
|
32
58
|
end
|
59
|
+
|
60
|
+
def llm
|
61
|
+
@llm ||= llm_class_name.constantize.new(
|
62
|
+
default_options: {
|
63
|
+
chat_completion_model_name: "gpt-3.5-turbo-16k",
|
64
|
+
temperature: 0.2
|
65
|
+
}
|
66
|
+
)
|
67
|
+
end
|
33
68
|
end
|
34
69
|
end
|
35
70
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bx_builder_chain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Ketelle
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|
@@ -150,6 +150,20 @@ dependencies:
|
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: 2.8.3
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: async
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 1.31.0
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 1.31.0
|
153
167
|
description: LangChain but for the builder tech stack.
|
154
168
|
email:
|
155
169
|
- paul.ketelle@builder.ai
|