diff --git a/devchat/engine/util.py b/devchat/engine/util.py index 6abc49e4..ded2d9c8 100644 --- a/devchat/engine/util.py +++ b/devchat/engine/util.py @@ -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: diff --git a/devchat/llm/openai.py b/devchat/llm/openai.py index e2204ea5..1b4e938b 100644 --- a/devchat/llm/openai.py +++ b/devchat/llm/openai.py @@ -4,6 +4,7 @@ import re from typing import Dict, List +import httpx import openai from .pipeline import ( @@ -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 @@ -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 diff --git a/devchat/openai/http_openai.py b/devchat/openai/http_openai.py index a7607c17..e5ebdacd 100755 --- a/devchat/openai/http_openai.py +++ b/devchat/openai/http_openai.py @@ -1,7 +1,8 @@ import http.client import json +import os import sys - +from urllib.parse import urlparse class LineReader: def __init__(self, response): @@ -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) diff --git a/devchat/openai/openai_chat.py b/devchat/openai/openai_chat.py index 225d9bfc..6426f904 100755 --- a/devchat/openai/openai_chat.py +++ b/devchat/openai/openai_chat.py @@ -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) @@ -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( @@ -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) @@ -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(