|
17 | 17 | from openai.util import convert_to_openai_object |
18 | 18 | else: |
19 | 19 | from openai.types.chat import ChatCompletion as OpenAIObject |
20 | | - from openai.types.chat import ParsedChatCompletion as OpenAIObjectParsed |
21 | | - from openai.types.chat import ParsedChatCompletionMessage |
22 | 20 |
|
23 | 21 | def convert_to_openai_object(kwargs) -> OpenAIObject: |
24 | 22 | if "id" not in kwargs: |
@@ -57,9 +55,16 @@ def get_original_methods(self, module_client=openai): |
57 | 55 | original_methods = {"ChatCompletion.create": module_client.ChatCompletion.create, "ChatCompletion.acreate": module_client.ChatCompletion.acreate} |
58 | 56 | else: |
59 | 57 | try: |
60 | | - original_methods = {"chat.completions.create": module_client.chat.completions.create, "beta.chat.completions.parse": module_client.beta.chat.completions.parse} |
| 58 | + original_methods = {"chat.completions.create": module_client.chat.completions.create} |
61 | 59 | except openai.OpenAIError: |
62 | 60 | original_methods = {} |
| 61 | + |
| 62 | + try: |
| 63 | + latest_methods = {"beta.chat.completions.parse": module_client.beta.chat.completions.parse} |
| 64 | + original_methods.update(latest_methods) |
| 65 | + except Exception: |
| 66 | + pass |
| 67 | + |
63 | 68 | return list(original_methods.keys()) |
64 | 69 |
|
65 | 70 | def init(self, log: Callable, cache: Cache = None, module_client=openai): |
@@ -271,7 +276,13 @@ def _kwargs_to_llm_configuration(kwargs): |
271 | 276 |
|
272 | 277 | @staticmethod |
273 | 278 | def _get_output(result: Any, model: Optional[str] = None) -> str: |
274 | | - if not isinstance(result, (OpenAIObject, OpenAIObjectParsed)) and isinstance(result, dict): |
| 279 | + try: |
| 280 | + from openai.types.chat import ParsedChatCompletion, ParsedChatCompletionMessage |
| 281 | + except ImportError: |
| 282 | + ParsedChatCompletion = None |
| 283 | + ParsedChatCompletionMessage = None |
| 284 | + |
| 285 | + if not isinstance(result, (OpenAIObject, ParsedChatCompletion)) and isinstance(result, dict): |
275 | 286 | result = convert_to_openai_object( |
276 | 287 | { |
277 | 288 | "choices": [ |
|
0 commit comments