Skip to content

Commit 6572ef4

Browse files
adlternativehuzhening.hzn
andauthored
Add proxy argument support for openai client (#313)
Co-authored-by: huzhening.hzn <[email protected]>
1 parent bd269ab commit 6572ef4

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ openai api chat_completions.create -m gpt-3.5-turbo -g user "Hello world"
138138

139139
# generate images via DALL·E API
140140
openai api image.create -p "two dogs playing chess, cartoon" -n 1
141+
142+
# using openai through a proxy
143+
openai --proxy=http://proxy.com api models.list
141144
```
142145

143146
## Example code

openai/_openai_scripts.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def main():
3232
)
3333
parser.add_argument("-b", "--api-base", help="What API base url to use.")
3434
parser.add_argument("-k", "--api-key", help="What API key to use.")
35+
parser.add_argument("-p", "--proxy", nargs='+', help="What proxy to use.")
3536
parser.add_argument(
3637
"-o",
3738
"--organization",
@@ -65,6 +66,13 @@ def help(args):
6566
openai.api_base = args.api_base
6667
if args.organization is not None:
6768
openai.organization = args.organization
69+
if args.proxy is not None:
70+
openai.proxy = {}
71+
for proxy in args.proxy:
72+
if proxy.startswith('https'):
73+
openai.proxy['https'] = proxy
74+
elif proxy.startswith('http'):
75+
openai.proxy['http'] = proxy
6876

6977
try:
7078
args.func(args)

0 commit comments

Comments
 (0)