Skip to content

Commit 99c7287

Browse files
authored
Feat: Refactoring plugin:LLM honeypot custom prompt (#154)
refactoring LLM honeypot custom prompt
1 parent c3d2ff8 commit 99c7287

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

plugins/llm-integration.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,19 @@ func InitLLMHoneypot(config LLMHoneypot) *LLMHoneypot {
9696
return &config
9797
}
9898

99-
func buildPrompt(histories []Message, protocol tracer.Protocol, command string) ([]Message, error) {
99+
func (llmHoneypot *LLMHoneypot) buildPrompt(command string) ([]Message, error) {
100100
var messages []Message
101+
var prompt string
101102

102-
switch protocol {
103+
switch llmHoneypot.Protocol {
103104
case tracer.SSH:
105+
prompt = systemPromptVirtualizeLinuxTerminal
106+
if llmHoneypot.CustomPrompt != "" {
107+
prompt = llmHoneypot.CustomPrompt
108+
}
104109
messages = append(messages, Message{
105110
Role: SYSTEM.String(),
106-
Content: systemPromptVirtualizeLinuxTerminal,
111+
Content: prompt,
107112
})
108113
messages = append(messages, Message{
109114
Role: USER.String(),
@@ -113,13 +118,17 @@ func buildPrompt(histories []Message, protocol tracer.Protocol, command string)
113118
Role: ASSISTANT.String(),
114119
Content: "/home/user",
115120
})
116-
for _, history := range histories {
121+
for _, history := range llmHoneypot.Histories {
117122
messages = append(messages, history)
118123
}
119124
case tracer.HTTP:
125+
prompt = systemPromptVirtualizeHTTPServer
126+
if llmHoneypot.CustomPrompt != "" {
127+
prompt = llmHoneypot.CustomPrompt
128+
}
120129
messages = append(messages, Message{
121130
Role: SYSTEM.String(),
122-
Content: systemPromptVirtualizeHTTPServer,
131+
Content: prompt,
123132
})
124133
messages = append(messages, Message{
125134
Role: USER.String(),
@@ -214,18 +223,7 @@ func (llmHoneypot *LLMHoneypot) ExecuteModel(command string) (string, error) {
214223
var err error
215224
var prompt []Message
216225

217-
if llmHoneypot.CustomPrompt != "" {
218-
prompt = append(prompt, Message{
219-
Role: SYSTEM.String(),
220-
Content: llmHoneypot.CustomPrompt,
221-
})
222-
prompt = append(prompt, Message{
223-
Role: USER.String(),
224-
Content: command,
225-
})
226-
} else {
227-
prompt, err = buildPrompt(llmHoneypot.Histories, llmHoneypot.Protocol, command)
228-
}
226+
prompt, err = llmHoneypot.buildPrompt(command)
229227

230228
if err != nil {
231229
return "", err

plugins/llm-integration_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ func TestBuildPromptEmptyHistory(t *testing.T) {
1616
var histories []Message
1717
command := "pwd"
1818

19+
honeypot := LLMHoneypot{
20+
Histories: histories,
21+
Protocol: tracer.SSH,
22+
}
23+
1924
//When
20-
prompt, err := buildPrompt(histories, tracer.SSH, command)
25+
prompt, err := honeypot.buildPrompt(command)
2126

2227
//Then
2328
assert.Nil(t, err)
@@ -35,14 +40,45 @@ func TestBuildPromptWithHistory(t *testing.T) {
3540

3641
command := "pwd"
3742

43+
honeypot := LLMHoneypot{
44+
Histories: histories,
45+
Protocol: tracer.SSH,
46+
}
47+
3848
//When
39-
prompt, err := buildPrompt(histories, tracer.SSH, command)
49+
prompt, err := honeypot.buildPrompt(command)
4050

4151
//Then
4252
assert.Nil(t, err)
4353
assert.Equal(t, SystemPromptLen+1, len(prompt))
4454
}
4555

56+
func TestBuildPromptWithCustomPrompt(t *testing.T) {
57+
//Given
58+
var histories = []Message{
59+
{
60+
Role: "cat hello.txt",
61+
Content: "world",
62+
},
63+
}
64+
65+
command := "pwd"
66+
67+
honeypot := LLMHoneypot{
68+
Histories: histories,
69+
Protocol: tracer.SSH,
70+
CustomPrompt: "act as calculator",
71+
}
72+
73+
//When
74+
prompt, err := honeypot.buildPrompt(command)
75+
76+
//Then
77+
assert.Nil(t, err)
78+
assert.Equal(t, prompt[0].Content, "act as calculator")
79+
assert.Equal(t, prompt[0].Role, SYSTEM.String())
80+
}
81+
4682
func TestBuildExecuteModelFailValidation(t *testing.T) {
4783

4884
llmHoneypot := LLMHoneypot{

0 commit comments

Comments
 (0)