这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
package com.bolingcavalry.helloopenai.controller;
import org.springframework.ai.chat.ChatClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class SimpleAiController {
// 负责处理OpenAI的bean,所需参数来自properties文件
private final ChatClient chatClient;
public SimpleAiController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@PostMapping("/ai/simple")
public Map<String, String> completion(@RequestBody Map<String,String> map) {
return Map.of("generation", chatClient.call(map.get("message")));
}
}
public interface ChatClient extends ModelClient<Prompt, ChatResponse> {
default String call(String message) {// implementation omitted
}
@Override
ChatResponse call(Prompt prompt);
}
@FunctionalInterface
public interface StreamingChatClient extends StreamingModelClient<Prompt, ChatResponse> {
default Flux<String> stream(String message) {
Prompt prompt = new Prompt(message);
return stream(prompt).map(response -> (response.getResult() == null || response.getResult().getOutput() == null
|| response.getResult().getOutput().getContent() == null) ? ""
: response.getResult().getOutput().getContent());
}
@Override
Flux<ChatResponse> stream(Prompt prompt);
}
public class Prompt implements ModelRequest<List<Message>> {
private final List<Message> messages;
private ChatOptions modelOptions;
@Override
public ChatOptions getOptions() {..}
@Override
public List<Message> getInstructions() {...}
public String getContents() {
StringBuilder sb = new StringBuilder();
for (Message message : getInstructions()) {
sb.append(message.getContent());
}
return sb.toString();
}
// constructors and utility methods omitted
}
public Prompt(String contents) {
this(new UserMessage(contents));
}
public interface Message {
String getContent();
List<Media> getMedia();
Map<String, Object> getProperties();
MessageType getMessageType();
}
public enum MessageType {
USER("user"),
ASSISTANT("assistant"),
SYSTEM("system"),
FUNCTION("function");
/**
* The ChatOptions represent the common options, portable across different chat models.
*/
public interface ChatOptions extends ModelOptions {
// 大模型生成的内容应该更严谨还是更有创造性
Float getTemperature();
// 返回概率超过P的所有内容
Float getTopP();
// 返回概率最高的前K个内容
Integer getTopK();
}
ChatOptions portablePromptOptions = ChatOptionsBuilder.builder()
.withTemperature(0.9f)
.withTopK(100)
.withTopP(0.6f)
.build();
public class ChatResponse implements ModelResponse<Generation> {
private final ChatResponseMetadata chatResponseMetadata;
private final List<Generation> generations;
@Override
public ChatResponseMetadata getMetadata() {...}
@Override
public List<Generation> getResults() {...}
// other methods omitted
}
public class Generation implements ModelResult<AssistantMessage> {
private AssistantMessage assistantMessage;
private ChatGenerationMetadata chatGenerationMetadata;
@Override
public AssistantMessage getOutput() {...}
@Override
public ChatGenerationMetadata getMetadata() {...}
// other methods omitted
}
public class AssistantMessage extends AbstractMessage {
public AssistantMessage(String content) {
super(MessageType.ASSISTANT, content);
}
public AssistantMessage(String content, Map<String, Object> properties) {
super(MessageType.ASSISTANT, content, properties);
}
@Override
public String toString() {
return "AssistantMessage{" + "content='" + getContent() + '\'' + ", properties=" + properties + ", messageType="
+ messageType + '}';
}
}