File size: 3,551 Bytes
ce5b5d6
 
 
 
 
a3b78b2
3e5fe2f
a3b78b2
ce5b5d6
 
 
 
 
 
 
 
a3b78b2
ce5b5d6
 
 
 
 
 
a3b78b2
ce5b5d6
a3b78b2
ce5b5d6
a3b78b2
 
ce5b5d6
a3b78b2
 
ce5b5d6
 
 
 
 
 
a3b78b2
ce5b5d6
 
a3b78b2
 
 
 
 
 
 
 
 
ce5b5d6
 
 
a3b78b2
 
 
ce5b5d6
a3b78b2
ce5b5d6
 
 
a3b78b2
ce5b5d6
 
a3b78b2
ce5b5d6
 
a3b78b2
ce5b5d6
 
 
a3b78b2
ce5b5d6
 
a3b78b2
ce5b5d6
a3b78b2
 
 
ce5b5d6
a3b78b2
ce5b5d6
 
 
a3b78b2
 
ce5b5d6
 
a3b78b2
ce5b5d6
a3b78b2
ce5b5d6
 
 
a3b78b2
ce5b5d6
 
 
 
 
a3b78b2
ce5b5d6
 
 
 
 
a3b78b2
ce5b5d6
 
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
import os
import gradio as gr
from dotenv import load_dotenv
from openai import OpenAI
from prompts.initial_prompt import INITIAL_PROMPT
from prompts.main_prompt import MAIN_PROMPT

# .env ํŒŒ์ผ์—์„œ OPENAI_API_KEY ๋กœ๋“œ
if os.path.exists(".env"):
    load_dotenv(".env")

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

client = OpenAI(api_key=OPENAI_API_KEY)



def gpt_call(history, user_message,
             model="gpt-4o-mini",
             max_tokens=512,
             temperature=0.7,
             top_p=0.95):
    """
    OpenAI ChatCompletion API๋ฅผ ํ†ตํ•ด ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
    - history: [(user_text, assistant_text), ...]
    - user_message: ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฉ๊ธˆ ์ž…๋ ฅํ•œ ๋ฉ”์‹œ์ง€
    """
    # 1) ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€(=MAIN_PROMPT)๋ฅผ ๊ฐ€์žฅ ์•ž์— ์ถ”๊ฐ€
    messages = [{"role": "system", "content": MAIN_PROMPT}]
    
    # 2) ๊ธฐ์กด ๋Œ€ํ™” ๊ธฐ๋ก(history)์„ OpenAI ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
    #    user_text -> 'user' / assistant_text -> 'assistant'
    for user_text, assistant_text in history:
        if user_text:
            messages.append({"role": "user", "content": user_text})
        if assistant_text:
            messages.append({"role": "assistant", "content": assistant_text})

    # 3) ๋งˆ์ง€๋ง‰์— ์ด๋ฒˆ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์ถ”๊ฐ€
    messages.append({"role": "user", "content": user_message})

    # 4) OpenAI API ํ˜ธ์ถœ
    completion = client.chat.completions.create(
        model=model,
        messages=messages,
        max_tokens=max_tokens,
        temperature=temperature,
        top_p=top_p
    )
    return completion.choices[0].message.content

def respond(user_message, history):
    """
    Gradio ์ƒ์—์„œ submitํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
    - user_message: ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฉ๊ธˆ ์นœ ๋ฉ”์‹œ์ง€
    - history: ๊ธฐ์กด (user, assistant) ํŠœํ”Œ ๋ฆฌ์ŠคํŠธ
    """
    # ์‚ฌ์šฉ์ž๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ณด๋ƒˆ๋‹ค๋ฉด ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š์Œ
    if not user_message:
        return "", history

    # GPT ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์„ ๋ฐ›์Œ
    assistant_reply = gpt_call(history, user_message)

    # history์— (user, assistant) ์Œ ์ถ”๊ฐ€
    history.append((user_message, assistant_reply))

    # Gradio์—์„œ๋Š” (์ƒˆ๋กœ ๋น„์›Œ์งˆ ์ž…๋ ฅ์ฐฝ, ๊ฐฑ์‹ ๋œ history)๋ฅผ ๋ฐ˜ํ™˜
    return "", history

##############################
#  Gradio Blocks UI
##############################
with gr.Blocks() as demo:
    gr.Markdown("## Simple Chat Interface")

    # Chatbot ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์„ค์ •
    # ์ฒซ ๋ฒˆ์งธ ๋ฉ”์‹œ์ง€๋Š” (user="", assistant=INITIAL_PROMPT) ํ˜•ํƒœ๋กœ ๋„ฃ์–ด
    # ํ™”๋ฉด์ƒ์—์„œ 'assistant'๊ฐ€ INITIAL_PROMPT๋ฅผ ๋งํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•จ
    chatbot = gr.Chatbot(
        value=[("", INITIAL_PROMPT)],  # (user, assistant)
        height=500
    )

    # (user, assistant) ์Œ์„ ์ €์žฅํ•  ํžˆ์Šคํ† ๋ฆฌ ์ƒํƒœ
    # ์—ฌ๊ธฐ์„œ๋„ ๋™์ผํ•œ ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ๋„ฃ์–ด์คŒ
    state_history = gr.State([("", INITIAL_PROMPT)])

    # ์‚ฌ์šฉ์ž ์ž…๋ ฅ
    user_input = gr.Textbox(
        placeholder="Type your message here...",
        label="Your Input"
    )

    # ์ž…๋ ฅ์ด submit๋˜๋ฉด respond() ํ˜ธ์ถœ โ†’ ์ถœ๋ ฅ์€ (์ƒˆ ์ž…๋ ฅ์ฐฝ, ๊ฐฑ์‹ ๋œ chatbot)
    user_input.submit(
        respond,
        inputs=[user_input, state_history],
        outputs=[user_input, chatbot]
    ).then(
        # respond ๋๋‚œ ๋’ค, ์ตœ์‹  history๋ฅผ state_history์— ๋ฐ˜์˜
        fn=lambda _, h: h,
        inputs=[user_input, chatbot],
        outputs=[state_history]
    )

# ๋ฉ”์ธ ์‹คํ–‰
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True)