forked from Westlake-AGI-Lab/Auto-Slides
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_openai.py
More file actions
91 lines (75 loc) · 2.71 KB
/
Copy pathpatch_openai.py
File metadata and controls
91 lines (75 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
"""
Fix OpenAI client initialization proxies parameter issue
"""
import os
import sys
import logging
import importlib.util
import inspect
from types import FunctionType, MethodType
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def patch_openai_client():
"""
Patch OpenAI client to remove proxies parameter
"""
try:
# Try to import OpenAI client
import openai
from openai import OpenAI
# Get original __init__ method
original_init = OpenAI.__init__
# Define new __init__ method
def patched_init(self, *args, **kwargs):
# Remove proxies parameter if it exists
if 'proxies' in kwargs:
logging.info("Removed 'proxies' parameter")
del kwargs['proxies']
# Call original __init__ method
return original_init(self, *args, **kwargs)
# Replace __init__ method
OpenAI.__init__ = patched_init
logging.info("Successfully patched OpenAI client")
return True
except Exception as e:
logging.error(f"Failed to patch OpenAI client: {str(e)}")
import traceback
traceback.print_exc()
return False
def patch_langchain_openai():
"""
Patch LangChain OpenAI integration
"""
try:
# Try to import LangChain OpenAI
import langchain_openai
from langchain_openai.chat_models import ChatOpenAI
# Get original __init__ method
original_init = ChatOpenAI.__init__
# Define new __init__ method
def patched_init(self, *args, **kwargs):
# Remove proxies parameter if it exists
if 'proxies' in kwargs:
logging.info("Removed 'proxies' parameter from ChatOpenAI")
del kwargs['proxies']
# Call original __init__ method
return original_init(self, *args, **kwargs)
# Replace __init__ method
ChatOpenAI.__init__ = patched_init
logging.info("Successfully patched LangChain ChatOpenAI")
return True
except Exception as e:
logging.error(f"Failed to patch LangChain ChatOpenAI: {str(e)}")
import traceback
traceback.print_exc()
return False
# Apply patches when run as script
if __name__ == "__main__":
success1 = patch_openai_client()
success2 = patch_langchain_openai()
if success1 and success2:
logging.info("All patches applied successfully")
else:
logging.warning("Some patches failed to apply")
sys.exit(0 if (success1 or success2) else 1)