Skip to content

Commit

Permalink
Make ArgumentProfile Prioritized. Expose ChatGTP's client. Rewrite Ch…
Browse files Browse the repository at this point in the history
…atGPT argument to APS. Add unit tests. Add support for programmatic for ChatGPT.
  • Loading branch information
Rollczi committed Oct 3, 2024
1 parent 3b2ffcf commit 20bdd56
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AnnotationInvoker<SENDER> process(AnnotationInvoker<SENDER> invoker) {
return;
}

argument.addProfile(profile);
argument.withProfile(profile);
});
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.rollczi.litecommands.chatgpt;

import dev.rollczi.litecommands.argument.SimpleArgument;
import dev.rollczi.litecommands.join.JoinProfile;
import dev.rollczi.litecommands.reflect.type.TypeToken;

public class ChatGptArgument extends SimpleArgument<String> {

public ChatGptArgument(String name, String topic) {
super(name, TypeToken.of(String.class), false);
this.withProfile(new ChatGptArgumentProfile(topic));
this.withProfile(new JoinProfile());
}

public ChatGptArgument(String name) {
this(name, "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.join.Join;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.chatgpt.annotation.ChatGpt;
import dev.rollczi.litecommands.join.JoinProfile;
import dev.rollczi.litecommands.programmatic.LiteCommand;
import dev.rollczi.litecommands.scheduler.SchedulerSameThreadImpl;
import dev.rollczi.litecommands.unit.annotations.LiteTestSpec;
import org.junit.jupiter.api.Test;
Expand All @@ -14,7 +17,14 @@ class ChatGptArgumentTest extends LiteTestSpec {
.scheduler(new SchedulerSameThreadImpl())
.extension(new LiteChatGptExtension<>(), configuration -> configuration
.chatGptClient(() -> new MockChatGptClient())
.tokensLimit(0, 256)
.prompt((commandStructure) -> "Hello, World!")
.promptWithTopic((commandStructure, topic) -> topic)
.tokensLimit(1, 16)
)
.commands(
new LiteCommand<>("programmatic")
.argument(new ChatGptArgument("message", "my topic"))
.executeReturn(context -> context.argument("message", String.class))
);

@Command(name = "chatgpt")
Expand All @@ -31,7 +41,22 @@ void testChatGptMessage() {
.assertSuccess("Hello, World!");

platform.suggest("chatgpt ")
.assertSuggest("Hello, World!");
.assertSuggest();

platform.suggest("chatgpt !")
.assertSuggest("!Hello, World!");
}

@Test
void testChatGptMessageWithProgrammatic() {
platform.execute("programmatic Hello, World!")
.assertSuccess("Hello, World!");

platform.suggest("programmatic ")
.assertSuggest();

platform.suggest("programmatic !")
.assertSuggest("!my topic");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ public class MockChatGptClient implements ChatGptClient {

@Override
public String chat(String message) {
return "'Hello, World!'";
throw new UnsupportedOperationException("Not implemented");
}

@Override
public String chat(String context, String message) {
return "'Hello, World!'";
return "'" + context + "'";
}

@Override
public String chat(ChatGptMessage... messages) {
return "'Hello, World!'";
throw new UnsupportedOperationException("Not implemented");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default Optional<ParseResult<T>> defaultValue() {
boolean hasDefaultValue();

@ApiStatus.Experimental
<P extends ArgumentProfile<P>> Argument<T> addProfile(P profile);
<P extends ArgumentProfile<P>> Argument<T> withProfile(P profile);

@ApiStatus.Experimental
<P> Optional<P> getProfile(ArgumentProfileNamespace<P> key);
Expand Down Expand Up @@ -57,7 +57,7 @@ static <T, P extends ArgumentProfile<P>> Argument<T> profiled(String name, Class
@ApiStatus.Experimental
static <T, P extends ArgumentProfile<P>> Argument<T> profiled(String name, TypeToken<T> type, P profile) {
return new SimpleArgument<>(name, type, false)
.addProfile(profile);
.withProfile(profile);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean hasDefaultValue() {
}

@ApiStatus.Experimental
public <P extends ArgumentProfile<P>> SimpleArgument<T> addProfile(P profile) {
public <P extends ArgumentProfile<P>> SimpleArgument<T> withProfile(P profile) {
ArgumentProfileNamespace<P> namespace = profile.getNamespace();

this.profiles.add(profile);
Expand Down

0 comments on commit 20bdd56

Please sign in to comment.