IDEA-DESIGN / app.py
seawolf2357's picture
Update app.py
e62103a verified
raw
history blame
2.93 kB
import os
import streamlit as st
import json
import anthropic
# API ์„ค์ •
api_key = os.environ.get("API_KEY")
client = anthropic.Anthropic(api_key=api_key)
def get_system_prompt():
return """๋‹น์‹ ์€ ์นœ์ ˆํ•˜๊ณ  ์ „๋ฌธ์ ์ธ AI ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค.
๋‹ค์Œ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ๋”ฐ๋ผ์ฃผ์„ธ์š”:
- ์ •ํ™•ํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค
- ๋ถˆํ™•์‹คํ•œ ๊ฒฝ์šฐ ๊ทธ ์‚ฌ์‹ค์„ ์ธ์ •ํ•ฉ๋‹ˆ๋‹ค
- ์œ„ํ—˜ํ•˜๊ฑฐ๋‚˜ ํ•ด๋กœ์šด ์กฐ์–ธ์€ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค
- ๊ฐœ์ธ์ •๋ณด๋Š” ๋ณดํ˜ธํ•ฉ๋‹ˆ๋‹ค
- ์˜ˆ์˜ ๋ฐ”๋ฅด๊ณ  ๊ณต์†ํ•˜๊ฒŒ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค
- ํ•œ๊ตญ์–ด๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋Œ€ํ™”ํ•ฉ๋‹ˆ๋‹ค"""
def chatbot_interface():
st.title("AI ์–ด์‹œ์Šคํ„ดํŠธ์™€ ๋Œ€ํ™”ํ•˜๊ธฐ")
# ๋ชจ๋ธ ๊ณ ์ • ์„ค์ • (์„ ํƒ ๋ฐ•์Šค ์ œ๊ฑฐ)
if "ai_model" not in st.session_state:
st.session_state["ai_model"] = "claude-3-5-sonnet-20241022"
# ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
if "messages" not in st.session_state:
st.session_state.messages = []
# ๋Œ€ํ™” ๊ธฐ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
st.sidebar.title("๋Œ€ํ™” ๊ธฐ๋ก")
uploaded_file = st.sidebar.file_uploader("๋Œ€ํ™” ๊ธฐ๋ก JSON ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
if uploaded_file is not None:
st.session_state.messages = json.load(uploaded_file)
# ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# ์‚ฌ์šฉ์ž ์ž…๋ ฅ
if prompt := st.chat_input("๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# AI ์‘๋‹ต ์ƒ์„ฑ
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
# Claude API ํ˜ธ์ถœ
with client.messages.stream(
max_tokens=1024,
system=get_system_prompt(),
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
model=st.session_state["ai_model"]
) as stream:
for text in stream.text_stream:
full_response += str(text) if text is not None else ""
message_placeholder.markdown(full_response + "โ–Œ")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# ๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ
if st.button("๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ"):
json_history = json.dumps(st.session_state.messages, indent=4)
st.download_button(
label="๋Œ€ํ™” ๊ธฐ๋ก ์ €์žฅํ•˜๊ธฐ",
data=json_history,
file_name="chat_history.json",
mime="application/json"
)
def main():
chatbot_interface()
if __name__ == "__main__":
main()