Java接入通義千問的簡單方法示例
前言
通義千問是阿里巴巴達摩院研發(fā)的預(yù)訓(xùn)練語言模型,提供了一系列的API和SDK可以方便地進行接入。本文將介紹如何使用SpringBoot接入通義千問,并實現(xiàn)搜索功能。
引入依賴
首先,我們需要在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文件中,配置以下內(nèi)容,設(shè)置通義千問的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]分別代表通義千問的應(yīng)用ID和請求端點,需要根據(jù)實際情況進行填寫。
Java代碼調(diào)用
在Controller類中,添加以下方法來調(diào)用通義千問的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是自定義的服務(wù)類,需要根據(jù)實際情況進行編寫。
在myService類中,添加以下方法來調(diào)用通義千問的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]分別代表通義千問的應(yīng)用ID和請求端點,需要根據(jù)實際情況進行填寫。
接下來,我們可以在SpringBoot應(yīng)用程序中,啟動HTTP服務(wù)器并打開瀏覽器訪問http://localhost:8080/api/search,即可看到通義千問的搜索結(jié)果。
以上就是一個簡單的通義千問接入示例,具體的實現(xiàn)方式可以根據(jù)實際情況進行調(diào)整和完善。下面是完整的代碼示例:
@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應(yīng)用程序中,啟動HTTP服務(wù)器并打開瀏覽器訪問http://localhost:8080/api/search,即可看到通義千問的搜索結(jié)果。
以上就是一個簡單的通義千問接入示例,具體的實現(xiàn)方式可以根據(jù)實際情況進行調(diào)整和完善。
總結(jié)
到此這篇關(guān)于Java接入通義千問的文章就介紹到這了,更多相關(guān)Java接入通義千問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中的@ConfigurationProperties在方法上的使用詳解
這篇文章主要介紹了Spring中的@ConfigurationProperties在方法上的使用詳解,@ConfigurationProperties應(yīng)該經(jīng)常被使用到,作用在類上的時候,將該類的屬性取值?與配置文件綁定,并生成配置bean對象,放入spring容器中,提供給其他地方使用,需要的朋友可以參考下2024-01-01
SpringBoot+Vue+Redis實現(xiàn)單點登錄(一處登錄另一處退出登錄)
小編接到一個需求,需要實現(xiàn)用戶在瀏覽器登錄后,跳轉(zhuǎn)到其他頁面,當(dāng)用戶在其它地方又登錄時,前面用戶登錄的頁面退出登錄,這篇文章主要介紹了SpringBoot+Vue+Redis實現(xiàn)單點登錄,需要的朋友可以參考下2019-12-12
JAVA中實現(xiàn)原生的 socket 通信機制原理
本篇文章主要介紹了JAVA中實現(xiàn)原生的 socket 通信機制原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

