When will you grow up?

BardAPI를 이용한 ChatBot 만들기 본문

02. Study/API

BardAPI를 이용한 ChatBot 만들기

미카이 2023. 6. 1. 17:56

이번에는 요즘 ChatGPT와 대립하여 많은 분들이 사용중인 Google에서 만든 BardAPI+Streamlit를 이용하여 ChatBot을 구현해보겠습니다.

Bard를 이용한 ChatBot 예시

Linux 환경 & Window 환경 둘 다 상관없이 잘 돌아가는 것을 확인했다.

1. 필요 Package 설치

pip install streamlit
pip install streamlit-chat
pip install bardapi

2. BardAPI 발급받기

주의사항 : 쿠키값(Cookie Value)이 주기적으로 변할 수 있으니 값을 항상 확인해주자

  • 크롬 브라우저에서 https://bard.google.com/ 방문하기
  • F12를 눌러 console 창 띄우기
  • Application -> Cookies -> __Secure-1PSID 값 복사하기

3. 복사한 값 os.environ['_BARD_API_KEY']에 넣기

- 아래 코드를 streamlit_bard.py 로 저장

# streamlit_bard.py 


import streamlit as st
from streamlit_chat import message
import requests
from bardapi import Bard
import os
import bardapi
os.environ['_BARD_API_KEY']="Insert the copied value here" # 저장한 값 변경


st.header("👨‍💻Bard ChatBot👨‍💻")
st.markdown("[Demo Site](boysboy3.tistory.com)")

if 'generated' not in st.session_state:
    st.session_state['generated'] = []

if 'past' not in st.session_state:
    st.session_state['past'] = []



with st.form('form', clear_on_submit=False):
    user_input = st.text_input('You: ', '', key='input')
    submitted = st.form_submit_button('Send')

if submitted or user_input:
    response = bardapi.core.Bard().get_answer(user_input)

    st.session_state.past.append(user_input)
    st.session_state.generated.append(response['content'])

if st.session_state['generated']:
    for i in range(len(st.session_state['generated'])-1, -1, -1):
        message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
        message(st.session_state["generated"][i], key=str(i))

4. 실행

아래 명령어를 cmd를 통해 실행 시킨 후 http://localhost:8501/로 접속 가능

streamlit run streamlit_bard.py

 

 

 

 

* Reference

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

https://github.com/AI-Yash/st-chat

Comments