From 32c1579179da4373b5ede1e7536c2efe04c366c7 Mon Sep 17 00:00:00 2001 From: Ivan Charapanau Date: Wed, 25 Sep 2024 20:34:55 +0200 Subject: [PATCH] feat: eli5 module --- boost/src/config.py | 17 ++++++++++ boost/src/modules/eli5.py | 66 +++++++++++++++++++++++++++++++++++++++ profiles/default.env | 2 +- 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 boost/src/modules/eli5.py diff --git a/boost/src/config.py b/boost/src/config.py index 5b9a06a..0243860 100644 --- a/boost/src/config.py +++ b/boost/src/config.py @@ -385,3 +385,20 @@ def _convert_value(self, value: str) -> T: default='1.414', description='The exploration constant for the MCTS algorithm' ) + +# ----------------- ELI5 ----------------- + +ELI5_STRAT = Config[str]( + name='HARBOR_ELI5_STRAT', + type=str, + default='match', + description='The strategy that selects messages to target for the eli5 module' +) + +ELI5_STRAT_PARAMS = Config[ConfigDict]( + name='HARBOR_ELI5_STRAT_PARAMS', + type=ConfigDict, + # Default - last user message + default='role=user,index=-1', + description='Parameters for eli5 message selection' +) diff --git a/boost/src/modules/eli5.py b/boost/src/modules/eli5.py new file mode 100644 index 0000000..75fd885 --- /dev/null +++ b/boost/src/modules/eli5.py @@ -0,0 +1,66 @@ +import llm +import log +import chat as ch +import config +import selection + +ID_PREFIX = "eli5" +logger = log.setup_logger(ID_PREFIX) + +eli5_prompt = """ +My friend asked me this question: "{question}". +Explain it to me like I'm stupid. Explain every word and its specific impact on the question. +Do not asnwer the question, though, I want to figure it out myself. +I just need a simpler explanation thats easy to understand and follow. +""".strip() + +answer_prompt = """ + +Given the initial question and its dedetailed explanation, provide the answer to the question. + + + +{question} + + + +{explanation} + +""".strip() + + +async def apply(chat: 'ch.Chat', llm: 'llm.LLM'): + strat = config.ELI5_STRAT.value + strat_params = config.ELI5_STRAT_PARAMS.value + debug_info = { + "strat": strat, + "strat_params": strat_params, + } + logger.debug(f"{ID_PREFIX}: {debug_info}") + + nodes = selection.apply_strategy(chat, strategy=strat, params=strat_params) + + if (len(nodes) > 1): + logger.warning( + f"{ID_PREFIX}: Matched multiple nodes, only the first one will be processed." + ) + + if len(nodes) == 0: + log.info(f"{ID_PREFIX}: No nodes matched, skipping.") + return await llm.stream_final_completion() + + node = nodes[0] + question = node.content + + await llm.emit_status("Explaining the question to myself...") + explanation = await llm.stream_chat_completion( + prompt=eli5_prompt.format(question=question), + ) + + await llm.emit_status('ELI5 Response') + await llm.stream_final_completion( + prompt=answer_prompt.format( + question=question, + explanation=explanation, + ) + ) diff --git a/profiles/default.env b/profiles/default.env index 0c07902..26d3665 100644 --- a/profiles/default.env +++ b/profiles/default.env @@ -323,7 +323,7 @@ HARBOR_KTRANSFORMERS_EXTRA_ARGS="" HARBOR_BOOST_HOST_PORT=34131 HARBOR_BOOST_OPENAI_URLS="" HARBOR_BOOST_OPENAI_KEYS="" -HARBOR_BOOST_MODULES="klmbr;rcn;g1;mcts" +HARBOR_BOOST_MODULES="klmbr;rcn;g1;mcts;eli5" HARBOR_BOOST_MODULE_FOLDERS="modules;custom_modules" HARBOR_BOOST_INTERMEDIATE_OUTPUT=true HARBOR_BOOST_STATUS_STYLE="md:codeblock"