콘텐츠로 이동

스트림 + 대화 연동하기

필요한 모듈 설치하기

!pip install openai
!pip install gradio

대화 이력과 스트림 방식을 통합한 결과

import gradio as gr
from openai import OpenAI

client = OpenAI()

def generate_response(prompt_text, history):

    history_openai_format = []

    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": prompt_text})

    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

gr.ChatInterface(
    generate_response,
    chatbot=gr.Chatbot(
        height=300, label='홍길동 챗봇입니다.',
    ),
    textbox=gr.Textbox(placeholder="질문을 입력해 주세요", container=False, scale=7),
    title="나의 챗봇",
).launch()  

0부터 100까지의 임의의 숫자 10개를 만들어줘.
위의 숫자의 합계를 알려줘.