Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse Openai client #39

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
29 changes: 7 additions & 22 deletions minds/minds.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import List, Union, Iterable
from urllib.parse import urlparse, urlunparse
from datetime import datetime

import utils
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utils is imported below from mind

from openai import OpenAI

import minds.exceptions as exc
Expand All @@ -10,7 +8,6 @@

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


class Mind:
def __init__(
self, client, name,
Expand All @@ -35,7 +32,11 @@ def __init__(
self.parameters = parameters
self.created_at = created_at
self.updated_at = updated_at

base_url = utils.create_base_url_openai(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 @@ -156,23 +157,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
15 changes: 15 additions & 0 deletions minds/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from urllib.parse import urlparse, urlunparse

def create_base_url_openai(base_url: str) -> str:
Better-Boy marked this conversation as resolved.
Show resolved Hide resolved
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)

Loading