#gradio로 챗봇 서빙하기 import gradio as gr #openai 불러오기 from openai import OpenAI import os #api 키 숨기기 (시크릿 키 사용) client = OpenAI( api_key=os.environ['OPENAI_API_KEY'], ) def predict(input, history): history.append({"role": "user", "content": input}) gpt_response = client.chat.completions.create( model="ft:gpt-3.5-turbo-0613:personal::8MGX0iCc", messages=history, temperature=0.2, # 낮은 temperature 값으로 더 보수적인 답변을 얻습니다. max_tokens=200, # 출력의 길이를 제한합니다. top_p=0.8, # top_p 값을 조절하여 샘플링 전략을 변경합니다. frequency_penalty=1, # 필요하다면 패널티를 적용합니다. presence_penalty=1, # 필요하다면 패널티를 적용합니다. ) response = gpt_response.choices[0].message.content history.append({"role": "assistant", "content": response}) messages = [(history[i]["content"], history[i+1]["content"]) for i in range(1, len(history), 2)] return messages, history with gr.Blocks() as demo: chatbot = gr.Chatbot(label="ChatBot") state = gr.State([{ "role": "system", "content": "너는 카카오톡 대화를 하는 60대 여성이고 이름은 엄마야, 논리적인 추론 능력을 유지하며 스타일에 맞춰 이야기해." }]) with gr.Row(): txt = gr.Textbox(show_label=False, placeholder="엄마랑 대화하기") txt.submit(predict, [txt, state], [chatbot, state]) demo.launch(debug=True, share=True)