fix: httpx v0.28.0 proxies bug (#2179)

Co-authored-by: Fangyin Cheng <staneyffer@gmail.com>
This commit is contained in:
Aries-ckt 2024-12-12 10:34:31 +08:00 committed by GitHub
parent abab4e3e65
commit 4da1809b31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 8 deletions

View File

@ -48,6 +48,7 @@ class OpenAIParameters:
api_azure_deployment: Optional[str] = None api_azure_deployment: Optional[str] = None
full_url: Optional[str] = None full_url: Optional[str] = None
proxies: Optional["ProxiesTypes"] = None proxies: Optional["ProxiesTypes"] = None
proxy: Optional["ProxyTypes"] = None
def _initialize_openai_v1(init_params: OpenAIParameters): def _initialize_openai_v1(init_params: OpenAIParameters):
@ -142,19 +143,28 @@ def _build_openai_client(init_params: OpenAIParameters) -> Tuple[str, ClientType
if api_type == "azure": if api_type == "azure":
from openai import AsyncAzureOpenAI from openai import AsyncAzureOpenAI
return api_type, AsyncAzureOpenAI( async_client = AsyncAzureOpenAI(
api_key=openai_params["api_key"], api_key=openai_params["api_key"],
api_version=api_version, api_version=api_version,
azure_deployment=api_azure_deployment, azure_deployment=api_azure_deployment,
azure_endpoint=openai_params["base_url"], azure_endpoint=openai_params["base_url"],
http_client=httpx.AsyncClient(proxies=init_params.proxies),
) )
else: else:
from openai import AsyncOpenAI from openai import AsyncOpenAI
return api_type, AsyncOpenAI( # Remove proxies for httpx AsyncClient when httpx version >= 0.28.0
**openai_params, http_client=httpx.AsyncClient(proxies=init_params.proxies) httpx_version = metadata.version("httpx")
) if httpx_version >= "0.28.0":
if init_params.proxy:
http_client = httpx.AsyncClient(proxy=init_params.proxy)
else:
http_client = httpx.AsyncClient()
elif init_params.proxies:
http_client = httpx.AsyncClient(proxies=init_params.proxies)
else:
http_client = httpx.AsyncClient()
async_client = AsyncOpenAI(**openai_params, http_client=http_client)
return api_type, async_client
class OpenAIStreamingOutputOperator(TransformStreamAbsOperator[ModelOutput, str]): class OpenAIStreamingOutputOperator(TransformStreamAbsOperator[ModelOutput, str]):

View File

@ -16,7 +16,7 @@ from dbgpt.rag.knowledge.factory import KnowledgeFactory
from dbgpt.util.i18n_utils import _ from dbgpt.util.i18n_utils import _
class KnowledgeOperator(MapOperator[str, Knowledge]): class KnowledgeOperator(MapOperator[dict, Knowledge]):
"""Knowledge Factory Operator.""" """Knowledge Factory Operator."""
metadata = ViewMetadata( metadata = ViewMetadata(
@ -91,10 +91,11 @@ class KnowledgeOperator(MapOperator[str, Knowledge]):
async def map(self, datasource: dict) -> Knowledge: async def map(self, datasource: dict) -> Knowledge:
"""Create knowledge from datasource.""" """Create knowledge from datasource."""
source = datasource.get("source")
if self._datasource: if self._datasource:
datasource = self._datasource source = self._datasource
return await self.blocking_func_to_async( return await self.blocking_func_to_async(
KnowledgeFactory.create, datasource, self._knowledge_type KnowledgeFactory.create, source, self._knowledge_type
) )