Skip to content

Commit c5cb684

Browse files
Merge pull request #5 from andreibondarev/classification-endpoint
Classification endpoint
2 parents 7b9e1de + 0496892 commit c5cb684

File tree

6 files changed

+119
-2
lines changed

6 files changed

+119
-2
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,26 @@ client.query.aggs(
187187
)
188188
```
189189

190-
#### Nodes
190+
### Classification
191+
```ruby
192+
client.classifications.create(
193+
type: "zeroshot",
194+
class_name: "Posts",
195+
classify_properties: ["hasColor"],
196+
based_on_properties: ["text"]
197+
)
198+
199+
client.classifications.get(
200+
id: ""
201+
)
202+
```
203+
204+
### Nodes
191205
```ruby
192206
client.nodes
193207
```
194208

195-
#### Health
209+
### Health
196210
```ruby
197211
client.live?
198212
client.ready?

lib/weaviate.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Weaviate
1313
autoload :Query, "weaviate/query"
1414
autoload :Nodes, "weaviate/nodes"
1515
autoload :Health, "weaviate/health"
16+
autoload :Classifications, "weaviate/classifications"
1617

1718
module Response
1819
autoload :Base, "weaviate/response/base"

lib/weaviate/classifications.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module Weaviate
4+
class Classifications < Base
5+
PATH = "classifications"
6+
7+
def get(id:)
8+
response = client.connection.get("#{PATH}/#{id}")
9+
response.body
10+
end
11+
12+
def create(
13+
class_name:,
14+
type:,
15+
classify_properties: nil,
16+
based_on_properties: nil,
17+
settings: nil,
18+
filters: nil
19+
)
20+
response = client.connection.post(PATH) do |req|
21+
req.body = {}
22+
req.body["class"] = class_name
23+
req.body["type"] = type
24+
req.body["classifyProperties"] = classify_properties if classify_properties
25+
req.body["basedOnProperties"] = based_on_properties if based_on_properties
26+
req.body["settings"] = settings if settings
27+
req.body["filters"] = filters if filters
28+
end
29+
30+
if response.success?
31+
response.body
32+
end
33+
end
34+
end
35+
end

lib/weaviate/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def ready?
5959
@health.ready?
6060
end
6161

62+
def classifications
63+
@classifications ||= Weaviate::Classifications.new(client: self)
64+
end
65+
6266
def objects
6367
@objects ||= Weaviate::Objects.new(client: self)
6468
end

spec/fixtures/classification.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"basedOnProperties": ["text"],
3+
"class": "Posts",
4+
"classifyProperties": ["hasColor"],
5+
"id": "1",
6+
"meta": {
7+
"completed": "0001-01-01T00:00:00.000Z",
8+
"started": "2023-04-03T19:58:13.965Z"
9+
},
10+
"status": "running",
11+
"type": "zeroshot"
12+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Weaviate::Classifications do
6+
let(:client) {
7+
Weaviate::Client.new(
8+
scheme: "http",
9+
host: "localhost:8080"
10+
)
11+
}
12+
13+
let(:classifications) { client.classifications }
14+
let(:classification_fixture) { JSON.parse(File.read("spec/fixtures/classification.json")) }
15+
16+
describe "#create" do
17+
let(:response) { OpenStruct.new(success?: true, body: classification_fixture) }
18+
19+
before do
20+
allow_any_instance_of(Faraday::Connection).to receive(:post)
21+
.with("classifications")
22+
.and_return(response)
23+
end
24+
25+
it "creates the classification" do
26+
response = classifications.create(
27+
class_name: "Posts",
28+
type: "zeroshot",
29+
classify_properties: ["hasColor"],
30+
based_on_properties: ["text"]
31+
)
32+
expect(response["type"]).to eq("zeroshot")
33+
expect(response["status"]).to eq("running")
34+
end
35+
end
36+
37+
describe "#get" do
38+
let(:response) { OpenStruct.new(success?: true, body: classification_fixture) }
39+
40+
before do
41+
allow_any_instance_of(Faraday::Connection).to receive(:get)
42+
.with("classifications/1")
43+
.and_return(response)
44+
end
45+
46+
it "returns the classification" do
47+
response = classifications.get(id: "1")
48+
expect(response["id"]).to eq("1")
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)