【Java接入通义千问】

前言

通义千问是阿里巴巴达摩院研发的预训练语言模型,提供了一系列的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,即可看到通义千问的搜索结果。

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

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java接口是一种抽象的数据类型,它定义了一组方法的集合。通常情况下,接口中的方法没有实现代码,只是定义了方法的名称、参数列表和返回类型。接口可以看作是一种契约,表示实现该接口的类必须提供接口中定义的方法。 通义千问Java接口指的是在Java编程中常见的关于接口的一系列问题。这些问题涵盖了接口的定义、实现、使用等方面。 例如,“如何定义一个接口?”。“一个类可以实现多个接口吗?”、“接口中的方法可以是静态方法吗?”、“接口和抽象类有什么区别?”、“接口可以继承其他接口吗?”等等。 在Java中,定义接口使用关键字“interface”,通过定义一个接口,可以规定类必须遵循的方法和规范。一个类可以通过实现一个或多个接口来实现接口中定义的方法。接口中的方法默认是公有的、抽象的,所以在实现接口时需要将这些方法进行具体实现。 接口可以被其他接口继承,通过继承可以扩展接口的功能。接口还可以和抽象类进行比较,在某些方面有相似的地方,但也有一些区别。接口可以看作是对所有实现了接口的类的一种承诺,表示类将提供接口中定义的方法。 总之,通义千问Java接口是指在Java编程中关于接口的一系列问题,包括接口的定义、实现和使用等方面。了解和掌握这些问题,对于编写符合Java规范的代码以及实现代码的复用和扩展具有重要作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值