Stockage et recherche RAG avec des bases de données vectorielles

Développer des applications LLM avec LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Étapes de développement RAG

Flux de travail RAG général : chargeur de documents, séparateur de documents, puis stockage et recherche.

  • Dans cette vidéo : stockage et recherche
Développer des applications LLM avec LangChain

Qu'est-ce qu'une base vectorielle et pourquoi en avez-vous besoin ?

Un flux RAG typique.

Développer des applications LLM avec LangChain

Quelle base vectorielle choisir ?

 

Panorama des différentes bases vectorielles, ouvertes ou propriétaires, dédiées ou avec prise en charge.

 

À considérer :

  • Ouvert vs propriétaire (licence)
  • Infocloud vs local (sur site)
  • Léger vs performant
1 Crédit image : Yingjun Wu
Développer des applications LLM avec LangChain

Découvrons les documents…

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"}
    )
]
Développer des applications LLM avec LangChain

Configurer une base vectorielle 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} )
Développer des applications LLM avec LangChain

Créer un gabarit d'invite

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)])
Développer des applications LLM avec LangChain

Tout enchaîner !

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!
Développer des applications LLM avec LangChain

Passons à la pratique !

Développer des applications LLM avec LangChain

Preparing Video For Download...