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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb8dc7d40d37072877b376103879593077531cecc15e4487197892eea0101c0f
4
- data.tar.gz: d49614a734f1add1ead4e75d71fdead7b20437a41ed1a01d85780766dd75dd81
3
+ metadata.gz: 38f4062b8cf5eb13c2ed44821bff90b6a29dbfbc3a39d5c38c052f19b8df7fb6
4
+ data.tar.gz: e8b167d59f1764d3c2f8aff7edac1d51a0990b9971e281b3d42ce531129762cc
5
5
  SHA512:
6
- metadata.gz: a847b3db61c0e705b06208061288690da66d2036449be89519d146b304a657ac31e1d2bd88dcae0c2ee91d8471e768b3ef7a8576222e3884199b15b9082bbb9e
7
- data.tar.gz: eae9012d1602ad2cedf44b5173edc08eda4c17c3081a1b88808989bc1eee7540b36a2a2d01839e6a01076cab4ec26829bc569cc5a5044aa0fcb07651b0bbf66f
6
+ metadata.gz: 57cc603b6f20f8d66270680d2243583ce1cac57d3b36f849412fdb4c8ec77dd8830579954256aa02f9693753e088ab63c1f6e3b64aed35719f66eb9fa68c5430
7
+ data.tar.gz: 607857292395ca97abbe6754ac5a896df79a4dfb2333e269a03be5dcdcdacfcbf9188c02d4eb09f387e66aebcfb6cce0792c39d5cbc5cff9625b9663681efa85
data/Gemfile CHANGED
@@ -18,6 +18,7 @@ gem "nokogiri"
18
18
  gem "docx"
19
19
  gem "roo"
20
20
  gem "baran"
21
+ gem "async"
21
22
 
22
23
  gem "pry"
23
24
 
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 is used for global public documents
74
+ namespace: user_id # default is nil, nil assumes global public documents
75
75
  )
76
76
  ```
77
77
 
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_dependency "nokogiri", "~> 1.8"
33
33
  spec.add_dependency "docx", "~> 0.8.0"
34
34
  spec.add_dependency "roo", "~> 2.8.3"
35
+ spec.add_dependency "async", "~> 1.31.0"
35
36
  end
@@ -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
- data = texts.map do |text|
73
- {content: text, vectors: llm.embed(text: text).to_s, namespace: namespaces[0]}
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BxBuilderChain
4
- VERSION = "0.1.12"
4
+ VERSION = "0.1.13"
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require "logger"
4
4
  require 'dotenv'
5
5
  require "zeitwerk"
6
+ require "async"
6
7
  require_relative 'bx_builder_chain/configuration'
7
8
 
8
9
  Dotenv.load
@@ -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
- private
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: llm_class_name.constantize.new(
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.12
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: 2023-09-29 00:00:00.000000000 Z
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