Skip to content

Commit 59e6692

Browse files
committed
cli,bot: allow setting system message
1 parent 4d3d4e7 commit 59e6692

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

.github/workflows/chatgpt-bot.yaml

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,28 @@ jobs:
2424
path: ~/.chatgpt
2525
key: conversations-${{ github.event.issue.number }}-${{ github.run_id }}-${{ github.run_attempt }}
2626
restore-keys: conversations-${{ github.event.issue.number }}-
27-
- uses: kezhenxu94/chatgpt-java/chatgpt-cli@v0.9.0
27+
- uses: kezhenxu94/chatgpt-java/chatgpt-cli@v0.10.0
2828
- name: Question
2929
if: github.event_name == 'issues'
3030
env:
3131
QUESTION: ${{ github.event.issue.title }}
32-
run: echo "QUESTION=${QUESTION#'Q:'}" >> $GITHUB_ENV
32+
SYSTEM: ${{ github.event.issue.body }}
33+
run: |
34+
echo "QUESTION=${QUESTION#'Q:'}" >> $GITHUB_ENV
35+
echo "SCRIPT=.github/workflows/chatgpt-issue-created.sh" >> $GITHUB_ENV
3336
- name: Question
3437
if: github.event_name == 'issue_comment'
3538
env:
3639
QUESTION: ${{ github.event.comment.body }}
37-
run: echo "QUESTION=${QUESTION#"Q:"}" >> $GITHUB_ENV
40+
run: |
41+
echo "QUESTION=${QUESTION#"Q:"}" >> $GITHUB_ENV
42+
echo "SCRIPT=.github/workflows/chatgpt-issue-comment.sh" >> $GITHUB_ENV
3843
- name: Answer
3944
env:
4045
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4146
ISSUE: ${{ github.event.issue.number }}
4247
run: |
43-
envsubst < .github/workflows/chatgpt-script.sh > chatgpt-script.sh
48+
envsubst < ${{ env.SCRIPT }} > chatgpt-script.sh
4449
chatgpt -Dspring.banner.location=file://$(pwd)/.github/workflows/banner.txt @chatgpt-script.sh | tee -a answer.txt
4550
4651
gh issue comment $ISSUE -F answer.txt
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
load 'issue-$ISSUE'
2-
ask '$QUESTION'
2+
ask "\"$QUESTION\""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load 'issue-$ISSUE'
2+
system "\"$SYSTEM\""
3+
ask "\"$QUESTION\""

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ plugins {
44

55
allprojects {
66
group = 'io.github.kezhenxu94'
7-
version = '0.8.0'
7+
version = '0.10.0'
88
sourceCompatibility = '8'
99
}

chatgpt-cli/src/main/java/io/github/kezhenxu94/chatgpt/cli/commands/ConversationCommands.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import io.github.kezhenxu94.chatgpt.ChatGPT;
44
import io.github.kezhenxu94.chatgpt.Conversation;
5+
import io.github.kezhenxu94.chatgpt.message.Message;
6+
import io.github.kezhenxu94.chatgpt.message.Role;
7+
import org.springframework.shell.Availability;
58
import org.springframework.shell.standard.ShellComponent;
69
import org.springframework.shell.standard.ShellMethod;
10+
import org.springframework.shell.standard.ShellMethodAvailability;
711

812
@ShellComponent
913
public class ConversationCommands {
@@ -15,13 +19,26 @@ public ConversationCommands(ChatGPT chatGPT) {
1519
this.chatGPT = chatGPT;
1620
}
1721

18-
@ShellMethod("Load a conversation by ID")
22+
@ShellMethod(
23+
"Set the system message as an instruction to guide the ChatGPT in following conversations")
24+
@ShellMethodAvailability("isConversationNull")
25+
public String system(String system) {
26+
if (conversation == null) {
27+
conversation = chatGPT.newConversation(system);
28+
} else if (conversation.messages().stream().allMatch(it -> it.role() != Role.SYSTEM)) {
29+
conversation.messages().add(0, Message.ofSystem(system));
30+
}
31+
return "System message set to: " + system;
32+
}
33+
34+
@ShellMethod(
35+
"Load a conversation by ID, if there is no conversation with the ID, a new one will be created with the ID")
1936
public String load(String id) throws Exception {
2037
conversation = chatGPT.loadConversation(id);
2138
return "You are now at conversation: " + id;
2239
}
2340

24-
@ShellMethod("Ask a question")
41+
@ShellMethod("Ask a question in the current conversation")
2542
public String ask(String question) throws Exception {
2643
if (conversation == null) {
2744
conversation = chatGPT.newConversation();
@@ -30,4 +47,12 @@ public String ask(String question) throws Exception {
3047
final var answer = conversation.ask(question);
3148
return answer.content().trim();
3249
}
50+
51+
Availability isConversationNull() {
52+
return conversation == null
53+
|| conversation.messages().stream().allMatch(it -> it.role() != Role.SYSTEM)
54+
? Availability.available()
55+
: Availability.unavailable(
56+
"you are already in a conversation, system message can only be set in new conversation.");
57+
}
3358
}

0 commit comments

Comments
 (0)