목록언어 AI (NLP)/LLM & RAG (24)
토니의 연습장

✅ 핵심 특징모델 압축 (Model Compression)큰 모델(teacher)의 지식을 작은 모델(student)에 전달해 경량화된 모델을 만듦.소프트 타겟 (Soft Targets)정답 레이블(hard label)뿐만 아니라, teacher의 예측 확률 분포(soft label)를 student가 학습함.이 soft label에는 클래스 간의 유사도 정보가 담겨 있음.일반화 성능 향상student 모델이 단순히 hard label만 학습할 때보다 더 좋은 일반화 성능을 보일 수 있음.모델 구조 유연성student는 반드시 teacher와 같은 구조일 필요는 없음. 훨씬 작거나, 다른 구조여도 가능. ✅ Distillation Loss의 정의Distillation Loss는 Knowledge Dis..

from operator import itemgetterfrom langchain.memory import ConversationBufferMemoryfrom langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain_core.runnables import RunnableLambda, RunnablePassthroughfrom langchain_openai import ChatOpenAI# ChatOpenAI 모델을 초기화합니다.model = ChatOpenAI()# 대화형 프롬프트를 생성합니다. 이 프롬프트는 시스템 메시지, 이전 대화 내역, 그리고 사용자 입력을 포함합니다.prompt = ChatPromptTe..

DDP : Distributed Data ParallelDMP : Distributed Model Parallel출처 : https://youtu.be/Wer9odeBWNg without accumulation : 순차 처리 방식with accumulation : 병렬 처리 방식 (multi-gpu)출처 : https://youtu.be/toUSzwR0EV8
*PDFRAG class 는 하단의 코드 참고from myrag import PDFRAG# 질문에 대한 답변하는 함수를 생성def ask_question_with_llm(llm): # PDFRAG 객체 생성 rag = PDFRAG( "data/SPRI_AI_Brief_2023년12월호_F.pdf", llm, ) # 검색기(retriever) 생성 retriever = rag.create_retriever() # 체인(chain) 생성 rag_chain = rag.create_chain(retriever) def _ask_question(inputs: dict): # 질문에 대한 컨텍스트 검색 context = r..
*아래는 전체코드가 아닌 핵심 코드만을 다루고 있습니다.참고) RAGAS: https://docs.ragas.io/en/latest/getstarted/evaluation.htmlfrom ragas.testset.generator import TestsetGeneratorfrom ragas.testset.evolutions import simple, reasoning, multi_context, conditionalfrom ragas.llms import LangchainLLMWrapperfrom ragas.embeddings import LangchainEmbeddingsWrapperfrom ragas.testset.extractor import KeyphraseExtractorfrom ragas.te..

*참고 : llm_call, llm_call_async functino 은 가장 하단의 utils.py 참고 1. 프롬프트 체이닝 (Prompt Chaining) prompt_chaining.pyfrom typing import Listfrom utils import llm_calldef prompt_chain_workflow(initial_input: str, prompt_chain: List[str]) -> List[str]: response_chain = [] response = initial_input for i, prompt in enumerate(prompt_chain, 1): print(f"\n==== 단계 {i} ====\n") final_promp..
vectorDB에 넣기 위해 중복문장을 제거할때,unique 문장을 뽑아내면서 리스트를 순회하지 않고 딕셔너리를 순회하며 키를 True로 넣는 이유unique_texts = {}docs_processed_unique = []for doc in docs_processed: if doc.page_content not in unique_texts: unique_texts[doc.page_content] = True docs_processed_unique.append(doc)이 코드에서 리스트 대신 딕셔너리를 사용하여 중복 여부를 확인하고, 키를 True로 넣는 이유를 설명해 드리겠습니다.🛠️ 1. 리스트와 딕셔너리의 성능 차이✅ 리스트 조회 (O(n))리스트에서 중복 여부를 확..

1. gguf -> ollama변환 도구 확인:gguf는 주로 llama.cpp 등에서 사용되는 모델 포맷입니다. 커뮤니티에서는 gguf 모델을 safetensors나 Hugging Face 형식으로 변환하기 위한 스크립트나 도구가 공유되고 있으므로, GitHub 이슈나 관련 리포지터리를 먼저 검색해보시기 바랍니다.예시 변환 (가상의 명령어):만약 변환 스크립트가 있다면 아래와 같이 실행할 수 있습니다.이 과정에서 모델 가중치와 토크나이저 파일이 Ollama에서 사용할 수 있는 형식(예: safetensors 파일들)으로 생성되어야 합니다.python convert_gguf_to_safetensors.py --input model.gguf --output /path/to/converted-model- Y..