Skip to content

Commit 2368871

Browse files
committed
fix: add v2 for methods to return raw results
1 parent 633527f commit 2368871

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

mindsdb_sdk/agents.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __repr__(self):
3535

3636
class Agent:
3737
"""Represents a MindsDB agent.
38-
38+
3939
Working with agents:
4040
4141
Get an agent by name:
@@ -246,6 +246,17 @@ def completion(self, name: str, messages: List[dict]) -> AgentCompletion:
246246

247247
return AgentCompletion(data['message']['content'])
248248

249+
def completion_v2(self, name: str, messages: List[dict]) -> AgentCompletion:
250+
"""
251+
Queries the agent for a completion.
252+
253+
:param name: Name of the agent
254+
:param messages: List of messages to be sent to the agent
255+
256+
:return: completion from querying the agent
257+
"""
258+
return self.api.agent_completion(self.project.name, name, messages)
259+
249260
def completion_stream(self, name, messages: List[dict]) -> Iterable[object]:
250261
"""
251262
Queries the agent for a completion and streams the response as an iterable object.
@@ -257,6 +268,17 @@ def completion_stream(self, name, messages: List[dict]) -> Iterable[object]:
257268
"""
258269
return self.api.agent_completion_stream(self.project.name, name, messages)
259270

271+
def completion_stream_v2(self, name, messages: List[dict]) -> Iterable[object]:
272+
"""
273+
Queries the agent for a completion and streams the response as an iterable object.
274+
275+
:param name: Name of the agent
276+
:param messages: List of messages to be sent to the agent
277+
278+
:return: iterable of completion chunks from querying the agent.
279+
"""
280+
return self.api.agent_completion_stream_v2(self.project.name, name, messages)
281+
260282
def _create_default_knowledge_base(self, agent: Agent, name: str) -> KnowledgeBase:
261283
# Make sure default ML engine for embeddings exists.
262284
try:

mindsdb_sdk/connectors/rest_api.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _raise_for_status(response):
3737

3838

3939
class RestAPI:
40-
def __init__(self, url=None, login=None, password=None, api_key=None, is_managed=False,
40+
def __init__(self, url=None, login=None, password=None, api_key=None, is_managed=False,
4141
cookies=None, headers=None):
4242

4343
self.url = url
@@ -289,6 +289,22 @@ def agent_completion_stream(self, project: str, name: str, messages: List[dict])
289289
# Stream objects loaded from SSE events 'data' param.
290290
yield json.loads(chunk.data)
291291

292+
@_try_relogin
293+
def agent_completion_stream_v2(self, project: str, name: str, messages: List[dict]):
294+
url = self.url + f'/api/projects/{project}/agents/{name}/completions/stream'
295+
response = self.session.post(url, json={'messages': messages}, stream=True)
296+
297+
# Check for HTTP errors before processing the stream
298+
response.raise_for_status()
299+
300+
client = SSEClient(response)
301+
302+
try:
303+
for chunk in client.events():
304+
yield chunk # Stream SSE events
305+
except Exception as e:
306+
yield e
307+
292308
@_try_relogin
293309
def create_agent(self, project: str, name: str, model: str = None, provider: str = None, skills: List[str] = None, params: dict = None):
294310
url = self.url + f'/api/projects/{project}/agents'
@@ -452,4 +468,3 @@ def knowledge_base_completion(self, project: str, knowledge_base_name, payload):
452468
)
453469
_raise_for_status(r)
454470
return r.json()
455-

0 commit comments

Comments
 (0)