Skip to content

Commit

Permalink
Handle KeyError for 'completion' in ClaudeAdapter
Browse files Browse the repository at this point in the history
In this commit, we've added error handling for scenarios where the key 'completion' is not present in the 'claude_response' dictionary. This exception was causing the program to crash when the application tried to access the 'completion' key in the 'claude_response' dictionary.

Now, the 'get' method is used on the dictionary which allows the program to provide a default value of an empty string if the 'completion' key is not present. This prevents a KeyError from being raised and allows the program to continue execution. The affected methods are 'claude_to_chatgpt_response_stream' and 'claude_to_chatgpt_response'.

This is an important fix that improves the robustness of the code by handling potential exceptions arising from unexpected data structures.
  • Loading branch information
JamesFlare1212 committed Jun 13, 2023
1 parent dad538c commit de2cd5c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions claude_to_chatgpt/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def openai_to_claude_params(self, openai_params):
return claude_params

def claude_to_chatgpt_response_stream(self, claude_response, prev_decoded_response):
completion_tokens = num_tokens_from_string(claude_response["completion"])
completion_tokens = num_tokens_from_string(claude_response.get("completion", ""))
openai_response = {
"id": f"chatcmpl-{str(time.time())}",
"object": "chat.completion.chunk",
Expand Down Expand Up @@ -98,7 +98,7 @@ def claude_to_chatgpt_response_stream(self, claude_response, prev_decoded_respon
return openai_response

def claude_to_chatgpt_response(self, claude_response):
completion_tokens = num_tokens_from_string(claude_response["completion"])
completion_tokens = num_tokens_from_string(claude_response.get("completion", ""))
openai_response = {
"id": f"chatcmpl-{str(time.time())}",
"object": "chat.completion",
Expand Down

0 comments on commit de2cd5c

Please sign in to comment.