Skip to content

Commit

Permalink
Update prompt format
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuravi committed May 10, 2024
1 parent ed81c65 commit e92498c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
79 changes: 79 additions & 0 deletions Sources/SpeziLLMLocal/LLMLocalSchema+PromptFormatting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,85 @@ import SpeziLLM
extension LLMLocalSchema {
/// Holds default prompt formatting strategies for [Llama2](https://ai.meta.com/llama/) as well as [Phi-2](https://www.microsoft.com/en-us/research/blog/phi-2-the-surprising-power-of-small-language-models/) models.
public enum PromptFormattingDefaults {
/// Prompt formatting closure for the [Llama3](https://ai.meta.com/llama/) model
public static let llama3: (@Sendable (LLMContext) throws -> String) = { chat in // swiftlint:disable:this closure_body_length
let BEGINOFTEXT = "<|begin_of_text|>"
let SYSTEM = "system"
let USER = "user"
let ASSISTANT = "assistant"
let STARTHEADERID = "<|start_header_id|>"
let ENDHEADERID = "<|end_header_id|>"
let EOTID = "<|eot_id|>"

guard chat.first?.role == .system else {
throw LLMLocalError.illegalContext
}

var systemPrompts: [String] = []
var initialUserPrompt: String = ""

for contextEntity in chat {
if contextEntity.role != .system {
if contextEntity.role == .user {
initialUserPrompt = contextEntity.content
break
} else {
throw LLMLocalError.illegalContext
}
}

systemPrompts.append(contextEntity.content)
}

/// Build the initial Llama3 prompt structure
///
/// A template of the prompt structure looks like:
/// """
/// <s>[INST] <<SYS>>
/// {your_system_prompt}
/// <</SYS>>
///
/// {user_message_1} [/INST]
/// """
var prompt = """
\(BEGINOFTEXT)
\(STARTHEADERID)\(SYSTEM)\(ENDHEADERID)
\(systemPrompts.joined(separator: " "))
\(EOTID)
\(STARTHEADERID)\(USER)\(ENDHEADERID)
\(initialUserPrompt)
\(EOTID)
""" + " " // Add a spacer to the generated output from the model

for contextEntity in chat.dropFirst(2) {
if contextEntity.role == .assistant() {
/// Append response from assistant to the Llama3 prompt structure
///
/// A template for appending an assistant response to the overall prompt looks like:
/// {user_message_1} [/INST]){model_reply_1}</s>
prompt += """
\(STARTHEADERID)\(ASSISTANT)\(ENDHEADERID)
\(contextEntity.content)

Check failure on line 75 in Sources/SpeziLLMLocal/LLMLocalSchema+PromptFormatting.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
\(EOTID)
"""
} else if contextEntity.role == .user {
/// Append response from user to the Llama3 prompt structure
///
/// A template for appending an assistant response to the overall prompt looks like:
/// <s>[INST] {user_message_2} [/INST]
prompt += """
\(STARTHEADERID)\(USER)\(ENDHEADERID)
\(contextEntity.content)

Check failure on line 85 in Sources/SpeziLLMLocal/LLMLocalSchema+PromptFormatting.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
\(EOTID)
""" + " " // Add a spacer to the generated output from the model
}
}

return prompt
}

/// Prompt formatting closure for the [Llama2](https://ai.meta.com/llama/) model
public static let llama2: (@Sendable (LLMContext) throws -> String) = { chat in // swiftlint:disable:this closure_body_length
/// BOS token of the LLM, used at the start of each prompt passage.
Expand Down
3 changes: 2 additions & 1 deletion Tests/UITests/TestApp/LLMLocal/LLMLocalChatTestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct LLMLocalChatTestView: View {
with: LLMLocalSchema(
modelPath: .cachesDirectory.appending(path: "llm.gguf"),
parameters: .init(maxOutputLength: 512),
contextParameters: .init(contextWindowSize: 1024)
contextParameters: .init(contextWindowSize: 1024),
formatChat: LLMLocalSchema.PromptFormattingDefaults.llama3
)
)
}
Expand Down

0 comments on commit e92498c

Please sign in to comment.