Fly ai 4기 4주차 day 29 : 챗봇 만들기

이성원·2024년 1월 29일
0

시각화 웹사이트 https://streamlit.io/

챗봇 만들기

1. azure에 openai만들기

2. gpt 모델 선택 후 배포

3. 배포된 모델에서 key값, ENDPOINT값 가져오기

4. python 파일 하나 만든 후에 openai 패키지 설치

5. chatbot.py 코드

import openai

OPENAI_API_KEY='key값'
OPENAI_API_ENDPOINT='Entpoint 값'
OPENAI_API_TYPE = 'azure'
OPENAI_API_VERSION = '2023-05-15'

openai.api_key = OPENAI_API_KEY
openai.azure_endpoint = OPENAI_API_ENDPOINT
openai.api_type = OPENAI_API_TYPE
openai.api_version = OPENAI_API_VERSION

query = input('Query? ') # 원하는 질문 입력

result = openai.chat.completions.create(
        model = 'dev-model', # 생성한 모델 입력
        messages = [
            {'role':'system', 'content':'You are a helpful assistant.'},
            {'role':'user', 'content':query}
        ]
    )

print(result.choices[0].message.content)

Helen Keller was an American author, political activist, and lecturer. She was the first deaf-blind person to earn a bachelor of arts degree. Despite her disabilities, she became a renowned advocate for people with disabilities and an inspiration to many.

Query? who is helen keller

위 코드를 적용하면 질문에 대한 답변이 나온다.

temperature 파라미터를 추가하면 답변의 온도가 달라진다. ex) 0 = T, 1 = F 성격

6. streamlit 사용

streamlit 적용 코드

import openai 
import streamlit as st 
 
OPENAI_API_KEY='key값'
OPENAI_API_ENDPOINT='Entpoint 값'
OPENAI_API_TYPE = 'azure' 
OPENAI_API_VERSION = '2023-05-15' 

openai.api_key = OPENAI_API_KEY 
openai.azure_endpoint = OPENAI_API_ENDPOINT 
openai.api_type = OPENAI_API_TYPE 
openai.api_version = OPENAI_API_VERSION 



st.header('Welcome to GPT Bot', divider='rainbow') 
st.write('궁금한 것을 물어보세요!') 

query = st.text_input('Query? ') 
button_click = st.button('run') 

if button_click: 
    with st.spinner('please wait...'): 
        result = openai.chat.completions.create( 
                    model='dev-model', 
                    temperature=1, 
                    messages=[ 
                        {'role':'system','content':'You are a helpful assistant.'}, 
                        {'role':'user','content': query} 
                    ] 
                ) 
        st.success('done!') 

        st.write(result.choices[0].message.content) 

적용 화면

시 만들기

import openai 
import streamlit as st 
 
OPENAI_API_KEY='key값'
OPENAI_API_ENDPOINT='Entpoint 값'
OPENAI_API_TYPE = 'azure' 
OPENAI_API_VERSION = '2023-05-15' 

openai.api_key = OPENAI_API_KEY 
openai.azure_endpoint = OPENAI_API_ENDPOINT 
openai.api_type = OPENAI_API_TYPE 
openai.api_version = OPENAI_API_VERSION 



st.header('Welcome to AI Poem', divider='rainbow') 
st.write('아름다운 시를 함께 지어BoA Yo~~!!') 

name= st.text_input('작가의 이름을 입력하세요.')
if (name):
    st.write(name+'님 반갑습니다.')

subject = st.text_input('시의 주제를 입력하세요.') 

content = st.text_area('시의 내용을 입력하세요.')

button_click = st.button('RUN') 

if button_click: 
    with st.spinner('please wait...'): 
        result = openai.chat.completions.create( 
                    model='dev-model', 
                    temperature=1, 
                    messages=[ 
                        {'role':'system','content':'You are a helpful assistant.'}, 
                        {'role':'user','content': '작가의 이름은 '+ name},
                        {'role':'user','content': '시의 주제는 '+ subject},
                        {'role':'user','content': '시의 내용은 '+ content},
                        {'role':'user','content': '이 정보로 시를 생성해줘'}
                    ] 
                ) 
        st.success('done!') 

        st.write(result.choices[0].message.content) 

결과

profile
개발자

0개의 댓글

관련 채용 정보