Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions devchat/llm/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import sys
from functools import wraps

import openai

from devchat.memory import ChatMemory

from .openai import (
Expand Down Expand Up @@ -33,7 +31,7 @@
"content": None,
"function_name": None,
"parameters": "",
"error": err.type if isinstance(err, openai.APIError) else err,
"error": err,
},
)

Expand Down Expand Up @@ -63,7 +61,7 @@ def wrapper(*args, **kwargs):
else:
response = chat_completion_stream_out(messages, llm_config=llm_config)
if not response.get("content", None):
print(f"call {func.__name__} failed:", response["error"], file=sys.stderr)
print(response["error"], file=sys.stderr)
return None

if memory:
Expand Down
9 changes: 2 additions & 7 deletions devchat/llm/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,7 @@ def chat_completion_no_stream_return_json(messages: List[Dict], llm_config: Dict
),
times=3,
),
lambda err: {
"content": None,
"function_name": None,
"parameters": "",
"error": err.type if isinstance(err, openai.APIError) else err,
},
lambda err: {"content": None, "function_name": None, "parameters": "", "error": err},
)

chat_call_completion_stream = exception_handle(
Expand All @@ -212,6 +207,6 @@ def chat_completion_no_stream_return_json(messages: List[Dict], llm_config: Dict
"function_name": None,
"parameters": "",
"tool_calls": [],
"error": err.type if isinstance(err, openai.APIError) else err,
"error": err,
},
)
11 changes: 3 additions & 8 deletions devchat/llm/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import time
from typing import Dict

import openai

from devchat.ide import IDEService


Expand All @@ -19,12 +17,12 @@ def wrapper(*args, **kwargs):
return func(*args, **kwargs)
except RetryException as err:
if index + 1 == times:
raise err.error
raise err
IDEService().ide_logging("debug", f"has retries: {index + 1}")
continue
except Exception as err:
IDEService().ide_logging("info", f"exception: {err}")
raise err.error
raise err

return wrapper

Expand All @@ -42,10 +40,7 @@ def wrapper(*args, **kwargs):

def exception_output_handle(func):
def wrapper(err):
if isinstance(err, openai.APIError):
print(err.type, file=sys.stderr, flush=True)
else:
print(err, file=sys.stderr, flush=True)
print(err, file=sys.stderr, flush=True)
return func(err)

return wrapper
Expand Down
73 changes: 40 additions & 33 deletions devchat/openai/http_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def stream_response(connection: http.client.HTTPSConnection, data, headers):

if response.status != 200:
print(
f"Error: {response.status} - {response.reason} {response.read()}",
f"received status code: {response.status} - reason: {response.reason}\n\n"
f"response: {response.read()}",
end="\n\n",
file=sys.stderr,
)
Expand All @@ -49,40 +50,46 @@ def stream_response(connection: http.client.HTTPSConnection, data, headers):


def stream_request(api_key, api_base, data):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
try:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}

if api_base.startswith("https://"):
url = api_base[8:]
elif api_base.startswith("http://"):
url = api_base[7:]
else:
print("Invalid API base URL", end="\n\n", file=sys.stderr)
raise ValueError("Invalid API base URL")
if api_base.startswith("https://"):
url = api_base[8:]
elif api_base.startswith("http://"):
url = api_base[7:]
else:
print("Invalid API base URL", end="\n\n", file=sys.stderr)
raise ValueError("Invalid API base URL")

url = url.split("/")[0]
proxy_url = os.environ.get("DEVCHAT_PROXY", "")
parsed_url = urlparse(proxy_url)
proxy_setting = {
"host": parsed_url.hostname,
**({"port": parsed_url.port} if parsed_url.port else {}),
}
url = url.split("/")[0]
proxy_url = os.environ.get("DEVCHAT_PROXY", "")
parsed_url = urlparse(proxy_url)
proxy_setting = {
"host": parsed_url.hostname,
**({"port": parsed_url.port} if parsed_url.port else {}),
}

if api_base.startswith("https://"):
if proxy_setting["host"]:
connection = http.client.HTTPSConnection(
**proxy_setting, context=ssl._create_unverified_context()
)
connection.set_tunnel(url)
else:
connection = http.client.HTTPSConnection(url, context=ssl._create_unverified_context())
else:
if proxy_setting["host"]:
connection = http.client.HTTPConnection(**proxy_setting)
connection.set_tunnel(url)
if api_base.startswith("https://"):
if proxy_setting["host"]:
connection = http.client.HTTPSConnection(
**proxy_setting, context=ssl._create_unverified_context()
)
connection.set_tunnel(url)
else:
connection = http.client.HTTPSConnection(
url, context=ssl._create_unverified_context()
)
else:
connection = http.client.HTTPConnection(url)
if proxy_setting["host"]:
connection = http.client.HTTPConnection(**proxy_setting)
connection.set_tunnel(url)
else:
connection = http.client.HTTPConnection(url)

return stream_response(connection, data, headers)
return stream_response(connection, data, headers)
except Exception as err:
print(err, end="\n\n", file=sys.stderr)
return None