Skip to content

Commit

Permalink
Supported custom ernie_api_base for Ernie (langchain-ai#10416)
Browse files Browse the repository at this point in the history
Description: Supported custom ernie_api_base for Ernie
 - ernie_api_base:Support Ernie custom endpoints
 - Rectifying omitted code modifications. langchain-ai#10398

Issue: None
Dependencies: None
Tag maintainer: @baskaryan 
Twitter handle: @JohnMai95
  • Loading branch information
johnmai-dev authored Sep 11, 2023
1 parent 70b6897 commit b50d724
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions libs/langchain/langchain/chat_models/ernie.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class ErnieBotChat(BaseChatModel):
"""

ernie_api_base: Optional[str] = None
"""Baidu application custom endpoints"""

ernie_client_id: Optional[str] = None
"""Baidu application client id"""

Expand Down Expand Up @@ -84,6 +87,9 @@ class ErnieBotChat(BaseChatModel):

@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
values["ernie_api_base"] = get_from_dict_or_env(
values, "ernie_api_base", "ERNIE_API_BASE", "https://aip.baidubce.com"
)
values["ernie_client_id"] = get_from_dict_or_env(
values,
"ernie_client_id",
Expand All @@ -97,7 +103,7 @@ def validate_environment(cls, values: Dict) -> Dict:
return values

def _chat(self, payload: object) -> dict:
base_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat"
base_url = f"{self.ernie_api_base}/rpc/2.0/ai_custom/v1/wenxinworkshop/chat"
model_paths = {
"ERNIE-Bot-turbo": "eb-instant",
"ERNIE-Bot": "completions",
Expand Down Expand Up @@ -125,7 +131,7 @@ def _chat(self, payload: object) -> dict:
def _refresh_access_token_with_lock(self) -> None:
with self._lock:
logger.debug("Refreshing access token")
base_url: str = "https://aip.baidubce.com/oauth/2.0/token"
base_url: str = f"{self.ernie_api_base}/oauth/2.0/token"
resp = requests.post(
base_url,
timeout=10,
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain/langchain/embeddings/ernie.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _embedding(self, json: object) -> dict:
def _refresh_access_token_with_lock(self) -> None:
with self._lock:
logger.debug("Refreshing access token")
base_url: str = "https://aip.baidubce.com/oauth/2.0/token"
base_url: str = f"{self.ernie_api_base}/oauth/2.0/token"
resp = requests.post(
base_url,
headers={
Expand Down

0 comments on commit b50d724

Please sign in to comment.