forked from cohere-ai/cohere-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_classify.py
More file actions
106 lines (97 loc) · 4.07 KB
/
Copy pathtest_classify.py
File metadata and controls
106 lines (97 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import unittest
import pytest
from utils import get_api_key
import cohere
from cohere.responses.classify import Example
co = cohere.Client(get_api_key())
class TestClassify(unittest.TestCase):
def test_success(self):
prediction = co.classify(
model="small",
inputs=["purple"],
examples=[
Example("apple", "fruit"),
Example("banana", "fruit"),
Example("cherry", "fruit"),
Example("watermelon", "fruit"),
Example("kiwi", "fruit"),
Example("red", "color"),
Example("blue", "color"),
Example("green", "color"),
Example("yellow", "color"),
Example("magenta", "color"),
],
)
self.assertIsInstance(prediction.classifications, list)
self.assertIsInstance(prediction.classifications[0].input, str)
self.assertIsInstance(prediction.classifications[0].prediction, str)
self.assertIsInstance(prediction.classifications[0].confidence, (int, float))
self.assertIsInstance(prediction.classifications[0].labels["color"].confidence, (int, float))
self.assertTrue(prediction.meta)
self.assertTrue(prediction.meta["api_version"])
self.assertTrue(prediction.meta["api_version"]["version"])
self.assertEqual(len(prediction.classifications[0].labels), 2)
self.assertEqual(len(prediction.classifications), 1)
self.assertEqual(prediction.classifications[0].prediction, "color")
def test_empty_inputs(self):
with self.assertRaises(cohere.CohereError):
co.classify(
model="small",
inputs=[],
examples=[
Example("apple", "fruit"),
Example("banana", "fruit"),
Example("cherry", "fruit"),
Example("watermelon", "fruit"),
Example("kiwi", "fruit"),
Example("red", "color"),
Example("blue", "color"),
Example("green", "color"),
Example("yellow", "color"),
Example("magenta", "color"),
],
)
def test_success_multi_input(self):
prediction = co.classify(
model="small",
inputs=["purple", "mango"],
examples=[
Example("apple", "fruit"),
Example("banana", "fruit"),
Example("cherry", "fruit"),
Example("watermelon", "fruit"),
Example("kiwi", "fruit"),
Example("red", "color"),
Example("blue", "color"),
Example("green", "color"),
Example("yellow", "color"),
Example("magenta", "color"),
],
)
self.assertEqual(prediction.classifications[0].prediction, "color")
self.assertEqual(prediction.classifications[1].prediction, "fruit")
self.assertEqual(len(prediction.classifications), 2)
def test_success_all_fields(self):
prediction = co.classify(
model="small",
inputs=["mango", "purple"],
examples=[
Example("apple", "fruit"),
Example("banana", "fruit"),
Example("cherry", "fruit"),
Example("watermelon", "fruit"),
Example("kiwi", "fruit"),
Example("red", "color"),
Example("blue", "color"),
Example("green", "color"),
Example("yellow", "color"),
Example("magenta", "color"),
],
)
self.assertEqual(prediction.classifications[0].prediction, "fruit")
self.assertEqual(prediction.classifications[1].prediction, "color")
@pytest.mark.skipif(bool(os.getenv("CO_API_URL")), reason="relies on preset existing in prod")
def test_preset_success(self):
prediction = co.classify(preset="SDK-TESTS-PRESET-rfa6h3")
self.assertIsInstance(prediction.classifications, list)