-
Notifications
You must be signed in to change notification settings - Fork 30
/
chatgradio.py
51 lines (39 loc) · 1.33 KB
/
chatgradio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
from langchain_upstage import ChatUpstage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.output_parsers import StrOutputParser
from langchain.schema import AIMessage, HumanMessage
llm = ChatUpstage(streaming=True)
# More general chat
chat_with_history_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant."),
MessagesPlaceholder(variable_name="history"),
("human", "{message}"),
]
)
chain = chat_with_history_prompt | llm | StrOutputParser()
def chat(message, history):
history_langchain_format = []
for human, ai in history:
history_langchain_format.append(HumanMessage(content=human))
history_langchain_format.append(AIMessage(content=ai))
generator = chain.stream({"message": message, "history": history_langchain_format})
assistant = ""
for gen in generator:
assistant += gen
yield assistant
with gr.Blocks() as demo:
chatbot = gr.ChatInterface(
chat,
examples=[
"How to eat healthy?",
"Best Places in Korea",
"How to make a chatbot?",
],
title="Solar Chatbot",
description="Upstage Solar Chatbot",
)
chatbot.chatbot.height = 300
if __name__ == "__main__":
demo.launch()