langchain.llms.databricks.Databricks¶
- class langchain.llms.databricks.Databricks(*, cache: Optional[bool] = None, verbose: bool = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, callback_manager: Optional[BaseCallbackManager] = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, host: str = None, api_token: str = None, endpoint_name: Optional[str] = None, cluster_id: Optional[str] = None, cluster_driver_port: Optional[str] = None, model_kwargs: Optional[Dict[str, Any]] = None, transform_input_fn: Optional[Callable] = None, transform_output_fn: Optional[Callable[[...], str]] = None)[source]¶
Bases:
LLMDatabricks serving endpoint or a cluster driver proxy app for LLM.
It supports two endpoint types:
Serving endpoint (recommended for both production and development). We assume that an LLM was registered and deployed to a serving endpoint. To wrap it as an LLM you must have “Can Query” permission to the endpoint. Set
endpoint_nameaccordingly and do not setcluster_idandcluster_driver_port. The expected model signature is:inputs:
[{"name": "prompt", "type": "string"}, {"name": "stop", "type": "list[string]"}]
outputs:
[{"type": "string"}]
Cluster driver proxy app (recommended for interactive development). One can load an LLM on a Databricks interactive cluster and start a local HTTP server on the driver node to serve the model at
/using HTTP POST method with JSON input/output. Please use a port number between[3000, 8000]and let the server listen to the driver IP address or simply0.0.0.0instead of localhost only. To wrap it as an LLM you must have “Can Attach To” permission to the cluster. Setcluster_idandcluster_driver_portand do not setendpoint_name. The expected server schema (using JSON schema) is:inputs:
{"type": "object", "properties": { "prompt": {"type": "string"}, "stop": {"type": "array", "items": {"type": "string"}}}, "required": ["prompt"]}`outputs:
{"type": "string"}
If the endpoint model signature is different or you want to set extra params, you can use transform_input_fn and transform_output_fn to apply necessary transformations before and after the query.
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
- param api_token: str [Optional]¶
Databricks personal access token. If not provided, the default value is determined by
the
DATABRICKS_TOKENenvironment variable if present, oran automatically generated temporary token if running inside a Databricks notebook attached to an interactive cluster in “single user” or “no isolation shared” mode.
- param cache: Optional[bool] = None¶
- param callback_manager: Optional[BaseCallbackManager] = None¶
- param callbacks: Callbacks = None¶
- param cluster_driver_port: Optional[str] = None¶
The port number used by the HTTP server running on the cluster driver node. The server should listen on the driver IP address or simply
0.0.0.0to connect. We recommend the server using a port number between[3000, 8000].
- param cluster_id: Optional[str] = None¶
ID of the cluster if connecting to a cluster driver proxy app. If neither
endpoint_namenorcluster_idis not provided and the code runs inside a Databricks notebook attached to an interactive cluster in “single user” or “no isolation shared” mode, the current cluster ID is used as default. You must not set bothendpoint_nameandcluster_id.
- param endpoint_name: Optional[str] = None¶
Name of the model serving endpoint. You must specify the endpoint name to connect to a model serving endpoint. You must not set both
endpoint_nameandcluster_id.
- param host: str [Optional]¶
Databricks workspace hostname. If not provided, the default value is determined by
the
DATABRICKS_HOSTenvironment variable if present, orthe hostname of the current Databricks workspace if running inside a Databricks notebook attached to an interactive cluster in “single user” or “no isolation shared” mode.
- param metadata: Optional[Dict[str, Any]] = None¶
Metadata to add to the run trace.
- param model_kwargs: Optional[Dict[str, Any]] = None¶
Extra parameters to pass to the endpoint.
- param tags: Optional[List[str]] = None¶
Tags to add to the run trace.
- param transform_input_fn: Optional[Callable] = None¶
A function that transforms
{prompt, stop, **kwargs}into a JSON-compatible request object that the endpoint accepts. For example, you can apply a prompt template to the input prompt.
- param transform_output_fn: Optional[Callable[[...], str]] = None¶
A function that transforms the output from the endpoint to the generated text.
- param verbose: bool [Optional]¶
Whether to print out response text.
- __call__(prompt: str, stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) str¶
Check Cache and run the LLM on the given prompt and input.
- async abatch(inputs: List[Union[PromptValue, str, List[BaseMessage]]], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, max_concurrency: Optional[int] = None, **kwargs: Any) List[str]¶
- async agenerate(prompts: List[str], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, *, tags: Optional[Union[List[str], List[List[str]]]] = None, metadata: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None, **kwargs: Any) LLMResult¶
Run the LLM on the given prompt and input.
- async agenerate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, **kwargs: Any) LLMResult¶
Asynchronously pass a sequence of prompts and return model generations.
This method should make use of batched calls for models that expose a batched API.
- Use this method when you want to:
take advantage of batched calls,
need more output from the model than just the top generated value,
- are building chains that are agnostic to the underlying language model
type (e.g., pure text completion models vs chat models).
- Parameters
prompts – List of PromptValues. A PromptValue is an object that can be converted to match the format of any language model (string for pure text generation models and BaseMessages for chat models).
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
callbacks – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
- An LLMResult, which contains a list of candidate Generations for each input
prompt and additional model provider-specific output.
- async ainvoke(input: Union[PromptValue, str, List[BaseMessage]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) str¶
- async apredict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str¶
Asynchronously pass a string to the model and return a string prediction.
- Use this method when calling pure text generation models and only the top
candidate generation is needed.
- Parameters
text – String input to pass to the model.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a string.
- async apredict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage¶
Asynchronously pass messages to the model and return a message prediction.
- Use this method when calling chat models and only the top
candidate generation is needed.
- Parameters
messages – A sequence of chat messages corresponding to a single model input.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a message.
- async astream(input: Union[PromptValue, str, List[BaseMessage]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) AsyncIterator[str]¶
- batch(inputs: List[Union[PromptValue, str, List[BaseMessage]]], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, max_concurrency: Optional[int] = None, **kwargs: Any) List[str]¶
- dict(**kwargs: Any) Dict¶
Return a dictionary of the LLM.
- generate(prompts: List[str], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, *, tags: Optional[Union[List[str], List[List[str]]]] = None, metadata: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None, **kwargs: Any) LLMResult¶
Run the LLM on the given prompt and input.
- generate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, **kwargs: Any) LLMResult¶
Pass a sequence of prompts to the model and return model generations.
This method should make use of batched calls for models that expose a batched API.
- Use this method when you want to:
take advantage of batched calls,
need more output from the model than just the top generated value,
- are building chains that are agnostic to the underlying language model
type (e.g., pure text completion models vs chat models).
- Parameters
prompts – List of PromptValues. A PromptValue is an object that can be converted to match the format of any language model (string for pure text generation models and BaseMessages for chat models).
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
callbacks – Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
- An LLMResult, which contains a list of candidate Generations for each input
prompt and additional model provider-specific output.
- get_num_tokens(text: str) int¶
Get the number of tokens present in the text.
Useful for checking if an input will fit in a model’s context window.
- Parameters
text – The string input to tokenize.
- Returns
The integer number of tokens in the text.
- get_num_tokens_from_messages(messages: List[BaseMessage]) int¶
Get the number of tokens in the messages.
Useful for checking if an input will fit in a model’s context window.
- Parameters
messages – The message inputs to tokenize.
- Returns
The sum of the number of tokens across the messages.
- get_token_ids(text: str) List[int]¶
Return the ordered ids of the tokens in a text.
- Parameters
text – The string input to tokenize.
- Returns
- A list of ids corresponding to the tokens in the text, in order they occur
in the text.
- invoke(input: Union[PromptValue, str, List[BaseMessage]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) str¶
- predict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str¶
Pass a single string input to the model and return a string prediction.
- Use this method when passing in raw text. If you want to pass in specific
types of chat messages, use predict_messages.
- Parameters
text – String input to pass to the model.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a string.
- predict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage¶
Pass a message sequence to the model and return a message prediction.
- Use this method when passing in chat messages. If you want to pass in raw text,
use predict.
- Parameters
messages – A sequence of chat messages corresponding to a single model input.
stop – Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.
**kwargs – Arbitrary additional keyword arguments. These are usually passed to the model provider API call.
- Returns
Top model prediction as a message.
- validator raise_deprecation » all fields¶
Raise deprecation warning if callback_manager is used.
- save(file_path: Union[Path, str]) None¶
Save the LLM.
- Parameters
file_path – Path to file to save the LLM to.
Example: .. code-block:: python
llm.save(file_path=”path/llm.yaml”)
- validator set_cluster_driver_port » cluster_driver_port[source]¶
- validator set_cluster_id » cluster_id[source]¶
- validator set_model_kwargs » model_kwargs[source]¶
- validator set_verbose » verbose¶
If verbose is None, set it.
This allows users to pass in None as verbose to access the global setting.
- stream(input: Union[PromptValue, str, List[BaseMessage]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) Iterator[str]¶
- to_json() Union[SerializedConstructor, SerializedNotImplemented]¶
- to_json_not_implemented() SerializedNotImplemented¶
- property lc_attributes: Dict¶
Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.
- property lc_namespace: List[str]¶
Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]
- property lc_secrets: Dict[str, str]¶
Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}
- property lc_serializable: bool¶
Return whether or not the class is serializable.