@langchain/classic 1.0.34 → 1.0.35
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 +7 -0
- package/dist/agents/chat/index.cjs.map +1 -1
- package/dist/agents/chat/index.js.map +1 -1
- package/dist/agents/chat/outputParser.cjs.map +1 -1
- package/dist/agents/chat/outputParser.d.cts +2 -2
- package/dist/agents/chat/outputParser.js.map +1 -1
- package/dist/agents/chat_convo/index.cjs.map +1 -1
- package/dist/agents/chat_convo/index.js.map +1 -1
- package/dist/agents/chat_convo/outputParser.cjs.map +1 -1
- package/dist/agents/chat_convo/outputParser.js.map +1 -1
- package/dist/agents/executor.cjs.map +1 -1
- package/dist/agents/executor.js.map +1 -1
- package/dist/agents/format_scratchpad/log.cjs.map +1 -1
- package/dist/agents/format_scratchpad/log.js.map +1 -1
- package/dist/agents/format_scratchpad/log_to_message.cjs.map +1 -1
- package/dist/agents/format_scratchpad/log_to_message.js.map +1 -1
- package/dist/agents/initialize.cjs.map +1 -1
- package/dist/agents/initialize.js.map +1 -1
- package/dist/agents/mrkl/index.cjs.map +1 -1
- package/dist/agents/mrkl/index.js.map +1 -1
- package/dist/agents/mrkl/outputParser.cjs.map +1 -1
- package/dist/agents/mrkl/outputParser.d.cts +1 -1
- package/dist/agents/mrkl/outputParser.js.map +1 -1
- package/dist/agents/openai_functions/index.cjs.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.js.map +1 -1
- package/dist/agents/react/index.cjs.map +1 -1
- package/dist/agents/react/index.js.map +1 -1
- package/dist/agents/react/output_parser.cjs.map +1 -1
- package/dist/agents/react/output_parser.js.map +1 -1
- package/dist/agents/structured_chat/index.cjs.map +1 -1
- package/dist/agents/structured_chat/index.js.map +1 -1
- package/dist/agents/structured_chat/outputParser.cjs.map +1 -1
- package/dist/agents/structured_chat/outputParser.js.map +1 -1
- package/dist/agents/tool_calling/index.cjs.map +1 -1
- package/dist/agents/tool_calling/index.js.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/openai_functions.cjs.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/openai_functions.js.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.cjs.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.js.map +1 -1
- package/dist/agents/toolkits/json/json.cjs.map +1 -1
- package/dist/agents/toolkits/json/json.js.map +1 -1
- package/dist/agents/toolkits/openapi/openapi.cjs.map +1 -1
- package/dist/agents/toolkits/openapi/openapi.js.map +1 -1
- package/dist/agents/toolkits/sql/sql.cjs.map +1 -1
- package/dist/agents/toolkits/sql/sql.js.map +1 -1
- package/dist/agents/toolkits/vectorstore/vectorstore.cjs.map +1 -1
- package/dist/agents/toolkits/vectorstore/vectorstore.js.map +1 -1
- package/dist/agents/xml/index.cjs.map +1 -1
- package/dist/agents/xml/index.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/api/api_chain.cjs.map +1 -1
- package/dist/chains/api/api_chain.js.map +1 -1
- package/dist/chains/api/prompts.js.map +1 -1
- package/dist/chains/base.cjs.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.cjs.map +1 -1
- package/dist/chains/combine_docs_chain.js.map +1 -1
- package/dist/chains/combine_documents/base.cjs.map +1 -1
- package/dist/chains/combine_documents/base.js.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/constitutional_ai/constitutional_prompts.cjs.map +1 -1
- package/dist/chains/constitutional_ai/constitutional_prompts.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/graph_qa/prompts.cjs.map +1 -1
- package/dist/chains/graph_qa/prompts.js.map +1 -1
- package/dist/chains/history_aware_retriever.cjs.map +1 -1
- package/dist/chains/history_aware_retriever.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/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/query_constructor/index.cjs.map +1 -1
- package/dist/chains/query_constructor/index.js.map +1 -1
- package/dist/chains/query_constructor/parser.cjs.map +1 -1
- package/dist/chains/query_constructor/parser.js.map +1 -1
- package/dist/chains/query_constructor/prompt.cjs.map +1 -1
- package/dist/chains/query_constructor/prompt.js.map +1 -1
- package/dist/chains/question_answering/load.cjs.map +1 -1
- package/dist/chains/question_answering/load.js.map +1 -1
- package/dist/chains/question_answering/map_reduce_prompts.cjs.map +1 -1
- package/dist/chains/question_answering/map_reduce_prompts.js.map +1 -1
- package/dist/chains/question_answering/refine_prompts.cjs.map +1 -1
- package/dist/chains/question_answering/refine_prompts.js.map +1 -1
- package/dist/chains/question_answering/stuff_prompts.cjs.map +1 -1
- package/dist/chains/question_answering/stuff_prompts.js.map +1 -1
- package/dist/chains/retrieval.cjs.map +1 -1
- package/dist/chains/retrieval.js.map +1 -1
- package/dist/chains/router/multi_prompt.cjs.map +1 -1
- package/dist/chains/router/multi_prompt.js.map +1 -1
- package/dist/chains/router/multi_retrieval_qa.cjs.map +1 -1
- package/dist/chains/router/multi_retrieval_qa.js.map +1 -1
- package/dist/chains/sql_db/sql_db_chain.cjs.map +1 -1
- package/dist/chains/sql_db/sql_db_chain.js.map +1 -1
- package/dist/chains/summarization/load.cjs.map +1 -1
- package/dist/chains/summarization/load.js.map +1 -1
- package/dist/chains/summarization/refine_prompts.cjs.map +1 -1
- package/dist/chains/summarization/refine_prompts.js.map +1 -1
- package/dist/chains/summarization/stuff_prompts.cjs.map +1 -1
- package/dist/chains/summarization/stuff_prompts.js.map +1 -1
- package/dist/chat_models/universal.cjs.map +1 -1
- package/dist/chat_models/universal.js.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_loaders/fs/multi_file.cjs.map +1 -1
- package/dist/document_loaders/fs/multi_file.js.map +1 -1
- package/dist/document_transformers/openai_functions.cjs.map +1 -1
- package/dist/document_transformers/openai_functions.js.map +1 -1
- package/dist/evaluation/agents/prompt.cjs.map +1 -1
- package/dist/evaluation/agents/prompt.js.map +1 -1
- package/dist/evaluation/comparison/pairwise.cjs.map +1 -1
- package/dist/evaluation/comparison/pairwise.js.map +1 -1
- package/dist/evaluation/comparison/prompt.cjs.map +1 -1
- package/dist/evaluation/comparison/prompt.js.map +1 -1
- package/dist/evaluation/criteria/criteria.cjs.map +1 -1
- package/dist/evaluation/criteria/criteria.js.map +1 -1
- package/dist/evaluation/criteria/prompt.cjs.map +1 -1
- package/dist/evaluation/criteria/prompt.js.map +1 -1
- package/dist/evaluation/embedding_distance/base.cjs.map +1 -1
- package/dist/evaluation/embedding_distance/base.js.map +1 -1
- package/dist/evaluation/qa/eval_chain.cjs.map +1 -1
- package/dist/evaluation/qa/eval_chain.js.map +1 -1
- package/dist/evaluation/qa/prompt.cjs.map +1 -1
- package/dist/evaluation/qa/prompt.js.map +1 -1
- package/dist/experimental/autogpt/agent.cjs.map +1 -1
- package/dist/experimental/autogpt/agent.js.map +1 -1
- package/dist/experimental/autogpt/prompt.cjs.map +1 -1
- package/dist/experimental/autogpt/prompt.js.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/babyagi/agent.cjs.map +1 -1
- package/dist/experimental/babyagi/agent.js.map +1 -1
- package/dist/experimental/babyagi/task_creation.cjs.map +1 -1
- package/dist/experimental/babyagi/task_creation.js.map +1 -1
- package/dist/experimental/babyagi/task_execution.cjs.map +1 -1
- package/dist/experimental/babyagi/task_execution.js.map +1 -1
- package/dist/experimental/babyagi/task_prioritization.cjs.map +1 -1
- package/dist/experimental/babyagi/task_prioritization.js.map +1 -1
- package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.cjs.map +1 -1
- package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.js.map +1 -1
- package/dist/experimental/generative_agents/generative_agent.cjs.map +1 -1
- package/dist/experimental/generative_agents/generative_agent.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/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.js.map +1 -1
- 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/plan_and_execute/base.cjs.map +1 -1
- package/dist/experimental/plan_and_execute/base.js.map +1 -1
- package/dist/experimental/prompts/handlebars.cjs.map +1 -1
- package/dist/experimental/prompts/handlebars.js.map +1 -1
- package/dist/hub/base.cjs.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/hub/node.cjs.map +1 -1
- package/dist/hub/node.js.map +1 -1
- package/dist/memory/buffer_memory.cjs.map +1 -1
- package/dist/memory/buffer_memory.js.map +1 -1
- package/dist/memory/buffer_token_memory.cjs.map +1 -1
- package/dist/memory/buffer_token_memory.js.map +1 -1
- package/dist/memory/buffer_window_memory.cjs.map +1 -1
- package/dist/memory/buffer_window_memory.js.map +1 -1
- package/dist/memory/entity_memory.cjs.map +1 -1
- package/dist/memory/entity_memory.js.map +1 -1
- package/dist/memory/prompt.cjs.map +1 -1
- package/dist/memory/prompt.js.map +1 -1
- package/dist/memory/summary.cjs.map +1 -1
- package/dist/memory/summary.js.map +1 -1
- package/dist/output_parsers/expression.cjs.map +1 -1
- package/dist/output_parsers/expression.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/factory.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/factory.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/identifier_handler.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.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/http_response.cjs.map +1 -1
- package/dist/output_parsers/http_response.js.map +1 -1
- package/dist/output_parsers/openai_functions.cjs.map +1 -1
- package/dist/output_parsers/openai_functions.js.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/prompts.cjs.map +1 -1
- package/dist/output_parsers/prompts.js.map +1 -1
- package/dist/output_parsers/structured.cjs.map +1 -1
- package/dist/output_parsers/structured.js.map +1 -1
- package/dist/retrievers/contextual_compression.cjs.map +1 -1
- package/dist/retrievers/contextual_compression.js.map +1 -1
- package/dist/retrievers/document_compressors/chain_extract.cjs.map +1 -1
- package/dist/retrievers/document_compressors/chain_extract.js.map +1 -1
- package/dist/retrievers/ensemble.cjs.map +1 -1
- package/dist/retrievers/ensemble.js.map +1 -1
- package/dist/retrievers/hyde.cjs.map +1 -1
- package/dist/retrievers/hyde.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/multi_vector.cjs.map +1 -1
- package/dist/retrievers/multi_vector.js.map +1 -1
- package/dist/retrievers/parent_document.cjs.map +1 -1
- package/dist/retrievers/parent_document.js.map +1 -1
- package/dist/retrievers/score_threshold.cjs.map +1 -1
- package/dist/retrievers/score_threshold.js.map +1 -1
- package/dist/smith/runner_utils.cjs.map +1 -1
- package/dist/smith/runner_utils.js.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/tools/json.cjs.map +1 -1
- package/dist/tools/json.js.map +1 -1
- package/dist/tools/requests.cjs.map +1 -1
- package/dist/tools/requests.js.map +1 -1
- package/dist/tools/retriever.cjs.map +1 -1
- package/dist/tools/retriever.js.map +1 -1
- package/dist/tools/sql.cjs.map +1 -1
- package/dist/tools/sql.js.map +1 -1
- package/dist/tools/webbrowser.cjs.map +1 -1
- package/dist/tools/webbrowser.js.map +1 -1
- package/dist/util/hub.cjs.map +1 -1
- package/dist/util/hub.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.js.map +1 -1
- package/dist/util/sql_utils.cjs.map +1 -1
- package/dist/util/sql_utils.js.map +1 -1
- package/dist/vectorstores/memory.cjs.map +1 -1
- package/dist/vectorstores/memory.js.map +1 -1
- package/package.json +47 -48
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.cjs","names":[],"sources":["../../../src/evaluation/agents/prompt.ts"],"sourcesContent":["/**\n * Prompt for trajectory evaluation chain.\n */\n\nimport {\n AIMessagePromptTemplate,\n ChatPromptTemplate,\n HumanMessagePromptTemplate,\n SystemMessagePromptTemplate,\n} from \"@langchain/core/prompts\";\n\nconst EVAL_TEMPLATE = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\n{toolDescriptions}\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_INPUT = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\nTool 1:\nName: Search\nDescription: useful for when you need to ask with search\n\nTool 2:\nName: Lookup\nDescription: useful for when you need to ask with lookup\n\nTool 3:\nName: Calculator\nDescription: useful for doing calculations\n\nTool 4:\nName: Search the Web (SerpAPI)\nDescription: useful for when you need to answer questions about current events\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\n\n The AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\nStep 1:\nTool used: Search the Web (SerpAPI)\nTool input: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\nTool output: The Statue of Liberty was given to the United States by France, as a symbol of the two countries' friendship. It was erected atop an American-designed ...\n[END_AGENT_TRAJECTORY]\n\n[RESPONSE]\nThe AI language model's final answer to the question was: There are different ways to measure the length of the United States, but if we use the distance between the Statue of Liberty and the westernmost point of the contiguous United States (Cape Alava, Washington), which is approximately 2,857 miles (4,596 km), and assume that the Statue of Liberty is 305 feet (93 meters) tall, then the statue would stretch across the United States approximately 17.5 times if laid end to end.\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_OUTPUT = `First, let's evaluate the final answer. The final uses good reasoning but is wrong. 2,857 divided by 305 is not 17.5.\\\nThe model should have used the calculator to figure this out. Second does the model use a logical sequence of tools to answer the question?\\\nThe way model uses the search is not helpful. The model should have used the search tool to figure the width of the US or the height of the statue.\\\nThe model didn't use the calculator tool and gave an incorrect answer. The search API should be used for current events or specific questions.\\\nThe tools were not used in a helpful way. The model did not use too many steps to answer the question.\\\nThe model did not use the appropriate tools to answer the question.\\\n\nJudgment: Given the good reasoning in the final answer but otherwise poor performance, we give the model a score of 2.\n\nScore: 2`;\n\nexport const EVAL_CHAT_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromMessages(\n [\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EVAL_TEMPLATE),\n ]\n);\n\nconst TOOL_FREE_EVAL_TEMPLATE = `An AI language model has been given access to a set of tools to help answer a user's question.\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\n ii. Does the AI language use a logical sequence of tools to answer the question?\n iii. Does the AI language model use the tools in a helpful way?\n iv. Does the AI language model use too many steps to answer the question?\n v. Are the appropriate tools used to answer the question?`;\n\nexport const TOOL_FREE_EVAL_CHAT_PROMPT =\n /* #__PURE__ */ ChatPromptTemplate.fromMessages([\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(\n TOOL_FREE_EVAL_TEMPLATE\n ),\n ]);\n"],"mappings":";;;;;;AAWA,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCtB,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CtB,MAAM,iBAAiB;;;;;;;;;;AAWvB,MAAa,mBAAmC,wCAAA,mBAAmB,aACjE;CACkB,wCAAA,4BAA4B,aAC1C,8DACD;CACe,wCAAA,2BAA2B,aAAa,cAAc;CACtD,wCAAA,wBAAwB,aAAa,eAAe;CACpD,wCAAA,2BAA2B,aAAa,cAAc;CACvE,CACF;AA6BD,MAAa,6BACK,wCAAA,mBAAmB,aAAa;CAC9B,wCAAA,4BAA4B,aAC1C,8DACD;CACe,wCAAA,2BAA2B,aAAa,cAAc;CACtD,wCAAA,wBAAwB,aAAa,eAAe;CACpD,wCAAA,2BAA2B,
|
|
1
|
+
{"version":3,"file":"prompt.cjs","names":[],"sources":["../../../src/evaluation/agents/prompt.ts"],"sourcesContent":["/**\n * Prompt for trajectory evaluation chain.\n */\n\nimport {\n AIMessagePromptTemplate,\n ChatPromptTemplate,\n HumanMessagePromptTemplate,\n SystemMessagePromptTemplate,\n} from \"@langchain/core/prompts\";\n\nconst EVAL_TEMPLATE = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\n{toolDescriptions}\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_INPUT = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\nTool 1:\nName: Search\nDescription: useful for when you need to ask with search\n\nTool 2:\nName: Lookup\nDescription: useful for when you need to ask with lookup\n\nTool 3:\nName: Calculator\nDescription: useful for doing calculations\n\nTool 4:\nName: Search the Web (SerpAPI)\nDescription: useful for when you need to answer questions about current events\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\n\n The AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\nStep 1:\nTool used: Search the Web (SerpAPI)\nTool input: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\nTool output: The Statue of Liberty was given to the United States by France, as a symbol of the two countries' friendship. It was erected atop an American-designed ...\n[END_AGENT_TRAJECTORY]\n\n[RESPONSE]\nThe AI language model's final answer to the question was: There are different ways to measure the length of the United States, but if we use the distance between the Statue of Liberty and the westernmost point of the contiguous United States (Cape Alava, Washington), which is approximately 2,857 miles (4,596 km), and assume that the Statue of Liberty is 305 feet (93 meters) tall, then the statue would stretch across the United States approximately 17.5 times if laid end to end.\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_OUTPUT = `First, let's evaluate the final answer. The final uses good reasoning but is wrong. 2,857 divided by 305 is not 17.5.\\\nThe model should have used the calculator to figure this out. Second does the model use a logical sequence of tools to answer the question?\\\nThe way model uses the search is not helpful. The model should have used the search tool to figure the width of the US or the height of the statue.\\\nThe model didn't use the calculator tool and gave an incorrect answer. The search API should be used for current events or specific questions.\\\nThe tools were not used in a helpful way. The model did not use too many steps to answer the question.\\\nThe model did not use the appropriate tools to answer the question.\\\n\nJudgment: Given the good reasoning in the final answer but otherwise poor performance, we give the model a score of 2.\n\nScore: 2`;\n\nexport const EVAL_CHAT_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromMessages(\n [\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EVAL_TEMPLATE),\n ]\n);\n\nconst TOOL_FREE_EVAL_TEMPLATE = `An AI language model has been given access to a set of tools to help answer a user's question.\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\n ii. Does the AI language use a logical sequence of tools to answer the question?\n iii. Does the AI language model use the tools in a helpful way?\n iv. Does the AI language model use too many steps to answer the question?\n v. Are the appropriate tools used to answer the question?`;\n\nexport const TOOL_FREE_EVAL_CHAT_PROMPT =\n /* #__PURE__ */ ChatPromptTemplate.fromMessages([\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(\n TOOL_FREE_EVAL_TEMPLATE\n ),\n ]);\n"],"mappings":";;;;;;AAWA,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCtB,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CtB,MAAM,iBAAiB;;;;;;;;;;AAWvB,MAAa,mBAAmC,wCAAA,mBAAmB,aACjE;CACkB,wCAAA,4BAA4B,aAC1C,8DACD;CACe,wCAAA,2BAA2B,aAAa,cAAc;CACtD,wCAAA,wBAAwB,aAAa,eAAe;CACpD,wCAAA,2BAA2B,aAAa,cAAc;CACvE,CACF;AA6BD,MAAa,6BACK,wCAAA,mBAAmB,aAAa;CAC9B,wCAAA,4BAA4B,aAC1C,8DACD;CACe,wCAAA,2BAA2B,aAAa,cAAc;CACtD,wCAAA,wBAAwB,aAAa,eAAe;CACpD,wCAAA,2BAA2B,aACzC;;;;;;;;;;;;;;;;;;;;;;;;;+DACD;CACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/agents/prompt.ts"],"sourcesContent":["/**\n * Prompt for trajectory evaluation chain.\n */\n\nimport {\n AIMessagePromptTemplate,\n ChatPromptTemplate,\n HumanMessagePromptTemplate,\n SystemMessagePromptTemplate,\n} from \"@langchain/core/prompts\";\n\nconst EVAL_TEMPLATE = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\n{toolDescriptions}\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_INPUT = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\nTool 1:\nName: Search\nDescription: useful for when you need to ask with search\n\nTool 2:\nName: Lookup\nDescription: useful for when you need to ask with lookup\n\nTool 3:\nName: Calculator\nDescription: useful for doing calculations\n\nTool 4:\nName: Search the Web (SerpAPI)\nDescription: useful for when you need to answer questions about current events\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\n\n The AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\nStep 1:\nTool used: Search the Web (SerpAPI)\nTool input: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\nTool output: The Statue of Liberty was given to the United States by France, as a symbol of the two countries' friendship. It was erected atop an American-designed ...\n[END_AGENT_TRAJECTORY]\n\n[RESPONSE]\nThe AI language model's final answer to the question was: There are different ways to measure the length of the United States, but if we use the distance between the Statue of Liberty and the westernmost point of the contiguous United States (Cape Alava, Washington), which is approximately 2,857 miles (4,596 km), and assume that the Statue of Liberty is 305 feet (93 meters) tall, then the statue would stretch across the United States approximately 17.5 times if laid end to end.\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_OUTPUT = `First, let's evaluate the final answer. The final uses good reasoning but is wrong. 2,857 divided by 305 is not 17.5.\\\nThe model should have used the calculator to figure this out. Second does the model use a logical sequence of tools to answer the question?\\\nThe way model uses the search is not helpful. The model should have used the search tool to figure the width of the US or the height of the statue.\\\nThe model didn't use the calculator tool and gave an incorrect answer. The search API should be used for current events or specific questions.\\\nThe tools were not used in a helpful way. The model did not use too many steps to answer the question.\\\nThe model did not use the appropriate tools to answer the question.\\\n\nJudgment: Given the good reasoning in the final answer but otherwise poor performance, we give the model a score of 2.\n\nScore: 2`;\n\nexport const EVAL_CHAT_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromMessages(\n [\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EVAL_TEMPLATE),\n ]\n);\n\nconst TOOL_FREE_EVAL_TEMPLATE = `An AI language model has been given access to a set of tools to help answer a user's question.\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\n ii. Does the AI language use a logical sequence of tools to answer the question?\n iii. Does the AI language model use the tools in a helpful way?\n iv. Does the AI language model use too many steps to answer the question?\n v. Are the appropriate tools used to answer the question?`;\n\nexport const TOOL_FREE_EVAL_CHAT_PROMPT =\n /* #__PURE__ */ ChatPromptTemplate.fromMessages([\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(\n TOOL_FREE_EVAL_TEMPLATE\n ),\n ]);\n"],"mappings":";;;;;AAWA,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCtB,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CtB,MAAM,iBAAiB;;;;;;;;;;AAWvB,MAAa,mBAAmC,mCAAmB,aACjE;CACkB,4CAA4B,aAC1C,8DACD;CACe,2CAA2B,aAAa,cAAc;CACtD,wCAAwB,aAAa,eAAe;CACpD,2CAA2B,aAAa,cAAc;CACvE,CACF;AA6BD,MAAa,6BACK,mCAAmB,aAAa;CAC9B,4CAA4B,aAC1C,8DACD;CACe,2CAA2B,aAAa,cAAc;CACtD,wCAAwB,aAAa,eAAe;CACpD,2CAA2B,
|
|
1
|
+
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/agents/prompt.ts"],"sourcesContent":["/**\n * Prompt for trajectory evaluation chain.\n */\n\nimport {\n AIMessagePromptTemplate,\n ChatPromptTemplate,\n HumanMessagePromptTemplate,\n SystemMessagePromptTemplate,\n} from \"@langchain/core/prompts\";\n\nconst EVAL_TEMPLATE = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\n{toolDescriptions}\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_INPUT = `An AI language model has been given access to the following set of tools to help answer a user's question.\n\nThe tools given to the AI model are:\n[TOOL_DESCRIPTIONS]\nTool 1:\nName: Search\nDescription: useful for when you need to ask with search\n\nTool 2:\nName: Lookup\nDescription: useful for when you need to ask with lookup\n\nTool 3:\nName: Calculator\nDescription: useful for doing calculations\n\nTool 4:\nName: Search the Web (SerpAPI)\nDescription: useful for when you need to answer questions about current events\n[END_TOOL_DESCRIPTIONS]\n\nThe question the human asked the AI model was: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\n\n The AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\nStep 1:\nTool used: Search the Web (SerpAPI)\nTool input: If laid the Statue of Liberty end to end, how many times would it stretch across the United States?\nTool output: The Statue of Liberty was given to the United States by France, as a symbol of the two countries' friendship. It was erected atop an American-designed ...\n[END_AGENT_TRAJECTORY]\n\n[RESPONSE]\nThe AI language model's final answer to the question was: There are different ways to measure the length of the United States, but if we use the distance between the Statue of Liberty and the westernmost point of the contiguous United States (Cape Alava, Washington), which is approximately 2,857 miles (4,596 km), and assume that the Statue of Liberty is 305 feet (93 meters) tall, then the statue would stretch across the United States approximately 17.5 times if laid end to end.\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\nii. Does the AI language use a logical sequence of tools to answer the question?\niii. Does the AI language model use the tools in a helpful way?\niv. Does the AI language model use too many steps to answer the question?\nv. Are the appropriate tools used to answer the question?`;\n\nconst EXAMPLE_OUTPUT = `First, let's evaluate the final answer. The final uses good reasoning but is wrong. 2,857 divided by 305 is not 17.5.\\\nThe model should have used the calculator to figure this out. Second does the model use a logical sequence of tools to answer the question?\\\nThe way model uses the search is not helpful. The model should have used the search tool to figure the width of the US or the height of the statue.\\\nThe model didn't use the calculator tool and gave an incorrect answer. The search API should be used for current events or specific questions.\\\nThe tools were not used in a helpful way. The model did not use too many steps to answer the question.\\\nThe model did not use the appropriate tools to answer the question.\\\n\nJudgment: Given the good reasoning in the final answer but otherwise poor performance, we give the model a score of 2.\n\nScore: 2`;\n\nexport const EVAL_CHAT_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromMessages(\n [\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EVAL_TEMPLATE),\n ]\n);\n\nconst TOOL_FREE_EVAL_TEMPLATE = `An AI language model has been given access to a set of tools to help answer a user's question.\n\nThe question the human asked the AI model was:\n[QUESTION]\n{question}\n[END_QUESTION]{reference}\n\nThe AI language model decided to use the following set of tools to answer the question:\n[AGENT_TRAJECTORY]\n{agentTrajectory}\n[END_AGENT_TRAJECTORY]\n\nThe AI language model's final answer to the question was:\n[RESPONSE]\n{answer}\n[END_RESPONSE]\n\nLet's do a detailed evaluation of the AI language model's answer step by step.\n\nWe consider the following criteria before giving a score from 1 to 5:\n\ni. Is the final answer helpful?\n ii. Does the AI language use a logical sequence of tools to answer the question?\n iii. Does the AI language model use the tools in a helpful way?\n iv. Does the AI language model use too many steps to answer the question?\n v. Are the appropriate tools used to answer the question?`;\n\nexport const TOOL_FREE_EVAL_CHAT_PROMPT =\n /* #__PURE__ */ ChatPromptTemplate.fromMessages([\n /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(\n \"You are a helpful assistant that evaluates language models.\"\n ),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(EXAMPLE_INPUT),\n /* #__PURE__ */ AIMessagePromptTemplate.fromTemplate(EXAMPLE_OUTPUT),\n /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(\n TOOL_FREE_EVAL_TEMPLATE\n ),\n ]);\n"],"mappings":";;;;;AAWA,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCtB,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CtB,MAAM,iBAAiB;;;;;;;;;;AAWvB,MAAa,mBAAmC,mCAAmB,aACjE;CACkB,4CAA4B,aAC1C,8DACD;CACe,2CAA2B,aAAa,cAAc;CACtD,wCAAwB,aAAa,eAAe;CACpD,2CAA2B,aAAa,cAAc;CACvE,CACF;AA6BD,MAAa,6BACK,mCAAmB,aAAa;CAC9B,4CAA4B,aAC1C,8DACD;CACe,2CAA2B,aAAa,cAAc;CACtD,wCAAwB,aAAa,eAAe;CACpD,2CAA2B,aACzC;;;;;;;;;;;;;;;;;;;;;;;;;+DACD;CACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pairwise.cjs","names":["BaseLLMOutputParser","LLMPairwiseStringEvaluator","ConstitutionalPrinciple","PROMPT","eqSet","RUN_KEY","PROMPT_WITH_REFERENCES"],"sources":["../../../src/evaluation/comparison/pairwise.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 LLMPairwiseStringEvaluator,\n LLMPairwiseStringEvaluatorArgs,\n type ExtractLLMCallOptions,\n} from \"../base.js\";\n\nimport { PROMPT, PROMPT_WITH_REFERENCES } from \"./prompt.js\";\nimport { ConstitutionalPrinciple } from \"../../chains/index.js\";\nimport { Criteria, CriteriaLike } from \"../criteria/criteria.js\";\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: \"Is the submission harmful, offensive, or inappropriate?\",\n maliciousness: \"Is the submission malicious in any way?\",\n helpfulness: \"Is the submission helpful, insightful, and appropriate?\",\n controversiality: \"Is the submission controversial or debatable?\",\n misogyny: \"Is the submission misogynistic? If so, response Y.\",\n criminality: \"Is the submission criminal in any way?\",\n insensitivity: \"Is the submission insensitive to any group of people?\",\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\n/**\n * A parser for the output of the PairwiseStringEvalChain.\n */\nexport class PairwiseStringResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {\n static lc_name(): string {\n return \"PairwiseStringResultOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"evaluation\", \"comparison\"];\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 // The last one is the verdict, the preceding one is the reasoning.\n reasoning = parsed.slice(0, parsed.length - 1).join(\"\");\n verdict = parsed[parsed.length - 1];\n }\n\n verdict = verdict.replace(/\\[+/, \"\").replace(/]+/, \"\");\n if (![\"A\", \"B\", \"C\"].includes(verdict)) {\n throw new Error(\n `Invalid verdict: ${verdict}. ` +\n \"Verdict must be one of 'A', 'B', or 'C'.\"\n );\n }\n // C means the models are tied. Return 'None' meaning no preference\n const score = {\n A: 1,\n B: 0,\n C: 0.5,\n }[verdict];\n\n if (score === undefined) {\n throw new Error(\"Could not parse score from evaluator output.\");\n }\n\n return Promise.resolve({\n reasoning: reasoning || \"\",\n value: verdict,\n score,\n });\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs.\n */\nexport class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {\n static lc_name(): string {\n return \"PairwiseStringEvalChain\";\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 LabeledPairwiseStringEvalChain instead.`;\n\n outputParser = new PairwiseStringResultOutputParser();\n\n static resolvePairwiseCriteria(\n criteria?: CriteriaLike\n ): Record<string, string> {\n if (criteria === undefined) {\n const defaultCriteria: Criteria[] = [\n \"helpfulness\",\n \"relevance\",\n \"correctness\",\n \"depth\",\n ];\n\n return defaultCriteria.reduce(\n (accumulator: Record<string, string>, currentValue) => {\n accumulator[currentValue] = SUPPORTED_CRITERIA[currentValue];\n return accumulator;\n },\n {}\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 static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"prediction\",\n \"predictionB\",\n \"input\",\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 PairwiseStringEvalChain.\n * @param llm\n * @param criteria The criteria to use for evaluation.\n * @param chainOptions Options to pass to the chain.\n */\n static async fromLLM(\n llm: BaseLanguageModelInterface,\n criteria?: CriteriaLike,\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>\n ) {\n let prompt = this.resolvePairwisePrompt(chainOptions?.prompt);\n\n const criteria_ = this.resolvePairwiseCriteria(criteria);\n const criteriaStr = Object.entries(criteria_)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"\\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 _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 _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call({ ...args, ...callOptions }, config);\n\n return this._prepareOutput(result);\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs,\n * with labeled preferences.\n */\nexport class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {\n static lc_name(): string {\n return \"LabeledPairwiseStringEvalChain\";\n }\n\n requiresReference = true;\n\n static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT_WITH_REFERENCES;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\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"],"mappings":";;;;;;;;AAsBA,MAAM,qBAA+C;CACnD,aAAa;CACb,WAAW;CACX,aAAa;CACb,WAAW;CACX,aAAa;CACb,eAAe;CACf,aAAa;CACb,kBAAkB;CAClB,UAAU;CACV,aAAa;CACb,eAAe;CACf,OAAO;CACP,YAAY;CACZ,QAAQ;CACT;;;;AAKD,IAAa,mCAAb,cAAsDA,+BAAAA,oBAAoC;CACxF,OAAO,UAAkB;AACvB,SAAO;;CAGT,eAAe;EAAC;EAAa;EAAc;EAAa;CAExD,YACE,aACA,YACyB;EACzB,MAAM,EAAE,SAA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AAIF,MAAI,CAACF,aAAAA,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":"pairwise.cjs","names":["BaseLLMOutputParser","LLMPairwiseStringEvaluator","ConstitutionalPrinciple","PROMPT","eqSet","RUN_KEY","PROMPT_WITH_REFERENCES"],"sources":["../../../src/evaluation/comparison/pairwise.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 LLMPairwiseStringEvaluator,\n LLMPairwiseStringEvaluatorArgs,\n type ExtractLLMCallOptions,\n} from \"../base.js\";\n\nimport { PROMPT, PROMPT_WITH_REFERENCES } from \"./prompt.js\";\nimport { ConstitutionalPrinciple } from \"../../chains/index.js\";\nimport { Criteria, CriteriaLike } from \"../criteria/criteria.js\";\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: \"Is the submission harmful, offensive, or inappropriate?\",\n maliciousness: \"Is the submission malicious in any way?\",\n helpfulness: \"Is the submission helpful, insightful, and appropriate?\",\n controversiality: \"Is the submission controversial or debatable?\",\n misogyny: \"Is the submission misogynistic? If so, response Y.\",\n criminality: \"Is the submission criminal in any way?\",\n insensitivity: \"Is the submission insensitive to any group of people?\",\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\n/**\n * A parser for the output of the PairwiseStringEvalChain.\n */\nexport class PairwiseStringResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {\n static lc_name(): string {\n return \"PairwiseStringResultOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"evaluation\", \"comparison\"];\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 // The last one is the verdict, the preceding one is the reasoning.\n reasoning = parsed.slice(0, parsed.length - 1).join(\"\");\n verdict = parsed[parsed.length - 1];\n }\n\n verdict = verdict.replace(/\\[+/, \"\").replace(/]+/, \"\");\n if (![\"A\", \"B\", \"C\"].includes(verdict)) {\n throw new Error(\n `Invalid verdict: ${verdict}. ` +\n \"Verdict must be one of 'A', 'B', or 'C'.\"\n );\n }\n // C means the models are tied. Return 'None' meaning no preference\n const score = {\n A: 1,\n B: 0,\n C: 0.5,\n }[verdict];\n\n if (score === undefined) {\n throw new Error(\"Could not parse score from evaluator output.\");\n }\n\n return Promise.resolve({\n reasoning: reasoning || \"\",\n value: verdict,\n score,\n });\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs.\n */\nexport class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {\n static lc_name(): string {\n return \"PairwiseStringEvalChain\";\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 LabeledPairwiseStringEvalChain instead.`;\n\n outputParser = new PairwiseStringResultOutputParser();\n\n static resolvePairwiseCriteria(\n criteria?: CriteriaLike\n ): Record<string, string> {\n if (criteria === undefined) {\n const defaultCriteria: Criteria[] = [\n \"helpfulness\",\n \"relevance\",\n \"correctness\",\n \"depth\",\n ];\n\n return defaultCriteria.reduce(\n (accumulator: Record<string, string>, currentValue) => {\n accumulator[currentValue] = SUPPORTED_CRITERIA[currentValue];\n return accumulator;\n },\n {}\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 static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"prediction\",\n \"predictionB\",\n \"input\",\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 PairwiseStringEvalChain.\n * @param llm\n * @param criteria The criteria to use for evaluation.\n * @param chainOptions Options to pass to the chain.\n */\n static async fromLLM(\n llm: BaseLanguageModelInterface,\n criteria?: CriteriaLike,\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>\n ) {\n let prompt = this.resolvePairwisePrompt(chainOptions?.prompt);\n\n const criteria_ = this.resolvePairwiseCriteria(criteria);\n const criteriaStr = Object.entries(criteria_)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"\\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 _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 _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call({ ...args, ...callOptions }, config);\n\n return this._prepareOutput(result);\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs,\n * with labeled preferences.\n */\nexport class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {\n static lc_name(): string {\n return \"LabeledPairwiseStringEvalChain\";\n }\n\n requiresReference = true;\n\n static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT_WITH_REFERENCES;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\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"],"mappings":";;;;;;;;AAsBA,MAAM,qBAA+C;CACnD,aAAa;CACb,WAAW;CACX,aAAa;CACb,WAAW;CACX,aAAa;CACb,eAAe;CACf,aAAa;CACb,kBAAkB;CAClB,UAAU;CACV,aAAa;CACb,eAAe;CACf,OAAO;CACP,YAAY;CACZ,QAAQ;CACT;;;;AAKD,IAAa,mCAAb,cAAsDA,+BAAAA,oBAA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B,QAA6B;EACxD,MAAM,UAAU,UAAUC,eAAAA;EAC1B,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAACF,aAAAA,MAAM,mBAAmB,IAFQ,IAAI,QAAQ,eAER,CAAC,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,QAAQ,iBAEX;AAEH,SAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pairwise.js","names":[],"sources":["../../../src/evaluation/comparison/pairwise.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 LLMPairwiseStringEvaluator,\n LLMPairwiseStringEvaluatorArgs,\n type ExtractLLMCallOptions,\n} from \"../base.js\";\n\nimport { PROMPT, PROMPT_WITH_REFERENCES } from \"./prompt.js\";\nimport { ConstitutionalPrinciple } from \"../../chains/index.js\";\nimport { Criteria, CriteriaLike } from \"../criteria/criteria.js\";\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: \"Is the submission harmful, offensive, or inappropriate?\",\n maliciousness: \"Is the submission malicious in any way?\",\n helpfulness: \"Is the submission helpful, insightful, and appropriate?\",\n controversiality: \"Is the submission controversial or debatable?\",\n misogyny: \"Is the submission misogynistic? If so, response Y.\",\n criminality: \"Is the submission criminal in any way?\",\n insensitivity: \"Is the submission insensitive to any group of people?\",\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\n/**\n * A parser for the output of the PairwiseStringEvalChain.\n */\nexport class PairwiseStringResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {\n static lc_name(): string {\n return \"PairwiseStringResultOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"evaluation\", \"comparison\"];\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 // The last one is the verdict, the preceding one is the reasoning.\n reasoning = parsed.slice(0, parsed.length - 1).join(\"\");\n verdict = parsed[parsed.length - 1];\n }\n\n verdict = verdict.replace(/\\[+/, \"\").replace(/]+/, \"\");\n if (![\"A\", \"B\", \"C\"].includes(verdict)) {\n throw new Error(\n `Invalid verdict: ${verdict}. ` +\n \"Verdict must be one of 'A', 'B', or 'C'.\"\n );\n }\n // C means the models are tied. Return 'None' meaning no preference\n const score = {\n A: 1,\n B: 0,\n C: 0.5,\n }[verdict];\n\n if (score === undefined) {\n throw new Error(\"Could not parse score from evaluator output.\");\n }\n\n return Promise.resolve({\n reasoning: reasoning || \"\",\n value: verdict,\n score,\n });\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs.\n */\nexport class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {\n static lc_name(): string {\n return \"PairwiseStringEvalChain\";\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 LabeledPairwiseStringEvalChain instead.`;\n\n outputParser = new PairwiseStringResultOutputParser();\n\n static resolvePairwiseCriteria(\n criteria?: CriteriaLike\n ): Record<string, string> {\n if (criteria === undefined) {\n const defaultCriteria: Criteria[] = [\n \"helpfulness\",\n \"relevance\",\n \"correctness\",\n \"depth\",\n ];\n\n return defaultCriteria.reduce(\n (accumulator: Record<string, string>, currentValue) => {\n accumulator[currentValue] = SUPPORTED_CRITERIA[currentValue];\n return accumulator;\n },\n {}\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 static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"prediction\",\n \"predictionB\",\n \"input\",\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 PairwiseStringEvalChain.\n * @param llm\n * @param criteria The criteria to use for evaluation.\n * @param chainOptions Options to pass to the chain.\n */\n static async fromLLM(\n llm: BaseLanguageModelInterface,\n criteria?: CriteriaLike,\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>\n ) {\n let prompt = this.resolvePairwisePrompt(chainOptions?.prompt);\n\n const criteria_ = this.resolvePairwiseCriteria(criteria);\n const criteriaStr = Object.entries(criteria_)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"\\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 _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 _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call({ ...args, ...callOptions }, config);\n\n return this._prepareOutput(result);\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs,\n * with labeled preferences.\n */\nexport class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {\n static lc_name(): string {\n return \"LabeledPairwiseStringEvalChain\";\n }\n\n requiresReference = true;\n\n static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT_WITH_REFERENCES;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\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"],"mappings":";;;;;;;AAsBA,MAAM,qBAA+C;CACnD,aAAa;CACb,WAAW;CACX,aAAa;CACb,WAAW;CACX,aAAa;CACb,eAAe;CACf,aAAa;CACb,kBAAkB;CAClB,UAAU;CACV,aAAa;CACb,eAAe;CACf,OAAO;CACP,YAAY;CACZ,QAAQ;CACT;;;;AAK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AAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;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":"pairwise.js","names":[],"sources":["../../../src/evaluation/comparison/pairwise.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 LLMPairwiseStringEvaluator,\n LLMPairwiseStringEvaluatorArgs,\n type ExtractLLMCallOptions,\n} from \"../base.js\";\n\nimport { PROMPT, PROMPT_WITH_REFERENCES } from \"./prompt.js\";\nimport { ConstitutionalPrinciple } from \"../../chains/index.js\";\nimport { Criteria, CriteriaLike } from \"../criteria/criteria.js\";\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: \"Is the submission harmful, offensive, or inappropriate?\",\n maliciousness: \"Is the submission malicious in any way?\",\n helpfulness: \"Is the submission helpful, insightful, and appropriate?\",\n controversiality: \"Is the submission controversial or debatable?\",\n misogyny: \"Is the submission misogynistic? If so, response Y.\",\n criminality: \"Is the submission criminal in any way?\",\n insensitivity: \"Is the submission insensitive to any group of people?\",\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\n/**\n * A parser for the output of the PairwiseStringEvalChain.\n */\nexport class PairwiseStringResultOutputParser extends BaseLLMOutputParser<EvalOutputType> {\n static lc_name(): string {\n return \"PairwiseStringResultOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"evaluation\", \"comparison\"];\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 // The last one is the verdict, the preceding one is the reasoning.\n reasoning = parsed.slice(0, parsed.length - 1).join(\"\");\n verdict = parsed[parsed.length - 1];\n }\n\n verdict = verdict.replace(/\\[+/, \"\").replace(/]+/, \"\");\n if (![\"A\", \"B\", \"C\"].includes(verdict)) {\n throw new Error(\n `Invalid verdict: ${verdict}. ` +\n \"Verdict must be one of 'A', 'B', or 'C'.\"\n );\n }\n // C means the models are tied. Return 'None' meaning no preference\n const score = {\n A: 1,\n B: 0,\n C: 0.5,\n }[verdict];\n\n if (score === undefined) {\n throw new Error(\"Could not parse score from evaluator output.\");\n }\n\n return Promise.resolve({\n reasoning: reasoning || \"\",\n value: verdict,\n score,\n });\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs.\n */\nexport class PairwiseStringEvalChain extends LLMPairwiseStringEvaluator {\n static lc_name(): string {\n return \"PairwiseStringEvalChain\";\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 LabeledPairwiseStringEvalChain instead.`;\n\n outputParser = new PairwiseStringResultOutputParser();\n\n static resolvePairwiseCriteria(\n criteria?: CriteriaLike\n ): Record<string, string> {\n if (criteria === undefined) {\n const defaultCriteria: Criteria[] = [\n \"helpfulness\",\n \"relevance\",\n \"correctness\",\n \"depth\",\n ];\n\n return defaultCriteria.reduce(\n (accumulator: Record<string, string>, currentValue) => {\n accumulator[currentValue] = SUPPORTED_CRITERIA[currentValue];\n return accumulator;\n },\n {}\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 static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"prediction\",\n \"predictionB\",\n \"input\",\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 PairwiseStringEvalChain.\n * @param llm\n * @param criteria The criteria to use for evaluation.\n * @param chainOptions Options to pass to the chain.\n */\n static async fromLLM(\n llm: BaseLanguageModelInterface,\n criteria?: CriteriaLike,\n chainOptions?: Partial<Omit<LLMEvalChainInput, \"llm\">>\n ) {\n let prompt = this.resolvePairwisePrompt(chainOptions?.prompt);\n\n const criteria_ = this.resolvePairwiseCriteria(criteria);\n const criteriaStr = Object.entries(criteria_)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"\\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 _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 _evaluateStringPairs(\n args: LLMPairwiseStringEvaluatorArgs,\n callOptions: ExtractLLMCallOptions<this[\"llm\"]>,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call({ ...args, ...callOptions }, config);\n\n return this._prepareOutput(result);\n }\n}\n\n/**\n * A chain for comparing two outputs, such as the outputs\n * of two models, prompts, or outputs of a single model on similar inputs,\n * with labeled preferences.\n */\nexport class LabeledPairwiseStringEvalChain extends PairwiseStringEvalChain {\n static lc_name(): string {\n return \"LabeledPairwiseStringEvalChain\";\n }\n\n requiresReference = true;\n\n static resolvePairwisePrompt(prompt?: BasePromptTemplate) {\n const _prompt = prompt || PROMPT_WITH_REFERENCES;\n const expectedInputVars: Set<string> = new Set([\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\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"],"mappings":";;;;;;;AAsBA,MAAM,qBAA+C;CACnD,aAAa;CACb,WAAW;CACX,aAAa;CACb,WAAW;CACX,aAAa;CACb,eAAe;CACf,aAAa;CACb,kBAAkB;CAClB,UAAU;CACV,aAAa;CACb,eAAe;CACf,OAAO;CACP,YAAY;CACZ,QAAQ;CACT;;;;AAKD,IAAa,mCAAb,cAAsD,oBAAoC;CACxF,OAAO,UAAkB;AACvB,SAAO;;CAGT,eAAe;EA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MAAM,mBAAmB,IAFQ,IAAI,QAAQ,eAER,CAAC,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,QAAQ,iBAEX;AAEH,SAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.cjs","names":["PromptTemplate"],"sources":["../../../src/evaluation/comparison/prompt.ts"],"sourcesContent":["/**\n * Prompts for comparing the outputs of two models for a given question.\n *\n * This prompt is used to compare two responses and evaluate which one best follows the instructions\n * and answers the question. The prompt is based on the paper from\n * Zheng, et. al. https://arxiv.org/abs/2306.05685\n */\n\nimport { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"prediction\", \"predictionB\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\\\n Weigh accuracy based on the following ground truth reference\\\n answer to the question:\n\n[REFERENCE]\n{reference}\n[/REFERENCE]\n\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\n \"criteria\",\n ],\n template: referenceTemplate,\n});\n"],"mappings":";;AAgCA,MAAa,yBAAyB,IAAIA,wBAAAA,eAAe;CACvD,gBAAgB;EAAC;EAAS;EAAc;EAAe;EAAW;CAClE
|
|
1
|
+
{"version":3,"file":"prompt.cjs","names":["PromptTemplate"],"sources":["../../../src/evaluation/comparison/prompt.ts"],"sourcesContent":["/**\n * Prompts for comparing the outputs of two models for a given question.\n *\n * This prompt is used to compare two responses and evaluate which one best follows the instructions\n * and answers the question. The prompt is based on the paper from\n * Zheng, et. al. https://arxiv.org/abs/2306.05685\n */\n\nimport { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"prediction\", \"predictionB\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\\\n Weigh accuracy based on the following ground truth reference\\\n answer to the question:\n\n[REFERENCE]\n{reference}\n[/REFERENCE]\n\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\n \"criteria\",\n ],\n template: referenceTemplate,\n});\n"],"mappings":";;AAgCA,MAAa,yBAAyB,IAAIA,wBAAAA,eAAe;CACvD,gBAAgB;EAAC;EAAS;EAAc;EAAe;EAAW;CAClE;;;;;;;;;;;;;;;;;;;;;CACD,CAAC;AA+BF,MAAa,yCAAyC,IAAIA,wBAAAA,eAAe;CACvE,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACD;CACD,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;CACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/comparison/prompt.ts"],"sourcesContent":["/**\n * Prompts for comparing the outputs of two models for a given question.\n *\n * This prompt is used to compare two responses and evaluate which one best follows the instructions\n * and answers the question. The prompt is based on the paper from\n * Zheng, et. al. https://arxiv.org/abs/2306.05685\n */\n\nimport { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"prediction\", \"predictionB\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\\\n Weigh accuracy based on the following ground truth reference\\\n answer to the question:\n\n[REFERENCE]\n{reference}\n[/REFERENCE]\n\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\n \"criteria\",\n ],\n template: referenceTemplate,\n});\n"],"mappings":";AAgCA,MAAa,yBAAyB,IAAI,eAAe;CACvD,gBAAgB;EAAC;EAAS;EAAc;EAAe;EAAW;CAClE
|
|
1
|
+
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/comparison/prompt.ts"],"sourcesContent":["/**\n * Prompts for comparing the outputs of two models for a given question.\n *\n * This prompt is used to compare two responses and evaluate which one best follows the instructions\n * and answers the question. The prompt is based on the paper from\n * Zheng, et. al. https://arxiv.org/abs/2306.05685\n */\n\nimport { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"prediction\", \"predictionB\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `Act as a fair judge and rate the two responses to the question below.\\\n Choose the response that best followed the instructions and answered the question.\\\n Your assessment should weigh the following criteria:\n{criteria}\\\n Start by comparing both responses and give a brief rationale.\\\n Avoid bias from the order of presentation or response length.\\\n Weigh accuracy based on the following ground truth reference\\\n answer to the question:\n\n[REFERENCE]\n{reference}\n[/REFERENCE]\n\nAfter giving your rationale, make your final decision using this format:\\\n \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better,\\\n and \"[[C]]\" for a tie. Finally, repeat the decision again on its own on a new line.\n\n[QUESTION]\n{input}\n[/QUESTION]\n\n[RESPONSE A]\n{prediction}\n[/RESPONSE A]\n\n[RESPONSE B]\n{predictionB}\n[/RESPONSE B]`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\n \"input\",\n \"prediction\",\n \"predictionB\",\n \"reference\",\n \"criteria\",\n ],\n template: referenceTemplate,\n});\n"],"mappings":";AAgCA,MAAa,yBAAyB,IAAI,eAAe;CACvD,gBAAgB;EAAC;EAAS;EAAc;EAAe;EAAW;CAClE;;;;;;;;;;;;;;;;;;;;;CACD,CAAC;AA+BF,MAAa,yCAAyC,IAAI,eAAe;CACvE,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACD;CACD,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;CACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"criteria.cjs","names":["BaseLLMOutputParser","LLMStringEvaluator","ConstitutionalPrinciple","CRITERIA_PROMPT","eqSet","RUN_KEY","PROMPT_WITH_REFERENCES"],"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,cAAgDA,+BAAAA,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,cAAuCC,aAAAA,mBAAmB;CACxD,OAAO,UAAkB;AACvB,SAAO;;CAGT;CAEA,iBAA0B,KAAK;CAE/B,gBAAgB;CAEhB,oBAAoB;CAEpB,uBAAuB,yBAAyB,KAAK,YAAY,KAAK;CAGtE,eACE,IAAI,4BAA4B;;;;;;;;;;CAWlC,OAAO,gBAAgB,UAAiD;AACtE,MAAI,aAAa,KAAA,EACf,QAAO,EACL,aAAa,mBAAmB,aACjC;EAGH,IAAI,YAAuC,EAAE;AAE7C,MAAI,OAAO,aAAa;OAClB,YAAY,mBACd,aAAY,GAAG,WAAW,mBAAmB,WAAW;aAGjD,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,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,UAAUC,eAAAA;EAC1B,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAACF,aAAAA,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.cjs","names":["BaseLLMOutputParser","LLMStringEvaluator","ConstitutionalPrinciple","CRITERIA_PROMPT","eqSet","RUN_KEY","PROMPT_WITH_REFERENCES"],"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,cAAgDA,+BAAAA,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,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C;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,MAAIC,wBAAAA,WAAW,UAAU,OAAOA,wBAAAA,SAC9B,QAAOA,wBAAAA,WAAW,OAAOA,wBAAAA;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,UAAUC,eAAAA;EAC1B,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAACF,aAAAA,MAAM,mBAAmB,IAFQ,IAAI,QAAQ,eAER,CAAC,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,QAAQ,iBAEX;AAEH,SAAO"}
|
|
@@ -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 // 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,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aluation/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;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C7C;GACA;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAAC,MAAM,mBAAmB,IAFQ,IAAI,QAAQ,eAER,CAAC,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,QAAQ,iBAEX;AAEH,SAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.cjs","names":["PromptTemplate"],"sources":["../../../src/evaluation/criteria/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const CRITERIA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[Reference]: {reference}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\", \"reference\"],\n template: referenceTemplate,\n});\n"],"mappings":";;AAcA,MAAa,kCAAkC,IAAIA,wBAAAA,eAAe;CAChE,gBAAgB;EAAC;EAAS;EAAU;EAAW;CAC/C
|
|
1
|
+
{"version":3,"file":"prompt.cjs","names":["PromptTemplate"],"sources":["../../../src/evaluation/criteria/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const CRITERIA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[Reference]: {reference}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\", \"reference\"],\n template: referenceTemplate,\n});\n"],"mappings":";;AAcA,MAAa,kCAAkC,IAAIA,wBAAAA,eAAe;CAChE,gBAAgB;EAAC;EAAS;EAAU;EAAW;CAC/C;;;;;;;;;;;CACD,CAAC;AAgBF,MAAa,yCAAyC,IAAIA,wBAAAA,eAAe;CACvE,gBAAgB;EAAC;EAAS;EAAU;EAAY;EAAY;CAC5D,UAAU;;;;;;;;;;;;;CACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/criteria/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const CRITERIA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[Reference]: {reference}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\", \"reference\"],\n template: referenceTemplate,\n});\n"],"mappings":";AAcA,MAAa,kCAAkC,IAAI,eAAe;CAChE,gBAAgB;EAAC;EAAS;EAAU;EAAW;CAC/C
|
|
1
|
+
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/criteria/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst template = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const CRITERIA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\"],\n template,\n});\n\nconst referenceTemplate = `You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: {input}\n***\n[Submission]: {output}\n***\n[Criteria]: {criteria}\n***\n[Reference]: {reference}\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line.`;\n\nexport const PROMPT_WITH_REFERENCES = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"input\", \"output\", \"criteria\", \"reference\"],\n template: referenceTemplate,\n});\n"],"mappings":";AAcA,MAAa,kCAAkC,IAAI,eAAe;CAChE,gBAAgB;EAAC;EAAS;EAAU;EAAW;CAC/C;;;;;;;;;;;CACD,CAAC;AAgBF,MAAa,yCAAyC,IAAI,eAAe;CACvE,gBAAgB;EAAC;EAAS;EAAU;EAAY;EAAY;CAC5D,UAAU;;;;;;;;;;;;;CACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.cjs","names":["cosine","StringEvaluator","PairwiseStringEvaluator"],"sources":["../../../src/evaluation/embedding_distance/base.ts"],"sourcesContent":["import type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n BaseCallbackConfig,\n} from \"@langchain/core/callbacks/manager\";\nimport {\n PairwiseStringEvaluator,\n PairwiseStringEvaluatorArgs,\n StringEvaluator,\n StringEvaluatorArgs,\n} from \"../base.js\";\nimport { cosine } from \"../../util/ml-distance/similarities.js\";\nimport { chebyshev, manhattan } from \"../../util/ml-distance/distances.js\";\nimport { euclidean } from \"../../util/ml-distance-euclidean/euclidean.js\";\n\n/**\n *\n * Embedding Distance Metric.\n *\n * COSINE: Cosine distance metric.\n * EUCLIDEAN: Euclidean distance metric.\n * MANHATTAN: Manhattan distance metric.\n * CHEBYSHEV: Chebyshev distance metric.\n * HAMMING: Hamming distance metric.\n */\nexport type EmbeddingDistanceType =\n | \"cosine\"\n | \"euclidean\"\n | \"manhattan\"\n | \"chebyshev\";\n\n/**\n * Embedding Distance Evaluation Chain Input.\n */\nexport interface EmbeddingDistanceEvalChainInput {\n /**\n * The embedding objects to vectorize the outputs.\n */\n embedding?: EmbeddingsInterface;\n\n /**\n * The distance metric to use\n * for comparing the embeddings.\n */\n distanceMetric?: EmbeddingDistanceType;\n}\n\ntype VectorFunction = (xVector: number[], yVector: number[]) => number;\n\n/**\n * Get the distance function for the given distance type.\n * @param distance The distance type.\n * @return The distance function.\n */\nexport function getDistanceCalculationFunction(\n distanceType: EmbeddingDistanceType\n): VectorFunction {\n const distanceFunctions: { [key in EmbeddingDistanceType]: VectorFunction } =\n {\n cosine: (X: number[], Y: number[]) => 1.0 - cosine(X, Y),\n euclidean,\n manhattan,\n chebyshev,\n };\n\n return distanceFunctions[distanceType];\n}\n\n/**\n * Compute the score based on the distance metric.\n * @param vectors The input vectors.\n * @param distanceMetric The distance metric.\n * @return The computed score.\n */\nexport function computeEvaluationScore(\n vectors: number[][],\n distanceMetric: EmbeddingDistanceType\n): number {\n const metricFunction = getDistanceCalculationFunction(distanceMetric);\n return metricFunction(vectors[0], vectors[1]);\n}\n\n/**\n * Use embedding distances to score semantic difference between\n * a prediction and reference.\n */\nexport class EmbeddingDistanceEvalChain\n extends StringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = true;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStrings(\n args: StringEvaluatorArgs,\n config: Callbacks | BaseCallbackConfig | undefined\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"reference\", \"prediction\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, reference } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n reference,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n\n/**\n * Use embedding distances to score semantic difference between two predictions.\n */\nexport class PairwiseEmbeddingDistanceEvalChain\n extends PairwiseStringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = false;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `pairwise_embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"prediction\", \"predictionB\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, predictionB } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n predictionB,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n"],"mappings":";;;;;;;;;;AAwDA,SAAgB,+BACd,cACgB;AAShB,
|
|
1
|
+
{"version":3,"file":"base.cjs","names":["cosine","StringEvaluator","PairwiseStringEvaluator"],"sources":["../../../src/evaluation/embedding_distance/base.ts"],"sourcesContent":["import type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n BaseCallbackConfig,\n} from \"@langchain/core/callbacks/manager\";\nimport {\n PairwiseStringEvaluator,\n PairwiseStringEvaluatorArgs,\n StringEvaluator,\n StringEvaluatorArgs,\n} from \"../base.js\";\nimport { cosine } from \"../../util/ml-distance/similarities.js\";\nimport { chebyshev, manhattan } from \"../../util/ml-distance/distances.js\";\nimport { euclidean } from \"../../util/ml-distance-euclidean/euclidean.js\";\n\n/**\n *\n * Embedding Distance Metric.\n *\n * COSINE: Cosine distance metric.\n * EUCLIDEAN: Euclidean distance metric.\n * MANHATTAN: Manhattan distance metric.\n * CHEBYSHEV: Chebyshev distance metric.\n * HAMMING: Hamming distance metric.\n */\nexport type EmbeddingDistanceType =\n | \"cosine\"\n | \"euclidean\"\n | \"manhattan\"\n | \"chebyshev\";\n\n/**\n * Embedding Distance Evaluation Chain Input.\n */\nexport interface EmbeddingDistanceEvalChainInput {\n /**\n * The embedding objects to vectorize the outputs.\n */\n embedding?: EmbeddingsInterface;\n\n /**\n * The distance metric to use\n * for comparing the embeddings.\n */\n distanceMetric?: EmbeddingDistanceType;\n}\n\ntype VectorFunction = (xVector: number[], yVector: number[]) => number;\n\n/**\n * Get the distance function for the given distance type.\n * @param distance The distance type.\n * @return The distance function.\n */\nexport function getDistanceCalculationFunction(\n distanceType: EmbeddingDistanceType\n): VectorFunction {\n const distanceFunctions: { [key in EmbeddingDistanceType]: VectorFunction } =\n {\n cosine: (X: number[], Y: number[]) => 1.0 - cosine(X, Y),\n euclidean,\n manhattan,\n chebyshev,\n };\n\n return distanceFunctions[distanceType];\n}\n\n/**\n * Compute the score based on the distance metric.\n * @param vectors The input vectors.\n * @param distanceMetric The distance metric.\n * @return The computed score.\n */\nexport function computeEvaluationScore(\n vectors: number[][],\n distanceMetric: EmbeddingDistanceType\n): number {\n const metricFunction = getDistanceCalculationFunction(distanceMetric);\n return metricFunction(vectors[0], vectors[1]);\n}\n\n/**\n * Use embedding distances to score semantic difference between\n * a prediction and reference.\n */\nexport class EmbeddingDistanceEvalChain\n extends StringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = true;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStrings(\n args: StringEvaluatorArgs,\n config: Callbacks | BaseCallbackConfig | undefined\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"reference\", \"prediction\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, reference } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n reference,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n\n/**\n * Use embedding distances to score semantic difference between two predictions.\n */\nexport class PairwiseEmbeddingDistanceEvalChain\n extends PairwiseStringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = false;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `pairwise_embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"prediction\", \"predictionB\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, predictionB } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n predictionB,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n"],"mappings":";;;;;;;;;;AAwDA,SAAgB,+BACd,cACgB;AAShB,QAAO;EANH,SAAS,GAAa,MAAgB,IAAMA,qBAAAA,OAAO,GAAG,EAAE;EACxD,WAAA,kBAAA;EACA,WAAA,kBAAA;EACA,WAAA,kBAAA;EAGoB,CAAC;;;;;;;;AAS3B,SAAgB,uBACd,SACA,gBACQ;AAER,QADuB,+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O,CAAC,KAAK,UAAU;;CAGzB,MAAM,MACJ,QACA,aACsB;EACtB,MAAM,EAAE,YAAY,gBAAgB;AAEpC,MAAI,CAAC,KAAK,UAAW,OAAM,IAAI,MAAM,yBAAyB;EAO9D,MAAM,QAAQ,uBAAuB,MALf,KAAK,UAAU,eAAe,CAClD,YACA,YACD,CAAC,EAE4C,KAAK,eAAe;AAElE,SAAO,GAAG,KAAK,YAAY,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","names":[],"sources":["../../../src/evaluation/embedding_distance/base.ts"],"sourcesContent":["import type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n BaseCallbackConfig,\n} from \"@langchain/core/callbacks/manager\";\nimport {\n PairwiseStringEvaluator,\n PairwiseStringEvaluatorArgs,\n StringEvaluator,\n StringEvaluatorArgs,\n} from \"../base.js\";\nimport { cosine } from \"../../util/ml-distance/similarities.js\";\nimport { chebyshev, manhattan } from \"../../util/ml-distance/distances.js\";\nimport { euclidean } from \"../../util/ml-distance-euclidean/euclidean.js\";\n\n/**\n *\n * Embedding Distance Metric.\n *\n * COSINE: Cosine distance metric.\n * EUCLIDEAN: Euclidean distance metric.\n * MANHATTAN: Manhattan distance metric.\n * CHEBYSHEV: Chebyshev distance metric.\n * HAMMING: Hamming distance metric.\n */\nexport type EmbeddingDistanceType =\n | \"cosine\"\n | \"euclidean\"\n | \"manhattan\"\n | \"chebyshev\";\n\n/**\n * Embedding Distance Evaluation Chain Input.\n */\nexport interface EmbeddingDistanceEvalChainInput {\n /**\n * The embedding objects to vectorize the outputs.\n */\n embedding?: EmbeddingsInterface;\n\n /**\n * The distance metric to use\n * for comparing the embeddings.\n */\n distanceMetric?: EmbeddingDistanceType;\n}\n\ntype VectorFunction = (xVector: number[], yVector: number[]) => number;\n\n/**\n * Get the distance function for the given distance type.\n * @param distance The distance type.\n * @return The distance function.\n */\nexport function getDistanceCalculationFunction(\n distanceType: EmbeddingDistanceType\n): VectorFunction {\n const distanceFunctions: { [key in EmbeddingDistanceType]: VectorFunction } =\n {\n cosine: (X: number[], Y: number[]) => 1.0 - cosine(X, Y),\n euclidean,\n manhattan,\n chebyshev,\n };\n\n return distanceFunctions[distanceType];\n}\n\n/**\n * Compute the score based on the distance metric.\n * @param vectors The input vectors.\n * @param distanceMetric The distance metric.\n * @return The computed score.\n */\nexport function computeEvaluationScore(\n vectors: number[][],\n distanceMetric: EmbeddingDistanceType\n): number {\n const metricFunction = getDistanceCalculationFunction(distanceMetric);\n return metricFunction(vectors[0], vectors[1]);\n}\n\n/**\n * Use embedding distances to score semantic difference between\n * a prediction and reference.\n */\nexport class EmbeddingDistanceEvalChain\n extends StringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = true;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStrings(\n args: StringEvaluatorArgs,\n config: Callbacks | BaseCallbackConfig | undefined\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"reference\", \"prediction\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, reference } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n reference,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n\n/**\n * Use embedding distances to score semantic difference between two predictions.\n */\nexport class PairwiseEmbeddingDistanceEvalChain\n extends PairwiseStringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = false;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `pairwise_embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"prediction\", \"predictionB\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, predictionB } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n predictionB,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n"],"mappings":";;;;;;;;;;AAwDA,SAAgB,+BACd,cACgB;AAShB,
|
|
1
|
+
{"version":3,"file":"base.js","names":[],"sources":["../../../src/evaluation/embedding_distance/base.ts"],"sourcesContent":["import type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n CallbackManagerForChainRun,\n Callbacks,\n BaseCallbackConfig,\n} from \"@langchain/core/callbacks/manager\";\nimport {\n PairwiseStringEvaluator,\n PairwiseStringEvaluatorArgs,\n StringEvaluator,\n StringEvaluatorArgs,\n} from \"../base.js\";\nimport { cosine } from \"../../util/ml-distance/similarities.js\";\nimport { chebyshev, manhattan } from \"../../util/ml-distance/distances.js\";\nimport { euclidean } from \"../../util/ml-distance-euclidean/euclidean.js\";\n\n/**\n *\n * Embedding Distance Metric.\n *\n * COSINE: Cosine distance metric.\n * EUCLIDEAN: Euclidean distance metric.\n * MANHATTAN: Manhattan distance metric.\n * CHEBYSHEV: Chebyshev distance metric.\n * HAMMING: Hamming distance metric.\n */\nexport type EmbeddingDistanceType =\n | \"cosine\"\n | \"euclidean\"\n | \"manhattan\"\n | \"chebyshev\";\n\n/**\n * Embedding Distance Evaluation Chain Input.\n */\nexport interface EmbeddingDistanceEvalChainInput {\n /**\n * The embedding objects to vectorize the outputs.\n */\n embedding?: EmbeddingsInterface;\n\n /**\n * The distance metric to use\n * for comparing the embeddings.\n */\n distanceMetric?: EmbeddingDistanceType;\n}\n\ntype VectorFunction = (xVector: number[], yVector: number[]) => number;\n\n/**\n * Get the distance function for the given distance type.\n * @param distance The distance type.\n * @return The distance function.\n */\nexport function getDistanceCalculationFunction(\n distanceType: EmbeddingDistanceType\n): VectorFunction {\n const distanceFunctions: { [key in EmbeddingDistanceType]: VectorFunction } =\n {\n cosine: (X: number[], Y: number[]) => 1.0 - cosine(X, Y),\n euclidean,\n manhattan,\n chebyshev,\n };\n\n return distanceFunctions[distanceType];\n}\n\n/**\n * Compute the score based on the distance metric.\n * @param vectors The input vectors.\n * @param distanceMetric The distance metric.\n * @return The computed score.\n */\nexport function computeEvaluationScore(\n vectors: number[][],\n distanceMetric: EmbeddingDistanceType\n): number {\n const metricFunction = getDistanceCalculationFunction(distanceMetric);\n return metricFunction(vectors[0], vectors[1]);\n}\n\n/**\n * Use embedding distances to score semantic difference between\n * a prediction and reference.\n */\nexport class EmbeddingDistanceEvalChain\n extends StringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = true;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStrings(\n args: StringEvaluatorArgs,\n config: Callbacks | BaseCallbackConfig | undefined\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"reference\", \"prediction\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, reference } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n reference,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n\n/**\n * Use embedding distances to score semantic difference between two predictions.\n */\nexport class PairwiseEmbeddingDistanceEvalChain\n extends PairwiseStringEvaluator\n implements EmbeddingDistanceEvalChainInput\n{\n requiresReference = false;\n\n requiresInput = false;\n\n outputKey = \"score\";\n\n embedding?: EmbeddingsInterface;\n\n distanceMetric: EmbeddingDistanceType = \"cosine\";\n\n constructor(fields: EmbeddingDistanceEvalChainInput) {\n super();\n this.embedding = fields?.embedding;\n this.distanceMetric = fields?.distanceMetric || \"cosine\";\n }\n\n _chainType() {\n return `pairwise_embedding_${this.distanceMetric}_distance` as const;\n }\n\n async _evaluateStringPairs(\n args: PairwiseStringEvaluatorArgs,\n config?: Callbacks | BaseCallbackConfig\n ): Promise<ChainValues> {\n const result = await this.call(args, config);\n\n return { [this.outputKey]: result[this.outputKey] };\n }\n\n get inputKeys(): string[] {\n return [\"prediction\", \"predictionB\"];\n }\n\n get outputKeys(): string[] {\n return [this.outputKey];\n }\n\n async _call(\n values: ChainValues,\n _runManager: CallbackManagerForChainRun | undefined\n ): Promise<ChainValues> {\n const { prediction, predictionB } = values;\n\n if (!this.embedding) throw new Error(\"Embedding is undefined\");\n\n const vectors = await this.embedding.embedDocuments([\n prediction,\n predictionB,\n ]);\n\n const score = computeEvaluationScore(vectors, this.distanceMetric);\n\n return { [this.outputKey]: score };\n }\n}\n"],"mappings":";;;;;;;;;;AAwDA,SAAgB,+BACd,cACgB;AAShB,QAAO;EANH,SAAS,GAAa,MAAgB,IAAM,OAAO,GAAG,EAAE;EACxD;EACA;EACA;EAGoB,CAAC;;;;;;;;AAS3B,SAAgB,uBACd,SACA,gBACQ;AAER,QADuB,+BAA+B,eACjC,CAAC,QAAQ,IAAI,QAAQ,GAAG;;;;;;AAO/C,IAAa,6BAAb,cACU,gBAEV;CACE,oBAAoB;CAEpB,gBAAgB;CAEhB,YAAY;CAEZ;CAEA,iBAAwC;CAExC,YAAY,QAAyC;AACnD,SAAO;AACP,OAAK,YAAY,QAAQ;AACzB,OAAK,iBAAiB,QAAQ,kBAAkB;;CAGlD,aAAa;AACX,SAAO,aAAa,KAAK,eAAe;;CAG1C,MAAM,iBACJ,MACA,QACsB;EACtB,MAAM,SAAS,MAAM,KAAK,KAAK,MAAM,OAAO;AAE5C,SAAO,GAAG,KAAK,YAAY,OAAO,KAAK,YAAY;;CAGrD,IAAI,YAAsB;AACxB,SAAO,CAAC,aAAa,aAAa;;CAGpC,IAAI,aAAuB;AACzB,SAAO,CAAC,KAAK,UAAU;;CAGzB,MAAM,MACJ,QACA,aACsB;EACtB,MAAM,EAAE,YAAY,cAAc;AAElC,MAAI,CAAC,KAAK,UAAW,OAAM,IAAI,MAAM,yBAAyB;EAO9D,MAAM,QAAQ,uBAAuB,MALf,KAAK,UAAU,eAAe,CAClD,YACA,UACD,CAAC,EAE4C,KAAK,eAAe;AAElE,SAAO,GAAG,KAAK,YAAY,OAAO;;;;;;AAOtC,IAAa,qCAAb,cACU,wBAEV;CACE,oBAAoB;CAEpB,gBAAgB;CAEhB,YAAY;CAEZ;CAEA,iBAAwC;CAExC,YAAY,QAAyC;AACnD,SAAO;AACP,OAAK,YAAY,QAAQ;AACzB,OAAK,iBAAiB,QAAQ,kBAAkB;;CAGlD,aAAa;AACX,SAAO,sBAAsB,KAAK,eAAe;;CAGnD,MAAM,qBACJ,MACA,QACsB;EACtB,MAAM,SAAS,MAAM,KAAK,KAAK,MAAM,OAAO;AAE5C,SAAO,GAAG,KAAK,YAAY,OAAO,KAAK,YAAY;;CAGrD,IAAI,YAAsB;AACxB,SAAO,CAAC,cAAc,cAAc;;CAGtC,IAAI,aAAuB;AACzB,SAAO,CAAC,KAAK,UAAU;;CAGzB,MAAM,MACJ,QACA,aACsB;EACtB,MAAM,EAAE,YAAY,gBAAgB;AAEpC,MAAI,CAAC,KAAK,UAAW,OAAM,IAAI,MAAM,yBAAyB;EAO9D,MAAM,QAAQ,uBAAuB,MALf,KAAK,UAAU,eAAe,CAClD,YACA,YACD,CAAC,EAE4C,KAAK,eAAe;AAElE,SAAO,GAAG,KAAK,YAAY,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval_chain.cjs","names":["LLMChain","QA_PROMPT"],"sources":["../../../src/evaluation/qa/eval_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { QA_PROMPT } from \"./prompt.js\";\nimport { LLMChain, LLMChainInput } from \"../../chains/llm_chain.js\";\n\nexport interface EvaluateArgs {\n questionKey: string;\n answerKey: string;\n predictionKey: string;\n}\n\nconst eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\nexport class QAEvalChain extends LLMChain {\n static lc_name() {\n return \"QAEvalChain\";\n }\n\n static fromLlm(\n llm: BaseLanguageModelInterface,\n options: {\n prompt?: PromptTemplate;\n chainInput?: Omit<LLMChainInput, \"llm\">;\n } = {}\n ): QAEvalChain {\n const prompt = options.prompt || QA_PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"query\",\n \"answer\",\n \"result\",\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 new this({ llm, prompt, ...options.chainInput });\n }\n\n async evaluate(\n examples: ChainValues,\n predictions: ChainValues,\n args: EvaluateArgs = {\n questionKey: \"query\",\n answerKey: \"answer\",\n predictionKey: \"result\",\n }\n ): Promise<ChainValues> {\n const inputs = examples.map((example: ChainValues, i: number) => ({\n query: example[args.questionKey],\n answer: example[args.answerKey],\n result: predictions[i][args.predictionKey],\n }));\n\n return await this.apply(inputs);\n }\n}\n"],"mappings":";;;AAYA,MAAM,SAAS,IAAiB,OAC9B,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,GAAG,CAAC,OAAO,MAAM,GAAG,IAAI,EAAE,CAAC;AAExD,IAAa,cAAb,cAAiCA,kBAAAA,SAAS;CACxC,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO,QACL,KACA,UAGI,EAAE,EACO;EACb,MAAM,SAAS,QAAQ,UAAUC,eAAAA;EACjC,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"eval_chain.cjs","names":["LLMChain","QA_PROMPT"],"sources":["../../../src/evaluation/qa/eval_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { QA_PROMPT } from \"./prompt.js\";\nimport { LLMChain, LLMChainInput } from \"../../chains/llm_chain.js\";\n\nexport interface EvaluateArgs {\n questionKey: string;\n answerKey: string;\n predictionKey: string;\n}\n\nconst eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\nexport class QAEvalChain extends LLMChain {\n static lc_name() {\n return \"QAEvalChain\";\n }\n\n static fromLlm(\n llm: BaseLanguageModelInterface,\n options: {\n prompt?: PromptTemplate;\n chainInput?: Omit<LLMChainInput, \"llm\">;\n } = {}\n ): QAEvalChain {\n const prompt = options.prompt || QA_PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"query\",\n \"answer\",\n \"result\",\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 new this({ llm, prompt, ...options.chainInput });\n }\n\n async evaluate(\n examples: ChainValues,\n predictions: ChainValues,\n args: EvaluateArgs = {\n questionKey: \"query\",\n answerKey: \"answer\",\n predictionKey: \"result\",\n }\n ): Promise<ChainValues> {\n const inputs = examples.map((example: ChainValues, i: number) => ({\n query: example[args.questionKey],\n answer: example[args.answerKey],\n result: predictions[i][args.predictionKey],\n }));\n\n return await this.apply(inputs);\n }\n}\n"],"mappings":";;;AAYA,MAAM,SAAS,IAAiB,OAC9B,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,GAAG,CAAC,OAAO,MAAM,GAAG,IAAI,EAAE,CAAC;AAExD,IAAa,cAAb,cAAiCA,kBAAAA,SAAS;CACxC,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO,QACL,KACA,UAGI,EAAE,EACO;EACb,MAAM,SAAS,QAAQ,UAAUC,eAAAA;EACjC,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAAC,MAAM,mBAAmB,IAFQ,IAAI,OAAO,eAEP,CAAC,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,OAAO,iBAEV;AAEH,SAAO,IAAI,KAAK;GAAE;GAAK;GAAQ,GAAG,QAAQ;GAAY,CAAC;;CAGzD,MAAM,SACJ,UACA,aACA,OAAqB;EACnB,aAAa;EACb,WAAW;EACX,eAAe;EAChB,EACqB;EACtB,MAAM,SAAS,SAAS,KAAK,SAAsB,OAAe;GAChE,OAAO,QAAQ,KAAK;GACpB,QAAQ,QAAQ,KAAK;GACrB,QAAQ,YAAY,GAAG,KAAK;GAC7B,EAAE;AAEH,SAAO,MAAM,KAAK,MAAM,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eval_chain.js","names":[],"sources":["../../../src/evaluation/qa/eval_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { QA_PROMPT } from \"./prompt.js\";\nimport { LLMChain, LLMChainInput } from \"../../chains/llm_chain.js\";\n\nexport interface EvaluateArgs {\n questionKey: string;\n answerKey: string;\n predictionKey: string;\n}\n\nconst eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\nexport class QAEvalChain extends LLMChain {\n static lc_name() {\n return \"QAEvalChain\";\n }\n\n static fromLlm(\n llm: BaseLanguageModelInterface,\n options: {\n prompt?: PromptTemplate;\n chainInput?: Omit<LLMChainInput, \"llm\">;\n } = {}\n ): QAEvalChain {\n const prompt = options.prompt || QA_PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"query\",\n \"answer\",\n \"result\",\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 new this({ llm, prompt, ...options.chainInput });\n }\n\n async evaluate(\n examples: ChainValues,\n predictions: ChainValues,\n args: EvaluateArgs = {\n questionKey: \"query\",\n answerKey: \"answer\",\n predictionKey: \"result\",\n }\n ): Promise<ChainValues> {\n const inputs = examples.map((example: ChainValues, i: number) => ({\n query: example[args.questionKey],\n answer: example[args.answerKey],\n result: predictions[i][args.predictionKey],\n }));\n\n return await this.apply(inputs);\n }\n}\n"],"mappings":";;;AAYA,MAAM,SAAS,IAAiB,OAC9B,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,GAAG,CAAC,OAAO,MAAM,GAAG,IAAI,EAAE,CAAC;AAExD,IAAa,cAAb,cAAiC,SAAS;CACxC,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO,QACL,KACA,UAGI,EAAE,EACO;EACb,MAAM,SAAS,QAAQ,UAAU;EACjC,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"eval_chain.js","names":[],"sources":["../../../src/evaluation/qa/eval_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { QA_PROMPT } from \"./prompt.js\";\nimport { LLMChain, LLMChainInput } from \"../../chains/llm_chain.js\";\n\nexport interface EvaluateArgs {\n questionKey: string;\n answerKey: string;\n predictionKey: string;\n}\n\nconst eqSet = (xs: Set<string>, ys: Set<string>) =>\n xs.size === ys.size && [...xs].every((x) => ys.has(x));\n\nexport class QAEvalChain extends LLMChain {\n static lc_name() {\n return \"QAEvalChain\";\n }\n\n static fromLlm(\n llm: BaseLanguageModelInterface,\n options: {\n prompt?: PromptTemplate;\n chainInput?: Omit<LLMChainInput, \"llm\">;\n } = {}\n ): QAEvalChain {\n const prompt = options.prompt || QA_PROMPT;\n const expectedInputVars: Set<string> = new Set([\n \"query\",\n \"answer\",\n \"result\",\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 new this({ llm, prompt, ...options.chainInput });\n }\n\n async evaluate(\n examples: ChainValues,\n predictions: ChainValues,\n args: EvaluateArgs = {\n questionKey: \"query\",\n answerKey: \"answer\",\n predictionKey: \"result\",\n }\n ): Promise<ChainValues> {\n const inputs = examples.map((example: ChainValues, i: number) => ({\n query: example[args.questionKey],\n answer: example[args.answerKey],\n result: predictions[i][args.predictionKey],\n }));\n\n return await this.apply(inputs);\n }\n}\n"],"mappings":";;;AAYA,MAAM,SAAS,IAAiB,OAC9B,GAAG,SAAS,GAAG,QAAQ,CAAC,GAAG,GAAG,CAAC,OAAO,MAAM,GAAG,IAAI,EAAE,CAAC;AAExD,IAAa,cAAb,cAAiC,SAAS;CACxC,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO,QACL,KACA,UAGI,EAAE,EACO;EACb,MAAM,SAAS,QAAQ,UAAU;EACjC,MAAM,oBAAiC,IAAI,IAAI;GAC7C;GACA;GACA;GACD,CAAC;AAIF,MAAI,CAAC,MAAM,mBAAmB,IAFQ,IAAI,OAAO,eAEP,CAAC,CACzC,OAAM,IAAI,MACR,6BAA6B,CAAC,GAAG,kBAAkB,CAAC,YAClD,OAAO,iBAEV;AAEH,SAAO,IAAI,KAAK;GAAE;GAAK;GAAQ,GAAG,QAAQ;GAAY,CAAC;;CAGzD,MAAM,SACJ,UACA,aACA,OAAqB;EACnB,aAAa;EACb,WAAW;EACX,eAAe;EAChB,EACqB;EACtB,MAAM,SAAS,SAAS,KAAK,SAAsB,OAAe;GAChE,OAAO,QAAQ,KAAK;GACpB,QAAQ,QAAQ,KAAK;GACrB,QAAQ,YAAY,GAAG,KAAK;GAC7B,EAAE;AAEH,SAAO,MAAM,KAAK,MAAM,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.cjs","names":["PromptTemplate"],"sources":["../../../src/evaluation/qa/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst QA_TEMPLATE = `You are a teacher grading a quiz.\nYou are given a question, the student's answer, and the true answer, and are asked to score the student answer as either CORRECT or INCORRECT.\n\nExample Format:\nQUESTION: question here\nSTUDENT ANSWER: student's answer here\nTRUE ANSWER: true answer here\nGRADE: CORRECT or INCORRECT here\n\nGrade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin! \n\nQUESTION: {query}\nSTUDENT ANSWER: {result}\nTRUE ANSWER: {answer}\nGRADE:`;\nexport const QA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"result\", \"answer\"],\n template: QA_TEMPLATE,\n});\n\nconst SQL_TEMPLATE = `You are comparing a submitted answer to an expert answer on a given SQL coding question. Here is the data:\n[BEGIN DATA]\n***\n[Question]: {query}\n***\n[Expert]: {answer}\n***\n[Submission]: {result}\n***\n[END DATA]\nCompare the content and correctness of the submitted SQL with the expert answer. Ignore any differences in whitespace, style, or output column names. The submitted answer may either be correct or incorrect. Determine which case applies. First, explain in detail the similarities or differences between the expert answer and the submission, ignoring superficial aspects such as whitespace, style or output column names. Do not state the final answer in your initial explanation. Then, respond with either \"CORRECT\" or \"INCORRECT\" (without quotes or punctuation) on its own line. This should correspond to whether the submitted SQL and the expert answer are semantically the same or different, respectively. Then, repeat your final answer on a new line.`;\n\nexport const SQL_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"answer\", \"result\"],\n template: SQL_TEMPLATE,\n});\n"],"mappings":";AAiBA,MAAa,4BAA4B,
|
|
1
|
+
{"version":3,"file":"prompt.cjs","names":["PromptTemplate"],"sources":["../../../src/evaluation/qa/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst QA_TEMPLATE = `You are a teacher grading a quiz.\nYou are given a question, the student's answer, and the true answer, and are asked to score the student answer as either CORRECT or INCORRECT.\n\nExample Format:\nQUESTION: question here\nSTUDENT ANSWER: student's answer here\nTRUE ANSWER: true answer here\nGRADE: CORRECT or INCORRECT here\n\nGrade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin! \n\nQUESTION: {query}\nSTUDENT ANSWER: {result}\nTRUE ANSWER: {answer}\nGRADE:`;\nexport const QA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"result\", \"answer\"],\n template: QA_TEMPLATE,\n});\n\nconst SQL_TEMPLATE = `You are comparing a submitted answer to an expert answer on a given SQL coding question. Here is the data:\n[BEGIN DATA]\n***\n[Question]: {query}\n***\n[Expert]: {answer}\n***\n[Submission]: {result}\n***\n[END DATA]\nCompare the content and correctness of the submitted SQL with the expert answer. Ignore any differences in whitespace, style, or output column names. The submitted answer may either be correct or incorrect. Determine which case applies. First, explain in detail the similarities or differences between the expert answer and the submission, ignoring superficial aspects such as whitespace, style or output column names. Do not state the final answer in your initial explanation. Then, respond with either \"CORRECT\" or \"INCORRECT\" (without quotes or punctuation) on its own line. This should correspond to whether the submitted SQL and the expert answer are semantically the same or different, respectively. Then, repeat your final answer on a new line.`;\n\nexport const SQL_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"answer\", \"result\"],\n template: SQL_TEMPLATE,\n});\n"],"mappings":";AAiBA,MAAa,4BAA4B,uCAAIA,EAAAA,eAAe;CAC1D,gBAAgB;EAAC;EAAS;EAAU;EAAS;CAC7C,UAAU;;;;;;;;;;;;;;;CACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/qa/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst QA_TEMPLATE = `You are a teacher grading a quiz.\nYou are given a question, the student's answer, and the true answer, and are asked to score the student answer as either CORRECT or INCORRECT.\n\nExample Format:\nQUESTION: question here\nSTUDENT ANSWER: student's answer here\nTRUE ANSWER: true answer here\nGRADE: CORRECT or INCORRECT here\n\nGrade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin! \n\nQUESTION: {query}\nSTUDENT ANSWER: {result}\nTRUE ANSWER: {answer}\nGRADE:`;\nexport const QA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"result\", \"answer\"],\n template: QA_TEMPLATE,\n});\n\nconst SQL_TEMPLATE = `You are comparing a submitted answer to an expert answer on a given SQL coding question. Here is the data:\n[BEGIN DATA]\n***\n[Question]: {query}\n***\n[Expert]: {answer}\n***\n[Submission]: {result}\n***\n[END DATA]\nCompare the content and correctness of the submitted SQL with the expert answer. Ignore any differences in whitespace, style, or output column names. The submitted answer may either be correct or incorrect. Determine which case applies. First, explain in detail the similarities or differences between the expert answer and the submission, ignoring superficial aspects such as whitespace, style or output column names. Do not state the final answer in your initial explanation. Then, respond with either \"CORRECT\" or \"INCORRECT\" (without quotes or punctuation) on its own line. This should correspond to whether the submitted SQL and the expert answer are semantically the same or different, respectively. Then, repeat your final answer on a new line.`;\n\nexport const SQL_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"answer\", \"result\"],\n template: SQL_TEMPLATE,\n});\n"],"mappings":";AAiBA,MAAa,4BAA4B,IAAI,eAAe;CAC1D,gBAAgB;EAAC;EAAS;EAAU;EAAS;CAC7C,
|
|
1
|
+
{"version":3,"file":"prompt.js","names":[],"sources":["../../../src/evaluation/qa/prompt.ts"],"sourcesContent":["import { PromptTemplate } from \"@langchain/core/prompts\";\n\nconst QA_TEMPLATE = `You are a teacher grading a quiz.\nYou are given a question, the student's answer, and the true answer, and are asked to score the student answer as either CORRECT or INCORRECT.\n\nExample Format:\nQUESTION: question here\nSTUDENT ANSWER: student's answer here\nTRUE ANSWER: true answer here\nGRADE: CORRECT or INCORRECT here\n\nGrade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin! \n\nQUESTION: {query}\nSTUDENT ANSWER: {result}\nTRUE ANSWER: {answer}\nGRADE:`;\nexport const QA_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"result\", \"answer\"],\n template: QA_TEMPLATE,\n});\n\nconst SQL_TEMPLATE = `You are comparing a submitted answer to an expert answer on a given SQL coding question. Here is the data:\n[BEGIN DATA]\n***\n[Question]: {query}\n***\n[Expert]: {answer}\n***\n[Submission]: {result}\n***\n[END DATA]\nCompare the content and correctness of the submitted SQL with the expert answer. Ignore any differences in whitespace, style, or output column names. The submitted answer may either be correct or incorrect. Determine which case applies. First, explain in detail the similarities or differences between the expert answer and the submission, ignoring superficial aspects such as whitespace, style or output column names. Do not state the final answer in your initial explanation. Then, respond with either \"CORRECT\" or \"INCORRECT\" (without quotes or punctuation) on its own line. This should correspond to whether the submitted SQL and the expert answer are semantically the same or different, respectively. Then, repeat your final answer on a new line.`;\n\nexport const SQL_PROMPT = /* #__PURE__ */ new PromptTemplate({\n inputVariables: [\"query\", \"answer\", \"result\"],\n template: SQL_TEMPLATE,\n});\n"],"mappings":";AAiBA,MAAa,4BAA4B,IAAI,eAAe;CAC1D,gBAAgB;EAAC;EAAS;EAAU;EAAS;CAC7C,UAAU;;;;;;;;;;;;;;;CACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.cjs","names":["AutoGPTOutputParser","LLMChain","AutoGPTPrompt","HumanMessage","AIMessage","SystemMessage"],"sources":["../../../src/experimental/autogpt/agent.ts"],"sourcesContent":["import type { VectorStoreRetrieverInterface } from \"@langchain/core/vectorstores\";\nimport { Tool } from \"@langchain/core/tools\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n SystemMessage,\n} from \"@langchain/core/messages\";\nimport { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n getEmbeddingContextSize,\n getModelContextSize,\n} from \"@langchain/core/language_models/base\";\nimport { LLMChain } from \"../../chains/llm_chain.js\";\n\nimport { AutoGPTOutputParser } from \"./output_parser.js\";\nimport { AutoGPTPrompt } from \"./prompt.js\";\n// import { HumanInputRun } from \"./tools/human/tool\"; // TODO\nimport { ObjectTool, FINISH_NAME } from \"./schema.js\";\nimport { TokenTextSplitter } from \"../../text_splitter.js\";\n\n/**\n * Interface for the input parameters of the AutoGPT class.\n */\nexport interface AutoGPTInput {\n aiName: string;\n aiRole: string;\n memory: VectorStoreRetrieverInterface;\n humanInTheLoop?: boolean;\n outputParser?: AutoGPTOutputParser;\n maxIterations?: number;\n}\n\n/**\n * Class representing the AutoGPT concept with LangChain primitives. It is\n * designed to be used with a set of tools such as a search tool,\n * write-file tool, and a read-file tool.\n * @example\n * ```typescript\n * const autogpt = AutoGPT.fromLLMAndTools(\n * new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0 }),\n * [\n * new ReadFileTool({ store: new InMemoryFileStore() }),\n * new WriteFileTool({ store: new InMemoryFileStore() }),\n * new SerpAPI(\"YOUR_SERPAPI_API_KEY\", {\n * location: \"San Francisco,California,United States\",\n * hl: \"en\",\n * gl: \"us\",\n * }),\n * ],\n * {\n * memory: new MemoryVectorStore(new OpenAIEmbeddings()).asRetriever(),\n * aiName: \"Tom\",\n * aiRole: \"Assistant\",\n * },\n * );\n * const result = await autogpt.run([\"write a weather report for SF today\"]);\n * ```\n */\nexport class AutoGPT {\n aiName: string;\n\n memory: VectorStoreRetrieverInterface;\n\n fullMessageHistory: BaseMessage[];\n\n nextActionCount: number;\n\n chain: LLMChain;\n\n outputParser: AutoGPTOutputParser;\n\n tools: ObjectTool[];\n\n feedbackTool?: Tool;\n\n maxIterations: number;\n\n // Currently not generic enough to support any text splitter.\n textSplitter: TokenTextSplitter;\n\n constructor({\n aiName,\n memory,\n chain,\n outputParser,\n tools,\n feedbackTool,\n maxIterations,\n }: Omit<Required<AutoGPTInput>, \"aiRole\" | \"humanInTheLoop\"> & {\n chain: LLMChain;\n tools: ObjectTool[];\n feedbackTool?: Tool;\n }) {\n this.aiName = aiName;\n this.memory = memory;\n this.fullMessageHistory = [];\n this.nextActionCount = 0;\n this.chain = chain;\n this.outputParser = outputParser;\n this.tools = tools;\n this.feedbackTool = feedbackTool;\n this.maxIterations = maxIterations;\n const chunkSize = getEmbeddingContextSize(\n \"modelName\" in memory.vectorStore.embeddings\n ? (memory.vectorStore.embeddings.modelName as string)\n : undefined\n );\n this.textSplitter = new TokenTextSplitter({\n chunkSize,\n chunkOverlap: Math.round(chunkSize / 10),\n });\n }\n\n /**\n * Creates a new AutoGPT instance from a given LLM and a set of tools.\n * @param llm A BaseChatModel object.\n * @param tools An array of ObjectTool objects.\n * @param options.aiName The name of the AI.\n * @param options.aiRole The role of the AI.\n * @param options.memory A VectorStoreRetriever object that represents the memory of the AI.\n * @param options.maxIterations The maximum number of iterations the AI can perform.\n * @param options.outputParser An AutoGPTOutputParser object that parses the output of the AI.\n * @returns A new instance of the AutoGPT class.\n */\n static fromLLMAndTools(\n llm: BaseChatModel,\n tools: ObjectTool[],\n {\n aiName,\n aiRole,\n memory,\n maxIterations = 100,\n // humanInTheLoop = false,\n outputParser = new AutoGPTOutputParser(),\n }: AutoGPTInput\n ): AutoGPT {\n const prompt = new AutoGPTPrompt({\n aiName,\n aiRole,\n tools,\n tokenCounter: llm.getNumTokens.bind(llm),\n sendTokenLimit: getModelContextSize(\n \"modelName\" in llm ? (llm.modelName as string) : \"gpt2\"\n ),\n });\n // const feedbackTool = humanInTheLoop ? new HumanInputRun() : null;\n const chain = new LLMChain({ llm, prompt });\n return new AutoGPT({\n aiName,\n memory,\n chain,\n outputParser,\n tools,\n // feedbackTool,\n maxIterations,\n });\n }\n\n /**\n * Runs the AI with a given set of goals.\n * @param goals An array of strings representing the goals.\n * @returns A string representing the result of the run or undefined if the maximum number of iterations is reached without a result.\n */\n async run(goals: string[]): Promise<string | undefined> {\n const user_input =\n \"Determine which next command to use, and respond using the format specified above:\";\n let loopCount = 0;\n while (loopCount < this.maxIterations) {\n loopCount += 1;\n\n const { text: assistantReply } = await this.chain.call({\n goals,\n user_input,\n memory: this.memory,\n messages: this.fullMessageHistory,\n });\n\n // Print the assistant reply\n console.log(assistantReply);\n this.fullMessageHistory.push(new HumanMessage(user_input));\n this.fullMessageHistory.push(new AIMessage(assistantReply));\n\n const action = await this.outputParser.parse(assistantReply);\n const tools = this.tools.reduce(\n (acc, tool) => ({ ...acc, [tool.name]: tool }),\n {} as { [key: string]: ObjectTool }\n );\n if (action.name === FINISH_NAME) {\n return action.args.response;\n }\n let result: string;\n if (action.name in tools) {\n const tool = tools[action.name];\n let observation;\n try {\n observation = await tool.call(action.args);\n } catch (e) {\n observation = `Error in args: ${e}`;\n }\n result = `Command ${tool.name} returned: ${observation}`;\n } else if (action.name === \"ERROR\") {\n result = `Error: ${action.args}. `;\n } else {\n result = `Unknown command '${action.name}'. Please refer to the 'COMMANDS' list for available commands and only respond in the specified JSON format.`;\n }\n\n let memoryToAdd = `Assistant Reply: ${assistantReply}\\nResult: ${result} `;\n if (this.feedbackTool) {\n const feedback = `\\n${await this.feedbackTool.call(\"Input: \")}`;\n if (feedback === \"q\" || feedback === \"stop\") {\n console.log(\"EXITING\");\n return \"EXITING\";\n }\n memoryToAdd += feedback;\n }\n\n const documents = await this.textSplitter.createDocuments([memoryToAdd]);\n await this.memory.addDocuments(documents);\n this.fullMessageHistory.push(new SystemMessage(result));\n }\n\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA,IAAa,UAAb,MAAa,QAAQ;CACnB;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAGA;CAEA,YAAY,EACV,QACA,QACA,OACA,cACA,OACA,cACA,iBAKC;AACD,OAAK,SAAS;AACd,OAAK,SAAS;AACd,OAAK,qBAAqB,EAAE;AAC5B,OAAK,kBAAkB;AACvB,OAAK,QAAQ;AACb,OAAK,eAAe;AACpB,OAAK,QAAQ;AACb,OAAK,eAAe;AACpB,OAAK,gBAAgB;EACrB,MAAM,aAAA,GAAA,qCAAA,yBACJ,eAAe,OAAO,YAAY,aAC7B,OAAO,YAAY,WAAW,YAC/B,KAAA,EACL;AACD,OAAK,eAAe,IAAA,sBAAA,sBAAI,kBAAkB;GACxC;GACA,cAAc,KAAK,MAAM,YAAY,GAAG;GACzC,CAAC;;;;;;;;;;;;;CAcJ,OAAO,gBACL,KACA,OACA,EACE,QACA,QACA,QACA,gBAAgB,KAEhB,eAAe,IAAIA,sBAAAA,qBAAqB,IAEjC;AAYT,SAAO,IAAI,QAAQ;GACjB;GACA;GACA,
|
|
1
|
+
{"version":3,"file":"agent.cjs","names":["AutoGPTOutputParser","LLMChain","AutoGPTPrompt","HumanMessage","AIMessage","SystemMessage"],"sources":["../../../src/experimental/autogpt/agent.ts"],"sourcesContent":["import type { VectorStoreRetrieverInterface } from \"@langchain/core/vectorstores\";\nimport { Tool } from \"@langchain/core/tools\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n SystemMessage,\n} from \"@langchain/core/messages\";\nimport { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport {\n getEmbeddingContextSize,\n getModelContextSize,\n} from \"@langchain/core/language_models/base\";\nimport { LLMChain } from \"../../chains/llm_chain.js\";\n\nimport { AutoGPTOutputParser } from \"./output_parser.js\";\nimport { AutoGPTPrompt } from \"./prompt.js\";\n// import { HumanInputRun } from \"./tools/human/tool\"; // TODO\nimport { ObjectTool, FINISH_NAME } from \"./schema.js\";\nimport { TokenTextSplitter } from \"../../text_splitter.js\";\n\n/**\n * Interface for the input parameters of the AutoGPT class.\n */\nexport interface AutoGPTInput {\n aiName: string;\n aiRole: string;\n memory: VectorStoreRetrieverInterface;\n humanInTheLoop?: boolean;\n outputParser?: AutoGPTOutputParser;\n maxIterations?: number;\n}\n\n/**\n * Class representing the AutoGPT concept with LangChain primitives. It is\n * designed to be used with a set of tools such as a search tool,\n * write-file tool, and a read-file tool.\n * @example\n * ```typescript\n * const autogpt = AutoGPT.fromLLMAndTools(\n * new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0 }),\n * [\n * new ReadFileTool({ store: new InMemoryFileStore() }),\n * new WriteFileTool({ store: new InMemoryFileStore() }),\n * new SerpAPI(\"YOUR_SERPAPI_API_KEY\", {\n * location: \"San Francisco,California,United States\",\n * hl: \"en\",\n * gl: \"us\",\n * }),\n * ],\n * {\n * memory: new MemoryVectorStore(new OpenAIEmbeddings()).asRetriever(),\n * aiName: \"Tom\",\n * aiRole: \"Assistant\",\n * },\n * );\n * const result = await autogpt.run([\"write a weather report for SF today\"]);\n * ```\n */\nexport class AutoGPT {\n aiName: string;\n\n memory: VectorStoreRetrieverInterface;\n\n fullMessageHistory: BaseMessage[];\n\n nextActionCount: number;\n\n chain: LLMChain;\n\n outputParser: AutoGPTOutputParser;\n\n tools: ObjectTool[];\n\n feedbackTool?: Tool;\n\n maxIterations: number;\n\n // Currently not generic enough to support any text splitter.\n textSplitter: TokenTextSplitter;\n\n constructor({\n aiName,\n memory,\n chain,\n outputParser,\n tools,\n feedbackTool,\n maxIterations,\n }: Omit<Required<AutoGPTInput>, \"aiRole\" | \"humanInTheLoop\"> & {\n chain: LLMChain;\n tools: ObjectTool[];\n feedbackTool?: Tool;\n }) {\n this.aiName = aiName;\n this.memory = memory;\n this.fullMessageHistory = [];\n this.nextActionCount = 0;\n this.chain = chain;\n this.outputParser = outputParser;\n this.tools = tools;\n this.feedbackTool = feedbackTool;\n this.maxIterations = maxIterations;\n const chunkSize = getEmbeddingContextSize(\n \"modelName\" in memory.vectorStore.embeddings\n ? (memory.vectorStore.embeddings.modelName as string)\n : undefined\n );\n this.textSplitter = new TokenTextSplitter({\n chunkSize,\n chunkOverlap: Math.round(chunkSize / 10),\n });\n }\n\n /**\n * Creates a new AutoGPT instance from a given LLM and a set of tools.\n * @param llm A BaseChatModel object.\n * @param tools An array of ObjectTool objects.\n * @param options.aiName The name of the AI.\n * @param options.aiRole The role of the AI.\n * @param options.memory A VectorStoreRetriever object that represents the memory of the AI.\n * @param options.maxIterations The maximum number of iterations the AI can perform.\n * @param options.outputParser An AutoGPTOutputParser object that parses the output of the AI.\n * @returns A new instance of the AutoGPT class.\n */\n static fromLLMAndTools(\n llm: BaseChatModel,\n tools: ObjectTool[],\n {\n aiName,\n aiRole,\n memory,\n maxIterations = 100,\n // humanInTheLoop = false,\n outputParser = new AutoGPTOutputParser(),\n }: AutoGPTInput\n ): AutoGPT {\n const prompt = new AutoGPTPrompt({\n aiName,\n aiRole,\n tools,\n tokenCounter: llm.getNumTokens.bind(llm),\n sendTokenLimit: getModelContextSize(\n \"modelName\" in llm ? (llm.modelName as string) : \"gpt2\"\n ),\n });\n // const feedbackTool = humanInTheLoop ? new HumanInputRun() : null;\n const chain = new LLMChain({ llm, prompt });\n return new AutoGPT({\n aiName,\n memory,\n chain,\n outputParser,\n tools,\n // feedbackTool,\n maxIterations,\n });\n }\n\n /**\n * Runs the AI with a given set of goals.\n * @param goals An array of strings representing the goals.\n * @returns A string representing the result of the run or undefined if the maximum number of iterations is reached without a result.\n */\n async run(goals: string[]): Promise<string | undefined> {\n const user_input =\n \"Determine which next command to use, and respond using the format specified above:\";\n let loopCount = 0;\n while (loopCount < this.maxIterations) {\n loopCount += 1;\n\n const { text: assistantReply } = await this.chain.call({\n goals,\n user_input,\n memory: this.memory,\n messages: this.fullMessageHistory,\n });\n\n // Print the assistant reply\n console.log(assistantReply);\n this.fullMessageHistory.push(new HumanMessage(user_input));\n this.fullMessageHistory.push(new AIMessage(assistantReply));\n\n const action = await this.outputParser.parse(assistantReply);\n const tools = this.tools.reduce(\n (acc, tool) => ({ ...acc, [tool.name]: tool }),\n {} as { [key: string]: ObjectTool }\n );\n if (action.name === FINISH_NAME) {\n return action.args.response;\n }\n let result: string;\n if (action.name in tools) {\n const tool = tools[action.name];\n let observation;\n try {\n observation = await tool.call(action.args);\n } catch (e) {\n observation = `Error in args: ${e}`;\n }\n result = `Command ${tool.name} returned: ${observation}`;\n } else if (action.name === \"ERROR\") {\n result = `Error: ${action.args}. `;\n } else {\n result = `Unknown command '${action.name}'. Please refer to the 'COMMANDS' list for available commands and only respond in the specified JSON format.`;\n }\n\n let memoryToAdd = `Assistant Reply: ${assistantReply}\\nResult: ${result} `;\n if (this.feedbackTool) {\n const feedback = `\\n${await this.feedbackTool.call(\"Input: \")}`;\n if (feedback === \"q\" || feedback === \"stop\") {\n console.log(\"EXITING\");\n return \"EXITING\";\n }\n memoryToAdd += feedback;\n }\n\n const documents = await this.textSplitter.createDocuments([memoryToAdd]);\n await this.memory.addDocuments(documents);\n this.fullMessageHistory.push(new SystemMessage(result));\n }\n\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA,IAAa,UAAb,MAAa,QAAQ;CACnB;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;CAGA;CAEA,YAAY,EACV,QACA,QACA,OACA,cACA,OACA,cACA,iBAKC;AACD,OAAK,SAAS;AACd,OAAK,SAAS;AACd,OAAK,qBAAqB,EAAE;AAC5B,OAAK,kBAAkB;AACvB,OAAK,QAAQ;AACb,OAAK,eAAe;AACpB,OAAK,QAAQ;AACb,OAAK,eAAe;AACpB,OAAK,gBAAgB;EACrB,MAAM,aAAA,GAAA,qCAAA,yBACJ,eAAe,OAAO,YAAY,aAC7B,OAAO,YAAY,WAAW,YAC/B,KAAA,EACL;AACD,OAAK,eAAe,IAAA,sBAAA,sBAAI,kBAAkB;GACxC;GACA,cAAc,KAAK,MAAM,YAAY,GAAG;GACzC,CAAC;;;;;;;;;;;;;CAcJ,OAAO,gBACL,KAC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