mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
fix: fix nlp cloud streaming
This commit is contained in:
parent
a688df79b1
commit
6d9f7b8f9d
3 changed files with 97 additions and 14 deletions
|
@ -131,14 +131,14 @@ def completion(
|
|||
logging_obj.pre_call(
|
||||
input=text,
|
||||
api_key=api_key,
|
||||
additional_args={"complete_input_dict": data},
|
||||
additional_args={"complete_input_dict": data, "headers": headers, "api_base": completion_url},
|
||||
)
|
||||
## COMPLETION CALL
|
||||
response = requests.post(
|
||||
completion_url, headers=headers, data=json.dumps(data), stream=optional_params["stream"] if "stream" in optional_params else False
|
||||
)
|
||||
if "stream" in optional_params and optional_params["stream"] == True:
|
||||
return response.iter_lines()
|
||||
return clean_and_iterate_chunks(response)
|
||||
else:
|
||||
## LOGGING
|
||||
logging_obj.post_call(
|
||||
|
@ -179,6 +179,34 @@ def completion(
|
|||
model_response.usage = usage
|
||||
return model_response
|
||||
|
||||
|
||||
# def clean_and_iterate_chunks(response):
|
||||
# def process_chunk(chunk):
|
||||
# print(f"received chunk: {chunk}")
|
||||
# cleaned_chunk = chunk.decode("utf-8")
|
||||
# # Perform further processing based on your needs
|
||||
# return cleaned_chunk
|
||||
|
||||
# for line in response.iter_lines():
|
||||
# if line:
|
||||
# yield process_chunk(line)
|
||||
def clean_and_iterate_chunks(response):
|
||||
buffer = b''
|
||||
|
||||
for chunk in response.iter_content(chunk_size=1024):
|
||||
if not chunk:
|
||||
break
|
||||
|
||||
buffer += chunk
|
||||
while b'\x00' in buffer:
|
||||
buffer = buffer.replace(b'\x00', b'')
|
||||
yield buffer.decode('utf-8')
|
||||
buffer = b''
|
||||
|
||||
# No more data expected, yield any remaining data in the buffer
|
||||
if buffer:
|
||||
yield buffer.decode('utf-8')
|
||||
|
||||
def embedding():
|
||||
# logic for parsing in - calling - parsing out model embedding calls
|
||||
pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue