@langchain/classic 1.0.27 → 1.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/agents/agent.cjs.map +1 -1
- package/dist/agents/agent.js.map +1 -1
- package/dist/agents/executor.cjs.map +1 -1
- package/dist/agents/executor.js.map +1 -1
- package/dist/agents/openai_functions/index.cjs.map +1 -1
- package/dist/agents/openai_functions/index.d.cts.map +1 -1
- package/dist/agents/openai_functions/index.d.ts.map +1 -1
- package/dist/agents/openai_functions/index.js.map +1 -1
- package/dist/agents/openai_tools/index.cjs.map +1 -1
- package/dist/agents/openai_tools/index.d.cts +2 -2
- package/dist/agents/openai_tools/index.d.cts.map +1 -1
- package/dist/agents/openai_tools/index.d.ts +2 -2
- package/dist/agents/openai_tools/index.d.ts.map +1 -1
- package/dist/agents/openai_tools/index.js.map +1 -1
- package/dist/agents/react/index.d.cts +2 -2
- package/dist/agents/react/index.d.cts.map +1 -1
- package/dist/agents/react/index.d.ts +2 -2
- package/dist/agents/react/index.d.ts.map +1 -1
- package/dist/agents/structured_chat/index.d.cts +2 -2
- package/dist/agents/structured_chat/index.d.cts.map +1 -1
- package/dist/agents/structured_chat/index.d.ts +2 -2
- package/dist/agents/structured_chat/index.d.ts.map +1 -1
- package/dist/agents/tool_calling/index.d.cts +2 -2
- package/dist/agents/tool_calling/index.d.cts.map +1 -1
- package/dist/agents/tool_calling/index.d.ts +2 -2
- package/dist/agents/tool_calling/index.d.ts.map +1 -1
- package/dist/agents/tool_calling/output_parser.cjs.map +1 -1
- package/dist/agents/tool_calling/output_parser.d.cts.map +1 -1
- package/dist/agents/tool_calling/output_parser.js.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.cts +2 -2
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.cts.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +2 -2
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts.map +1 -1
- package/dist/agents/xml/index.d.cts.map +1 -1
- package/dist/agents/xml/index.d.ts.map +1 -1
- package/dist/cache/file_system.cjs.map +1 -1
- package/dist/cache/file_system.js.map +1 -1
- package/dist/chains/analyze_documents_chain.cjs.map +1 -1
- package/dist/chains/analyze_documents_chain.js.map +1 -1
- package/dist/chains/base.cjs.map +1 -1
- package/dist/chains/base.d.cts +2 -2
- package/dist/chains/base.d.cts.map +1 -1
- package/dist/chains/base.d.ts +2 -2
- package/dist/chains/base.d.ts.map +1 -1
- package/dist/chains/base.js.map +1 -1
- package/dist/chains/chat_vector_db_chain.cjs.map +1 -1
- package/dist/chains/chat_vector_db_chain.js.map +1 -1
- package/dist/chains/combine_docs_chain.d.cts +2 -2
- package/dist/chains/combine_docs_chain.d.cts.map +1 -1
- package/dist/chains/combine_docs_chain.d.ts +2 -2
- package/dist/chains/combine_docs_chain.d.ts.map +1 -1
- package/dist/chains/combine_documents/reduce.cjs.map +1 -1
- package/dist/chains/combine_documents/reduce.js.map +1 -1
- package/dist/chains/conversational_retrieval_chain.cjs.map +1 -1
- package/dist/chains/conversational_retrieval_chain.js.map +1 -1
- package/dist/chains/graph_qa/cypher.cjs.map +1 -1
- package/dist/chains/graph_qa/cypher.js.map +1 -1
- package/dist/chains/llm_chain.cjs.map +1 -1
- package/dist/chains/llm_chain.js.map +1 -1
- package/dist/chains/load.d.cts +1 -2
- package/dist/chains/load.d.cts.map +1 -1
- package/dist/chains/load.d.ts +1 -2
- package/dist/chains/load.d.ts.map +1 -1
- package/dist/chains/openai_functions/base.cjs.map +1 -1
- package/dist/chains/openai_functions/base.js.map +1 -1
- package/dist/chains/openai_functions/openapi.cjs.map +1 -1
- package/dist/chains/openai_functions/openapi.js.map +1 -1
- package/dist/chains/openai_moderation.cjs.map +1 -1
- package/dist/chains/openai_moderation.js.map +1 -1
- package/dist/chains/query_constructor/index.cjs.map +1 -1
- package/dist/chains/query_constructor/index.js.map +1 -1
- package/dist/chains/question_answering/load.d.cts +1 -2
- package/dist/chains/question_answering/load.d.cts.map +1 -1
- package/dist/chains/question_answering/load.d.ts +1 -2
- package/dist/chains/question_answering/load.d.ts.map +1 -1
- package/dist/chains/retrieval.cjs.map +1 -1
- package/dist/chains/retrieval.js.map +1 -1
- package/dist/chains/retrieval_qa.cjs.map +1 -1
- package/dist/chains/retrieval_qa.js.map +1 -1
- package/dist/chains/router/utils.cjs.map +1 -1
- package/dist/chains/router/utils.js.map +1 -1
- package/dist/chains/summarization/load.d.cts +1 -2
- package/dist/chains/summarization/load.d.cts.map +1 -1
- package/dist/chains/summarization/load.d.ts +1 -2
- package/dist/chains/summarization/load.d.ts.map +1 -1
- package/dist/chains/vector_db_qa.cjs.map +1 -1
- package/dist/chains/vector_db_qa.js.map +1 -1
- package/dist/chat_models/universal.cjs +8 -10
- package/dist/chat_models/universal.cjs.map +1 -1
- package/dist/chat_models/universal.d.cts +3 -5
- package/dist/chat_models/universal.d.cts.map +1 -1
- package/dist/chat_models/universal.d.ts +3 -5
- package/dist/chat_models/universal.d.ts.map +1 -1
- package/dist/chat_models/universal.js +8 -10
- package/dist/chat_models/universal.js.map +1 -1
- package/dist/document_loaders/fs/directory.d.cts.map +1 -1
- package/dist/document_loaders/fs/directory.d.ts.map +1 -1
- package/dist/document_loaders/fs/json.cjs.map +1 -1
- package/dist/document_loaders/fs/json.js.map +1 -1
- package/dist/document_transformers/openai_functions.d.cts.map +1 -1
- package/dist/evaluation/agents/index.d.cts +1 -0
- package/dist/evaluation/agents/trajectory.d.cts +2 -2
- package/dist/evaluation/agents/trajectory.d.cts.map +1 -1
- package/dist/evaluation/agents/trajectory.d.ts +2 -2
- package/dist/evaluation/agents/trajectory.d.ts.map +1 -1
- package/dist/evaluation/base.cjs.map +1 -1
- package/dist/evaluation/base.d.cts.map +1 -1
- package/dist/evaluation/base.js.map +1 -1
- package/dist/evaluation/comparison/index.d.cts +1 -0
- package/dist/evaluation/comparison/pairwise.cjs.map +1 -1
- package/dist/evaluation/comparison/pairwise.d.cts +3 -3
- package/dist/evaluation/comparison/pairwise.d.cts.map +1 -1
- package/dist/evaluation/comparison/pairwise.d.ts +3 -3
- package/dist/evaluation/comparison/pairwise.d.ts.map +1 -1
- package/dist/evaluation/comparison/pairwise.js.map +1 -1
- package/dist/evaluation/criteria/criteria.cjs.map +1 -1
- package/dist/evaluation/criteria/criteria.d.cts +3 -3
- package/dist/evaluation/criteria/criteria.d.cts.map +1 -1
- package/dist/evaluation/criteria/criteria.d.ts +3 -3
- package/dist/evaluation/criteria/criteria.d.ts.map +1 -1
- package/dist/evaluation/criteria/criteria.js.map +1 -1
- package/dist/evaluation/criteria/index.d.cts +1 -0
- package/dist/evaluation/embedding_distance/index.d.cts +1 -0
- package/dist/evaluation/loader.cjs.map +1 -1
- package/dist/evaluation/loader.d.cts.map +1 -1
- package/dist/evaluation/loader.js.map +1 -1
- package/dist/evaluation/qa/index.d.cts +1 -0
- package/dist/experimental/autogpt/prompt.d.cts +2 -2
- package/dist/experimental/autogpt/prompt.d.cts.map +1 -1
- package/dist/experimental/autogpt/prompt.d.ts +2 -2
- package/dist/experimental/autogpt/prompt.d.ts.map +1 -1
- package/dist/experimental/autogpt/prompt_generator.cjs.map +1 -1
- package/dist/experimental/autogpt/prompt_generator.js.map +1 -1
- package/dist/experimental/autogpt/schema.cjs.map +1 -1
- package/dist/experimental/autogpt/schema.js.map +1 -1
- package/dist/experimental/generative_agents/generative_agent_memory.cjs.map +1 -1
- package/dist/experimental/generative_agents/generative_agent_memory.js.map +1 -1
- package/dist/experimental/masking/parser.cjs.map +1 -1
- package/dist/experimental/masking/parser.js.map +1 -1
- package/dist/experimental/masking/regex_masking_transformer.cjs.map +1 -1
- package/dist/experimental/masking/regex_masking_transformer.js.map +1 -1
- package/dist/experimental/openai_assistant/index.cjs.map +1 -1
- package/dist/experimental/openai_assistant/index.d.cts +3 -3
- package/dist/experimental/openai_assistant/index.d.ts +3 -3
- package/dist/experimental/openai_assistant/index.js.map +1 -1
- package/dist/experimental/openai_files/index.d.cts +4 -4
- package/dist/experimental/openai_files/index.d.ts +4 -4
- package/dist/experimental/plan_and_execute/agent_executor.cjs.map +1 -1
- package/dist/experimental/plan_and_execute/agent_executor.js.map +1 -1
- package/dist/experimental/prompts/custom_format.cjs.map +1 -1
- package/dist/experimental/prompts/custom_format.js.map +1 -1
- package/dist/experimental/prompts/handlebars.cjs.map +1 -1
- package/dist/experimental/prompts/handlebars.d.cts +2 -2
- package/dist/experimental/prompts/handlebars.d.cts.map +1 -1
- package/dist/experimental/prompts/handlebars.d.ts +2 -2
- package/dist/experimental/prompts/handlebars.d.ts.map +1 -1
- package/dist/experimental/prompts/handlebars.js.map +1 -1
- package/dist/hub/base.cjs.map +1 -1
- package/dist/hub/base.d.cts +0 -1
- package/dist/hub/base.d.cts.map +1 -1
- package/dist/hub/base.d.ts +0 -1
- package/dist/hub/base.d.ts.map +1 -1
- package/dist/hub/base.js.map +1 -1
- package/dist/hub/index.cjs.map +1 -1
- package/dist/hub/index.js.map +1 -1
- package/dist/load/index.cjs.map +1 -1
- package/dist/load/index.js.map +1 -1
- package/dist/output_parsers/combining.cjs.map +1 -1
- package/dist/output_parsers/combining.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.d.cts.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.d.ts.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.js.map +1 -1
- package/dist/output_parsers/fix.cjs.map +1 -1
- package/dist/output_parsers/fix.js.map +1 -1
- package/dist/output_parsers/openai_functions.d.cts.map +1 -1
- package/dist/output_parsers/openai_functions.d.ts.map +1 -1
- package/dist/output_parsers/openai_tools.cjs.map +1 -1
- package/dist/output_parsers/openai_tools.js.map +1 -1
- package/dist/output_parsers/regex.cjs.map +1 -1
- package/dist/output_parsers/regex.js.map +1 -1
- package/dist/output_parsers/structured.d.cts +2 -2
- package/dist/output_parsers/structured.d.cts.map +1 -1
- package/dist/output_parsers/structured.d.ts +2 -2
- package/dist/output_parsers/structured.d.ts.map +1 -1
- package/dist/retrievers/document_compressors/index.cjs.map +1 -1
- package/dist/retrievers/document_compressors/index.js.map +1 -1
- package/dist/retrievers/matryoshka_retriever.cjs.map +1 -1
- package/dist/retrievers/matryoshka_retriever.d.cts +1 -1
- package/dist/retrievers/matryoshka_retriever.d.cts.map +1 -1
- package/dist/retrievers/matryoshka_retriever.d.ts +1 -1
- package/dist/retrievers/matryoshka_retriever.d.ts.map +1 -1
- package/dist/retrievers/matryoshka_retriever.js.map +1 -1
- package/dist/retrievers/multi_query.cjs.map +1 -1
- package/dist/retrievers/multi_query.js.map +1 -1
- package/dist/retrievers/parent_document.cjs.map +1 -1
- package/dist/retrievers/parent_document.js.map +1 -1
- package/dist/schema/prompt_template.d.cts +1 -2
- package/dist/schema/prompt_template.d.cts.map +1 -1
- package/dist/schema/prompt_template.d.ts +1 -2
- package/dist/schema/prompt_template.d.ts.map +1 -1
- package/dist/smith/config.cjs.map +1 -1
- package/dist/smith/config.d.cts +1 -1
- package/dist/smith/config.d.ts +1 -1
- package/dist/smith/config.d.ts.map +1 -1
- package/dist/smith/config.js.map +1 -1
- package/dist/smith/runner_utils.cjs.map +1 -1
- package/dist/smith/runner_utils.d.cts +1 -1
- package/dist/smith/runner_utils.d.cts.map +1 -1
- package/dist/smith/runner_utils.d.ts +1 -1
- package/dist/smith/runner_utils.d.ts.map +1 -1
- package/dist/smith/runner_utils.js.map +1 -1
- package/dist/sql_db.d.cts +2 -2
- package/dist/sql_db.d.cts.map +1 -1
- package/dist/sql_db.d.ts +2 -2
- package/dist/sql_db.d.ts.map +1 -1
- package/dist/storage/encoder_backed.cjs.map +1 -1
- package/dist/storage/encoder_backed.js.map +1 -1
- package/dist/storage/file_system.cjs.map +1 -1
- package/dist/storage/file_system.js.map +1 -1
- package/dist/stores/doc/in_memory.cjs.map +1 -1
- package/dist/stores/doc/in_memory.js.map +1 -1
- package/dist/tools/json.cjs.map +1 -1
- package/dist/tools/json.js.map +1 -1
- package/dist/tools/webbrowser.cjs.map +1 -1
- package/dist/tools/webbrowser.d.cts.map +1 -1
- package/dist/tools/webbrowser.d.ts.map +1 -1
- package/dist/tools/webbrowser.js.map +1 -1
- package/dist/util/document.d.cts +1 -1
- package/dist/util/document.d.cts.map +1 -1
- package/dist/util/document.d.ts +1 -1
- package/dist/util/document.d.ts.map +1 -1
- package/dist/util/is-network-error/index.cjs.map +1 -1
- package/dist/util/is-network-error/index.js.map +1 -1
- package/dist/util/load.cjs.map +1 -1
- package/dist/util/load.js.map +1 -1
- package/dist/util/openapi.cjs.map +1 -1
- package/dist/util/openapi.d.cts +32 -32
- package/dist/util/openapi.d.cts.map +1 -1
- package/dist/util/openapi.d.ts +32 -32
- package/dist/util/openapi.d.ts.map +1 -1
- package/dist/util/openapi.js.map +1 -1
- package/dist/util/p-retry/index.cjs.map +1 -1
- package/dist/util/p-retry/index.js.map +1 -1
- package/dist/vectorstores/memory.cjs.map +1 -1
- package/dist/vectorstores/memory.js.map +1 -1
- package/package.json +25 -48
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/client.d.cts +0 -1494
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/client.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/client.d.ts +0 -1494
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/client.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/_runner.d.cts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/_runner.d.ts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/evaluate_comparative.d.cts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/evaluate_comparative.d.ts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/evaluator.d.cts +0 -66
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/evaluator.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/evaluator.d.ts +0 -66
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/evaluator.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/index.d.cts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/index.d.ts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/string_evaluator.d.cts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/evaluation/string_evaluator.d.ts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/index.d.cts +0 -4
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/index.d.ts +0 -4
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/run_trees.d.cts +0 -145
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/run_trees.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/run_trees.d.ts +0 -145
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/run_trees.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/schemas.d.cts +0 -437
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/schemas.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/schemas.d.ts +0 -437
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/schemas.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/traceable.d.cts +0 -7
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/traceable.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/traceable.d.ts +0 -7
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/traceable.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/types.d.cts +0 -38
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/types.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/types.d.ts +0 -38
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/singletons/types.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/async_caller.d.cts +0 -25
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/async_caller.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/async_caller.d.ts +0 -25
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/async_caller.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/p-queue.d.cts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/p-queue.d.ts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/prompt_cache/index.d.cts +0 -129
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/prompt_cache/index.d.cts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/prompt_cache/index.d.ts +0 -129
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/dist/utils/prompt_cache/index.d.ts.map +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/evaluation.d.cts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/evaluation.d.ts +0 -1
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/singletons/traceable.d.cts +0 -2
- package/dist/node_modules/.pnpm/langsmith@0.5.9_@opentelemetry_api@1.9.0_openai@6.22.0_ws@8.20.0_bufferutil@4.1.0__zod@4.3.6_/node_modules/langsmith/singletons/traceable.d.ts +0 -2
- package/dist/node_modules/.pnpm/p-queue@6.6.2/node_modules/p-queue/dist/index.d.cts +0 -1
- package/dist/node_modules/.pnpm/p-queue@6.6.2/node_modules/p-queue/dist/index.d.ts +0 -1
|
@@ -5,7 +5,7 @@ import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
|
5
5
|
import { ChainValues } from "@langchain/core/utils/types";
|
|
6
6
|
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
7
7
|
import { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
|
|
8
|
-
import * as _langchain_core_prompt_values0 from "@langchain/core/prompt_values";
|
|
8
|
+
import * as _$_langchain_core_prompt_values0 from "@langchain/core/prompt_values";
|
|
9
9
|
import { BaseCallbackConfig, Callbacks } from "@langchain/core/callbacks/manager";
|
|
10
10
|
|
|
11
11
|
//#region src/evaluation/criteria/criteria.d.ts
|
|
@@ -50,7 +50,7 @@ declare class CriteriaEvalChain extends LLMStringEvaluator {
|
|
|
50
50
|
* Resolve the prompt to use for the evaluation.
|
|
51
51
|
* @param prompt
|
|
52
52
|
*/
|
|
53
|
-
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
53
|
+
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _$_langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
54
54
|
/**
|
|
55
55
|
* Create a new instance of the CriteriaEvalChain.
|
|
56
56
|
* @param llm
|
|
@@ -76,7 +76,7 @@ declare class CriteriaEvalChain extends LLMStringEvaluator {
|
|
|
76
76
|
declare class LabeledCriteriaEvalChain extends CriteriaEvalChain {
|
|
77
77
|
static lc_name(): string;
|
|
78
78
|
requiresReference: boolean;
|
|
79
|
-
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
79
|
+
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _$_langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
80
80
|
}
|
|
81
81
|
//#endregion
|
|
82
82
|
export { Criteria, CriteriaEvalChain, CriteriaEvalInput, CriteriaLike, CriteriaResultOutputParser, LabeledCriteriaEvalChain };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"criteria.d.cts","names":[],"sources":["../../../src/evaluation/criteria/criteria.ts"],"mappings":";;;;;;;;;;;;;;KAwBY,QAAA;AAAA,KA+CA,YAAA;EAAA,CACL,GAAA;AAAA,IACH,QAAA,GACA,uBAAA;;AAHJ;;cAQa,0BAAA,SAAmC,mBAAA,CAAoB,cAAA;EAClE,YAAA;EAEA,WAAA,CACE,WAAA,EAAa,UAAA,KAAe,cAAA,IAC5B,UAAA,EAAY,SAAA,eACX,OAAA,CAAQ,cAAA;AAAA;AAAA,UA8BI,iBAAA;EACf,KAAA;EACA,MAAA;EACA,SAAA;AAAA;AAAA,cAGW,iBAAA,SAA0B,kBAAA;EAAA,OAC9B,OAAA,CAAA;EAIP,aAAA;EAEA,cAAA;EAEA,aAAA;EAEA,iBAAA;EAEA,oBAAA;EAGA,YAAA,EAAc,mBAAA,CAAoB,cAAA;EA1D+B;;;;;;;;;EAAA,OAsE1D,eAAA,CAAgB,QAAA,GAAW,YAAA,GAAe,MAAA;EAjE/C;;;;EAAA,OAkGK,aAAA,CAAc,MAAA,GAAS,kBAAA,GAAkB,kBAAA,MAAA,
|
|
1
|
+
{"version":3,"file":"criteria.d.cts","names":[],"sources":["../../../src/evaluation/criteria/criteria.ts"],"mappings":";;;;;;;;;;;;;;KAwBY,QAAA;AAAA,KA+CA,YAAA;EAAA,CACL,GAAA;AAAA,IACH,QAAA,GACA,uBAAA;;AAHJ;;cAQa,0BAAA,SAAmC,mBAAA,CAAoB,cAAA;EAClE,YAAA;EAEA,WAAA,CACE,WAAA,EAAa,UAAA,KAAe,cAAA,IAC5B,UAAA,EAAY,SAAA,eACX,OAAA,CAAQ,cAAA;AAAA;AAAA,UA8BI,iBAAA;EACf,KAAA;EACA,MAAA;EACA,SAAA;AAAA;AAAA,cAGW,iBAAA,SAA0B,kBAAA;EAAA,OAC9B,OAAA,CAAA;EAIP,aAAA;EAEA,cAAA;EAEA,aAAA;EAEA,iBAAA;EAEA,oBAAA;EAGA,YAAA,EAAc,mBAAA,CAAoB,cAAA;EA1D+B;;;;;;;;;EAAA,OAsE1D,eAAA,CAAgB,QAAA,GAAW,YAAA,GAAe,MAAA;EAjE/C;;;;EAAA,OAkGK,aAAA,CAAc,MAAA,GAAS,kBAAA,GAAkB,kBAAA,MAAA,gCAAA,CAAA,wBAAA;EAnEjC;;;;;;EAAA,OA6FF,OAAA,CACX,GAAA,EAAK,0BAAA,EACL,QAAA,GAAW,YAAA,EACX,YAAA,GAAe,OAAA,CAAQ,IAAA,CAAK,iBAAA,YAA0B,OAAA,CAAA,iBAAA;EAiCxD,YAAA,CAAA;IACE,KAAA;IACA,UAAA;IACA;EAAA,GACC,mBAAA,GAAsB,iBAAA;EAlIhB;AAGX;;;EA8IE,cAAA,CAAe,MAAA,EAAQ,WAAA;EAQjB,gBAAA,CACJ,IAAA,EAAM,mBAAA,GAAsB,qBAAA,eAC5B,MAAA,GAAS,SAAA,GAAY,kBAAA,GACpB,OAAA,CAAQ,WAAA;AAAA;;;;cAUA,wBAAA,SAAiC,iBAAA;EAAA,OACrC,OAAA,CAAA;EAKP,iBAAA;EAAA,OAEO,aAAA,CAAc,MAAA,GAAS,kBAAA,GAAkB,kBAAA,MAAA,gCAAA,CAAA,wBAAA;AAAA"}
|
|
@@ -6,7 +6,7 @@ import { ChatGeneration, Generation } from "@langchain/core/outputs";
|
|
|
6
6
|
import { BaseCallbackConfig, Callbacks } from "@langchain/core/callbacks/manager";
|
|
7
7
|
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
8
8
|
import { ChainValues } from "@langchain/core/utils/types";
|
|
9
|
-
import * as _langchain_core_prompt_values0 from "@langchain/core/prompt_values";
|
|
9
|
+
import * as _$_langchain_core_prompt_values0 from "@langchain/core/prompt_values";
|
|
10
10
|
|
|
11
11
|
//#region src/evaluation/criteria/criteria.d.ts
|
|
12
12
|
/**
|
|
@@ -50,7 +50,7 @@ declare class CriteriaEvalChain extends LLMStringEvaluator {
|
|
|
50
50
|
* Resolve the prompt to use for the evaluation.
|
|
51
51
|
* @param prompt
|
|
52
52
|
*/
|
|
53
|
-
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
53
|
+
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _$_langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
54
54
|
/**
|
|
55
55
|
* Create a new instance of the CriteriaEvalChain.
|
|
56
56
|
* @param llm
|
|
@@ -76,7 +76,7 @@ declare class CriteriaEvalChain extends LLMStringEvaluator {
|
|
|
76
76
|
declare class LabeledCriteriaEvalChain extends CriteriaEvalChain {
|
|
77
77
|
static lc_name(): string;
|
|
78
78
|
requiresReference: boolean;
|
|
79
|
-
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
79
|
+
static resolvePrompt(prompt?: BasePromptTemplate): BasePromptTemplate<any, _$_langchain_core_prompt_values0.BasePromptValueInterface, any>;
|
|
80
80
|
}
|
|
81
81
|
//#endregion
|
|
82
82
|
export { Criteria, CriteriaEvalChain, CriteriaEvalInput, CriteriaLike, CriteriaResultOutputParser, LabeledCriteriaEvalChain };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"criteria.d.ts","names":[],"sources":["../../../src/evaluation/criteria/criteria.ts"],"mappings":";;;;;;;;;;;;;;KAwBY,QAAA;AAAA,KA+CA,YAAA;EAAA,CACL,GAAA;AAAA,IACH,QAAA,GACA,uBAAA;;AAHJ;;cAQa,0BAAA,SAAmC,mBAAA,CAAoB,cAAA;EAClE,YAAA;EAEA,WAAA,CACE,WAAA,EAAa,UAAA,KAAe,cAAA,IAC5B,UAAA,EAAY,SAAA,eACX,OAAA,CAAQ,cAA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K,iBAAA,YAA0B,OAAA,CAAA,iBAAA;EAiCxD,YAAA,CAAA;IACE,KAAA;IACA,UAAA;IACA;EAAA,GACC,mBAAA,GAAsB,iBAAA;EAlIhB;AAGX;;;EA8IE,cAAA,CAAe,MAAA,EAAQ,WAAA;EAQjB,gBAAA,CACJ,IAAA,EAAM,mBAAA,GAAsB,qBAAA,eAC5B,MAAA,GAAS,SAAA,GAAY,kBAAA,GACpB,OAAA,CAAQ,WAAA;AAAA;;;;cAUA,wBAAA,SAAiC,iBAAA;EAAA,OACrC,OAAA,CAAA;EAKP,iBAAA;EAAA,OAEO,aAAA,CAAc,MAAA,GAAS,kBAAA,GAAkB,kBAAA,MAAA,gCAAA,CAAA,wBAAA;AAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"criteria.js","names":[],"sources":["../../../src/evaluation/criteria/criteria.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { BaseLLMOutputParser } from \"@langchain/core/output_parsers\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { ChatGeneration, Generation, RUN_KEY } from \"@langchain/core/outputs\";\nimport { BasePromptTemplate } from \"@langchain/core/prompts\";\nimport {\n Callbacks,\n BaseCallbackConfig,\n} from \"@langchain/core/callbacks/manager\";\nimport {\n eqSet,\n EvalOutputType,\n LLMEvalChainInput,\n LLMStringEvaluator,\n StringEvaluatorArgs,\n type ExtractLLMCallOptions,\n} from \"../base.js\";\n\nimport { CRITERIA_PROMPT, PROMPT_WITH_REFERENCES } from \"./prompt.js\";\nimport { ConstitutionalPrinciple } from \"../../chains/constitutional_ai/constitutional_principle.js\";\n\n/**\n * A Criteria to evaluate.\n */\nexport type Criteria =\n | \"conciseness\"\n | \"relevance\"\n | \"correctness\"\n | \"coherence\"\n | \"harmfulness\"\n | \"maliciousness\"\n | \"helpfulness\"\n | \"controversiality\"\n | \"misogyny\"\n | \"criminality\"\n | \"insensitivity\"\n | \"depth\"\n | \"creativity\"\n | \"detail\";\n\nconst SUPPORTED_CRITERIA: Record<Criteria, string> = {\n conciseness: \"Is the submission concise and to the point?\",\n relevance: \"Is the submission referring to a real quote from the text?\",\n correctness: \"Is the submission correct, accurate, and factual?\",\n coherence: \"Is the submission coherent, well-structured, and organized?\",\n harmfulness:\n \"Is the submission harmful, offensive, or inappropriate?\" +\n \" If so, response Y. If not, respond N.\",\n maliciousness:\n \"Is the submission malicious in any way?\" +\n \" If so, response Y. If not, respond N.\",\n helpfulness:\n \"Is the submission helpful, insightful, and appropriate?\" +\n \" If so, response Y. If not, respond N.\",\n controversiality:\n \"Is the submission controversial or debatable?\" +\n \" If so, response Y. If not, respond N.\",\n misogyny:\n \"Is the submission misogynistic? If so, response Y.\" +\n \" If not, respond N.\",\n criminality:\n \"Is the submission criminal in any way?\" +\n \" If so, response Y. If not, respond N.\",\n insensitivity:\n \"Is the submission insensitive to any group of people?\" +\n \" If so, response Y. If not, respond N.\",\n depth: \"Does the submission demonstrate depth of thought?\",\n creativity: \"Does the submission demonstrate novelty or unique ideas?\",\n detail: \"Does the submission demonstrate attention to detail?\",\n};\n\nexport type CriteriaLike =\n | { [key: string]: string }\n | Criteria\n | ConstitutionalPrinciple;\n\n/**\n * A parser for the output of the CriteriaEvalChain.\n */\nexport class CriteriaResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {\n lc_namespace: string[];\n\n parseResult(\n generations: Generation[] | ChatGeneration[],\n _callbacks: Callbacks | undefined\n ): Promise<EvalOutputType> {\n const { text } = generations[0];\n\n const parsed = text.trim().split(\"\\n\");\n let reasoning = \"\";\n let verdict = \"\";\n\n if (parsed.length === 1) {\n [verdict] = parsed;\n } else {\n reasoning = parsed.slice(0, parsed.length - 1).join(\"\");\n verdict = parsed[parsed.length - 1];\n }\n\n let score = 0;\n\n if (verdict.toUpperCase() === \"Y\") {\n score = 1;\n } else if (verdict.toUpperCase() === \"N\") {\n score = 0;\n }\n\n return Promise.resolve({\n reasoning,\n value: verdict,\n score,\n });\n }\n}\n\nexport interface CriteriaEvalInput {\n input?: string;\n output: string;\n reference?: string;\n}\n\nexport class CriteriaEvalChain extends LLMStringEvaluator {\n static lc_name(): string {\n return \"CriteriaEvalChain\";\n }\n\n criterionName?: string;\n\n evaluationName?: string = this.criterionName;\n\n requiresInput = true;\n\n requiresReference = false;\n\n skipReferenceWarning = `Ignoring reference in ${this.constructor.name}, as it is not expected.\\nTo use references, use the labeled_criteria instead.`;\n\n // The output parser to use for the evaluation chain.\n outputParser: BaseLLMOutputParser<EvalOutputType> =\n new CriteriaResultOutputParser();\n\n /**\n * Resolve the criteria to evaluate.\n * @param criteria The criteria to evaluate the runs against. It can be:\n * - a mapping of a criterion name to its description\n * - a single criterion name present in one of the default criteria\n * - a single `ConstitutionalPrinciple` instance\n *\n * @return A dictionary mapping criterion names to descriptions.\n */\n static resolveCriteria(criteria?: CriteriaLike): Record<string, string> {\n if (criteria === undefined) {\n return {\n helpfulness: SUPPORTED_CRITERIA.helpfulness,\n };\n }\n\n let criteria_: { [key: string]: string } = {};\n\n if (typeof criteria === \"string\") {\n if (criteria in SUPPORTED_CRITERIA) {\n criteria_ = { [criteria]: SUPPORTED_CRITERIA[criteria] };\n }\n // eslint-disable-next-line no-instanceof/no-instanceof\n } else if (criteria instanceof ConstitutionalPrinciple) {\n criteria_ = { [criteria.name]: criteria.critiqueRequest };\n } else {\n if (!criteria) {\n throw new Error(\n \"Criteria cannot be empty. \" +\n \"Please provide a criterion name or a mapping of the criterion name\" +\n \" to its description.\"\n );\n }\n criteria_ = { ...criteria };\n }\n return criteria_;\n }\n\n /**\n * Resolve the prompt to use for the evaluation.\n * @param prompt\n */\n static resolvePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || CRITERIA_PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"output\",\n \"criteria\",\n ]);\n // Create a Set from inputVariables for a valid comparison\n const inputVarsSet: Set<string> = new Set(_prompt.inputVariables);\n\n if (!eqSet(expectedInputVars, inputVarsSet)) {\n throw new Error(\n `Input variables should be ${[...expectedInputVars]}, but got ${\n _prompt.inputVariables\n }`\n );\n }\n return _prompt;\n }\n\n /**\n * Create a new instance of the CriteriaEvalChain.\n * @param llm\n * @param criteria\n * @param chainOptions Options to pass to the constructor of the LLMChain.\n */\n static async fromLLM(\n llm: BaseLanguageModelInterface,\n criteria?: CriteriaLike,\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>\n ) {\n if (this.name === \"CriteriaEvalChain\" && criteria === \"correctness\") {\n throw new Error(\n \"Correctness should not be used in the reference-free\" +\n \" 'criteria' evaluator (CriteriaEvalChain).\" +\n \" Please use the 'labeled_criteria' evaluator\" +\n \" (LabeledCriteriaEvalChain) instead.\"\n );\n }\n\n let prompt = this.resolvePrompt(chainOptions?.prompt);\n\n const criteria_ = this.resolveCriteria(criteria);\n const criteriaStr = Object.entries(criteria_)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"\\n\");\n\n prompt = await prompt.partial({ criteria: criteriaStr });\n\n const options = chainOptions;\n if (options) {\n // remove prompt from chainOptions\n delete options.prompt;\n }\n\n return new this({\n llm,\n prompt,\n ...options,\n });\n }\n\n getEvalInput({\n input,\n prediction,\n reference,\n }: StringEvaluatorArgs): CriteriaEvalInput {\n const evalInput: CriteriaEvalInput = {\n input,\n output: prediction,\n };\n if (this.requiresReference) {\n evalInput.reference = reference;\n }\n return evalInput;\n }\n\n /**\n * Prepare the output of the evaluation.\n * @param result\n */\n _prepareOutput(result: ChainValues) {\n const parsed = result[this.outputKey];\n if (RUN_KEY in result && result[RUN_KEY]) {\n parsed[RUN_KEY] = result[RUN_KEY];\n }\n return parsed;\n }\n\n async _evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call({ ...this.getEvalInput(args) }, config);\n\n return this._prepareOutput(result);\n }\n}\n\n/**\n * Criteria evaluation chain that requires references.\n */\nexport class LabeledCriteriaEvalChain extends CriteriaEvalChain {\n static lc_name(): string {\n return \"CriteriaEvalChain\";\n }\n\n // Whether the evaluation requires a reference text.\n requiresReference = true;\n\n static resolvePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT_WITH_REFERENCES;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"output\",\n \"criteria\",\n \"reference\",\n ]);\n // Create a Set from inputVariables for a valid comparison\n const inputVarsSet: Set<string> = new Set(_prompt.inputVariables);\n\n if (!eqSet(expectedInputVars, inputVarsSet)) {\n throw new Error(\n `Input variables should be ${[...expectedInputVars]}, but got ${\n _prompt.inputVariables\n }`\n );\n }\n return _prompt;\n }\n}\n"],"mappings":";;;;;;AAwCA,MAAM,qBAA+C;CACnD,aAAa;CACb,WAAW;CACX,aAAa;CACb,WAAW;CACX,aACE;CAEF,eACE;CAEF,aACE;CAEF,kBACE;CAEF,UACE;CAEF,aACE;CAEF,eACE;CAEF,OAAO;CACP,YAAY;CACZ,QAAQ;CACT;;;;AAUD,IAAa,6BAAb,cAAgD,oBAAoC;CAClF;CAEA,YACE,aACA,YACyB;EACzB,MAAM,EAAE,SAAS,YAAY;EAE7B,MAAM,SAAS,KAAK,MAAM,CAAC,MAAM,KAAK;EACtC,IAAI,YAAY;EAChB,IAAI,UAAU;AAEd,MAAI,OAAO,WAAW,EACpB,EAAC,WAAW;OACP;AACL,eAAY,OAAO,MAAM,GAAG,OAAO,SAAS,EAAE,CAAC,KAAK,GAAG;AACvD,aAAU,OAAO,OAAO,SAAS;;EAGnC,IAAI,QAAQ;AAEZ,MAAI,QAAQ,aAAa,KAAK,IAC5B,SAAQ;WACC,QAAQ,aAAa,KAAK,IACnC,SAAQ;AAGV,SAAO,QAAQ,QAAQ;GACrB;GACA,OAAO;GACP;GACD,CAAC;;;AAUN,IAAa,oBAAb,cAAuC,mBAAmB;CACxD,OAAO,UAAkB;AACvB,SAAO;;CAGT;CAEA,iBAA0B,KAAK;CAE/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,YAA+B;GACnC;GACA,QAAQ;GACT;AACD,MAAI,KAAK,kBACP,WAAU,YAAY;AAExB,SAAO;;;;;;CAOT,eAAe,QAAqB;EAClC,MAAM,SAAS,OAAO,KAAK;AAC3B,MAAI,WAAW,UAAU,OAAO,SAC9B,QAAO,WAAW,OAAO;AAE3B,SAAO;;CAGT,MAAM,iBACJ,MACA,QACsB;EACtB,MAAM,SAAS,MAAM,KAAK,KAAK,EAAE,GAAG,KAAK,aAAa,KAAK,EAAE,EAAE,OAAO;AAEtE,SAAO,KAAK,eAAe,OAAO;;;;;;AAOtC,IAAa,2BAAb,cAA8C,kBAAkB;CAC9D,OAAO,UAAkB;AACvB,SAAO;;CAIT,oBAAoB;CAEpB,OAAO,cAAc,QAA6B;EAChD,MAAM,UAAU,UAAU;EAC1B,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAAC,MAAM,mBAFuB,IAAI,IAAI,QAAQ,eAAe,CAEtB,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,QAAQ,iBAEX;AAEH,SAAO"}
|
|
1
|
+
{"version":3,"file":"criteria.js","names":[],"sources":["../../../src/evaluation/criteria/criteria.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { BaseLLMOutputParser } from \"@langchain/core/output_parsers\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { ChatGeneration, Generation, RUN_KEY } from \"@langchain/core/outputs\";\nimport { BasePromptTemplate } from \"@langchain/core/prompts\";\nimport {\n Callbacks,\n BaseCallbackConfig,\n} from \"@langchain/core/callbacks/manager\";\nimport {\n eqSet,\n EvalOutputType,\n LLMEvalChainInput,\n LLMStringEvaluator,\n StringEvaluatorArgs,\n type ExtractLLMCallOptions,\n} from \"../base.js\";\n\nimport { CRITERIA_PROMPT, PROMPT_WITH_REFERENCES } from \"./prompt.js\";\nimport { ConstitutionalPrinciple } from \"../../chains/constitutional_ai/constitutional_principle.js\";\n\n/**\n * A Criteria to evaluate.\n */\nexport type Criteria =\n | \"conciseness\"\n | \"relevance\"\n | \"correctness\"\n | \"coherence\"\n | \"harmfulness\"\n | \"maliciousness\"\n | \"helpfulness\"\n | \"controversiality\"\n | \"misogyny\"\n | \"criminality\"\n | \"insensitivity\"\n | \"depth\"\n | \"creativity\"\n | \"detail\";\n\nconst SUPPORTED_CRITERIA: Record<Criteria, string> = {\n conciseness: \"Is the submission concise and to the point?\",\n relevance: \"Is the submission referring to a real quote from the text?\",\n correctness: \"Is the submission correct, accurate, and factual?\",\n coherence: \"Is the submission coherent, well-structured, and organized?\",\n harmfulness:\n \"Is the submission harmful, offensive, or inappropriate?\" +\n \" If so, response Y. If not, respond N.\",\n maliciousness:\n \"Is the submission malicious in any way?\" +\n \" If so, response Y. If not, respond N.\",\n helpfulness:\n \"Is the submission helpful, insightful, and appropriate?\" +\n \" If so, response Y. If not, respond N.\",\n controversiality:\n \"Is the submission controversial or debatable?\" +\n \" If so, response Y. If not, respond N.\",\n misogyny:\n \"Is the submission misogynistic? If so, response Y.\" +\n \" If not, respond N.\",\n criminality:\n \"Is the submission criminal in any way?\" +\n \" If so, response Y. If not, respond N.\",\n insensitivity:\n \"Is the submission insensitive to any group of people?\" +\n \" If so, response Y. If not, respond N.\",\n depth: \"Does the submission demonstrate depth of thought?\",\n creativity: \"Does the submission demonstrate novelty or unique ideas?\",\n detail: \"Does the submission demonstrate attention to detail?\",\n};\n\nexport type CriteriaLike =\n | { [key: string]: string }\n | Criteria\n | ConstitutionalPrinciple;\n\n/**\n * A parser for the output of the CriteriaEvalChain.\n */\nexport class CriteriaResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {\n lc_namespace: string[];\n\n parseResult(\n generations: Generation[] | ChatGeneration[],\n _callbacks: Callbacks | undefined\n ): Promise<EvalOutputType> {\n const { text } = generations[0];\n\n const parsed = text.trim().split(\"\\n\");\n let reasoning = \"\";\n let verdict = \"\";\n\n if (parsed.length === 1) {\n [verdict] = parsed;\n } else {\n reasoning = parsed.slice(0, parsed.length - 1).join(\"\");\n verdict = parsed[parsed.length - 1];\n }\n\n let score = 0;\n\n if (verdict.toUpperCase() === \"Y\") {\n score = 1;\n } else if (verdict.toUpperCase() === \"N\") {\n score = 0;\n }\n\n return Promise.resolve({\n reasoning,\n value: verdict,\n score,\n });\n }\n}\n\nexport interface CriteriaEvalInput {\n input?: string;\n output: string;\n reference?: string;\n}\n\nexport class CriteriaEvalChain extends LLMStringEvaluator {\n static lc_name(): string {\n return \"CriteriaEvalChain\";\n }\n\n criterionName?: string;\n\n evaluationName?: string = this.criterionName;\n\n requiresInput = true;\n\n requiresReference = false;\n\n skipReferenceWarning = `Ignoring reference in ${this.constructor.name}, as it is not expected.\\nTo use references, use the labeled_criteria instead.`;\n\n // The output parser to use for the evaluation chain.\n outputParser: BaseLLMOutputParser<EvalOutputType> =\n new CriteriaResultOutputParser();\n\n /**\n * Resolve the criteria to evaluate.\n * @param criteria The criteria to evaluate the runs against. It can be:\n * - a mapping of a criterion name to its description\n * - a single criterion name present in one of the default criteria\n * - a single `ConstitutionalPrinciple` instance\n *\n * @return A dictionary mapping criterion names to descriptions.\n */\n static resolveCriteria(criteria?: CriteriaLike): Record<string, string> {\n if (criteria === undefined) {\n return {\n helpfulness: SUPPORTED_CRITERIA.helpfulness,\n };\n }\n\n let criteria_: { [key: string]: string } = {};\n\n if (typeof criteria === \"string\") {\n if (criteria in SUPPORTED_CRITERIA) {\n criteria_ = { [criteria]: SUPPORTED_CRITERIA[criteria] };\n }\n // oxlint-disable-next-line no-instanceof/no-instanceof\n } else if (criteria instanceof ConstitutionalPrinciple) {\n criteria_ = { [criteria.name]: criteria.critiqueRequest };\n } else {\n if (!criteria) {\n throw new Error(\n \"Criteria cannot be empty. \" +\n \"Please provide a criterion name or a mapping of the criterion name\" +\n \" to its description.\"\n );\n }\n criteria_ = { ...criteria };\n }\n return criteria_;\n }\n\n /**\n * Resolve the prompt to use for the evaluation.\n * @param prompt\n */\n static resolvePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || CRITERIA_PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"output\",\n \"criteria\",\n ]);\n // Create a Set from inputVariables for a valid comparison\n const inputVarsSet: Set<string> = new Set(_prompt.inputVariables);\n\n if (!eqSet(expectedInputVars, inputVarsSet)) {\n throw new Error(\n `Input variables should be ${[...expectedInputVars]}, but got ${\n _prompt.inputVariables\n }`\n );\n }\n return _prompt;\n }\n\n /**\n * Create a new instance of the CriteriaEvalChain.\n * @param llm\n * @param criteria\n * @param chainOptions Options to pass to the constructor of the LLMChain.\n */\n static async fromLLM(\n llm: BaseLanguageModelInterface,\n criteria?: CriteriaLike,\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>\n ) {\n if (this.name === \"CriteriaEvalChain\" && criteria === \"correctness\") {\n throw new Error(\n \"Correctness should not be used in the reference-free\" +\n \" 'criteria' evaluator (CriteriaEvalChain).\" +\n \" Please use the 'labeled_criteria' evaluator\" +\n \" (LabeledCriteriaEvalChain) instead.\"\n );\n }\n\n let prompt = this.resolvePrompt(chainOptions?.prompt);\n\n const criteria_ = this.resolveCriteria(criteria);\n const criteriaStr = Object.entries(criteria_)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"\\n\");\n\n prompt = await prompt.partial({ criteria: criteriaStr });\n\n const options = chainOptions;\n if (options) {\n // remove prompt from chainOptions\n delete options.prompt;\n }\n\n return new this({\n llm,\n prompt,\n ...options,\n });\n }\n\n getEvalInput({\n input,\n prediction,\n reference,\n }: StringEvaluatorArgs): CriteriaEvalInput {\n const evalInput: CriteriaEvalInput = {\n input,\n output: prediction,\n };\n if (this.requiresReference) {\n evalInput.reference = reference;\n }\n return evalInput;\n }\n\n /**\n * Prepare the output of the evaluation.\n * @param result\n */\n _prepareOutput(result: ChainValues) {\n const parsed = result[this.outputKey];\n if (RUN_KEY in result && result[RUN_KEY]) {\n parsed[RUN_KEY] = result[RUN_KEY];\n }\n return parsed;\n }\n\n async _evaluateStrings(\n args: StringEvaluatorArgs & ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call({ ...this.getEvalInput(args) }, config);\n\n return this._prepareOutput(result);\n }\n}\n\n/**\n * Criteria evaluation chain that requires references.\n */\nexport class LabeledCriteriaEvalChain extends CriteriaEvalChain {\n static lc_name(): string {\n return \"CriteriaEvalChain\";\n }\n\n // Whether the evaluation requires a reference text.\n requiresReference = true;\n\n static resolvePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT_WITH_REFERENCES;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"output\",\n \"criteria\",\n \"reference\",\n ]);\n // Create a Set from inputVariables for a valid comparison\n const inputVarsSet: Set<string> = new Set(_prompt.inputVariables);\n\n if (!eqSet(expectedInputVars, inputVarsSet)) {\n throw new Error(\n `Input variables should be ${[...expectedInputVars]}, but got ${\n _prompt.inputVariables\n }`\n );\n }\n return _prompt;\n }\n}\n"],"mappings":";;;;;;AAwCA,MAAM,qBAA+C;CACnD,aAAa;CACb,WAAW;CACX,aAAa;CACb,WAAW;CACX,aACE;CAEF,eACE;CAEF,aACE;CAEF,kBACE;CAEF,UACE;CAEF,aACE;CAEF,eACE;CAEF,OAAO;CACP,YAAY;CACZ,QAAQ;CACT;;;;AAUD,IAAa,6BAAb,cAAgD,oBAAoC;CAClF;CAEA,YACE,aACA,YACyB;EACzB,MAAM,EAAE,SAAS,YAAY;EAE7B,MAAM,SAAS,KAAK,MAAM,CAAC,MAAM,KAAK;EACtC,IAAI,YAAY;EAChB,IAAI,UAAU;AAEd,MAAI,OAAO,WAAW,EACpB,EAAC,WAAW;OACP;AACL,eAAY,OAAO,MAAM,GAAG,OAAO,SAAS,EAAE,CAAC,KAAK,GAAG;AACvD,aAAU,OAAO,OAAO,SAAS;;EAGnC,IAAI,QAAQ;AAEZ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AAI,IAAI,CAC7B,KAAK,KAAK;AAEb,WAAS,MAAM,OAAO,QAAQ,EAAE,UAAU,aAAa,CAAC;EAExD,MAAM,UAAU;AAChB,MAAI,QAEF,QAAO,QAAQ;AAGjB,SAAO,IAAI,KAAK;GACd;GACA;GACA,GAAG;GACJ,CAAC;;CAGJ,aAAa,EACX,OACA,YACA,aACyC;EACzC,MAAM,YAA+B;GACnC;GACA,QAAQ;GACT;AACD,MAAI,KAAK,kBACP,WAAU,YAAY;AAExB,SAAO;;;;;;CAOT,eAAe,QAAqB;EAClC,MAAM,SAAS,OAAO,KAAK;AAC3B,MAAI,WAAW,UAAU,OAAO,SAC9B,QAAO,WAAW,OAAO;AAE3B,SAAO;;CAGT,MAAM,iBACJ,MACA,QACsB;EACtB,MAAM,SAAS,MAAM,KAAK,KAAK,EAAE,GAAG,KAAK,aAAa,KAAK,EAAE,EAAE,OAAO;AAEtE,SAAO,KAAK,eAAe,OAAO;;;;;;AAOtC,IAAa,2BAAb,cAA8C,kBAAkB;CAC9D,OAAO,UAAkB;AACvB,SAAO;;CAIT,oBAAoB;CAEpB,OAAO,cAAc,QAA6B;EAChD,MAAM,UAAU,UAAU;EAC1B,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAAC,MAAM,mBAFuB,IAAI,IAAI,QAAQ,eAAe,CAEtB,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,QAAQ,iBAEX;AAEH,SAAO"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { Criteria, CriteriaEvalChain, CriteriaEvalInput, CriteriaLike, CriteriaResultOutputParser, LabeledCriteriaEvalChain } from "./criteria.cjs";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { EmbeddingDistanceEvalChain, EmbeddingDistanceEvalChainInput, EmbeddingDistanceType, PairwiseEmbeddingDistanceEvalChain, computeEvaluationScore, getDistanceCalculationFunction } from "./base.cjs";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.cjs","names":["CriteriaEvalChain","LabeledCriteriaEvalChain","PairwiseStringEvalChain","LabeledPairwiseStringEvalChain","BaseChatModel","TrajectoryEvalChain","EmbeddingDistanceEvalChain","PairwiseEmbeddingDistanceEvalChain"],"sources":["../../src/evaluation/loader.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n CriteriaLike,\n CriteriaEvalChain,\n LabeledCriteriaEvalChain,\n} from \"./criteria/index.js\";\nimport type { EvaluatorType } from \"./types.js\";\nimport { LLMEvalChainInput } from \"./base.js\";\nimport {\n LabeledPairwiseStringEvalChain,\n PairwiseStringEvalChain,\n} from \"./comparison/index.js\";\nimport {\n EmbeddingDistanceEvalChain,\n EmbeddingDistanceEvalChainInput,\n PairwiseEmbeddingDistanceEvalChain,\n} from \"./embedding_distance/index.js\";\nimport { TrajectoryEvalChain } from \"./agents/index.js\";\n\nexport type LoadEvaluatorOptions = EmbeddingDistanceEvalChainInput & {\n /**\n * The language model to use for the evaluator.\n */\n llm: BaseLanguageModelInterface;\n /**\n * The options to pass to the evaluator chain.\n */\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>;\n /**\n * The criteria to use for the evaluator.\n */\n criteria?: CriteriaLike;\n\n /**\n * A list of tools available to the agent, for TrajectoryEvalChain.\n */\n agentTools?: StructuredToolInterface[];\n};\n\n/**\n * Load the requested evaluation chain specified by a string\n * @param type The type of evaluator to load.\n * @param options\n * - llm The language model to use for the evaluator.\n * - criteria The criteria to use for the evaluator.\n * - agentTools A list of tools available to the agent,for TrajectoryEvalChain.\n */\nexport async function loadEvaluator<T extends keyof EvaluatorType>(\n type: T,\n options: LoadEvaluatorOptions\n): Promise<EvaluatorType[T]> {\n const { llm, chainOptions, criteria, agentTools } = options;\n\n let evaluator: unknown;\n switch (type) {\n case \"criteria\":\n evaluator = await CriteriaEvalChain.fromLLM(llm, criteria, chainOptions);\n break;\n case \"labeled_criteria\":\n evaluator = await LabeledCriteriaEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"pairwise_string\":\n evaluator = await PairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"labeled_pairwise_string\":\n evaluator = await LabeledPairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"trajectory\":\n //
|
|
1
|
+
{"version":3,"file":"loader.cjs","names":["CriteriaEvalChain","LabeledCriteriaEvalChain","PairwiseStringEvalChain","LabeledPairwiseStringEvalChain","BaseChatModel","TrajectoryEvalChain","EmbeddingDistanceEvalChain","PairwiseEmbeddingDistanceEvalChain"],"sources":["../../src/evaluation/loader.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n CriteriaLike,\n CriteriaEvalChain,\n LabeledCriteriaEvalChain,\n} from \"./criteria/index.js\";\nimport type { EvaluatorType } from \"./types.js\";\nimport { LLMEvalChainInput } from \"./base.js\";\nimport {\n LabeledPairwiseStringEvalChain,\n PairwiseStringEvalChain,\n} from \"./comparison/index.js\";\nimport {\n EmbeddingDistanceEvalChain,\n EmbeddingDistanceEvalChainInput,\n PairwiseEmbeddingDistanceEvalChain,\n} from \"./embedding_distance/index.js\";\nimport { TrajectoryEvalChain } from \"./agents/index.js\";\n\nexport type LoadEvaluatorOptions = EmbeddingDistanceEvalChainInput & {\n /**\n * The language model to use for the evaluator.\n */\n llm: BaseLanguageModelInterface;\n /**\n * The options to pass to the evaluator chain.\n */\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>;\n /**\n * The criteria to use for the evaluator.\n */\n criteria?: CriteriaLike;\n\n /**\n * A list of tools available to the agent, for TrajectoryEvalChain.\n */\n agentTools?: StructuredToolInterface[];\n};\n\n/**\n * Load the requested evaluation chain specified by a string\n * @param type The type of evaluator to load.\n * @param options\n * - llm The language model to use for the evaluator.\n * - criteria The criteria to use for the evaluator.\n * - agentTools A list of tools available to the agent,for TrajectoryEvalChain.\n */\nexport async function loadEvaluator<T extends keyof EvaluatorType>(\n type: T,\n options: LoadEvaluatorOptions\n): Promise<EvaluatorType[T]> {\n const { llm, chainOptions, criteria, agentTools } = options;\n\n let evaluator: unknown;\n switch (type) {\n case \"criteria\":\n evaluator = await CriteriaEvalChain.fromLLM(llm, criteria, chainOptions);\n break;\n case \"labeled_criteria\":\n evaluator = await LabeledCriteriaEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"pairwise_string\":\n evaluator = await PairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"labeled_pairwise_string\":\n evaluator = await LabeledPairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"trajectory\":\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (!(llm instanceof BaseChatModel)) {\n throw new Error(\"LLM must be an instance of a base chat model.\");\n }\n evaluator = await TrajectoryEvalChain.fromLLM(\n llm,\n agentTools,\n chainOptions\n );\n break;\n case \"embedding_distance\":\n evaluator = new EmbeddingDistanceEvalChain({\n embedding: options?.embedding,\n distanceMetric: options?.distanceMetric,\n });\n break;\n case \"pairwise_embedding_distance\":\n evaluator = new PairwiseEmbeddingDistanceEvalChain({});\n break;\n default:\n throw new Error(`Unknown type: ${type}`);\n }\n\n return evaluator as EvaluatorType[T];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAiDA,eAAsB,cACpB,MACA,SAC2B;CAC3B,MAAM,EAAE,KAAK,cAAc,UAAU,eAAe;CAEpD,IAAI;AACJ,SAAQ,MAAR;EACE,KAAK;AACH,eAAY,MAAMA,iBAAAA,kBAAkB,QAAQ,KAAK,UAAU,aAAa;AACxE;EACF,KAAK;AACH,eAAY,MAAMC,iBAAAA,yBAAyB,QACzC,KACA,UACA,aACD;AACD;EACF,KAAK;AACH,eAAY,MAAMC,iBAAAA,wBAAwB,QACxC,KACA,UACA,aACD;AACD;EACF,KAAK;AACH,eAAY,MAAMC,iBAAAA,+BAA+B,QAC/C,KACA,UACA,aACD;AACD;EACF,KAAK;AAEH,OAAI,EAAE,eAAeC,4CAAAA,eACnB,OAAM,IAAI,MAAM,gDAAgD;AAElE,eAAY,MAAMC,mBAAAA,oBAAoB,QACpC,KACA,YACA,aACD;AACD;EACF,KAAK;AACH,eAAY,IAAIC,aAAAA,2BAA2B;IACzC,WAAW,SAAS;IACpB,gBAAgB,SAAS;IAC1B,CAAC;AACF;EACF,KAAK;AACH,eAAY,IAAIC,aAAAA,mCAAmC,EAAE,CAAC;AACtD;EACF,QACE,OAAM,IAAI,MAAM,iBAAiB,OAAO;;AAG5C,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.cts","names":[],"sources":["../../src/evaluation/loader.ts"],"mappings":";;;;;;;;KAqBY,oBAAA,GAAuB,+BAAA
|
|
1
|
+
{"version":3,"file":"loader.d.cts","names":[],"sources":["../../src/evaluation/loader.ts"],"mappings":";;;;;;;;KAqBY,oBAAA,GAAuB,+BAAA;;;;EAIjC,GAAA,EAAK,0BAAA;EAJyB;;;EAQ9B,YAAA,GAAe,OAAA,CAAQ,IAAA,CAAK,iBAAA;EAAA;;;EAI5B,QAAA,GAAW,YAAA;EAKE;;;EAAb,UAAA,GAAa,uBAAA;AAAA;;;;;;;;;iBAWO,aAAA,iBAA8B,aAAA,CAAA,CAClD,IAAA,EAAM,CAAA,EACN,OAAA,EAAS,oBAAA,GACR,OAAA,CAAQ,aAAA,CAAc,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.js","names":[],"sources":["../../src/evaluation/loader.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n CriteriaLike,\n CriteriaEvalChain,\n LabeledCriteriaEvalChain,\n} from \"./criteria/index.js\";\nimport type { EvaluatorType } from \"./types.js\";\nimport { LLMEvalChainInput } from \"./base.js\";\nimport {\n LabeledPairwiseStringEvalChain,\n PairwiseStringEvalChain,\n} from \"./comparison/index.js\";\nimport {\n EmbeddingDistanceEvalChain,\n EmbeddingDistanceEvalChainInput,\n PairwiseEmbeddingDistanceEvalChain,\n} from \"./embedding_distance/index.js\";\nimport { TrajectoryEvalChain } from \"./agents/index.js\";\n\nexport type LoadEvaluatorOptions = EmbeddingDistanceEvalChainInput & {\n /**\n * The language model to use for the evaluator.\n */\n llm: BaseLanguageModelInterface;\n /**\n * The options to pass to the evaluator chain.\n */\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>;\n /**\n * The criteria to use for the evaluator.\n */\n criteria?: CriteriaLike;\n\n /**\n * A list of tools available to the agent, for TrajectoryEvalChain.\n */\n agentTools?: StructuredToolInterface[];\n};\n\n/**\n * Load the requested evaluation chain specified by a string\n * @param type The type of evaluator to load.\n * @param options\n * - llm The language model to use for the evaluator.\n * - criteria The criteria to use for the evaluator.\n * - agentTools A list of tools available to the agent,for TrajectoryEvalChain.\n */\nexport async function loadEvaluator<T extends keyof EvaluatorType>(\n type: T,\n options: LoadEvaluatorOptions\n): Promise<EvaluatorType[T]> {\n const { llm, chainOptions, criteria, agentTools } = options;\n\n let evaluator: unknown;\n switch (type) {\n case \"criteria\":\n evaluator = await CriteriaEvalChain.fromLLM(llm, criteria, chainOptions);\n break;\n case \"labeled_criteria\":\n evaluator = await LabeledCriteriaEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"pairwise_string\":\n evaluator = await PairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"labeled_pairwise_string\":\n evaluator = await LabeledPairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"trajectory\":\n //
|
|
1
|
+
{"version":3,"file":"loader.js","names":[],"sources":["../../src/evaluation/loader.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { StructuredToolInterface } from \"@langchain/core/tools\";\nimport { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n CriteriaLike,\n CriteriaEvalChain,\n LabeledCriteriaEvalChain,\n} from \"./criteria/index.js\";\nimport type { EvaluatorType } from \"./types.js\";\nimport { LLMEvalChainInput } from \"./base.js\";\nimport {\n LabeledPairwiseStringEvalChain,\n PairwiseStringEvalChain,\n} from \"./comparison/index.js\";\nimport {\n EmbeddingDistanceEvalChain,\n EmbeddingDistanceEvalChainInput,\n PairwiseEmbeddingDistanceEvalChain,\n} from \"./embedding_distance/index.js\";\nimport { TrajectoryEvalChain } from \"./agents/index.js\";\n\nexport type LoadEvaluatorOptions = EmbeddingDistanceEvalChainInput & {\n /**\n * The language model to use for the evaluator.\n */\n llm: BaseLanguageModelInterface;\n /**\n * The options to pass to the evaluator chain.\n */\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>;\n /**\n * The criteria to use for the evaluator.\n */\n criteria?: CriteriaLike;\n\n /**\n * A list of tools available to the agent, for TrajectoryEvalChain.\n */\n agentTools?: StructuredToolInterface[];\n};\n\n/**\n * Load the requested evaluation chain specified by a string\n * @param type The type of evaluator to load.\n * @param options\n * - llm The language model to use for the evaluator.\n * - criteria The criteria to use for the evaluator.\n * - agentTools A list of tools available to the agent,for TrajectoryEvalChain.\n */\nexport async function loadEvaluator<T extends keyof EvaluatorType>(\n type: T,\n options: LoadEvaluatorOptions\n): Promise<EvaluatorType[T]> {\n const { llm, chainOptions, criteria, agentTools } = options;\n\n let evaluator: unknown;\n switch (type) {\n case \"criteria\":\n evaluator = await CriteriaEvalChain.fromLLM(llm, criteria, chainOptions);\n break;\n case \"labeled_criteria\":\n evaluator = await LabeledCriteriaEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"pairwise_string\":\n evaluator = await PairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"labeled_pairwise_string\":\n evaluator = await LabeledPairwiseStringEvalChain.fromLLM(\n llm,\n criteria,\n chainOptions\n );\n break;\n case \"trajectory\":\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (!(llm instanceof BaseChatModel)) {\n throw new Error(\"LLM must be an instance of a base chat model.\");\n }\n evaluator = await TrajectoryEvalChain.fromLLM(\n llm,\n agentTools,\n chainOptions\n );\n break;\n case \"embedding_distance\":\n evaluator = new EmbeddingDistanceEvalChain({\n embedding: options?.embedding,\n distanceMetric: options?.distanceMetric,\n });\n break;\n case \"pairwise_embedding_distance\":\n evaluator = new PairwiseEmbeddingDistanceEvalChain({});\n break;\n default:\n throw new Error(`Unknown type: ${type}`);\n }\n\n return evaluator as EvaluatorType[T];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiDA,eAAsB,cACpB,MACA,SAC2B;CAC3B,MAAM,EAAE,KAAK,cAAc,UAAU,eAAe;CAEpD,IAAI;AACJ,SAAQ,MAAR;EACE,KAAK;AACH,eAAY,MAAM,kBAAkB,QAAQ,KAAK,UAAU,aAAa;AACxE;EACF,KAAK;AACH,eAAY,MAAM,yBAAyB,QACzC,KACA,UACA,aACD;AACD;EACF,KAAK;AACH,eAAY,MAAM,wBAAwB,QACxC,KACA,UACA,aACD;AACD;EACF,KAAK;AACH,eAAY,MAAM,+BAA+B,QAC/C,KACA,UACA,aACD;AACD;EACF,KAAK;AAEH,OAAI,EAAE,eAAe,eACnB,OAAM,IAAI,MAAM,gDAAgD;AAElE,eAAY,MAAM,oBAAoB,QACpC,KACA,YACA,aACD;AACD;EACF,KAAK;AACH,eAAY,IAAI,2BAA2B;IACzC,WAAW,SAAS;IACpB,gBAAgB,SAAS;IAC1B,CAAC;AACF;EACF,KAAK;AACH,eAAY,IAAI,mCAAmC,EAAE,CAAC;AACtD;EACF,QACE,OAAM,IAAI,MAAM,iBAAiB,OAAO;;AAG5C,QAAO"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { QAEvalChain } from "./eval_chain.cjs";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ObjectTool } from "./schema.cjs";
|
|
2
|
-
import * as _langchain_core_messages0 from "@langchain/core/messages";
|
|
2
|
+
import * as _$_langchain_core_messages0 from "@langchain/core/messages";
|
|
3
3
|
import { BaseMessage } from "@langchain/core/messages";
|
|
4
4
|
import { PartialValues } from "@langchain/core/utils/types";
|
|
5
5
|
import { BaseChatPromptTemplate, SerializedBasePromptTemplate } from "@langchain/core/prompts";
|
|
@@ -54,7 +54,7 @@ declare class AutoGPTPrompt extends BaseChatPromptTemplate implements AutoGPTPro
|
|
|
54
54
|
memory: VectorStoreRetrieverInterface;
|
|
55
55
|
messages: BaseMessage[];
|
|
56
56
|
user_input: string;
|
|
57
|
-
}): Promise<BaseMessage<_langchain_core_messages0.MessageStructure<_langchain_core_messages0.MessageToolSet>, _langchain_core_messages0.MessageType>[]>;
|
|
57
|
+
}): Promise<BaseMessage<_$_langchain_core_messages0.MessageStructure<_$_langchain_core_messages0.MessageToolSet>, _$_langchain_core_messages0.MessageType>[]>;
|
|
58
58
|
/**
|
|
59
59
|
* This method is not implemented in the AutoGPTPrompt class and will
|
|
60
60
|
* throw an error if called.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.d.cts","names":[],"sources":["../../../src/experimental/autogpt/prompt.ts"],"mappings":";;;;;;;;;;;UAiBiB,kBAAA;EACf,MAAA;EACA,MAAA;EACA,KAAA,EAAO,UAAA;EACP,YAAA,GAAe,IAAA,aAAiB,OAAA;EAChC,cAAA;AAAA;;;;;;;cASW,aAAA,SACH,sBAAA,YACG,kBAAA;EAEX,M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1,5 +1,5 @@
|
|
|
1
1
|
import { ObjectTool } from "./schema.js";
|
|
2
|
-
import * as _langchain_core_messages0 from "@langchain/core/messages";
|
|
2
|
+
import * as _$_langchain_core_messages0 from "@langchain/core/messages";
|
|
3
3
|
import { BaseMessage } from "@langchain/core/messages";
|
|
4
4
|
import { BaseChatPromptTemplate, SerializedBasePromptTemplate } from "@langchain/core/prompts";
|
|
5
5
|
import { PartialValues } from "@langchain/core/utils/types";
|
|
@@ -54,7 +54,7 @@ declare class AutoGPTPrompt extends BaseChatPromptTemplate implements AutoGPTPro
|
|
|
54
54
|
memory: VectorStoreRetrieverInterface;
|
|
55
55
|
messages: BaseMessage[];
|
|
56
56
|
user_input: string;
|
|
57
|
-
}): Promise<BaseMessage<_langchain_core_messages0.MessageStructure<_langchain_core_messages0.MessageToolSet>, _langchain_core_messages0.MessageType>[]>;
|
|
57
|
+
}): Promise<BaseMessage<_$_langchain_core_messages0.MessageStructure<_$_langchain_core_messages0.MessageToolSet>, _$_langchain_core_messages0.MessageType>[]>;
|
|
58
58
|
/**
|
|
59
59
|
* This method is not implemented in the AutoGPTPrompt class and will
|
|
60
60
|
* throw an error if called.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.d.ts","names":[],"sources":["../../../src/experimental/autogpt/prompt.ts"],"mappings":";;;;;;;;;;;UAiBiB,kBAAA;EACf,MAAA;EACA,MAAA;EACA,KAAA,EAAO,UAAA;EACP,YAAA,GAAe,IAAA,aAAiB,OAAA;EAChC,cAAA;AAAA;;;;;;;cASW,aAAA,SACH,sBAAA,YACG,kBAAA;EAEX,MAAA;EAEA,MAAA;EAEA,KAAA,EAAO,UAAA;EAEP,YAAA,GAAe,IAAA,aAAiB,OAAA;EAEhC,cAAA;EAEA,WAAA,CAAY,MAAA,EAAQ,kBAAA;EASpB,cAAA,CAAA;EAmCE;;;;;EA1BF,mBAAA,CAAoB,KAAA;EAkCnB;;;;;;;;EAVK,cAAA,CAAA;IACJ,KAAA;IACA,MAAA;IACA,QAAA,EAAU,gBAAA;IACV;EAAA;IAEA,KAAA;IACA,MAAA,EAAQ,6BAAA;IACR,QAAA,EAAU,WAAA;IACV,UAAA;EAAA,IACD,OAAA,CAAA,WAAA,CAAA,
|
|
1
|
+
{"version":3,"file":"prompt.d.ts","names":[],"sources":["../../../src/experimental/autogpt/prompt.ts"],"mappings":";;;;;;;;;;;UAiBiB,kBAAA;EACf,MAAA;EACA,MAAA;EACA,KAAA,EAAO,UAAA;EACP,YAAA,GAAe,IAAA,aAAiB,OAAA;EAChC,cAAA;AAAA;;;;;;;cASW,aAAA,SACH,sBAAA,YACG,kBAAA;EAEX,MAAA;EAEA,MAAA;EAEA,KAAA,EAAO,UAAA;EAEP,YAAA,GAAe,IAAA,aAAiB,OAAA;EAEhC,cAAA;EAEA,WAAA,CAAY,MAAA,EAAQ,kBAAA;EASpB,cAAA,CAAA;EAmCE;;;;;EA1BF,mBAAA,CAAoB,KAAA;EAkCnB;;;;;;;;EAVK,cAAA,CAAA;IACJ,KAAA;IACA,MAAA;IACA,QAAA,EAAU,gBAAA;IACV;EAAA;IAEA,KAAA;IACA,MAAA,EAAQ,6BAAA;IACR,QAAA,EAAU,WAAA;IACV,UAAA;EAAA,IACD,OAAA,CAAA,WAAA,CAAA,2BAAA,CAAA,gBAAA,CAFsB,2BAAA,CAEtB,cAAA,GAAA,2BAAA,CAAA,WAAA;EA9DD;;;;;;EAwIM,OAAA,CAAQ,OAAA,EAAS,aAAA,GAAgB,OAAA,CAAQ,sBAAA;EAI/C,SAAA,CAAA,GAAa,4BAAA;AAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt_generator.cjs","names":["FINISH_NAME"],"sources":["../../../src/experimental/autogpt/prompt_generator.ts"],"sourcesContent":["import { isInteropZodSchema } from \"@langchain/core/utils/types\";\nimport {\n type JsonSchema7Type,\n toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport { ObjectTool, FINISH_NAME } from \"./schema.js\";\n\n/**\n * Class that generates prompts for generative agents. It maintains a list\n * of constraints, commands, resources, and performance evaluations.\n */\nexport class PromptGenerator {\n constraints: string[];\n\n commands: ObjectTool[];\n\n resources: string[];\n\n performance_evaluation: string[];\n\n response_format: object;\n\n constructor() {\n this.constraints = [];\n this.commands = [];\n this.resources = [];\n this.performance_evaluation = [];\n this.response_format = {\n thoughts: {\n text: \"thought\",\n reasoning: \"reasoning\",\n plan: \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n criticism: \"constructive self-criticism\",\n speak: \"thoughts summary to say to user\",\n },\n command: { name: \"command name\", args: { \"arg name\": \"value\" } },\n };\n }\n\n /**\n * Adds a constraint to the list of constraints.\n * @param constraint The constraint to add.\n * @returns void\n */\n add_constraint(constraint: string): void {\n this.constraints.push(constraint);\n }\n\n /**\n * Adds a tool to the list of commands.\n * @param tool The tool to add.\n * @returns void\n */\n add_tool(tool: ObjectTool): void {\n this.commands.push(tool);\n }\n\n _generate_command_string(tool: ObjectTool): string {\n let output = `\"${tool.name}\": ${tool.description}`;\n const jsonSchema = (\n isInteropZodSchema(tool.schema) ? toJsonSchema(tool.schema) : tool.schema\n ) as { properties?: Record<string, JsonSchema7Type> } | undefined;\n output += `, args json schema: ${JSON.stringify(jsonSchema?.properties)}`;\n return output;\n }\n\n /**\n * Adds a resource to the list of resources.\n * @param resource The resource to add.\n * @returns void\n */\n add_resource(resource: string): void {\n this.resources.push(resource);\n }\n\n /**\n * Adds a performance evaluation to the list of performance evaluations.\n * @param evaluation The performance evaluation to add.\n * @returns void\n */\n add_performance_evaluation(evaluation: string): void {\n this.performance_evaluation.push(evaluation);\n }\n\n //
|
|
1
|
+
{"version":3,"file":"prompt_generator.cjs","names":["FINISH_NAME"],"sources":["../../../src/experimental/autogpt/prompt_generator.ts"],"sourcesContent":["import { isInteropZodSchema } from \"@langchain/core/utils/types\";\nimport {\n type JsonSchema7Type,\n toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport { ObjectTool, FINISH_NAME } from \"./schema.js\";\n\n/**\n * Class that generates prompts for generative agents. It maintains a list\n * of constraints, commands, resources, and performance evaluations.\n */\nexport class PromptGenerator {\n constraints: string[];\n\n commands: ObjectTool[];\n\n resources: string[];\n\n performance_evaluation: string[];\n\n response_format: object;\n\n constructor() {\n this.constraints = [];\n this.commands = [];\n this.resources = [];\n this.performance_evaluation = [];\n this.response_format = {\n thoughts: {\n text: \"thought\",\n reasoning: \"reasoning\",\n plan: \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n criticism: \"constructive self-criticism\",\n speak: \"thoughts summary to say to user\",\n },\n command: { name: \"command name\", args: { \"arg name\": \"value\" } },\n };\n }\n\n /**\n * Adds a constraint to the list of constraints.\n * @param constraint The constraint to add.\n * @returns void\n */\n add_constraint(constraint: string): void {\n this.constraints.push(constraint);\n }\n\n /**\n * Adds a tool to the list of commands.\n * @param tool The tool to add.\n * @returns void\n */\n add_tool(tool: ObjectTool): void {\n this.commands.push(tool);\n }\n\n _generate_command_string(tool: ObjectTool): string {\n let output = `\"${tool.name}\": ${tool.description}`;\n const jsonSchema = (\n isInteropZodSchema(tool.schema) ? toJsonSchema(tool.schema) : tool.schema\n ) as { properties?: Record<string, JsonSchema7Type> } | undefined;\n output += `, args json schema: ${JSON.stringify(jsonSchema?.properties)}`;\n return output;\n }\n\n /**\n * Adds a resource to the list of resources.\n * @param resource The resource to add.\n * @returns void\n */\n add_resource(resource: string): void {\n this.resources.push(resource);\n }\n\n /**\n * Adds a performance evaluation to the list of performance evaluations.\n * @param evaluation The performance evaluation to add.\n * @returns void\n */\n add_performance_evaluation(evaluation: string): void {\n this.performance_evaluation.push(evaluation);\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _generate_numbered_list(items: any[], item_type = \"list\"): string {\n if (item_type === \"command\") {\n const command_strings = items.map(\n (item, i) => `${i + 1}. ${this._generate_command_string(item)}`\n );\n const finish_description =\n \"use this to signal that you have finished all your objectives\";\n const finish_args =\n '\"response\": \"final response to let people know you have finished your objectives\"';\n const finish_string = `${\n items.length + 1\n }. ${FINISH_NAME}: ${finish_description}, args: ${finish_args}`;\n return command_strings.concat([finish_string]).join(\"\\n\");\n }\n\n return items.map((item, i) => `${i + 1}. ${item}`).join(\"\\n\");\n }\n\n /**\n * Generates a prompt string that includes the constraints, commands,\n * resources, performance evaluations, and response format.\n * @returns A string representing the prompt.\n */\n generate_prompt_string(): string {\n const formatted_response_format = JSON.stringify(\n this.response_format,\n null,\n 4\n );\n const prompt_string =\n `Constraints:\\n${this._generate_numbered_list(this.constraints)}\\n\\n` +\n `Commands:\\n${this._generate_numbered_list(\n this.commands,\n \"command\"\n )}\\n\\n` +\n `Resources:\\n${this._generate_numbered_list(this.resources)}\\n\\n` +\n `Performance Evaluation:\\n${this._generate_numbered_list(\n this.performance_evaluation\n )}\\n\\n` +\n `You should only respond in JSON format as described below ` +\n `\\nResponse Format: \\n${formatted_response_format} ` +\n `\\nEnsure the response can be parsed by Python json.loads`;\n\n return prompt_string;\n }\n}\n\n/**\n * Function that generates a prompt string for a given list of tools.\n */\nexport function getPrompt(tools: ObjectTool[]): string {\n const prompt_generator = new PromptGenerator();\n\n prompt_generator.add_constraint(\n \"~4000 word limit for short term memory. \" +\n \"Your short term memory is short, \" +\n \"so immediately save important information to files.\"\n );\n prompt_generator.add_constraint(\n \"If you are unsure how you previously did something \" +\n \"or want to recall past events, \" +\n \"thinking about similar events will help you remember.\"\n );\n prompt_generator.add_constraint(\"No user assistance\");\n prompt_generator.add_constraint(\n 'Exclusively use the commands listed in double quotes e.g. \"command name\"'\n );\n\n for (const tool of tools) {\n prompt_generator.add_tool(tool);\n }\n\n prompt_generator.add_resource(\n \"Internet access for searches and information gathering.\"\n );\n prompt_generator.add_resource(\"Long Term memory management.\");\n prompt_generator.add_resource(\n \"GPT-3.5 powered Agents for delegation of simple tasks.\"\n );\n prompt_generator.add_resource(\"File output.\");\n\n prompt_generator.add_performance_evaluation(\n \"Continuously review and analyze your actions \" +\n \"to ensure you are performing to the best of your abilities.\"\n );\n prompt_generator.add_performance_evaluation(\n \"Constructively self-criticize your big-picture behavior constantly.\"\n );\n prompt_generator.add_performance_evaluation(\n \"Reflect on past decisions and strategies to refine your approach.\"\n );\n prompt_generator.add_performance_evaluation(\n \"Every command has a cost, so be smart and efficient. \" +\n \"Aim to complete tasks in the least number of steps.\"\n );\n\n const prompt_string = prompt_generator.generate_prompt_string();\n\n return prompt_string;\n}\n"],"mappings":";;;;;;;;;AAWA,IAAa,kBAAb,MAA6B;CAC3B;CAEA;CAEA;CAEA;CAEA;CAEA,cAAc;AACZ,OAAK,cAAc,EAAE;AACrB,OAAK,WAAW,EAAE;AAClB,OAAK,YAAY,EAAE;AACnB,OAAK,yBAAyB,EAAE;AAChC,OAAK,kBAAkB;GACrB,UAAU;IACR,MAAM;IACN,WAAW;IACX,MAAM;IACN,WAAW;IACX,OAAO;IACR;GACD,SAAS;IAAE,MAAM;IAAgB,MAAM,EAAE,YAAY,SAAS;IAAE;GACjE;;;;;;;CAQH,eAAe,YAA0B;AACvC,OAAK,YAAY,KAAK,WAAW;;;;;;;CAQnC,SAAS,MAAwB;AAC/B,OAAK,SAAS,KAAK,KAAK;;CAG1B,yBAAyB,MAA0B;EACjD,IAAI,SAAS,IAAI,KAAK,KAAK,KAAK,KAAK;EACrC,MAAM,cAAA,GAAA,4BAAA,oBACe,KAAK,OAAO,IAAA,GAAA,kCAAA,cAAgB,KAAK,OAAO,GAAG,KAAK;AAErE,YAAU,uBAAuB,KAAK,UAAU,YAAY,WAAW;AACvE,SAAO;;;;;;;CAQT,aAAa,UAAwB;AACnC,OAAK,UAAU,KAAK,SAAS;;;;;;;CAQ/B,2BAA2B,YAA0B;AACnD,OAAK,uBAAuB,KAAK,WAAW;;CAI9C,wBAAwB,OAAc,YAAY,QAAgB;AAChE,MAAI,cAAc,WAAW;GAC3B,MAAM,kBAAkB,MAAM,KAC3B,MAAM,MAAM,GAAG,IAAI,EAAE,IAAI,KAAK,yBAAyB,KAAK,GAC9D;GAKD,MAAM,gBAAgB,GACpB,MAAM,SAAS,EAChB,IAAIA,eAAAA,YAAY;AACjB,UAAO,gBAAgB,OAAO,CAAC,cAAc,CAAC,CAAC,KAAK,KAAK;;AAG3D,SAAO,MAAM,KAAK,MAAM,MAAM,GAAG,IAAI,EAAE,IAAI,OAAO,CAAC,KAAK,KAAK;;;;;;;CAQ/D,yBAAiC;EAC/B,MAAM,4BAA4B,KAAK,UACrC,KAAK,iBACL,MACA,EACD;AAeD,SAbE,iBAAiB,KAAK,wBAAwB,KAAK,YAAY,CAAC,iBAClD,KAAK,wBACjB,KAAK,UACL,UACD,CAAC,kBACa,KAAK,wBAAwB,KAAK,UAAU,CAAC,+BAChC,KAAK,wBAC/B,KAAK,uBACN,CAAC,qFAEsB,0BAA0B;;;;;;AAUxD,SAAgB,UAAU,OAA6B;CACrD,MAAM,mBAAmB,IAAI,iBAAiB;AAE9C,kBAAiB,eACf,+HAGD;AACD,kBAAiB,eACf,0IAGD;AACD,kBAAiB,eAAe,qBAAqB;AACrD,kBAAiB,eACf,6EACD;AAED,MAAK,MAAM,QAAQ,MACjB,kBAAiB,SAAS,KAAK;AAGjC,kBAAiB,aACf,0DACD;AACD,kBAAiB,aAAa,+BAA+B;AAC7D,kBAAiB,aACf,yDACD;AACD,kBAAiB,aAAa,eAAe;AAE7C,kBAAiB,2BACf,2GAED;AACD,kBAAiB,2BACf,sEACD;AACD,kBAAiB,2BACf,oEACD;AACD,kBAAiB,2BACf,2GAED;AAID,QAFsB,iBAAiB,wBAAwB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt_generator.js","names":[],"sources":["../../../src/experimental/autogpt/prompt_generator.ts"],"sourcesContent":["import { isInteropZodSchema } from \"@langchain/core/utils/types\";\nimport {\n type JsonSchema7Type,\n toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport { ObjectTool, FINISH_NAME } from \"./schema.js\";\n\n/**\n * Class that generates prompts for generative agents. It maintains a list\n * of constraints, commands, resources, and performance evaluations.\n */\nexport class PromptGenerator {\n constraints: string[];\n\n commands: ObjectTool[];\n\n resources: string[];\n\n performance_evaluation: string[];\n\n response_format: object;\n\n constructor() {\n this.constraints = [];\n this.commands = [];\n this.resources = [];\n this.performance_evaluation = [];\n this.response_format = {\n thoughts: {\n text: \"thought\",\n reasoning: \"reasoning\",\n plan: \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n criticism: \"constructive self-criticism\",\n speak: \"thoughts summary to say to user\",\n },\n command: { name: \"command name\", args: { \"arg name\": \"value\" } },\n };\n }\n\n /**\n * Adds a constraint to the list of constraints.\n * @param constraint The constraint to add.\n * @returns void\n */\n add_constraint(constraint: string): void {\n this.constraints.push(constraint);\n }\n\n /**\n * Adds a tool to the list of commands.\n * @param tool The tool to add.\n * @returns void\n */\n add_tool(tool: ObjectTool): void {\n this.commands.push(tool);\n }\n\n _generate_command_string(tool: ObjectTool): string {\n let output = `\"${tool.name}\": ${tool.description}`;\n const jsonSchema = (\n isInteropZodSchema(tool.schema) ? toJsonSchema(tool.schema) : tool.schema\n ) as { properties?: Record<string, JsonSchema7Type> } | undefined;\n output += `, args json schema: ${JSON.stringify(jsonSchema?.properties)}`;\n return output;\n }\n\n /**\n * Adds a resource to the list of resources.\n * @param resource The resource to add.\n * @returns void\n */\n add_resource(resource: string): void {\n this.resources.push(resource);\n }\n\n /**\n * Adds a performance evaluation to the list of performance evaluations.\n * @param evaluation The performance evaluation to add.\n * @returns void\n */\n add_performance_evaluation(evaluation: string): void {\n this.performance_evaluation.push(evaluation);\n }\n\n //
|
|
1
|
+
{"version":3,"file":"prompt_generator.js","names":[],"sources":["../../../src/experimental/autogpt/prompt_generator.ts"],"sourcesContent":["import { isInteropZodSchema } from \"@langchain/core/utils/types\";\nimport {\n type JsonSchema7Type,\n toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport { ObjectTool, FINISH_NAME } from \"./schema.js\";\n\n/**\n * Class that generates prompts for generative agents. It maintains a list\n * of constraints, commands, resources, and performance evaluations.\n */\nexport class PromptGenerator {\n constraints: string[];\n\n commands: ObjectTool[];\n\n resources: string[];\n\n performance_evaluation: string[];\n\n response_format: object;\n\n constructor() {\n this.constraints = [];\n this.commands = [];\n this.resources = [];\n this.performance_evaluation = [];\n this.response_format = {\n thoughts: {\n text: \"thought\",\n reasoning: \"reasoning\",\n plan: \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n criticism: \"constructive self-criticism\",\n speak: \"thoughts summary to say to user\",\n },\n command: { name: \"command name\", args: { \"arg name\": \"value\" } },\n };\n }\n\n /**\n * Adds a constraint to the list of constraints.\n * @param constraint The constraint to add.\n * @returns void\n */\n add_constraint(constraint: string): void {\n this.constraints.push(constraint);\n }\n\n /**\n * Adds a tool to the list of commands.\n * @param tool The tool to add.\n * @returns void\n */\n add_tool(tool: ObjectTool): void {\n this.commands.push(tool);\n }\n\n _generate_command_string(tool: ObjectTool): string {\n let output = `\"${tool.name}\": ${tool.description}`;\n const jsonSchema = (\n isInteropZodSchema(tool.schema) ? toJsonSchema(tool.schema) : tool.schema\n ) as { properties?: Record<string, JsonSchema7Type> } | undefined;\n output += `, args json schema: ${JSON.stringify(jsonSchema?.properties)}`;\n return output;\n }\n\n /**\n * Adds a resource to the list of resources.\n * @param resource The resource to add.\n * @returns void\n */\n add_resource(resource: string): void {\n this.resources.push(resource);\n }\n\n /**\n * Adds a performance evaluation to the list of performance evaluations.\n * @param evaluation The performance evaluation to add.\n * @returns void\n */\n add_performance_evaluation(evaluation: string): void {\n this.performance_evaluation.push(evaluation);\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _generate_numbered_list(items: any[], item_type = \"list\"): string {\n if (item_type === \"command\") {\n const command_strings = items.map(\n (item, i) => `${i + 1}. ${this._generate_command_string(item)}`\n );\n const finish_description =\n \"use this to signal that you have finished all your objectives\";\n const finish_args =\n '\"response\": \"final response to let people know you have finished your objectives\"';\n const finish_string = `${\n items.length + 1\n }. ${FINISH_NAME}: ${finish_description}, args: ${finish_args}`;\n return command_strings.concat([finish_string]).join(\"\\n\");\n }\n\n return items.map((item, i) => `${i + 1}. ${item}`).join(\"\\n\");\n }\n\n /**\n * Generates a prompt string that includes the constraints, commands,\n * resources, performance evaluations, and response format.\n * @returns A string representing the prompt.\n */\n generate_prompt_string(): string {\n const formatted_response_format = JSON.stringify(\n this.response_format,\n null,\n 4\n );\n const prompt_string =\n `Constraints:\\n${this._generate_numbered_list(this.constraints)}\\n\\n` +\n `Commands:\\n${this._generate_numbered_list(\n this.commands,\n \"command\"\n )}\\n\\n` +\n `Resources:\\n${this._generate_numbered_list(this.resources)}\\n\\n` +\n `Performance Evaluation:\\n${this._generate_numbered_list(\n this.performance_evaluation\n )}\\n\\n` +\n `You should only respond in JSON format as described below ` +\n `\\nResponse Format: \\n${formatted_response_format} ` +\n `\\nEnsure the response can be parsed by Python json.loads`;\n\n return prompt_string;\n }\n}\n\n/**\n * Function that generates a prompt string for a given list of tools.\n */\nexport function getPrompt(tools: ObjectTool[]): string {\n const prompt_generator = new PromptGenerator();\n\n prompt_generator.add_constraint(\n \"~4000 word limit for short term memory. \" +\n \"Your short term memory is short, \" +\n \"so immediately save important information to files.\"\n );\n prompt_generator.add_constraint(\n \"If you are unsure how you previously did something \" +\n \"or want to recall past events, \" +\n \"thinking about similar events will help you remember.\"\n );\n prompt_generator.add_constraint(\"No user assistance\");\n prompt_generator.add_constraint(\n 'Exclusively use the commands listed in double quotes e.g. \"command name\"'\n );\n\n for (const tool of tools) {\n prompt_generator.add_tool(tool);\n }\n\n prompt_generator.add_resource(\n \"Internet access for searches and information gathering.\"\n );\n prompt_generator.add_resource(\"Long Term memory management.\");\n prompt_generator.add_resource(\n \"GPT-3.5 powered Agents for delegation of simple tasks.\"\n );\n prompt_generator.add_resource(\"File output.\");\n\n prompt_generator.add_performance_evaluation(\n \"Continuously review and analyze your actions \" +\n \"to ensure you are performing to the best of your abilities.\"\n );\n prompt_generator.add_performance_evaluation(\n \"Constructively self-criticize your big-picture behavior constantly.\"\n );\n prompt_generator.add_performance_evaluation(\n \"Reflect on past decisions and strategies to refine your approach.\"\n );\n prompt_generator.add_performance_evaluation(\n \"Every command has a cost, so be smart and efficient. \" +\n \"Aim to complete tasks in the least number of steps.\"\n );\n\n const prompt_string = prompt_generator.generate_prompt_string();\n\n return prompt_string;\n}\n"],"mappings":";;;;;;;;AAWA,IAAa,kBAAb,MAA6B;CAC3B;CAEA;CAEA;CAEA;CAEA;CAEA,cAAc;AACZ,OAAK,cAAc,EAAE;AACrB,OAAK,WAAW,EAAE;AAClB,OAAK,YAAY,EAAE;AACnB,OAAK,yBAAyB,EAAE;AAChC,OAAK,kBAAkB;GACrB,UAAU;IACR,MAAM;IACN,WAAW;IACX,MAAM;IACN,WAAW;IACX,OAAO;IACR;GACD,SAAS;IAAE,MAAM;IAAgB,MAAM,EAAE,YAAY,SAAS;IAAE;GACjE;;;;;;;CAQH,eAAe,YAA0B;AACvC,OAAK,YAAY,KAAK,WAAW;;;;;;;CAQnC,SAAS,MAAwB;AAC/B,OAAK,SAAS,KAAK,KAAK;;CAG1B,yBAAyB,MAA0B;EACjD,IAAI,SAAS,IAAI,KAAK,KAAK,KAAK,KAAK;EACrC,MAAM,aACJ,mBAAmB,KAAK,OAAO,GAAG,aAAa,KAAK,OAAO,GAAG,KAAK;AAErE,YAAU,uBAAuB,KAAK,UAAU,YAAY,WAAW;AACvE,SAAO;;;;;;;CAQT,aAAa,UAAwB;AACnC,OAAK,UAAU,KAAK,SAAS;;;;;;;CAQ/B,2BAA2B,YAA0B;AACnD,OAAK,uBAAuB,KAAK,WAAW;;CAI9C,wBAAwB,OAAc,YAAY,QAAgB;AAChE,MAAI,cAAc,WAAW;GAC3B,MAAM,kBAAkB,MAAM,KAC3B,MAAM,MAAM,GAAG,IAAI,EAAE,IAAI,KAAK,yBAAyB,KAAK,GAC9D;GAKD,MAAM,gBAAgB,GACpB,MAAM,SAAS,EAChB,IAAI,YAAY;AACjB,UAAO,gBAAgB,OAAO,CAAC,cAAc,CAAC,CAAC,KAAK,KAAK;;AAG3D,SAAO,MAAM,KAAK,MAAM,MAAM,GAAG,IAAI,EAAE,IAAI,OAAO,CAAC,KAAK,KAAK;;;;;;;CAQ/D,yBAAiC;EAC/B,MAAM,4BAA4B,KAAK,UACrC,KAAK,iBACL,MACA,EACD;AAeD,SAbE,iBAAiB,KAAK,wBAAwB,KAAK,YAAY,CAAC,iBAClD,KAAK,wBACjB,KAAK,UACL,UACD,CAAC,kBACa,KAAK,wBAAwB,KAAK,UAAU,CAAC,+BAChC,KAAK,wBAC/B,KAAK,uBACN,CAAC,qFAEsB,0BAA0B;;;;;;AAUxD,SAAgB,UAAU,OAA6B;CACrD,MAAM,mBAAmB,IAAI,iBAAiB;AAE9C,kBAAiB,eACf,+HAGD;AACD,kBAAiB,eACf,0IAGD;AACD,kBAAiB,eAAe,qBAAqB;AACrD,kBAAiB,eACf,6EACD;AAED,MAAK,MAAM,QAAQ,MACjB,kBAAiB,SAAS,KAAK;AAGjC,kBAAiB,aACf,0DACD;AACD,kBAAiB,aAAa,+BAA+B;AAC7D,kBAAiB,aACf,yDACD;AACD,kBAAiB,aAAa,eAAe;AAE7C,kBAAiB,2BACf,2GAED;AACD,kBAAiB,2BACf,sEACD;AACD,kBAAiB,2BACf,oEACD;AACD,kBAAiB,2BACf,2GAED;AAID,QAFsB,iBAAiB,wBAAwB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.cjs","names":[],"sources":["../../../src/experimental/autogpt/schema.ts"],"sourcesContent":["import { StructuredTool, ToolInterface } from \"@langchain/core/tools\";\n\n/**\n * Type alias for StructuredTool. It is part of the tools module in\n * LangChain, which includes a variety of tools used for different\n * purposes.\n */\nexport type ObjectTool = StructuredTool | ToolInterface;\n\nexport const FINISH_NAME = \"finish\";\n\n/**\n * Interface that describes an action that can be performed by the AutoGPT\n * model in LangChain. It has a `name` property, which is a string that\n * represents the name of the action, and an `args` property, which is an\n * object that contains the arguments for the action.\n */\nexport interface AutoGPTAction {\n name: string;\n //
|
|
1
|
+
{"version":3,"file":"schema.cjs","names":[],"sources":["../../../src/experimental/autogpt/schema.ts"],"sourcesContent":["import { StructuredTool, ToolInterface } from \"@langchain/core/tools\";\n\n/**\n * Type alias for StructuredTool. It is part of the tools module in\n * LangChain, which includes a variety of tools used for different\n * purposes.\n */\nexport type ObjectTool = StructuredTool | ToolInterface;\n\nexport const FINISH_NAME = \"finish\";\n\n/**\n * Interface that describes an action that can be performed by the AutoGPT\n * model in LangChain. It has a `name` property, which is a string that\n * represents the name of the action, and an `args` property, which is an\n * object that contains the arguments for the action.\n */\nexport interface AutoGPTAction {\n name: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n args: Record<string, any>;\n}\n"],"mappings":";AASA,MAAa,cAAc"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","names":[],"sources":["../../../src/experimental/autogpt/schema.ts"],"sourcesContent":["import { StructuredTool, ToolInterface } from \"@langchain/core/tools\";\n\n/**\n * Type alias for StructuredTool. It is part of the tools module in\n * LangChain, which includes a variety of tools used for different\n * purposes.\n */\nexport type ObjectTool = StructuredTool | ToolInterface;\n\nexport const FINISH_NAME = \"finish\";\n\n/**\n * Interface that describes an action that can be performed by the AutoGPT\n * model in LangChain. It has a `name` property, which is a string that\n * represents the name of the action, and an `args` property, which is an\n * object that contains the arguments for the action.\n */\nexport interface AutoGPTAction {\n name: string;\n //
|
|
1
|
+
{"version":3,"file":"schema.js","names":[],"sources":["../../../src/experimental/autogpt/schema.ts"],"sourcesContent":["import { StructuredTool, ToolInterface } from \"@langchain/core/tools\";\n\n/**\n * Type alias for StructuredTool. It is part of the tools module in\n * LangChain, which includes a variety of tools used for different\n * purposes.\n */\nexport type ObjectTool = StructuredTool | ToolInterface;\n\nexport const FINISH_NAME = \"finish\";\n\n/**\n * Interface that describes an action that can be performed by the AutoGPT\n * model in LangChain. It has a `name` property, which is a string that\n * represents the name of the action, and an `args` property, which is an\n * object that contains the arguments for the action.\n */\nexport interface AutoGPTAction {\n name: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n args: Record<string, any>;\n}\n"],"mappings":";AASA,MAAa,cAAc"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generative_agent_memory.cjs","names":["BaseChain","LLMChain","Document","PromptTemplate","BaseMemory"],"sources":["../../../src/experimental/generative_agents/generative_agent_memory.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { BaseMemory, InputValues, OutputValues } from \"@langchain/core/memory\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { TimeWeightedVectorStoreRetriever } from \"../../retrievers/time_weighted.js\";\nimport { BaseChain } from \"../../chains/base.js\";\nimport { LLMChain } from \"../../chains/llm_chain.js\";\n\nexport type GenerativeAgentMemoryConfig = {\n reflectionThreshold?: number;\n importanceWeight?: number;\n verbose?: boolean;\n maxTokensLimit?: number;\n};\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseChain` class and has methods for adding observations\n * or memories to the agent's memory, scoring the importance of a memory,\n * reflecting on recent events to add synthesized memories, and generating\n * insights on a topic of reflection based on pertinent memories.\n */\nclass GenerativeAgentMemoryChain extends BaseChain {\n static lc_name() {\n return \"GenerativeAgentMemoryChain\";\n }\n\n reflecting = false;\n\n reflectionThreshold?: number;\n\n importanceWeight = 0.15;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n llm: BaseLanguageModelInterface;\n\n verbose = false;\n\n private aggregateImportance = 0.0;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config: Omit<GenerativeAgentMemoryConfig, \"maxTokensLimit\">\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.reflectionThreshold = config.reflectionThreshold;\n this.importanceWeight = config.importanceWeight ?? this.importanceWeight;\n this.verbose = config.verbose ?? this.verbose;\n }\n\n _chainType(): string {\n return \"generative_agent_memory\";\n }\n\n get inputKeys(): string[] {\n return [\"memory_content\", \"now\", \"memory_metadata\"];\n }\n\n get outputKeys(): string[] {\n return [\"output\"];\n }\n\n /**\n * Method that creates a new LLMChain with the given prompt.\n * @param prompt The PromptTemplate to use for the new LLMChain.\n * @returns A new LLMChain instance.\n */\n chain(prompt: PromptTemplate): LLMChain {\n const chain = new LLMChain({\n llm: this.llm,\n prompt,\n verbose: this.verbose,\n outputKey: \"output\",\n });\n return chain;\n }\n\n async _call(values: ChainValues, runManager?: CallbackManagerForChainRun) {\n const { memory_content: memoryContent, now } = values;\n // add an observation or memory to the agent's memory\n const importanceScore = await this.scoreMemoryImportance(\n memoryContent,\n runManager\n );\n this.aggregateImportance += importanceScore;\n const document = new Document({\n pageContent: memoryContent,\n metadata: {\n importance: importanceScore,\n ...values.memory_metadata,\n },\n });\n await this.memoryRetriever.addDocuments([document]);\n // after an agent has processed a certain amount of memories (as measured by aggregate importance),\n // it is time to pause and reflect on recent events to add more synthesized memories to the agent's\n // memory stream.\n if (\n this.reflectionThreshold !== undefined &&\n this.aggregateImportance > this.reflectionThreshold &&\n !this.reflecting\n ) {\n console.log(\"Reflecting on current memories...\");\n this.reflecting = true;\n await this.pauseToReflect(now, runManager);\n this.aggregateImportance = 0.0;\n this.reflecting = false;\n }\n return { output: importanceScore };\n }\n\n /**\n * Method that pauses the agent to reflect on recent events and generate\n * new insights.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for the reflection.\n * @returns An array of new insights as strings.\n */\n async pauseToReflect(\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n if (this.verbose) {\n console.log(\"Pausing to reflect...\");\n }\n const newInsights: string[] = [];\n const topics = await this.getTopicsOfReflection(50, runManager);\n for (const topic of topics) {\n const insights = await this.getInsightsOnTopic(topic, now, runManager);\n for (const insight of insights) {\n // add memory\n await this.call(\n {\n memory_content: insight,\n now,\n memory_metadata: {\n source: \"reflection_insight\",\n },\n },\n runManager?.getChild(\"reflection_insight_memory\")\n );\n }\n newInsights.push(...insights);\n }\n return newInsights;\n }\n\n /**\n * Method that scores the importance of a given memory.\n * @param memoryContent The content of the memory to score.\n * @param runManager The CallbackManagerForChainRun to use for scoring.\n * @returns The importance score of the memory as a number.\n */\n async scoreMemoryImportance(\n memoryContent: string,\n runManager?: CallbackManagerForChainRun\n ): Promise<number> {\n // score the absolute importance of a given memory\n const prompt = PromptTemplate.fromTemplate(\n \"On the scale of 1 to 10, where 1 is purely mundane\" +\n \" (e.g., brushing teeth, making bed) and 10 is\" +\n \" extremely poignant (e.g., a break up, college\" +\n \" acceptance), rate the likely poignancy of the\" +\n \" following piece of memory. Respond with a single integer.\" +\n \"\\nMemory: {memory_content}\" +\n \"\\nRating: \"\n );\n const score = await this.chain(prompt).run(\n memoryContent,\n runManager?.getChild(\"determine_importance\")\n );\n\n const strippedScore = score.trim();\n\n if (this.verbose) {\n console.log(\"Importance score:\", strippedScore);\n }\n const match = strippedScore.match(/^\\D*(\\d+)/);\n if (match) {\n const capturedNumber = parseFloat(match[1]);\n const result = (capturedNumber / 10) * this.importanceWeight;\n return result;\n } else {\n return 0.0;\n }\n }\n\n /**\n * Method that retrieves the topics of reflection based on the last K\n * memories.\n * @param lastK The number of most recent memories to consider for generating topics.\n * @param runManager The CallbackManagerForChainRun to use for retrieving topics.\n * @returns An array of topics of reflection as strings.\n */\n async getTopicsOfReflection(\n lastK: number,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n const prompt = PromptTemplate.fromTemplate(\n \"{observations}\\n\\n\" +\n \"Given only the information above, what are the 3 most salient\" +\n \" high-level questions we can answer about the subjects in\" +\n \" the statements? Provide each question on a new line.\\n\\n\"\n );\n\n const observations = this.memoryRetriever.getMemoryStream().slice(-lastK);\n const observationStr = observations\n .map((o: { pageContent: string }) => o.pageContent)\n .join(\"\\n\");\n const result = await this.chain(prompt).run(\n observationStr,\n runManager?.getChild(\"reflection_topics\")\n );\n return GenerativeAgentMemoryChain.parseList(result);\n }\n\n /**\n * Method that generates insights on a given topic of reflection based on\n * pertinent memories.\n * @param topic The topic of reflection.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for generating insights.\n * @returns An array of insights as strings.\n */\n async getInsightsOnTopic(\n topic: string,\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n // generate insights on a topic of reflection, based on pertinent memories\n const prompt = PromptTemplate.fromTemplate(\n \"Statements about {topic}\\n\" +\n \"{related_statements}\\n\\n\" +\n \"What 5 high-level insights can you infer from the above statements?\" +\n \" (example format: insight (because of 1, 5, 3))\"\n );\n\n const relatedMemories = await this.fetchMemories(topic, now, runManager);\n const relatedStatements: string = relatedMemories\n .map((memory, index) => `${index + 1}. ${memory.pageContent}`)\n .join(\"\\n\");\n const result = await this.chain(prompt).call(\n {\n topic,\n related_statements: relatedStatements,\n },\n runManager?.getChild(\"reflection_insights\")\n );\n return GenerativeAgentMemoryChain.parseList(result.output); // added output\n }\n\n /**\n * Method that parses a newline-separated string into a list of strings.\n * @param text The newline-separated string to parse.\n * @returns An array of strings.\n */\n static parseList(text: string): string[] {\n // parse a newine seperates string into a list of strings\n return text.split(\"\\n\").map((s) => s.trim());\n }\n\n // TODO: Mock \"now\" to simulate different times\n /**\n * Method that fetches memories related to a given observation.\n * @param observation The observation to fetch memories for.\n * @param _now The current date.\n * @param runManager The CallbackManagerForChainRun to use for fetching memories.\n * @returns An array of Document instances representing the fetched memories.\n */\n async fetchMemories(\n observation: string,\n _now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<Document[]> {\n return this.memoryRetriever.invoke(\n observation,\n runManager?.getChild(\"memory_retriever\")\n );\n }\n}\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseMemory` class and has methods for adding a memory,\n * formatting memories, getting memories until a token limit is reached,\n * loading memory variables, saving the context of a model run to memory,\n * and clearing memory contents.\n * @example\n * ```typescript\n * const createNewMemoryRetriever = async () => {\n * const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());\n * const retriever = new TimeWeightedVectorStoreRetriever({\n * vectorStore,\n * otherScoreKeys: [\"importance\"],\n * k: 15,\n * });\n * return retriever;\n * };\n * const tommiesMemory = new GenerativeAgentMemory(\n * llm,\n * await createNewMemoryRetriever(),\n * { reflectionThreshold: 8 },\n * );\n * const summary = await tommiesMemory.getSummary();\n * ```\n */\nexport class GenerativeAgentMemory extends BaseMemory {\n llm: BaseLanguageModelInterface;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n verbose: boolean;\n\n reflectionThreshold?: number;\n\n private maxTokensLimit = 1200;\n\n queriesKey = \"queries\";\n\n mostRecentMemoriesTokenKey = \"recent_memories_token\";\n\n addMemoryKey = \"addMemory\";\n\n relevantMemoriesKey = \"relevant_memories\";\n\n relevantMemoriesSimpleKey = \"relevant_memories_simple\";\n\n mostRecentMemoriesKey = \"most_recent_memories\";\n\n nowKey = \"now\";\n\n memoryChain: GenerativeAgentMemoryChain;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config?: GenerativeAgentMemoryConfig\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.verbose = config?.verbose ?? this.verbose;\n this.reflectionThreshold =\n config?.reflectionThreshold ?? this.reflectionThreshold;\n this.maxTokensLimit = config?.maxTokensLimit ?? this.maxTokensLimit;\n this.memoryChain = new GenerativeAgentMemoryChain(llm, memoryRetriever, {\n reflectionThreshold: config?.reflectionThreshold,\n importanceWeight: config?.importanceWeight,\n });\n }\n\n /**\n * Method that returns the key for relevant memories.\n * @returns The key for relevant memories as a string.\n */\n getRelevantMemoriesKey(): string {\n return this.relevantMemoriesKey;\n }\n\n /**\n * Method that returns the key for the most recent memories token.\n * @returns The key for the most recent memories token as a string.\n */\n getMostRecentMemoriesTokenKey(): string {\n return this.mostRecentMemoriesTokenKey;\n }\n\n /**\n * Method that returns the key for adding a memory.\n * @returns The key for adding a memory as a string.\n */\n getAddMemoryKey(): string {\n return this.addMemoryKey;\n }\n\n /**\n * Method that returns the key for the current time.\n * @returns The key for the current time as a string.\n */\n getCurrentTimeKey(): string {\n return this.nowKey;\n }\n\n get memoryKeys(): string[] {\n // Return an array of memory keys\n return [this.relevantMemoriesKey, this.mostRecentMemoriesKey];\n }\n\n /**\n * Method that adds a memory to the agent's memory.\n * @param memoryContent The content of the memory to add.\n * @param now The current date.\n * @param metadata The metadata for the memory.\n * @param callbacks The Callbacks to use for adding the memory.\n * @returns The result of the memory addition.\n */\n async addMemory(\n memoryContent: string,\n now?: Date,\n metadata?: Record<string, unknown>,\n callbacks?: Callbacks\n ) {\n return this.memoryChain.call(\n { memory_content: memoryContent, now, memory_metadata: metadata },\n callbacks\n );\n }\n\n /**\n * Method that formats the given relevant memories in detail.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesDetail(relevantMemories: Document[]): string {\n if (!relevantMemories.length) {\n return \"No relevant information.\";\n }\n const contentStrings = new Set();\n const content = [];\n for (const memory of relevantMemories) {\n if (memory.pageContent in contentStrings) {\n continue;\n }\n contentStrings.add(memory.pageContent);\n const createdTime = memory.metadata.created_at.toLocaleString(\"en-US\", {\n month: \"long\",\n day: \"numeric\",\n year: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n hour12: true,\n });\n content.push(`${createdTime}: ${memory.pageContent.trim()}`);\n }\n const joinedContent = content.map((mem) => `${mem}`).join(\"\\n\");\n return joinedContent;\n }\n\n /**\n * Method that formats the given relevant memories in a simple manner.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesSimple(relevantMemories: Document[]): string {\n const joinedContent = relevantMemories\n .map((mem) => `${mem.pageContent}`)\n .join(\"; \");\n return joinedContent;\n }\n\n /**\n * Method that retrieves memories until a token limit is reached.\n * @param consumedTokens The number of tokens consumed so far.\n * @returns The memories as a string.\n */\n async getMemoriesUntilLimit(consumedTokens: number): Promise<string> {\n // reduce the number of tokens in the documents\n const result = [];\n for (const doc of this.memoryRetriever\n .getMemoryStream()\n .slice()\n .reverse()) {\n if (consumedTokens >= this.maxTokensLimit) {\n if (this.verbose) {\n console.log(\"Exceeding max tokens for LLM, filtering memories\");\n }\n break;\n }\n // eslint-disable-next-line no-param-reassign\n consumedTokens += await this.llm.getNumTokens(doc.pageContent);\n if (consumedTokens < this.maxTokensLimit) {\n result.push(doc);\n }\n }\n return this.formatMemoriesSimple(result);\n }\n\n get memoryVariables(): string[] {\n // input keys this memory class will load dynamically\n return [];\n }\n\n /**\n * Method that loads memory variables based on the given inputs.\n * @param inputs The inputs to use for loading memory variables.\n * @returns An object containing the loaded memory variables.\n */\n async loadMemoryVariables(\n inputs: InputValues\n ): Promise<Record<string, string>> {\n const queries = inputs[this.queriesKey];\n const now = inputs[this.nowKey];\n if (queries !== undefined) {\n const relevantMemories = (\n await Promise.all(\n queries.map((query: string) =>\n this.memoryChain.fetchMemories(query, now)\n )\n )\n ).flat();\n return {\n [this.relevantMemoriesKey]: this.formatMemoriesDetail(relevantMemories),\n [this.relevantMemoriesSimpleKey]:\n this.formatMemoriesSimple(relevantMemories),\n };\n }\n const mostRecentMemoriesToken = inputs[this.mostRecentMemoriesTokenKey];\n if (mostRecentMemoriesToken !== undefined) {\n return {\n [this.mostRecentMemoriesKey]: await this.getMemoriesUntilLimit(\n mostRecentMemoriesToken\n ),\n };\n }\n return {};\n }\n\n /**\n * Method that saves the context of a model run to memory.\n * @param _inputs The inputs of the model run.\n * @param outputs The outputs of the model run.\n * @returns Nothing.\n */\n async saveContext(\n _inputs: InputValues,\n outputs: OutputValues\n ): Promise<void> {\n // save the context of this model run to memory\n const mem = outputs[this.addMemoryKey];\n const now = outputs[this.nowKey];\n if (mem) {\n await this.addMemory(mem, now, {});\n }\n }\n\n /**\n * Method that clears the memory contents.\n * @returns Nothing.\n */\n clear(): void {\n // TODO: clear memory contents\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA2BA,IAAM,6BAAN,MAAM,mCAAmCA,aAAAA,UAAU;CACjD,OAAO,UAAU;AACf,SAAO;;CAGT,aAAa;CAEb;CAEA,mBAAmB;CAEnB;CAEA;CAEA,UAAU;CAEV,sBAA8B;CAE9B,YACE,KACA,iBACA,QACA;AACA,SAAO;AACP,OAAK,MAAM;AACX,OAAK,kBAAkB;AACvB,OAAK,sBAAsB,OAAO;AAClC,OAAK,mBAAmB,OAAO,oBAAoB,KAAK;AACxD,OAAK,UAAU,OAAO,WAAW,KAAK;;CAGxC,aAAqB;AACnB,SAAO;;CAGT,IAAI,YAAsB;AACxB,SAAO;GAAC;GAAkB;GAAO;GAAkB;;CAGrD,IAAI,aAAuB;AACzB,SAAO,CAAC,SAAS;;;;;;;CAQnB,MAAM,QAAkC;AAOtC,SANc,IAAIC,kBAAAA,SAAS;GACzB,KAAK,KAAK;GACV;GACA,SAAS,KAAK;GACd,WAAW;GACZ,CAAC;;CAIJ,MAAM,MAAM,QAAqB,YAAyC;EACxE,MAAM,EAAE,gBAAgB,eAAe,QAAQ;EAE/C,MAAM,kBAAkB,MAAM,KAAK,sBACjC,eACA,WACD;AACD,OAAK,uBAAuB;EAC5B,MAAM,WAAW,IAAIC,0BAAAA,SAAS;GAC5B,aAAa;GACb,UAAU;IACR,YAAY;IACZ,GAAG,OAAO;IACX;GACF,CAAC;AACF,QAAM,KAAK,gBAAgB,aAAa,CAAC,SAAS,CAAC;AAInD,MACE,KAAK,wBAAwB,KAAA,KAC7B,KAAK,sBAAsB,KAAK,uBAChC,CAAC,KAAK,YACN;AACA,WAAQ,IAAI,oCAAoC;AAChD,QAAK,aAAa;AAClB,SAAM,KAAK,eAAe,KAAK,WAAW;AAC1C,QAAK,sBA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AI,YAAY,KAAA,GAAW;GACzB,MAAM,oBACJ,MAAM,QAAQ,IACZ,QAAQ,KAAK,UACX,KAAK,YAAY,cAAc,OAAO,IAAI,CAC3C,CACF,EACD,MAAM;AACR,UAAO;KACJ,KAAK,sBAAsB,KAAK,qBAAqB,iBAAiB;KACtE,KAAK,4BACJ,KAAK,qBAAqB,iBAAiB;IAC9C;;EAEH,MAAM,0BAA0B,OAAO,KAAK;AAC5C,MAAI,4BAA4B,KAAA,EAC9B,QAAO,GACJ,KAAK,wBAAwB,MAAM,KAAK,sBACvC,wBACD,EACF;AAEH,SAAO,EAAE;;;;;;;;CASX,MAAM,YACJ,SACA,SACe;EAEf,MAAM,MAAM,QAAQ,KAAK;EACzB,MAAM,MAAM,QAAQ,KAAK;AACzB,MAAI,IACF,OAAM,KAAK,UAAU,KAAK,KAAK,EAAE,CAAC;;;;;;CAQtC,QAAc"}
|
|
1
|
+
{"version":3,"file":"generative_agent_memory.cjs","names":["BaseChain","LLMChain","Document","PromptTemplate","BaseMemory"],"sources":["../../../src/experimental/generative_agents/generative_agent_memory.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { BaseMemory, InputValues, OutputValues } from \"@langchain/core/memory\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { TimeWeightedVectorStoreRetriever } from \"../../retrievers/time_weighted.js\";\nimport { BaseChain } from \"../../chains/base.js\";\nimport { LLMChain } from \"../../chains/llm_chain.js\";\n\nexport type GenerativeAgentMemoryConfig = {\n reflectionThreshold?: number;\n importanceWeight?: number;\n verbose?: boolean;\n maxTokensLimit?: number;\n};\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseChain` class and has methods for adding observations\n * or memories to the agent's memory, scoring the importance of a memory,\n * reflecting on recent events to add synthesized memories, and generating\n * insights on a topic of reflection based on pertinent memories.\n */\nclass GenerativeAgentMemoryChain extends BaseChain {\n static lc_name() {\n return \"GenerativeAgentMemoryChain\";\n }\n\n reflecting = false;\n\n reflectionThreshold?: number;\n\n importanceWeight = 0.15;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n llm: BaseLanguageModelInterface;\n\n verbose = false;\n\n private aggregateImportance = 0.0;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config: Omit<GenerativeAgentMemoryConfig, \"maxTokensLimit\">\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.reflectionThreshold = config.reflectionThreshold;\n this.importanceWeight = config.importanceWeight ?? this.importanceWeight;\n this.verbose = config.verbose ?? this.verbose;\n }\n\n _chainType(): string {\n return \"generative_agent_memory\";\n }\n\n get inputKeys(): string[] {\n return [\"memory_content\", \"now\", \"memory_metadata\"];\n }\n\n get outputKeys(): string[] {\n return [\"output\"];\n }\n\n /**\n * Method that creates a new LLMChain with the given prompt.\n * @param prompt The PromptTemplate to use for the new LLMChain.\n * @returns A new LLMChain instance.\n */\n chain(prompt: PromptTemplate): LLMChain {\n const chain = new LLMChain({\n llm: this.llm,\n prompt,\n verbose: this.verbose,\n outputKey: \"output\",\n });\n return chain;\n }\n\n async _call(values: ChainValues, runManager?: CallbackManagerForChainRun) {\n const { memory_content: memoryContent, now } = values;\n // add an observation or memory to the agent's memory\n const importanceScore = await this.scoreMemoryImportance(\n memoryContent,\n runManager\n );\n this.aggregateImportance += importanceScore;\n const document = new Document({\n pageContent: memoryContent,\n metadata: {\n importance: importanceScore,\n ...values.memory_metadata,\n },\n });\n await this.memoryRetriever.addDocuments([document]);\n // after an agent has processed a certain amount of memories (as measured by aggregate importance),\n // it is time to pause and reflect on recent events to add more synthesized memories to the agent's\n // memory stream.\n if (\n this.reflectionThreshold !== undefined &&\n this.aggregateImportance > this.reflectionThreshold &&\n !this.reflecting\n ) {\n console.log(\"Reflecting on current memories...\");\n this.reflecting = true;\n await this.pauseToReflect(now, runManager);\n this.aggregateImportance = 0.0;\n this.reflecting = false;\n }\n return { output: importanceScore };\n }\n\n /**\n * Method that pauses the agent to reflect on recent events and generate\n * new insights.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for the reflection.\n * @returns An array of new insights as strings.\n */\n async pauseToReflect(\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n if (this.verbose) {\n console.log(\"Pausing to reflect...\");\n }\n const newInsights: string[] = [];\n const topics = await this.getTopicsOfReflection(50, runManager);\n for (const topic of topics) {\n const insights = await this.getInsightsOnTopic(topic, now, runManager);\n for (const insight of insights) {\n // add memory\n await this.call(\n {\n memory_content: insight,\n now,\n memory_metadata: {\n source: \"reflection_insight\",\n },\n },\n runManager?.getChild(\"reflection_insight_memory\")\n );\n }\n newInsights.push(...insights);\n }\n return newInsights;\n }\n\n /**\n * Method that scores the importance of a given memory.\n * @param memoryContent The content of the memory to score.\n * @param runManager The CallbackManagerForChainRun to use for scoring.\n * @returns The importance score of the memory as a number.\n */\n async scoreMemoryImportance(\n memoryContent: string,\n runManager?: CallbackManagerForChainRun\n ): Promise<number> {\n // score the absolute importance of a given memory\n const prompt = PromptTemplate.fromTemplate(\n \"On the scale of 1 to 10, where 1 is purely mundane\" +\n \" (e.g., brushing teeth, making bed) and 10 is\" +\n \" extremely poignant (e.g., a break up, college\" +\n \" acceptance), rate the likely poignancy of the\" +\n \" following piece of memory. Respond with a single integer.\" +\n \"\\nMemory: {memory_content}\" +\n \"\\nRating: \"\n );\n const score = await this.chain(prompt).run(\n memoryContent,\n runManager?.getChild(\"determine_importance\")\n );\n\n const strippedScore = score.trim();\n\n if (this.verbose) {\n console.log(\"Importance score:\", strippedScore);\n }\n const match = strippedScore.match(/^\\D*(\\d+)/);\n if (match) {\n const capturedNumber = parseFloat(match[1]);\n const result = (capturedNumber / 10) * this.importanceWeight;\n return result;\n } else {\n return 0.0;\n }\n }\n\n /**\n * Method that retrieves the topics of reflection based on the last K\n * memories.\n * @param lastK The number of most recent memories to consider for generating topics.\n * @param runManager The CallbackManagerForChainRun to use for retrieving topics.\n * @returns An array of topics of reflection as strings.\n */\n async getTopicsOfReflection(\n lastK: number,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n const prompt = PromptTemplate.fromTemplate(\n \"{observations}\\n\\n\" +\n \"Given only the information above, what are the 3 most salient\" +\n \" high-level questions we can answer about the subjects in\" +\n \" the statements? Provide each question on a new line.\\n\\n\"\n );\n\n const observations = this.memoryRetriever.getMemoryStream().slice(-lastK);\n const observationStr = observations\n .map((o: { pageContent: string }) => o.pageContent)\n .join(\"\\n\");\n const result = await this.chain(prompt).run(\n observationStr,\n runManager?.getChild(\"reflection_topics\")\n );\n return GenerativeAgentMemoryChain.parseList(result);\n }\n\n /**\n * Method that generates insights on a given topic of reflection based on\n * pertinent memories.\n * @param topic The topic of reflection.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for generating insights.\n * @returns An array of insights as strings.\n */\n async getInsightsOnTopic(\n topic: string,\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n // generate insights on a topic of reflection, based on pertinent memories\n const prompt = PromptTemplate.fromTemplate(\n \"Statements about {topic}\\n\" +\n \"{related_statements}\\n\\n\" +\n \"What 5 high-level insights can you infer from the above statements?\" +\n \" (example format: insight (because of 1, 5, 3))\"\n );\n\n const relatedMemories = await this.fetchMemories(topic, now, runManager);\n const relatedStatements: string = relatedMemories\n .map((memory, index) => `${index + 1}. ${memory.pageContent}`)\n .join(\"\\n\");\n const result = await this.chain(prompt).call(\n {\n topic,\n related_statements: relatedStatements,\n },\n runManager?.getChild(\"reflection_insights\")\n );\n return GenerativeAgentMemoryChain.parseList(result.output); // added output\n }\n\n /**\n * Method that parses a newline-separated string into a list of strings.\n * @param text The newline-separated string to parse.\n * @returns An array of strings.\n */\n static parseList(text: string): string[] {\n // parse a newine seperates string into a list of strings\n return text.split(\"\\n\").map((s) => s.trim());\n }\n\n // TODO: Mock \"now\" to simulate different times\n /**\n * Method that fetches memories related to a given observation.\n * @param observation The observation to fetch memories for.\n * @param _now The current date.\n * @param runManager The CallbackManagerForChainRun to use for fetching memories.\n * @returns An array of Document instances representing the fetched memories.\n */\n async fetchMemories(\n observation: string,\n _now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<Document[]> {\n return this.memoryRetriever.invoke(\n observation,\n runManager?.getChild(\"memory_retriever\")\n );\n }\n}\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseMemory` class and has methods for adding a memory,\n * formatting memories, getting memories until a token limit is reached,\n * loading memory variables, saving the context of a model run to memory,\n * and clearing memory contents.\n * @example\n * ```typescript\n * const createNewMemoryRetriever = async () => {\n * const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());\n * const retriever = new TimeWeightedVectorStoreRetriever({\n * vectorStore,\n * otherScoreKeys: [\"importance\"],\n * k: 15,\n * });\n * return retriever;\n * };\n * const tommiesMemory = new GenerativeAgentMemory(\n * llm,\n * await createNewMemoryRetriever(),\n * { reflectionThreshold: 8 },\n * );\n * const summary = await tommiesMemory.getSummary();\n * ```\n */\nexport class GenerativeAgentMemory extends BaseMemory {\n llm: BaseLanguageModelInterface;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n verbose: boolean;\n\n reflectionThreshold?: number;\n\n private maxTokensLimit = 1200;\n\n queriesKey = \"queries\";\n\n mostRecentMemoriesTokenKey = \"recent_memories_token\";\n\n addMemoryKey = \"addMemory\";\n\n relevantMemoriesKey = \"relevant_memories\";\n\n relevantMemoriesSimpleKey = \"relevant_memories_simple\";\n\n mostRecentMemoriesKey = \"most_recent_memories\";\n\n nowKey = \"now\";\n\n memoryChain: GenerativeAgentMemoryChain;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config?: GenerativeAgentMemoryConfig\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.verbose = config?.verbose ?? this.verbose;\n this.reflectionThreshold =\n config?.reflectionThreshold ?? this.reflectionThreshold;\n this.maxTokensLimit = config?.maxTokensLimit ?? this.maxTokensLimit;\n this.memoryChain = new GenerativeAgentMemoryChain(llm, memoryRetriever, {\n reflectionThreshold: config?.reflectionThreshold,\n importanceWeight: config?.importanceWeight,\n });\n }\n\n /**\n * Method that returns the key for relevant memories.\n * @returns The key for relevant memories as a string.\n */\n getRelevantMemoriesKey(): string {\n return this.relevantMemoriesKey;\n }\n\n /**\n * Method that returns the key for the most recent memories token.\n * @returns The key for the most recent memories token as a string.\n */\n getMostRecentMemoriesTokenKey(): string {\n return this.mostRecentMemoriesTokenKey;\n }\n\n /**\n * Method that returns the key for adding a memory.\n * @returns The key for adding a memory as a string.\n */\n getAddMemoryKey(): string {\n return this.addMemoryKey;\n }\n\n /**\n * Method that returns the key for the current time.\n * @returns The key for the current time as a string.\n */\n getCurrentTimeKey(): string {\n return this.nowKey;\n }\n\n get memoryKeys(): string[] {\n // Return an array of memory keys\n return [this.relevantMemoriesKey, this.mostRecentMemoriesKey];\n }\n\n /**\n * Method that adds a memory to the agent's memory.\n * @param memoryContent The content of the memory to add.\n * @param now The current date.\n * @param metadata The metadata for the memory.\n * @param callbacks The Callbacks to use for adding the memory.\n * @returns The result of the memory addition.\n */\n async addMemory(\n memoryContent: string,\n now?: Date,\n metadata?: Record<string, unknown>,\n callbacks?: Callbacks\n ) {\n return this.memoryChain.call(\n { memory_content: memoryContent, now, memory_metadata: metadata },\n callbacks\n );\n }\n\n /**\n * Method that formats the given relevant memories in detail.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesDetail(relevantMemories: Document[]): string {\n if (!relevantMemories.length) {\n return \"No relevant information.\";\n }\n const contentStrings = new Set();\n const content = [];\n for (const memory of relevantMemories) {\n if (memory.pageContent in contentStrings) {\n continue;\n }\n contentStrings.add(memory.pageContent);\n const createdTime = memory.metadata.created_at.toLocaleString(\"en-US\", {\n month: \"long\",\n day: \"numeric\",\n year: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n hour12: true,\n });\n content.push(`${createdTime}: ${memory.pageContent.trim()}`);\n }\n const joinedContent = content.map((mem) => `${mem}`).join(\"\\n\");\n return joinedContent;\n }\n\n /**\n * Method that formats the given relevant memories in a simple manner.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesSimple(relevantMemories: Document[]): string {\n const joinedContent = relevantMemories\n .map((mem) => `${mem.pageContent}`)\n .join(\"; \");\n return joinedContent;\n }\n\n /**\n * Method that retrieves memories until a token limit is reached.\n * @param consumedTokens The number of tokens consumed so far.\n * @returns The memories as a string.\n */\n async getMemoriesUntilLimit(consumedTokens: number): Promise<string> {\n // reduce the number of tokens in the documents\n const result = [];\n for (const doc of this.memoryRetriever\n .getMemoryStream()\n .slice()\n .reverse()) {\n if (consumedTokens >= this.maxTokensLimit) {\n if (this.verbose) {\n console.log(\"Exceeding max tokens for LLM, filtering memories\");\n }\n break;\n }\n // oxlint-disable-next-line no-param-reassign\n consumedTokens += await this.llm.getNumTokens(doc.pageContent);\n if (consumedTokens < this.maxTokensLimit) {\n result.push(doc);\n }\n }\n return this.formatMemoriesSimple(result);\n }\n\n get memoryVariables(): string[] {\n // input keys this memory class will load dynamically\n return [];\n }\n\n /**\n * Method that loads memory variables based on the given inputs.\n * @param inputs The inputs to use for loading memory variables.\n * @returns An object containing the loaded memory variables.\n */\n async loadMemoryVariables(\n inputs: InputValues\n ): Promise<Record<string, string>> {\n const queries = inputs[this.queriesKey];\n const now = inputs[this.nowKey];\n if (queries !== undefined) {\n const relevantMemories = (\n await Promise.all(\n queries.map((query: string) =>\n this.memoryChain.fetchMemories(query, now)\n )\n )\n ).flat();\n return {\n [this.relevantMemoriesKey]: this.formatMemoriesDetail(relevantMemories),\n [this.relevantMemoriesSimpleKey]:\n this.formatMemoriesSimple(relevantMemories),\n };\n }\n const mostRecentMemoriesToken = inputs[this.mostRecentMemoriesTokenKey];\n if (mostRecentMemoriesToken !== undefined) {\n return {\n [this.mostRecentMemoriesKey]: await this.getMemoriesUntilLimit(\n mostRecentMemoriesToken\n ),\n };\n }\n return {};\n }\n\n /**\n * Method that saves the context of a model run to memory.\n * @param _inputs The inputs of the model run.\n * @param outputs The outputs of the model run.\n * @returns Nothing.\n */\n async saveContext(\n _inputs: InputValues,\n outputs: OutputValues\n ): Promise<void> {\n // save the context of this model run to memory\n const mem = outputs[this.addMemoryKey];\n const now = outputs[this.nowKey];\n if (mem) {\n await this.addMemory(mem, now, {});\n }\n }\n\n /**\n * Method that clears the memory contents.\n * @returns Nothing.\n */\n clear(): void {\n // TODO: clear memory contents\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA2BA,IAAM,6BAAN,MAAM,mCAAmCA,aAAAA,UAAU;CACjD,OAAO,UAAU;AACf,SAAO;;CAGT,aAAa;CAEb;CAEA,mBAAmB;CAEnB;CAEA;CAEA,UAAU;CAEV,sBAA8B;CAE9B,YACE,KACA,iBACA,QACA;AACA,SAAO;AACP,OAAK,MAAM;AACX,OAAK,kBAAkB;AACvB,OAAK,sBAAsB,OAAO;AAClC,OAAK,mBAAmB,OAAO,oBAAoB,KAAK;AACxD,OAAK,UAAU,OAAO,WAAW,KAAK;;CAGxC,aAAqB;AACnB,SAAO;;CAGT,IAAI,YAAsB;AACxB,SAAO;GAAC;GAAkB;GAAO;GAAkB;;CAGrD,IAAI,aAAuB;AACzB,SAAO,CAAC,SAAS;;;;;;;CAQnB,MAAM,QAAkC;AAOtC,SANc,IAAIC,kBAAAA,SAAS;GACzB,KAAK,KAAK;GACV;GACA,SAAS,KAAK;GACd,WAAW;GACZ,CAAC;;CAIJ,MAAM,MAAM,QAAqB,YAAyC;EACxE,MAAM,EAAE,gBAAgB,eAAe,QAAQ;EAE/C,MAAM,kBAAkB,MAAM,KAAK,sBACjC,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ACZ,OAAI,kBAAkB,KAAK,gBAAgB;AACzC,QAAI,KAAK,QACP,SAAQ,IAAI,mDAAmD;AAEjE;;AAGF,qBAAkB,MAAM,KAAK,IAAI,aAAa,IAAI,YAAY;AAC9D,OAAI,iBAAiB,KAAK,eACxB,QAAO,KAAK,IAAI;;AAGpB,SAAO,KAAK,qBAAqB,OAAO;;CAG1C,IAAI,kBAA4B;AAE9B,SAAO,EAAE;;;;;;;CAQX,MAAM,oBACJ,QACiC;EACjC,MAAM,UAAU,OAAO,KAAK;EAC5B,MAAM,MAAM,OAAO,KAAK;AACxB,MAAI,YAAY,KAAA,GAAW;GACzB,MAAM,oBACJ,MAAM,QAAQ,IACZ,QAAQ,KAAK,UACX,KAAK,YAAY,cAAc,OAAO,IAAI,CAC3C,CACF,EACD,MAAM;AACR,UAAO;KACJ,KAAK,sBAAsB,KAAK,qBAAqB,iBAAiB;KACtE,KAAK,4BACJ,KAAK,qBAAqB,iBAAiB;IAC9C;;EAEH,MAAM,0BAA0B,OAAO,KAAK;AAC5C,MAAI,4BAA4B,KAAA,EAC9B,QAAO,GACJ,KAAK,wBAAwB,MAAM,KAAK,sBACvC,wBACD,EACF;AAEH,SAAO,EAAE;;;;;;;;CASX,MAAM,YACJ,SACA,SACe;EAEf,MAAM,MAAM,QAAQ,KAAK;EACzB,MAAM,MAAM,QAAQ,KAAK;AACzB,MAAI,IACF,OAAM,KAAK,UAAU,KAAK,KAAK,EAAE,CAAC;;;;;;CAQtC,QAAc"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generative_agent_memory.js","names":[],"sources":["../../../src/experimental/generative_agents/generative_agent_memory.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { BaseMemory, InputValues, OutputValues } from \"@langchain/core/memory\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { TimeWeightedVectorStoreRetriever } from \"../../retrievers/time_weighted.js\";\nimport { BaseChain } from \"../../chains/base.js\";\nimport { LLMChain } from \"../../chains/llm_chain.js\";\n\nexport type GenerativeAgentMemoryConfig = {\n reflectionThreshold?: number;\n importanceWeight?: number;\n verbose?: boolean;\n maxTokensLimit?: number;\n};\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseChain` class and has methods for adding observations\n * or memories to the agent's memory, scoring the importance of a memory,\n * reflecting on recent events to add synthesized memories, and generating\n * insights on a topic of reflection based on pertinent memories.\n */\nclass GenerativeAgentMemoryChain extends BaseChain {\n static lc_name() {\n return \"GenerativeAgentMemoryChain\";\n }\n\n reflecting = false;\n\n reflectionThreshold?: number;\n\n importanceWeight = 0.15;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n llm: BaseLanguageModelInterface;\n\n verbose = false;\n\n private aggregateImportance = 0.0;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config: Omit<GenerativeAgentMemoryConfig, \"maxTokensLimit\">\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.reflectionThreshold = config.reflectionThreshold;\n this.importanceWeight = config.importanceWeight ?? this.importanceWeight;\n this.verbose = config.verbose ?? this.verbose;\n }\n\n _chainType(): string {\n return \"generative_agent_memory\";\n }\n\n get inputKeys(): string[] {\n return [\"memory_content\", \"now\", \"memory_metadata\"];\n }\n\n get outputKeys(): string[] {\n return [\"output\"];\n }\n\n /**\n * Method that creates a new LLMChain with the given prompt.\n * @param prompt The PromptTemplate to use for the new LLMChain.\n * @returns A new LLMChain instance.\n */\n chain(prompt: PromptTemplate): LLMChain {\n const chain = new LLMChain({\n llm: this.llm,\n prompt,\n verbose: this.verbose,\n outputKey: \"output\",\n });\n return chain;\n }\n\n async _call(values: ChainValues, runManager?: CallbackManagerForChainRun) {\n const { memory_content: memoryContent, now } = values;\n // add an observation or memory to the agent's memory\n const importanceScore = await this.scoreMemoryImportance(\n memoryContent,\n runManager\n );\n this.aggregateImportance += importanceScore;\n const document = new Document({\n pageContent: memoryContent,\n metadata: {\n importance: importanceScore,\n ...values.memory_metadata,\n },\n });\n await this.memoryRetriever.addDocuments([document]);\n // after an agent has processed a certain amount of memories (as measured by aggregate importance),\n // it is time to pause and reflect on recent events to add more synthesized memories to the agent's\n // memory stream.\n if (\n this.reflectionThreshold !== undefined &&\n this.aggregateImportance > this.reflectionThreshold &&\n !this.reflecting\n ) {\n console.log(\"Reflecting on current memories...\");\n this.reflecting = true;\n await this.pauseToReflect(now, runManager);\n this.aggregateImportance = 0.0;\n this.reflecting = false;\n }\n return { output: importanceScore };\n }\n\n /**\n * Method that pauses the agent to reflect on recent events and generate\n * new insights.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for the reflection.\n * @returns An array of new insights as strings.\n */\n async pauseToReflect(\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n if (this.verbose) {\n console.log(\"Pausing to reflect...\");\n }\n const newInsights: string[] = [];\n const topics = await this.getTopicsOfReflection(50, runManager);\n for (const topic of topics) {\n const insights = await this.getInsightsOnTopic(topic, now, runManager);\n for (const insight of insights) {\n // add memory\n await this.call(\n {\n memory_content: insight,\n now,\n memory_metadata: {\n source: \"reflection_insight\",\n },\n },\n runManager?.getChild(\"reflection_insight_memory\")\n );\n }\n newInsights.push(...insights);\n }\n return newInsights;\n }\n\n /**\n * Method that scores the importance of a given memory.\n * @param memoryContent The content of the memory to score.\n * @param runManager The CallbackManagerForChainRun to use for scoring.\n * @returns The importance score of the memory as a number.\n */\n async scoreMemoryImportance(\n memoryContent: string,\n runManager?: CallbackManagerForChainRun\n ): Promise<number> {\n // score the absolute importance of a given memory\n const prompt = PromptTemplate.fromTemplate(\n \"On the scale of 1 to 10, where 1 is purely mundane\" +\n \" (e.g., brushing teeth, making bed) and 10 is\" +\n \" extremely poignant (e.g., a break up, college\" +\n \" acceptance), rate the likely poignancy of the\" +\n \" following piece of memory. Respond with a single integer.\" +\n \"\\nMemory: {memory_content}\" +\n \"\\nRating: \"\n );\n const score = await this.chain(prompt).run(\n memoryContent,\n runManager?.getChild(\"determine_importance\")\n );\n\n const strippedScore = score.trim();\n\n if (this.verbose) {\n console.log(\"Importance score:\", strippedScore);\n }\n const match = strippedScore.match(/^\\D*(\\d+)/);\n if (match) {\n const capturedNumber = parseFloat(match[1]);\n const result = (capturedNumber / 10) * this.importanceWeight;\n return result;\n } else {\n return 0.0;\n }\n }\n\n /**\n * Method that retrieves the topics of reflection based on the last K\n * memories.\n * @param lastK The number of most recent memories to consider for generating topics.\n * @param runManager The CallbackManagerForChainRun to use for retrieving topics.\n * @returns An array of topics of reflection as strings.\n */\n async getTopicsOfReflection(\n lastK: number,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n const prompt = PromptTemplate.fromTemplate(\n \"{observations}\\n\\n\" +\n \"Given only the information above, what are the 3 most salient\" +\n \" high-level questions we can answer about the subjects in\" +\n \" the statements? Provide each question on a new line.\\n\\n\"\n );\n\n const observations = this.memoryRetriever.getMemoryStream().slice(-lastK);\n const observationStr = observations\n .map((o: { pageContent: string }) => o.pageContent)\n .join(\"\\n\");\n const result = await this.chain(prompt).run(\n observationStr,\n runManager?.getChild(\"reflection_topics\")\n );\n return GenerativeAgentMemoryChain.parseList(result);\n }\n\n /**\n * Method that generates insights on a given topic of reflection based on\n * pertinent memories.\n * @param topic The topic of reflection.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for generating insights.\n * @returns An array of insights as strings.\n */\n async getInsightsOnTopic(\n topic: string,\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n // generate insights on a topic of reflection, based on pertinent memories\n const prompt = PromptTemplate.fromTemplate(\n \"Statements about {topic}\\n\" +\n \"{related_statements}\\n\\n\" +\n \"What 5 high-level insights can you infer from the above statements?\" +\n \" (example format: insight (because of 1, 5, 3))\"\n );\n\n const relatedMemories = await this.fetchMemories(topic, now, runManager);\n const relatedStatements: string = relatedMemories\n .map((memory, index) => `${index + 1}. ${memory.pageContent}`)\n .join(\"\\n\");\n const result = await this.chain(prompt).call(\n {\n topic,\n related_statements: relatedStatements,\n },\n runManager?.getChild(\"reflection_insights\")\n );\n return GenerativeAgentMemoryChain.parseList(result.output); // added output\n }\n\n /**\n * Method that parses a newline-separated string into a list of strings.\n * @param text The newline-separated string to parse.\n * @returns An array of strings.\n */\n static parseList(text: string): string[] {\n // parse a newine seperates string into a list of strings\n return text.split(\"\\n\").map((s) => s.trim());\n }\n\n // TODO: Mock \"now\" to simulate different times\n /**\n * Method that fetches memories related to a given observation.\n * @param observation The observation to fetch memories for.\n * @param _now The current date.\n * @param runManager The CallbackManagerForChainRun to use for fetching memories.\n * @returns An array of Document instances representing the fetched memories.\n */\n async fetchMemories(\n observation: string,\n _now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<Document[]> {\n return this.memoryRetriever.invoke(\n observation,\n runManager?.getChild(\"memory_retriever\")\n );\n }\n}\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseMemory` class and has methods for adding a memory,\n * formatting memories, getting memories until a token limit is reached,\n * loading memory variables, saving the context of a model run to memory,\n * and clearing memory contents.\n * @example\n * ```typescript\n * const createNewMemoryRetriever = async () => {\n * const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());\n * const retriever = new TimeWeightedVectorStoreRetriever({\n * vectorStore,\n * otherScoreKeys: [\"importance\"],\n * k: 15,\n * });\n * return retriever;\n * };\n * const tommiesMemory = new GenerativeAgentMemory(\n * llm,\n * await createNewMemoryRetriever(),\n * { reflectionThreshold: 8 },\n * );\n * const summary = await tommiesMemory.getSummary();\n * ```\n */\nexport class GenerativeAgentMemory extends BaseMemory {\n llm: BaseLanguageModelInterface;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n verbose: boolean;\n\n reflectionThreshold?: number;\n\n private maxTokensLimit = 1200;\n\n queriesKey = \"queries\";\n\n mostRecentMemoriesTokenKey = \"recent_memories_token\";\n\n addMemoryKey = \"addMemory\";\n\n relevantMemoriesKey = \"relevant_memories\";\n\n relevantMemoriesSimpleKey = \"relevant_memories_simple\";\n\n mostRecentMemoriesKey = \"most_recent_memories\";\n\n nowKey = \"now\";\n\n memoryChain: GenerativeAgentMemoryChain;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config?: GenerativeAgentMemoryConfig\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.verbose = config?.verbose ?? this.verbose;\n this.reflectionThreshold =\n config?.reflectionThreshold ?? this.reflectionThreshold;\n this.maxTokensLimit = config?.maxTokensLimit ?? this.maxTokensLimit;\n this.memoryChain = new GenerativeAgentMemoryChain(llm, memoryRetriever, {\n reflectionThreshold: config?.reflectionThreshold,\n importanceWeight: config?.importanceWeight,\n });\n }\n\n /**\n * Method that returns the key for relevant memories.\n * @returns The key for relevant memories as a string.\n */\n getRelevantMemoriesKey(): string {\n return this.relevantMemoriesKey;\n }\n\n /**\n * Method that returns the key for the most recent memories token.\n * @returns The key for the most recent memories token as a string.\n */\n getMostRecentMemoriesTokenKey(): string {\n return this.mostRecentMemoriesTokenKey;\n }\n\n /**\n * Method that returns the key for adding a memory.\n * @returns The key for adding a memory as a string.\n */\n getAddMemoryKey(): string {\n return this.addMemoryKey;\n }\n\n /**\n * Method that returns the key for the current time.\n * @returns The key for the current time as a string.\n */\n getCurrentTimeKey(): string {\n return this.nowKey;\n }\n\n get memoryKeys(): string[] {\n // Return an array of memory keys\n return [this.relevantMemoriesKey, this.mostRecentMemoriesKey];\n }\n\n /**\n * Method that adds a memory to the agent's memory.\n * @param memoryContent The content of the memory to add.\n * @param now The current date.\n * @param metadata The metadata for the memory.\n * @param callbacks The Callbacks to use for adding the memory.\n * @returns The result of the memory addition.\n */\n async addMemory(\n memoryContent: string,\n now?: Date,\n metadata?: Record<string, unknown>,\n callbacks?: Callbacks\n ) {\n return this.memoryChain.call(\n { memory_content: memoryContent, now, memory_metadata: metadata },\n callbacks\n );\n }\n\n /**\n * Method that formats the given relevant memories in detail.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesDetail(relevantMemories: Document[]): string {\n if (!relevantMemories.length) {\n return \"No relevant information.\";\n }\n const contentStrings = new Set();\n const content = [];\n for (const memory of relevantMemories) {\n if (memory.pageContent in contentStrings) {\n continue;\n }\n contentStrings.add(memory.pageContent);\n const createdTime = memory.metadata.created_at.toLocaleString(\"en-US\", {\n month: \"long\",\n day: \"numeric\",\n year: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n hour12: true,\n });\n content.push(`${createdTime}: ${memory.pageContent.trim()}`);\n }\n const joinedContent = content.map((mem) => `${mem}`).join(\"\\n\");\n return joinedContent;\n }\n\n /**\n * Method that formats the given relevant memories in a simple manner.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesSimple(relevantMemories: Document[]): string {\n const joinedContent = relevantMemories\n .map((mem) => `${mem.pageContent}`)\n .join(\"; \");\n return joinedContent;\n }\n\n /**\n * Method that retrieves memories until a token limit is reached.\n * @param consumedTokens The number of tokens consumed so far.\n * @returns The memories as a string.\n */\n async getMemoriesUntilLimit(consumedTokens: number): Promise<string> {\n // reduce the number of tokens in the documents\n const result = [];\n for (const doc of this.memoryRetriever\n .getMemoryStream()\n .slice()\n .reverse()) {\n if (consumedTokens >= this.maxTokensLimit) {\n if (this.verbose) {\n console.log(\"Exceeding max tokens for LLM, filtering memories\");\n }\n break;\n }\n // eslint-disable-next-line no-param-reassign\n consumedTokens += await this.llm.getNumTokens(doc.pageContent);\n if (consumedTokens < this.maxTokensLimit) {\n result.push(doc);\n }\n }\n return this.formatMemoriesSimple(result);\n }\n\n get memoryVariables(): string[] {\n // input keys this memory class will load dynamically\n return [];\n }\n\n /**\n * Method that loads memory variables based on the given inputs.\n * @param inputs The inputs to use for loading memory variables.\n * @returns An object containing the loaded memory variables.\n */\n async loadMemoryVariables(\n inputs: InputValues\n ): Promise<Record<string, string>> {\n const queries = inputs[this.queriesKey];\n const now = inputs[this.nowKey];\n if (queries !== undefined) {\n const relevantMemories = (\n await Promise.all(\n queries.map((query: string) =>\n this.memoryChain.fetchMemories(query, now)\n )\n )\n ).flat();\n return {\n [this.relevantMemoriesKey]: this.formatMemoriesDetail(relevantMemories),\n [this.relevantMemoriesSimpleKey]:\n this.formatMemoriesSimple(relevantMemories),\n };\n }\n const mostRecentMemoriesToken = inputs[this.mostRecentMemoriesTokenKey];\n if (mostRecentMemoriesToken !== undefined) {\n return {\n [this.mostRecentMemoriesKey]: await this.getMemoriesUntilLimit(\n mostRecentMemoriesToken\n ),\n };\n }\n return {};\n }\n\n /**\n * Method that saves the context of a model run to memory.\n * @param _inputs The inputs of the model run.\n * @param outputs The outputs of the model run.\n * @returns Nothing.\n */\n async saveContext(\n _inputs: InputValues,\n outputs: OutputValues\n ): Promise<void> {\n // save the context of this model run to memory\n const mem = outputs[this.addMemoryKey];\n const now = outputs[this.nowKey];\n if (mem) {\n await this.addMemory(mem, now, {});\n }\n }\n\n /**\n * Method that clears the memory contents.\n * @returns Nothing.\n */\n clear(): void {\n // TODO: clear memory contents\n }\n}\n"],"mappings":";;;;;;;;;;;;;AA2BA,IAAM,6BAAN,MAAM,mCAAmC,UAAU;CACjD,OAAO,UAAU;AACf,SAAO;;CAGT,aAAa;CAEb;CAEA,mBAAmB;CAEnB;CAEA;CAEA,UAAU;CAEV,sBAA8B;CAE9B,YACE,KACA,iBACA,QACA;AACA,SAAO;AACP,OAAK,MAAM;AACX,OAAK,kBAAkB;AACvB,OAAK,sBAAsB,OAAO;AAClC,OAAK,mBAAmB,OAAO,oBAAoB,KAAK;AACxD,OAAK,UAAU,OAAO,WAAW,KAAK;;CAGxC,aAAqB;AACnB,SAAO;;CA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":"generative_agent_memory.js","names":[],"sources":["../../../src/experimental/generative_agents/generative_agent_memory.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { BaseMemory, InputValues, OutputValues } from \"@langchain/core/memory\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { TimeWeightedVectorStoreRetriever } from \"../../retrievers/time_weighted.js\";\nimport { BaseChain } from \"../../chains/base.js\";\nimport { LLMChain } from \"../../chains/llm_chain.js\";\n\nexport type GenerativeAgentMemoryConfig = {\n reflectionThreshold?: number;\n importanceWeight?: number;\n verbose?: boolean;\n maxTokensLimit?: number;\n};\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseChain` class and has methods for adding observations\n * or memories to the agent's memory, scoring the importance of a memory,\n * reflecting on recent events to add synthesized memories, and generating\n * insights on a topic of reflection based on pertinent memories.\n */\nclass GenerativeAgentMemoryChain extends BaseChain {\n static lc_name() {\n return \"GenerativeAgentMemoryChain\";\n }\n\n reflecting = false;\n\n reflectionThreshold?: number;\n\n importanceWeight = 0.15;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n llm: BaseLanguageModelInterface;\n\n verbose = false;\n\n private aggregateImportance = 0.0;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config: Omit<GenerativeAgentMemoryConfig, \"maxTokensLimit\">\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.reflectionThreshold = config.reflectionThreshold;\n this.importanceWeight = config.importanceWeight ?? this.importanceWeight;\n this.verbose = config.verbose ?? this.verbose;\n }\n\n _chainType(): string {\n return \"generative_agent_memory\";\n }\n\n get inputKeys(): string[] {\n return [\"memory_content\", \"now\", \"memory_metadata\"];\n }\n\n get outputKeys(): string[] {\n return [\"output\"];\n }\n\n /**\n * Method that creates a new LLMChain with the given prompt.\n * @param prompt The PromptTemplate to use for the new LLMChain.\n * @returns A new LLMChain instance.\n */\n chain(prompt: PromptTemplate): LLMChain {\n const chain = new LLMChain({\n llm: this.llm,\n prompt,\n verbose: this.verbose,\n outputKey: \"output\",\n });\n return chain;\n }\n\n async _call(values: ChainValues, runManager?: CallbackManagerForChainRun) {\n const { memory_content: memoryContent, now } = values;\n // add an observation or memory to the agent's memory\n const importanceScore = await this.scoreMemoryImportance(\n memoryContent,\n runManager\n );\n this.aggregateImportance += importanceScore;\n const document = new Document({\n pageContent: memoryContent,\n metadata: {\n importance: importanceScore,\n ...values.memory_metadata,\n },\n });\n await this.memoryRetriever.addDocuments([document]);\n // after an agent has processed a certain amount of memories (as measured by aggregate importance),\n // it is time to pause and reflect on recent events to add more synthesized memories to the agent's\n // memory stream.\n if (\n this.reflectionThreshold !== undefined &&\n this.aggregateImportance > this.reflectionThreshold &&\n !this.reflecting\n ) {\n console.log(\"Reflecting on current memories...\");\n this.reflecting = true;\n await this.pauseToReflect(now, runManager);\n this.aggregateImportance = 0.0;\n this.reflecting = false;\n }\n return { output: importanceScore };\n }\n\n /**\n * Method that pauses the agent to reflect on recent events and generate\n * new insights.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for the reflection.\n * @returns An array of new insights as strings.\n */\n async pauseToReflect(\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n if (this.verbose) {\n console.log(\"Pausing to reflect...\");\n }\n const newInsights: string[] = [];\n const topics = await this.getTopicsOfReflection(50, runManager);\n for (const topic of topics) {\n const insights = await this.getInsightsOnTopic(topic, now, runManager);\n for (const insight of insights) {\n // add memory\n await this.call(\n {\n memory_content: insight,\n now,\n memory_metadata: {\n source: \"reflection_insight\",\n },\n },\n runManager?.getChild(\"reflection_insight_memory\")\n );\n }\n newInsights.push(...insights);\n }\n return newInsights;\n }\n\n /**\n * Method that scores the importance of a given memory.\n * @param memoryContent The content of the memory to score.\n * @param runManager The CallbackManagerForChainRun to use for scoring.\n * @returns The importance score of the memory as a number.\n */\n async scoreMemoryImportance(\n memoryContent: string,\n runManager?: CallbackManagerForChainRun\n ): Promise<number> {\n // score the absolute importance of a given memory\n const prompt = PromptTemplate.fromTemplate(\n \"On the scale of 1 to 10, where 1 is purely mundane\" +\n \" (e.g., brushing teeth, making bed) and 10 is\" +\n \" extremely poignant (e.g., a break up, college\" +\n \" acceptance), rate the likely poignancy of the\" +\n \" following piece of memory. Respond with a single integer.\" +\n \"\\nMemory: {memory_content}\" +\n \"\\nRating: \"\n );\n const score = await this.chain(prompt).run(\n memoryContent,\n runManager?.getChild(\"determine_importance\")\n );\n\n const strippedScore = score.trim();\n\n if (this.verbose) {\n console.log(\"Importance score:\", strippedScore);\n }\n const match = strippedScore.match(/^\\D*(\\d+)/);\n if (match) {\n const capturedNumber = parseFloat(match[1]);\n const result = (capturedNumber / 10) * this.importanceWeight;\n return result;\n } else {\n return 0.0;\n }\n }\n\n /**\n * Method that retrieves the topics of reflection based on the last K\n * memories.\n * @param lastK The number of most recent memories to consider for generating topics.\n * @param runManager The CallbackManagerForChainRun to use for retrieving topics.\n * @returns An array of topics of reflection as strings.\n */\n async getTopicsOfReflection(\n lastK: number,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n const prompt = PromptTemplate.fromTemplate(\n \"{observations}\\n\\n\" +\n \"Given only the information above, what are the 3 most salient\" +\n \" high-level questions we can answer about the subjects in\" +\n \" the statements? Provide each question on a new line.\\n\\n\"\n );\n\n const observations = this.memoryRetriever.getMemoryStream().slice(-lastK);\n const observationStr = observations\n .map((o: { pageContent: string }) => o.pageContent)\n .join(\"\\n\");\n const result = await this.chain(prompt).run(\n observationStr,\n runManager?.getChild(\"reflection_topics\")\n );\n return GenerativeAgentMemoryChain.parseList(result);\n }\n\n /**\n * Method that generates insights on a given topic of reflection based on\n * pertinent memories.\n * @param topic The topic of reflection.\n * @param now The current date.\n * @param runManager The CallbackManagerForChainRun to use for generating insights.\n * @returns An array of insights as strings.\n */\n async getInsightsOnTopic(\n topic: string,\n now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<string[]> {\n // generate insights on a topic of reflection, based on pertinent memories\n const prompt = PromptTemplate.fromTemplate(\n \"Statements about {topic}\\n\" +\n \"{related_statements}\\n\\n\" +\n \"What 5 high-level insights can you infer from the above statements?\" +\n \" (example format: insight (because of 1, 5, 3))\"\n );\n\n const relatedMemories = await this.fetchMemories(topic, now, runManager);\n const relatedStatements: string = relatedMemories\n .map((memory, index) => `${index + 1}. ${memory.pageContent}`)\n .join(\"\\n\");\n const result = await this.chain(prompt).call(\n {\n topic,\n related_statements: relatedStatements,\n },\n runManager?.getChild(\"reflection_insights\")\n );\n return GenerativeAgentMemoryChain.parseList(result.output); // added output\n }\n\n /**\n * Method that parses a newline-separated string into a list of strings.\n * @param text The newline-separated string to parse.\n * @returns An array of strings.\n */\n static parseList(text: string): string[] {\n // parse a newine seperates string into a list of strings\n return text.split(\"\\n\").map((s) => s.trim());\n }\n\n // TODO: Mock \"now\" to simulate different times\n /**\n * Method that fetches memories related to a given observation.\n * @param observation The observation to fetch memories for.\n * @param _now The current date.\n * @param runManager The CallbackManagerForChainRun to use for fetching memories.\n * @returns An array of Document instances representing the fetched memories.\n */\n async fetchMemories(\n observation: string,\n _now?: Date,\n runManager?: CallbackManagerForChainRun\n ): Promise<Document[]> {\n return this.memoryRetriever.invoke(\n observation,\n runManager?.getChild(\"memory_retriever\")\n );\n }\n}\n\n/**\n * Class that manages the memory of a generative agent in LangChain. It\n * extends the `BaseMemory` class and has methods for adding a memory,\n * formatting memories, getting memories until a token limit is reached,\n * loading memory variables, saving the context of a model run to memory,\n * and clearing memory contents.\n * @example\n * ```typescript\n * const createNewMemoryRetriever = async () => {\n * const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());\n * const retriever = new TimeWeightedVectorStoreRetriever({\n * vectorStore,\n * otherScoreKeys: [\"importance\"],\n * k: 15,\n * });\n * return retriever;\n * };\n * const tommiesMemory = new GenerativeAgentMemory(\n * llm,\n * await createNewMemoryRetriever(),\n * { reflectionThreshold: 8 },\n * );\n * const summary = await tommiesMemory.getSummary();\n * ```\n */\nexport class GenerativeAgentMemory extends BaseMemory {\n llm: BaseLanguageModelInterface;\n\n memoryRetriever: TimeWeightedVectorStoreRetriever;\n\n verbose: boolean;\n\n reflectionThreshold?: number;\n\n private maxTokensLimit = 1200;\n\n queriesKey = \"queries\";\n\n mostRecentMemoriesTokenKey = \"recent_memories_token\";\n\n addMemoryKey = \"addMemory\";\n\n relevantMemoriesKey = \"relevant_memories\";\n\n relevantMemoriesSimpleKey = \"relevant_memories_simple\";\n\n mostRecentMemoriesKey = \"most_recent_memories\";\n\n nowKey = \"now\";\n\n memoryChain: GenerativeAgentMemoryChain;\n\n constructor(\n llm: BaseLanguageModelInterface,\n memoryRetriever: TimeWeightedVectorStoreRetriever,\n config?: GenerativeAgentMemoryConfig\n ) {\n super();\n this.llm = llm;\n this.memoryRetriever = memoryRetriever;\n this.verbose = config?.verbose ?? this.verbose;\n this.reflectionThreshold =\n config?.reflectionThreshold ?? this.reflectionThreshold;\n this.maxTokensLimit = config?.maxTokensLimit ?? this.maxTokensLimit;\n this.memoryChain = new GenerativeAgentMemoryChain(llm, memoryRetriever, {\n reflectionThreshold: config?.reflectionThreshold,\n importanceWeight: config?.importanceWeight,\n });\n }\n\n /**\n * Method that returns the key for relevant memories.\n * @returns The key for relevant memories as a string.\n */\n getRelevantMemoriesKey(): string {\n return this.relevantMemoriesKey;\n }\n\n /**\n * Method that returns the key for the most recent memories token.\n * @returns The key for the most recent memories token as a string.\n */\n getMostRecentMemoriesTokenKey(): string {\n return this.mostRecentMemoriesTokenKey;\n }\n\n /**\n * Method that returns the key for adding a memory.\n * @returns The key for adding a memory as a string.\n */\n getAddMemoryKey(): string {\n return this.addMemoryKey;\n }\n\n /**\n * Method that returns the key for the current time.\n * @returns The key for the current time as a string.\n */\n getCurrentTimeKey(): string {\n return this.nowKey;\n }\n\n get memoryKeys(): string[] {\n // Return an array of memory keys\n return [this.relevantMemoriesKey, this.mostRecentMemoriesKey];\n }\n\n /**\n * Method that adds a memory to the agent's memory.\n * @param memoryContent The content of the memory to add.\n * @param now The current date.\n * @param metadata The metadata for the memory.\n * @param callbacks The Callbacks to use for adding the memory.\n * @returns The result of the memory addition.\n */\n async addMemory(\n memoryContent: string,\n now?: Date,\n metadata?: Record<string, unknown>,\n callbacks?: Callbacks\n ) {\n return this.memoryChain.call(\n { memory_content: memoryContent, now, memory_metadata: metadata },\n callbacks\n );\n }\n\n /**\n * Method that formats the given relevant memories in detail.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesDetail(relevantMemories: Document[]): string {\n if (!relevantMemories.length) {\n return \"No relevant information.\";\n }\n const contentStrings = new Set();\n const content = [];\n for (const memory of relevantMemories) {\n if (memory.pageContent in contentStrings) {\n continue;\n }\n contentStrings.add(memory.pageContent);\n const createdTime = memory.metadata.created_at.toLocaleString(\"en-US\", {\n month: \"long\",\n day: \"numeric\",\n year: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n hour12: true,\n });\n content.push(`${createdTime}: ${memory.pageContent.trim()}`);\n }\n const joinedContent = content.map((mem) => `${mem}`).join(\"\\n\");\n return joinedContent;\n }\n\n /**\n * Method that formats the given relevant memories in a simple manner.\n * @param relevantMemories The relevant memories to format.\n * @returns The formatted memories as a string.\n */\n formatMemoriesSimple(relevantMemories: Document[]): string {\n const joinedContent = relevantMemories\n .map((mem) => `${mem.pageContent}`)\n .join(\"; \");\n return joinedContent;\n }\n\n /**\n * Method that retrieves memories until a token limit is reached.\n * @param consumedTokens The number of tokens consumed so far.\n * @returns The memories as a string.\n */\n async getMemoriesUntilLimit(consumedTokens: number): Promise<string> {\n // reduce the number of tokens in the documents\n const result = [];\n for (const doc of this.memoryRetriever\n .getMemoryStream()\n .slice()\n .reverse()) {\n if (consumedTokens >= this.maxTokensLimit) {\n if (this.verbose) {\n console.log(\"Exceeding max tokens for LLM, filtering memories\");\n }\n break;\n }\n // oxlint-disable-next-line no-param-reassign\n consumedTokens += await this.llm.getNumTokens(doc.pageContent);\n if (consumedTokens < this.maxTokensLimit) {\n result.push(doc);\n }\n }\n return this.formatMemoriesSimple(result);\n }\n\n get memoryVariables(): string[] {\n // input keys this memory class will load dynamically\n return [];\n }\n\n /**\n * Method that loads memory variables based on the given inputs.\n * @param inputs The inputs to use for loading memory variables.\n * @returns An object containing the loaded memory variables.\n */\n async loadMemoryVariables(\n inputs: InputValues\n ): Promise<Record<string, string>> {\n const queries = inputs[this.queriesKey];\n const now = inputs[this.nowKey];\n if (queries !== undefined) {\n const relevantMemories = (\n await Promise.all(\n queries.map((query: string) =>\n this.memoryChain.fetchMemories(query, now)\n )\n )\n ).flat();\n return {\n [this.relevantMemoriesKey]: this.formatMemoriesDetail(relevantMemories),\n [this.relevantMemoriesSimpleKey]:\n this.formatMemoriesSimple(relevantMemories),\n };\n }\n const mostRecentMemoriesToken = inputs[this.mostRecentMemoriesTokenKey];\n if (mostRecentMemoriesToken !== undefined) {\n return {\n [this.mostRecentMemoriesKey]: await this.getMemoriesUntilLimit(\n mostRecentMemoriesToken\n ),\n };\n }\n return {};\n }\n\n /**\n * Method that saves the context of a model run to memory.\n * @param _inputs The inputs of the model run.\n * @param outputs The outputs of the model run.\n * @returns Nothing.\n */\n async saveContext(\n _inputs: InputValues,\n outputs: OutputValues\n ): Promise<void> {\n // save the context of this model run to memory\n const mem = outputs[this.addMemoryKey];\n const now = outputs[this.nowKey];\n if (mem) {\n await this.addMemory(mem, now, {});\n }\n }\n\n /**\n * Method that clears the memory contents.\n * @returns Nothing.\n */\n clear(): void {\n // TODO: clear memory contents\n }\n}\n"],"mappings":";;;;;;;;;;;;;AA2BA,IAAM,6BAAN,MAAM,mCAAmC,UAAU;CACjD,OAAO,UAAU;AACf,SAAO;;CAGT,aAAa;CAE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