Skip to content

Commit

Permalink
Merge branch 'main' into deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Jun 16, 2024
2 parents 28167fc + 9b1a9bd commit fecb54d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/utils/llm/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,28 @@ class SafeMessage(TypedDict):
content: str


def ensure_safe(prompt: str | list[Message]):
def ensure_no_system(prompt: str | list[Message]):
return [
cast(SafeMessage, {"role": i["role"] if i["role"] != "system" else "user", "content": i["content"]})
for i in ensure(prompt)
]


def join_adjacent_inplace(messages: list[SafeMessage]):
i = 1
while i < len(messages):
if messages[i - 1]["role"] == messages[i]["role"]:
if messages[i - 1]["content"] != messages[i]["content"]:
messages[i - 1]["content"] += "\n\n" + messages[i]["content"]
messages.pop(i)
else:
i += 1


def ensure_safe(prompt: str | list[Message]):
messages = ensure_no_system(prompt)
join_adjacent_inplace(messages)
if messages[0]["role"] == "assistant":
messages.insert(0, {"role": "user", "content": "..."})

return messages

0 comments on commit fecb54d

Please sign in to comment.