Spring Boot整合Google Bard – Web接口拜访Google AI聊天机器人
之前开发了一个对于Google Bard
的Java库,能够帮忙咱们简略的发问并取得答案。当初我把它整合到Spring Boot
利用中,通过Web API让大家能够拜访。
增加依赖
把pkslow google bard
增加到Spring Boot
我的项目中去:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.pkslow</groupId>
<artifactId>google-bard</artifactId>
<version>0.0.3</version>
</dependency>
</dependencies>
创立GoogleBardClient
在应用它之前,咱们须要创立一个对应的GoogleBardClient
Bean:
@Configuration
public class GoogleBardConfig {
@Bean
public GoogleBardClient googleBardClient(@Value("${ai.google-bard.token}") String token) {
return new GoogleBardClient(token);
}
}
BardController
把GoogleBardClient
对象注入,通过HTTP GET办法来发问。所以Controller要从GET申请中获取问题,并向Google Bard
发问:
@RestController
@RequestMapping("/google-bard")
public class BardController {
private final GoogleBardClient client;
public BardController(GoogleBardClient client) {
this.client = client;
}
@GetMapping("/ask")
public BardAnswer ask(@RequestParam("q") String question) {
Answer answer = client.ask(question);
if (answer.status() == AnswerStatus.OK) {
return new BardAnswer(answer.chosenAnswer(), answer.draftAnswers());
}
if (answer.status() == AnswerStatus.NO_ANSWER) {
return new BardAnswer("No Answer", null);
}
throw new RuntimeException("Can't access to Google Bard");
}
}
获取到答案后,咱们返回一个对应的DTO对象。
配置
须要配置一个用于鉴权的Token:
server.port=8088
ai.google-bard.token=UgiXYPjpaIYuE9K_3BSqCWnT2W**********************
如何获取token
拜访: https://bard.google.com/
- F12
-
找到Cookie并复制
- Session: Go to Application → Cookies →
__Secure-1PSID
.
- Session: Go to Application → Cookies →
通过Postman测试
Question 1: How to be a good father?
Queston 2: what is pkslow.com?
Log:
代码
整合的代码在GitHub.
References:
- Try out Google Bard, Will Google Bard beat the ChatGPT?
- Use Python to Build a Google Bard Chatbot Client Locally
- Java Library for Google Bard to Ask Questions and Receive Answers
发表回复