通义千问是阿里巴巴达摩院研发的预训练语言模型,提供了一系列的API和SDK可以方便地进行接入。本文将介绍如何使用SpringBoot接入通义千问,并实现搜索功能。
首先,我们需要在l文件中添加以下依赖项:
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-open-api-core</artifactId><version>4.2.6</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-open-api-common</artifactId><version>4.2.6</version>
</dependency>
在application.properties文件中,配置以下内容,设置通义千问的API key和主机名:
dpoint=/[your_app_id]/[your_endpoint]
aliyun.openapi.private_key=your_private_key
aliyun.openapi.host=[your_host]
其中,[your_app_id]和[your_endpoint]分别代表通义千问的应用ID和请求端点,需要根据实际情况进行填写。
在Controller类中,添加以下方法来调用通义千问的API:
@RestController
@RequestMapping("/api")
public class MyController {@Autowiredprivate MyService myService;@GetMapping("/search")public String search(@RequestParam String query) {List<Hello> helloList = myService.search(query);String helloString = String.format("{%s}", helloList);return helloString;}
}
其中,MyService是自定义的服务类,需要根据实际情况进行编写。
在myService类中,添加以下方法来调用通义千问的API:
public List<Hello> search(String query) {String url = "/[your_app_id]/[your_endpoint]?query=" + query;List<Hello> helloList = null;try {URL obj = new URL(url);HttpURLConnection con = (HttpURLConnection) obj.openConnection();con.setRequestMethod("GET");if (ResponseCode() != 200) {return null;}BufferedReader in = new BufferedReader(new InputStream()));String inputLine;StringBuilder response = new StringBuilder();while ((inputLine = in.readLine()) != null) {response.append(inputLine);}in.close();helloList = new Gson().String(), new TypeToken<List<Hello>>() {}.getType());} catch (Exception e) {e.printStackTrace();}return helloList;
}
其中,[your_app_id]和[your_endpoint]分别代表通义千问的应用ID和请求端点,需要根据实际情况进行填写。
接下来,我们可以在SpringBoot应用程序中,启动HTTP服务器并打开浏览器访问localhost:8080/api/search,即可看到通义千问的搜索结果。
以上就是一个简单的通义千问接入示例,具体的实现方式可以根据实际情况进行调整和完善。下面是完整的代码示例:
@Configuration
public class ApplicationConfig {@Beanpublic EmbeddedServletContainer servletContainer() {TomcatEmbeddedServletContainer container = new TomcatEmbeddedServletContainer();container.setPort(8080);return container;}@Beanpublic AnnotationConfigServletWebMvcConfigurer mvcConfigurer() {AnnotationConfigServletWebMvcConfigurer c = new AnnotationConfigServletWebMvcConfigurer();c.addInterceptors(new LoggingInterceptor());return c;}}
@RestController
@RequestMapping("/api")
public class MyController {@Autowiredprivate MyService myService;@GetMapping("/search")public String search(@RequestParam String query) {List<Hello> helloList = myService.search(query);String helloString = String.format("{%s}", helloList);return helloString;}
}@Configuration
public class WebMvcConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoggingInterceptor());}}
在SpringBoot应用程序中,启动HTTP服务器并打开浏览器访问localhost:8080/api/search,即可看到通义千问的搜索结果。
以上就是一个简单的通义千问接入示例,具体的实现方式可以根据实际情况进行调整和完善。
本文发布于:2024-02-02 00:00:37,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170680908440010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |