{"version":3,"file":"llms.cjs","names":["AbstractGoogleLLMConnection","ChatGoogleBase","LLM","ensureParams","DefaultGeminiSafetyHandler","ApiKeyGoogleAuth","copyAIModelParams","BaseLLM","CallbackManager","GenerationChunk"],"sources":["../src/llms.ts"],"sourcesContent":["import { CallbackManager, Callbacks } from \"@langchain/core/callbacks/manager\";\nimport { BaseLLM, LLM } from \"@langchain/core/language_models/llms\";\nimport {\n  type BaseLanguageModelCallOptions,\n  BaseLanguageModelInput,\n} from \"@langchain/core/language_models/base\";\nimport { BaseMessage, MessageContent } from \"@langchain/core/messages\";\nimport { GenerationChunk } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\nimport { AbstractGoogleLLMConnection } from \"./connection.js\";\nimport {\n  GoogleAIBaseLLMInput,\n  GoogleAIModelParams,\n  GoogleAISafetySetting,\n  GooglePlatformType,\n  GeminiContent,\n  GoogleAIResponseMimeType,\n} from \"./types.js\";\nimport {\n  copyAIModelParams,\n  copyAndValidateModelParamsInto,\n} from \"./utils/common.js\";\nimport { DefaultGeminiSafetyHandler } from \"./utils/gemini.js\";\nimport { ApiKeyGoogleAuth, GoogleAbstractedClient } from \"./auth.js\";\nimport { ensureParams } from \"./utils/failed_handler.js\";\nimport { ChatGoogleBase } from \"./chat_models.js\";\nimport type { GoogleBaseLLMInput, GoogleAISafetyHandler } from \"./types.js\";\n\nexport { GoogleBaseLLMInput };\n\nclass GoogleLLMConnection<AuthOptions> extends AbstractGoogleLLMConnection<\n  MessageContent,\n  AuthOptions\n> {\n  async formatContents(\n    input: MessageContent,\n    _parameters: GoogleAIModelParams\n  ): Promise<GeminiContent[]> {\n    const parts = await this.api.messageContentToParts!(input);\n    const contents: GeminiContent[] = [\n      {\n        role: \"user\", // Required by Vertex AI\n        parts,\n      },\n    ];\n    return contents;\n  }\n}\n\ntype ProxyChatInput<AuthOptions> = GoogleAIBaseLLMInput<AuthOptions> & {\n  connection: GoogleLLMConnection<AuthOptions>;\n};\n\nclass ProxyChatGoogle<AuthOptions> extends ChatGoogleBase<AuthOptions> {\n  constructor(fields: ProxyChatInput<AuthOptions>) {\n    super(fields);\n    this._addVersion(\"@langchain/google-common\", __PKG_VERSION__);\n  }\n\n  buildAbstractedClient(\n    fields: ProxyChatInput<AuthOptions>\n  ): GoogleAbstractedClient {\n    return fields.connection.client;\n  }\n}\n\n/**\n * Integration with an LLM.\n */\nexport abstract class GoogleBaseLLM<AuthOptions>\n  extends LLM<BaseLanguageModelCallOptions>\n  implements GoogleBaseLLMInput<AuthOptions>\n{\n  // Used for tracing, replace with the same name as your class\n  static lc_name() {\n    return \"GoogleLLM\";\n  }\n\n  get lc_secrets(): { [key: string]: string } | undefined {\n    return {\n      authOptions: \"GOOGLE_AUTH_OPTIONS\",\n    };\n  }\n\n  originalFields?: GoogleBaseLLMInput<AuthOptions>;\n\n  lc_serializable = true;\n\n  modelName = \"gemini-pro\";\n\n  model = \"gemini-pro\";\n\n  temperature = 0.7;\n\n  maxOutputTokens = 1024;\n\n  topP = 0.8;\n\n  topK = 40;\n\n  stopSequences: string[] = [];\n\n  safetySettings: GoogleAISafetySetting[] = [];\n\n  safetyHandler: GoogleAISafetyHandler;\n\n  responseMimeType: GoogleAIResponseMimeType = \"text/plain\";\n\n  protected connection: GoogleLLMConnection<AuthOptions>;\n\n  protected streamedConnection: GoogleLLMConnection<AuthOptions>;\n\n  constructor(fields?: GoogleBaseLLMInput<AuthOptions>) {\n    super(ensureParams(fields));\n    this.originalFields = fields;\n\n    copyAndValidateModelParamsInto(fields, this);\n    this.safetyHandler =\n      fields?.safetyHandler ?? new DefaultGeminiSafetyHandler();\n\n    const client = this.buildClient(fields);\n    this.buildConnection(fields ?? {}, client);\n  }\n\n  abstract buildAbstractedClient(\n    fields?: GoogleAIBaseLLMInput<AuthOptions>\n  ): GoogleAbstractedClient;\n\n  buildApiKeyClient(apiKey: string): GoogleAbstractedClient {\n    return new ApiKeyGoogleAuth(apiKey);\n  }\n\n  buildApiKey(fields?: GoogleAIBaseLLMInput<AuthOptions>): string | undefined {\n    return fields?.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n  }\n\n  buildClient(\n    fields?: GoogleAIBaseLLMInput<AuthOptions>\n  ): GoogleAbstractedClient {\n    const apiKey = this.buildApiKey(fields);\n    if (apiKey) {\n      return this.buildApiKeyClient(apiKey);\n    } else {\n      return this.buildAbstractedClient(fields);\n    }\n  }\n\n  buildConnection(\n    fields: GoogleBaseLLMInput<AuthOptions>,\n    client: GoogleAbstractedClient\n  ) {\n    this.connection = new GoogleLLMConnection(\n      { ...fields, ...this },\n      this.caller,\n      client,\n      false\n    );\n\n    this.streamedConnection = new GoogleLLMConnection(\n      { ...fields, ...this },\n      this.caller,\n      client,\n      true\n    );\n  }\n\n  get platform(): GooglePlatformType {\n    return this.connection.platform;\n  }\n\n  // Replace\n  _llmType() {\n    return \"googlellm\";\n  }\n\n  formatPrompt(prompt: string): MessageContent {\n    return prompt;\n  }\n\n  /**\n   * For some given input string and options, return a string output.\n   *\n   * Despite the fact that `invoke` is overridden below, we still need this\n   * in order to handle public APi calls to `generate()`.\n   */\n  async _call(\n    prompt: string,\n    options: this[\"ParsedCallOptions\"]\n  ): Promise<string> {\n    const parameters = copyAIModelParams(this, options);\n    const result = await this.connection.request(prompt, parameters, options);\n    const ret = this.connection.api.responseToString(result);\n    return ret;\n  }\n\n  // Normally, you should not override this method and instead should override\n  // _streamResponseChunks. We are doing so here to allow for multimodal inputs into\n  // the LLM.\n  async *_streamIterator(\n    input: BaseLanguageModelInput,\n    options?: BaseLanguageModelCallOptions\n  ): AsyncGenerator<string> {\n    // TODO: Refactor callback setup and teardown code into core\n    const prompt = BaseLLM._convertInputToPromptValue(input);\n    const [runnableConfig, callOptions] =\n      this._separateRunnableConfigFromCallOptions(options);\n    const invocationParams = this.invocationParams(callOptions);\n    const callbackManager_ = await CallbackManager.configure(\n      runnableConfig.callbacks,\n      this.callbacks,\n      runnableConfig.tags,\n      this.tags,\n      runnableConfig.metadata,\n      this.metadata,\n      {\n        verbose: this.verbose,\n        tracerInheritableMetadata:\n          this._filterInvocationParamsForTracing(invocationParams),\n      }\n    );\n    const extra = {\n      options: callOptions,\n      invocation_params: invocationParams,\n      batch_size: 1,\n    };\n    const runManagers = await callbackManager_?.handleLLMStart(\n      this.toJSON(),\n      [prompt.toString()],\n      undefined,\n      undefined,\n      extra,\n      undefined,\n      undefined,\n      runnableConfig.runName\n    );\n    let generation = new GenerationChunk({\n      text: \"\",\n    });\n    const proxyChat = this.createProxyChat();\n    try {\n      for await (const chunk of proxyChat._streamIterator(input, options)) {\n        const stringValue = this.connection.api.chunkToString(chunk);\n        const generationChunk = new GenerationChunk({\n          text: stringValue,\n        });\n        generation = generation.concat(generationChunk);\n        yield stringValue;\n      }\n    } catch (err) {\n      await Promise.all(\n        (runManagers ?? []).map((runManager) => runManager?.handleLLMError(err))\n      );\n      throw err;\n    }\n    await Promise.all(\n      (runManagers ?? []).map((runManager) =>\n        runManager?.handleLLMEnd({\n          generations: [[generation]],\n        })\n      )\n    );\n  }\n\n  async predictMessages(\n    messages: BaseMessage[],\n    options?: string[] | BaseLanguageModelCallOptions,\n    _callbacks?: Callbacks\n  ): Promise<BaseMessage> {\n    const { content } = messages[0];\n    const result = await this.connection.request(\n      content,\n      {},\n      options as BaseLanguageModelCallOptions\n    );\n    const ret = this.connection.api.responseToBaseMessage(result);\n    return ret;\n  }\n\n  /**\n   * Internal implementation detail to allow Google LLMs to support\n   * multimodal input by delegating to the chat model implementation.\n   *\n   * TODO: Replace with something less hacky.\n   */\n  protected createProxyChat(): ChatGoogleBase<AuthOptions> {\n    return new ProxyChatGoogle<AuthOptions>({\n      ...this.originalFields,\n      connection: this.connection,\n    });\n  }\n\n  // TODO: Remove the need to override this - we are doing it to\n  // allow the LLM to handle multimodal types of input.\n  async invoke(\n    input: BaseLanguageModelInput,\n    options?: BaseLanguageModelCallOptions\n  ): Promise<string> {\n    const stream = await this._streamIterator(input, options);\n    let generatedOutput = \"\";\n    for await (const chunk of stream) {\n      generatedOutput += chunk;\n    }\n    return generatedOutput;\n  }\n}\n"],"mappings":";;;;;;;;;;;AA+BA,IAAM,sBAAN,cAA+CA,mBAAAA,4BAG7C;CACA,MAAM,eACJ,OACA,aAC0B;AAQ1B,SAAO,CALL;GACE,MAAM;GACN,OAAA,MAJgB,KAAK,IAAI,sBAAuB,MAAM;GAKvD,CAEY;;;AAQnB,IAAM,kBAAN,cAA2CC,oBAAAA,eAA4B;CACrE,YAAY,QAAqC;AAC/C,QAAM,OAAO;AACb,OAAK,YAAY,4BAAA,QAA4C;;CAG/D,sBACE,QACwB;AACxB,SAAO,OAAO,WAAW;;;;;;AAO7B,IAAsB,gBAAtB,cACUC,qCAAAA,IAEV;CAEE,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,aAAoD;AACtD,SAAO,EACL,aAAa,uBACd;;CAGH;CAEA,kBAAkB;CAElB,YAAY;CAEZ,QAAQ;CAER,cAAc;CAEd,kBAAkB;CAElB,OAAO;CAEP,OAAO;CAEP,gBAA0B,EAAE;CAE5B,iBAA0C,EAAE;CAE5C;CAEA,mBAA6C;CAE7C;CAEA;CAEA,YAAY,QAA0C;AACpD,QAAMC,uBAAAA,aAAa,OAAO,CAAC;AAC3B,OAAK,iBAAiB;AAEtB,iBAAA,+BAA+B,QAAQ,KAAK;AAC5C,OAAK,gBACH,QAAQ,iBAAiB,IAAIC,eAAAA,4BAA4B;EAE3D,MAAM,SAAS,KAAK,YAAY,OAAO;AACvC,OAAK,gBAAgB,UAAU,EAAE,EAAE,OAAO;;CAO5C,kBAAkB,QAAwC;AACxD,SAAO,IAAIC,aAAAA,iBAAiB,OAAO;;CAGrC,YAAY,QAAgE;AAC1E,SAAO,QAAQ,WAAA,GAAA,0BAAA,wBAAiC,iBAAiB;;CAGnE,YACE,QACwB;EACxB,MAAM,SAAS,KAAK,YAAY,OAAO;AACvC,MAAI,OACF,QAAO,KAAK,kBAAkB,OAAO;MAErC,QAAO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