Skip to content

Commit

Permalink
Merge pull request #39 from Better-Boy/openai-client
Browse files Browse the repository at this point in the history
Reuse Openai client
  • Loading branch information
ZoranPandovski authored Oct 31, 2024
2 parents bac1981 + ae7c9d4 commit dcbe1e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 7 additions & 21 deletions minds/minds.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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
from minds.datasources import Datasource, DatabaseConfig

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


class Mind:
def __init__(
self, client, name,
Expand All @@ -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):
Expand Down Expand Up @@ -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}
Expand Down
16 changes: 15 additions & 1 deletion minds/utils.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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.")

0 comments on commit dcbe1e5

Please sign in to comment.