langchain.llms.aleph_alpha.AlephAlpha¶

class langchain.llms.aleph_alpha.AlephAlpha(*, 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, client: Any = None, model: Optional[str] = 'luminous-base', maximum_tokens: int = 64, temperature: float = 0.0, top_k: int = 0, top_p: float = 0.0, presence_penalty: float = 0.0, frequency_penalty: float = 0.0, repetition_penalties_include_prompt: Optional[bool] = False, use_multiplicative_presence_penalty: Optional[bool] = False, penalty_bias: Optional[str] = None, penalty_exceptions: Optional[List[str]] = None, penalty_exceptions_include_stop_sequences: Optional[bool] = None, best_of: Optional[int] = None, n: int = 1, logit_bias: Optional[Dict[int, float]] = None, log_probs: Optional[int] = None, tokens: Optional[bool] = False, disable_optimizations: Optional[bool] = False, minimum_tokens: Optional[int] = 0, echo: bool = False, use_multiplicative_frequency_penalty: bool = False, sequence_penalty: float = 0.0, sequence_penalty_min_length: int = 2, use_multiplicative_sequence_penalty: bool = False, completion_bias_inclusion: Optional[Sequence[str]] = None, completion_bias_inclusion_first_token_only: bool = False, completion_bias_exclusion: Optional[Sequence[str]] = None, completion_bias_exclusion_first_token_only: bool = False, contextual_control_threshold: Optional[float] = None, control_log_additive: Optional[bool] = True, repetition_penalties_include_completion: bool = True, raw_completion: bool = False, aleph_alpha_api_key: Optional[str] = None, stop_sequences: Optional[List[str]] = None)[source]¶

Bases: LLM

Aleph Alpha large language models.

To use, you should have the aleph_alpha_client python package installed, and the environment variable ALEPH_ALPHA_API_KEY set with your API key, or pass it as a named parameter to the constructor.

Parameters are explained more in depth here: https://github.com/Aleph-Alpha/aleph-alpha-client/blob/c14b7dd2b4325c7da0d6a119f6e76385800e097b/aleph_alpha_client/completion.py#L10

Example

from langchain.llms import AlephAlpha
aleph_alpha = AlephAlpha(aleph_alpha_api_key="my-api-key")

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 aleph_alpha_api_key: Optional[str] = None¶

API key for Aleph Alpha API.

param best_of: Optional[int] = None¶

returns the one with the “best of” results (highest log probability per token)

param cache: Optional[bool] = None¶
param callback_manager: Optional[BaseCallbackManager] = None¶
param callbacks: Callbacks = None¶
param completion_bias_exclusion: Optional[Sequence[str]] = None¶
param completion_bias_exclusion_first_token_only: bool = False¶

Only consider the first token for the completion_bias_exclusion.

param completion_bias_inclusion: Optional[Sequence[str]] = None¶
param completion_bias_inclusion_first_token_only: bool = False¶
param contextual_control_threshold: Optional[float] = None¶

If set to None, attention control parameters only apply to those tokens that have explicitly been set in the request. If set to a non-None value, control parameters are also applied to similar tokens.

param control_log_additive: Optional[bool] = True¶

True: apply control by adding the log(control_factor) to attention scores. False: (attention_scores - - attention_scores.min(-1)) * control_factor

param disable_optimizations: Optional[bool] = False¶
param echo: bool = False¶

Echo the prompt in the completion.

param frequency_penalty: float = 0.0¶

Penalizes repeated tokens according to frequency.

param log_probs: Optional[int] = None¶

Number of top log probabilities to be returned for each generated token.

param logit_bias: Optional[Dict[int, float]] = None¶

The logit bias allows to influence the likelihood of generating tokens.

param maximum_tokens: int = 64¶

The maximum number of tokens to be generated.

param metadata: Optional[Dict[str, Any]] = None¶

Metadata to add to the run trace.

param minimum_tokens: Optional[int] = 0¶

Generate at least this number of tokens.

param model: Optional[str] = 'luminous-base'¶

Model name to use.

param n: int = 1¶

How many completions to generate for each prompt.

param penalty_bias: Optional[str] = None¶

Penalty bias for the completion.

param penalty_exceptions: Optional[List[str]] = None¶

List of strings that may be generated without penalty, regardless of other penalty settings

param penalty_exceptions_include_stop_sequences: Optional[bool] = None¶

Should stop_sequences be included in penalty_exceptions.

param presence_penalty: float = 0.0¶

Penalizes repeated tokens.

param raw_completion: bool = False¶

Force the raw completion of the model to be returned.

param repetition_penalties_include_completion: bool = True¶

Flag deciding whether presence penalty or frequency penalty are updated from the completion.

param repetition_penalties_include_prompt: Optional[bool] = False¶

Flag deciding whether presence penalty or frequency penalty are updated from the prompt.

param sequence_penalty: float = 0.0¶
param sequence_penalty_min_length: int = 2¶
param stop_sequences: Optional[List[str]] = None¶

Stop sequences to use.

param tags: Optional[List[str]] = None¶

Tags to add to the run trace.

param temperature: float = 0.0¶

A non-negative float that tunes the degree of randomness in generation.

param tokens: Optional[bool] = False¶

return tokens of completion.

param top_k: int = 0¶

Number of most likely tokens to consider at each step.

param top_p: float = 0.0¶

Total probability mass of tokens to consider at each step.

param use_multiplicative_frequency_penalty: bool = False¶
param use_multiplicative_presence_penalty: Optional[bool] = False¶

Flag deciding whether presence penalty is applied multiplicatively (True) or additively (False).

param use_multiplicative_sequence_penalty: bool = False¶
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:
  1. take advantage of batched calls,

  2. need more output from the model than just the top generated value,

  3. 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:
  1. take advantage of batched calls,

  2. need more output from the model than just the top generated value,

  3. 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_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¶
validator validate_environment  »  all fields[source]¶

Validate that api key and python package exists in environment.

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.

model Config[source]¶

Bases: object

Configuration for this pydantic object.

extra = 'forbid'¶

Examples using AlephAlpha¶