@@ -75,7 +75,7 @@ def stream_create( # type: ignore[return]
7575 ** kwargs ,
7676 ) -> Union [ChatCompletions , Iterator [ChatCompletionChunk ]]:
7777 extra_headers = kwargs .get ("extra_headers" , {})
78- with self .openai_client . with_streaming_response .chat .completions .create (
78+ return self .openai_client .chat .completions .create (
7979 model = model ,
8080 messages = messages ,
8181 stream = stream ,
@@ -91,22 +91,7 @@ def stream_create( # type: ignore[return]
9191 store = store ,
9292 extra_headers = extra_headers ,
9393 extra_body = kwargs ,
94- ) as response :
95- for line in response .iter_lines ():
96- json_string = line .replace ("data: " , "" )
97- json_string = json_string .strip ().rstrip ("\n " )
98- if json_string == "" :
99- continue
100- if json_string .startswith (":" ):
101- continue
102- elif json_string == "[DONE]" :
103- break
104- elif json_string != "" :
105- json_data = json .loads (json_string )
106- json_data = ChatCompletionChunk (** json_data )
107- yield json_data
108- else :
109- return ""
94+ )
11095
11196 def normal_create (
11297 self ,
@@ -488,7 +473,7 @@ async def stream_create(
488473 ** kwargs ,
489474 ) -> Union [ChatCompletions , AsyncIterator [ChatCompletionChunk ]]:
490475 extra_headers = kwargs .get ("extra_headers" , {})
491- async with self .openai_client . with_streaming_response .chat .completions .create (
476+ return await self .openai_client .chat .completions .create (
492477 model = model ,
493478 messages = messages ,
494479 stream = stream ,
@@ -504,22 +489,7 @@ async def stream_create(
504489 store = store ,
505490 extra_headers = extra_headers ,
506491 extra_body = kwargs ,
507- ) as response :
508- async for line in response .iter_lines ():
509- json_string = line .replace ("data: " , "" )
510- json_string = json_string .strip ().rstrip ("\n " )
511- if json_string == "" :
512- continue
513- if json_string .startswith (":" ):
514- continue
515- elif json_string == "[DONE]" :
516- break
517- elif json_string != "" :
518- json_data = json .loads (json_string )
519- json_data = ChatCompletionChunk (** json_data )
520- yield json_data
521- else :
522- pass
492+ )
523493
524494 async def normal_create (
525495 self ,
@@ -579,7 +549,7 @@ async def create(
579549 ** kwargs ,
580550 ) -> Union [ChatCompletions , AsyncIterator [ChatCompletionChunk ]]:
581551 if stream is True :
582- return self .stream_create (
552+ return await self .stream_create (
583553 model = model ,
584554 messages = messages ,
585555 stream = stream ,
0 commit comments