Profile picture

[Python] Google Bard API 사용해보기

JaehyoJJAng2023년 11월 04일

▶︎ Gemini API

  • 구 Bard
  • Google AI에서 만든 대규모 언어 모델
  • 무료로 사용가능
  • 한글 지원
  • 구글 계정 로그인 시 무료로 이용 가능
  • https://bard.google.com

▶︎ 토큰 발급

Bard의 경우 아직 정식 API 서비스를 지원하지 않고 있음.
정식 API를 지원하지 않기에 API 토큰을 발급받는 방법은 현재로써는 없지만 아래와 같은 과정을 통하여 토큰을 얻어올 수 있음


크롬 브라우저를 열고 https://bard.google.com에 접속하여 구글 계정에 로그인
image


로그인 한 뒤, F12를 눌러 개발자 도구를 열자
image


개발자 도구 상단 메뉴에서 "Application"을 클릭
image


왼쪽 탭에서 "Cookies" - "https://bard.google.com" 을 클릭
image


__Secure-1PSIDCC, __Secure-1PSID, __Secure-1PSIDTS 각각 클릭
image


하단의 "Cookie Value"의 값을 복사
image


▶︎ gemini api 설치


Google Bard 파이썬 패키지인 bardapi를 설치

pip install bardapi

‣ 텍스트 생성 코드

  • 입력된 질문에 대한 답변을 텍스트로 반환

{% include codeHeader.html name="generate_text_bard.py" %}

from bardapi import BardCookies

# Cookies may vary by account or region. Consider sending the entire cookie file.
cookie_dict: dict[str,str] = {
    "__Secure-1PSIDCC" : "value",
    "__Secure-1PSID" : "value",
    "__Secure-1PSIDTS" : "value",
}

bard = BardCookies(cookie_dict=cookie_dict,timeout=30)
answer: str = bard.get_answer("인생이란 뭘까")['content']
print(answer)

image

  • dict 형식으로 쿠키를 명시적으로 선언

▶︎ Reference

https://github.com/dsdanielpark/Bard-API


Loading script...