Vuex를 다시 일하면서 쓸일이 생겼다..

ㅎㅎ.. 알고는 있지만 정확하게 이해하고 있는거 같진 않아서 다시 정리해보고자 한다!

(백엔드 위주로 하고싶은데.. 참,,, FE 를 놓지 못하게 한다 회사가)

 

 

 


1. Vuex 란?

  • Vue의 상태(State) 관리 도구
    • 상태: 여러 컴포넌트 간에 공유되는 데이터 속성
  • 서비스가 복잡해 졌을 때, 데이터를 다른 컴포넌트에 공유해야 할 수도 있음
    • 컴포넌트 레벨이 깊어지거나 관계가 복잡했을 때 상태관리 도구는 유용하게 사용
  • 컴포넌트에서 API를 불러와 화면을 그리는 것이 아닌, Vuex에서 API를 불러옴
    • Vuex의 State에 API로부터 받아오 데이터를 담음
    • State를 컴포넌트에 전달하여 화면을 그림
  • Vuex 구성요소
    • state : 여러 컴포넌트에 공유되는 data
    • getters : 연산된 state 값을 접근하는 속성 computed
    • mutations : state 값을 변경하는 이벤트 로직, 메서드 methods
    • actions : 비동기 로직을 선언하는 async mehods

 

2.State

  • 여러 컴포넌트 간에 공유할 데이터 - 상태

 

 

3.Getter

  • state값을 접근하는 속성이자 computed() 처럼 미리 연산된 값을 접근하는 속성

 

 

4. Mutations 이란?

  • state의 값을 변경 할 수 있는 유일한 방법이자 메서드
  • mutations는 commit()으로 동작
// store.js
state: { storeNum: 10 },
mutations: {
  modifyState(state, payload) {
    console.log(payload.str);
    return state.storeNum += payload.num;
  }
}

// App.vue
this.$store.commit('modifyState', {
  str: 'passed from payload',
  num: 20
})

 

  • mutations로 상태를 변경해야하는 이유?
    • 특정 시점에 어떤 컴포넌트가 state를 접근하여 변경한 건지 확인하기 어렵기 때문
    • 따라서, 뷰의 반응성을 거스르지 않게 명시적으로 상태변화를 수행
    • 반응성, 디버깅, 테스팅 혜택

src/store/index.js

import Vue from 'vue';
import Vuex from 'vuex';
import {fetchNewsList} from '../api/index.js';

// Vuex는 플러그인 형식으로 제공
Vue.use(Vuex);

//  인스턴스
export const store = new Vuex.Store({
  state : {
    news: []
  },
  mutations: {
    // 첫번째 인자 state, 두번째 인자 action으로부터 전달받은 값
    SET_NEWS(state, news) {
      // state에 데이터 전달
      state.news = news;
    }
  },
  actions: {
    // mutation에 접근할 수 있도록 context 인자가 제공
    FETCH_NEWS(context) {
      fetchNewsList()
        .then(response => {
          // mutation에 data를 넘길 수 있음
          context.commit('SET_NEWS', response.data);
        })
        .catch(error => {
          console.log(error);
        }) 
    }
  }
})

 

 

src/views/NewsView.vue

<template>
  <div>
	  // store의 state에 접근
    <div v-for="item in this.$store.state.news" > {{ item.title }} </div>
  </div>
</template>

<script>


export default {
  created() {
    // action 호출
    this.$store.dispatch('FETCH_NEWS');
  }

}
</script>

<style>

</style>

 

 

5. Actions 이란?

  • 비동기 처리 로직을 선언하는 메서드
  • 비동기 로직을 담당하는 mutations
  • 데이터 요청, Promise, ES6 , async 와 같은 비동기 처리는 모두 actions에 선언
  • actions에 비동기 로직을 선언해야하는 이유?
    • 언제 어느 컴포넌트에서 해당 state를 호출하고, 변경했는지 규격화를 하여 확인하기 위해
    • mutations에 시간차를 두고 state를 변경하는 경우 추적하기 어려움
    • 그러므로 mutations속성에는 동기처리 로직만 넣어야함
  • Vuex에서 api 호출은 Actions에서 하기
    • 비동기 호출
    • Backend API를 호출하여 Mutations에 넘겨주기 위한 속성
    • Vuex 구조상 actions에서 state에 바로 담을 수 없게 되어 있음
    • Actions는 Vue Component에서 Dispatch라는 api로 호출 가능
// store.js
mutations: {
  setData(state, fetchData) {
    state.product = fetchData;
  },
},
actions: {
  fetchProductData(context) {
    // api 호출 이후 응답을 state에 넣음
    return axios.get('https://domaion.com/products/1')
    						.then(response = context.commit('setData', response));
  }
}

// App.vue
methods: {
  incrementCounter() {
    this.$store.dispatch('fetchProductData');
  }
}

 

 

6. Module

  • state, getters, mutations, actions가 많아질 수록 코드의 가독성은 떨어지고 유지보수는 힘들어짐
    • 별도의 파일로 모듈화 하는 것이 편함
    • actions.js, mutations.js 파일에 각 코드를 옮기기

 

7. Helper

7.1 mapStates

  • Vuex에 선언한 states 속성을 뷰 컴포넌트에 더 쉽게 연결해주는 헬퍼
<template>
  <div>
    <div v-for="item in ask"> {{ item.title }} </div>
  </div>
</template>

<script>
import {mapState} from 'vuex';
export default {
  computed: {
    // store의 state에 접근하여 ask를 가져옴
    // spread 연산자로 computed에 ask를 뿌림
    ...mapState({
      ask: state => state.ask,
    }),
  },
  created() {
    this.$store.dispatch('FETCH_ASK');
  },
}
</script>

<style>

</style>

7.2 mapGetters

  • Vuex에 선언한 getters 속성을 뷰 컴포넌트에 더 쉽게 연결해주는 헬퍼
import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
    // mix the getters into computed with object spread operator
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}
...mapGetters({
  // map `this.doneCount` to `this.$store.getters.doneTodosCount`
  doneCount: 'doneTodosCount'
})

7.3 mapMutations

  • Vuex에 선언한 mutations 속성을 뷰 컴포넌트에 더 쉽게 연결해주는 헬퍼
// App.vue
import { mapMutations } from 'vuex';

methods: {
  ...mapMutations(['clickBtn']),
  atuhLogin() {},
    displayTable() {}
}

// store.js
mutations: {
  clickBtn(state) {
    alert(state.msg);
  }
}

 

<button @click="clickBtn">popup message</button>

 

7.4 mapActions

  • Vuex에 선언한 actions 속성을 뷰 컴포넌트에 더 쉽게 연결해주는 헬퍼
// App.vue
import { mapActions } from 'vuex';

methods: {
  ...mapActions(['delayClickbtn']),
}
  
// store.js
actions: {
  delayClickBtn(context) {
    setTimeout(() => context.commit('clickBtn'), 2000);
  }
}
<button @click="delayClickBtn">delay popup message</button>

참고:

https://greedysiru.tistory.com/808

 

Vue.js - Vuex, Vuex 모듈화 및 state 적용

본 내용은 인프런 장기효(캡틴판교)님의 Vue.js 완벽 가이드 - 실습과 리팩토링으로 배우는 실전 개념 강의를 토대로 작성하였습니다. 1. Vuex Vue의 상태(State)관리 도구 상태: 여러 컴포넌트 간에 공

greedysiru.tistory.com

https://vuex.vuejs.org/guide/getters.html#the-mapgetters-helper

 

Getters | Vuex

Getters Sometimes we may need to compute derived state based on store state, for example filtering through a list of items and counting them: computed: { doneTodosCount () { return this.$store.state.todos.filter(todo => todo.done).length } } If more than o

vuex.vuejs.org

 

다음 글은 인프런의 무료 강의를 요약 정리한 내용입니다

 

 

링크 : https://www.inflearn.com/course/llm-%EC%95%A0%ED%94%8C%EB%A6%AC%EC%BC%80%EC%9D%B4%EC%85%98-%EA%B0%9C%EB%B0%9C%EA%B2%BD%ED%97%98-%EA%B3%B5%EC%9C%A0%ED%9A%8C/dashboard

 

[무료] LLM Application 개발 경험 공유회 - 인프런 | 강의

LLM Application 개발을 위한 기본 배경지식과 개발 경험을 공유합니다., 5월에 진행한 LLM Application 개발 지식 & 경험 공유회를 업로드합니다! LLM Application을 개발하시는 분들에게 조금이라도 도움이

www.inflearn.com

찾아보니 유튜브에도 똑같은 내용으로 올라와 있다

링크 : https://www.youtube.com/live/TJ2mYNpUTAY?si=lQsgGqbAZm7k10TR

 

처음엔 가볍게 듣고자 했는데 

생각보다 너무나도 알차서 어느순간 필기하고 있는 나자신을 발견...

찾다보니 유튜브에도 많은 강의가 있던데..

 

다음엔 LangChain을 다룬 강의도 들어 볼까함,,

 


1. LLM Base Knowledge

 

LLM(Large Language Model)은 현재 NLP분야에서 가장 주목
- 트랜스 포머 기반(Decoder Only)모델이 대세
- 상용으로는 ChatGPT4 , PaLM2
- 오픈 소스가 열심히 추격중 (요샌 falcon?)

 

 

 

사진출처&nbsp; :&nbsp; https://velog.io/@dongyoungkim/GPT-fine-tuning-7.-Glossary


Fine-Tuning
- Pre trained Model(like ChatGPT)을 용도에 맞게 튜닝하는 것
- Instruction fine tuning -> RLHF(Reinforcement Learning with Human Feedback) -> Adaptor(P-tuning,LoRA) 방식으로 발전!
- 오픈 소스 LLM 기반 파인 튜닝이 얼마나 퍼포먼스가 잘 나오는지는 아직 모름
- ChatGPT는 gpt-3.5-turbo, gpt-5모두 Fine Tuning API 

In Context Learning
 - 우리 모두 열심히 하고 있는 Prompt Engineering이 바로 이것
 - 별도의 모델 가중치 업데이트를 시키지 않고 명령 프롬프트 내에서 원하는 대답을 얻게 하는 것
 - X-Shot Learning, Chain-of-Thought, Self-Consistency 등의 여러 기법들 존재


2. LLM Application (RAG)


LLM Application 이란?
- 기존 애플리케이션: 코드를 통해 결정적인(Deterministic) Rule Base 기반으로 동작함
- LLM 애플리케이션 : 비결정성이 포함된(지멋대로의) LLM의 추론 결과를 바탕으로 동작함
- LLM은 사람 수준의 추론을 가능하게 해줘서 기존에 가지고 있던 문제를 수비게 풀어 줄 수 있음
(추천,분류,챗봇,AI어시스턴스)

 LLM Application 간단 플로우
1) 클라이언트의 입력이 들어온다
2) 미리 설정한 프롬프트에 질문을 넣어서 LLM에게 요청한다
- 필요한 경우 질문에 필욯나 데이터를 포함시킨다
- 외부 API, 데이터베이스, VectorDB 등
3) 요청 결과를 바탕으로 새로운 프롬프트,함수를 실행한다(Chaining)
4) 최종 결과가 나오면 답변으로 반환한다.

 

사실 현재 대부분은 결국 Private/Public 데이터를 기반으로 대답을 해주는 방식으로 LLM Application 개발중
  - 고객센터 챗봇
  - 법률 판례 검색
  - 주식 리포트 생성


 이는 RAG( Retrieval Augmented Generation) 이라고 하는데, 사용자 Input에 수집된 데이터를 합께 프롬프트에 담아 질문하는방식
->  결국 본질은 1. 질문에 필요한 데이터를 최대한 잘 뽑아서 2. LLM이 잘 대답해 줄 수 있도록 하는 것
 

 


<Data Retrieval>

유저 질문(Input)에 답변할 떄 필요한 정보를 가져오기 위해서 여러가지 방법이 있음

1. 외부 API 활용
 - search api (google search , bing...) -> 비쌈
 - web scraper
 - Saas API (Slack , Notion)
 - Document loading (PDF,CSV, ...)
 보통 Search API를 활용해서 답변의 신뢰도를 높이는 경우가 많음 (하지만 비쌈)
 LangChain, LlamaIndex, Unstructured 같은 오픈소스들에서 많이 지원해줌
 
2. Structured 데이터베이스 활용
 - Data Warehouse(BingQuery, Snotflake, ...), RDBMS
 - 서비스 운영에 필요한
 - 질문을 엔진에 질의할 수 있는 SQL 형태로 변경 필요 (LLM 에게 요청하기)
 
 1) Question to LLM : 1주일 전 테슬라 가격 알려줘
 2) LLM Answer : SELECt ticker, price FROM stock_price WHERE create_at = '2023-06-07 00:00:00'
 3) Action : RDBMS에 쿼리
 
3. 키워드(Keyword) 기반 혹은 전문(Full Text) 검색
 - Elastic Search , Solr , OpenSearch 등
 - 기본적으로 많이 알려진 검색 엔진들 활용하기
 - Saas(Algolia) 형태로 제공해주는 엔진을 사용하는 것도 빠른 시도에 도움이 될듯 
 
4. 유사도 기반 검색 ( Similarity Search) *** 문맥의 유사도 분석
 - 두 벡터의 거리를 기반으로 유사도를 측정함
- 일반적으로 텍스트를 임베딩(Enbedding)시킨 벡터로 변환하여 거리 기반 유사도를 검사
 - 임베딩 모델을 통해 텍스트를 벡터로 임베딩하게 됨
 - 임베딩된 벡터들이 들어간 데이터베이스를 VectorDB라고 함
- VectorDB는 벡터간 유사도 검색을 내부에서 지원해줌
- 사용자는 그냥 API로 손쉽게 검색만 하면 됨

5. VectorDB는 현재 크게 게임 체인저는 없음. 다 비슷비슷함
 - Chroma, FAISS (테스트 환경, Evaluate 등의 로컬 환경에서 돌릴 때 유용)
 - Pinecone, Milvus, Weaviate 같이 요새 핫한 친구들도 나옴 
 - Elastic Search, Redis 처럼 범용 데이터베이스에서 지원해주기도 함
 
 결국 유사도 검색의 핵심은 질문의 벡터와 답변의 벡터가 잘 연결 될 수 있도록 Embedding 하는 방식이 중요
 - Embedding API로 openAI에서 제공하는 Embedding API 많이 사용자
- OpenAI Embedding API의 가격이 75% 저렴해짐
 - Embedding Model도 계속 발전하고 있으니 계속 지켜봅시다
 
6. 우리는 Pinecone 사용
 - 선택한 가장 큰 이유는 직관성 + 쉬운 사용 (Free Trial 제공부터 하면서 넘어옴)
 - Hybrid Search(Metadata Filter) + Upsert도 지원
- 다만 Saas 형태로만 지원하고 Self Hosting이 안되는 건 아쉬움

 


<Data Retrieval Tips>


7. 사실 검색 결과를 높이기 위해선 Semantic Search + Keyword Search 를 함께 적용하는 게 좋음
 - 일반적으로 검색을 통해 다양한 후보군을 가져온 후 ReRanking 하는 작업 진행함 (구글도 그렇다고..)
Cohere가 요새 뜬다는데 쓰는 것도 고려중(API 비용이 막 저렴한 건 아니라서 고민중)
 - 전통적인 검색 엔진 방법(Keyword , full text) + 유사도 검색을 함께 사용해서 정확도를 높이는 것이 좋음
 https://txt/cohere.com/rerank
 
 
8. Vector DB 매칭 확률을 높이기 위해서 여러가지 시도를 해보는 것도 의미가 있음
텍스트 데이터를 어떤 방식으로 넣을건지 고민해보자
  1)어떻게 쪼개서 넣을거니?
   - Chunk Size, Overlap 여러 개로 테스트 해보기
   - Pinpecone은 ChunkSize를 256~512 Token Size 로 추천함(OpenAI Embedding model)
 2) Input을 어떻게 넣을 것인가?
    - 질문이 복잡하다면 쪼개보자! decompose
    - input 을 답변처럼 가공해서 쿼리하는 HYDE 방식도 있음
( 테슬라 얼만지를 답변으로 반환하게 해서 답변을 가지고 유사도)

9.  Embeddubg 방식을 바꿔보는 것도 테스트 해봐도 좋음
 - 실제 벤치마크 점수 기준으로 openAI의 text-embedding-ada-002를 이기는 임베딩 모델들 나옴
 - 하지만 편하게 쓸 수 있는 건 아직까지 OpenAI Embedding이 짱(cohere는 어떤지 궁금)
 https://huggingface.com/spaces/mteb/learderboard
 
10. 다양하게 VectorDB 인덱스를 구성하면서 테스트를 해보는 게 중요
뒷단에서 데이터를 빠르게 넣고 + 멱등하게 관리할 수 있는 Data Engineering infra 필요

<LLM Library>

11. LLM Application을 구성하기 위해서 필요한게 생각보다 있다
 - Prompt 템플릿 + 변수관리
 - 다양한 외부 데이터 접근(API)
 - Vector DB Embedding + Search
 - (이전에 답변에 컨텍스트가 필요하다면) Short Term memory
 - (복잡한 태스크 수행이 필요하다면) Agent
 - ...
 
 현재 LangChain, LlamaIndex, Semantic Kernel 오픈 소스들이 나오고 있음
 
12. LLM Library(LangChain)
 - LangChain이 LLM Application 개발에서 가장 많이 쓰임
 - LLM과 통신하며 수행하는 작업 단위를 Chain으로 만들어서 관리
 - Vector DB 관련 인터페이스가 가장 직관적이고 깔끔
 - Pyhton, Javascript 모두 지원
 - LangChain 생태계 + 커뮤니티가 가장 활발하다
- Awesome-langchain에서 이모저모 볼 수 있음
https://github.com/kyrolabs/awesome-langchain
 - 근데 다큐먼트 솔직히 너무 불칠절하다
Pinecone에서 런북 만든게 설명 잘되어 있음 (https://www.pinecone.io/learn/langchain)
 - Langchain에 뭐가 많기는 하지만 결국 유즈케이스에 맞게 직접 커스터마이징 하게 됨
 - LangChain이 좋은 건 뛰어난 확장성
- 우리는 우리 용도에 맞게 커스터마이징 많이 해서 사용중
- 여러 용도에 맞게 custom chain을 만들어서 사용자
- Conversational Ceontext를 유지하게 위해 Custom Memory(서비스 DB와 통신)를 만들어 구현
- LLM 결과 모니터링을 위해 Custom Callback 구현해서 메타데이터, 지표 모니터링 

13. LLM Library (Llama Index)
 - LangCahin과 유사하게 LLM Application 개발 올인원 툴로 사용 할 수 있음
 - Index라는 단위로 데이터를 구조화 시켜 쿼리를 용이하게 함
- 다양한 Index(VectorStore, Tree, Knowledge Graph 등)을 지원함
- 즉 기존 데이터 소스를 더 효과적으로 찾을 수 있도록 인덱스 방식을 지원해줌
 - 다양한 Query Engine을 지원해서 Index에 질의하는 방식을 다양화 함
- 복잡한 질문을 쪼개서 질문하기
 - 확실히 Langchauin에 비해서 Data Retrieval 하는 과정과 Post Processing을 잘 지원해줌
 - Langchain과 꽤 겹치는 부분이 있기도 하고 확실히 Retrieval 쪽에 강점이 있음
 - 그러나 도입하려다 제외함. Langchain에 비해 러닝커브가 높고 Index management하기 쉽지 않음
 - 여러 기법들을 적용하는 게 과연 얼마나 퍼포먼스 향상에 동무이 될지는 모르겠음 -> 결국 모두 실험해 봐야하는 데 이것 또한 비용
 - Data Retrieval에 대한 퀄리티 고민을 더하게 될 때 도입 다시 해볼듯
 
 
14. 복잡합 유즈케이스
 - 하나의 프롬프트에 많은 역할과 추론 과정을 요구할 때, 원하는 대로 말을 안들을 때가 많음
- 만약 이게 가능한 모델이 있다 하더라도(gpt-3.5는 우선 아님) 좋은 Prompt를 만지는 데 시간을 꽤 투자해야함

 - 미국 주식 Q&A 챗봇 예시
1). 질문 유형에 따른 다양한 요구사항이 있음
   - 시황을 물어볼 경우 시의성 중요함
  - 특정 주식의 Fundamental 정보를 물어볼 경우 해당 정보에 접근하는 API 활용하기
  - 질문이 여러 문맥을 포함하고 있을 수 있음
  - 최근 3개월 간 가장 수익률이 높은 주식의 PER은 몇이야?(질문금지!)

 2) 해결 방법( 두방식을 보통 같이 활용하곤 )
  가. 프롬프트의 역할을 명확하게 해서 쪼갠 후 Chaining하기
  나. Retrospective 하게 추론 & 실행을 반복하는 Agent 활용하기( 반복적으로 물어보기)

 15. Chaining하기
- Contol flows with LLMs
https://huyenchip.com/2023/04/11/llm-engineering.html#part_2_task_composability
- Sequential / Parallel / If / For loop

 - 프롬프트를 Chaining하여, 개별 단계에서 답변의 정확도를 높이기 (우리는 파이프라인이라 부름)
 - 다만, 답변 생성 시간/토큰 비용이 Trade-off + 초반 프롬프트에서 답변이 이상하면 downstream으로 에러가 전파될 수 있음
 - 파이프라인을 잘 구성하기 위해서는 엔지니어링 리소스가 많이 들어감
- 예외처리, 유닛테스트/E2E테스트 모두 진행해봐야 함
 - 우리 팀도 여러 파이프라인을 구성하여 답변 퀄리티를 높이기 위해 E2E테스트를 수시로 진행함
 
 16. Agent (LLM에게 계속 반복적으로 물어봄..)
 - Agent : 목표와 사용가능한 도구를 주면 스스로 행동하도록 하는 방식 혹은 아키텍처 혹은 코드 구현체
 - 외부 리소스(구글 서치, Open API, 기타 API 등)를 활용해서 복잡한 태스크를 수행해야 할 때 유용함
 - AGI(Artificial General Intelligence)를 구현하는 기본적인 방식임 auto gpt-3


 쉽게 설명하면 아래와 같이 동작함 ( recursive 하게 동작함)


  1) LLM에게 미리 정의한 툴(Search, VectorDB , 외부 API등)을 알려주고 질문에 대답하기 위해 툴을 선택하게 한다
  2) 선택한 툴을 코드로 실행해서 결과를 얻는다
  3) LLM에게 결과를 주고 질문에 충분히 대답할 수 있는지 물어본다. 만약 충분히 대답이 되면 결과 반환 후 종료
  4) 대답이 안되면 답변에 필요한 새로운 질문을 생성해 1-3 반복

 

- Auto-GPT, BabyAGI, Jarvis(HuggingGPT)등 다양한 아키텍처이자 구현체가 존재함
- 다만 위 친구들이 얼마나 활용도가 높은지는 모르겠음(구현체인 만큼 자유도가 떨어짐)
 - Agent를 사용하려면 자유도를 높게 가져갈 수 있는 Langchain을 활용하는 걸 추천함
- Langchain으로 autoGPT 구현한 코드도 있음( https://python.langchain.com/en/latest/use_cases/autonomous_agents/autogpt.html )
 - Agnet는 Action의 Full Cycle이 LLM의 추론으로 돌아가는 방식임. 따로 운영시 테스트/디버깅이 힘들 수 있음.
충분히 잘 검토해보고 도입 필요가 있음
 - 개인적으로는 예측 가능한 솔루션을 만들려면, Agent보다는 Prompt Chain + Rule Base(if-else 같은)로 가져가는 게 더 나아보임
 
17. 운영에서 신경 쓸 것
유저 인터페이스를 어떻게 가져가냐에 따라 기능적 요구사항이 달라짐
  1) 챗봇 형태인가?
    - 응답 Latency가 중요 -> Streaming 구현
    - 이전 대화를 바탕으로 대답할 것인가? -> Memory 구현
   2) 사용자가 어떻게 사용할 수 있는가?
    - Quota가 따로 없다면 ChatGPT의 경우 Rate Limit을 신경 써야함
         - 백그라운드에서 Bulk로 돌리는 경우가 많으면 OpenAI Organization 추가하는 게 좋음 API 두개로 들고?
    - 인증이 없다면 외부 공격에 취약할 수 있음(다 돈이다!)
   3) LLM Application 운영시 주요 metric
     - Token Size
     - First touch latency : 얼마나 답을 빠르게 시작했나
     - Last touch latency : 얼마나 답을 빠르게 종료 했냐
 4)후행 지표로 답변 Quality도 체크해야함
    - 답변에 대한 Evaluation을 하는 파이프라인을 뒷단에 구성하는 것도 방법
 5) 답변 퀄리티에 대해서 지속적으로 Evaluation하고 Quality Control 필요
    평가 기준을 세우고 일관된 템플릿으로 답변 결과에 대한 퀄리티 비교 및 제안
 6)Data Ingestion Infra 고민해봐야함
   - 결국 원본 데이터를 보관하기 위한 Stage(Data Warehouse, 운영 DB 등)를 두고 용도에 맞게 Ingest 시켜야함 
   - 워크플로우 툴(e.g Airflow)을 적용하는 게 추후 좋을 수 있음 

18. 느낀점
 - 해당 애플리케이션을 개발한다는 것은 AI Engineering + Data Engineering(+MLOps) + Backend
 - Engineering 이 적절하게 섞여는 느낌
 - 생각보다 답변을 제대로 하는 LLM Application을 만들기까지 노력이 꽤 들어감
- LLM 답변에 대한 퀄리티와 신뢰성을 높이기 위한 작업이 쉽지 않음
- 결국 답변 퀄리티를 높이기 위한 Ops 환경 구성이 생각보다 비용이 들음 (MLOps와 유사)
 - 결국 LLM Application의 품질은 답변과 직접적으로 연결되어 있음. 답변 퀄리티를 높이기 위해선 Data Retrieval이 제일 중요한 것 같음
- 프롬프트를 계속 만지는 것보다 질문에 적합한 Data를 가져오도록 하는 게 더 나을수도
- 더 나은 답변이 나오도록 계속해서 실험해봐야함 이를 위한 실험 환경도 구성되어야함

 




3. LLM OPs

 

 1.LLM을 위한 MLOps 환경
    - LLM : 언어를 출력으로 하는 대규모 딥러닝 모델
    - MLOps : ML기 1. LLM을 위한 MLOps 환경
    - LLM : 언어를 출력으로 하는 대규모 딥러닝 모델
     - MLOps : ML기반 애플리케이션 수명 주기를 관리하는 전체 환경
 - 이미 학습된 LLM이 있다는 전제 하에 환경이 구성되었다는 점에서 MLOps와 다름
     - 실험 측면에서도 MLOps는 모델의 아키텍처, 하이퍼 파라미터, 데이터 보강을 집중
     - LLMOps는 프롬프트, 파인튜닝을 주로 봄 

 


 <LLM Ops 란?>
 2. LLMOps는 무엇을 가능하게 하는가
1) LLM 관련
   - Foundation Model 선택
  - Fine tuning
2)Prompt Engineering
  - 프롬프트 별 버저닝 및 히스토리 관리 
  - 프롬프트 실행
3)Enxternal data Management
  - Test Data Set (보통 Q&A 셋)
  - Vector DB (& Embedding)
4)Evaluation
  - 프롬프트, 파이프라인(Prompt Chained)결과 평가
  - 유저 피드백 루프
  - A/B Testing
5)Deployment
   - Prompt, 파이프라인 배포
  - LLM Metric 모니터링
 3. Tool 소개
- Vellum 
- HoneyHive
- FlowGpt, langflow ,flow eyes : prompt 끼리 chaining

 


 4. 오픈소스/ Saas 보면서 느낀점
1)뭔가 하나를 딱 쓰고 싶지만, 다 애매함. 펀딩받고 있느 ㄴ단계의 서비스들이 많다 보니 아직까지 성숙도가 떨어짐
 - 프롬프트 테스트 후 배포해도, 결국 배포된 api 하나만 사용하는게 아님
   - 보통 프롬프트간 Control Flow가 적용된 파이프라인이 필요함
   - +파이프라인이라는 작업 단위에 대한 Fine grained 설정이 필요함
 - Visual Interface로 프롬프트를 체이닝 하는 경우, 중간에 output을 transform하는 경우들이 존재함
 - External Data Management(VectorDB, Others...)에 대한 환경 제공은 아직 부족함
2)사내 LLMOps 툴 개발
 - OpenAI Playground 에서 계속 테스트 하는건 더이상 힘들다고 판단
 - 결국 인하우스 툴이 필요하다고 느껴져, 최소한의 기능으로 어드민 개발
거시적인 관점에서 하나의 질문도 여러번 해보는 것이 좋다 (매번 다른 답이 나오므로)

 


 5. 느낀점
- 초반에 만들어둔 노력대비 아주 잘 사용하고 있음
아무리 못해도 프롬프트 버저닝, 배치실행 기능은 꼭 필요한듯. 지금이 가장 낫다는 보장이 없음
조금 더 예측 가능하고 정량적으로 평가를 진행하는 게 중요
- 이게 더 나을거 같아요 X
- 테스트에서 이런 경향성을 보이고 있어요. 이어서 ~~~을 설정해서 운영중인 프롬프트와 비교해볼게요 O
다만 하나의 프로덕트를 만드는 느낌이라 비용이 꽤 들어갈 수 있는 걸 고려해야함
- DB 설계부터 버저닝, 배포 환경 격리 등 신경써야 할 게 꽤 많음

 



4.Prompt Engineering

 


 1.
 - 영어로 작성하기 (한글로 쓰고 ChatGPT로 번역해서라도 넣으세요)
 - 간결하게 + 명확하게 작성하기
 - 프롬프트 Instruction 문단에 대한 구분은 $$$ , """" 같은 구분자로 명확하게
 - 예제를 넣자
 - 하지 말라고 하기보단 하라고 하는게 나음
 
 2. X-shot Learning : 예시를 주면서
 단순한 프롬프트일수록 적용해줄 때 효과가 좋음
 zero-shot , one-shot, few-shot : 예제를 넣어라
 
 3. Chain-of-Thought : 생각하는 과정 넣기 추론 하게 하기 
 CoT와 X-Shot은 보통 함께 쓰는 게 좋음
 우리도 기본적으로 CoT + X-Shot 활용함(아예 팀에서 사용할 템플릿 만드는 것 추천)
 
 4.Self-Consistency
 LLM의 비결정적인 문제를 해결하기 위해 여러 번 돌려서 결과 채택하기
 비용 측면에서 비효율적(우린 안씀)
 다만 Data Retrieval을 할때 비슷한 방식을 적용해보는 시도는 가치가 있을지도 
 
 5. Tree-of-Thought
 Tree 형식으로 후보 추론 -> 선택 -> 탐색을 반복하면서 최적의 답을 찾는 방식
 근래 나온 Prompt 기법 중 퍼포먼스가 가장 높다고함
 하지만 꽤 많은 비용(시간+돈)이 Trade-off
 https://github.com/kyegomez/tree-of-thoughts
 


 6. ChatGPT
 결정성 관련
 - 결정성(Determinitsic)을 높여야 하는 경우 Temperature, Top P를 낮게(0이 거의 기본값) 반면 
 창의적인 콘텐츠라면 Temperature나 Top P를 높게 가져가는 게 좋음
 - OpenAI에서는 temperature와 top_p를 함께 만지는 것을 권장하지 않는다고 함
 - 답변이 긴 경우, 반복을 줄이기 위해 Frequency Penalty를 높이는 것 권장
 - 개인적으로, 출력 토큰이 많지 않은 경우에는 Temperature만 조절해도 충분했음
 - ChatGPT 서비스의 gpt-3.5 모델과 gpt-3.5-turbo API는 다름. Gpt-3.5-turbo API 성능이 더 안좋음
 - 따라서 API를 사용하는 경우 ChatGPT 서비스가 아닌 OpenAI Playground로만 테스트 해야함
 - gpt-3.5-turbo가 text-davinch-003(Complete)와 10배 저렴+효율이 비슷하다고 했는데 잘 모르겠음
  text-davinch-003가 거의 대부분 결과를 더 잘 만들어줌
  - gpt-3.5-turbo-0301의 경우 System Message보단 User Message 쓰라고 권장
  (gpt-3.5-turbo에서 system message에 대한 개선이 얼마나 되었는지는 모르겠음)
  
  
  7. Hallucination
  제일 중요한 건 Ground Truth를 잘 쥐어주는 것
  시점에 대해서 제약사항을 주는 것도 좋음
  
 - 답변의 신뢰성이 중요한 유즈케이스에서는 여러 조건으로 프롬프트를 옥죄는 게 나아보임
- ex1. 정말 맞는 지 다시 한번 생각하게 하기
- ex2. 인용을 달게 하기
- 물론 옥죄는 만큼 신뢰성을 높아지지만 답변의 다양성은 낮아질 수 있음
(그럼에도 불구하고 Hallucination이 종종 발생함)
 - PaLM2 같이 최신 정보를 바탕으로 대답이 가능한 모델을 쓰거나 ChatGPT4를 쓰는 것도 좋음
 - 하지만 결국 직접 파인튜닝 하지 않을거라면, 댇바에 필요한 Ground Truth 소스들을 잘넣어 주는 게 퀄리티에 갖아 큰 영향을 끼침
- 결국 데이터를 잘 가져오자!


 8. 프롬프트 평가(Evaluation)
 우리가 만든 프롬프트는 정말 나은걸까?

 

1) 정답이 있다면?
- 정답이 객관식이라면 Equal Check
- 정답과 얼마나 유사한지 ChatGPT에게 물어보기
- 정답과의 유사도(Embedding)를 통한 점수화
- 사람이 측정


2) 정답이 없다면?
- 프롬프트 간 비교 결과를 메트릭으로(자동화)
- 사람이 측정(평가 기준을 명확하게 세우기)


 프롬프트를 평가하면서 느낀건데,
 
프롬프트에 역할, 규칙이 많아질수록 말을 안듣는다
- 즉, 몇개 돌려보고 끝내는게 아니라 충분하 데이터 셋을 여러번 돌려보자
동일한 질문도 여러 번 돌려보는 것도 방법
- 포맷에 대한 확인도 꼭해보기
(이번 OepnAI Update에서 ChatGPT가 Structured format을 지원한다고 함)

9. 프롬프트 관리
 팀에서 프롬프트를 관리하려면, 애초에 기본적인 포맷에 대한 템플릿은 Source of Truth로 잡고 가는 게 낫다
 (가독성도 가독성이지만 프롬프트를 처리하는 코드-레벨에서 편리함)
 
 
10. 느낀점
 - 프롬프트에 정답은 없다. 너무 개별적인 것들에 집중하면 머리아픔(거시적으로 봐야함)
 - 프롬프트 엔지니어링은 어느정도만 잘해놓고 다른일 하는게 나을수도
- LLM의 성능이 계속 더 발전하니, 우리가 고민하는 프롬프트 엔지니어링 이슈들이 금방 해결 될수도 있다.
 - Hallucination의 본질적은 문제는 결국 Ground Truth를 잘 줘야함. 즉, Input에 해당하는 정보들을 잘 가져오는게 
 프롬프트를 튜닝하는 것보다 답변 퀄리티가 더 잘나올 수 있음
 
 

python이란?

  • 병렬 실행 불가능 (https://it-eldorado.tistory.com/160)
    • Python 인터프리터 : Python으로 작성된 코드를 한 줄 씩 읽으면서 실행 하는 프로그램 → CPython
    • GIL ( Global Interpreter Lock) → 한 프로세스 내에서, Python 인터프리터는 한 시점에 하나의 쓰레드에 의해서만 실행 가능 ( Python 의 객체들에 대한 접근을 보호하는 일종의 뮤텍스 Mutex)
      • Mutex : 멀티 쓰레딩 환경에서 여러개의 쓰레더가 어떠한 공유 자원에 접근 가능 할 떄, 그 공유 자원에 접근하기 위해 가지고 있어야 하는 일종의 열쇠
    •  
    • GIL이 필요한 이유? Race Condition을 방지하기 위해 Mutex 필요
      • Python 에서 GC(Garbage Collection)는 Object의 참조횟수가 0이 되면 해당 객체를 메모리에서 삭제 시키는 메커니즘으로 동작
      • 여러개의 Thread 가 Python 인터프리터를 동시에 실행하면 Race Condition(하나의 값에 여러 쓰레드가 동시에 접근해서 값이 올바르지 않게 Read/Write 할 수 있는 상태)이 발생 할 수도 있음
    • → CPU연산이 비중이 적은, 즉 외부 연산(I/O, Sleep 등) 의 비중이 큰 작업을 할 때는 멀티 쓰레딩이 굉장히 좋은 성능!

 

LangFlow

 

 

FastAPI

 

 

 

LangChain

  • LLM 프롬프트의 실행과 외부 소스의 실행(계산기, 구글 검색, 슬랙 메시지 전송이나 소스코드 실행 등)을 엮어 연쇄(Chaining)하는 것
  • https://python.langchain.com/docs/get_started/introduction
  • https://corp.onda.me/post/developing-llm-applications-with-langchain
    • LangChain Library : Python 및 JavaScript 라이브러리. 수많은 구성 요소에 대한 인터페이스 및 통합, 이러한 구성 요소를 체인 및 에이전트로 결합하기 위한 기본 런타임, 체인 및 에이전트의 기성 구현이 포함
    • LangChain Template : 다양한 작업을 위해 쉽게 배포할 수 있는 참조 아키텍처 모음
    • LangServe: LangChain REST API로 배포하기 위한 라이브러리
    • LangSmith: LLM 프레임워크에 구축된 체인을 디버그, 테스트, 평가 및 모니터링하고 LangChain과 원활하게 통합할 수 있는 개발자 플랫폼
  • Pydantic
    • https://seoyeonhwng.medium.com/pydantic-살펴보기-27c67273f0be
    • pydantic은 파이썬 타입 어노테이션을 사용해서 데이터 유효성 검사와 설정 관리를 하는 라이브러리
    • pydantic은 validation이 아닌 parsing 라이브러리이기 때문에 input data를 정의된 타입으로 변환하여 output model의 타입과 제약 조건을 보장

 

 

 

IDE for Python 

 

부모 객체를 상속받아 여러 자식객체들로 이루어진 jsonString을 java backend에서 

VO로 mapping하고 싶은 경우가 있다. (deserialize)

 

 

다음과 같은 구조의 class들이 있다. 

public abstract class Animal{
 private String type;
 private String name;
}
public class Dog extends Animal{
 private int age;
 
 //getter , setter
}
public class Cat extends Animal{
 private boolean isHome;
 
 //getters, setters
}

 

Animal이 부모이고 Dog과 Cat이 자식이다.  Dog과 Cat 모두 Animal을 상속 받고 있다.

 

 

다음과 같은 json String이 있으면 이 code를 사용해서 vo객체에 mapping 해줄 수 있다.

 

public List<Animal> deserializeJsonStringToObj(String str){
 // example :  str = "[{\"name\":\"Milo\",\"age\":\"9\",\"type\":\"Dog\"},{\"name\":\"miyao\",\"isHome\":\"true\",\"type\":\"Cat\"}]";

 Type listOfAnimals = new TypeToken<ArrayList<Animal>>(){}.getType();
 RuntimeTypeAdapterFactory<Animal> adapter = RuntimeTypeAdapterFactory.of(Animal.class, "type")
      .registerSubtype(Dog.class)
      .registerSubtype(Cat.class);
      
Gson gson = new GsonBuilder().registerTypeAdapterFactory(adapter).create();
return gson.fromJson(str, listOfFilterJson);
}

 

 

 

RuntimeTypeAdapterFactory을 사용하는게 쉬운데,

gson에 RuntimeTypeAdapterFactory가 종종 없다고도 한다.

그러면 다음 파일을 불러와서 쓰면된다.

 

 

 

package com.sds.lowcode.data.functions;/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Adapts values whose runtime type may differ from their declaration type. This
 * is necessary when a field's type is not the same type that GSON should create
 * when deserializing that field. For example, consider these types:
 * <pre>   {@code
 *   abstract class Shape {
 *     int x;
 *     int y;
 *   }
 *   class Circle extends Shape {
 *     int radius;
 *   }
 *   class Rectangle extends Shape {
 *     int width;
 *     int height;
 *   }
 *   class Diamond extends Shape {
 *     int width;
 *     int height;
 *   }
 *   class Drawing {
 *     Shape bottomShape;
 *     Shape topShape;
 *   }
 * }</pre>
 * <p>Without additional type information, the serialized JSON is ambiguous. Is
 * the bottom shape in this drawing a rectangle or a diamond? <pre>   {@code
 *   {
 *     "bottomShape": {
 *       "width": 10,
 *       "height": 5,
 *       "x": 0,
 *       "y": 0
 *     },
 *     "topShape": {
 *       "radius": 2,
 *       "x": 4,
 *       "y": 1
 *     }
 *   }}</pre>
 * This class addresses this problem by adding type information to the
 * serialized JSON and honoring that type information when the JSON is
 * deserialized: <pre>   {@code
 *   {
 *     "bottomShape": {
 *       "type": "Diamond",
 *       "width": 10,
 *       "height": 5,
 *       "x": 0,
 *       "y": 0
 *     },
 *     "topShape": {
 *       "type": "Circle",
 *       "radius": 2,
 *       "x": 4,
 *       "y": 1
 *     }
 *   }}</pre>
 * Both the type field name ({@code "type"}) and the type labels ({@code
 * "Rectangle"}) are configurable.
 *
 * <h3>Registering Types</h3>
 * Create a {@code RuntimeTypeAdapterFactory} by passing the base type and type field
 * name to the {@link #of} factory method. If you don't supply an explicit type
 * field name, {@code "type"} will be used. <pre>   {@code
 *   RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory
 *       = RuntimeTypeAdapterFactory.of(Shape.class, "type");
 * }</pre>
 * Next register all of your subtypes. Every subtype must be explicitly
 * registered. This protects your application from injection attacks. If you
 * don't supply an explicit type label, the type's simple name will be used.
 * <pre>   {@code
 *   shapeAdapterFactory.registerSubtype(Rectangle.class, "Rectangle");
 *   shapeAdapterFactory.registerSubtype(Circle.class, "Circle");
 *   shapeAdapterFactory.registerSubtype(Diamond.class, "Diamond");
 * }</pre>
 * Finally, register the type adapter factory in your application's GSON builder:
 * <pre>   {@code
 *   Gson gson = new GsonBuilder()
 *       .registerTypeAdapterFactory(shapeAdapterFactory)
 *       .create();
 * }</pre>
 * Like {@code GsonBuilder}, this API supports chaining: <pre>   {@code
 *   RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class)
 *       .registerSubtype(Rectangle.class)
 *       .registerSubtype(Circle.class)
 *       .registerSubtype(Diamond.class);
 * }</pre>
 *
 * <h3>Serialization and deserialization</h3>
 * In order to serialize and deserialize a polymorphic object,
 * you must specify the base type explicitly.
 * <pre>   {@code
 *   Diamond diamond = new Diamond();
 *   String json = gson.toJson(diamond, Shape.class);
 * }</pre>
 * And then:
 * <pre>   {@code
 *   Shape shape = gson.fromJson(json, Shape.class);
 * }</pre>
 */
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
    private final Class<?> baseType;
    private final String typeFieldName;
    private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<String, Class<?>>();
    private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<Class<?>, String>();
    private final boolean maintainType;

    private RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName, boolean maintainType) {
        if (typeFieldName == null || baseType == null) {
            throw new NullPointerException();
        }
        this.baseType = baseType;
        this.typeFieldName = typeFieldName;
        this.maintainType = maintainType;
    }

    /**
     * Creates a new runtime type adapter using for {@code baseType} using {@code
     * typeFieldName} as the type field name. Type field names are case sensitive.
     * {@code maintainType} flag decide if the type will be stored in pojo or not.
     */
    public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName, boolean maintainType) {
        return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, maintainType);
    }

    /**
     * Creates a new runtime type adapter using for {@code baseType} using {@code
     * typeFieldName} as the type field name. Type field names are case sensitive.
     */
    public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) {
        return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, false);
    }

    /**
     * Creates a new runtime type adapter for {@code baseType} using {@code "type"} as
     * the type field name.
     */
    public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
        return new RuntimeTypeAdapterFactory<T>(baseType, "type", false);
    }

    /**
     * Registers {@code type} identified by {@code label}. Labels are case
     * sensitive.
     *
     * @throws IllegalArgumentException if either {@code type} or {@code label}
     *     have already been registered on this type adapter.
     */
    public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
        if (type == null || label == null) {
            throw new NullPointerException();
        }
        if (subtypeToLabel.containsKey(type) || labelToSubtype.containsKey(label)) {
            throw new IllegalArgumentException("types and labels must be unique");
        }
        labelToSubtype.put(label, type);
        subtypeToLabel.put(type, label);
        return this;
    }

    /**
     * Registers {@code type} identified by its {@link Class#getSimpleName simple
     * name}. Labels are case sensitive.
     *
     * @throws IllegalArgumentException if either {@code type} or its simple name
     *     have already been registered on this type adapter.
     */
    public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
        return registerSubtype(type, type.getSimpleName());
    }

    public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
        if (type.getRawType() != baseType) {
            return null;
        }

        final Map<String, TypeAdapter<?>> labelToDelegate
            = new LinkedHashMap<String, TypeAdapter<?>>();
        final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
            = new LinkedHashMap<Class<?>, TypeAdapter<?>>();
        for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
            TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
            labelToDelegate.put(entry.getKey(), delegate);
            subtypeToDelegate.put(entry.getValue(), delegate);
        }

        return new TypeAdapter<R>() {
            @Override public R read(JsonReader in) throws IOException {
                JsonElement jsonElement = Streams.parse(in);
                JsonElement labelJsonElement;
                if (maintainType) {
                    labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
                } else {
                    labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
                }

                if (labelJsonElement == null) {
                    throw new JsonParseException("cannot deserialize " + baseType
                        + " because it does not define a field named " + typeFieldName);
                }
                String label = labelJsonElement.getAsString();
                @SuppressWarnings("unchecked") // registration requires that subtype extends T
                    TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
                if (delegate == null) {
                    throw new JsonParseException("cannot deserialize " + baseType + " subtype named "
                        + label + "; did you forget to register a subtype?");
                }
                return delegate.fromJsonTree(jsonElement);
            }

            @Override public void write(JsonWriter out, R value) throws IOException {
                Class<?> srcType = value.getClass();
                String label = subtypeToLabel.get(srcType);
                @SuppressWarnings("unchecked") // registration requires that subtype extends T
                    TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
                if (delegate == null) {
                    throw new JsonParseException("cannot serialize " + srcType.getName()
                        + "; did you forget to register a subtype?");
                }
                JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();

                if (maintainType) {
                    Streams.write(jsonObject, out);
                    return;
                }

                JsonObject clone = new JsonObject();

                if (jsonObject.has(typeFieldName)) {
                    throw new JsonParseException("cannot serialize " + srcType.getName()
                        + " because it already defines a field named " + typeFieldName);
                }
                clone.add(typeFieldName, new JsonPrimitive(label));

                for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                    clone.add(e.getKey(), e.getValue());
                }
                Streams.write(clone, out);
            }
        }.nullSafe();
    }
}

 

 

 


참고

https://www.baeldung.com/gson-list

vue 에서 code Mirror를 쓰는 방법과

더나아가 MaxLength를 설정하는 방법을 알아보좌!

 

 

1. Code mirror 설치

npm install vue-codemirror --save

 그러면 package.json에 codemirror가 추가된걸 확인 할 수 있다.

 

2. main.js 에 선언

import Codemirror  from 'vue-codemirror';

Vue.use(Codemirror);

 

3. code mirror 컴포넌트 사용

<codemirror v-model="query" :options="options" ref="codeMirror" />

v-model에는 codemirror에 입력되는 값을 변수 선언한 뒤 넣어주고

다양한 모드와 옵션이 있는데 그건 부모 컴포넌트에서 options 로 선언해주고 props로 내려주면된다.

 

나같은 경우에는 query를 입력하는 창이라 관련되어서 옵션을 data에 선언해 주었다.

 

예)

                options: {
                    tabSize: 4,
                    styleActiveLine: true,
                    mode: 'text/x-sql',
                    lineNumbers: true,
                    line: true,
                    lineWrapping: true,
                    theme: 'default'
                },

 

더 다양한 옵션은 다음을 참고해보자

https://github.com/surmon-china/vue-codemirror#readme

 

GitHub - surmon-china/vue-codemirror: ⌨️ @codemirror component for @vuejs

⌨️ @codemirror component for @vuejs. Contribute to surmon-china/vue-codemirror development by creating an account on GitHub.

github.com

 

4. max Length 설정하기

options에 따로 max Length 관련 지원이 없어서 직접 구현하였다.

 

먼저 기존 options에 maxLength라고 선언해준다.

                options: {
                    tabSize: 4,
                    styleActiveLine: true,
                    mode: 'text/x-sql',
                    lineNumbers: true,
                    line: true,
                    lineWrapping: true,
                    theme: 'default',
                    maxLength: 4000,
                },

 

그 뒤 , 다음 링크를 참고하여 구현 하였다.

https://github.com/codemirror/CodeMirror/issues/821

 

MaxLength attribute · Issue #821 · codemirror/CodeMirror

Is there a way to set a MaxLength number of characters allowed?

github.com

 

 

codemirror 소스를 보니 모든 event가 발생하면 emit 해주고 있었다.

 

        const allEvents = [
          'scroll',
          'changes',
          'beforeChange',
          'cursorActivity',
          'keyHandled',
          'inputRead',
          'electricInput',
          'beforeSelectionChange',
          'viewportChange',
          'swapDoc',
          'gutterClick',
          'gutterContextMenu',
          'focus',
          'blur',
          'refresh',
          'optionChange',
          'scrollCursorIntoView',
          'update'
        ]
        .concat(this.events)
        .concat(this.globalEvents)
        .filter(e => (!tmpEvents[e] && (tmpEvents[e] = true)))
        .forEach(event => {
          // 循环事件,并兼容 run-time 事件命名
          this.cminstance.on(event, (...args) => {
            // console.log('当有事件触发了', event, args)
            this.$emit(event, ...args) // EMIT !!!!
            const lowerCaseEvent = event.replace(/([A-Z])/g, '-$1').toLowerCase()
            if (lowerCaseEvent !== event) {
              this.$emit(lowerCaseEvent, ...args)
            }
          })
        })

 

그래서 부모 컴포넌트에서 codeMirror를 사용하는쪽에서 emit 하는 function을 받아주었다.

<codemirror v-model="query" :options="options" ref="codeMirror" @beforeChange="enforceMaxLength" />

 그리고 다음과 같이 더이상 입력되지 않도록 function 을 선언해주었다. 

            enforceMaxLength(cm, change) {
                let maxLength = cm.getOption('maxLength');
                if (maxLength) {
                    let str = change.text.join('\n');
                    let delta = str.length - (cm.indexFromPos(change.to) - cm.indexFromPos(change.from));
                    this.cmLength = cm.getValue().length + delta;
                    if (this.cmLength > maxLength) {
                        this.cmLength = maxLength;
                    }
                    delta = cm.getValue().length + delta - maxLength;
                    if (delta >= 0) {
                        str = str.substr(0, str.length - delta);
                        change.update(change.from, change.to, str.split('\n'));
                    }
                }
                return true;
            },

 

그리고 현재 length를 세어주기 위해 cmLength를 선언해주고 length를 넣어주었다. 

 


Test Double 이란?

실제 객체를 대신해서 테스팅에서 사용하는 모든 방법을 일컬어 호칭한다. 

Java 진영에서는 대표적으로 Mockito가 있습니다.



Mockito의 어노테이션

@Mock
@MockBean
@Spy
@SpyBean
@InjectMocks



1. Java의 test double : Mockito

1. @Mock

Mockito.mock() 코드를 대체

@Mock으로 mock 객체 생성


 

1.2 @InjectMocks

해당 클래스가 필요한 의존성과 맞는 Mock 객체들을 감지하여 , 해당 클래스의 객체가 만들어질때 사용하여

객체를 만들고 해당변수에 객체를 주입하게된다.

1.2 @Spy

- 실제 객체의 스파이를 생성하여 실제 객체의 메소드를 호출 할 수 있게 합니다.

- stub 하면 stub 하는 객체 , 아니면 실제 객체를 호출 합니다. 
- 하나의 객체를 선택적으로 stub 할 수 있도록 하는 기능 

- mockito.spy()도 사용가능

- When Returns 해서 어떤값이 들어갔을때 해당 값이 리턴되도록 미리 선언해둔다

이유는.. 해당 method는 부가적인 기능이라 중점적인 기능을 test 하기위해 미리 선언해두는 것을 stubbing이라고 한다.

- 둘의 가장 큰 차이점은 @Spy 실제 인스턴스를 사용해서 mocking을 하고, @Mock은 실제 인스턴스 없이 가상의 mock 인스턴스를 직접 만들어 사용한다는 것이다. 그래서 @Spy Mockito.when() 이나 BDDMockito.given() 메서드 등으로 메서드의 행위를 지정해 주지 않으면 @Spy 객체를 만들 때 사용한 실제 인스턴스의 메서드를 호출한다.

 

stubbing 예제

// stubbing
when(mockedList.get(0)).thenReturn("ok");
when(mockedList.get(1)).thenThrow(new RuntimeException());

 

- @Spy 는 객체 instance의 초기화를 해주어야한다 

@Spy
List<String> spyList = new ArrayList<String>(); //초기화

@Test
public void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
    spyList.add("one");
    spyList.add("two");

    Mockito.verify(spyList).add("one");
    Mockito.verify(spyList).add("two");

    assertEquals(2, spyList.size());
}



2. SpringBootTest의 Test double


1. @MockBean


@MockBean은 스프링 컨텍스트에 mock객체를 등록하게 되고 스프링 컨텍스트에 의해 @Autowired가 동작할 때 등록된 mock객체를 사용할 수 있도록 동작합니다.

 


- Spring 영역의 어노테이션
- @Mock은 @InjectMocks에 대해서만 해당 클래스안에서 정의된 객체를 찾아서 의존성을 해결합니다.
- @MockBean은 mock 객체를 스프링 컨텍스트에 등록하는 것이기 때문에 @SpringBootTest를 통해서 Autowired에 의존성이 주입되게 됩니다.

- @Autowired라는 강력한 어노테이션으로 컨텍스트에서 알아서 생성된 객체를 주입받아 테스트를 진행할 수 있도록 합니다.

Mock 종류

의존성 주입
@Mock @InjectMocks 
@MockBean @Autowired

 

 


2. @SpyBean

- @MockBean과 마찬가지로 스프링 컨테이너에 Bean으로 등록된 객체에 대해 Spy를 생성

- @SpyBean이 Interface일 경우 구현체가 반드시 Spring Context에 등록되어야 합니다. => 등록되지 않은 상태라면, @MockBean을 사용하는 것이 좋은 방법이 될 수 있습니다.

- @SpyBean은 실제 구현된 객체를 감싸는 프록시 객체 형태이기 때문에 스프링 컨텍스트에 실제 구현체가 등록되어 있어야 합니다.


참고

https://cobbybb.tistory.com/16

https://www.baeldung.com/mockito-spy

https://velog.io/@june0313/Mockito-Mock-%EB%A6%AC%EC%8A%A4%ED%8A%B8%EB%A5%BC-%EC%A3%BC%EC%9E%85%ED%95%98%EA%B3%A0-%ED%85%8C%EC%8A%A4%ED%8A%B8-%ED%95%98%EA%B8%B0

vue 개발을 하다가 data의 값이 잘 바꼈는데

컴포넌트 rerendering이 잘안돼서 그대로 이상한 값이 남아 있는 경우가 있었다.

 

그래서 찾아보니 좋은 stackOverFlow를 발견..

강제로 rerendering을 시켜주고 싶었다!

 

https://stackoverflow.com/questions/32106155/can-you-force-vue-js-to-reload-re-render

 

Can you force Vue.js to reload/re-render?

Just a quick question. Can you force Vue.js to reload/recalculate everything? If so, how?

stackoverflow.com

 

 


이중에 어떤 방법을 할까하다가

 

처음엔 당연히

this.$forceUpdate()를 쓰려고 했지만 잘안돼서.. 그치만 다시 찬찬히 읽어보니 방법이 생각났지만

여기 나온 방식중에서 가장 Best way라고 나온  key를 이용하는 방식을 써서 해결하였다.

 

그럼 forceUpdate와 Key를 이용해서 강제 rerendering하는 것을 알아보자 

(https://michaelnthiessen.com/force-re-render/ 다음을 해석하였습니다. 잘쓰여져있네여)

 


1. forceUpdate();

이것은 vue에서 공식적으로 제공해주는 방식이다. (https://vuejs.org/v2/api/#vm-forceUpdate)

 

보통은 Vue에서 data의 변화를 감지하는데 이것을 보통 reactive 하다고 말합니다. 

그런데 아시다시피 Vue의 reactivity 시스템은 모든 변화를 감지하지 못합니다..(언제그러는지는 정확하게 모르겠으나 개발하다보면 왕왕 있습니다..)

 

그래서 변화를 감지하지 못할때 강제 리렌더링을 해줍니다.

 

forceUpdate를 하기위해서는두가지 방법이 있는데 , Global하게 forceUpdate 하는 것과component 단위로 forceUpdate 하는방법

 

단순하게 생각해도 Global한 방식은 잘안쓸거 같군요..

// Globally
import Vue from 'vue';
Vue.forceUpdate();

// Using the component instance
export default {
  methods: {
    methodThatForcesUpdate() {
      // ...
      this.$forceUpdate();  // Notice we have to use a $ here
      // ...
    }
  }
}

 

 

2. key 이용

 

key를 이용 하려면 Vue에서 제공하는 key에 대해서 알아야 하는데

 

Vue에서 key는 각 특정 component에 특정 값을 매핑해 두는데 이게 key라고 볼 수 있다.

그래서, key가 동일하면 component가 변하지 않지만

key가 변하면 Vue는 예전 component 를 지우고 새로운 component를 만듭니다.

 

 

<template>
   <component-to-re-render :key="componentKey" />
</template>

<script>
 export default {
  data() {
    return {
      componentKey: 0,
    };
  },
  methods: {
    forceRerender() {
      this.componentKey += 1;  
    }
  }
 }
</script>

그래서 다음과같이 자식 component에 key를 props로 내려주고

forceRerender라는 method에 key값을 변경해주도록 해주면

 

강제 Rerendering이 필요할때 forceRerender() method를 호출해주면

Vue에서 이전 component 를 지우고 새 component를 그려줍니다.

 

저도 이방법으로 쓰레기값이 남아있는 문제를 해결하였습니다!

 

굳굳!

 

 

 

1. SQLExcpetion

JDBC는 모든 Exception을 SQLException 에 하나에 모두 담아버린다. 대부분의 SQLException은 복구가 불가능하다. DAO 밖에서 SQLException을 다룰 수 있는 가능성은 거의 없다. 따라서 필요도 없는 기계적인 throws 선언이 등장하도록 방치하지 말고 가능한 한 빨리 언체크/런타임 예외로 전환해줘야 한다.

 

2. DataAccessException

런타임 예외이고 DataAccessException 중 가장 루트 class이다.

JdbcTemplate 템플릿과 콜백 안에서 발생하는 모든 SQLException을 런타임 예외인 DataAccessException으로 포장해서 던져준다.

Spring에서 DB 관련 Exception 은 DataAccessException 으로 한번 감싸줘서 알려준다고 보면 된다.

DBMS에 따라서 에러코드가 다를텐데 그래도 코드는 스프링에서 일관적으로 보여주니 일관성이 있어서 좋을 것이다. 

 

  • dbcTemplate은 SQLException을 단지 런타임 예외인 DataAccessException으로 포장하는 것이 아니라 DB의 에러 코드를 DataAccessException 계층구조의 클래스 중 하나로 매핑해준다.JdbcTemplate에서 던지는 예외는 모두 DataAccessException의 서브클래스 타입이다.
  • 드라이버나 DB 메타정보를 참고해서 DB 종류를 확인하고 DB별로 미리 준비된 매핑정보를 참고해서 적절한 예외 클래스를 선택하기 때문에 DB가 달라져도 같은 종류의 에러라면 동일한 예외를 받을 수 있는 것이다.

 

주의할점은, 

키값 중복이 되는 같은 상황이라도 똑같은 예외 발생하지 않을 수 있다.

  • 데이터 액세스 기술에 상관없이 키 값이 중복이 되는 상황에 아래와 같이 동일한 예외가 발생하지 않는다.
  • JDBC는 DuplicateKeyException, 하이버네이트는 ContraintViolationException을 발생시킬 것이다.

 

3. 결론

결론적으로, 스프링이 잘 정립한 DataAccessException을 활용하는 것이 바람직 하지만, 특정기술에 따라 예상하지 못한 결과가 나올수 있다. 

아직 모든 예외가 명확하게 추상화되어있지 않다. 

이런 경우 로직에서 적절한 DataAccessException의 하위 클래스로 전환하는 방법 등을 사용해서 최대한 일관된 예외 전략을 가져가는 것이 좋을 것 같다.

스프링은 DataAccessException을 통해 DB에 독립적으로 적용 가능한 추상화된 런타임 예외계층을 제공한다.

DAO를 데이터 액세스 기술에서 독립시키려면 인터페이스 도입과 런타임 예외 전환, 기술에 독립적인 추상화된 예외로 전환이 필요하다.

 


참고)

https://gunju-ko.github.io/toby-spring/2018/11/07/%EC%98%88%EC%99%B8-%EC%B2%98%EB%A6%AC.html

https://velog.io/@kyle/%ED%86%A0%EB%B9%84-%EC%98%88%EC%99%B8-%EC%B2%98%EB%A6%AC

https://withseungryu.tistory.com/95

https://namocom.tistory.com/913

업무하면서 예외처리에 대해서 고민할 일이 있었다

 

정리해보자

 

보통 예외처리는 공통으로 처리하는게 많다

그 공통을 어떻게 처리할까?

 


 

1. @ExceptionHandler 

- Controller 에서 일어나는 예외를 잡아서 한번에 처리해 줌

- controller, RestController에서 사용 가능

- 리턴타입 , parameter 타입 자유 

- 아래 예시처럼 여러개 Exception class 를 여러개 나열 가능 

 

@RestController
public class TestController{

	@ExceptionHandler(NullPointerException.class)
    public Object nullex(Exception e){
    	System.err.println(e.getClass());
        return;
    }
    
     @ExceptionHandler({JdbcSQLException.class, BadSqlGrammarException.class, MyBatisSystemException.class})
    public ResponseEntity<customedException> handleJdbcSqlException(Exception exception) {
        return customedException(exception);
    }
}

 

 

2. @RestControllerAdvice

- 전역으로 예외처리 관리 해주는 어노테이션이다

@RestControllerAdvice
public class GlovalExceptionTest{
	@ExceptionHandler(NullPointer.class)
    public ResponseEntity<CustomException> handleNullpointerException(Exception e){
    	return new CustomException(e);
	}
}

 

 

- @RestControllerAdvice = @ControllerAdvice + @ResponseBody

=> 즉, @ControllerAdvice 와 같은 역할인데 @ResponseBody가 추가돼서 , 객체도 리턴이 가능한 것이다.

 예외처리 페이지로 리다이렉트만 할것이면 @ControllerAdvice만 써도되고 , 

API 서버라서 객체를 리턴해야 한다면 @ResponseBody 어노테이션이 추가된 @RestControllerAdvice 를 사용하면 된다.

 

보통은 backend와 frontend를 따로 띄워서 하는게 대부분이기 때문에 (MSA..)

@RestControllerAdvice를 쓰면 될거같다 객체로 리턴!!

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ControllerAdvice
@ResponseBody
public @interface RestControllerAdvice {
    @AliasFor(
        annotation = ControllerAdvice.class
    )
    String[] value() default {};

    @AliasFor(
        annotation = ControllerAdvice.class
    )
    String[] basePackages() default {};

    @AliasFor(
        annotation = ControllerAdvice.class
    )
    Class<?>[] basePackageClasses() default {};

    @AliasFor(
        annotation = ControllerAdvice.class
    )
    Class<?>[] assignableTypes() default {};

    @AliasFor(
        annotation = ControllerAdvice.class
    )
    Class<? extends Annotation>[] annotations() default {};
}

 

 


참고

https://jeong-pro.tistory.com/195

프로젝트를 하면서 

ddl과 insert query가 필요했다

현재 쓰는 툴로는 postgresql이 쿼리가 뽑히지 않아서

pg_dump를 이용해 쿼리를 뽑아ㅏㅆ다.

 

내가사용한건 다음과 같이 두 명령어

 

 

-- ddl

./pg_dump -Cs apim -n schemaname e -n pubic -E utf-8 > ddl_schema.sql

 

-- insert query

./pg_dump -a -O --inserts -E UTF8 dbname > dump.sql

 

 

참고)

schema 단위 백업

./pg_dump -Cs dbname -n schemaname -n schemaname2 -E utf-8 > ddl_schema.sql

./pg_dump -a dbname -n schemaname -n schemaname2 -E utf-8 > data_schema.sql

 

table 단위 백업

./pg_dump -Cs dbname -t schema.table -E utf-8 > ddl_table.sql

./pg_dump -a dbname -t schema.table -E utf-8 > data_table.sql

 


참고

 https://stricky.tistory.com/169

 

[gpdb 백업] pg_dump & pg_restore 간단 사용법

[gpdb 백업] pg_dump & pg_restore 간단 사용법 안녕하세요. GPDB 에서 PG_DUMP 와 PG_RESTORE를 이용한 백업과 복구에 관해서 간략하게 안내해 드릴께요. pg_dump & pg_restore는 sql 기반으로 gpdb안의 데이터..

stricky.tistory.com

       

'DEVELOP > DB' 카테고리의 다른 글

[DB] postgreSql Centos7에 설치 및 설정하기  (0) 2020.08.24
[DB]Isolation Level 알아보기  (0) 2020.07.19
[mySql] 계정 생성 및 권한 설정  (0) 2020.05.25
[MariaDB] general log 설정하기  (0) 2020.05.25
DB (mysql) 설정 변경  (0) 2020.03.13

+ Recent posts