UNPKG

@langchain/community

Version:
1 lines 2.98 kB
{"version":3,"file":"arcjet.cjs","names":["LLM"],"sources":["../../src/llms/arcjet.ts"],"sourcesContent":["import {\n LLM,\n BaseLLM,\n type BaseLLMCallOptions,\n} from \"@langchain/core/language_models/llms\";\nimport type { ArcjetSensitiveInfoType, RedactOptions } from \"@arcjet/redact\";\n\ntype DetectSensitiveInfoEntities<T> = (\n tokens: string[]\n) => Array<ArcjetSensitiveInfoType | T | undefined>;\ntype ValidEntities<Detect> = Array<\n undefined extends Detect\n ? ArcjetSensitiveInfoType\n : Detect extends DetectSensitiveInfoEntities<infer CustomEntities>\n ? ArcjetSensitiveInfoType | CustomEntities\n : never\n>;\n\nexport type { ArcjetSensitiveInfoType, RedactOptions };\n\nexport interface ArcjetRedactOptions<Detect> extends BaseLLMCallOptions {\n llm: BaseLLM;\n entities?: ValidEntities<Detect>;\n contextWindowSize?: number;\n detect?: Detect;\n replace?: (entity: ValidEntities<Detect>[number]) => string | undefined;\n}\n\nexport class ArcjetRedact<\n Detect extends DetectSensitiveInfoEntities<CustomEntities> | undefined,\n CustomEntities extends string,\n> extends LLM {\n static lc_name() {\n return \"ArcjetRedact\";\n }\n\n llm: BaseLLM;\n\n entities?: ValidEntities<Detect>;\n\n contextWindowSize?: number;\n\n detect?: Detect;\n\n replace?: (entity: ValidEntities<Detect>[number]) => string | undefined;\n\n constructor(options: ArcjetRedactOptions<Detect>) {\n super(options);\n\n if (options.entities && options.entities.length === 0) {\n throw new Error(\"no entities configured for redaction\");\n }\n this.llm = options.llm;\n this.entities = options.entities;\n this.contextWindowSize = options.contextWindowSize;\n this.detect = options.detect;\n this.replace = options.replace;\n }\n\n _llmType() {\n return \"arcjet_redact\";\n }\n\n async _call(input: string, options?: BaseLLMCallOptions): Promise<string> {\n const ajOptions: RedactOptions<Detect> = {\n entities: this.entities,\n contextWindowSize: this.contextWindowSize,\n detect: this.detect,\n replace: this.replace,\n };\n\n const { redact } = await import(\"@arcjet/redact\");\n const [redacted, unredact] = await redact(input, ajOptions);\n\n // Invoke the underlying LLM with the prompt and options\n const result = await this.llm.invoke(redacted, options);\n\n return unredact(result);\n }\n}\n"],"mappings":";;;;;AA4BA,IAAa,eAAb,cAGUA,qCAAAA,IAAI;CACZ,OAAO,UAAU;AACf,SAAO;;CAGT;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,SAAsC;AAChD,QAAM,QAAQ;AAEd,MAAI,QAAQ,YAAY,QAAQ,SAAS,WAAW,EAClD,OAAM,IAAI,MAAM,uCAAuC;AAEzD,OAAK,MAAM,QAAQ;AACnB,OAAK,WAAW,QAAQ;AACxB,OAAK,oBAAoB,QAAQ;AACjC,OAAK,SAAS,QAAQ;AACtB,OAAK,UAAU,QAAQ;;CAGzB,WAAW;AACT,SAAO;;CAGT,MAAM,MAAM,OAAe,SAA+C;EACxE,MAAM,YAAmC;GACvC,UAAU,KAAK;GACf,mBAAmB,KAAK;GACxB,QAAQ,KAAK;GACb,SAAS,KAAK;GACf;EAED,MAAM,EAAE,WAAW,MAAM,OAAO;EAChC,MAAM,CAAC,UAAU,YAAY,MAAM,OAAO,OAAO,UAAU;AAK3D,SAAO,SAFQ,MAAM,KAAK,IAAI,OAAO,UAAU,QAAQ,CAEhC"}