当前位置:   article > 正文

【Ai生态开发】Spring AI上架,打造专属业务大模型,AI开发再也不是难事!_spring ai框架

spring ai框架

大家好 这里是苏泽 后端是工作 ai是兴趣 

对于ai的产生我的立场是拥抱ai的  是希望拿他作为提升能力的工具  那么这一篇带大家来学习如何使用ai打造一个专属的业务大模型 

需求 就是说假设现在有一个 商城系统 里面有查询订单的api和获取商品购买方式的api   用户只需要输入 “帮我看看我前几天买过最便宜的衣服”  经过语言处理 ai就能够调用 查询订单的api并在里面自动的添加查询条件以及 排序条件  这是我们的目标  本文就是来讲解实现这样的目标

Spring AI介绍

Spring AI 是 AI 工程师的一个应用框架,它提供了一个友好的 API 和开发 AI 应用的抽象,旨在简化 AI 应用的开发工序。

提供对常见模型的接入能力,目前已经上架 https://start.spring.io/,提供大家测试访问。(请注意虽然已经上架 start.spring.io,但目前还是在 Spring 私服,未发布至 Maven 中央仓库)

基本知识讲解:

函数调用

函数调用(Function Calling)是OpenAI在2023年6月13日对外发布的新能力。根据OpenAI官方博客描述,函数调用能力可以让大模型输出一个请求调用函数的消息,其中包含所需调用的函数信息、以及调用函数时所携带的参数信息。这是一种将大模型(LLM)能力与外部工具/API连接起来的新方式。

比如用户输入:

What’s the weather like in Tokyo?

使用function calling,可实现函数执行get_current_weather(location: string),从而获取函数输出,即得到对应地理位置的天气情况。这其中,location这个参数及其取值是借助大模型能力从用户输入中抽取出来的,同时,大模型判断得到调用的函数为get_current_weather

开发人员可以使用大模型的function calling能力实现:

  • 在进行自然语言交流时,通过调用外部工具回答问题(类似于ChatGPT插件);
  • 将自然语言转换为调用API调用,或数据库查询语句;
  • 从文本中抽取结构化数据
  • 其它

实现步骤

1. 添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.ai</groupId>
  7. <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
  8. </dependency>
  9. <!-- 配置 Spring 仓库 -->
  10. <repositories>
  11. <repository>
  12. <id>spring-milestones</id>
  13. <name>Spring Milestones</name>
  14. <url>https://repo.spring.io/milestone</url>
  15. <snapshots>
  16. <enabled>false</enabled>
  17. </snapshots>
  18. </repository>
  19. </repositories>

2. 配置 OpenAI 相关参数

spring:
  ai:
    openai:
      base-url: # 支持 openai-sb、openai-hk 等中转站点,如用官方则不填
      api-key: sk-xxxx
 

3.创建一个Spring Controller处理HTTP请求。

在Spring项目中创建一个Controller类,用于处理提取要素的HTTP请求和生成调用的API和变量集合。

  1. import com.google.gson.Gson;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. @RestController
  10. public class ElementExtractionController {
  11. @Autowired
  12. private ElementExtractionService elementExtractionService;
  13. @PostMapping("/extract-elements")
  14. public ResponseEntity<Map<String, Object>> extractElements(@RequestBody String userInput) {
  15. Map<String, Object> result = elementExtractionService.extractElements(userInput);
  16. return ResponseEntity.ok(result);
  17. }
  18. }

3.创建一个ElementExtractionService服务类来提取要素

创建一个服务类,用于封装提取要素的逻辑。在这个服务类中,可以使用自然语言处理技术来分析用户输入并提取需求和变量。可以使用现有的开源NLP库或API,如NLTK、SpaCy、Stanford CoreNLP、Google Cloud Natural Language API等
这里使用NLTK库来进行文本分析和实体识别,以提取用户输入中的需求和变量:

  1. import org.springframework.stereotype.Service;
  2. import edu.stanford.nlp.simple.Document;
  3. import edu.stanford.nlp.simple.Sentence;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. @Service
  8. public class ElementExtractionService {
  9. public Map<String, Object> extractElements(String userInput) {
  10. // 使用NLTK库进行文本分析和实体识别
  11. Document doc = new Document(userInput);
  12. List<Sentence> sentences = doc.sentences();
  13. // 提取需求
  14. String requirement = extractRequirement(sentences);
  15. // 提取变量
  16. Map<String, String> variables = extractVariables(sentences);
  17. // 构建结果
  18. Map<String, Object> result = new HashMap<>();
  19. result.put("api", requirement);
  20. result.put("variables", variables);
  21. return result;
  22. }
  23. private String extractRequirement(List<Sentence> sentences) {
  24. // 在这里根据实际需求,从句子中提取需求
  25. // 可以使用关键词提取、模式匹配等方法
  26. // 这里示例直接返回第一句话作为需求
  27. if (!sentences.isEmpty()) {
  28. return sentences.get(0).text();
  29. }
  30. return "";
  31. }
  32. private Map<String, String> extractVariables(List<Sentence> sentences) {
  33. // 在这里根据实际需求,从句子中提取变量
  34. // 可以使用实体识别、关键词提取等方法
  35. // 这里示例直接从第一句话中提取名词作为变量
  36. Map<String, String> variables = new HashMap<>();
  37. if (!sentences.isEmpty()) {
  38. Sentence sentence = sentences.get(0);
  39. for (String word : sentence.words()) {
  40. if (isNoun(word)) {
  41. variables.put(word, "true");
  42. }
  43. }
  44. }
  45. return variables;
  46. }
  47. private boolean isNoun(String word) {
  48. // 在这里根据实际需求,判断一个词是否为名词
  49. // 可以使用词性标注、词典匹配等方法
  50. // 这里示例简单判断是否以大写字母开头,作为名词的判断条件
  51. return Character.isUpperCase(word.charAt(0));
  52. }
  53. }

那么下一步 :


4.封装一个API来操作open ai的Assistants API

创建一个Spring Service来操作OpenAI Assistants API。

创建一个服务类,用于封装操作OpenAI Assistants API的逻辑。

  1. import com.google.gson.Gson;
  2. import okhttp3.*;
  3. import org.springframework.stereotype.Service;
  4. import java.io.IOException;
  5. @Service
  6. public class OpenAIAssistantsService {
  7. public String callOpenAIAssistantsAPI(String prompt) {
  8. OkHttpClient client = new OkHttpClient();
  9. MediaType mediaType = MediaType.parse("application/json");
  10. JsonObject requestBody = new JsonObject();
  11. requestBody.addProperty("prompt", prompt);
  12. requestBody.addProperty("max_tokens", 32);
  13. requestBody.addProperty("stop", null);
  14. RequestBody body = RequestBody.create(mediaType, requestBody.toString());
  15. Request request = new Request.Builder()
  16. .url(OPENAI_API_URL)
  17. .post(body)
  18. .addHeader("Authorization", "Bearer " + OPENAI_API_KEY)
  19. .build();
  20. try {
  21. Response response = client.newCall(request).execute();
  22. if (response.isSuccessful()) {
  23. String responseBody = response.body().string();
  24. JsonObject jsonObject = new Gson().fromJson(responseBody, JsonObject.class);
  25. return jsonObject.getAsJsonObject("choices")
  26. .get(0)
  27. .getAsJsonObject()
  28. .get("text")
  29. .getAsString();
  30. } else {
  31. System.out.println("OpenAI Assistants API调用失败: " + response.code() + " - " + response.message());
  32. }
  33. } catch (IOException e) {
  34. System.out.println("OpenAI Assistants API调用异常: " + e.getMessage());
  35. }
  36. return null;
  37. }
  38. }

创建一个自定义函数签名。

创建一个函数,它将调用其他项目中的API,并返回结果。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class CustomFunctionService {
  5. @Autowired
  6. private OtherAPIService otherAPIService;
  7. public String customFunction(String apiId, String inputParameters) {
  8. // 根据API的ID筛选需要调用的API
  9. String apiEndpoint = getApiEndpoint(apiId);
  10. // 调用其他项目中的API,并进行处理
  11. String result = otherAPIService.callOtherAPI(apiEndpoint, inputParameters);
  12. // 对结果进行处理,并返回
  13. return "处理后的结果:" + result;
  14. }
  15. private String getApiEndpoint(String apiId) {//这里还会有很多具体业务的api就不一一列举了
  16. // 根据API的ID获取相应的API的URL或其他信息
  17. // 这里可以根据实际情况进行实现
  18. if (apiId.equals("api1")) {
  19. return "https://api.example.com/api1";
  20. } else if (apiId.equals("api2")) {
  21. return "https://api.example.com/api2";
  22. } else {
  23. throw new IllegalArgumentException("无效的API ID: " + apiId);
  24. }
  25. }
  26. }

创建一个Spring Controller来调用自定义函数。

创建一个Controller类,它将调用自定义函数,并返回结果。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.http.ResponseEntity;
  3. import org.springframework.web.bind.annotation.PostMapping;
  4. import org.springframework.web.bind.annotation.RequestBody;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. @RestController
  9. public class CustomFunctionController {
  10. @Autowired
  11. private CustomFunctionService customFunctionService;
  12. @PostMapping("/call-custom-function")
  13. public ResponseEntity<String> callCustomFunction(@RequestBody String userInput) {
  14. String result = customFunctionService.customFunction(userInput);
  15. return ResponseEntity.ok(result);
  16. }
  17. }

在上面提取要素的服务(ElementExtractionService)的基础上,我们可以再封装一个Assistants服务,它将接受用户的请求并调用提取要素的服务。然后,Assistants服务将提取的要素和变量(uid)作为输入传递给封装了OpenAI的服务(OpenAIAssistantsService),并根据要素选择适当的API进行调用,并返回对应的结果。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. import java.util.Map;
  4. @Service
  5. public class AssistantsService {
  6. @Autowired
  7. private ElementExtractionService elementExtractionService;
  8. @Autowired
  9. private OpenAIAssistantsService openAIAssistantsService;
  10. public String processUserRequest(String userInput) {
  11. // 提取要素
  12. Map<String, Object> elements = elementExtractionService.extractElements(userInput);
  13. // 获取要素和变量
  14. String requirement = (String) elements.get("api");
  15. Map<String, String> variables = (Map<String, String>) elements.get("variables");
  16. String uid = (String) elements.get("uid");
  17. // 调用OpenAI Assistants服务
  18. String result = openAIAssistantsService.callOpenAIAssistantsAPI(requirement, variables, uid);
  19. return result;
  20. }
  21. }

AssistantsService类接受用户的请求,并调用ElementExtractionService来提取要素。然后,它获取要素、变量和uid,并将它们作为参数传递给OpenAIAssistantsService的callOpenAIAssistantsAPI方法。该方法根据要素选择适当的API进行调用,并返回结果。

具体的业务实现“提取要素”的逻辑部分

请注意,为了实现这个过程,还需要修改ElementExtractionService中提取要素的逻辑,以确保这个服务能符合具体业务的逻辑  例如我提到的 “帮我看看我买过最便宜的衣服”

  1. import org.springframework.stereotype.Service;
  2. import edu.stanford.nlp.simple.Document;
  3. import edu.stanford.nlp.simple.Sentence;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. @Service
  8. public class ElementExtractionService {
  9. public Map<String, Object> extractElements(String userInput) {
  10. // 使用NLTK库进行文本分析和实体识别
  11. Document doc = new Document(userInput);
  12. List<Sentence> sentences = doc.sentences();
  13. // 提取需求
  14. String requirement = extractRequirement(sentences);
  15. // 提取变量
  16. Map<String, String> variables = extractVariables(sentences);
  17. // 构建结果
  18. Map<String, Object> result = new HashMap<>();
  19. result.put("api", requirement);
  20. result.put("variables", variables);
  21. return result;
  22. }
  23. private String extractRequirement(List<Sentence> sentences) {
  24. // 在这里根据实际需求,从句子中提取需求
  25. // 可以使用关键词提取、模式匹配等方法
  26. // 这里示例直接返回第一句话作为需求
  27. if (!sentences.isEmpty()) {
  28. return sentences.get(0).text();
  29. }
  30. return "";
  31. }
  32. private Map<String, String> extractVariables(List<Sentence> sentences) {
  33. // 在这里根据实际需求,从句子中提取变量
  34. // 可以使用实体识别、关键词提取等方法
  35. // 这里示例从第一句话中提取名词作为变量,并根据特定模式进行匹配
  36. Map<String, String> variables = new HashMap<>();
  37. if (!sentences.isEmpty()) {
  38. Sentence sentence = sentences.get(0);
  39. List<String> words = sentence.words();
  40. for (int i = 0; i < words.size() - 1; i++) {
  41. String currentWord = words.get(i);
  42. String nextWord = words.get(i + 1);
  43. if (isNoun(currentWord) && nextWord.equals("的")) {
  44. variables.put(currentWord, "true");
  45. }
  46. }
  47. }
  48. return variables;
  49. }
  50. private boolean isNoun(String word) {
  51. // 在这里根据实际需求,判断一个词是否为名词
  52. // 可以使用词性标注、词典匹配等方法
  53. // 这里示例简单判断是否以大写字母开头,作为名词的判断条件
  54. return Character.isUpperCase(word.charAt(0));
  55. }
  56. }

我将extractVariables方法进行了修改。现在它从第一句话中提取名词作为变量,并且根据特定模式进行匹配。特定模式是判断当前词是否为名词,以及下一个词是否为"的"。如果匹配成功,则将当前词作为变量存储。

这样我们就基本实现了一开始的那个目标:

假设现在有一个 商城系统 里面有查询订单的api和获取商品购买方式的api   用户只需要输入 “帮我看看我前几天买过最便宜的衣服”  经过语言处理 ai就能够调用 查询订单的api并在里面自动的添加查询条件以及 排序条件  这是我们的目标  本文就是来讲解实现这样的目标

更长远的目标:

希望能够开发出一款中间件(作为一个服务被注册到项目当中) 能够作为open ai 和具体项目的桥梁  即在开发配置当中我输入我的已有项目的服务的签名   那这个助手能够根据用户的自然语言输入 自动的去调用执行 项目中已有的各种服务 来做各种各样的复杂的数据库查询 等操作

本文所受启发 参考文献:

  1. Function calling and other API updates: https://openai.com/blog/function-calling-and-other-api-updates
  2. OpenAI assistants in LangChain: https://python.langchain.com/docs/modules/agents/agent_types/openai_assistants
  3. Multi-Input Tools in LangChain: https://python.langchain.com/docs/modules/agents/tools/multi_input_tool
  4. examples/Assistants_API_overview_python.ipynb: https://github.com/openai/opena...
  5. The Spring Boot Actuator is the one dependency you should include in every project (danvega.dev)
  6. Assistants API won't allow external web request - API - OpenAI Developer Forum

 

本文只是简单提供一个可行的思路做参考 真正做出可拓展性的ai开发插件道路还很长 先在这立个小flag吧  希望今年能够完成这个小目标  如果有一起开发这个项目的伙伴可以跟我来讨论哦

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/320374
推荐阅读
相关标签
  

闽ICP备14008679号