본문 바로가기
LLM

[LLM][RAG][AutoRAG]AutoRAG

by Chandler.j 2024. 7. 5.
반응형

AutoRAG

1. 개념

AutoRAG(Automatic Retrieval-Augmented Generation)은 문서 검색과 질문 응답(QA) 시스템을 구축하는 데 도움을 주는 프레임워크임. 기본적으로 대량의 문서나 데이터를 기반으로 필요한 정보를 검색하고, 이를 기반으로 응답을 생성하는 시스템을 구축할 수 있음. 특히, 대규모 언어 모델을 활용하여 정확하고 효율적인 QA 시스템을 만들 수 있음.

AutoRAG docs documentation


2. 준비물

AutoRAG를 사용하기 위해서는 다음과 같은 준비물이 필요함:

  • Python 환경: Python 3.6 이상
  • 필수 라이브러리: transformers, torch, pandas 등
  • 데이터셋: QA 데이터셋과 검색할 문서 데이터셋

3. 코드

!pip install transformers torch pandas AutoRAG
import pandas as pd

# QA 데이터 로드
qa_data_path = './data/qa.parquet'
qa_data = pd.read_parquet(qa_data_path)

# 문서 데이터 로드
corpus_data_path = './data/corpus.parquet'
corpus_data = pd.read_parquet(corpus_data_path)

from autorag.evaluator import Evaluator

# 평가 인스턴스 생성
evaluator = Evaluator(qa_data_path=qa_data_path, corpus_data_path=corpus_data_path)

# 설정 파일을 기반으로 평가 시작
evaluator.start_trial('config.yaml')
  • config.yaml
node_lines:
- node_line_name: pre_retrieve_node_line  # Arbitrary node line name
  nodes:
    - node_type: query_expansion
      strategy:
        metrics: [retrieval_f1, retrieval_recall, retrieval_precision]
        speed_threshold: 10
        top_k: 10
        retrieval_modules:
          - module_type: bm25
          - module_type: vectordb
            embedding_model: openai
      modules:
        - module_type: pass_query_expansion
        - module_type: query_decompose
          llm: openai
          temperature: [0.2, 0.5, 1.0]
        - module_type: hyde
          llm: openai
          max_token: 64
- node_line_name: retrieve_node_line
  nodes:
    - node_type: retrieval
      strategy:
        metrics: [retrieval_f1, retrieval_recall, retrieval_precision]
      top_k: [3, 5, 10]
      modules:
        - module_type: vectordb
          embedding_model: openai
        - module_type: bm25
          bm25_tokenizer: [ porter_stemmer, ko_kiwi, space, gpt2]
        - module_type: hybrid_rrf
          target_modules: ('bm25', 'vectordb')
          rrf_k: [3, 5, 10, 20, 30]
        - module_type: hybrid_cc
          target_modules: ('bm25', 'vectordb')
          weights:
              - (0.5, 0.5)
              - (0.3, 0.7)
              - (0.7, 0.3)
        - module_type: hybrid_rsf
          target_modules: ('bm25', 'vectordb')
          weights:
              - (0.5, 0.5)
              - (0.3, 0.7)
              - (0.7, 0.3)          
        - module_type: hybrid_dbsf
          target_modules: ('bm25', 'vectordb')
          weights:
            - (0.5, 0.5)
            - (0.3, 0.7)
            - (0.7, 0.3)       
- node_line_name: post_retrieve_node_line
  nodes:
    - node_type: prompt_maker
      strategy:
        metrics:
          - metric_name: meteor
          - metric_name: rouge
          - metric_name: sem_score
            embedding_model: openai
      modules:
        - module_type: fstring
          prompt: [ "Tell me something about the question: {query} \\n\\n {retrieved_contents}","Question: {query} \\n Something to read: {retrieved_contents} \\n What is your answer?" ]
        - module_type: long_context_reorder
          prompt: [ "Tell me something about the question: {query} \\n\\n {retrieved_contents}","Question: {query} \\n Something to read: {retrieved_contents} \\n What's your answer?" ]
    - node_type: generator
      strategy:
        metrics:
      #     - metric_name: meteor
      #     - metric_name: rouge
      #     - metric_name: sem_score
      #       embedding_model: openai
      # modules:
      #   - module_type: llama_index_llm
      #     llm: openai
      #     model: [gpt-3.5-turbo-16k, gpt-4-turbo, gpt-4o]
      #     batch: 2
      #   - module_type: vllm
      #     llm: mistralai/Mistral-7B-Instruct-v0.2
      #     temperature: [ 0.1, 1.0 ]
           - metric_name: bleu
           - metric_name: meteor
           - metric_name: sem_score
             embedding_model: openai
           - metric_name: g_eval
             metrics: [consistency, fluency, relevance, coherence]
             model: gpt-4
           - metric_name: bert_score
             lang: en
             batch: 64
        speed_threshold: 20
        token_threshold: 8000
      modules:
        - module_type: llama_index_llm
          llm: [openai]
          model: [gpt-3.5-turbo, gpt-4, gpt-4o]
          temperature: [0.0, 0.5, 1.0, 1.5]

TOP

Designed by 티스토리