Skip to content

Commit 7e4f8bb

Browse files
added function to the API client to handle POST with streaming
1 parent ec5ba82 commit 7e4f8bb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

minds/rest_api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import requests
23

34
import minds.exceptions as exc
@@ -59,6 +60,29 @@ def post(self, url, data={}):
5960
_raise_for_status(resp)
6061
return resp
6162

63+
def post_stream(self, url, data={}):
64+
"""Makes a POST request and yields chunks as they arrive (OpenAI-style streaming)."""
65+
with requests.post(
66+
self.base_url + url,
67+
headers=self._headers(),
68+
json=data,
69+
stream=True,
70+
) as resp:
71+
_raise_for_status(resp)
72+
for line in resp.iter_lines():
73+
if not line:
74+
continue
75+
decoded = line.decode("utf-8")
76+
if decoded.startswith("data: "):
77+
payload = decoded[len("data: "):]
78+
if payload.strip() == "[DONE]":
79+
break
80+
try:
81+
obj = json.loads(payload)
82+
yield obj
83+
except json.JSONDecodeError:
84+
yield {"error": f"Failed to parse: {decoded}"}
85+
6286
def put(self, url, data={}):
6387
resp = requests.put(
6488
self.base_url + url,

0 commit comments

Comments
 (0)