Skip to content

Commit dcbe1e5

Browse files
Merge pull request #39 from Better-Boy/openai-client
Reuse Openai client
2 parents bac1981 + ae7c9d4 commit dcbe1e5

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,5 @@ client.datasources.drop('my_datasource')
149149
```
150150
>Note: The SDK currently does not support automatically removing a data source if it is no longer connected to any mind.
151151
152+
### Other SDKs
153+
#### [Command-Line](https://github.com/Better-Boy/minds-cli-sdk)

minds/minds.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from typing import List, Union, Iterable
2-
from urllib.parse import urlparse, urlunparse
3-
2+
import utils
43
from openai import OpenAI
54
import minds.utils as utils
65
import minds.exceptions as exc
76
from minds.datasources import Datasource, DatabaseConfig
87

98
DEFAULT_PROMPT_TEMPLATE = 'Use your database tools to answer the user\'s question: {{question}}'
109

11-
1210
class Mind:
1311
def __init__(
1412
self, client, name,
@@ -33,7 +31,11 @@ def __init__(
3331
self.parameters = parameters
3432
self.created_at = created_at
3533
self.updated_at = updated_at
36-
34+
base_url = utils.get_openai_base_url(self.api.base_url)
35+
self.openai_client = OpenAI(
36+
api_key=self.api.api_key,
37+
base_url=base_url
38+
)
3739
self.datasources = datasources
3840

3941
def __repr__(self):
@@ -157,23 +159,7 @@ def completion(self, message: str, stream: bool = False) -> Union[str, Iterable[
157159
158160
:return: string if stream mode is off or iterator of ChoiceDelta objects (by openai)
159161
"""
160-
parsed = urlparse(self.api.base_url)
161-
162-
netloc = parsed.netloc
163-
if netloc == 'mdb.ai':
164-
llm_host = 'llm.mdb.ai'
165-
else:
166-
llm_host = 'ai.' + netloc
167-
168-
parsed = parsed._replace(path='', netloc=llm_host)
169-
170-
base_url = urlunparse(parsed)
171-
openai_client = OpenAI(
172-
api_key=self.api.api_key,
173-
base_url=base_url
174-
)
175-
176-
response = openai_client.chat.completions.create(
162+
response = self.openai_client.chat.completions.create(
177163
model=self.name,
178164
messages=[
179165
{'role': 'user', 'content': message}

minds/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import re
22
import minds.exceptions as exc
3+
from urllib.parse import urlparse, urlunparse
4+
5+
def get_openai_base_url(base_url: str) -> str:
6+
parsed = urlparse(base_url)
7+
8+
netloc = parsed.netloc
9+
if netloc == 'mdb.ai':
10+
llm_host = 'llm.mdb.ai'
11+
else:
12+
llm_host = 'ai.' + netloc
13+
14+
parsed = parsed._replace(path='', netloc=llm_host)
15+
16+
return urlunparse(parsed)
17+
318

419
def validate_mind_name(mind_name):
520
"""
@@ -23,4 +38,3 @@ def validate_mind_name(mind_name):
2338
# Check if the Mind name matches the pattern
2439
if not re.match(pattern, mind_name):
2540
raise exc.MindNameInvalid("Mind name should start with a letter and contain only letters, numbers or underscore, with a maximum of 32 characters. Spaces are not allowed.")
26-

0 commit comments

Comments
 (0)