Adds "pre-fill" support for Claude

This commit is contained in:
Dustin Miller 2024-01-03 18:45:36 -06:00 committed by GitHub
parent b103ab1f0b
commit b10f64face
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -295,6 +295,9 @@ def claude_2_1_pt(
if system message is passed in, you can only do system, human, assistant or system, human
if a system message is passed in and followed by an assistant message, insert a blank human message between them.
Additionally, you can "put words in Claude's mouth" by ending with an assistant message.
See: https://docs.anthropic.com/claude/docs/put-words-in-claudes-mouth
"""
class AnthropicConstants(Enum):
@ -311,6 +314,7 @@ def claude_2_1_pt(
if idx > 0 and messages[idx - 1]["role"] == "system":
prompt += f"{AnthropicConstants.HUMAN_PROMPT.value}" # Insert a blank human message
prompt += f"{AnthropicConstants.AI_PROMPT.value}{message['content']}"
if messages[-1]["role"] != "assistant":
prompt += f"{AnthropicConstants.AI_PROMPT.value}" # prompt must end with \"\n\nAssistant: " turn
return prompt
@ -364,6 +368,10 @@ def format_prompt_togetherai(messages, prompt_format, chat_template):
def anthropic_pt(
messages: list,
): # format - https://docs.anthropic.com/claude/reference/complete_post
"""
You can "put words in Claude's mouth" by ending with an assistant message.
See: https://docs.anthropic.com/claude/docs/put-words-in-claudes-mouth
"""
class AnthropicConstants(Enum):
HUMAN_PROMPT = "\n\nHuman: "
AI_PROMPT = "\n\nAssistant: "
@ -382,6 +390,7 @@ def anthropic_pt(
idx == 0 and message["role"] == "assistant"
): # ensure the prompt always starts with `\n\nHuman: `
prompt = f"{AnthropicConstants.HUMAN_PROMPT.value}" + prompt
if messages[-1]["role"] != "assistant":
prompt += f"{AnthropicConstants.AI_PROMPT.value}"
return prompt