이 글은 손쉽게 웹 어플리케이션을 개발할 수 있는 Gradio 와 LangChain 을 이용해 간단한 챗봇을 개발해 본다.
환경 설치
터미널에서 다음 라이브러리를 설치한다.
pip install gradio langchain
코딩
다음을 코딩하고 실행한다.
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage, SystemMessage
import os
import gradio as gr
os.environ["OPENAI_API_KEY"] = "<input your openai key" # API 키 설정
llm = ChatOpenAI(temperature=1.0, model='gpt-4o-mini')
# LLM 응답 처리
def response(message, history, additional_input_info):
history_langchain_format = []
history_langchain_format.append(SystemMessage(content= additional_input_info))
for human, ai in history:
history_langchain_format.append(HumanMessage(content=human))
history_langchain_format.append(AIMessage(content=ai))
history_langchain_format.append(HumanMessage(content=message))
gpt_response = llm(history_langchain_format)
return gpt_response.content
# 인터페이스 생성
gr.ChatInterface(
fn=response, # LLM 응답처리 콜백함수 설정
textbox=gr.Textbox(placeholder="Talk", container=False, scale=7),
chatbot=gr.Chatbot(height=1000),
title="ChatBot",
description="I'm a chatbot that can chat with you. I'm lovely chatbot.",
theme="soft",
examples=[["Hi"], ["I'm good"], ["What's your name?"]],
retry_btn="resend",
undo_btn="delete❌",
clear_btn="delete all💫",
additional_inputs=[
gr.Textbox("", label="Input System Prompt", placeholder="I'm chatbot.")
]
).launch()
실행 결과
성공한 결과는 다음과 같다.
마무리
Gradio는 2019년에 Abubakar Abid에 의해 개발된 오픈소스 파이썬 라이브러리로, 머신러닝 모델을 손쉽게 웹 애플리케이션 형태로 배포하고 공유할 수 있도록 설계되었다.021년, Gradio는 AI 커뮤니티에서 널리 알려진 Hugging Face에 인수되었으며, 이를 통해 더욱 발전하고 있다.근에는 Gradio 5.0 버전이 출시되어, 월 사용자 수 200만 명과 47만 개 이상의 애플리케이션을 보유한 AI 개발 생태계의 핵심 도구로 자리 잡았다.
댓글 없음:
댓글 쓰기