LoginSignup
1
1

Spring AI フレームワーク その2

Posted at

Spring AI フレームワーク その2

前回、Spring AI フレームワークを触ってみた - Qiita の続きです。他にもまだいくつかプロジェクトテンプレートが存在するので、試してみようと思います。

テンプレ

spring project list すると、前回追加されたプロジェクトテンプレートが表示されます。

├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai                        │Hello, World Open AI Application                                  │https://github.com/rd-1-2022/ai-openai-helloworld                   │gs      │[java-17, boot-3.1.x, ai, openai]                  │
├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai-azure                  │Hello, World Azure Open AI Application                            │https://github.com/rd-1-2022/ai-azure-openai-helloworld             │gs      │[java-17, boot-3.1.x, ai, openai, azure]           │
├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai-azure-hello-world      │AI - Hello, World                                                 │https://github.com/rd-1-2022/ai-azure-openai-helloworld             │ai-azure│[java-17, boot-3.1.x, ai, azure, web]              │
├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai-azure-prompt-templating│AI - Prompt Templating                                            │https://github.com/rd-1-2022/ai-azure-prompt-template               │ai-azure│[java-17, boot-3.1.x, ai, azure, prompt templating]│
├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai-azure-prompt-roles     │AI - Prompt Templating                                            │https://github.com/rd-1-2022/ai-azure-openai-prompt-roles           │ai-azure│[java-17, boot-3.1.x, ai, azure, prompt roles]     │
├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai-azure-output-parser    │AI - Output Parser                                                │https://github.com/rd-1-2022/ai-azure-openai-output-parser          │ai-azure│[java-17, boot-3.1.x, ai, azure, output parser]    │
├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai-azure-stuff-prompt     │AI - Stuff the prompt                                             │https://github.com/rd-1-2022/ai-azure-stuff-prompt                  │ai-azure│[java-17, boot-3.1.x, ai, azure, prompt stuff]     │
├──────────────────────────┼──────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────────────────────────────────────────┤
│ai-azure-prompt-rag       │AI - Retrieval Augmented Generation                               │https://github.com/rd-1-2022/ai-azure-retrieval-augmented-generation│ai-azure│[java-17, boot-3.1.x, ai, azure, retrieval]        │

プロジェクトへの追加

spring boot ai-azure-prompt-templating で、テンプレート(さっきのテンプレとは違う意味)を使った処理コードの追加をします。

追加されるのは以下のクラスと、

@RestController
public class PromptTemplateController {

    private final AiClient aiClient;

    @Value("classpath:/prompts/joke-prompt.st")
    private Resource jokeResource;


    @Autowired
    public PromptTemplateController(AiClient aiClient) {
        this.aiClient = aiClient;
    }

    @GetMapping("/ai/prompt")
    public Generation completion(@RequestParam(value = "adjective", defaultValue = "funny") String adjective,
                                 @RequestParam(value = "topic", defaultValue = "cows") String topic) {
        PromptTemplate promptTemplate = new PromptTemplate(jokeResource);
        Prompt prompt = promptTemplate.create(Map.of("adjective", adjective, "topic", topic));
        return aiClient.generate(prompt).getGeneration();
    }
}

resources/prompts/joke-prompt.st のテンプレートファイルです。

Tell me a {adjective} joke about {topic}.

布団を使った面白いジョークを教えててきなものを、adjectivetopic を渡して答えて貰うというものです。

プロンプトテンプレートは、 org.springframework.ai.prompt.PromptTemplate で定義されていてAIフレームワーク側に用意されている感じなので、簡単に使えて便利な気がします。Mapkey-value を元に作成し、aiClient.generate(prompt) でAIに問い合わせるだけです。

テスト

適当にパラメータを私て呼び出します。英語に鳴ってしまいます。

curl "http://localhost:8080/ai/prompt?adjective=sad&topic=cat"

{"text":"Why did the cat sit on the computer?\nBecause it wanted to press Paws.","info":{}}

適当にテンプレを修正し、日本語で答えを要求してみます。

Tell me a {adjective} joke about {topic}. Please answer in Japanese.
curl "http://localhost:8080/ai/prompt?adjective=sad&topic=cat"
{"text":"猫がミルクをこぼしてしまった時の悲しいジョークです。「なんてことだ、猫のミルクはこぼしたら、もう一度はすっぽり入らないのさ」","info":{}}

最後に日本語でOKと入れたので、意味としてはよく分からないのですが、日本語で答えてくれます。

というわけで、今回は PromptTemplateを使ったサンプルでした。

以上

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1