ai 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 0edef7f201e016d718ffb659af381c32c5eaae07
4
- data.tar.gz: 2df14d968144d0f1b3bbd7d9c7f158c0f8f559ff
3
+ metadata.gz: 9aa72c3d7d57c0ac6ee419dcba9852763d906a93
4
+ data.tar.gz: 24885f0576dc51585c41fa1a6968e71e6d1ce25d
5
5
  SHA512:
6
- metadata.gz: f2cb3d7ece962adb5b7aa640dc99798a4b9ff36457e04af2f1d147aa24b29704fdc21ba08f8c8ca7499424782e0a18861afe0be7e6045abc14d7499b7a0cfe23
7
- data.tar.gz: fe2827f3d2a8be77e37a6cf7b7c0ae549bd857e76d263dfc645a5877ea70185bb0863e2710809a3c328b744b33caa7c2bc451ca7d4cb3e5d1c0fa6f003994b7b
6
+ metadata.gz: ac0361bc12ac255195918c7063a02da6e15b3668b8fa645f22f82340e7a93007d59cb854bcc5206692aac462e91d4fb1ff0184b6af6e5b599700eba43e660d8e
7
+ data.tar.gz: a688d7d6c7249549f97742fc5ccf7caa8e156ccfb4d8e1203e6feb21d6132561703eee5734e62eda3f8214166815a7050d303b3149e5af290e2ca67997239fbb
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ai'
3
+
4
+ utterance = ARGV.join(' ')
5
+
6
+ Ai.import(utterance)
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ai'
3
+
4
+ puts 'Enter Ctrl-D to end input.'
5
+ puts 'Enter question:'
6
+ question = STDIN.read
7
+
8
+ puts 'Enter answer:'
9
+ answer = STDIN.read
10
+
11
+ puts "Importing"
12
+
13
+ Ai.learn_answer([question.strip], answer.strip)
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ai'
3
+
4
+ puts 'Enter Ctrl-D to end input.'
5
+ puts 'Enter script question:'
6
+ question = STDIN.read
7
+
8
+ puts 'Enter script:'
9
+ answer = STDIN.read
10
+
11
+ puts "Importing"
12
+
13
+ Ai.learn_script([question.strip], answer.strip)
data/lib/ai.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'net/http'
2
2
  require 'uri'
3
3
  require 'json'
4
+ require 'ai_client'
4
5
 
5
6
  class Ai
6
7
  def self.process(utterance)
@@ -22,5 +23,17 @@ class Ai
22
23
 
23
24
  JSON.parse(response.body)
24
25
  end
26
+
27
+ def self.import(path)
28
+ ::AiClient.import(path)
29
+ end
30
+
31
+ def self.learn_answer(questions, answer)
32
+ ::AiClient.learn_answer(questions, answer)
33
+ end
34
+
35
+ def self.learn_script(questions, answer)
36
+ ::AiClient.learn_script(questions, answer)
37
+ end
25
38
  end
26
39
 
@@ -0,0 +1,38 @@
1
+ require 'web_client'
2
+ require 'constants'
3
+
4
+ class AiClient
5
+ def self.import(path)
6
+ data = { knowledge: JSON.parse(File.read(path)) }
7
+
8
+ WebClient.post(BASE_URL + '/api/v1/direct_responses/import', data)
9
+ end
10
+
11
+ def self.learn_answer(utterances, response)
12
+ body = { knowledge: {
13
+ "knowledge" => [
14
+ {
15
+ "utterances" => utterances,
16
+ "response": response
17
+ }
18
+ ]
19
+ }
20
+ }
21
+
22
+ WebClient.post(BASE_URL + '/api/v1/direct_responses/import', body)
23
+ end
24
+
25
+ def self.learn_script(utterances, script)
26
+ body = { knowledge: {
27
+ "scripts" => [
28
+ {
29
+ "utterances" => utterances,
30
+ "script": script
31
+ }
32
+ ]
33
+ }
34
+ }
35
+
36
+ WebClient.post(BASE_URL + '/api/v1/direct_responses/import', body)
37
+ end
38
+ end
@@ -0,0 +1 @@
1
+ BASE_URL=ENV['PIVOTAL_API_URL'] || "http://milligan-acceptance.herokuapp.com"
@@ -0,0 +1,22 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class WebClient
5
+ def self.post(url, body)
6
+ uri = URI.parse(url)
7
+ http = Net::HTTP.new(uri.host, uri.port)
8
+
9
+ request = Net::HTTP::Post.new(
10
+ uri.request_uri,
11
+ 'Content-Type' => 'application/json'
12
+ )
13
+ request.body = body.to_json
14
+
15
+ response = http.request(request)
16
+ unless response.body.nil?
17
+ JSON.parse(response.body)
18
+ else
19
+ {}
20
+ end
21
+ end
22
+ end
@@ -1,43 +1,59 @@
1
- require 'ai'
2
-
3
- RSpec.describe Ai do
4
- describe '#process' do
5
- it 'makes a post request' do
6
- http = double("Http")
7
- expect(Net::HTTP).to receive(:new).and_return(http)
8
- response = double('Response')
9
- expect(response).to receive(:body).and_return({'type' => 'script', 'script' => 'ls -a'}.to_json)
10
- expect(http).to receive(:request_post).and_return(response)
11
- expect(Kernel).to receive(:system).with('ls -a').and_return(true)
12
- # request = double('Response')
13
- # expect(Net::HTTP::Post).to receive(:new).and_return(request)
14
- # expect(request).to receive(:body=).with("{\"utterance\":\"show files\"}")
15
- # expect(http).to receive(:request).and_return(response)
16
-
17
- Ai.process('show files')
1
+ require 'ai_client'
2
+ require 'web_client'
3
+
4
+ RSpec.describe AiClient do
5
+ describe 'self#import' do
6
+ it 'submits import file to the server' do
7
+ url = "http://localhost:4040/api/v1/direct_responses/import"
8
+
9
+ path = '/Users/apliszka/workspace/ai/spec/fixtures/import.json'
10
+ body = { knowledge: JSON.parse(File.read(path)) }
11
+
12
+ expect(WebClient).to receive(:post).with(url, body)
13
+
14
+ AiClient.import(path)
18
15
  end
16
+ end
17
+
18
+ describe 'self#learn' do
19
+ it 'submits new utterance to server' do
20
+ url = "http://localhost:4040/api/v1/direct_responses/import"
19
21
 
20
- context 'given acton response' do
21
- it 'runs the command' do
22
- response = {'type' => 'script', 'script' => 'ls -a'}
22
+ body = {
23
+ "knowledge" => [
24
+ {
25
+ "utterances" => [
26
+ "What are the best AI courses?"
27
+ ],
28
+ "response": "Fast.ai MOOC http://course.fast.ai and Coursers"
29
+ }
30
+ ]
31
+ }
23
32
 
24
- expect(Kernel).to receive(:system).with('ls -a').and_return(true)
25
- expect(Ai).to receive(:post).and_return(response)
33
+ expect(WebClient).to receive(:post).with(url, { knowledge: body })
26
34
 
27
- Ai.process('show files')
28
- end
35
+ AiClient.learn_answer(["What are the best AI courses?"], "Fast.ai MOOC http://course.fast.ai and Coursers")
29
36
  end
37
+ end
38
+
39
+ describe 'self#learn_script' do
40
+ it 'submits new script to server' do
41
+ url = "http://localhost:4040/api/v1/direct_responses/import"
30
42
 
31
- context 'given text response' do
32
- it 'prints the response' do
33
- response = {'type' => 'text', 'text' => 'hi'}
43
+ body = {
44
+ "scripts" => [
45
+ {
46
+ "utterances" => [
47
+ "show changes"
48
+ ],
49
+ "script": "git diff"
50
+ }
51
+ ]
52
+ }
34
53
 
35
- expect(Kernel).to_not receive(:system)
36
- expect(Kernel).to receive(:puts).with('hi').and_return(true)
37
- expect(Ai).to receive(:post).and_return(response)
54
+ expect(WebClient).to receive(:post).with(url, { knowledge: body })
38
55
 
39
- Ai.process('Hello')
40
- end
56
+ AiClient.learn_script(["show changes"], "git diff")
41
57
  end
42
58
  end
43
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Pliszka
@@ -15,11 +15,20 @@ description: PivotalAI is a artificial intelligence and natural language process
15
15
  email: andy@pivotalai.com
16
16
  executables:
17
17
  - ai
18
+ - ai-import
19
+ - ai-learn-script
20
+ - ai-learn-answer
18
21
  extensions: []
19
22
  extra_rdoc_files: []
20
23
  files:
21
24
  - bin/ai
25
+ - bin/ai-import
26
+ - bin/ai-learn-answer
27
+ - bin/ai-learn-script
22
28
  - lib/ai.rb
29
+ - lib/ai_client.rb
30
+ - lib/constants.rb
31
+ - lib/web_client.rb
23
32
  - spec/ai_spec.rb
24
33
  homepage: http://pivotalai.com
25
34
  licenses: []