Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
openai-python/openai/api_resources/chat_completion.py /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (39 sloc)
1.55 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| from openai import util | |
| from openai.api_resources.abstract.engine_api_resource import EngineAPIResource | |
| from openai.error import TryAgain | |
| class ChatCompletion(EngineAPIResource): | |
| engine_required = False | |
| OBJECT_NAME = "chat.completions" | |
| @classmethod | |
| def create(cls, *args, **kwargs): | |
| """ | |
| Creates a new chat completion for the provided messages and parameters. | |
| See https://platform.openai.com/docs/api-reference/chat-completions/create | |
| for a list of valid parameters. | |
| """ | |
| start = time.time() | |
| timeout = kwargs.pop("timeout", None) | |
| while True: | |
| try: | |
| return super().create(*args, **kwargs) | |
| except TryAgain as e: | |
| if timeout is not None and time.time() > start + timeout: | |
| raise | |
| util.log_info("Waiting for model to warm up", error=e) | |
| @classmethod | |
| async def acreate(cls, *args, **kwargs): | |
| """ | |
| Creates a new chat completion for the provided messages and parameters. | |
| See https://platform.openai.com/docs/api-reference/chat-completions/create | |
| for a list of valid parameters. | |
| """ | |
| start = time.time() | |
| timeout = kwargs.pop("timeout", None) | |
| while True: | |
| try: | |
| return await super().acreate(*args, **kwargs) | |
| except TryAgain as e: | |
| if timeout is not None and time.time() > start + timeout: | |
| raise | |
| util.log_info("Waiting for model to warm up", error=e) |