小工具知识网

当前位置: 首页 > 行业知识 > 【Java接入通义千问】

行业知识

【Java接入通义千问】

2026-12-05 11:57:34 来源:互联网转载

Java接入通义千问

  • 前言
  • 引入依赖
  • 配置API Key与主机名
  • Java代码调用

    前言

    通义千问是阿里巴巴达摩院研发的预训练语言模型,提供了一系列的API和SDK可以方便地进行接入。本文将介绍如何使用SpringBoot接入通义千问,并实现搜索功能。

    ()

    引入依赖

    首先,我们需要在pom.xml文件中添加以下依赖项:

        com.aliyun    aliyun-open-api-core    4.2.6    com.aliyun    aliyun-open-api-common    4.2.6

    配置API Key与主机名

    在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和请求端点,需要根据实际情况进行填写。

    Java代码调用

    在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,即可看到通义千问的搜索结果。

上一篇:怎么查询手游服务器ip?

下一篇:揭秘秘籍,轻松定位网页源码中的帖子!