Last active
December 3, 2025 13:21
-
-
Save kishida/372a8ddc3ed059eb52ddd828149e6de0 to your computer and use it in GitHub Desktop.
Function Callingを試すサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>kis</groupId> | |
| <artifactId>function-calling-sample</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <packaging>jar</packaging> | |
| <dependencies> | |
| <dependency> | |
| <groupId>dev.langchain4j</groupId> | |
| <artifactId>langchain4j-open-ai</artifactId> | |
| <version>1.7.1</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>dev.langchain4j</groupId> | |
| <artifactId>langchain4j</artifactId> | |
| <version>1.7.1</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>dev.langchain4j</groupId> | |
| <artifactId>langchain4j-http-client-jdk</artifactId> | |
| <version>1.7.1</version> | |
| </dependency> | |
| </dependencies> | |
| <properties> | |
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| <maven.compiler.release>25</maven.compiler.release> | |
| </properties> | |
| <name>Function Calling Sample</name> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import dev.langchain4j.agent.tool.Tool; | |
| import dev.langchain4j.http.client.jdk.JdkHttpClient; | |
| import dev.langchain4j.memory.chat.MessageWindowChatMemory; | |
| import dev.langchain4j.model.openai.OpenAiStreamingChatModel; | |
| import dev.langchain4j.service.AiServices; | |
| import dev.langchain4j.service.TokenStream; | |
| import module java.desktop; | |
| import java.net.http.HttpClient; | |
| import java.time.LocalTime; | |
| public class ToolSample { | |
| static class WeatherService { | |
| String weather = "雨"; | |
| int temperature = 15; | |
| @Tool("return weather of the location") | |
| String getWeather(String location) { | |
| System.out.println("getWeather called"); | |
| return weather; | |
| } | |
| @Tool("return temperature in celsius of the location") | |
| int getTemperature(String location) { | |
| System.out.println("getTemperature called"); | |
| return temperature; | |
| } | |
| @Tool("return current time") | |
| String getCurrentTime() { | |
| System.out.println("getCurrentTime called"); | |
| return LocalTime.now().toString(); | |
| } | |
| } | |
| interface Assistant { | |
| TokenStream chat(String userMessage); | |
| } | |
| static String MODEL = | |
| "ministral-3-3b-instruct-2512"; | |
| public static void main(String[] args) { | |
| var model = OpenAiStreamingChatModel.builder() | |
| .baseUrl("http://localhost:1234/v1") | |
| .modelName(MODEL) | |
| .httpClientBuilder(JdkHttpClient.builder().httpClientBuilder( | |
| HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1))) | |
| .build(); | |
| var service = new WeatherService(); | |
| var assistant = AiServices.builder(Assistant.class) | |
| .streamingChatModel(model) | |
| .systemMessageProvider(memId -> """ | |
| /nothink | |
| あなたは天気や気温、現在時刻を返すことができるエージェントです。 | |
| """) | |
| .chatMemory(MessageWindowChatMemory.withMaxMessages(30)) | |
| .tools(service) | |
| .build(); | |
| var top = new JPanel(new FlowLayout()); | |
| var text = new JTextField(25); | |
| var button = new JButton("投稿"); | |
| top.add(text); | |
| top.add(button); | |
| var bottom = new JPanel(new FlowLayout()); | |
| var combo = new JComboBox<String>(new String[]{"晴れ", "曇り", "雨", "雪"}); | |
| var slider = new JSlider(-10, 50, 20); | |
| bottom.add(combo); | |
| bottom.add(slider); | |
| var panel = new JPanel(new GridLayout(2, 1)); | |
| panel.add(top); | |
| panel.add(bottom); | |
| JFrame f = new JFrame("天気アプリ"); | |
| f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| f.setSize(400, 300); | |
| f.add(BorderLayout.NORTH, panel); | |
| var area = new JTextArea(); | |
| area.setLineWrap(true); | |
| f.add(new JScrollPane(area, | |
| JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); | |
| f.setVisible(true); | |
| ActionListener event = ae -> { | |
| service.temperature = slider.getValue(); | |
| service.weather = combo.getSelectedItem().toString(); | |
| var prompt = text.getText(); | |
| Thread.ofVirtual().start(() -> { | |
| SwingUtilities.invokeLater(() -> { | |
| area.append("> %s\n".formatted(prompt)); | |
| text.setText(prompt); | |
| }); | |
| var response = assistant.chat(text.getText()); | |
| response.onPartialResponse(str -> | |
| SwingUtilities.invokeLater(() -> area.append(str))) | |
| .ignoreErrors() | |
| .onToolExecuted(te -> { | |
| SwingUtilities.invokeLater(() -> area.append( | |
| "%s invoked with %s\n".formatted(te.request().name(), te.request().arguments()))); | |
| }) | |
| .onCompleteResponse(resp -> SwingUtilities.invokeLater(() -> area.append("\n"))) | |
| .start(); | |
| }); | |
| }; | |
| button.addActionListener(event); | |
| text.addActionListener(event); | |
| } | |
| } |
Author
kishida
commented
Dec 3, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment