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
7 changes: 6 additions & 1 deletion devchat/engine/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ def select_function_by_llm(
history_messages: List[Dict], tools: List[Dict], model: str = DEFAULT_MODEL
):
import openai
import httpx
proxy_url = os.environ.get("DEVCHAT_PROXY", "")
proxy_setting ={"proxy": {"https://": proxy_url, "http://": proxy_url}} if proxy_url else {}

client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", None),
base_url=os.environ.get("OPENAI_API_BASE", None)
base_url=os.environ.get("OPENAI_API_BASE", None),
http_client=httpx.Client(**proxy_setting, trust_env=False)
)

try:
Expand Down
9 changes: 9 additions & 0 deletions devchat/llm/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
from typing import Dict, List

import httpx
import openai

from .pipeline import (
Expand Down Expand Up @@ -39,9 +40,13 @@ def chat_completion_stream_commit(
messages: List[Dict], # [{"role": "user", "content": "hello"}]
llm_config: Dict, # {"model": "...", ...}
):
proxy_url = os.environ.get("DEVCHAT_PROXY", "")
proxy_setting ={"proxy": {"https://": proxy_url, "http://": proxy_url}} if proxy_url else {}

client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", None),
base_url=os.environ.get("OPENAI_API_BASE", None),
http_client=httpx.Client(**proxy_setting, trust_env=False)
)

llm_config["stream"] = True
Expand All @@ -50,9 +55,13 @@ def chat_completion_stream_commit(


def chat_completion_stream_raw(**kwargs):
proxy_url = os.environ.get("DEVCHAT_PROXY", "")
proxy_setting ={"proxy": {"https://": proxy_url, "http://": proxy_url}} if proxy_url else {}

client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", None),
base_url=os.environ.get("OPENAI_API_BASE", None),
http_client=httpx.Client(**proxy_setting, trust_env=False)
)

kwargs["stream"] = True
Expand Down
21 changes: 18 additions & 3 deletions devchat/openai/http_openai.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import http.client
import json
import os
import sys

from urllib.parse import urlparse

class LineReader:
def __init__(self, response):
Expand Down Expand Up @@ -55,10 +56,24 @@ def stream_request(api_key, api_base, data):
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 {}),
}

if api_base.startswith("https://"):
connection = http.client.HTTPSConnection(url)
if proxy_setting["host"]:
connection = http.client.HTTPSConnection(**proxy_setting)
connection.set_tunnel(url)
else:
connection = http.client.HTTPSConnection(url)
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)
14 changes: 12 additions & 2 deletions devchat/openai/openai_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def load_prompt(self, data: dict) -> OpenAIPrompt:

def complete_response(self, prompt: OpenAIPrompt) -> str:
import openai
import httpx

# Filter the config parameters with set values
config_params = self.config.dict(exclude_unset=True)
Expand All @@ -71,9 +72,13 @@ def complete_response(self, prompt: OpenAIPrompt) -> str:
config_params['function_call'] = 'auto'
config_params['stream'] = False

proxy_url = os.environ.get("DEVCHAT_PROXY", "")
proxy_setting ={"proxy": {"https://": proxy_url, "http://": proxy_url}} if proxy_url else {}

client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", None),
base_url=os.environ.get("OPENAI_API_BASE", None)
base_url=os.environ.get("OPENAI_API_BASE", None),
http_client=httpx.Client(**proxy_setting, trust_env=False)
)

response = client.chat.completions.create(
Expand Down Expand Up @@ -103,6 +108,7 @@ def stream_response(self, prompt: OpenAIPrompt) -> Iterator:
response = stream_request(api_key, base_url, data)
return response
import openai
import httpx

# Filter the config parameters with set values
config_params = self.config.dict(exclude_unset=True)
Expand All @@ -111,9 +117,13 @@ def stream_response(self, prompt: OpenAIPrompt) -> Iterator:
config_params['function_call'] = 'auto'
config_params['stream'] = True

proxy_url = os.environ.get("DEVCHAT_PROXY", "")
proxy_setting ={"proxy": {"https://": proxy_url, "http://": proxy_url}} if proxy_url else {}

client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", None),
base_url=os.environ.get("OPENAI_API_BASE", None)
base_url=os.environ.get("OPENAI_API_BASE", None),
http_client=httpx.Client(**proxy_setting, trust_env=False)
)

response = client.chat.completions.create(
Expand Down