Skip to content

Commit

Permalink
Update parsing async openai response
Browse files Browse the repository at this point in the history
  • Loading branch information
kxtran committed May 29, 2024
1 parent bc72c3e commit 4b6d3b2
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions log10/_httpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,38 +338,43 @@ def parse_anthropic_responses(self, responses: list[str]):

def parse_openai_responses(self, responses: list[str]):
r_json = None
last_message_choice = responses[-4]
for r in responses:
if self.is_openai_response_end_reached(r):
break

# loading the substring of response text after 'data: '.
# example: 'data: {"choices":[{"text":"Hello, how can I help you today?"}]}'
r_json = json.loads(r[6:])
delta = r_json["choices"][0]["delta"]

# Delta may have content
if "content" in delta:
content = delta["content"]
if content:
self.full_content += content

# May be a function call, and have to reconstruct the arguments
if "function_call" in delta:
# May be function name
if "name" in delta["function_call"]:
self.function_name = delta["function_call"]["name"]
# May be function arguments
if "arguments" in delta["function_call"]:
self.full_argument += delta["function_call"]["arguments"]

if tc := delta.get("tool_calls", []):
if tc[0].get("id", ""):
self.tool_calls.append(tc[0])
elif tc[0].get("function", {}).get("arguments", ""):
idx = tc[0].get("index")
self.tool_calls[idx]["function"]["arguments"] += tc[0]["function"]["arguments"]

if r_json["choices"] and "delta" in r_json["choices"][0]:
delta = r_json["choices"][0]["delta"]

# Delta may have content
if "content" in delta:
content = delta["content"]
if content:
self.full_content += content

# May be a function call, and have to reconstruct the arguments
if "function_call" in delta:
# May be function name
if "name" in delta["function_call"]:
self.function_name = delta["function_call"]["name"]
# May be function arguments
if "arguments" in delta["function_call"]:
self.full_argument += delta["function_call"]["arguments"]

if tc := delta.get("tool_calls", []):
if tc[0].get("id", ""):
self.tool_calls.append(tc[0])
elif tc[0].get("function", {}).get("arguments", ""):
idx = tc[0].get("index")
self.tool_calls[idx]["function"]["arguments"] += tc[0]["function"]["arguments"]

last_message_choice_json = json.loads(last_message_choice[6:])
r_json["object"] = "chat.completion"
r_json["choices"] = last_message_choice_json["choices"]
return r_json

def parse_response_data(self, responses: list[str]):
Expand Down

0 comments on commit 4b6d3b2

Please sign in to comment.