From 8f9c1cd7113a665507ddf640e5c05bae3f122dd9 Mon Sep 17 00:00:00 2001 From: zarathustra Date: Tue, 18 Mar 2025 19:24:43 +0100 Subject: [PATCH] feat: add most recent chat history to system prompt --- packages/packages.json | 6 ++--- .../agents/asylum_abci_app/aea-config.yaml | 2 +- .../services/asylum_abci_app/service.yaml | 2 +- .../skills/asylum_abci_app/behaviours.py | 24 ++++++++++++++++++- .../skills/asylum_abci_app/handlers.py | 18 +++++--------- .../skills/asylum_abci_app/skill.yaml | 4 ++-- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/packages/packages.json b/packages/packages.json index 0840a2d..762e487 100644 --- a/packages/packages.json +++ b/packages/packages.json @@ -4,10 +4,10 @@ "protocol/zarathustra/llm_chat_completion/1.0.0": "bafybeifdp2rwfar5vbs33p56dlin76v62p5fdvspwnpasraunp2wa7gxo4", "connection/eightballer/telegram_wrapper/0.1.0": "bafybeib2sm5p4wtnerutf3y4wzzkk2hhzbl5tf3z5crk3xxyangxngsqjm", "connection/zarathustra/openai_api/0.1.0": "bafybeihfzkq5mxzf54kgtbrlgrbwd5l2osixnur7e6iszumla3vbgoucue", - "skill/zarathustra/asylum_abci_app/0.1.0": "bafybeibrxsojugb57vcgxqcdnl6h56tl5c5mwfsk42tsyupcfzpz2xne2i", + "skill/zarathustra/asylum_abci_app/0.1.0": "bafybeie44mlapcbl766xd6fc26qgrsemxbhzdy4hs3ric5p5w2dcf75wku", "agent/eightballer/telegram_bot/0.1.0": "bafybeifwijoua3m4zgd7beyexpzcvq5opyzb5bbnb6e2uhi5rpnugmq2e4", - "agent/zarathustra/asylum_abci_app/0.1.0": "bafybeiefn5zeahzvwg2bv4tafbburpofs4smddu5ihryk2h3ecab6cswci", - "service/zarathustra/asylum_abci_app/0.1.0": "bafybeihot3aklfwflzvsme7g4lhip3d6z3hfhkkehiy4cumy5b2ui54ooa" + "agent/zarathustra/asylum_abci_app/0.1.0": "bafybeiaf7ef624jm5cxhr5vzxh6v7o66zlo7hpaclls7nstq3lappid6wu", + "service/zarathustra/asylum_abci_app/0.1.0": "bafybeidgvm25zbqbbynzcg64dpnmjxxjqca4deofatdgl5f6tifrwezdny" }, "third_party": { "protocol/eightballer/default/0.1.0": "bafybeicsdb3bue2xoopc6lue7njtyt22nehrnkevmkuk2i6ac65w722vwy", diff --git a/packages/zarathustra/agents/asylum_abci_app/aea-config.yaml b/packages/zarathustra/agents/asylum_abci_app/aea-config.yaml index ee968ef..c151315 100644 --- a/packages/zarathustra/agents/asylum_abci_app/aea-config.yaml +++ b/packages/zarathustra/agents/asylum_abci_app/aea-config.yaml @@ -25,7 +25,7 @@ protocols: - open_aea/signing:1.0.0:bafybeig2d36zxy65vd7fwhs7scotuktydcarm74aprmrb5nioiymr3yixm skills: - eightballer/prometheus:0.1.0:bafybeicl5i7e467aowfarke4bbyixo2dggar276njmvyuwbsby5pxshhtu -- zarathustra/asylum_abci_app:0.1.0:bafybeibrxsojugb57vcgxqcdnl6h56tl5c5mwfsk42tsyupcfzpz2xne2i +- zarathustra/asylum_abci_app:0.1.0:bafybeie44mlapcbl766xd6fc26qgrsemxbhzdy4hs3ric5p5w2dcf75wku customs: [] default_ledger: ethereum required_ledgers: diff --git a/packages/zarathustra/services/asylum_abci_app/service.yaml b/packages/zarathustra/services/asylum_abci_app/service.yaml index 932a62a..352f01a 100644 --- a/packages/zarathustra/services/asylum_abci_app/service.yaml +++ b/packages/zarathustra/services/asylum_abci_app/service.yaml @@ -10,7 +10,7 @@ fingerprint: tests/__init__.py: bafybeibkiew5ph62jf7mvavzjtwqozwivuniwpov6oi2hne4xvwa23pmau tests/test_service.py: bafybeicslmnjnf43pbzmwlqr7p7xw2iktcrnuq6l7klkrc5r4yberg66u4 fingerprint_ignore_patterns: [] -agent: zarathustra/asylum_abci_app:0.1.0:bafybeiefn5zeahzvwg2bv4tafbburpofs4smddu5ihryk2h3ecab6cswci +agent: zarathustra/asylum_abci_app:0.1.0:bafybeiaf7ef624jm5cxhr5vzxh6v7o66zlo7hpaclls7nstq3lappid6wu number_of_agents: 4 deployment: agent: diff --git a/packages/zarathustra/skills/asylum_abci_app/behaviours.py b/packages/zarathustra/skills/asylum_abci_app/behaviours.py index 4dcbf6f..9a8220b 100644 --- a/packages/zarathustra/skills/asylum_abci_app/behaviours.py +++ b/packages/zarathustra/skills/asylum_abci_app/behaviours.py @@ -19,6 +19,7 @@ """This package contains the behaviours for the AsylumAbciApp.""" import os +import re import json import functools from abc import ABC @@ -53,6 +54,7 @@ TELEGRAM_MSG_CHAR_LIMIT = 4_000 MERMAID_DIAGRAMS = Path("specs") / "fsms" / "mermaid" SPONSOR_BOUNTY_DATA = Path("bounties") / "sponsor_bounties.json" +BOT_PATTERN = re.compile(r"(🤖.*?🤖)") @functools.lru_cache @@ -95,6 +97,21 @@ def clean_str(s: str) -> str: return f"Sponsor: {target_sponsor}\nBounty: {bounty_key}\nDescription:{description}\n" +def get_chat_history(context) -> tuple[str, set[str]]: + """Get most recent telegram chat history.""" + authors = set() + messages = [] + for text in reversed(context.asylum_strategy.chat_history): + if bot_match := BOT_PATTERN.search(text): + authors.add(bot_match.group(1)) + messages.append(text) + else: + authors.add("Human") + messages.append(f"Human agent says:\n{text}") + agent_conversation = "\n\n".join(f"{i}. {msg}" for i, msg in enumerate(messages)) + return agent_conversation, authors + + USER_PERSONA_PROMPT = dedent(""" I have a dataset of GitHub issues and discussions related to {github_repositories}. I want you to analyze and summarize the contributions of the main participants to infer their technical personas. @@ -176,6 +193,9 @@ def clean_str(s: str) -> str: - **Never break character.** - **All responses must reflect the perspective of your real-world counterpart.** - Ensure the FSM diagram is under {telegram_msg_char_limit} characters so it fits within Telegram's message limit. If needed, simplify state names or remove unnecessary transitions while keeping the happy path intact. + + ### Most recent message exchange leading up to this point (latest messages appear at the top): + {chat_history} """) # noqa: E501 @@ -307,7 +327,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__(**kwargs) self._state = AsylumAbciAppStates.REQUEST_LLM_RESPONSE_ROUND - def act(self) -> None: + def act(self) -> None: # noqa: PLR0914 """Act.""" self.sponsor_bounty_info = get_bounty_info(self.context) self.context.logger.info(f"In state: {self._state}") @@ -357,6 +377,7 @@ def act(self) -> None: model = LLMModel.META_LLAMA_3_3_70B_INSTRUCT github_username = self.agent_persona.github_username user_persona = self.context.asylum_strategy.user_persona + chat_history, _ = get_chat_history(self.context) self.context.logger.info(f"I AM: {user_persona}") content = [ Message( @@ -368,6 +389,7 @@ def act(self) -> None: telegram_msg_char_limit=TELEGRAM_MSG_CHAR_LIMIT, sponsor_bounty_info=self.sponsor_bounty_info, mermaid_diagram_examples=mermaid_diagram_examples, + chat_history=chat_history, ), ), Message(role=Role.USER, content=text_data, name=username), diff --git a/packages/zarathustra/skills/asylum_abci_app/handlers.py b/packages/zarathustra/skills/asylum_abci_app/handlers.py index e673e95..e9160d9 100644 --- a/packages/zarathustra/skills/asylum_abci_app/handlers.py +++ b/packages/zarathustra/skills/asylum_abci_app/handlers.py @@ -42,7 +42,11 @@ DefaultDialogues, ) from packages.zarathustra.connections.openai_api.connection import reconstitute -from packages.zarathustra.skills.asylum_abci_app.behaviours import TELEGRAM_MSG_CHAR_LIMIT, get_bounty_info +from packages.zarathustra.skills.asylum_abci_app.behaviours import ( + TELEGRAM_MSG_CHAR_LIMIT, + get_bounty_info, + get_chat_history, +) from packages.zarathustra.protocols.llm_chat_completion.message import LlmChatCompletionMessage from packages.zarathustra.protocols.llm_chat_completion.dialogues import ( LlmChatCompletionDialogue, @@ -50,7 +54,6 @@ ) -BOT_PATTERN = re.compile(r"(🤖.*?🤖)") MERMAID_PATTERN = re.compile(r"```mermaid\s*([\s\S]+?)\s*```") @@ -66,16 +69,7 @@ def create_readme(context, mermaid: str, fsm_spec, out_path: Path): content = template_file.read_text() bounty_info: str = get_bounty_info(context) sponsor_bounty = bounty_info.split("\nDescription:")[0] - authors = set() - messages = [] - for text in context.asylum_strategy.chat_history: - if bot_match := BOT_PATTERN.search(text): - authors.add(bot_match.group(1)) - messages.append(text) - else: - authors.add("Human") - messages.append(f"Human agent says:\n{text}") - agent_conversation = "\n\n".join(f"{i}. {msg}" for i, msg in enumerate(messages)) + agent_conversation, authors = get_chat_history(context) formatted_readme = content.format( project_name=fsm_spec.label, authors=", ".join(authors), diff --git a/packages/zarathustra/skills/asylum_abci_app/skill.yaml b/packages/zarathustra/skills/asylum_abci_app/skill.yaml index 67dffeb..9e179a0 100644 --- a/packages/zarathustra/skills/asylum_abci_app/skill.yaml +++ b/packages/zarathustra/skills/asylum_abci_app/skill.yaml @@ -8,10 +8,10 @@ aea_version: '>=1.0.0, <2.0.0' fingerprint: README.md: bafybeiesl5jlvvu4enydib32bpyfqphlkdulxy3oqid3t32cjxya5qykci __init__.py: bafybeiarezohbq3skvjb6k5djqpyr2cssmconp3jbhsoisrnmw2pqly47q - behaviours.py: bafybeid4w76zrq5xw4gukyflesg3uknduwht4nzot4vlwag3q635xaelee + behaviours.py: bafybeihclqw6na7trx7t6em2lp56665tnw3tktjmurp56sokssbyakxnzq dialogues.py: bafybeihumikopwajayxsmbyz4dqhzzchief6qkgcjfjtdtni62wazii6ea fsm_specification.yaml: bafybeigzazuujebi3vxw4lxghqgss6fsmetm5iixjxkpqafs3ztfc3rxcy - handlers.py: bafybeigrdnhgizxb3uxbmnwyr2o56makdfeaosjxzpund3xiuhc5e33bti + handlers.py: bafybeib5fzcoxjs6pp5hrc4oaa64jpnkt6qnwbddkq4bsa7vpogt5l5zle mermaid_diagrams/asylum_abci_app.mmd: bafybeigyh7kf7gjnmsyfnpmf4pc5bq4zvzgzi5ehws3y3abbbxikzg55fm scraper.py: bafybeia7m3ilj5puq7iycx4x62vndee72qakxsa5zv34rq2v5jscniz4nu strategy.py: bafybeickw6x3vydvdbmtuy2c5ynfgq5c4ezfvu42keaoyie4w4kht2m4h4