새소식

반응형
IT Trends

LINER PDF Chat Tutorial (3) Pinecone + Mixtral (HuggigFace)

  • -
728x90
반응형

 

 

전체 튜토리얼은 아래 글을 참고하기

2024.03.06 - [NLP/tutorials] - LINER PDF Chat Tutorial

 

LINER PDF Chat Tutorial

https://github.com/liner-engineering/liner-pdf-chat-tutorial/tree/main GitHub - liner-engineering/liner-pdf-chat-tutorial: LINER PDF Chat Tutorial with ChatGPT & Pinecone LINER PDF Chat Tutorial with ChatGPT & Pinecone. Contribute to liner-engineering/line

pinopino.tistory.com

 

 

Mixtral 사용해서 PDFChat 만들기

from langchain import HuggingFaceHub
from langchain_community.chat_models.huggingface import ChatHuggingFace

 

1) 사용자 쿼리 벡터화하고 Hybrid Search로 레퍼런스 문서 찾기

# 1. 사용자 쿼리 벡터화
query_vector = query_embed(query)

# 2. Hybrid Search 통해 레퍼런스 문서 반환
query_response = index.query(
    namespace="test--virus",
    top_k=10,
    include_values=True,
    include_metadata=True,
    vector=query_vector,
    filter={
        "data": {"$in": [DATA_NAME]},
    },
)

reference = query_response["matches"][0]["metadata"]

 

 

 

2) Mixtral 모델 로드

# 3. 프롬프트에 레퍼런스 문서 정보와 사용자 쿼리 정보 입력
llm = HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
                     model_kwargs={'temperature': 0.6, 'max_length': 100})
chat_model = ChatHuggingFace(llm=llm)

 

** 원하는 허깅페이스 repo를 찾으면 된다.

 

 

3) 프롬프트 작성

TemplateError: Conversation roles must alternate user/assistant/user/assistant/...

 

Mixtral의 경우 User/ Assistant / User/ Assistant / ... 순으로 주어져야 하므로 기존 튜토리얼의 방식대로 하면 오류가 발생한다. 따라서 아래의 프롬프트 템플릿을 만들고 전달해줘야 한다.

 

prompt = f"""
        Context information is below.
        ---------------------
        title : {reference['title']}
        {reference['text']}
        ---------------------
        Your role is to answer the Query based on the context information provided. 
        You must base your answer solely on the context information and regardless of your own knowledge, and you must include the title information in your answer.
        Answer the Query.
        Query: {query}
        Answer:
        """

 

 

4) 답변 생성하기

# 4. LLM 생성 답변 반환
answer = chat_model.invoke(prompt)
return answer.content

 

 

 

 

 

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.