Lưu trữ và truy xuất RAG bằng cơ sở dữ liệu vector

Phát triển ứng dụng LLM với LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Các bước phát triển RAG

Quy trình RAG tổng quát: bộ nạp tài liệu, bộ tách tài liệu, và quy trình lưu trữ & truy xuất.

  • Trọng tâm video: lưu trữtruy xuất
Phát triển ứng dụng LLM với LangChain

Cơ sở dữ liệu vector là gì và vì sao cần?

Một quy trình RAG điển hình.

Phát triển ứng dụng LLM với LangChain

Nên dùng cơ sở dữ liệu vector nào?

 

Bức tranh hệ sinh thái các cơ sở dữ liệu vector: mã nguồn mở/đóng, chuyên biệt hoặc chỉ hỗ trợ thao tác vector.

 

Cần cân nhắc:

  • Mã nguồn mở vs. mã nguồn đóng (giấy phép)
  • Đám mây vs. tại chỗ
  • Nhẹ vs. mạnh
1 Nguồn ảnh: Yingjun Wu
Phát triển ứng dụng LLM với LangChain

Làm quen với tài liệu...

docs
[
    Document(
        page_content="In all marketing copy, TechStack should always be written with the T and S
        capitalized. Incorrect: techstack, Techstack, etc.",
        metadata={"guideline": "brand-capitalization"}
    ),
    Document(
        page_content="Our users should be referred to as techies in both internal and external
        communications.",
        metadata={"guideline": "referring-to-users"}
    )
]
Phát triển ứng dụng LLM với LangChain

Thiết lập cơ sở dữ liệu vector Chroma

from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma

embedding_function = OpenAIEmbeddings(api_key=openai_api_key, model='text-embedding-3-small')


vectorstore = Chroma.from_documents( docs, embedding=embedding_function, persist_directory="path/to/directory" )
retriever = vectorstore.as_retriever( search_type="similarity", search_kwargs={"k": 2} )
Phát triển ứng dụng LLM với LangChain

Tạo mẫu prompt

from langchain_core.prompts import ChatPromptTemplate

message = """
Review and fix the following TechStack marketing copy with the following guidelines in consideration:

Guidelines:
{guidelines}

Copy:
{copy}

Fixed Copy:
"""

prompt_template = ChatPromptTemplate.from_messages([("human", message)])
Phát triển ứng dụng LLM với LangChain

Xâu chuỗi tất cả lại!

from langchain_core.runnables import RunnablePassthrough

rag_chain = ({"guidelines": retriever, "copy": RunnablePassthrough()}
             | prompt_template
             | llm)

response = rag_chain.invoke("Here at techstack, our users are the best in the world!")
print(response.content)
Here at TechStack, our techies are the best in the world!
Phát triển ứng dụng LLM với LangChain

Luyện tập nào!

Phát triển ứng dụng LLM với LangChain

Preparing Video For Download...