Java接入通义千问 前言引入依赖配置API Key与主机名Java代码调用 前言 通义千问是阿里巴巴达摩院研发的预训练语言模型,提供了一系列的api和SDK可以方便地进行接入。本文将介绍如何使用SpringBoot接入通义千问
通义千问是阿里巴巴达摩院研发的预训练语言模型,提供了一系列的api和SDK可以方便地进行接入。本文将介绍如何使用SpringBoot接入通义千问,并实现搜索功能。
首先,我们需要在pom.xml文件中添加以下依赖项:
com.aliyun aliyun-open-api-core 4.2.6 com.aliyun aliyun-open-api-common 4.2.6 在application.properties文件中,配置以下内容,设置通义千问的API key和主机名:
aliyun.openapi.endpoint=https://ai.aliyun.com/openapi/v4/appid/[your_app_id]/[your_endpoint]aliyun.openapi.private_key=your_private_keyaliyun.openapi.host=[your_host] 其中,[your_app_id]和[your_endpoint]分别代表通义千问的应用ID和请求端点,需要根据实际情况进行填写。
在Controller类中,添加以下方法来调用通义千问的API:
@RestController@RequestMapping("/api")public class MyController { @Autowired private MyService myService; @GetMapping("/search") public String search(@RequestParam String query) { List helloList = myService.search(query); String helloString = String.fORMat("{%s}", helloList); return helloString; }} 其中,MyService是自定义的服务类,需要根据实际情况进行编写。
在myService类中,添加以下方法来调用通义千问的API:
public List search(String query) { String url = "Https://ai.aliyun.com/openapi/v4/appid/[your_app_id]/[your_endpoint]?query=" + query; List helloList = null; try { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); if (con.getResponseCode() != 200) { return null; } BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); helloList = new Gson().fromJSON(response.toString(), new TypeToken>() { }.getType()); } catch (Exception e) { e.printStackTrace(); } return helloList;}
其中,[your_app_id]和[your_endpoint]分别代表通义千问的应用ID和请求端点,需要根据实际情况进行填写。
接下来,我们可以在springBoot应用程序中,启动HTTP服务器并打开浏览器访问http://localhost:8080/api/search,即可看到通义千问的搜索结果。
以上就是一个简单的通义千问接入示例,具体的实现方式可以根据实际情况进行调整和完善。下面是完整的代码示例:
@Configurationpublic class ApplicationConfig { @Bean public EmbeddedServletContainer servletContainer() { TomcatEmbeddedServletContainer container = new TomcatEmbeddedServletContainer(); container.setPort(8080); return container; } @Bean public AnnotationConfigServletWEBmvcConfigurer mvcConfigurer() { AnnotationConfigServletWebMvcConfigurer c = new AnnotationConfigServletWebMvcConfigurer(); c.addInterceptors(new LoggingInterceptor()); return c; }} @RestController@RequestMapping("/api")public class MyController { @Autowired private MyService myService; @GetMapping("/search") public String search(@RequestParam String query) { List helloList = myService.search(query); String helloString = String.format("{%s}", helloList); return helloString; }}@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorReGIStry registry) { registry.addInterceptor(new LoggingInterceptor()); }} 在SpringBoot应用程序中,启动HTTP服务器并打开浏览器访问http://localhost:8080/api/search,即可看到通义千问的搜索结果。
以上就是一个简单的通义千问接入示例,具体的实现方式可以根据实际情况进行调整和完善。
来源地址:https://blog.csdn.net/hao_haiyang/article/details/131383541
--结束END--
本文标题: 【Java接入通义千问】
本文链接: https://www.lsjlt.com/news/373964.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0