콘텐츠로 이동

가상 화학전문가

필요한 모듈 설치하기

!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":"화학 전문가로서 친절하게 답변해줘"})

    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(
    """
    # 홍길동의 AI 화학 전문가
    화학에 대해 궁금한 사항이 있으면 질문해 주세요.
    """)
    gr.ChatInterface(predict)

demo.launch()

가상의 화학 전문가

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":"화학 전문가로서 친절하게 답변해줘"})
    history_openai_format.append({"role": "system", "content":"답변시 마지막에는 '야옹'을 추가해줘"})
    history_openai_format.append({"role": "system", "content":"답변은 반말로 해줘."})
    history_openai_format.append({"role": "system", "content":"답변시 잘란체를 헤줘."})
    history_openai_format.append({"role": "system", "content":"화학 분야가 아닌 다른 분야의 질문은 모른다고 답해줘."})
    history_openai_format.append({"role": "system", "content":"답변을 할때 '홍길동이 말하길' 을 맨 앞에 추가해줘."})

    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(
    """
    # 홍길동의 AI 화학 전문가
    화학에 대해 궁금한 사항이 있으면 질문해 주세요.
    """)
    gr.ChatInterface(predict)

demo.launch()