UNPKG

@langchain/community

Version:
1 lines • 3.15 kB
{"version":3,"file":"arcjet.cjs","names":["LLM","options: ArcjetRedactOptions<Detect>","input: string","options?: BaseLLMCallOptions","ajOptions: RedactOptions<Detect>"],"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,0CAAI;CACZ,OAAO,UAAU;AACf,SAAO;CACR;CAED;CAEA;CAEA;CAEA;CAEA;CAEA,YAAYC,SAAsC;EAChD,MAAM,QAAQ;AAEd,MAAI,QAAQ,YAAY,QAAQ,SAAS,WAAW,EAClD,OAAM,IAAI,MAAM;EAElB,KAAK,MAAM,QAAQ;EACnB,KAAK,WAAW,QAAQ;EACxB,KAAK,oBAAoB,QAAQ;EACjC,KAAK,SAAS,QAAQ;EACtB,KAAK,UAAU,QAAQ;CACxB;CAED,WAAW;AACT,SAAO;CACR;CAED,MAAM,MAAMC,OAAeC,SAA+C;EACxE,MAAMC,YAAmC;GACvC,UAAU,KAAK;GACf,mBAAmB,KAAK;GACxB,QAAQ,KAAK;GACb,SAAS,KAAK;EACf;EAED,MAAM,EAAE,QAAQ,GAAG,MAAM,OAAO;EAChC,MAAM,CAAC,UAAU,SAAS,GAAG,MAAM,OAAO,OAAO,UAAU;EAG3D,MAAM,SAAS,MAAM,KAAK,IAAI,OAAO,UAAU,QAAQ;AAEvD,SAAO,SAAS,OAAO;CACxB;AACF"}