Skip to content

Commit

Permalink
cohere[patch]: Allow overriding of the base URL in Cohere Client (lan…
Browse files Browse the repository at this point in the history
…gchain-ai#19766)

This PR adds the ability for a user to override the base API url for the
Cohere client for embeddings and chat llm.
  • Loading branch information
BeatrixCohere authored Mar 29, 2024
1 parent 1252ccc commit bd02b83
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libs/partners/cohere/langchain_cohere/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class CohereEmbeddings(BaseModel, Embeddings):
user_agent: str = "langchain"
"""Identifier for the application making the request."""

base_url: Optional[str] = None
"""Override the default Cohere API URL."""

class Config:
"""Configuration for this pydantic object."""

Expand All @@ -64,11 +67,13 @@ def validate_environment(cls, values: Dict) -> Dict:
cohere_api_key,
timeout=request_timeout,
client_name=client_name,
base_url=values["base_url"],
)
values["async_client"] = cohere.AsyncClient(
cohere_api_key,
timeout=request_timeout,
client_name=client_name,
base_url=values["base_url"],
)

return values
Expand Down
5 changes: 5 additions & 0 deletions libs/partners/cohere/langchain_cohere/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class BaseCohere(Serializable):
user_agent: str = "langchain"
"""Identifier for the application making the request."""

base_url: Optional[str] = None
"""Override the default Cohere API URL."""

@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
Expand All @@ -79,10 +82,12 @@ def validate_environment(cls, values: Dict) -> Dict:
values["client"] = cohere.Client(
api_key=values["cohere_api_key"].get_secret_value(),
client_name=client_name,
base_url=values["base_url"],
)
values["async_client"] = cohere.AsyncClient(
api_key=values["cohere_api_key"].get_secret_value(),
client_name=client_name,
base_url=values["base_url"],
)
return values

Expand Down

0 comments on commit bd02b83

Please sign in to comment.