File size: 5,924 Bytes
e82962e
 
 
 
 
db9828e
e82962e
 
 
55ec4cb
9a73ea8
55ec4cb
db9828e
55ec4cb
78f19de
 
 
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
9aaa4bf
78f19de
 
e82962e
55ec4cb
 
db9828e
0228286
55ec4cb
 
 
75bce88
55ec4cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e82962e
 
55ec4cb
e82962e
 
55ec4cb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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)

# ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜ ์„ค์ • (Claude-3 Sonnet์˜ ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜)
MAX_TOKENS = 7999

def get_system_prompt():

    return """


๋‹น์‹ ์€ ์ „๋ฌธ ๋ธ”๋กœ๊ทธ ์ž‘์„ฑ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ธ”๋กœ๊ทธ ๊ธ€ ์ž‘์„ฑ ์š”์ฒญ์— ๋Œ€ํ•ด ๋‹ค์Œ์˜ 8๋‹จ๊ณ„ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ฒ ์ €ํžˆ ๋”ฐ๋ฅด๋˜, ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋งค๋ ฅ์ ์ธ ๊ธ€์ด ๋˜๋„๋ก ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

๋…์ž ์—ฐ๊ฒฐ ๋‹จ๊ณ„ 1.1. ๊ณต๊ฐ๋Œ€ ํ˜•์„ฑ์„ ์œ„ํ•œ ์นœ๊ทผํ•œ ์ธ์‚ฌ 1.2. ๋…์ž์˜ ์‹ค์ œ ๊ณ ๋ฏผ์„ ๋ฐ˜์˜ํ•œ ๋„์ž… ์งˆ๋ฌธ 1.3. ์ฃผ์ œ์— ๋Œ€ํ•œ ์ฆ‰๊ฐ์  ๊ด€์‹ฌ ์œ ๋„

๋ฌธ์ œ ์ •์˜ ๋‹จ๊ณ„ 2.1. ๋…์ž์˜ ํŽ˜์ธํฌ์ธํŠธ ๊ตฌ์ฒดํ™” 2.2. ๋ฌธ์ œ์˜ ์‹œ๊ธ‰์„ฑ๊ณผ ์˜ํ–ฅ๋„ ๋ถ„์„ 2.3. ํ•ด๊ฒฐ ํ•„์š”์„ฑ์— ๋Œ€ํ•œ ๊ณต๊ฐ๋Œ€ ํ˜•์„ฑ

์ „๋ฌธ์„ฑ ์ž…์ฆ ๋‹จ๊ณ„ 3.1. ๊ฐ๊ด€์  ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ๋ถ„์„ 3.2. ์ „๋ฌธ๊ฐ€ ๊ฒฌํ•ด์™€ ์—ฐ๊ตฌ ๊ฒฐ๊ณผ ์ธ์šฉ 3.3. ์‹ค์ œ ์‚ฌ๋ก€๋ฅผ ํ†ตํ•œ ๋ฌธ์ œ ๊ตฌ์ฒดํ™”

์†”๋ฃจ์…˜ ์ œ๊ณต ๋‹จ๊ณ„ 4.1. ๋‹จ๊ณ„๋ณ„ ์‹ค์ฒœ ๊ฐ€์ด๋“œ๋ผ์ธ ์ œ์‹œ 4.2. ์ฆ‰์‹œ ์ ์šฉ ๊ฐ€๋Šฅํ•œ ๊ตฌ์ฒด์  ํŒ 4.3. ์˜ˆ์ƒ ์žฅ์• ๋ฌผ๊ณผ ๊ทน๋ณต ๋ฐฉ์•ˆ ํฌํ•จ

์‹ ๋ขฐ๋„ ๊ฐ•ํ™” ๋‹จ๊ณ„ 5.1. ์‹ค์ œ ์„ฑ๊ณต ์‚ฌ๋ก€ ์ œ์‹œ 5.2. ๊ตฌ์ฒด์  ์‚ฌ์šฉ์ž ํ›„๊ธฐ ์ธ์šฉ 5.3. ๊ฐ๊ด€์  ๋ฐ์ดํ„ฐ๋กœ ํšจ๊ณผ ์ž…์ฆ

ํ–‰๋™ ์œ ๋„ ๋‹จ๊ณ„ 6.1. ๋ช…ํ™•ํ•œ ์ฒซ ์‹ค์ฒœ ๋‹จ๊ณ„ ์ œ์‹œ 6.2. ์‹œ๊ธ‰์„ฑ์„ ๊ฐ•์กฐํ•œ ํ–‰๋™ ์ด‰๊ตฌ 6.3. ์‹ค์ฒœ ๋™๊ธฐ ๋ถ€์—ฌ ์š”์†Œ ํฌํ•จ

์ง„์ •์„ฑ ๊ฐ•ํ™” ๋‹จ๊ณ„ 7.1. ์†”๋ฃจ์…˜์˜ ํ•œ๊ณ„ ํˆฌ๋ช…ํ•˜๊ฒŒ ๊ณต๊ฐœ 7.2. ๊ฐœ์ธ๋ณ„ ์ฐจ์ด ์กด์žฌ ์ธ์ • 7.3. ํ•„์š” ์กฐ๊ฑด๊ณผ ์ฃผ์˜์‚ฌํ•ญ ๋ช…์‹œ

๊ด€๊ณ„ ์ง€์† ๋‹จ๊ณ„ 8.1. ์ง„์ •์„ฑ ์žˆ๋Š” ๊ฐ์‚ฌ ์ธ์‚ฌ 8.2. ๋‹ค์Œ ์ปจํ…์ธ  ์˜ˆ๊ณ ๋กœ ๊ธฐ๋Œ€๊ฐ ์กฐ์„ฑ 8.3. ์†Œํ†ต ์ฑ„๋„ ์•ˆ๋‚ด

์ž‘์„ฑ ์‹œ ์ค€์ˆ˜์‚ฌํ•ญ 9.1. ๊ธ€์ž ์ˆ˜: 1500-2000์ž ๋‚ด์™ธ 9.2. ๋ฌธ๋‹จ ๊ธธ์ด: 3-4๋ฌธ์žฅ ์ด๋‚ด 9.3. ์‹œ๊ฐ์  ๊ตฌ๋ถ„: ์†Œ์ œ๋ชฉ, ๊ตฌ๋ถ„์„ , ๋ฒˆํ˜ธ ๋ชฉ๋ก ํ™œ์šฉ 9.4. ํ†ค์•ค๋งค๋„ˆ: ์นœ๊ทผํ•˜๊ณ  ์ „๋ฌธ์ ์ธ ๋Œ€ํ™”์ฒด 9.5. ๋ฐ์ดํ„ฐ: ๋ชจ๋“  ์ •๋ณด์˜ ์ถœ์ฒ˜ ๋ช…์‹œ 9.6. ๊ฐ€๋…์„ฑ: ๋ช…ํ™•ํ•œ ๋‹จ๋ฝ ๊ตฌ๋ถ„๊ณผ ๊ฐ•์กฐ์  ์‚ฌ์šฉ

์ด๋Ÿฌํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ, ์š”์ฒญ๋ฐ›์€ ์ฃผ์ œ์— ๋Œ€ํ•ด ์ฒด๊ณ„์ ์ด๊ณ  ๋งค๋ ฅ์ ์ธ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

"""



def chatbot_interface():
    st.title("Ginigen Blog")
    
    # ๋ชจ๋ธ ๊ณ ์ • ์„ค์ •
    if "ai_model" not in st.session_state:
        st.session_state["ai_model"] = "claude-3-7-sonnet-20250219"
    # ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
    if "messages" not in st.session_state:
        st.session_state.messages = []
        
    # ์ž๋™ ์ €์žฅ ๊ธฐ๋Šฅ
    if "auto_save" not in st.session_state:
        st.session_state.auto_save = True

    # ๋Œ€ํ™” ๊ธฐ๋ก ๊ด€๋ฆฌ (์‚ฌ์ด๋“œ๋ฐ”)
    st.sidebar.title("๋Œ€ํ™” ๊ธฐ๋ก ๊ด€๋ฆฌ")
    
    # ์ž๋™ ์ €์žฅ ํ† ๊ธ€
    st.session_state.auto_save = st.sidebar.toggle("์ž๋™ ์ €์žฅ", value=st.session_state.auto_save)
    
    # ๋Œ€ํ™” ๊ธฐ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
    uploaded_file = st.sidebar.file_uploader("๋Œ€ํ™” ๊ธฐ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ", type=['json'])
    if uploaded_file is not None:
        try:
            content = uploaded_file.getvalue().decode()
            if content.strip():
                st.session_state.messages = json.loads(content)
                st.sidebar.success("๋Œ€ํ™” ๊ธฐ๋ก์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค!")
            else:
                st.sidebar.warning("์—…๋กœ๋“œ๋œ ํŒŒ์ผ์ด ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.")
        except json.JSONDecodeError:
            st.sidebar.error("์˜ฌ๋ฐ”๋ฅธ JSON ํ˜•์‹์˜ ํŒŒ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.")
        except Exception as e:
            st.sidebar.error(f"ํŒŒ์ผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}")

    # ๋Œ€ํ™” ๊ธฐ๋ก ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ
    if st.sidebar.button("๋Œ€ํ™” ๊ธฐ๋ก ์ดˆ๊ธฐํ™”"):
        st.session_state.messages = []
        st.sidebar.success("๋Œ€ํ™” ๊ธฐ๋ก์ด ์ดˆ๊ธฐํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")

    # ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
    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 = ""
            
            # API ํ˜ธ์ถœ
            with client.messages.stream(
                max_tokens=MAX_TOKENS,
                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.session_state.auto_save:
            try:
                with open('chat_history_auto_save.json', 'w', encoding='utf-8') as f:
                    json.dump(st.session_state.messages, f, ensure_ascii=False, indent=4)
            except Exception as e:
                st.sidebar.error(f"์ž๋™ ์ €์žฅ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")

    # ๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ
    if st.sidebar.button("๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ"):
        json_history = json.dumps(st.session_state.messages, indent=4, ensure_ascii=False)
        st.sidebar.download_button(
            label="๋Œ€ํ™” ๊ธฐ๋ก ์ €์žฅํ•˜๊ธฐ",
            data=json_history,
            file_name="chat_history.json",
            mime="application/json"
        )

def main():
    chatbot_interface()

if __name__ == "__main__":
    main()