-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAiApplication.java
62 lines (55 loc) · 1.83 KB
/
AiApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.ai;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import java.util.concurrent.CountDownLatch;
@SpringBootApplication
public class AiApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(AiApplication.class)
.web(WebApplicationType.NONE)
.run(args);
}
/**
* 阻塞式访问,在获得完整回答后返回
*/
@Bean
public CommandLineRunner call() {
return args -> {
OllamaChatModel model = OllamaChatModel.builder()
.ollamaApi(new OllamaApi())
.build();
ChatOptions options = ChatOptions.builder()
.model("deepseek-r1:32b")
.build();
ChatResponse response = model.call(new Prompt("你是谁?", options));
System.out.println(response.getResult().getOutput().getText());
};
}
/**
* 流式访问,每个token返回
*/
@Bean
public CommandLineRunner stream() {
return args -> {
OllamaChatModel model = OllamaChatModel.builder()
.ollamaApi(new OllamaApi())
.build();
CountDownLatch latch = new CountDownLatch(1);
ChatOptions options = ChatOptions.builder()
.model("deepseek-r1:32b")
.build();
model.stream(new Prompt("你是谁?", options))
.doFinally(signalType -> latch.countDown())
.subscribe(chatResponse -> System.out.print(chatResponse.getResult().getOutput().getText()));
latch.await();
};
}
}