콘텐츠로 이동

스트림 방식으로 결과 받기

필요한 모듈 설치하기

!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 = []
    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).launch()        

서울의 맛집 5개를 알려줘.

생성 결과를 실시간으로 받는 방법

import gradio as gr
from openai import OpenAI

client = OpenAI()

def generate_response(prompt_text, history):

    history_openai_format = []
    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()