diff --git a/README.md b/README.md index 55aefd8..3ea610a 100644 --- a/README.md +++ b/README.md @@ -149,3 +149,5 @@ client.datasources.drop('my_datasource') ``` >Note: The SDK currently does not support automatically removing a data source if it is no longer connected to any mind. +### Other SDKs +#### [Command-Line](https://github.com/Better-Boy/minds-cli-sdk) diff --git a/minds/minds.py b/minds/minds.py index 02a6ee8..de643a9 100644 --- a/minds/minds.py +++ b/minds/minds.py @@ -1,6 +1,5 @@ from typing import List, Union, Iterable -from urllib.parse import urlparse, urlunparse - +import utils from openai import OpenAI import minds.utils as utils import minds.exceptions as exc @@ -8,7 +7,6 @@ DEFAULT_PROMPT_TEMPLATE = 'Use your database tools to answer the user\'s question: {{question}}' - class Mind: def __init__( self, client, name, @@ -33,7 +31,11 @@ def __init__( self.parameters = parameters self.created_at = created_at self.updated_at = updated_at - + base_url = utils.get_openai_base_url(self.api.base_url) + self.openai_client = OpenAI( + api_key=self.api.api_key, + base_url=base_url + ) self.datasources = datasources def __repr__(self): @@ -157,23 +159,7 @@ def completion(self, message: str, stream: bool = False) -> Union[str, Iterable[ :return: string if stream mode is off or iterator of ChoiceDelta objects (by openai) """ - parsed = urlparse(self.api.base_url) - - netloc = parsed.netloc - if netloc == 'mdb.ai': - llm_host = 'llm.mdb.ai' - else: - llm_host = 'ai.' + netloc - - parsed = parsed._replace(path='', netloc=llm_host) - - base_url = urlunparse(parsed) - openai_client = OpenAI( - api_key=self.api.api_key, - base_url=base_url - ) - - response = openai_client.chat.completions.create( + response = self.openai_client.chat.completions.create( model=self.name, messages=[ {'role': 'user', 'content': message} diff --git a/minds/utils.py b/minds/utils.py index 261ef68..92861f4 100644 --- a/minds/utils.py +++ b/minds/utils.py @@ -1,5 +1,20 @@ import re import minds.exceptions as exc +from urllib.parse import urlparse, urlunparse + +def get_openai_base_url(base_url: str) -> str: + parsed = urlparse(base_url) + + netloc = parsed.netloc + if netloc == 'mdb.ai': + llm_host = 'llm.mdb.ai' + else: + llm_host = 'ai.' + netloc + + parsed = parsed._replace(path='', netloc=llm_host) + + return urlunparse(parsed) + def validate_mind_name(mind_name): """ @@ -23,4 +38,3 @@ def validate_mind_name(mind_name): # Check if the Mind name matches the pattern if not re.match(pattern, mind_name): 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.") -