关于人工智能:Spring-Boot整合Google-Bard-Web接口访问Google-AI聊天机器人

9次阅读

共计 1724 个字符,预计需要花费 5 分钟才能阅读完成。

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.

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

正文完
 0