RAG, MMR, 임베딩 심화
This commit is contained in:
+50
-33
@@ -1,55 +1,72 @@
|
||||
from langchain_ollama import ChatOllama
|
||||
from langchain_ibm import ChatWatsonx
|
||||
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
|
||||
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder
|
||||
from langchain_core.output_parsers import (
|
||||
StrOutputParser,
|
||||
JsonOutputParser,
|
||||
PydanticOutputParser,
|
||||
)
|
||||
from langchain_core.chat_history import InMemoryChatMessageHistory, BaseChatMessageHistory
|
||||
from langchain_core.runnables.history import RunnableWithMessageHistory
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Literal
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import gradio as gr
|
||||
import uuid
|
||||
|
||||
qwen_llm = ChatOllama(model="qwen3.5:4b")
|
||||
exaone_llm = ChatOllama(model="exaone3.5:2.4b")\
|
||||
gemma_llm = ChatOllama(model="gemma4:e2b")
|
||||
# qwen_llm = ChatOllama(model="qwen3.5:4b")
|
||||
# exaone_llm = ChatOllama(model="exaone3.5:2.4b")
|
||||
|
||||
system_prompt = """\
|
||||
당신은 20년 경력의 전문 셰프이자 요리 연구가입니다.
|
||||
사용자의 요리 질문에 대해
|
||||
재료, 조리방법, 실패 방지 팁, 대체 재료를
|
||||
포함하여 답변하세요.
|
||||
항상 한국어로 답변하세요
|
||||
"""
|
||||
store = {}
|
||||
|
||||
template = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
("system", system_prompt),
|
||||
("human", "{question}"),
|
||||
]
|
||||
)
|
||||
def get_sesstion_history(session_id) -> BaseChatMessageHistory:
|
||||
if session_id not in store:
|
||||
store[session_id] = InMemoryChatMessageHistory()
|
||||
return store[session_id]
|
||||
|
||||
chain = template | gemma_llm | StrOutputParser()
|
||||
def create_chain():
|
||||
|
||||
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"]])
|
||||
gemma_llm = ChatOllama(model="gemma4:e2b")
|
||||
|
||||
response = chain.invoke({"history": chat_history, "question": question})
|
||||
return response
|
||||
system_prompt = """\
|
||||
당신은 20년 경력의 전문 셰프이자 요리 연구가입니다.
|
||||
사용자의 요리 질문에 대해
|
||||
재료, 조리방법, 실패 방지 팁, 대체 재료를
|
||||
포함하여 답변하세요.
|
||||
항상 한국어로 답변하세요
|
||||
"""
|
||||
|
||||
template = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
("system", system_prompt),
|
||||
MessagesPlaceholder(variable_name="history"), # 대화 이력이 삽입될 위치
|
||||
("human", "{question}"),
|
||||
]
|
||||
)
|
||||
|
||||
chatbot = gr.ChatInterface(
|
||||
fn=chat,
|
||||
title="🍝 요리 전문가",
|
||||
description="요리경력 20년의 전문가입니다. 요리에 대한 궁금증을 해결하세요.",
|
||||
)
|
||||
chain = template | gemma_llm | StrOutputParser()
|
||||
|
||||
return RunnableWithMessageHistory(chain, get_sesstion_history, input_messages_key="question", history_messages_key="history")
|
||||
|
||||
chain = create_chain()
|
||||
|
||||
def chat(question, history, session_id):
|
||||
full_response = ""
|
||||
for chunk in chain.stream({"question": question}, config={"configurable": {"session_id":session_id}}):
|
||||
full_response+=chunk
|
||||
yield full_response
|
||||
|
||||
with gr.Blocks() as chatbot:
|
||||
# 세션ID uuid 사용
|
||||
# gr.State() : 사용자별 데이터를 서버 메모리에 저장하는 컴포넌트
|
||||
session_state = gr.State(str(uuid.uuid4()))
|
||||
|
||||
gr.ChatInterface(
|
||||
fn=chat,
|
||||
additional_inputs=[session_state],
|
||||
title="🍝 요리 전문가",
|
||||
description="요리경력 20년의 전문가입니다. 요리에 대한 궁금증을 해결하세요.",
|
||||
)
|
||||
|
||||
chatbot.launch()
|
||||
|
||||
Reference in New Issue
Block a user