Skip to content

The agent engineering platform.

community[patch]: Add param "task" to Databricks LLM to work around serialization of transform_output_fn (#14933)

Liang Zhang6479aab74f72
diff --git a/libs/community/langchain_community/llms/databricks.py b/libs/community/langchain_community/llms/databricks.py
index a3f505b5c2..9333629f95 100644
--- a/libs/community/langchain_community/llms/databricks.py
+++ b/libs/community/langchain_community/llms/databricks.py
@@ -55,6 +55,10 @@ def _transform_completions(response: Dict[str, Any]) -> str:
return response["choices"][0]["text"]
+def _transform_llama2_chat(response: Dict[str, Any]) -> str:
+ return response["candidates"][0]["text"]
+
+
def _transform_chat(response: Dict[str, Any]) -> str:
return response["choices"][0]["message"]["content"]
@@ -87,11 +91,12 @@ class _DatabricksServingEndpointClient(_DatabricksClientBase):
"external_model",
"foundation_model_api",
)
- self.task = endpoint.get("task")
+ if self.task is None:
+ self.task = endpoint.get("task")
@property
def llm(self) -> bool:
- return self.task in ("llm/v1/chat", "llm/v1/completions")
+ return self.task in ("llm/v1/chat", "llm/v1/completions", "llama2/chat")
@root_validator(pre=True)
def set_api_url(cls, values: Dict[str, Any]) -> Dict[str, Any]:
@@ -125,6 +130,8 @@ class _DatabricksServingEndpointClient(_DatabricksClientBase):
preds = response["predictions"]
# For a single-record query, the result is not a list.
pred = preds[0] if isinstance(preds, list) else preds
+ if self.task == "llama2/chat":
+ return _transform_llama2_chat(pred)
return transform_output_fn(pred) if transform_output_fn else pred
@@ -325,6 +332,10 @@ class Databricks(LLM):
"""The maximum number of tokens to generate."""
extra_params: Dict[str, Any] = Field(default_factory=dict)
"""Any extra parameters to pass to the endpoint."""
+ task: Optional[str] = None
+ """The task of the endpoint. Only used when using a serving endpoint.
+ If not provided, the task is automatically inferred from the endpoint.
+ """
_client: _DatabricksClientBase = PrivateAttr()
@@ -401,6 +412,7 @@ class Databricks(LLM):
api_token=self.api_token,
endpoint_name=self.endpoint_name,
databricks_uri=self.databricks_uri,
+ task=self.task,
)
elif self.cluster_id and self.cluster_driver_port:
self._client = _DatabricksClusterDriverProxyClient(
@@ -430,6 +442,7 @@ class Databricks(LLM):
"stop": self.stop,
"max_tokens": self.max_tokens,
"extra_params": self.extra_params,
+ "task": self.task,
# TODO: Support saving transform_input_fn and transform_output_fn
# "transform_input_fn": self.transform_input_fn,
# "transform_output_fn": self.transform_output_fn,