콘텐츠로 이동

학과소개

필요한 모듈 설치하기

!pip install openai
!pip install gradio

학과 소개 안내 챗봇

from openai import OpenAI
import gradio as gr

client = OpenAI()

def predict(message, history):

    history_openai_format = []
    history_openai_format.append({"role": "system", "content":"너는 이제 신소재공학과를 소개하는 안내자야"})
    history_openai_format.append({"role": "system", "content":"신소재공학과의 연락처는 010-1234-5678 이야."})
    history_openai_format.append({"role": "system", "content":"신소재공학과에는 10명의 교수님이 계셔."})
    history_openai_format.append({"role": "system", "content":"학과장님은 홍길동 교수님이야."})
    history_openai_format.append({"role": "system", "content":"재학생수는 400명이야."})

    for human, assistant in history:
        history_openai_format.append({"role": "user", "content": human })
        history_openai_format.append({"role": "assistant", "content":assistant})

    history_openai_format.append({"role": "user", "content": message})

    response = client.chat.completions.create(model='gpt-4o-mini',
    messages= history_openai_format,
    temperature=1.0,
    stream=True)

    partial_message = ""

    for chunk in response:
        if chunk.choices[0].delta.content is not None:
              partial_message = partial_message + chunk.choices[0].delta.content
              yield partial_message

with gr.Blocks() as demo:
    gr.Markdown(
    """
    # 신소재학과 안내 챗봇
    신소재학과에 대해서 궁금한 사항이 있으면 질문해 주세요.
    """)
    gr.ChatInterface(predict)

demo.launch()

신소재학과에 대해서 소개해줘
신소재학과의 연락처는?
신소재학과의 학과장님은?
신소재공학과의 교수님과 학생수는 총 몇명이지?