Skip to content

Commit

Permalink
Merge pull request #8 from mindsdb/stream-mind
Browse files Browse the repository at this point in the history
Stream completion
  • Loading branch information
ZoranPandovski authored Sep 25, 2024
2 parents e3fa8c7 + 762ba60 commit 89b2750
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions examples/base_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
# call completion
print(mind.completion('2+3'))

# stream completion
for chunk in mind.completion('2+3', stream=True):
print(chunk.content)

# --- managing datasources ---

# create or replace
Expand Down
22 changes: 17 additions & 5 deletions minds/minds.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Union
from typing import List, Union, Iterable
from urllib.parse import urlparse, urlunparse

from openai import OpenAI
Expand Down Expand Up @@ -84,7 +84,15 @@ def add_datasource(self, datasource: Datasource):
def del_datasource(self, datasource: Union[Datasource, str]):
raise NotImplementedError

def completion(self, message):
def completion(self, message: str, stream: bool = False) -> Union[str, Iterable[object]]:
"""
Call mind completion
:param message: input question
:param stream: to enable stream mode
:return: string if stream mode is off or iterator of ChoiceDelta objects (by openai)
"""
parsed = urlparse(self.api.base_url)

netloc = parsed.netloc
Expand All @@ -101,14 +109,18 @@ def completion(self, message):
base_url=base_url
)

completion = openai_client.chat.completions.create(
response = openai_client.chat.completions.create(
model=self.name,
messages=[
{'role': 'user', 'content': message}
],
stream=False
stream=stream
)
return completion.choices[0].message.content
if stream:
for chunk in response:
yield chunk.choices[0].delta
else:
return response.choices[0].message.content


class Minds:
Expand Down

0 comments on commit 89b2750

Please sign in to comment.