Stocarea și recuperarea RAG cu baze de date vectoriale

Developing LLM Applications with LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Etapele dezvoltării RAG

Fluxul general RAG: un încărcător de documente, un separator de documente și procesul de stocare și recuperare.

  • Subiectul acestui videoclip: stocarea și recuperarea
Developing LLM Applications with LangChain

Ce este o bază de date vectorială și de ce este necesară?

Un flux de lucru RAG tipic.

Developing LLM Applications with LangChain

Ce bază de date vectorială să aleg?

 

Peisajul bazelor de date vectoriale disponibile, clasificate după tipul sursei (deschisă/închisă) și după modul de utilizare (dedicate sau cu suport vectorial).

 

Aspecte de luat în considerare:

  • Sursă deschisă vs. închisă (licență)
  • Cloud vs. on-premises
  • Ușor vs. performant
1 Image Credit: Yingjun Wu
Developing LLM Applications with LangChain

Cunoașterea documentelor...

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"}
    )
]
Developing LLM Applications with LangChain

Configurarea unei baze de date vectoriale 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} )
Developing LLM Applications with LangChain

Crearea unui șablon de 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)])
Developing LLM Applications with LangChain

Asamblarea lanțului!

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!
Developing LLM Applications with LangChain

Să exersăm!

Developing LLM Applications with LangChain

Preparing Video For Download...