Spaces:
Runtime error
Runtime error
File size: 1,697 Bytes
531d477 1fcd7be 171f69d fb5e935 171f69d fb5e935 1fcd7be 171f69d 1fcd7be 531d477 1fcd7be |
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 |
import gradio as gr
import openai
# โ
OpenAI API ํค ์
๋ ฅ (์์ ํ ์ ์ฅ ๋ฐฉ์ ๊ถ์ฅ)
openai.api_key = "YOUR_OPENAI_API_KEY"
# GPT์๊ฒ ๋ณด๋ผ ํ๋กฌํํธ๋ฅผ ๊ตฌ์ฑํ๊ณ ์๋ต ๋ฐ๊ธฐ
def chat_with_gpt(message, history):
# ์์คํ
ํ๋กฌํํธ: ์ญํ ์ค์
system_prompt = "๋๋ ๋ฌธ์ฅ์ ๊ณต์ํ๊ณ ์์ ์๊ฒ ๋ฐ๊ฟ์ฃผ๋ ํ๊ตญ์ด ์ ๋ฌธ๊ฐ์ผ."
# ์ฌ์ฉ์ ํ๋กฌํํธ ๊ตฌ์ฑ
user_prompt = f"""์๋ ๋ฌธ์ฅ์ ๋ฌด๋กํ๊ฑฐ๋ ๊ณต๊ฒฉ์ ์ธ ํํ์ ์ฐพ์๋ด๊ณ , ๋ ์์ ์๋ ํํ์ผ๋ก ๋ฐ๊ฟ์ค. ๊ฐ๋จํ ์ด์ ๋ ํจ๊ป ์๋ ค์ค.
๋ฌธ์ฅ: "{message}"
์๋ต ํ์:
1. ์ง์ ์ฌํญ: (๋ฌด๋กํ ํํ์ด ์๋ค๋ฉด ์ด๋ค ๋ถ๋ถ์ธ์ง ์ค๋ช
)
2. ์ ์ ๋ฌธ์ฅ: (๋ ์์ ์๊ฒ ๋ฐ๊พผ ๋ฌธ์ฅ ์ ์)
"""
try:
response = openai.ChatCompletion.create(
model="gpt-4", # ๋๋ "gpt-3.5-turbo"
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=0.7,
)
reply = response["choices"][0]["message"]["content"].strip()
return history + [[message, reply]]
except Exception as e:
return history + [[message, f"โ ๏ธ ์ค๋ฅ ๋ฐ์: {str(e)}"]]
# Gradio ์ฑ ๊ตฌ์ฑ
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox(label="๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์", placeholder="์: ๋ ์ ๋ง ์ ๊ทธ๋ ๊ฒ ๋งํด?")
def respond_and_clear(user_input, history):
updated_history = chat_with_gpt(user_input, history)
return "", updated_history
msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot])
demo.launch() |