이번 게시물에서는 Llama2 모델을 사용하여 Excel 데이터를 지능적으로 쿼리하는 챗봇을 구축한 방법을 설명하겠습니다.
우리가 만들고 있는 것
- 엑셀 파일을 불러옵니다.
- 데이터를 관리 가능한 단위로 나눕니다.
- 빠른 검색을 위해 데이터를 벡터 데이터베이스에 저장합니다.
- 로컬 Llama2 모델을 사용하여 다음을 기반으로 질문에 답하세요. 엑셀 파일의 내용입니다.
전제 조건:
파이썬(≥ 3.8)
라이브러리: langchain, pandas, unstructured, Chroma
1단계: 종속성 설치
%pip install -q unstructured langchain %pip install -q "unstructured[all-docs]"
2단계: Excel 파일 로드
import pandas as pd excel_path = "Book2.xlsx" if excel_path: df = pd.read_excel(excel_path) data = df.to_string(index=False) else: print("Upload an Excel file")
3단계: 데이터를 청크하여 벡터 데이터베이스에 저장
대용량 텍스트 데이터는 효과적인 삽입 및 쿼리를 위해 더 작고 겹치는 덩어리로 분할됩니다. 이러한 청크는 Chroma 벡터 데이터베이스에 저장됩니다.
from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_community.embeddings import OllamaEmbeddings from langchain_community.vectorstores import Chroma text_splitter = RecursiveCharacterTextSplitter(chunk_size=7500, chunk_overlap=100) chunks = text_splitter.split_text(data) embedding_model = OllamaEmbeddings(model="nomic-embed-text", show_progress=False) vector_db = Chroma.from_texts( texts=chunks, embedding=embedding_model, collection_name="local-rag" )
4단계: Llama2 모델 초기화
ChatOllama를 사용하여 Llama2 모델을 로컬로 로드합니다.
from langchain_community.chat_models import ChatOllama local_model = "llama2" llm = ChatOllama(model=local_model)
5단계: 쿼리 프롬프트 생성
챗봇은 Excel 파일의 특정 열 이름을 기반으로 응답합니다. 모델을 안내하는 프롬프트 템플릿을 만듭니다
from langchain.prompts import PromptTemplate QUERY_PROMPT = PromptTemplate( input_variables=["question"], template="""You are an AI assistant. Answer the user's questions based on the column names: Id, order_id, name, sales, refund, and status. Original question: {question}""" )
6단계: 검색기 설정
Llama2 모델이 질문에 답하는 데 사용할 벡터 데이터베이스에서 관련 청크를 가져오도록 검색기를 구성합니다.
from langchain.retrievers.multi_query import MultiQueryRetriever retriever = MultiQueryRetriever.from_llm( vector_db.as_retriever(), llm, prompt=QUERY_PROMPT )
7단계: 대응 체인 구축
응답 체인은 다음을 통합합니다.
- 컨텍스트를 가져오는 검색기
- 질문과 맥락의 형식을 지정하는 프롬프트
- 답변을 생성하는 Llama2 모델.
- 응답 형식을 지정하는 출력 파서.
from langchain.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough from langchain_core.output_parsers import StrOutputParser template = """Answer the question based ONLY on the following context: {context} Question: {question} """ prompt = ChatPromptTemplate.from_template(template) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() )
8단계: 질문하기
이제 질문할 준비가 되었습니다! 응답을 얻기 위해 체인을 호출하는 방법은 다음과 같습니다.
raw_result = chain.invoke("How many rows are there?") final_result = f"{raw_result}\n\nIf you have more questions, feel free to ask!" print(final_result)
샘플 출력
샘플 Excel 파일에서 위 코드를 실행했을 때 얻은 결과는 다음과 같습니다.
Based on the provided context, there are 10 rows in the table. If you have more questions, feel free to ask!
결론:
이 접근 방식은 임베딩과 Llama2 모델의 강력한 기능을 활용하여 Excel 데이터용 스마트 대화형 챗봇을 만듭니다. 약간의 조정을 통해 이를 확장하여 다른 유형의 문서와 함께 작동하거나 완전한 앱에 통합할 수 있습니다!
내 LinkedIn에서 UI 작업 예제를 확인하세요.
BChat Excel 소개: Excel 파일 상호 작용을 위한 대화형 AI 기반 도구
위 내용은 Excel을 사용하여 LlamaChat으로 간단한 챗봇 구축]의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

forhandlinglargedatasetsinpython, usenumpyarraysforbetterperformance.1) numpyarraysarememory-effic andfasterfornumericaloperations.2) leveragevectorization foredtimecomplexity.4) managemoryusage withorfications data

inpython, listsusedyammoryAllocation과 함께 할당하고, whilempyarraysallocatefixedMemory.1) listsAllocatemememorythanneedInitiality.

Inpython, youcansspecthedatatypeyfelemeremodelerernspant.1) usenpynernrump.1) usenpynerp.dloatp.ploatm64, 포모 선례 전분자.

numpyissentialfornumericalcomputinginpythonduetoitsspeed, memory-efficiency 및 comperniveMathematicaticaltions

contiguousUousUousUlorAllocationScrucialForraysbecauseItAllowsOfficationAndFastElementAccess.1) ItenableSconstantTimeAccess, o (1), DuetodirectAddressCalculation.2) Itimprovesceeffiency theMultipleementFetchespercacheline.3) Itsimplififiesmomorym

slicepaythonlistisdoneusingthesyntaxlist [start : step : step] .here'showitworks : 1) startistheindexofthefirstelementtoinclude.2) stopistheindexofthefirstelemement.3) stepisincrementbetwetweentractionsoftortionsoflists

NumpyAllowsForVariousOperationsOnArrays : 1) BasicArithmeticLikeadDition, Subtraction, A 및 Division; 2) AdvancedOperationsSuchasmatrixmultiplication; 3) extrayintondsfordatamanipulation; 5) Ag

Arraysinpython, 특히 Stroughnumpyandpandas, areestentialfordataanalysis, setingspeedandefficiency


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

Dreamweaver Mac版
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)
