콘텐츠로 이동

Gradio와 하이퍼클로버X 히스토리

필요한 모듈 설치하기

!pip install openai
!pip install gradio

키 정의하기

import os
os.environ["CLOVA_API_KEY"] = "당신의 CLOVA Studio API Key를 입력하세요"

Gradio에서 하이퍼클로바X 모델 호출하기 (스트림 + 히스토리)

from openai import OpenAI
import gradio as gr

client = OpenAI(
    api_key = os.environ["CLOVA_API_KEY"],
    base_url="https://clovastudio.stream.ntruss.com/v1/openai"
)

def predict(message, 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": message})

    response = client.chat.completions.create(
        model='HCX-005',
        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(predict).launch()