jtsang4 / claude-to-chatgpt

This project converts the API of Anthropic's Claude model to the OpenAI Chat API format.
MIT License
1.27k stars 150 forks source link

Handle KeyError for 'completion' and add timeout for HTTPX client #10

Closed JamesFlare1212 closed 1 year ago

JamesFlare1212 commented 1 year ago

In this PR, we've made two improvements:

  1. 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.

  1. Implemented a timeout for the HTTPX client to avoid ReadTimeout errors. This change ensures the client operations do not hang indefinitely when there are network issues, by providing a predefined time limit for the request to either succeed or raise a timeout exception.

In both functions claude_to_chatgpt_response_stream and claude_to_chatgpt_response, claude_response.get("completion", "") is used instead of claude_response["completion"] to prevent KeyError if completion is not in claude_response. If completion is not present, it will return an empty string as default, and this empty string is then passed to num_tokens_from_string. Please adjust the default value according to your requirements.


It is related to https://github.com/jtsang4/claude-to-chatgpt/issues/6 https://github.com/jtsang4/claude-to-chatgpt/issues/7 and https://github.com/jtsang4/claude-to-chatgpt/issues/2 . I think I fixed it. At least fixed the error when long query was sent.

jtsang4 commented 1 year ago

Nice job! LGTM!