Skip to content

Commit 2769c13

Browse files
committed
Adjust CodeLlama fitm strategy
1 parent 74e5fad commit 2769c13

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

Core/Sources/SuggestionService/RequestStrategies/CodeLlamaFillInTheMiddleRequestStrategy.swift

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,13 @@ struct CodeLlamaFillInTheMiddleRequestStrategy: RequestStrategy {
3131
}
3232

3333
struct Prompt: PromptStrategy {
34-
let systemPrompt: String = """
35-
You are a senior programer who take the surrounding code and \
36-
references from the codebase into account in order to write high-quality code to \
37-
complete the code enclosed in the given code. \
38-
You only respond with code that works and fits seamlessly with surrounding code. \
39-
Don't include anything else beyond the code. \
40-
The prefix will follow the PRE tag and the suffix will follow the SUF tag.
41-
"""
34+
fileprivate(set) var systemPrompt: String = ""
4235
var sourceRequest: SuggestionRequest
4336
var prefix: [String]
4437
var suffix: [String]
4538
var filePath: String { sourceRequest.relativePath ?? sourceRequest.fileURL.path }
4639
var relevantCodeSnippets: [RelevantCodeSnippet] { sourceRequest.relevantCodeSnippets }
47-
var stopWords: [String] { ["\n\n"] }
40+
var stopWords: [String] { ["\n\n", "<EOT>"] }
4841
var language: CodeLanguage? { sourceRequest.language }
4942

5043
var suggestionPrefix: SuggestionPrefix {
@@ -57,6 +50,7 @@ struct CodeLlamaFillInTheMiddleRequestStrategy: RequestStrategy {
5750
truncatedSuffix: [String],
5851
includedSnippets: [RelevantCodeSnippet]
5952
) -> [PromptMessage] {
53+
let suffix = truncatedSuffix.joined()
6054
return [
6155
.init(
6256
role: .user,
@@ -67,7 +61,7 @@ struct CodeLlamaFillInTheMiddleRequestStrategy: RequestStrategy {
6761
\(sourceRequest.usesTabsForIndentation ? "tab" : "space")
6862
\(includedSnippets.map(\.content).joined(separator: "\n\n"))
6963
\(truncatedPrefix.joined()) \
70-
\(Tag.suffix)\(truncatedSuffix.joined()) \
64+
\(Tag.suffix)\(suffix.isEmpty ? "\n// End of file" : suffix) \
7165
\(Tag.middle)
7266
""".trimmingCharacters(in: .whitespacesAndNewlines)
7367
),
@@ -76,3 +70,30 @@ struct CodeLlamaFillInTheMiddleRequestStrategy: RequestStrategy {
7670
}
7771
}
7872

73+
struct CodeLlamaFillInTheMiddleWithSystemPromptRequestStrategy: RequestStrategy {
74+
let strategy: CodeLlamaFillInTheMiddleRequestStrategy
75+
76+
init(sourceRequest: SuggestionRequest, prefix: [String], suffix: [String]) {
77+
strategy = .init(sourceRequest: sourceRequest, prefix: prefix, suffix: suffix)
78+
}
79+
80+
func createPrompt() -> some PromptStrategy {
81+
var prompt = strategy.createPrompt()
82+
prompt.systemPrompt = """
83+
You are a senior programer who take the surrounding code and \
84+
references from the codebase into account in order to write high-quality code to \
85+
complete the code enclosed in the given code. \
86+
You only respond with code that works and fits seamlessly with surrounding code. \
87+
Don't include anything else beyond the code. \
88+
The prefix will follow the PRE tag and the suffix will follow the SUF tag. \
89+
You should write the code that fits seamlessly after the MID tag.
90+
""".trimmingCharacters(in: .whitespacesAndNewlines)
91+
92+
return prompt
93+
}
94+
95+
func createRawSuggestionPostProcessor() -> some RawSuggestionPostProcessingStrategy {
96+
strategy.createRawSuggestionPostProcessor()
97+
}
98+
}
99+

Core/Sources/SuggestionService/RequestStrategy.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public enum RequestStrategyOption: String, CaseIterable, Codable {
2626
case `default` = ""
2727
case naive
2828
case `continue`
29+
case codeLlamaFillInTheMiddle
30+
case codeLlamaFillInTheMiddleWithSystemPrompt
2931
}
3032

3133
extension RequestStrategyOption {
@@ -37,6 +39,10 @@ extension RequestStrategyOption {
3739
return NaiveRequestStrategy.self
3840
case .continue:
3941
return ContinueRequestStrategy.self
42+
case .codeLlamaFillInTheMiddle:
43+
return CodeLlamaFillInTheMiddleRequestStrategy.self
44+
case .codeLlamaFillInTheMiddleWithSystemPrompt:
45+
return CodeLlamaFillInTheMiddleWithSystemPromptRequestStrategy.self
4046
}
4147
}
4248
}

CustomSuggestionService/ContentView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ struct RequestStrategyPicker: View {
177177
Text("Naive").tag(option.rawValue)
178178
case .continue:
179179
Text("Continue").tag(option.rawValue)
180+
case .codeLlamaFillInTheMiddle:
181+
Text("CodeLlama Fill-in-the-Middle (Good for Codellama:xb-code)")
182+
.tag(option.rawValue)
183+
case .codeLlamaFillInTheMiddleWithSystemPrompt:
184+
Text("CodeLlama Fill-in-the-Middle with System Prompt")
185+
.tag(option.rawValue)
180186
}
181187
}
182188
}

0 commit comments

Comments
 (0)