Archiviazione e recupero RAG con database vettoriali

Sviluppare applicazioni LLM con LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Fasi di sviluppo RAG

Il flusso RAG generale: loader, splitter, e processo di archiviazione/recupero.

  • Focus di questo video: archiviazione e recupero
Sviluppare applicazioni LLM con LangChain

Cos’è un database vettoriale e perché serve?

Un tipico workflow RAG.

Sviluppare applicazioni LLM con LangChain

Quale database vettoriale usare?

 

Il panorama dei vari database vettoriali, divisi per open/closed source e per dedicati vs. con supporto.

 

Da considerare:

  • Open source vs. closed source (licenza)
  • Cloud vs. on-premise
  • Leggero vs. potente
1 Crediti immagine: Yingjun Wu
Sviluppare applicazioni LLM con LangChain

Conosci i documenti...

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"}
    )
]
Sviluppare applicazioni LLM con LangChain

Configurare un database vettoriale 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} )
Sviluppare applicazioni LLM con LangChain

Creare un prompt template

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)])
Sviluppare applicazioni LLM con LangChain

Collegare tutto insieme!

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!
Sviluppare applicazioni LLM con LangChain

Ayo berlatih!

Sviluppare applicazioni LLM con LangChain

Preparing Video For Download...