UNPKG

@langchain/community

Version:
1 lines • 13.7 kB
{"version":3,"file":"aleph_alpha.cjs","names":["LLM","fields: Partial<AlephAlpha>","prompt: string","options: this[\"ParsedCallOptions\"]"],"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 // eslint-disable-next-line @typescript-eslint/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,0CAA+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,6EAA8C,sBAAsB;CAEpE;CAEA,WAAW;CAEX,YAAYC,QAA6B;EACvC,MAAM,UAAU,CAAE,EAAC;EACnB,KAAK,QAAQ,QAAQ,SAAS,KAAK;EACnC,KAAK,cAAc,QAAQ,eAAe,KAAK;EAC/C,KAAK,iBAAiB,QAAQ,kBAAkB,KAAK;EACrD,KAAK,iBAAiB,QAAQ,kBAAkB,KAAK;EACrD,KAAK,QAAQ,QAAQ,SAAS,KAAK;EACnC,KAAK,QAAQ,QAAQ,SAAS,KAAK;EACnC,KAAK,mBAAmB,QAAQ,oBAAoB,KAAK;EACzD,KAAK,oBACH,QAAQ,qBAAqB,KAAK;EACpC,KAAK,mBAAmB,QAAQ,oBAAoB,KAAK;EACzD,KAAK,8BACH,QAAQ,+BAA+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