Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove system outputs from tool examples in system prompt #320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions gptme/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,23 @@ def get_tool_prompt(self, examples: bool, tool_format: ToolFormat):
if instructions:
prompt += f"\n\n**Instructions:** {instructions}"
if examples and (
examples_content := self.get_examples(tool_format, quote=True).strip()
examples_content := self.get_examples(
tool_format, quote=True, strip_system=True
).strip()
):
prompt += f"\n\n### Examples\n\n{examples_content}"
return prompt

def get_examples(self, tool_format: ToolFormat = "markdown", quote=False):
def get_examples(
self, tool_format: ToolFormat = "markdown", quote=False, strip_system=False
):
if callable(self.examples):
examples = self.examples(tool_format)
else:
examples = self.examples
# make sure headers have exactly two newlines after them
examples = re.sub(r"\n*(\n#+.*?)\n+", r"\n\1\n\n", examples)
return clean_example(examples, quote=quote)
return clean_example(examples, quote=quote, strip_system=strip_system)

def get_functions_description(self) -> str:
# return a prompt with a brief description of the available functions
Expand Down
29 changes: 28 additions & 1 deletion gptme/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,40 @@ def epoch_to_age(epoch, incl_date=False):
)


def clean_example(s: str, strict=False, quote=False) -> str:
def clean_example(s: str, strict=False, quote=False, strip_system=False) -> str:
orig = s
s = re.sub(
r"(^|\n)([>] )?([A-Za-z]+):",
rf"\1{'> ' if quote else ''}\3:",
s,
)
if strip_system:
# Split into lines and filter out system messages and their content
lines = s.split("\n")
filtered_lines = []
skip_next = False
in_codeblock = False
for line in lines:
# Track codeblock state
if line.strip().startswith("```"):
in_codeblock = not in_codeblock

if skip_next:
# Only stop skipping if we're not in a codeblock and either:
# - line is empty
# - new role message starts
if not in_codeblock and (
not line.strip() or re.match(r"^(> )?[A-Za-z]+:", line)
):
skip_next = False
if line.strip(): # If it's a new role, keep the line
filtered_lines.append(line)
continue
if re.match(r"^(> )?System:", line):
skip_next = True
continue
filtered_lines.append(line)
s = "\n".join(filtered_lines)
if strict:
assert s != orig, "Couldn't find a message"
return s
Expand Down
Loading