instruct 0.1.0a1

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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +202 -0
  3. data/README.md +387 -0
  4. data/SCRATCHPAD.md +489 -0
  5. data/lib/instruct/compile_erb.rb +39 -0
  6. data/lib/instruct/env.rb +27 -0
  7. data/lib/instruct/error.rb +4 -0
  8. data/lib/instruct/gen/completion_request.rb +63 -0
  9. data/lib/instruct/gen/completion_response.rb +66 -0
  10. data/lib/instruct/gen/gen.rb +70 -0
  11. data/lib/instruct/gen/generate_completion.rb +61 -0
  12. data/lib/instruct/helpers/erb_helper.rb +29 -0
  13. data/lib/instruct/helpers/gen_helper.rb +22 -0
  14. data/lib/instruct/helpers/helpers.rb +9 -0
  15. data/lib/instruct/helpers/model_helper.rb +13 -0
  16. data/lib/instruct/helpers/refinements.rb +54 -0
  17. data/lib/instruct/llms/anthropic/completion_model.rb +107 -0
  18. data/lib/instruct/llms/anthropic/messages_completion_response.rb +35 -0
  19. data/lib/instruct/llms/anthropic/middleware.rb +91 -0
  20. data/lib/instruct/llms/openai/chat_completion_response.rb +21 -0
  21. data/lib/instruct/llms/openai/completion_model.rb +129 -0
  22. data/lib/instruct/llms/openai/completion_response.rb +20 -0
  23. data/lib/instruct/llms/openai/middleware.rb +52 -0
  24. data/lib/instruct/middleware/chat_completion_middleware.rb +90 -0
  25. data/lib/instruct/middleware/chomp_middleware.rb +56 -0
  26. data/lib/instruct/model.rb +21 -0
  27. data/lib/instruct/prompt.rb +217 -0
  28. data/lib/instruct/rails/active_job_object_serializer.rb +23 -0
  29. data/lib/instruct/rails/active_record_coders.rb +36 -0
  30. data/lib/instruct/railtie.rb +15 -0
  31. data/lib/instruct/utils/middleware_chain.rb +48 -0
  32. data/lib/instruct/utils/serializable_with_version.rb +73 -0
  33. data/lib/instruct/utils/serializer.rb +70 -0
  34. data/lib/instruct/utils/symbolize_keys.rb +22 -0
  35. data/lib/instruct/utils/variables.rb +37 -0
  36. data/lib/instruct/version.rb +3 -0
  37. data/lib/instruct.rb +74 -0
  38. metadata +122 -0
@@ -0,0 +1,70 @@
1
+ require 'psych'
2
+ require 'yaml'
3
+
4
+ module Instruct::Serializer
5
+
6
+ class << self
7
+ def dump(prompt_or_completion)
8
+ YAML.dump(prompt_or_completion)
9
+ end
10
+
11
+ def load(prompt_or_completion_bytes, permitted_classes: [])
12
+ permitted_classes = permitted_classes + @@permitted_classes.dup
13
+ # permitted_classes += PERMITTED_CLASSES
14
+ # all instruct classes at load time should be permitted
15
+ doc = Psych.parse(prompt_or_completion_bytes)
16
+ transform_ast(doc, permitted_classes)
17
+ doc.to_ruby
18
+ end
19
+
20
+ def add_permitted_class(klass)
21
+ @@permitted_classes << klass
22
+ end
23
+
24
+ private
25
+
26
+ @@permitted_classes = Instruct.constants.map { |const| Instruct.const_get(const) }.select { |const| const.is_a?(Class) }
27
+
28
+ def transform_ast(node, permitted_classes)
29
+ if node.is_a?(Psych::Nodes::Mapping) && node.tag&.start_with?('!ruby/instruct:')
30
+ class_id, version = node.tag.split(':').last.split('@')
31
+ klass = ClassRegistry.lookup(class_id)
32
+ node.tag = "!ruby/object:#{klass}"
33
+ # add a version integer to the object
34
+ version_node_key = Psych::Nodes::Scalar.new('version')
35
+ version_node_value = Psych::Nodes::Scalar.new(version.to_s) # Ensure it's an integer
36
+ node.children << version_node_key
37
+ node.children << version_node_value
38
+ if klass == nil
39
+ raise ArgumentError, "Class #{klass} not found in serialization registry"
40
+ end
41
+ elsif node.is_a?(Psych::Nodes::Mapping) && node.tag&.start_with?('!ruby/object:')
42
+ klass = node.tag.sub('!ruby/object:', '')
43
+ if !permitted_classes.include?(klass)
44
+ raise ArgumentError, "Class #{klass} not permitted"
45
+ end
46
+ end
47
+
48
+ if node.respond_to?(:children) && node.children
49
+ node.children.each { |child| transform_ast(child, permitted_classes) }
50
+ end
51
+ end
52
+ end
53
+
54
+
55
+
56
+
57
+ module ClassRegistry
58
+ @registry = {}
59
+ def self.register(class_id, current_klass)
60
+ @registry[class_id.to_s] = current_klass
61
+ end
62
+
63
+ def self.lookup(class_id)
64
+ @registry[class_id.to_s]
65
+ end
66
+
67
+ end
68
+
69
+
70
+ end
@@ -0,0 +1,22 @@
1
+ module Instruct
2
+ module SymbolizeKeys
3
+ class << self
4
+ def recursive(hash)
5
+ {}.tap do |h|
6
+ hash.each { |key, value| h[key.to_sym] = map_value(value) }
7
+ end
8
+ end
9
+
10
+ def map_value(thing)
11
+ case thing
12
+ when Hash
13
+ recursive(thing)
14
+ when Array
15
+ thing.map { |v| map_value(v) }
16
+ else
17
+ thing
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ # Provides a simple hash-like interface for storing variables in LM.
2
+ class Instruct::LM
3
+ module Variables
4
+ attr_reader :variables
5
+
6
+ def [](key)
7
+ raise ArgumentError, "key must not be nil" if key.nil?
8
+ key = key.to_sym
9
+ @variables[key]
10
+ end
11
+
12
+ private
13
+
14
+ def []=(key, value)
15
+ key = key.to_sym
16
+ @variables[key] = value
17
+ end
18
+
19
+ def capture_result_in_variable(response, arr_name: nil, name: nil, **kwargs)
20
+ if arr_name
21
+ self[arr_name] ||= []
22
+ self[arr_name] << response
23
+ end
24
+ if name
25
+ self[name] = response
26
+ end
27
+ end
28
+
29
+ def dup_variables
30
+ @variables.dup
31
+ end
32
+
33
+ def initialize_variables(variables = {})
34
+ @variables = variables.dup
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Instruct
2
+ VERSION = "0.1.0a1"
3
+ end
data/lib/instruct.rb ADDED
@@ -0,0 +1,74 @@
1
+ require_relative "instruct/version"
2
+
3
+ # stdlib
4
+ require 'erb'
5
+ require 'ostruct'
6
+
7
+ # dependencies
8
+ require 'attributed-string'
9
+ # require 'async/http/faraday'
10
+
11
+
12
+
13
+ # utils
14
+ require_relative "instruct/utils/serializer"
15
+ require_relative "instruct/utils/serializable_with_version"
16
+ require_relative "instruct/utils/middleware_chain"
17
+ require_relative "instruct/utils/symbolize_keys"
18
+ require_relative "instruct/utils/variables"
19
+
20
+ # modules
21
+
22
+ require_relative "instruct/compile_erb"
23
+ require_relative "instruct/env"
24
+ require_relative "instruct/error"
25
+ require_relative "instruct/model"
26
+ require_relative "instruct/gen/completion_response"
27
+ require_relative "instruct/gen/completion_request"
28
+ require_relative "instruct/gen/generate_completion"
29
+ require_relative "instruct/gen/gen"
30
+ require_relative "instruct/middleware/chat_completion_middleware"
31
+ require_relative "instruct/middleware/chomp_middleware"
32
+ require_relative "instruct/prompt"
33
+
34
+ require_relative "instruct/helpers/erb_helper"
35
+ require_relative "instruct/helpers/gen_helper"
36
+ require_relative "instruct/helpers/model_helper"
37
+ require_relative "instruct/helpers/refinements"
38
+ require_relative "instruct/helpers/helpers"
39
+
40
+ # optional dependencies
41
+ begin
42
+ require 'rainbow'
43
+ rescue LoadError
44
+ end
45
+
46
+ # if the caller is using ruby-openai gem
47
+ begin
48
+ require "ruby/openai"
49
+ rescue LoadError
50
+ end
51
+
52
+ if defined? ::OpenAI
53
+ require_relative "instruct/llms/openai/middleware"
54
+ require_relative "instruct/llms/openai/completion_model"
55
+ require_relative "instruct/llms/openai/completion_response"
56
+ require_relative "instruct/llms/openai/chat_completion_response"
57
+ Instruct.openai_loaded = true
58
+ else
59
+ end
60
+
61
+ # if the caller is using ruby/anthropic gem
62
+ begin require "anthropic"
63
+ rescue LoadError
64
+ end
65
+
66
+ if defined? ::Anthropic
67
+ require_relative "instruct/llms/anthropic/middleware"
68
+ require_relative "instruct/llms/anthropic/completion_model"
69
+ require_relative "instruct/llms/anthropic/messages_completion_response"
70
+ Instruct.anthropic_loaded = true
71
+ else
72
+ end
73
+
74
+ require "instruct/railtie" if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instruct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0a1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Mackross
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ostruct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mutex_m
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: attributed-string
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email: andrew@mackross.net
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - LICENSE
62
+ - README.md
63
+ - SCRATCHPAD.md
64
+ - lib/instruct.rb
65
+ - lib/instruct/compile_erb.rb
66
+ - lib/instruct/env.rb
67
+ - lib/instruct/error.rb
68
+ - lib/instruct/gen/completion_request.rb
69
+ - lib/instruct/gen/completion_response.rb
70
+ - lib/instruct/gen/gen.rb
71
+ - lib/instruct/gen/generate_completion.rb
72
+ - lib/instruct/helpers/erb_helper.rb
73
+ - lib/instruct/helpers/gen_helper.rb
74
+ - lib/instruct/helpers/helpers.rb
75
+ - lib/instruct/helpers/model_helper.rb
76
+ - lib/instruct/helpers/refinements.rb
77
+ - lib/instruct/llms/anthropic/completion_model.rb
78
+ - lib/instruct/llms/anthropic/messages_completion_response.rb
79
+ - lib/instruct/llms/anthropic/middleware.rb
80
+ - lib/instruct/llms/openai/chat_completion_response.rb
81
+ - lib/instruct/llms/openai/completion_model.rb
82
+ - lib/instruct/llms/openai/completion_response.rb
83
+ - lib/instruct/llms/openai/middleware.rb
84
+ - lib/instruct/middleware/chat_completion_middleware.rb
85
+ - lib/instruct/middleware/chomp_middleware.rb
86
+ - lib/instruct/model.rb
87
+ - lib/instruct/prompt.rb
88
+ - lib/instruct/rails/active_job_object_serializer.rb
89
+ - lib/instruct/rails/active_record_coders.rb
90
+ - lib/instruct/railtie.rb
91
+ - lib/instruct/utils/middleware_chain.rb
92
+ - lib/instruct/utils/serializable_with_version.rb
93
+ - lib/instruct/utils/serializer.rb
94
+ - lib/instruct/utils/symbolize_keys.rb
95
+ - lib/instruct/utils/variables.rb
96
+ - lib/instruct/version.rb
97
+ homepage: https://instruct-rb.com
98
+ licenses:
99
+ - Apache-2.0
100
+ metadata:
101
+ source_code_uri: https://github.com/instruct-rb/instruct
102
+ homepage_uri: https://instruct-rb.com
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 3.2.3
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.5.16
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Instruct LLMs to do what you want
122
+ test_files: []