From dee025b7a325c94c9db0cfb70363ea52bb2822b8 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Tue, 23 Jan 2024 19:31:31 -0800 Subject: [PATCH] (docs) misc/cookbook - OpenAI python timeout --- cookbook/misc/openai_timeouts.py | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 cookbook/misc/openai_timeouts.py diff --git a/cookbook/misc/openai_timeouts.py b/cookbook/misc/openai_timeouts.py new file mode 100644 index 000000000..0192d7054 --- /dev/null +++ b/cookbook/misc/openai_timeouts.py @@ -0,0 +1,34 @@ +import os +from openai import OpenAI +from dotenv import load_dotenv +import httpx +import concurrent.futures + +load_dotenv() + +client = OpenAI( + # This is the default and can be omitted + api_key=os.environ.get("OPENAI_API_KEY"), +) + + +def create_chat_completion(): + return client.chat.completions.create( + messages=[ + { + "role": "user", + "content": "Say this is a test. Respond in 20 lines", + } + ], + model="gpt-3.5-turbo", + ) + + +with concurrent.futures.ThreadPoolExecutor() as executor: + # Set a timeout of 10 seconds + future = executor.submit(create_chat_completion) + try: + chat_completion = future.result(timeout=0.00001) + print(chat_completion) + except concurrent.futures.TimeoutError: + print("Operation timed out.")