Skip to content

Commit e62a269

Browse files
Merge pull request #11 from mindsdb/integration-test
Minds integration tests
2 parents bf613d4 + 84cabf4 commit e62a269

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

minds/minds.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ def completion(self, message: str, stream: bool = False) -> Union[str, Iterable[
126126
stream=stream
127127
)
128128
if stream:
129-
for chunk in response:
130-
yield chunk.choices[0].delta
129+
return self.stream_response(response)
131130
else:
132131
return response.choices[0].message.content
133132

133+
def stream_response(self, response):
134+
for chunk in response:
135+
yield chunk.choices[0].delta
136+
134137

135138
class Minds:
136139
def __init__(self, client):

requirements_test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import os
2+
import copy
3+
4+
api_key = os.getenv('API_KEY')
5+
base_url = 'https://dev.mindsdb.com'
6+
7+
from minds.client import Client
8+
9+
client = Client(api_key, base_url=base_url)
10+
11+
import logging
12+
logging.basicConfig(level=logging.DEBUG)
13+
14+
from minds.datasources.examples import example_ds
15+
16+
from minds.exceptions import ObjectNotFound
17+
18+
19+
def test_datasources():
20+
21+
# remove previous object
22+
try:
23+
client.datasources.drop(example_ds.name)
24+
except ObjectNotFound:
25+
...
26+
27+
# create
28+
ds = client.datasources.create(example_ds)
29+
ds = client.datasources.create(example_ds, replace=True)
30+
assert ds.name == example_ds.name
31+
32+
# get
33+
ds = client.datasources.get(example_ds.name)
34+
35+
# list
36+
ds_list = client.datasources.list()
37+
assert len(ds_list) > 0
38+
39+
# drop
40+
client.datasources.drop(ds.name)
41+
42+
43+
def test_minds():
44+
ds_name = 'test_datasource_'
45+
ds_name2 = 'test_datasource2_'
46+
mind_name = 'int_test_mind_'
47+
mind_name2 = 'int_test_mind2_'
48+
prompt1 = 'answer in german'
49+
prompt2 = 'answer in spanish'
50+
51+
# remove previous objects
52+
for name in (mind_name, mind_name2):
53+
try:
54+
client.minds.drop(name)
55+
except ObjectNotFound:
56+
...
57+
58+
# prepare datasources
59+
ds_cfg = copy.copy(example_ds)
60+
ds_cfg.name = ds_name
61+
ds = client.datasources.create(example_ds, replace=True)
62+
63+
# second datasource
64+
ds2_cfg = copy.copy(example_ds)
65+
ds2_cfg.name = ds_name2
66+
67+
# create
68+
mind = client.minds.create(
69+
mind_name,
70+
datasources=[ds],
71+
provider='openai'
72+
)
73+
mind = client.minds.create(
74+
mind_name,
75+
replace=True,
76+
datasources=[ds.name, ds2_cfg],
77+
parameters={
78+
'prompt_template': prompt1
79+
}
80+
)
81+
82+
# get
83+
mind = client.minds.get(mind_name)
84+
assert len(mind.datasources) == 2
85+
assert mind.parameters['prompt_template'] == prompt1
86+
87+
# list
88+
mind_list = client.minds.list()
89+
assert len(mind_list) > 0
90+
91+
# rename & update
92+
mind.update(
93+
name=mind_name2,
94+
datasources=[ds.name],
95+
parameters={
96+
'prompt_template': prompt2
97+
}
98+
)
99+
try:
100+
mind = client.minds.get(mind_name)
101+
except ObjectNotFound:
102+
...
103+
else:
104+
raise Exception('mind is not renamed')
105+
106+
mind = client.minds.get(mind_name2)
107+
assert len(mind.datasources) == 1
108+
assert mind.parameters['prompt_template'] == prompt2
109+
110+
# add datasource
111+
mind.add_datasource(ds2_cfg)
112+
assert len(mind.datasources) == 2
113+
114+
# del datasource
115+
mind.del_datasource(ds2_cfg.name)
116+
assert len(mind.datasources) == 1
117+
118+
# completion
119+
answer = mind.completion('say hello')
120+
assert 'hola' in answer.lower()
121+
# stream completion
122+
success = False
123+
for chunk in mind.completion('say hello', stream=True):
124+
if 'hola' in chunk.content.lower():
125+
success = True
126+
assert success is True
127+
128+
# drop
129+
client.minds.drop(mind_name2)
130+
client.datasources.drop(ds.name)
131+
client.datasources.drop(ds2_cfg.name)

0 commit comments

Comments
 (0)