반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

Chapter 8. Agents With Long-Term Memory

2023. 11. 19. 02:21 | Posted by 솔웅


반응형

https://www.pinecone.io/learn/series/langchain/langchain-tools/

 

Building Custom Tools for LLM Agents | Pinecone

© Pinecone Systems, Inc. | San Francisco, CA Pinecone is a registered trademark of Pinecone Systems, Inc.

www.pinecone.io

 

Retrieval Agents

 

We've seen in previous chapters how powerful retrieval augmentation and conversational agents can be. They become even more impressive when we begin using them together.

 

우리는 이전 장에서 검색 확대 및 대화 에이전트가 얼마나 강력한지 살펴보았습니다. 함께 사용하기 시작하면 더욱 인상적이 됩니다.

 

Conversational agents can struggle with data freshness, knowledge about specific domains, or accessing internal documentation. By coupling agents with retrieval augmentation tools we no longer have these problems.

 

대화형 에이전트는 데이터 최신성, 특정 도메인에 대한 지식 또는 내부 문서에 액세스하는 데 어려움을 겪을 수 있습니다. 에이전트를 검색 확대 도구와 결합 retrieval augmentation tools 하면 더 이상 이러한 문제가 발생하지 않습니다.

 

One the other side, using "naive" retrieval augmentation without the use of an agent means we will retrieve contexts with every query. Again, this isn't always ideal as not every query requires access to external knowledge.

 

다른 한편으로는 에이전트를 사용하지 않고 "순진한" 검색 확대를 사용한다는 것은 모든 쿼리에서 컨텍스트를 검색한다는 의미입니다. 다시 말하지만, 모든 쿼리에 외부 지식에 대한 액세스가 필요한 것은 아니기 때문에 이것이 항상 이상적인 것은 아닙니다.

 

Merging these methods gives us the best of both worlds. In this notebook we'll learn how to do this.

 

이러한 방법을 병합하면 두 가지 장점을 모두 얻을 수 있습니다. 이 노트에서 우리는 이를 수행하는 방법을 배울 것입니다.

 

To begin, we must install the prerequisite libraries that we will be using in this notebook.

 

시작하려면 이 노트북에서 사용할 필수 구성 요소 라이브러리를 설치해야 합니다.

 

!pip install -qU \
    openai==0.27.7 \
    "pinecone-client[grpc]"==2.2.1 \
    langchain==0.0.162 \
    tiktoken==0.4.0 \
    datasets==2.12.0

 

 

패키지 설치: 코드의 첫 부분에서는 필요한 Python 패키지를 설치합니다. !pip install 명령어를 사용하여 다음 패키지를 설치합니다.

  • openai: OpenAI의 라이브러리로, GPT 모델을 사용할 수 있게 해줍니다.
  • pinecone-client[grpc]: Pinecone 서비스에 연결하기 위한 클라이언트 라이브러리입니다. grpc 옵션은 gRPC 통신을 지원하기 위한 것입니다.
  • langchain: Langchain 라이브러리로, 다양한 언어 모델과 체인을 관리하는 도구를 제공합니다.
  • tiktoken: 텍스트의 토큰 수를 셀 수 있는 라이브러리입니다.
  • datasets: Hugging Face의 datasets 라이브러리로, 다양한 데이터셋을 쉽게 사용할 수 있게 해줍니다.
  •  

Building the Knowledge Base

We start by constructing our knowledge base. We'll use a mostly prepared dataset called Stanford Question-Answering Dataset (SQuAD) hosted on Hugging Face Datasets. We download it like so:

 

우리는 지식 기반을 구축하는 것부터 시작합니다. Hugging Face Datasets에서 호스팅되는 SQuAD(Stanford Question-Answering Dataset)라는 대부분 준비된 데이터 세트를 사용합니다. 다음과 같이 다운로드합니다.

 

from datasets import load_dataset

data = load_dataset('squad', split='train')
data

 

 

 

  1. 데이터셋 로딩: datasets 라이브러리를 사용하여 'squad' 데이터셋의 'train' 스플릿을 로드합니다. 'SQuAD'는 Stanford Question Answering Dataset으로, 질문에 대한 답변을 찾는 데 사용되는 기계 학습 데이터셋 중 하나입니다.
  2. 데이터셋 출력: 로드한 데이터셋은 data 변수에 저장되며, 이를 출력하여 데이터셋의 내용을 확인합니다.

 

The dataset does contain duplicate contexts, which we can remove like so:

 

데이터세트에는 중복된 컨텍스트가 포함되어 있으며 다음과 같이 제거할 수 있습니다.

 

data = data.to_pandas()
data.head()

 

 

이 코드는 Hugging Face의 datasets 라이브러리로 로드한 'SQuAD' 데이터셋을 Pandas 데이터프레임으로 변환하고, 그 데이터프레임의 처음 몇 개 행을 출력하는 작업을 수행합니다.

  1. data.to_pandas(): 'SQuAD' 데이터셋을 Pandas 데이터프레임으로 변환합니다. to_pandas() 메서드를 사용하여 데이터셋을 Pandas의 DataFrame 형식으로 가져옵니다.
  2. data.head(): 변환된 Pandas 데이터프레임에서 처음 몇 개의 행을 출력합니다. head() 메서드는 데이터프레임의 상단 부분을 보여주는 함수로, 괄호 안에 숫자를 지정하여 원하는 개수만큼의 행을 출력할 수 있습니다. 여기서는 기본값인 5를 사용하여 처음 5개의 행을 출력합니다.

따라서 코드의 결과는 'SQuAD' 데이터셋의 처음 5개의 행이 Pandas 데이터프레임 형식으로 출력되게 됩니다. 데이터프레임은 표 형식으로 데이터를 보여주며, 각 행은 데이터셋의 각 예시(질문과 답변)에 해당합니다.

 

data.drop_duplicates(subset='context', keep='first', inplace=True)
data.head()

 

 

이 코드는 Pandas 데이터프레임에서 중복된 행을 제거하는 작업을 수행합니다. 아래는 코드의 각 부분에 대한 설명입니다:

  1. data.drop_duplicates(subset='context', keep='first', inplace=True): drop_duplicates 메서드를 사용하여 중복된 값을 제거합니다. 여기서 사용된 매개변수는 다음과 같습니다:
    • subset='context': 'context' 열을 기준으로 중복을 검사합니다.
    • keep='first': 중복된 값 중 첫 번째 값을 유지하고 나머지는 제거합니다.
    • inplace=True: 원본 데이터프레임을 수정하도록 지정합니다. 이 경우, 새로운 데이터프레임을 반환하는 것이 아니라 기존 데이터프레임을 변경합니다.
  2. data.head(): 중복이 제거된 데이터프레임의 처음 몇 개 행을 출력합니다. 이는 데이터프레임이 변경되었음을 확인하기 위한 용도로 사용됩니다.

따라서 코드는 'context' 열을 기준으로 중복된 행을 제거하고, 그 결과로 얻은 데이터프레임의 처음 5개 행을 출력합니다.

 

Initialize the Embedding Model and Vector DB

 

We'll be using OpenAI's text-embedding-ada-002 model initialize via LangChain and the Pinecone vector DB. We start by initializing the embedding model, for this we need an OpenAI API key.

 

우리는 LangChain과 Pinecone 벡터 DB를 통해 OpenAI의 text-embedding-ada-002 모델 초기화를 사용할 것입니다. 임베딩 모델을 초기화하는 것부터 시작합니다. 이를 위해서는 OpenAI API 키가 필요합니다.

 

(Note that OpenAI is a paid service and so running the remainder of this notebook may incur some small cost)

 

(OpenAI는 유료 서비스이므로 이 노트북의 나머지 부분을 실행하는 데 약간의 비용이 발생할 수 있습니다.)

 

 

from getpass import getpass
from langchain.embeddings.openai import OpenAIEmbeddings

OPENAI_API_KEY = getpass("OpenAI API Key: ")  # platform.openai.com
model_name = 'text-embedding-ada-002'

embed = OpenAIEmbeddings(
    model=model_name,
    openai_api_key=OPENAI_API_KEY
)

 

이 부분은 로컬에서 실행할 때 약간 바꾸었습니다.

우선 opanai_api_key는 외부 파일인 ../openaiapikey.txt 에서 가져오는 것으로 했습니다.

그리고 오픈AI의 임베딩 모델인 2023년 11월 18일 현재 최신 모델인 ada v2로 설정 했습니다.

자세한 내용은 아래와 같습니다.

 

 

Next we initialize the vector database. For this we need a free API key, then we create the index:

 

다음으로 벡터 데이터베이스를 초기화합니다. 이를 위해 무료 API 키가 필요하며 인덱스를 생성합니다.

 

import pinecone

# find API key in console at app.pinecone.io
YOUR_API_KEY = getpass("Pinecone API Key: ")
# find ENV (cloud region) next to API key in console
YOUR_ENV = input("Pinecone environment: ")

index_name = 'langchain-retrieval-agent'
pinecone.init(
    api_key=YOUR_API_KEY,
    environment=YOUR_ENV
)

if index_name not in pinecone.list_indexes():
    # we create a new index
    pinecone.create_index(
        name=index_name,
        metric='dotproduct',
        dimension=1536  # 1536 dim of text-embedding-ada-002
    )

 

이 코드는 Pinecone을 사용하여 검색 인덱스를 설정하는 작업을 수행합니다. 아래는 코드의 각 부분에 대한 설명입니다:

  1. import pinecone: Pinecone 클라이언트 라이브러리를 가져옵니다.
  2. YOUR_API_KEY = getpass("Pinecone API Key: "): 사용자로부터 Pinecone API 키를 입력받습니다. getpass 함수를 사용하여 입력 내용이 터미널에 표시되지 않도록 보안을 유지합니다.
  3. YOUR_ENV = input("Pinecone environment: "): 사용자로부터 Pinecone 환경을 입력받습니다. 이는 Pinecone이 호스팅되는 클라우드 환경(예: us-west1, us-central1 등)을 나타냅니다.
  4. index_name = 'langchain-retrieval-agent': 검색 인덱스의 이름을 설정합니다.
  5. pinecone.init(api_key=YOUR_API_KEY, environment=YOUR_ENV): Pinecone 클라이언트를 초기화합니다. 입력받은 API 키와 환경을 사용하여 Pinecone 서버에 연결합니다.
  6. if index_name not in pinecone.list_indexes():: 지정된 이름의 검색 인덱스가 Pinecone 서버에 존재하지 않으면 아래의 코드 블록을 실행합니다.
  7. pinecone.create_index(name=index_name, metric='dotproduct', dimension=1536): 새로운 검색 인덱스를 생성합니다. 여기서 사용된 매개변수는 다음과 같습니다:
    • name=index_name: 생성할 인덱스의 이름을 지정합니다.
    • metric='dotproduct': 유사도를 측정하는 데 사용할 메트릭을 선택합니다. 여기서는 'dotproduct'를 사용합니다.
    • dimension=1536: 인덱스의 차원을 설정합니다. 이 값은 text-embedding-ada-002 모델의 차원과 일치해야 합니다.

따라서 코드는 Pinecone 서버에 연결하고, 지정된 이름의 검색 인덱스가 없으면 새로운 인덱스를 생성합니다.

 

여기에서는 로컬에서는 에러가 나네요. Pinecone 무료버전이라서 그런지..... 

그래서 아래 부터는 로컬에서 실행하지 않고 교재만 보고 진행 합니다.

 

 

Then connect to the index:  그런 다음 인덱스에 연결합니다.

 

index = pinecone.GRPCIndex(index_name)
index.describe_index_stats()
{'dimension': 1536,
 'index_fullness': 0.0,
 'namespaces': {},
 'total_vector_count': 0}

 

이 코드는 Pinecone에서 만든 검색 인덱스에 대한 통계 정보를 가져오는 작업을 수행합니다. 아래는 코드의 각 부분에 대한 설명입니다:

  1. index = pinecone.GRPCIndex(index_name): 지정된 이름의 Pinecone 검색 인덱스에 대한 GRPC 인덱스 객체를 생성합니다. 이를 통해 해당 인덱스와 상호작용할 수 있습니다.
  2. index.describe_index_stats(): 생성한 인덱스의 통계 정보를 가져오는 메서드를 호출합니다. 이 메서드는 해당 인덱스에 저장된 데이터의 특성 및 성능과 관련된 다양한 통계를 제공합니다.

따라서 이 코드는 Pinecone에서 생성한 검색 인덱스에 대한 통계 정보를 출력합니다. 결과는 해당 인덱스에 대한 특정 지표 및 데이터 통계를 나타낼 것입니다.

 

We should see that the new Pinecone index has a total_vector_count of 0, as we haven't added any vectors yet.

 

아직 벡터를 추가하지 않았으므로 새 Pinecone 인덱스의 total_Vector_count가 0임을 확인해야 합니다.

 

Indexing

 

We can perform the indexing task using the LangChain vector store object. But for now it is much faster to do it via the Pinecone python client directly. We will do this in batches of 100 or more.

 

LangChain 벡터 저장소 개체를 사용하여 색인 작업을 수행할 수 있습니다. 하지만 지금은 Pinecone Python 클라이언트를 통해 직접 수행하는 것이 훨씬 더 빠릅니다. 우리는 이것을 100개 이상의 배치로 수행할 것입니다.

 

from tqdm.auto import tqdm
from uuid import uuid4

batch_size = 100

texts = []
metadatas = []

for i in tqdm(range(0, len(data), batch_size)):
    # get end of batch
    i_end = min(len(data), i+batch_size)
    batch = data.iloc[i:i_end]
    # first get metadata fields for this record
    metadatas = [{
        'title': record['title'],
        'text': record['context']
    } for j, record in batch.iterrows()]
    # get the list of contexts / documents
    documents = batch['context']
    # create document embeddings
    embeds = embed.embed_documents(documents)
    # get IDs
    ids = batch['id']
    # add everything to pinecone
    index.upsert(vectors=zip(ids, embeds, metadatas))

이 코드는 Pinecone에 대량의 텍스트 데이터를 업로드하여 검색 가능한 인덱스를 만드는 작업을 수행합니다. 아래는 코드의 각 부분에 대한 설명입니다:

  1. from tqdm.auto import tqdm: tqdm은 Python에서 진행 상황을 시각적으로 나타내주는 라이브러리입니다. tqdm.auto 모듈을 가져옵니다.
  2. from uuid import uuid4: UUID(Universally Unique Identifier)를 생성하기 위해 uuid4 함수를 가져옵니다.
  3. batch_size = 100: 데이터를 처리하는 데 사용할 배치 크기를 설정합니다. 한 번에 처리할 데이터의 양을 결정합니다.
  4. texts = []: 문서 텍스트를 저장할 빈 리스트를 생성합니다.
  5. metadatas = []: 문서에 대한 메타데이터를 저장할 빈 리스트를 생성합니다.
  6. for i in tqdm(range(0, len(data), batch_size)):: 데이터를 배치 단위로 처리하는 반복문을 시작합니다. tqdm을 사용하여 진행 상황을 시각적으로 표시합니다.
  7. i_end = min(len(data), i+batch_size): 현재 배치의 끝 인덱스를 계산합니다. 이는 데이터의 길이와 현재 인덱스에 배치 크기를 더한 값 중 작은 값을 사용합니다.
  8. batch = data.iloc[i:i_end]: 현재 배치를 데이터프레임에서 추출합니다.
  9. metadatas = [...]: 현재 배치에 대한 메타데이터를 생성합니다. 각 문서에 대한 'title'과 'text'를 메타데이터로 저장합니다.
  10. documents = batch['context']: 현재 배치의 문서 텍스트를 추출합니다.
  11. embeds = embed.embed_documents(documents): 문서 텍스트를 임베딩하여 벡터로 변환합니다. 이는 Pinecone에 업로드할 수 있는 형식으로 텍스트를 표현합니다.
  12. ids = batch['id']: 각 문서에 대한 고유 식별자(ID)를 추출합니다.
  13. index.upsert(vectors=zip(ids, embeds, metadatas)): Pinecone 인덱스에 업로드할 데이터를 생성하여 업로드합니다. upsert 메서드를 사용하여 벡터(임베딩), 문서 ID, 및 메타데이터를 함께 업로드합니다.

이렇게 하면 코드는 데이터를 배치 단위로 처리하면서 Pinecone 인덱스에 텍스트 데이터를 업로드합니다.

 

 

We've indexed everything, now we can check the number of vectors in our index like so:

 

모든 것을 인덱싱했으므로 이제 다음과 같이 인덱스의 벡터 수를 확인할 수 있습니다.

 

index.describe_index_stats()
{'dimension': 1536,
 'index_fullness': 0.0,
 'namespaces': {'': {'vector_count': 18891}},
 'total_vector_count': 18891}

 

Creating a Vector Store and Querying

Now that we've build our index we can switch back over to LangChain. We start by initializing a vector store using the same index we just built. We do that like so:

 

이제 인덱스를 구축했으므로 LangChain으로 다시 전환할 수 있습니다. 방금 구축한 것과 동일한 인덱스를 사용하여 벡터 저장소를 초기화하는 것부터 시작합니다. 우리는 그렇게 합니다:

 

from langchain.vectorstores import Pinecone

text_field = "text"

# switch back to normal index for langchain
index = pinecone.Index(index_name)

vectorstore = Pinecone(
    index, embed.embed_query, text_field
)

 

이 코드는 Pinecone 벡터 스토어를 설정하고 초기화하는 부분으로 보입니다. 아래는 코드의 각 부분에 대한 설명입니다:

  1. from langchain.vectorstores import Pinecone: langchain 라이브러리에서 Pinecone 벡터 스토어를 가져옵니다.
  2. text_field = "text": 벡터 스토어에서 사용할 텍스트 필드의 이름을 설정합니다. 여기서는 "text"로 설정되어 있습니다.
  3. index = pinecone.Index(index_name): Pinecone에 저장된 인덱스를 가져옵니다. 이전에 만들어진 index_name을 사용하여 해당 인덱스를 가져옵니다.
  4. vectorstore = Pinecone(index, embed.embed_query, text_field): Pinecone 클래스를 사용하여 벡터 스토어를 초기화합니다. 이때, 기존에 생성한 Pinecone 인덱스(index), 쿼리를 임베딩하는 데 사용할 함수(embed.embed_query), 그리고 텍스트 필드(text_field)를 지정합니다.

이렇게 설정된 vectorstore는 Pinecone 인덱스에 저장된 벡터를 사용하여 쿼리를 임베딩하고, 텍스트 필드를 기반으로 검색을 수행할 수 있는 객체가 됩니다. 이를 통해 특정 텍스트에 대한 검색 및 유사성 분석을 수행할 수 있습니다.

 

As in previous examples, we can use the similarity_search method to do a pure semantic search (without the generation component).

 

이전 예제와 마찬가지로, 유사성_검색 메소드를 사용하여 (생성 구성요소 없이) 순수한 의미 검색을 수행할 수 있습니다.

 

query = "when was the college of engineering in the University of Notre Dame established?"

vectorstore.similarity_search(
    query,  # our search query
    k=3  # return 3 most relevant docs
)
[Document(page_content="In 1919 Father James Burns became president of Notre Dame, and in three years he produced an academic revolution that brought the school up to national standards by adopting the elective system and moving away from the university's traditional scholastic and classical emphasis. By contrast, the Jesuit colleges, bastions of academic conservatism, were reluctant to move to a system of electives. Their graduates were shut out of Harvard Law School for that reason. Notre Dame continued to grow over the years, adding more colleges, programs, and sports teams. By 1921, with the addition of the College of Commerce, Notre Dame had grown from a small college to a university with five colleges and a professional law school. The university continued to expand and add new residence halls and buildings with each subsequent president.", metadata={'title': 'University_of_Notre_Dame'}),
 Document(page_content='The College of Engineering was established in 1920, however, early courses in civil and mechanical engineering were a part of the College of Science since the 1870s. Today the college, housed in the Fitzpatrick, Cushing, and Stinson-Remick Halls of Engineering, includes five departments of study – aerospace and mechanical engineering, chemical and biomolecular engineering, civil engineering and geological sciences, computer science and engineering, and electrical engineering – with eight B.S. degrees offered. Additionally, the college offers five-year dual degree programs with the Colleges of Arts and Letters and of Business awarding additional B.A. and Master of Business Administration (MBA) degrees, respectively.', metadata={'title': 'University_of_Notre_Dame'}),
 Document(page_content='Since 2005, Notre Dame has been led by John I. Jenkins, C.S.C., the 17th president of the university. Jenkins took over the position from Malloy on July 1, 2005. In his inaugural address, Jenkins described his goals of making the university a leader in research that recognizes ethics and building the connection between faith and studies. During his tenure, Notre Dame has increased its endowment, enlarged its student body, and undergone many construction projects on campus, including Compton Family Ice Arena, a new architecture hall, additional residence halls, and the Campus Crossroads, a $400m enhancement and expansion of Notre Dame Stadium.', metadata={'title': 'University_of_Notre_Dame'})]

이 코드는 Pinecone 벡터 스토어를 사용하여 특정 쿼리에 대한 유사도 검색을 수행하는 부분입니다. 아래는 코드의 각 부분에 대한 설명입니다:

  1. query = "when was the college of engineering in the University of Notre Dame established?": 검색하고자 하는 특정 질문이나 문장을 query 변수에 할당합니다.
  2. vectorstore.similarity_search(query, k=3): vectorstore 객체의 similarity_search 메서드를 호출하여 유사도 검색을 수행합니다. 이때, 첫 번째 인자로는 검색할 쿼리(query)를 전달하고, k 매개변수에는 반환할 가장 유사한 문서의 개수를 지정합니다.

결과적으로 이 코드는 주어진 쿼리에 대해 Pinecone 벡터 스토어에서 유사한 문서를 검색하고, 가장 유사한 문서 3개를 반환합니다. 반환된 문서들은 검색 쿼리와의 유사도에 따라 순위가 매겨져 있습니다.

 
 

Looks like we're getting good results. Let's take a look at how we can begin integrating this into a conversational agent.

좋은 결과를 얻고 있는 것 같습니다. 이것을 대화형 에이전트에 통합하는 방법을 살펴보겠습니다.

 

Initializing the Conversational Agent

 

Our conversational agent needs a Chat LLM, conversational memory, and a RetrievalQA chain to initialize. We create these using:

 

대화 에이전트를 초기화하려면 Chat LLM, 대화 메모리 및 RetrievalQA 체인이 필요합니다. 다음을 사용하여 이를 만듭니다.

 

from langchain.chat_models import ChatOpenAI
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.chains import RetrievalQA

# chat completion llm
llm = ChatOpenAI(
    openai_api_key=OPENAI_API_KEY,
    model_name='gpt-3.5-turbo',
    temperature=0.0
)
# conversational memory
conversational_memory = ConversationBufferWindowMemory(
    memory_key='chat_history',
    k=5,
    return_messages=True
)
# retrieval qa chain
qa = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=vectorstore.as_retriever()
)

 

이 코드는 다양한 기능을 수행할 수 있는 ChatOpenAI 대화 모델과 대화 기록을 저장하는 메모리인 ConversationBufferWindowMemory, 그리고 RetrievalQA 체인을 초기화하는 부분입니다. 아래는 코드의 각 부분에 대한 설명입니다:

  1. from langchain.chat_models import ChatOpenAI: langchain 라이브러리에서 ChatOpenAI 대화 모델을 가져옵니다. 이 모델은 대화 기능을 수행할 수 있는 GPT-3.5-turbo 모델을 사용합니다.
  2. from langchain.chains.conversation.memory import ConversationBufferWindowMemory: 대화 기록을 저장하는 메모리 클래스인 ConversationBufferWindowMemory를 가져옵니다. 이 메모리는 대화 기록을 유지하면서 필요한 메모리 윈도우를 관리합니다.
  3. from langchain.chains import RetrievalQA: RetrievalQA 체인을 가져옵니다. 이 체인은 대화 모델을 사용하여 정보 검색 및 답변을 수행하는 기능을 제공합니다.
  4. llm = ChatOpenAI(...): ChatOpenAI 대화 모델을 초기화합니다. 이때, OpenAI API 키, 모델 이름, 및 온도(temperature)를 설정합니다.
  5. conversational_memory = ConversationBufferWindowMemory(...): ConversationBufferWindowMemory를 초기화합니다. 이때, 메모리의 키(memory_key), 윈도우 크기(k), 및 메시지 반환 여부(return_messages)를 설정합니다.
  6. qa = RetrievalQA.from_chain_type(...): RetrievalQA 체인을 초기화합니다. 이때, ChatOpenAI 대화 모델(llm), 체인 타입(chain_type), 및 정보 검색을 위한 벡터 스토어(vectorstore)를 설정합니다.

이렇게 초기화된 객체들은 대화 기록 관리, ChatOpenAI를 사용한 대화 모델, 그리고 정보 검색을 수행하는 RetrievalQA 체인을 사용하여 다양한 자연어 처리 작업을 수행할 수 있습니다.

 

Using these we can generate an answer using the run method:

 

이를 사용하여 run 메소드를 사용하여 답변을 생성할 수 있습니다.

 

qa.run(query)
The College of Engineering was established in 1920 at the University of Notre Dame.

 

Note: 여기까지 제가 이해한 과정은 이렇습니다. 일단  OpenAI의 임베딩 모델을 사용해 해당 데이터 세트에 대한 임베딩 정보를 얻습니다. 그리고 이 임베딩 정보를 pinecone을 사용해 벡터 디비에 보관합니다.

이렇게 되면 해당 데이터세트에 대한 임베딩 정보를 얻기 위해 OpenAI에 계속 접속할 필요 없이 pinecone의 벡터 디비를 사용하면 되므로 비용을 줄일 수 있습니다.

그 다음 대화 기능을 사용하기 위해 OpenAI의 채팅 모델을 사용합니다. 그리고 지난 섹터에서 다루었던 Langchain의 기능 (conversational buffer window memory , Retrival QA 등등) 을 사용해 채팅 어플리케이션을 만듭니다.

 

But this isn't yet ready for our conversational agent. For that we need to convert this retrieval chain into a tool. We do that like so:

 

하지만 아직 대화 에이전트에는 준비가 되어 있지 않습니다. 이를 위해서는 이 검색 체인을 도구로 변환해야 합니다. 우리는 그렇게 합니다:

 

from langchain.agents import Tool

tools = [
    Tool(
        name='Knowledge Base',
        func=qa.run,
        description=(
            'use this tool when answering general knowledge queries to get '
            'more information about the topic'
        )
    )
]

 

이 코드는 langchain 라이브러리에서 제공하는 Tool 클래스를 사용하여 대화 에이전트에 추가할 도구를 정의하는 부분입니다. 아래는 코드의 주요 설명입니다:

  1. from langchain.agents import Tool: langchain 라이브러리에서 Tool 클래스를 가져옵니다. 이 클래스는 에이전트에 추가할 도구를 정의하는 데 사용됩니다.
  2. Tool(...): Tool 클래스의 인스턴스를 생성합니다. 이 때, 다음과 같은 매개변수를 설정합니다:
    • name: 도구의 이름으로, 여기서는 'Knowledge Base'로 설정되어 있습니다.
    • func: 도구를 실행할 함수로, 여기서는 qa.run으로 설정되어 있습니다. 이는 qa 객체의 run 메서드를 실행하는 것을 의미합니다.
    • description: 도구에 대한 설명으로, 여기서는 일반 지식 질문에 대한 추가 정보를 얻기 위해 사용되는 도구임을 나타내는 설명입니다.
  3. tools = [...]: Tool 클래스로 생성된 도구를 리스트에 추가합니다. 이 리스트는 나중에 에이전트에 추가될 도구들을 모아놓은 것입니다.

이렇게 정의된 tools는 이후에 초기화된 에이전트에 추가되어, 해당 도구를 사용하여 특정 작업을 수행하도록 구성됩니다.

 

Now we can initialize the agent like so:

 

이제 다음과 같이 에이전트를 초기화할 수 있습니다.

 

from langchain.agents import initialize_agent

agent = initialize_agent(
    agent='chat-conversational-react-description',
    tools=tools,
    llm=llm,
    verbose=True,
    max_iterations=3,
    early_stopping_method='generate',
    memory=conversational_memory
)

 

With that our retrieval augmented conversational agent is ready and we can begin using it.

 

이로써 검색 증강 대화 에이전트가 준비되었으며 사용을 시작할 수 있습니다.

 

Using the Conversational Agent

 

To make queries we simply call the agent directly. 문의하려면 agent  에게 직접 call 하면 됩니다.

 

agent(query)
> Entering new AgentExecutor chain...
{
    "action": "Knowledge Base",
    "action_input": "When was the College of Engineering at the University of Notre Dame established?"
}
Observation: The College of Engineering at the University of Notre Dame was established in 1920.
Thought:{
    "action": "Final Answer",
    "action_input": "The College of Engineering at the University of Notre Dame was established in 1920."
}

> Finished chain.
{'input': 'when was the college of engineering in the University of Notre Dame established?',
 'chat_history': [],
 'output': 'The College of Engineering at the University of Notre Dame was established in 1920.'}

 

Looks great, now what if we ask it a non-general knowledge question?

 

멋지네요. 이제 일반 지식이 아닌 질문을 하면 어떨까요?

 

agent("what is 2 * 7?")
> Entering new AgentExecutor chain...
{
    "action": "Final Answer",
    "action_input": "The result of 2 * 7 is 14."
}

> Finished chain.
{'input': 'what is 2 * 7?',
 'chat_history': [HumanMessage(content='when was the college of engineering in the University of Notre Dame established?', additional_kwargs={}, example=False),
  AIMessage(content='The College of Engineering at the University of Notre Dame was established in 1920.', additional_kwargs={}, example=False)],
 'output': 'The result of 2 * 7 is 14.'}

 

Perfect, the agent is able to recognize that it doesn't need to refer to it's general knowledge tool for that question. Let's try some more questions.

 

완벽합니다. 에이전트는 해당 질문에 대해 일반 지식 도구를 참조할 필요가 없다는 것을 인식할 수 있습니다. 몇 가지 질문을 더 시도해 보겠습니다.

 

 

agent("can you tell me some facts about the University of Notre Dame?")
> Entering new AgentExecutor chain...
{
    "action": "Knowledge Base",
    "action_input": "University of Notre Dame facts"
}
Observation: - The University of Notre Dame is a Catholic research university located in South Bend, Indiana, USA.
- It is consistently ranked among the top twenty universities in the United States and as a major global university.
- The undergraduate component of the university is organized into four colleges (Arts and Letters, Science, Engineering, Business) and the Architecture School.
- The university's graduate program has more than 50 master's, doctoral and professional degree programs offered by the five schools, with the addition of the Notre Dame Law School and a MD-PhD program offered in combination with IU medical School.
- Notre Dame's campus covers 1,250 acres in a suburban setting and it contains a number of recognizable landmarks, such as the Golden Dome, the "Word of Life" mural (commonly known as Touchdown Jesus), and the Basilica.
- The university counts approximately 120,000 alumni, considered among the strongest alumni networks among U.S. colleges.
- The university has many multi-disciplinary institutes devoted to research in varying fields, including the Medieval Institute, the Kellogg Institute for International Studies, the Kroc Institute for International Peace studies, and the Center for Social Concerns.
- Recent research includes work on family conflict and child development, genome mapping, the increasing trade deficit of the United States with China, studies in fluid mechanics, computational science and engineering, and marketing trends on the Internet.
- In 2014 the Notre Dame student body consisted of 12,179 students, with 8,448 undergraduates, 2,138 graduate and professional and 1,593 professional (Law, M.Div., Business, M.Ed.) students.
- The university's intramural sports program was named in 2004 by Sports Illustrated as the best program in the country, while in 2007 The Princeton Review named it as the top school where "Everyone Plays Intramural Sports."
Thought:{
    "action": "Final Answer",
    "action_input": "The University of Notre Dame is a Catholic research university located in South Bend, Indiana, USA. It is consistently ranked among the top twenty universities in the United States and as a major global university. The undergraduate component of the university is organized into four colleges (Arts and Letters, Science, Engineering, Business) and the Architecture School. The university's graduate program has more than 50 master's, doctoral and professional degree programs offered by the five schools, with the addition of the Notre Dame Law School and a MD-PhD program offered in combination with IU medical School. Notre Dame's campus covers 1,250 acres in a suburban setting and it contains a number of recognizable landmarks, such as the Golden Dome, the \"Word of Life\" mural (commonly known as Touchdown Jesus), and the Basilica. The university counts approximately 120,000 alumni, considered among the strongest alumni networks among U.S. colleges. The university has many multi-disciplinary institutes devoted to research in varying fields, including the Medieval Institute, the Kellogg Institute for International Studies, the Kroc Institute for International Peace studies, and the Center for Social Concerns. Recent research includes work on family conflict and child development, genome mapping, the increasing trade deficit of the United States with China, studies in fluid mechanics, computational science and engineering, and marketing trends on the Internet. In 2014 the Notre Dame student body consisted of 12,179 students, with 8,448 undergraduates, 2,138 graduate and professional and 1,593 professional (Law, M.Div., Business, M.Ed.) students. The university's intramural sports program was named in 2004 by Sports Illustrated as the best program in the country, while in 2007 The Princeton Review named it as the top school where \"Everyone Plays Intramural Sports.\""
}

> Finished chain.
{'input': 'can you tell me some facts about the University of Notre Dame?',
 'chat_history': [HumanMessage(content='when was the college of engineering in the University of Notre Dame established?', additional_kwargs={}, example=False),
  AIMessage(content='The College of Engineering at the University of Notre Dame was established in 1920.', additional_kwargs={}, example=False),
  HumanMessage(content='what is 2 * 7?', additional_kwargs={}, example=False),
  AIMessage(content='The result of 2 * 7 is 14.', additional_kwargs={}, example=False)],
 'output': 'The University of Notre Dame is a Catholic research university located in South Bend, Indiana, USA. It is consistently ranked among the top twenty universities in the United States and as a major global university. The undergraduate component of the university is organized into four colleges (Arts and Letters, Science, Engineering, Business) and the Architecture School. The university\'s graduate program has more than 50 master\'s, doctoral and professional degree programs offered by the five schools, with the addition of the Notre Dame Law School and a MD-PhD program offered in combination with IU medical School. Notre Dame\'s campus covers 1,250 acres in a suburban setting and it contains a number of recognizable landmarks, such as the Golden Dome, the "Word of Life" mural (commonly known as Touchdown Jesus), and the Basilica. The university counts approximately 120,000 alumni, considered among the strongest alumni networks among U.S. colleges. The university has many multi-disciplinary institutes devoted to research in varying fields, including the Medieval Institute, the Kellogg Institute for International Studies, the Kroc Institute for International Peace studies, and the Center for Social Concerns. Recent research includes work on family conflict and child development, genome mapping, the increasing trade deficit of the United States with China, studies in fluid mechanics, computational science and engineering, and marketing trends on the Internet. In 2014 the Notre Dame student body consisted of 12,179 students, with 8,448 undergraduates, 2,138 graduate and professional and 1,593 professional (Law, M.Div., Business, M.Ed.) students. The university\'s intramural sports program was named in 2004 by Sports Illustrated as the best program in the country, while in 2007 The Princeton Review named it as the top school where "Everyone Plays Intramural Sports."'}

 

agent("can you summarize these facts in two short sentences")
> Entering new AgentExecutor chain...
{
    "action": "Final Answer",
    "action_input": "The University of Notre Dame is a Catholic research university located in South Bend, Indiana, USA. It is consistently ranked among the top twenty universities in the United States and as a major global university."
}

> Finished chain.
{'input': 'can you summarize these facts in two short sentences',
 'chat_history': [HumanMessage(content='when was the college of engineering in the University of Notre Dame established?', additional_kwargs={}, example=False),
  AIMessage(content='The College of Engineering at the University of Notre Dame was established in 1920.', additional_kwargs={}, example=False),
  HumanMessage(content='what is 2 * 7?', additional_kwargs={}, example=False),
  AIMessage(content='The result of 2 * 7 is 14.', additional_kwargs={}, example=False),
  HumanMessage(content='can you tell me some facts about the University of Notre Dame?', additional_kwargs={}, example=False),
  AIMessage(content='The University of Notre Dame is a Catholic research university located in South Bend, Indiana, USA. It is consistently ranked among the top twenty universities in the United States and as a major global university. The undergraduate component of the university is organized into four colleges (Arts and Letters, Science, Engineering, Business) and the Architecture School. The university\'s graduate program has more than 50 master\'s, doctoral and professional degree programs offered by the five schools, with the addition of the Notre Dame Law School and a MD-PhD program offered in combination with IU medical School. Notre Dame\'s campus covers 1,250 acres in a suburban setting and it contains a number of recognizable landmarks, such as the Golden Dome, the "Word of Life" mural (commonly known as Touchdown Jesus), and the Basilica. The university counts approximately 120,000 alumni, considered among the strongest alumni networks among U.S. colleges. The university has many multi-disciplinary institutes devoted to research in varying fields, including the Medieval Institute, the Kellogg Institute for International Studies, the Kroc Institute for International Peace studies, and the Center for Social Concerns. Recent research includes work on family conflict and child development, genome mapping, the increasing trade deficit of the United States with China, studies in fluid mechanics, computational science and engineering, and marketing trends on the Internet. In 2014 the Notre Dame student body consisted of 12,179 students, with 8,448 undergraduates, 2,138 graduate and professional and 1,593 professional (Law, M.Div., Business, M.Ed.) students. The university\'s intramural sports program was named in 2004 by Sports Illustrated as the best program in the country, while in 2007 The Princeton Review named it as the top school where "Everyone Plays Intramural Sports."', additional_kwargs={}, example=False)],
 'output': 'The University of Notre Dame is a Catholic research university located in South Bend, Indiana, USA. It is consistently ranked among the top twenty universities in the United States and as a major global university.'}

 

Looks great! We're also able to ask questions that refer to previous interactions in the conversation and the agent is able to refer to the conversation history to as a source of information.

 

멋져요! 또한 대화에서 이전 상호 작용을 참조하는 질문을 할 수 있으며 상담원은 대화 기록을 정보 소스로 참조할 수 있습니다.

 

That's all for this example of building a retrieval augmented conversational agent with OpenAI and Pinecone (the OP stack) and LangChain.

 

OpenAI, Pinecone(OP 스택) 및 LangChain을 사용하여 검색 증강 대화 에이전트를 구축하는 이 예는 이것이 전부입니다.

 

Once finished, we delete the Pinecone index to save resources:

 

완료되면 Pinecone 인덱스를 삭제하여 리소스를 절약합니다.

 

pinecone.delete_index(index_name)

 

이 코드는 Pinecone에서 지정된 인덱스를 삭제하는 명령어입니다. 아래는 코드의 설명입니다:

  1. pinecone.delete_index(index_name): Pinecone에서 delete_index 함수를 사용하여 지정된 인덱스를 삭제합니다. 이 함수는 Pinecone에게 해당 이름의 인덱스를 삭제하도록 요청합니다. index_name은 삭제하려는 인덱스의 이름을 나타냅니다.

이 명령을 실행하면 Pinecone에서 해당 인덱스와 관련된 데이터가 모두 삭제됩니다. 삭제된 인덱스는 더 이상 쿼리나 업데이트를 수행할 수 없게 됩니다. 주의해야 할 점은 한 번 삭제된 인덱스는 되돌릴 수 없으므로 신중하게 사용해야 합니다.

 

 

반응형