requirements.txt ( pip install -r requirements.txt )
slack_bolt
python-dotenv
langchain
langchain-openai
ipython
app.py
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from backend import aice_chain
# Initializes your app with your bot token and socket mode handler
# Slack API에 연결되는 봇 인스턴스 생성
# 여기에 리스너(이벤트 핸들러)를 등록해서 슬랙 메시지에 반응할 수 있게 함
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
# Listens to incoming messages that contain "hello"
# 슬랙 채팅방에서 누군가가 "hello"라고 메시지를 보내면 이 함수가 실행
# 이 함수는 메시지를 받고, 그 메시지에 대한 응답을 생성해서 다시 채널에 보내는 역할을 함
# visit https://slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html
@app.message("안녕")
def message_hello(message, say):
# say(...) 함수는 같은 채널에 메시지를 다시 보내주는 함수야
# <@{user}> 문법은 슬랙에서 멘션을 의미함 (즉, 사용자에게 직접 말하는 것처럼 보여)
# breakpoint()
message_text = message.get("text")
response = aice_chain(message_text)
say(response,thread_ts=message.get("ts"))
# say(f"안녕하세요 <@{message['user']}> 님!")
# Start your app
# SocketModeHandler는 슬랙 앱을 소켓 모드로 실행하기 위한 핸들러
# 이 핸들러는 슬랙 이벤트를 실시간으로 처리할 수 있게 해줌
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
backend.py
import os
import getpass
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
load_dotenv()
model = ChatOpenAI(model="gpt-4o", api_key=os.environ["OPENAI_API_KEY"])
# os.environ["OPENAI_API_KEY"] = getpass.getpass()
def aice_chain(text):
system_prompt = '''
You are an expert tutor and test generator for the AICE (AI Certificate Exam).
You help students prepare for both the AICE Basic and Associate levels.
The student's goal is to pass the certification, and they are using you to review key concepts and solve practice questions.
Your tasks include:
- Summarizing key concepts in a simple, beginner-friendly way
- Generating 2 to 3 practice questions (multiple choice or short answer) based on real exam style
- Providing clear answers and explanations for each question
- Giving hints and feedback for common mistakes
Output your response in Markdown format with the following structure:
1. [Summary of Key Concepts]
2. [Practice Questions]
3. [Answers and Explanations]
4. [Tips for Further Study]
All explanations should be in simple English, and the tone should be encouraging and educational.
'''
prompt_template = ChatPromptTemplate.from_messages(
[("system", system_prompt), ("user", "{text}")]
)
# result = prompt_template.invoke({"language": "italian", "text": "hi"})
chain = prompt_template | model | StrOutputParser()
return chain.invoke({
"text": text,
})
클래스의 내부 구조를 알면 직접 커스텀 할 수 있습니다.
'✨Ai 프로젝트 만들기' 카테고리의 다른 글
6. OpenAI 를 활용하여 Aice 자격증 챗봇 만들기 (결과) (2) | 2025.06.26 |
---|---|
5. OpenAI 를 활용하여 Aice 자격증 챗봇 만들기 (0) | 2025.06.26 |
4. OpenAI 를 활용하여 Aice 자격증 챗봇 만들기 - 대화 하기(맨션을 통해) (0) | 2025.06.23 |
3. OpenAI 를 활용하여 Aice 자격증 챗봇 만들기 - 파이썬 debug (0) | 2025.06.23 |
2. OpenAI 를 활용하여 Aice 자격증 챗봇 만들기 - prompt 만들기 (1) | 2025.06.16 |