File size: 2,929 Bytes
e82962e
 
 
 
 
db9828e
e82962e
 
 
db9828e
 
 
 
 
 
 
 
 
e82962e
db9828e
 
 
e62103a
 
 
e82962e
db9828e
e82962e
 
 
db9828e
 
 
e82962e
 
 
db9828e
e82962e
 
 
 
db9828e
 
e82962e
 
 
 
db9828e
e82962e
db9828e
e82962e
db9828e
 
e82962e
 
e62103a
 
e82962e
 
 
 
db9828e
 
 
 
e82962e
 
db9828e
 
e82962e
 
db9828e
e82962e
 
 
 
 
 
 
 
 
db9828e
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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()