UNPKG

@langchain/community

Version:
1 lines 13.6 kB
{"version":3,"file":"aleph_alpha.cjs","names":["LLM"],"sources":["../../src/llms/aleph_alpha.ts"],"sourcesContent":["import { LLM, type BaseLLMParams } from \"@langchain/core/language_models/llms\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\n/**\n * Interface for the input parameters specific to the Aleph Alpha LLM.\n */\nexport interface AlephAlphaInput extends BaseLLMParams {\n model: string;\n maximum_tokens: number;\n minimum_tokens?: number;\n echo?: boolean;\n temperature?: number;\n top_k?: number;\n top_p?: number;\n presence_penalty?: number;\n frequency_penalty?: number;\n sequence_penalty?: number;\n sequence_penalty_min_length?: number;\n repetition_penalties_include_prompt?: boolean;\n repetition_penalties_include_completion?: boolean;\n use_multiplicative_presence_penalty?: boolean;\n use_multiplicative_frequency_penalty?: boolean;\n use_multiplicative_sequence_penalty?: boolean;\n penalty_bias?: string;\n penalty_exceptions?: string[];\n penalty_exceptions_include_stop_sequences?: boolean;\n best_of?: number;\n n?: number;\n logit_bias?: object;\n log_probs?: number;\n tokens?: boolean;\n raw_completion: boolean;\n disable_optimizations?: boolean;\n completion_bias_inclusion?: string[];\n completion_bias_inclusion_first_token_only: boolean;\n completion_bias_exclusion?: string[];\n completion_bias_exclusion_first_token_only: boolean;\n contextual_control_threshold?: number;\n control_log_additive: boolean;\n stop?: string[];\n aleph_alpha_api_key?: string;\n base_url: string;\n}\n\n/**\n * Specific implementation of a Large Language Model (LLM) designed to\n * interact with the Aleph Alpha API. It extends the base LLM class and\n * includes a variety of parameters for customizing the behavior of the\n * Aleph Alpha model.\n */\nexport class AlephAlpha extends LLM implements AlephAlphaInput {\n lc_serializable = true;\n\n model = \"luminous-base\";\n\n maximum_tokens = 64;\n\n minimum_tokens = 0;\n\n echo: boolean;\n\n temperature = 0.0;\n\n top_k: number;\n\n top_p = 0.0;\n\n presence_penalty?: number;\n\n frequency_penalty?: number;\n\n sequence_penalty?: number;\n\n sequence_penalty_min_length?: number;\n\n repetition_penalties_include_prompt?: boolean;\n\n repetition_penalties_include_completion?: boolean;\n\n use_multiplicative_presence_penalty?: boolean;\n\n use_multiplicative_frequency_penalty?: boolean;\n\n use_multiplicative_sequence_penalty?: boolean;\n\n penalty_bias?: string;\n\n penalty_exceptions?: string[];\n\n penalty_exceptions_include_stop_sequences?: boolean;\n\n best_of?: number;\n\n n?: number;\n\n logit_bias?: object;\n\n log_probs?: number;\n\n tokens?: boolean;\n\n raw_completion: boolean;\n\n disable_optimizations?: boolean;\n\n completion_bias_inclusion?: string[];\n\n completion_bias_inclusion_first_token_only: boolean;\n\n completion_bias_exclusion?: string[];\n\n completion_bias_exclusion_first_token_only: boolean;\n\n contextual_control_threshold?: number;\n\n control_log_additive: boolean;\n\n aleph_alpha_api_key? = getEnvironmentVariable(\"ALEPH_ALPHA_API_KEY\");\n\n stop?: string[];\n\n base_url = \"https://api.aleph-alpha.com/complete\";\n\n constructor(fields: Partial<AlephAlpha>) {\n super(fields ?? {});\n this.model = fields?.model ?? this.model;\n this.temperature = fields?.temperature ?? this.temperature;\n this.maximum_tokens = fields?.maximum_tokens ?? this.maximum_tokens;\n this.minimum_tokens = fields?.minimum_tokens ?? this.minimum_tokens;\n this.top_k = fields?.top_k ?? this.top_k;\n this.top_p = fields?.top_p ?? this.top_p;\n this.presence_penalty = fields?.presence_penalty ?? this.presence_penalty;\n this.frequency_penalty =\n fields?.frequency_penalty ?? this.frequency_penalty;\n this.sequence_penalty = fields?.sequence_penalty ?? this.sequence_penalty;\n this.sequence_penalty_min_length =\n fields?.sequence_penalty_min_length ?? this.sequence_penalty_min_length;\n this.repetition_penalties_include_prompt =\n fields?.repetition_penalties_include_prompt ??\n this.repetition_penalties_include_prompt;\n this.repetition_penalties_include_completion =\n fields?.repetition_penalties_include_completion ??\n this.repetition_penalties_include_completion;\n this.use_multiplicative_presence_penalty =\n fields?.use_multiplicative_presence_penalty ??\n this.use_multiplicative_presence_penalty;\n this.use_multiplicative_frequency_penalty =\n fields?.use_multiplicative_frequency_penalty ??\n this.use_multiplicative_frequency_penalty;\n this.use_multiplicative_sequence_penalty =\n fields?.use_multiplicative_sequence_penalty ??\n this.use_multiplicative_sequence_penalty;\n this.penalty_bias = fields?.penalty_bias ?? this.penalty_bias;\n this.penalty_exceptions =\n fields?.penalty_exceptions ?? this.penalty_exceptions;\n this.penalty_exceptions_include_stop_sequences =\n fields?.penalty_exceptions_include_stop_sequences ??\n this.penalty_exceptions_include_stop_sequences;\n this.best_of = fields?.best_of ?? this.best_of;\n this.n = fields?.n ?? this.n;\n this.logit_bias = fields?.logit_bias ?? this.logit_bias;\n this.log_probs = fields?.log_probs ?? this.log_probs;\n this.tokens = fields?.tokens ?? this.tokens;\n this.raw_completion = fields?.raw_completion ?? this.raw_completion;\n this.disable_optimizations =\n fields?.disable_optimizations ?? this.disable_optimizations;\n this.completion_bias_inclusion =\n fields?.completion_bias_inclusion ?? this.completion_bias_inclusion;\n this.completion_bias_inclusion_first_token_only =\n fields?.completion_bias_inclusion_first_token_only ??\n this.completion_bias_inclusion_first_token_only;\n this.completion_bias_exclusion =\n fields?.completion_bias_exclusion ?? this.completion_bias_exclusion;\n this.completion_bias_exclusion_first_token_only =\n fields?.completion_bias_exclusion_first_token_only ??\n this.completion_bias_exclusion_first_token_only;\n this.contextual_control_threshold =\n fields?.contextual_control_threshold ?? this.contextual_control_threshold;\n this.control_log_additive =\n fields?.control_log_additive ?? this.control_log_additive;\n this.aleph_alpha_api_key =\n fields?.aleph_alpha_api_key ?? this.aleph_alpha_api_key;\n this.stop = fields?.stop ?? this.stop;\n }\n\n /**\n * Validates the environment by ensuring the necessary Aleph Alpha API key\n * is available. Throws an error if the API key is missing.\n */\n validateEnvironment() {\n if (!this.aleph_alpha_api_key) {\n throw new Error(\n \"Aleph Alpha API Key is missing in environment variables.\"\n );\n }\n }\n\n /** Get the default parameters for calling Aleph Alpha API. */\n get defaultParams() {\n return {\n model: this.model,\n temperature: this.temperature,\n maximum_tokens: this.maximum_tokens,\n minimum_tokens: this.minimum_tokens,\n top_k: this.top_k,\n top_p: this.top_p,\n presence_penalty: this.presence_penalty,\n frequency_penalty: this.frequency_penalty,\n sequence_penalty: this.sequence_penalty,\n sequence_penalty_min_length: this.sequence_penalty_min_length,\n repetition_penalties_include_prompt:\n this.repetition_penalties_include_prompt,\n repetition_penalties_include_completion:\n this.repetition_penalties_include_completion,\n use_multiplicative_presence_penalty:\n this.use_multiplicative_presence_penalty,\n use_multiplicative_frequency_penalty:\n this.use_multiplicative_frequency_penalty,\n use_multiplicative_sequence_penalty:\n this.use_multiplicative_sequence_penalty,\n penalty_bias: this.penalty_bias,\n penalty_exceptions: this.penalty_exceptions,\n penalty_exceptions_include_stop_sequences:\n this.penalty_exceptions_include_stop_sequences,\n best_of: this.best_of,\n n: this.n,\n logit_bias: this.logit_bias,\n log_probs: this.log_probs,\n tokens: this.tokens,\n raw_completion: this.raw_completion,\n disable_optimizations: this.disable_optimizations,\n completion_bias_inclusion: this.completion_bias_inclusion,\n completion_bias_inclusion_first_token_only:\n this.completion_bias_inclusion_first_token_only,\n completion_bias_exclusion: this.completion_bias_exclusion,\n completion_bias_exclusion_first_token_only:\n this.completion_bias_exclusion_first_token_only,\n contextual_control_threshold: this.contextual_control_threshold,\n control_log_additive: this.control_log_additive,\n };\n }\n\n /** Get the identifying parameters for this LLM. */\n get identifyingParams() {\n return { ...this.defaultParams };\n }\n\n /** Get the type of LLM. */\n _llmType(): string {\n return \"aleph_alpha\";\n }\n\n async _call(\n prompt: string,\n options: this[\"ParsedCallOptions\"]\n ): Promise<string> {\n let stop = options?.stop;\n this.validateEnvironment();\n if (this.stop && stop && this.stop.length > 0 && stop.length > 0) {\n throw new Error(\"`stop` found in both the input and default params.\");\n }\n stop = this.stop ?? stop ?? [];\n const headers = {\n Authorization: `Bearer ${this.aleph_alpha_api_key}`,\n \"Content-Type\": \"application/json\",\n Accept: \"application/json\",\n };\n const data = { prompt, stop_sequences: stop, ...this.defaultParams };\n const responseData = await this.caller.call(async () => {\n const response = await fetch(this.base_url, {\n method: \"POST\",\n headers,\n body: JSON.stringify(data),\n signal: options.signal,\n });\n if (!response.ok) {\n // consume the response body to release the connection\n // https://undici.nodejs.org/#/?id=garbage-collection\n const text = await response.text();\n const error = new Error(\n `Aleph Alpha call failed with status ${response.status} and body ${text}`\n );\n // oxlint-disable-next-line typescript/no-explicit-any\n (error as any).response = response;\n throw error;\n }\n return response.json();\n });\n\n if (\n !responseData.completions ||\n responseData.completions.length === 0 ||\n !responseData.completions[0].completion\n ) {\n throw new Error(\"No completions found in response\");\n }\n\n return responseData.completions[0].completion ?? \"\";\n }\n}\n"],"mappings":";;;;;;;;;;;;AAkDA,IAAa,aAAb,cAAgCA,qCAAAA,IAA+B;CAC7D,kBAAkB;CAElB,QAAQ;CAER,iBAAiB;CAEjB,iBAAiB;CAEjB;CAEA,cAAc;CAEd;CAEA,QAAQ;CAER;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA,uBAAA,GAAA,0BAAA,wBAA8C,sBAAsB;CAEpE;CAEA,WAAW;CAEX,YAAY,QAA6B;AACvC,QAAM,UAAU,EAAE,CAAC;AACnB,OAAK,QAAQ,QAAQ,SAAS,KAAK;AACnC,OAAK,cAAc,QAAQ,eAAe,KAAK;AAC/C,OAAK,iBAAiB,QAAQ,kBAAkB,KAAK;AACrD,OAAK,iBAAiB,QAAQ,kBAAkB,KAAK;AACrD,OAAK,QAAQ,QAAQ,SAAS,KAAK;AACnC,OAAK,QAAQ,QAAQ,SAAS,KAAK;AACnC,OAAK,mBAAmB,QAAQ,oBAAoB,KAAK;AACzD,OAAK,oBACH,QAAQ,qBAAqB,KAAK;AACpC,OAAK,mBAAmB,QAAQ,oBAAoB,KAAK;AACzD,OAAK,8BACH,QAAQ,+BAA+B,KAAK;AAC9C,OAAK,sCACH,QA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