• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java接入通义千问

武飞扬头像
郝先生吖
帮助1

前言

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

引入依赖

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

<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>

配置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_key
aliyun.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<Hello> helloList = myService.search(query);
        String helloString = String.format("{%s}", helloList);
        return helloString;
    }
}

其中,MyService是自定义的服务类,需要根据实际情况进行编写。

在myService类中,添加以下方法来调用通义千问的API:

public List<Hello> search(String query) {
    String url = "https://ai.aliyun.com/openapi/v4/appid/[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 (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<List<Hello>>() {
        }.getType());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return helloList;
}
学新通

其中,[your_app_id]和[your_endpoint]分别代表通义千问的应用ID和请求端点,需要根据实际情况进行填写。

接下来,我们可以在SpringBoot应用程序中,启动HTTP服务器并打开浏览器访问http://localhost:8080/api/search,即可看到通义千问的搜索结果。

@Configuration
public 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<Hello> helloList = myService.search(query);
        String helloString = String.format("{%s}", helloList);
        return helloString;
    }
}

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoggingInterceptor());
    }

}
学新通

在SpringBoot应用程序中,启动HTTP服务器并打开浏览器访问http://localhost:8080/api/search,即可看到通义千问的搜索结果。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiebhai
系列文章
更多 icon
同类精品
更多 icon
继续加载