Skip to content

Commit

Permalink
Temporarily disable certificate verification on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
w0rp committed Nov 17, 2023
1 parent 186fced commit a4f742a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 14 additions & 1 deletion neural_sources/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
A Neural datasource for ChatGPT conversations.
"""
import json
import platform
import ssl
import sys
import urllib.error
import urllib.request
Expand Down Expand Up @@ -67,7 +69,18 @@ def get_chatgpt_completion(
)
role: Optional[str] = None

with urllib.request.urlopen(req) as response:
# Disable SSL certificate verification on macOS.
# This is bad for security, and we need to deal with SSL errors better.
#
# This is the error:
# urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)> # noqa
context = (
ssl._create_unverified_context() # type: ignore
if platform.system() == "Darwin" else
None
)

with urllib.request.urlopen(req, context=context) as response:
while True:
line_bytes = response.readline()

Expand Down
17 changes: 16 additions & 1 deletion neural_sources/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
A Neural datasource for loading generated text via OpenAI.
"""
import json
import platform
import ssl
import sys
import urllib.error
import urllib.request
Expand Down Expand Up @@ -51,14 +53,27 @@ def get_openai_completion(config: Config, prompt: str) -> None:
"frequency_penalty": config.frequency_penalty,
"stream": True,
}

req = urllib.request.Request(
API_ENDPOINT,
data=json.dumps(data).encode("utf-8"),
headers=headers,
method="POST",
unverifiable=True,
)

# Disable SSL certificate verification on macOS.
# This is bad for security, and we need to deal with SSL errors better.
#
# This is the error:
# urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)> # noqa
context = (
ssl._create_unverified_context() # type: ignore
if platform.system() == "Darwin" else
None
)

with urllib.request.urlopen(req) as response:
with urllib.request.urlopen(req, context=context) as response:
while True:
line_bytes = response.readline()

Expand Down

0 comments on commit a4f742a

Please sign in to comment.