promptspec 0.0.5 → 0.0.7

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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/promptspec.rb +24 -20
  3. data/promptspec.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8344f339aa0ef8a57c14422dc2f50046a203076ec9c2daee525c4432f57f6bf
4
- data.tar.gz: 96014c0bc00da57d4b0289df8c39fa4a3c8505dc6a0b6814a717a31f735ad591
3
+ metadata.gz: 02cda944eeaf09e58bd5ce6735e6c3f04346624d40be692d6de61b584717846c
4
+ data.tar.gz: acac7f476102409d6b1b0f9f277a8ccd00de474ffdd2044f4469960fb221f0f8
5
5
  SHA512:
6
- metadata.gz: 1516fbdb9607f0724faa3c10081269b3d99c243145b1f31a19588faec6a5b7602120d536acba56fd1629b7afc1b6bd9f2d25f7cd18e4491687bceb66964c0534
7
- data.tar.gz: 611b943f18480666c2d9da0a157eb186f0a4392ee876897af67d4c026829391a701882abcf19c1e9343eca7e8d35aee2131de888887537fbbf0fd96e075caf37
6
+ metadata.gz: 6a665777ac2a33223b7104dce51e23dd0420c37d72973bbd4fe3b32126cecba11c4e614ce3981a0b107ceb3df6a79ff3825f1bf6fea9a2ee50921d906555be2c
7
+ data.tar.gz: f8573b02222b519c0b79ee3c62385f4fcd49a1ef90d034e3c3ad32edf345a1dabb4efb1af1c24987ad978196c774427114d656f65fd82bf20b7e9a3e84d88b62
data/lib/promptspec.rb CHANGED
@@ -6,7 +6,7 @@ require 'net/http'
6
6
  require 'uri'
7
7
 
8
8
  class PromptSpec
9
- attr_reader :file_path, :validate_required_params
9
+ attr_reader :file_path, :validate_required_params, :parameters, :yaml_content
10
10
 
11
11
  def initialize(file_path, validate_required_params: true)
12
12
  @file_path = file_path
@@ -15,8 +15,9 @@ class PromptSpec
15
15
  end
16
16
 
17
17
  def call(**parameters)
18
- validate_required_inputs!(parameters) if @validate_required_params
19
- parse_prompt_messages(parameters)
18
+ @parameters = parameters
19
+ validate_required_inputs! if @validate_required_params
20
+ parse_prompt_messages
20
21
  construct_endpoint_request
21
22
  end
22
23
 
@@ -30,25 +31,24 @@ class PromptSpec
30
31
  raise ParseError, "YAML parsing error: #{e.message}"
31
32
  end
32
33
 
33
- def validate_required_inputs!(parameters)
34
+ def validate_required_inputs!
34
35
  required_params = @yaml_content.dig('parameters', 'required') || []
35
- missing_params = required_params - parameters.keys.map(&:to_s)
36
+ missing_params = required_params - @parameters.keys.map(&:to_s)
36
37
  raise RequiredParameterError, "Missing required parameters: #{missing_params.join(', ')}" unless missing_params.empty?
37
38
  end
38
39
 
39
- def parse_prompt_messages(parameters)
40
- messages = @yaml_content['prompt']['messages'].map do |message|
40
+ def parse_prompt_messages
41
+ @yaml_content['prompt']['messages'].each do |message|
41
42
  content = message['content']
42
- parameters.each do |key, value|
43
- content.gsub!("{#{key}}", value.to_s)
43
+ @parameters.each do |key, value|
44
+ content.gsub!("{#{key}}", value.to_s) if content.include?("{#{key}}")
44
45
  end
45
- { role: message['role'], content: content }
46
+ message['content'] = content
46
47
  end
47
- @yaml_content['prompt']['messages'] = messages
48
48
  end
49
49
 
50
50
  def construct_endpoint_request
51
- endpoint = @yaml_content['endpoint'] || construct_default_endpoint
51
+ endpoint = @yaml_content['url'] || construct_default_endpoint
52
52
  headers = construct_headers
53
53
  payload = @yaml_content['prompt']
54
54
 
@@ -67,7 +67,7 @@ class PromptSpec
67
67
  end
68
68
 
69
69
  def construct_default_endpoint
70
- @model = @yaml_content['prompt']['model']
70
+ @model ||= @yaml_content['prompt']['model']
71
71
  case @model
72
72
  when 'gpt-4', 'gpt-4-0613', 'gpt-4-32k', 'gpt-4-32k-0613', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613'
73
73
  'https://api.openai.com/v1/chat/completions'
@@ -77,18 +77,22 @@ class PromptSpec
77
77
  end
78
78
 
79
79
  def construct_headers
80
- headers = @yaml_content['headers'] || {}
81
-
82
- if headers.empty?
80
+ @headers ||= begin
81
+ yaml_headers = @yaml_content.fetch('headers', {})
82
+ headers = {}
83
+
83
84
  case @model
84
85
  when 'gpt-4', 'gpt-4-0613', 'gpt-4-32k', 'gpt-4-32k-0613', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613'
85
86
  api_key = ENV['OPENAI_API_KEY']
86
87
  headers['Authorization'] = "Bearer #{api_key}" if api_key
87
88
  # Add more cases here for other providers
88
89
  end
90
+ else
91
+ headers.merge!(yaml_headers)
92
+ end
93
+
94
+ headers['Content-Type'] = 'application/json' unless headers.key?('Content-Type')
95
+ headers
89
96
  end
90
-
91
- headers['Content-Type'] = 'application/json' unless headers.key?('Content-Type')
92
- headers
93
97
  end
94
- end
98
+ end
data/promptspec.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # promptspec.gemspec
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = "promptspec"
4
- spec.version = "0.0.5"
4
+ spec.version = "0.0.7"
5
5
  spec.authors = ["Hyperaide, John Paul, Daniel Paul"]
6
6
  spec.email = ["jp@hyperaide.com"]
7
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: promptspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hyperaide, John Paul, Daniel Paul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-03 00:00:00.000000000 Z
11
+ date: 2023-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yaml