返回顶部
首页 > 资讯 > 后端开发 > JAVA >【Java接入通义千问】
  • 336
分享到

【Java接入通义千问】

java开发语言 2023-08-18 12:08:38 336人浏览 泡泡鱼
摘要

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

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

以上就是一个简单的通义千问接入示例,具体的实现方式可以根据实际情况进行调整和完善。

来源地址:https://blog.csdn.net/hao_haiyang/article/details/131383541

--结束END--

本文标题: 【Java接入通义千问】

本文链接: https://www.lsjlt.com/news/373964.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作