GPT function call #300
Preetam-Blr
started this conversation in
General
Replies: 1 comment
-
Hi @Preetam-Blr I've successfully implemented a function call with langchain4j. Below the code public class CopilotCLITest
{
interface Assistant {
String chat(String message);
}
class SystemCommandTool {
private String command ;
Optional<String> lastCommand() {
return Optional.ofNullable(command);
}
@Tool("all system commands")
String exec(@P("command") String command) {
this.command = command;
return format( "command executed: %s", command);
}
}
/**
* Rigourous Test :-)
*/
@Test
public void executeSystemCommands()
{
var chatLanguageModel = OpenAiChatModel.builder()
.apiKey( MY_API_KEY )
.modelName(GPT_3_5_TURBO_0613)
.temperature(0.0)
.build()
;
var chatMemory = MessageWindowChatMemory.withMaxMessages(10);
var systemCommandTool = new SystemCommandTool();
var service = AiServices.builder(Assistant.class)
.chatLanguageModel(chatLanguageModel)
.tools( systemCommandTool )
.chatMemory( chatMemory )
.build()
;
String answer;
answer = service.chat( "change to home directory" );
assertEquals("You have successfully changed to the home directory.", answer);
assertTrue( systemCommandTool.lastCommand().isPresent() );
assertEquals( "cd ~", systemCommandTool.lastCommand().get() );
answer = service.chat( "find all pdf files" );
// assertEquals("You have successfully changed to the home directory.", answer);
assertTrue( systemCommandTool.lastCommand().isPresent() );
assertEquals( "find . -name '*.pdf'", systemCommandTool.lastCommand().get() );
}
} You have to create a Hope this help |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want response in Structured format(YAML,JSON).We can achieve this using Function call in openAi function call .Please let me know How to achieve this using langchain4j
Beta Was this translation helpful? Give feedback.
All reactions