Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Stream
- effective java
- producer
- SpringBoot
- Spring Security
- orElseGet
- Factory Method Pattern
- mokito
- 패스트캠퍼스 #환급챌린지 #패스트캠퍼스후기 #습관형성 #직장인자기계발 #오공완
- kafka
- 함수형 프로그래밍
- optional
- 싱글톤
- #패스트캠퍼스 #환급챌린지 #패스트캠퍼스후기 #습관형성 #직장인자기계발 #오공완
- 카프카
- git cli
- 인텔리제이 단축키
- Functional Programming
- JWT
- TDD
- Authentication
- Java8
- topic
- orelse
- Clean Code
- signWith
- junit5
- Java
- consumer
- 디자인패턴
Archives
- Today
- Total
goodbye
패스트캠퍼스 환급챌린지 31일차 : 테디노트의 RAG 비법노트 강의 후기 본문
본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성하였습니다
https://fastcampus.info/4n8ztzq
(~6/20) 50일의 기적 AI 환급반💫 | 패스트캠퍼스
초간단 미션! 하루 20분 공부하고 수강료 전액 환급에 AI 스킬 장착까지!
fastcampus.co.kr
패스트캠퍼스 환급챌린지 31일차!
1) 공부 시작 시간 인증A

2) 공부 종료 시간 인증

3) 강의 수강 클립 인증

4) 학습 인증샷

5) 학습통계

Today I Learned
노드 생성
from langchain_core.messages import get_buffer_string
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
# 검색 쿼리 작성
search_instructions = SystemMessage(
content=f"""You will be given a conversation between an analyst and an expert.
Your goal is to generate a well-structured query for use in retrieval and / or web-search related to the conversation.
First, analyze the full conversation.
Pay particular attention to the final question posed by the analyst.
Convert this final question into a well-structured web search query"""
)
# 웹 검색 수행 함수 정의
def search_web(state: InterviewState):
"""웹 검색을 통한 문서 검색"""
# 검색 쿼리 생성
structured_llm = llm.with_structured_output(SearchQuery)
search_query = structured_llm.invoke([search_instructions] + state["messages"])
# 검색 수행
search_docs = tavily_search.invoke(search_query.search_query)
# 검색 결과 형식 지정
formatted_search_docs = "\\n\\n---\\n\\n".join(
[
f'\\n{doc["content"]}\\n'
for doc in search_docs
]
)
return {"context": [formatted_search_docs]}
# Arxiv 검색 노드 생성
def search_arxiv(state: InterviewState):
"""Arxiv 검색 노드"""
# 검색 쿼리 생성
structured_llm = llm.with_structured_output(SearchQuery)
search_query = structured_llm.invoke([search_instructions] + state["messages"])
try:
# 검색 수행
arxiv_search_results = arxiv_retriever.invoke(
search_query.search_query,
load_max_docs=2,
load_all_available_meta=True,
get_full_documents=True,
)
# 검색 결과 형식 지정
formatted_search_docs = "\\n\\n---\\n\\n".join(
[
f'\\n\\n\\n\\n{doc.metadata["Summary"]}\\n\\n\\n\\n{doc.page_content}\\n\\n'
for doc in arxiv_search_results
]
)
return {"context": [formatted_search_docs]}
except Exception as e:
print(f"Arxiv 검색 중 오류 발생: {str(e)}")
return {
"context": ["Arxiv 검색 결과를 가져오는데 실패했습니다."]
}
answer_instructions = """You are an expert being interviewed by an analyst.
Here is analyst area of focus: {goals}.
You goal is to answer a question posed by the interviewer.
To answer question, use this context:
{context}
When answering questions, follow these guidelines:
1. Use only the information provided in the context.
2. Do not introduce external information or make assumptions beyond what is explicitly stated in the context.
3. The context contain sources at the topic of each individual document.
4. Include these sources your answer next to any relevant statements. For example, for source # 1 use [1].
5. List your sources in order at the bottom of your answer. [1] Source 1, [2] Source 2, etc
6. If the source is: ' then just list:
[1] assistant/docs/llama3_1.pdf, page 7
And skip the addition of the brackets as well as the Document source preamble in your citation."""
# 질문에 대한 답변 생성 함수 정의
def generate_answer(state: InterviewState):
"""질문에 대한 답변 생성 노드"""
# 상태에서 분석가와 메시지 가져오기
analyst = state["analyst"]
messages = state["messages"]
context = state["context"]
# 질문에 대한 답변 생성
system_message = answer_instructions.format(goals=analyst.persona, context=context)
answer = llm.invoke([SystemMessage(content=system_message)] + messages)
# 메시지를 전문가의 답변으로 명명
answer.name = "expert"
# 상태에 메시지 추가
return {"messages": [answer]}
# 인터뷰 저장 함수 정의
def save_interview(state: InterviewState):
"""인터뷰 저장"""
# 메시지 가져오기
messages = state["messages"]
# 인터뷰를 문자열로 변환
interview = get_buffer_string(messages)
# 인터뷰 키에 저장
return {"interview": interview}
# 메시지 라우팅 함수 정의
def route_messages(state: InterviewState, name: str = "expert"):
"""질문과 답변 사이의 라우팅"""
# 메시지 가져오기
messages = state["messages"]
max_num_turns = state.get("max_num_turns", 2)
# 전문가의 답변 수 확인
num_responses = len(
[m for m in messages if isinstance(m, AIMessage) and m.name == name]
)
# 전문가가 최대 턴 수 이상 답변한 경우 종료
if num_responses >= max_num_turns:
return "save_interview"
# 이 라우터는 각 질문-답변 쌍 후에 실행됨
# 논의 종료를 신호하는 마지막 질문 가져오기
last_question = messages[-2]
if "Thank you so much for your help" in last_question.content:
return "save_interview"
return "ask_question"
# 세션 작성 지시사항
section_writer_instructions = """You are an expert technical writer.
Your task is to create a detailed and comprehensive section of a report, thoroughly analyzing a set of source documents.
This involves extracting key insights, elaborating on relevant points, and providing in-depth explanations to ensure clarity and understanding. Your writing should include necessary context, supporting evidence, and examples to enhance the reader's comprehension. Maintain a logical and well-organized structure, ensuring that all critical aspects are covered in detail and presented in a professional tone.
Please follow these instructions:
1. Analyze the content of the source documents:
- The name of each source document is at the start of the document, with the https://ai.meta.com/blog/meta-llama-3-1/>
[4] <https://ai.meta.com/blog/meta-llama-3-1/>
There should be no redundant sources. It should simply be:
[3] <https://ai.meta.com/blog/meta-llama-3-1/>
9. Final review:
- Ensure the report follows the required structure
- Include no preamble before the title of the report
- Check that all guidelines have been followed"""
# 섹션 작성 함수 정의
def write_section(state: InterviewState):
"""질문에 대한 답변 생성 노드"""
# 상태에서 컨텍스트, 분석가 가져오기
context = state["context"]
analyst = state["analyst"]
# 섹션 작성을 위한 시스템 프롬프트 정의
system_message = section_writer_instructions.format(focus=analyst.description)
section = llm.invoke(
[SystemMessage(content=system_message)]
+ [HumanMessage(content=f"Use this source to write your section: {context}")]
)
# 상태에 섹션 추가
return {"sections": [section.content]}
인터뷰 그래프 생성, 실행
from langgraph.graph import StateGraph
from langgraph.checkpoint.memory import MemorySaver
# 노드 및 엣지 추가
interview_builder = StateGraph(InterviewState)
interview_builder.add_node("ask_question", generate_question)
interview_builder.add_node("search_web", search_web)
interview_builder.add_node("search_arxiv", search_arxiv)
interview_builder.add_node("answer_question", generate_answer)
interview_builder.add_node("save_interview", save_interview)
interview_builder.add_node("write_section", write_section)
# 흐름 설정
interview_builder.add_edge(START, "ask_question")
interview_builder.add_edge("ask_question", "search_web")
interview_builder.add_edge("ask_question", "search_arxiv")
interview_builder.add_edge("search_web", "answer_question")
interview_builder.add_edge("search_arxiv", "answer_question")
interview_builder.add_conditional_edges(
"answer_question", route_messages, ["ask_question", "save_interview"]
)
interview_builder.add_edge("save_interview", "write_section")
interview_builder.add_edge("write_section", END)
# 인터뷰 그래프 생성
memory = MemorySaver()
interview_graph = interview_builder.compile(checkpointer=memory).with_config(
run_name="Conduct Interviews"
)
# 그래프 시각화
visualize_graph(interview_graph)
from IPython.display import Markdown
# 주제 설정
topic = "Modular RAG 가 기존의 Naive RAG 와 어떤 차이가 있는지와 production level 에서 사용하는 이점"
# 인터뷰 시작 메시지 생성
messages = [HumanMessage(f"So you said you were writing an article on {topic}?")]
# 스레드 ID 설정
config = RunnableConfig(
recursion_limit=100,
configurable={"thread_id": random_uuid()},
)
# 그래프 실행
invoke_graph(
interview_graph,
{"analyst": analysts[0], "messages": messages, "max_num_turns": 5},
config,
)
# 완성된 인터뷰 섹션 출력
Markdown(interview_graph.get_state(config).values["sections"][0])
print(interview_graph.get_state(config).values["sections"][0])
'Lecture > 패스트캠퍼스' 카테고리의 다른 글
| 패스트캠퍼스 환급챌린지 33일차 : 테디노트의 RAG 비법노트 강의 후기 (0) | 2025.08.02 |
|---|---|
| 패스트캠퍼스 환급챌린지 32일차 : 테디노트의 RAG 비법노트 강의 후기 (1) | 2025.08.01 |
| 패스트캠퍼스 환급챌린지 30일차 : 테디노트의 RAG 비법노트 강의 후기 (3) | 2025.07.30 |
| 패스트캠퍼스 환급챌린지 29일차 : 테디노트의 RAG 비법노트 강의 후기 (3) | 2025.07.29 |
| 패스트캠퍼스 환급챌린지 28일차 : 테디노트의 RAG 비법노트 강의 후기 (3) | 2025.07.28 |
Comments