spring-ai
38
总安装量
38
周安装量
#5407
全站排名
安装命令
npx skills add https://github.com/teachingai/full-stack-skills --skill spring-ai
Agent 安装分布
opencode
30
claude-code
27
gemini-cli
25
codex
24
github-copilot
22
Skill 文档
Spring AI å¼åæå
æ¦è¿°
Spring AI æ¯ Spring 宿¹æä¾ç AI åºç¨å¼åæ¡æ¶ï¼ç®åäºä¸åç§å¤§è¯è¨æ¨¡åï¼LLMï¼çéæï¼å æ¬ OpenAIãAnthropicãAzure OpenAI çã
æ ¸å¿åè½
1. 项ç®å建
ä¾èµï¼
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
æä½¿ç¨ Gradleï¼
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
2. Chat Client
é ç½®ï¼
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4
temperature: 0.7
ä½¿ç¨ ChatClientï¼
@Service
public class ChatService {
private final ChatClient chatClient;
public ChatService(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String chat(String message) {
return chatClient.call(message);
}
public String chatWithPrompt(String userMessage) {
Prompt prompt = new Prompt(new UserMessage(userMessage));
ChatResponse response = chatClient.call(prompt);
return response.getResult().getOutput().getContent();
}
}
æµå¼ååºï¼
@Service
public class ChatService {
private final StreamingChatClient streamingChatClient;
public ChatService(StreamingChatClient streamingChatClient) {
this.streamingChatClient = streamingChatClient;
}
public Flux<String> streamChat(String message) {
return streamingChatClient.stream(message)
.map(response -> response.getResult().getOutput().getContent());
}
}
3. Prompt Template
å®ä¹æ¨¡æ¿ï¼
@Service
public class PromptService {
private final PromptTemplate promptTemplate;
public PromptService() {
this.promptTemplate = new PromptTemplate(
"请ç¨{style}飿 ¼åç以ä¸é®é¢ï¼{question}"
);
}
public String generatePrompt(String style, String question) {
Map<String, Object> variables = Map.of(
"style", style,
"question", question
);
return promptTemplate.render(variables);
}
}
ä½¿ç¨ ChatClientï¼
@Service
public class ChatService {
private final ChatClient chatClient;
private final PromptTemplate promptTemplate;
public ChatService(ChatClient chatClient) {
this.chatClient = chatClient;
this.promptTemplate = new PromptTemplate(
"请ç¨{style}飿 ¼åç以ä¸é®é¢ï¼{question}"
);
}
public String chatWithStyle(String style, String question) {
Prompt prompt = promptTemplate.create(Map.of(
"style", style,
"question", question
));
ChatResponse response = chatClient.call(prompt);
return response.getResult().getOutput().getContent();
}
}
4. Embedding
é ç½®ï¼
spring:
ai:
openai:
embedding:
options:
model: text-embedding-ada-002
ä½¿ç¨ EmbeddingClientï¼
@Service
public class EmbeddingService {
private final EmbeddingClient embeddingClient;
public EmbeddingService(EmbeddingClient embeddingClient) {
this.embeddingClient = embeddingClient;
}
public List<Double> embed(String text) {
EmbeddingResponse response = embeddingClient.embedForResponse(
List.of(text)
);
return response.getResult().getOutput();
}
public List<List<Double>> embedBatch(List<String> texts) {
EmbeddingResponse response = embeddingClient.embedForResponse(texts);
return response.getResult().getOutput();
}
}
5. Vector Store
é ç½®ï¼
spring:
ai:
vectorstore:
pgvector:
index-type: HNSW
distance-type: COSINE_DISTANCE
ä½¿ç¨ VectorStoreï¼
@Service
public class VectorStoreService {
private final VectorStore vectorStore;
private final EmbeddingClient embeddingClient;
public VectorStoreService(
VectorStore vectorStore,
EmbeddingClient embeddingClient
) {
this.vectorStore = vectorStore;
this.embeddingClient = embeddingClient;
}
public void addDocument(String id, String content) {
List<Double> embedding = embeddingClient.embed(content);
Document document = new Document(id, content, Map.of());
vectorStore.add(List.of(document));
}
public List<Document> searchSimilar(String query, int topK) {
List<Double> queryEmbedding = embeddingClient.embed(query);
return vectorStore.similaritySearch(
SearchRequest.query(query)
.withTopK(topK)
);
}
}
6. Function Calling
å®ä¹å½æ°ï¼
@Bean
public Function<WeatherRequest, WeatherResponse> weatherFunction() {
return request -> {
// è°ç¨å¤©æ° API
WeatherResponse response = weatherService.getWeather(
request.getLocation()
);
return response;
};
}
é ç½® Function Callingï¼
@Configuration
public class FunctionCallingConfig {
@Bean
public Function<WeatherRequest, WeatherResponse> weatherFunction() {
return request -> {
// å®ç°å¤©æ°æ¥è¯¢é»è¾
return new WeatherResponse(/* ... */);
};
}
}
ä½¿ç¨ Function Callingï¼
@Service
public class ChatService {
private final ChatClient chatClient;
private final FunctionCallbackRegistry functionCallbackRegistry;
public ChatService(
ChatClient chatClient,
FunctionCallbackRegistry functionCallbackRegistry
) {
this.chatClient = chatClient;
this.functionCallbackRegistry = functionCallbackRegistry;
}
public String chatWithFunction(String message) {
Prompt prompt = new Prompt(
new UserMessage(message),
functionCallbackRegistry.getFunctionCallbacks()
);
ChatResponse response = chatClient.call(prompt);
return response.getResult().getOutput().getContent();
}
}
7. 夿¨¡åæ¯æ
é ç½®å¤ä¸ªæ¨¡åï¼
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
anthropic:
api-key: ${ANTHROPIC_API_KEY}
azure:
openai:
api-key: ${AZURE_OPENAI_API_KEY}
endpoint: ${AZURE_OPENAI_ENDPOINT}
使ç¨ç¹å®æ¨¡åï¼
@Service
public class MultiModelService {
private final ChatClient openAiChatClient;
private final ChatClient anthropicChatClient;
public MultiModelService(
@Qualifier("openAiChatClient") ChatClient openAiChatClient,
@Qualifier("anthropicChatClient") ChatClient anthropicChatClient
) {
this.openAiChatClient = openAiChatClient;
this.anthropicChatClient = anthropicChatClient;
}
public String chatWithOpenAI(String message) {
return openAiChatClient.call(message);
}
public String chatWithAnthropic(String message) {
return anthropicChatClient.call(message);
}
}
æä½³å®è·µ
1. é 置管ç
- 使ç¨ç¯å¢åéåå¨ API Key
- åºåå¼ååç产ç¯å¢é ç½®
- é ç½®åççè¶ æ¶åéè¯çç¥
2. é误å¤ç
@Service
public class ChatService {
private final ChatClient chatClient;
public String chat(String message) {
try {
return chatClient.call(message);
} catch (Exception e) {
// å¤çé误
log.error("Chat error", e);
return "æ±æï¼å¤çè¯·æ±æ¶åºç°é误";
}
}
}
3. æ§è½ä¼å
- ä½¿ç¨æµå¼ååºæåç¨æ·ä½éª
- åç使ç¨ç¼ååå° API è°ç¨
- æ¹éå¤ç Embedding 请æ±
4. ææ¬æ§å¶
- éæ©åéçæ¨¡åï¼GPT-3.5 vs GPT-4ï¼
- éå¶ Token 使ç¨é
- çæ§ API è°ç¨æ åµ
常ç¨ä¾èµ
<!-- OpenAI -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<!-- Anthropic -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-anthropic-spring-boot-starter</artifactId>
</dependency>
<!-- Azure OpenAI -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>
<!-- Vector Store (PGVector) -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
ç¤ºä¾ Prompt
- “å¦ä½ä½¿ç¨ Spring AI éæ OpenAIï¼”
- “Spring AI ä¸å¦ä½å®ç°æµå¼ååºï¼”
- “å¦ä½å¨ Spring AI ä¸ä½¿ç¨ Embedding å Vector Storeï¼”
- “Spring AI ä¸å¦ä½å®ç° Function Callingï¼”
- “å¦ä½é ç½® Spring AI æ¯æå¤ä¸ªæ¨¡åï¼”