GenAI_Chatbot / README copy.md
Ridealist's picture
Init project
2872c06
|
raw
history blame
6.51 kB

Streamlit Chatbot

streamlit-chat์€ Streamlit์„ ์ด์šฉํ•˜์—ฌ ์‹ค์‹œ๊ฐ„ ๋Œ€ํ™”ํ˜• Web ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋ฉฐ ์˜ˆ์ œ ํ™•์ธ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

streamlit-chat์œผ๋กœ Hugging Face์—์„œ ์ œ๊ณตํ•˜๋Š” Facebook AI BlenderBot๊ณผ OpenAI์˜ GPT-3 ๋ชจ๋ธ๋กœ ์ฑ—๋ด‡ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


1. ์„ค์น˜ (Installation)

streamlit๊ณผ streamlit-chat ํŒจํ‚ค์ง€ ์„ค์น˜๋Š” pip ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

pip install streamlit
pip install streamlit-chat

streamlit ์„ค์น˜์™€ ๊ด€๋ จ๋œ ๋‚ด์šฉ์€ ์ด์ „ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

Streamlit ์‹œ์ž‘ํ•˜๊ธฐ (์„ค์น˜๋ฐฉ๋ฒ•)

2. BlenderBot ์ฑ—๋ด‡

BlenderBot์€ Hugging Face์˜ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € Hugging Face Inference API Key ๋ฐœ๊ธ‰์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

Hugging Face Inference API Key ๋ฐœ๊ธ‰

Python ํŒŒ์ผ blenderbot_app.py ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. API_TOKEN์€ ์ž์‹ ์˜ Hugging Face Inference API Key ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: hf_xxxxxxxxxxxxxxxxxxxxx)

import streamlit as st
from streamlit_chat import message
import requests
 
API_URL = "https://api-inference.huggingface.co/models/facebook/blenderbot-400M-distill"
API_TOKEN = "YOUR API TOKEN HERE"
headers = {"Authorization": f"Bearer {API_TOKEN}"}
 
st.header("๐Ÿค–Yunwoong's BlenderBot (Demo)")
st.markdown("[Be Original](https://yunwoong.tistory.com/)")
 
if 'generated' not in st.session_state:
    st.session_state['generated'] = []
 
if 'past' not in st.session_state:
    st.session_state['past'] = []
 
def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()
 
 
with st.form('form', clear_on_submit=True):
    user_input = st.text_input('You: ', '', key='input')
    submitted = st.form_submit_button('Send')
 
if submitted and user_input:
    output = query({
        "inputs": {
            "past_user_inputs": st.session_state.past,
            "generated_responses": st.session_state.generated,
            "text": user_input,
        },
        "parameters": {"repetition_penalty": 1.33},
    })
 
    st.session_state.past.append(user_input)
    st.session_state.generated.append(output["generated_text"])
 
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))

ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด Streamlit ์•ฑ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

streamlit run blenderbot_app.py

3. GPT-3 ์ฑ—๋ด‡

๋‹ค์Œ์œผ๋กœ OpenAI API๋ฅผ ์ด์šฉํ•œ GPT-3 ์ฑ—๋ด‡์„ ๋งŒ๋“ค๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € OpenAI API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด API ํ‚ค ๋ฐœ๊ธ‰์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € OpenAI API ์‚ฌ์ดํŠธ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. OpenAI ๊ณ„์ •์ด ํ•„์š”ํ•˜๋ฉฐ ๊ณ„์ •์ด ์—†๋‹ค๋ฉด ๊ณ„์ • ์ƒ์„ฑ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ Google์ด๋‚˜ Microsoft ๊ณ„์ •์„ ์—ฐ๋™ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ๊ณ„์ •์ด ์žˆ๋‹ค๋ฉด ๋กœ๊ทธ์ธ ํ›„ ์ง„ํ–‰ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ์ด ๋˜์—ˆ๋‹ค๋ฉด ์šฐ์ธก ์ƒ๋‹จ Personal -> [ View API Keys ] ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

img

[ + Create new secret key ] ๋ฅผ ํด๋ฆญํ•˜์—ฌ API Key๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. API key generated ์ฐฝ์ด ํ™œ์„ฑํ™”๋˜๋ฉด Key ๋ฅผ ๋ฐ˜๋“œ์‹œ ๋ณต์‚ฌํ•˜์—ฌ ๋‘์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ฐฝ์„ ๋‹ซ์œผ๋ฉด ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. (๋งŒ์•ฝ ๋ณต์‚ฌํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ๋‹ค์‹œ Create new secret key ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ƒ์„ฑํ•˜๋ฉด ๋˜๋‹ˆ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค.)

Python ํŒŒ์ผ chatgpt_app.py ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. openai.api_key๋Š” ์ž์‹ ์˜ OpenAI API Key๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: sk-xxxxxxxxxxxxxxxxxxxxx)

import openai
import streamlit as st
from streamlit_chat import message
 
openai.api_key = 'YOUR API KEY HERE'
 
def generate_response(prompt):
    completions = openai.Completion.create (
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        stop=None,
        temperature=0,
        top_p=1,
    )
 
    message = completions["choices"][0]["text"].replace("\n", "")
    return message
 
 
st.header("๐Ÿค–Yunwoong's ChatGPT-3 (Demo)")
st.markdown("[Be Original](https://yunwoong.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=True):
    user_input = st.text_input('You: ', '', key='input')
    submitted = st.form_submit_button('Send')
 
if submitted and user_input:
    output = generate_response(user_input)
    st.session_state.past.append(user_input)
    st.session_state.generated.append(output)
 
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))

ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด Streamlit ์•ฑ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

streamlit run chatgpt_app.py

๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ Web ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด ์‹œ๋ฎฌ๋ ˆ์ด์…˜์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์ผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ ๊ณผํ•™์ž๋‚˜ AI ๋ชจ๋ธ๋Ÿฌ์ธ ๊ฒฝ์šฐ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๋ ค๋ฉด ์‹œ๊ฐ„๊ณผ ๋…ธ๋ ฅ์ด ๋งŽ์ด ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ Streamlit์„ ์ด์šฉํ•œ๋‹ค๋ฉด ์ด ๊ณผ์ •์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.