58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from langchain_ollama import ChatOllama
|
|
from langchain_ibm import ChatWatsonx
|
|
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
|
|
from langchain_core.output_parsers import (
|
|
StrOutputParser,
|
|
JsonOutputParser,
|
|
PydanticOutputParser,
|
|
)
|
|
from pydantic import BaseModel, Field
|
|
from typing import Literal
|
|
from dotenv import load_dotenv
|
|
from langchain_core.prompts import MessagesPlaceholder # 추가 필수
|
|
import os
|
|
import gradio as gr
|
|
|
|
qwen_llm = ChatOllama(model="qwen3.5:4b")
|
|
exaone_llm = ChatOllama(model="exaone3.5:2.4b")
|
|
gemma_llm = ChatOllama(model="gemma4:e2b")
|
|
|
|
system_prompt = """\
|
|
당신은 20년 경력의 전문 셰프이자 요리 연구가입니다.
|
|
사용자의 요리 질문에 대해
|
|
재료, 조리방법, 실패 방지 팁, 대체 재료를
|
|
포함하여 답변하세요.
|
|
항상 한국어로 답변하세요
|
|
"""
|
|
|
|
template = ChatPromptTemplate.from_messages(
|
|
[
|
|
("system", system_prompt),
|
|
MessagesPlaceholder(variable_name="history"), # 대화 이력이 삽입될 위치
|
|
("human", "{question}"),
|
|
]
|
|
)
|
|
|
|
chain = template | gemma_llm | StrOutputParser()
|
|
|
|
def chat(question, history):
|
|
chat_history = []
|
|
# history 를 이용한 직접 관리
|
|
for msg in history:
|
|
if msg["role"] == "user":
|
|
chat_history.append(["human", msg["content"]])
|
|
elif msg["role"] == "assistant":
|
|
chat_history.append(["ai", msg["content"]])
|
|
|
|
response = chain.invoke({"history": chat_history, "question": question})
|
|
return response
|
|
|
|
|
|
chatbot = gr.ChatInterface(
|
|
fn=chat,
|
|
title="🍝 요리 전문가",
|
|
description="요리경력 20년의 전문가입니다. 요리에 대한 궁금증을 해결하세요.",
|
|
)
|
|
|
|
chatbot.launch()
|