Skip to content

Commit 6d140a5

Browse files
committed
Cleaner implementation of streaming.
1 parent d9bba83 commit 6d140a5

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

llms_wrapper/llms.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,26 @@ def query(
697697
messages=messages,
698698
**completion_kwargs)
699699
if stream:
700-
# TODO: for now we take a shortcut here and simply return the original response
701-
# as "response".
702-
ret["response"] = response
700+
def chunk_generator(model_generator, retobj):
701+
try:
702+
for chunk in model_generator:
703+
choice0 = chunk["choices"][0]
704+
if choice0.finish_reason == "stop":
705+
break
706+
content = choice0["delta"].get("content", "")
707+
yield dict(error="", answer=content, ok=True)
708+
except Exception as e:
709+
yield dict(error=str(e), answer="", ok=False)
710+
finally:
711+
# TODO: add cost and elapsed time information into retobj
712+
# litellm does not support cost on streaming responses
713+
# response.__hidden_params["response_cost"] is 0.0
714+
ret["cost"] = None
715+
ret["elapsed_time"] = time.time() - start
716+
pass
717+
if return_response:
718+
ret["response"] = response
719+
ret["chunks"] = chunk_generator(response, ret)
703720
ret["ok"] = True
704721
ret["error"] = ""
705722
return ret
@@ -714,6 +731,9 @@ def query(
714731
del completion_kwargs["api_key"]
715732
ret["kwargs"] = completion_kwargs
716733
if return_cost:
734+
# TODO: replace with response._hidden_params["response_cost"] ?
735+
# but what if cost not supported for the model?
736+
717737
try:
718738
ret["cost"] = completion_cost(
719739
completion_response=response,

llms_wrapper/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import importlib.metadata
2-
__version__ = "0.3.0"
2+
__version__ = "0.3.1"
33

notebooks/test-streaming.ipynb

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
},
111111
{
112112
"cell_type": "code",
113-
"execution_count": 14,
113+
"execution_count": 3,
114114
"id": "a89258b6",
115115
"metadata": {},
116116
"outputs": [
@@ -121,7 +121,7 @@
121121
" 'role': 'user'}]"
122122
]
123123
},
124-
"execution_count": 14,
124+
"execution_count": 3,
125125
"metadata": {},
126126
"output_type": "execute_result"
127127
}
@@ -133,45 +133,55 @@
133133
},
134134
{
135135
"cell_type": "code",
136-
"execution_count": 15,
136+
"execution_count": 4,
137137
"id": "491b0ddb",
138138
"metadata": {},
139+
"outputs": [],
140+
"source": [
141+
"ret = llms.query(\"openai/gpt-4o\", msgs, temperature=0.5, max_tokens=1000, stream=True)\n"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 5,
147+
"id": "1c795bd0",
148+
"metadata": {},
139149
"outputs": [
140150
{
141151
"name": "stdout",
142152
"output_type": "stream",
143153
"text": [
144-
"A monoid is an algebraic structure with a single associative binary operation and an identity element. Specifically, a set M is a monoid if it is equipped with a binary operation (let's call it *) that satisfies the following properties:\n",
154+
"A monoid is an algebraic structure with a single associative binary operation and an identity element. Formally, a set M equipped with an operation * (often called multiplication) is a monoid if it satisfies two properties:\n",
145155
"\n",
146156
"1. **Associativity**: For all elements a, b, and c in M, the equation (a * b) * c = a * (b * c) holds.\n",
147-
"2. **Identity Element**: There exists an element e in M such that for every element a in M, the equation e * a = a * e = a holds.\n",
157+
"2. **Identity element**: There exists an element e in M such that for every element a in M, the equations e * a = a * e = a hold.\n",
158+
"\n",
159+
"A simple example of a monoid is the set of natural numbers (including zero) with the operation of addition. In this case:\n",
148160
"\n",
149-
"A simple example of a monoid is the set of natural numbers (including zero) with the operation of addition. \n",
161+
"- The binary operation is addition (+), which is associative. For any natural numbers a, b, and c, we have (a + b) + c = a + (b + c).\n",
162+
"- The identity element is 0, because for any natural number a, a + 0 = 0 + a = a.\n",
150163
"\n",
151-
"- The set is {0, 1, 2, 3, ...}.\n",
152-
"- The binary operation is addition (+).\n",
153-
"- The identity element is 0 because adding 0 to any natural number does not change the number (0 + a = a + 0 = a).\n",
154-
"- Addition is associative because for any natural numbers a, b, and c, the equation (a + b) + c = a + (b + c) is always true."
164+
"Thus, the natural numbers with addition form a monoid.\n"
155165
]
156166
}
157167
],
158168
"source": [
159-
"ret = llms.query(\"openai/gpt-4o\", msgs, temperature=0.5, max_tokens=1000, stream=True)\n",
160169
"if ret[\"ok\"]:\n",
161-
" for chunk in ret[\"response\"]:\n",
162-
" choice0 = chunk.choices[0]\n",
163-
" if choice0.finish_reason == \"stop\":\n",
164-
" break \n",
165-
" content = choice0.delta.content \n",
166-
" print(content, end=\"\", flush=True)\n",
170+
" for chunk in ret[\"chunks\"]:\n",
171+
" if chunk[\"error\"]:\n",
172+
" print()\n",
173+
" print(\"Error:\", chunk[\"error\"])\n",
174+
" break\n",
175+
" print(chunk[\"answer\"], end=\"\")\n",
176+
" print()\n",
167177
"else:\n",
168178
" print(\"Error:\", ret[\"error\"])"
169179
]
170180
},
171181
{
172182
"cell_type": "code",
173183
"execution_count": null,
174-
"id": "0eeea159",
184+
"id": "f7fa724b",
175185
"metadata": {},
176186
"outputs": [],
177187
"source": []

0 commit comments

Comments
 (0)