From ea4f2e722feb0626bf6dd7f1751e234baa36fb74 Mon Sep 17 00:00:00 2001 From: agonza1 Date: Wed, 3 Apr 2024 23:24:21 -0400 Subject: [PATCH] log openai error --- src/demo_utils/inference_engine.py | 138 ++++++++++++++++------------- 1 file changed, 76 insertions(+), 62 deletions(-) diff --git a/src/demo_utils/inference_engine.py b/src/demo_utils/inference_engine.py index 47a7acf..d86b217 100644 --- a/src/demo_utils/inference_engine.py +++ b/src/demo_utils/inference_engine.py @@ -102,45 +102,54 @@ def generate(self, prompt: list = None, max_new_tokens=4096, temperature=None, m prompt2 = prompt[2] if turn_number == 0: - base64_image = encode_image(image_path) - # Assume one turn dialogue - prompt1_input = [ - {"role": "system", "content": [{"type": "text", "text": prompt0}]}, - {"role": "user", - "content": [{"type": "text", "text": prompt1}, {"type": "image_url", "image_url": {"url": - f"data:image/jpeg;base64,{base64_image}", - "detail": "high"}, - }]}, - ] - response1 = openai.ChatCompletion.create( - model=model if model else self.model, - messages=prompt1_input, - max_tokens=max_new_tokens if max_new_tokens else 4096, - temperature=temperature if temperature else self.temperature, - **kwargs, - ) - answer1 = [choice["message"]["content"] for choice in response1["choices"]][0] + try: + base64_image = encode_image(image_path) + # Assume one turn dialogue + prompt1_input = [ + {"role": "system", "content": [{"type": "text", "text": prompt0}]}, + {"role": "user", + "content": [{"type": "text", "text": prompt1}, {"type": "image_url", "image_url": {"url": + f"data:image/jpeg;base64,{base64_image}", + "detail": "high"}, + }]}, + ] + response1 = openai.ChatCompletion.create( + model=model if model else self.model, + messages=prompt1_input, + max_tokens=max_new_tokens if max_new_tokens else 4096, + temperature=temperature if temperature else self.temperature, + **kwargs, + ) + answer1 = [choice["message"]["content"] for choice in response1["choices"]][0] + + return answer1 + except Exception as ex: + print(f"Error: {ex}") + raise APIError("An error occurred prompting OpenAI") - return answer1 elif turn_number == 1: - base64_image = encode_image(image_path) - prompt2_input = [ - {"role": "system", "content": [{"type": "text", "text": prompt0}]}, - {"role": "user", - "content": [{"type": "text", "text": prompt1}, {"type": "image_url", "image_url": {"url": - f"data:image/jpeg;base64,{base64_image}", - "detail": "high"}, }]}, - {"role": "assistant", "content": [{"type": "text", "text": f"\n\n{ouput__0}"}]}, - {"role": "user", "content": [{"type": "text", "text": prompt2}]}, ] - response2 = openai.ChatCompletion.create( - model=model if model else self.model, - messages=prompt2_input, - max_tokens=max_new_tokens if max_new_tokens else 4096, - temperature=temperature if temperature else self.temperature, - **kwargs, - ) - return [choice["message"]["content"] for choice in response2["choices"]][0] - + try: + base64_image = encode_image(image_path) + prompt2_input = [ + {"role": "system", "content": [{"type": "text", "text": prompt0}]}, + {"role": "user", + "content": [{"type": "text", "text": prompt1}, {"type": "image_url", "image_url": {"url": + f"data:image/jpeg;base64,{base64_image}", + "detail": "high"}, }]}, + {"role": "assistant", "content": [{"type": "text", "text": f"\n\n{ouput__0}"}]}, + {"role": "user", "content": [{"type": "text", "text": prompt2}]}, ] + response2 = openai.ChatCompletion.create( + model=model if model else self.model, + messages=prompt2_input, + max_tokens=max_new_tokens if max_new_tokens else 4096, + temperature=temperature if temperature else self.temperature, + **kwargs, + ) + return [choice["message"]["content"] for choice in response2["choices"]][0] + + except Exception as ex: + print(f"Error: {ex}") + raise APIError("An error occurred prompting OpenAI") class OpenaiEngine_MindAct(Engine): def __init__( @@ -185,29 +194,34 @@ def __init__( (APIError, RateLimitError, APIConnectionError, ServiceUnavailableError), ) def generate(self, prompt, max_new_tokens=50, temperature=0, model=None, **kwargs): - self.current_key_idx = (self.current_key_idx + 1) % len(self.api_keys) - start_time = time.time() - if ( - self.request_interval > 0 - and start_time < self.next_avil_time[self.current_key_idx] - ): - time.sleep(self.next_avil_time[self.current_key_idx] - start_time) - openai.api_key = self.api_keys[self.current_key_idx] - if isinstance(prompt, str): - # Assume one turn dialogue - prompt = [ - {"role": "user", "content": prompt}, - ] - response = openai.ChatCompletion.create( - model=model if model else self.model, - messages=prompt, - max_tokens=max_new_tokens, - temperature=temperature, - **kwargs, - ) - if self.request_interval > 0: - self.next_avil_time[self.current_key_idx] = ( - max(start_time, self.next_avil_time[self.current_key_idx]) - + self.request_interval + try: + self.current_key_idx = (self.current_key_idx + 1) % len(self.api_keys) + start_time = time.time() + if ( + self.request_interval > 0 + and start_time < self.next_avil_time[self.current_key_idx] + ): + time.sleep(self.next_avil_time[self.current_key_idx] - start_time) + openai.api_key = self.api_keys[self.current_key_idx] + if isinstance(prompt, str): + # Assume one turn dialogue + prompt = [ + {"role": "user", "content": prompt}, + ] + response = openai.ChatCompletion.create( + model=model if model else self.model, + messages=prompt, + max_tokens=max_new_tokens, + temperature=temperature, + **kwargs, ) - return [choice["message"]["content"] for choice in response["choices"]] + if self.request_interval > 0: + self.next_avil_time[self.current_key_idx] = ( + max(start_time, self.next_avil_time[self.current_key_idx]) + + self.request_interval + ) + return [choice["message"]["content"] for choice in response["choices"]] + + except Exception as ex: + print(f"Error: {ex}") + raise APIError("An error occurred prompting OpenAI")