mock_graphql_ai 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc76561ab75b8fa4646d98737559285a790e3ee961643bdc44558092cbc885a1
4
- data.tar.gz: 32a1e0f3fec394745dd10aa54df7e6fd160481e5b428347a0a689832dcebba19
3
+ metadata.gz: 9608b7928f7505d25c1d1b7c730eb6832777a392caa62b5f4fabee3aff322d30
4
+ data.tar.gz: 89bbced79385aff524126dd9b8dd09d54437070bef3424ec95f0db6d84ab4283
5
5
  SHA512:
6
- metadata.gz: 74d93dbc1bf31910495a9baf022acd8ac356c496b840d6f098707335f8c8b55b3a863e5447dc29ac90f08970413394bdb74438e6666fc4ea3d3c67eb8eff9d7a
7
- data.tar.gz: 064ed835690f84f6824dc1590e1f3bc37c86ca8e63a8a8d90add371bfb4cbb2ebcd7c705ba7067d79f37502003a8c8ec93b3169d70dea8e869250b989cc06d0d
6
+ metadata.gz: 2200ec1dd098149f2383a626531041d67814f07bb1b6a83154b30d6373177af14df93360a59d9b1f5ad8762f480fe4785a857483e2909adfbc54a1647c18e8f7
7
+ data.tar.gz: 893ccf6715ec28d3917b1fe1e3baf413f2a02bb1aa3834dbf5a2d68e7815f74f1c83b1f5a2ba346df2d1708f9a762191471a86e348dbc733d71662476bb0f280
@@ -3,6 +3,8 @@ require 'rails/generators'
3
3
  module MockGraphqlAi
4
4
  module Generators
5
5
  class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
6
8
  source_root(File.expand_path('../templates', __dir__))
7
9
 
8
10
  def copy_module_file
@@ -12,10 +14,32 @@ module MockGraphqlAi
12
14
  def copy_controller_file
13
15
  copy_file('graphql_mocks_controller.rb', 'app/controllers/graphql_mocks_controller.rb')
14
16
  end
17
+
18
+ def copy_model_file
19
+ copy_file('mock_graphql_response.rb', 'app/models/mock_graphql_response.rb')
20
+ end
21
+
22
+ def copy_migration
23
+ migration_template(
24
+ 'migration.rb',
25
+ 'db/migrate/create_mock_graphql_responses.rb',
26
+ migration_version: migration_version
27
+ )
28
+ end
15
29
 
16
30
  def register_controller_main_route
17
31
  route "get '/imitate_graphql', to: 'graphql_mocks#imitate', as: :imitate_graphql"
18
32
  end
33
+
34
+ private
35
+
36
+ def self.next_migration_number(dirname)
37
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
38
+ end
39
+
40
+ def migration_version
41
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
42
+ end
19
43
  end
20
44
  end
21
45
  end
@@ -1,5 +1,10 @@
1
1
  class GraphqlMocksController < ApplicationController
2
2
  def imitate
3
- render json: { "data" => { "query" => params[:query], "say" => "Here we'll send Mock API response!" } }
3
+ result = MockGraphqlAi::Resource.new(
4
+ query: params[:query],
5
+ token: "add-openai-access-token-here" # Rails.application.credentials.dig(:openai, :access_token)
6
+ ).respond!
7
+
8
+ render json: result
4
9
  end
5
10
  end
@@ -0,0 +1,13 @@
1
+ class CreateMockGraphqlResponses < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :mock_graphql_responses do |t|
4
+ t.string :name
5
+ t.integer :req_type, null: false, default: 0
6
+ t.integer :output_mode, null: false, default: 0
7
+ t.jsonb :data, null: false, default: {}
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,6 @@
1
+ class MockGraphqlResponse < ApplicationRecord
2
+ enum req_type: [:query, :mutation]
3
+
4
+ # All output modes except :void map to the OpenAI response attribute 'finish_reason' to determine model output
5
+ enum output_mode: [:void, :token_limit, :content_filter, :incomplete, :stop]
6
+ end
@@ -1,3 +1,3 @@
1
1
  module MockGraphqlAi
2
- VERSION = '1.0.4'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -1,4 +1,95 @@
1
1
  require 'mock_graphql_ai/version'
2
+ require 'openai'
3
+ require 'graphql'
2
4
 
3
5
  module MockGraphqlAi
6
+ class Resource
7
+ attr_accessor :query, :type, :name, :client, :openai_access_token, :response
8
+
9
+ def initialize(options={})
10
+ @query = options[:query]
11
+ @openai_access_token = options[:token]
12
+ @temperature = 0.3
13
+ @model = "gpt-3.5-turbo"
14
+ end
15
+
16
+ def respond!
17
+ @type, @name = grab_query_details
18
+ initiate_openai_client
19
+ get_response_from_ai
20
+ end
21
+
22
+ private
23
+
24
+ def grab_query_details
25
+ query_definition = GraphQL.parse(query).definitions[0]
26
+ [query_definition.operation_type, query_definition.selections[0].name]
27
+ end
28
+
29
+ def initiate_openai_client
30
+ @client = OpenAI::Client.new(access_token: openai_access_token)
31
+ end
32
+
33
+ def get_response_from_ai
34
+ return mock_response.data if mock_response
35
+
36
+ @response = client.chat(
37
+ parameters: {
38
+ model: @model,
39
+ temperature: @temperature,
40
+ messages: [
41
+ { role: "system", content: instructions_to_model },
42
+ { role: "user", content: "#{query}" }
43
+ ],
44
+ max_tokens: 1000
45
+ }
46
+ )
47
+
48
+ persist_response! if complete_model_output?
49
+ response.dig("choices", 0, "message", "content")
50
+ end
51
+
52
+ def mock_response
53
+ MockGraphqlResponse.find_by(name: name, req_type: type)
54
+ end
55
+
56
+ def instructions_to_model
57
+ "You are a helpful coding assistant. Below are the points you must consider while helping me
58
+
59
+ - I will give you a GraphQL query or mutation
60
+ - You have to return me the only response back
61
+ - Don't append any conversational or explanation text around it
62
+ - Don't give me code implementations you used to achieve this response
63
+ - Use double quotes around key value pairs
64
+ - Give me data objects to consume within
65
+ - I want real data instead of empty collection back to consume within my React Frontend app
66
+ - Avoid errors because I have to parse data back to JSON"
67
+ end
68
+
69
+ def persist_response!
70
+ MockGraphqlResponse.create(
71
+ data: @response.dig("choices", 0, "message", "content"),
72
+ name: name,
73
+ req_type: type,
74
+ output_mode: figure_output_mode
75
+ )
76
+ end
77
+
78
+ def complete_model_output?
79
+ @response.dig("choices", 0, "finish_reason") == "stop"
80
+ end
81
+
82
+ def figure_output_mode
83
+ finish_reason = response.dig("choices", 0, "finish_reason")
84
+
85
+ reasons_mapper = {
86
+ "length" => :token_limit,
87
+ "content_filter" => :content_filter,
88
+ "null" => :incomplete,
89
+ "stop" => :stop
90
+ }
91
+
92
+ reasons_mapper[finish_reason]
93
+ end
94
+ end
4
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_graphql_ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moiz Ali
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-29 00:00:00.000000000 Z
11
+ date: 2023-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -181,10 +181,10 @@ files:
181
181
  - LICENSE
182
182
  - README.md
183
183
  - Rakefile
184
- - lib/generators/mock_controller.rb
185
184
  - lib/generators/mock_graphql_ai/install_generator.rb
186
- - lib/generators/mock_module.rb
187
185
  - lib/generators/templates/graphql_mocks_controller.rb
186
+ - lib/generators/templates/migration.rb
187
+ - lib/generators/templates/mock_graphql_response.rb
188
188
  - lib/generators/templates/mockable.rb
189
189
  - lib/mock_graphql_ai.rb
190
190
  - lib/mock_graphql_ai/version.rb
@@ -1,15 +0,0 @@
1
- require "rails/generators"
2
-
3
- module Generators
4
- class MockController < Rails::Generators::Base
5
- source_root(File.expand_path("../generators/templates/controllers", __dir__))
6
-
7
- def copy_controller_file
8
- copy_file("graphql_mocks_controller.rb", "app/controllers/graphql_mocks_controller.rb")
9
- end
10
-
11
- def register_controller_main_route
12
- route "get '/imitate_graphql', to: 'graphql_mocks#imitate', as: :imitate_graphql"
13
- end
14
- end
15
- end
@@ -1,11 +0,0 @@
1
- require 'rails/generators'
2
-
3
- module Generators
4
- class MockModule < Rails::Generators::Base
5
- source_root(File.expand_path('../generators/templates', __dir__))
6
-
7
- def copy_module_file
8
- copy_file("mockable.rb", "app/controllers/concerns/mockable.rb")
9
- end
10
- end
11
- end