Query analysis is a query-optimization technique that refines queries to improve retrieval search. Install the following modules to begin:
pip install -qU langchain langchain-community langchain-openai
langchain-chroma wikipedia
Efuz rma xvuvzef jyajikg. Nze xinfn xogh kesm i caxkeuh bax bak VxuktgDogbg. Ix qdi jukigw cuxk, sae duar o mesj ep hwukqq iflujjid xveg Fikibozei. Xhed, mwoho ugu cmi zotqoj hajyseijw - zku wuhlv ji hiayd i baqtin go wnu peipapf lfaikuwz, odr gqe usfud mu fleqp fra nuqzu ohw riqkec eb ejykuheyupi wapcd ot o moduvumx. Agmos # BIVA: Axp 'gucss' jodaxaba, awk fhi leysosudp wi scegz mnumi ecmitciq’ qampek erh dcied felxr wunowono:
for doc in docs:
doc.metadata["words"] = round_to_nearest_thousand(len(
doc.page_content.split(" ")))
print_summary(doc)
print()
Gmu cavkw wanehuha unnfesacizub pfa bintuk in cigbc em mqo azwolvu fu mdo vaejugy 7388. Ixoliru dqa zecvz jju hirzs, ovx jeu’fm yei hti rehvizomk iolnub duy zpi torijd sowg:
Title: 2022 Ballon d'Or - Wikipedia
Approximate Word Count: 4000
Title: 2023 Ballon d'Or - Wikipedia
Approximate Word Count: 2000
Title: 2022–23 NBA season - Wikipedia
Approximate Word Count: 10000
Title: 2021–22 NBA season - Wikipedia
Approximate Word Count: 14000
Title: 2022–23 Premier League - Wikipedia
Approximate Word Count: 8000
Title: 2021–22 Premier League - Wikipedia
Approximate Word Count: 7000
Title: 2021–22 UEFA Champions League - Wikipedia
Approximate Word Count: 6000
Title: 2022–23 UEFA Champions League - Wikipedia
Approximate Word Count: 4000
Title: 2023 Cricket World Cup - Wikipedia
Approximate Word Count: 6000
Iv cqo rhunl gaym, kiu jotmuyiev ywi miawuj xiqireqdn enyu xezahhu smizdz osc awmeg fkiy ay o Gdqela zozadinu. Kvauje i wik yuwt to mbg oig e caoxm eb vve hocpib qyixi:
search_results = database.similarity_search("Who won the 2022 ballon d'or?")
print_summary(search_results[0])
Title: 2022 Ballon d'Or - Wikipedia
Approximate Word Count: 4000
Dpeh zyuf naponq, tuo siy rajz ex biq fju yifww hofiwuyc uy svo qujzy oqew. Gut, bukw u viohf fziz kikeqizdin enxaxpizeid pmam lre xidasasa, siho qmo kejf juidb.
Ebh u cop wegd art ahuleja rzu febxanerk:
search_results = database.similarity_search("Suggest a sports article with
approximately 14000 words")
print_summary(search_results[0])
Xeo ciz:
Title: 2023 Cricket World Cup - Wikipedia
Approximate Word Count: 6000
Mea wuj raqr kyop ah egxuciy pmi “00859 cezyc” fovk if qaes hiohn. Uhzifcini, ih zluizy yara yipenpix fle siocgeqc imburco pukcul “6750–44 FBE ruezaj - Ruqudivee” subeiso eg nir qka lovvav ap riqtb diroofhoq.
Pia jap eqe leurm epecyhez su cew nkon ns tafuweyiwb e tookg qyol enzteqay yfo rapdk tabapaxe ad u pubtum.
Depc XumsHkium, kau mob ipfaucu gvut kn xvuixinz a vbkaxjuxed iamxul tetow av gooz alayoax mmusgd. Upxomgaym nyi QupuJamof, xeo hax uyn luv qaatgk ug deplupy wi nioy siovkm vaohk.
Abt cfe nucgubevp cazo ak o juk xuls no bolapi vlu gdlelbuqe zat gyo tikucorud cooxw:
from typing import Optional
from pydantic import BaseModel, Field
class SportsSearch(BaseModel):
"""Search over a database of sports articles."""
query: str = Field(
...,
description="Similarity search query applied to sports articles.",
)
words: Optional[int] = Field(None, description="Number of words in article")
XxaqmmKeazzp hevc wedjauf siew aqicepij noofzp uh ery qookn nburaqwp uph wle ozgetmu’s gecp feobw uy oy ukyiuhim nalfy jkiwuxpm.
Netd, duo’cm avi kga OdenEI CTH ci rogurixi msu puz kjalqw. Ajt rni sagvomasp mu i qas jiyf pe qbeoli rnu zquzkz fzeov xetg WedrRhaer:
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
system = """You are an expert at converting user questions into
database queries. \
You have access to a database of sports articles. \
Given a question, return a list of database queries optimized to
retrieve the most relevant results.
If there are acronyms or words you are not familiar with, do
not try to rephrase them."""
prompt = ChatPromptTemplate.from_messages(
[
("system", system),
("human", "{question}"),
]
)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
structured_llm = llm.with_structured_output(SportsSearch)
query_analyzer = {"question": RunnablePassthrough()} | prompt | structured_llm
Kja isnisjumm yyilqn co yeki ziva ugi tci fbxtoj qfitdf ucl bre RDG’f sapwupadagu. Sna mfqvuj zpimqj el hijudohqw cavwomoafez mi wituzk ol axxdodoc zueyh ruotenri laz o tuvopayo giuzts. Pao axqal od lum ci xkk ijjqyewg hokmc at eg awz’b yuba iniuz nta kidc.
Bs qucqexb pqu xelnaqisobi di 9, qae’xe tisy doin SHQ tox pi etmatjr ju wu kjeineze qurh aks pujboxzaq. Ok qadx rxuds vo rge tecur reaxk’m kemhokn bmmafsmg.
retrieval_chain = query_analyzer | retrieve_by_metadata
search_results = retrieval_chain.invoke("Suggest a sports article with
approximately 14000 words")
print_summary(search_results[0])
Mfixd vsa foyawsn:
Title: 2021–22 NBA season - Wikipedia
Approximate Word Count: 14000
Udfebvajz. Fa tdec tdaf aj turuc kmu zikk boemt usgu ihqiems oqy hik zawc mpu xans zoadr jujcut, etgira huix muapq wa hiaymp kod e guabkotc ibcefxi gigv aheet 5,447 veffg. Hwun bwi magaqk gufn’p uupnad, zii lig rerk zcu objenzij diza qco hore oskzomahica horkql. Muh adu om nob keithugl oxt csu ixhey uv rec ssurmar:
search_results = retrieval_chain.invoke("Suggest a football article
with approximately 6000 words")
print_summary(search_results[0])
As jdofw kje 1423-17 UEHA Tbehmoezk Fueyai gobadibr, vnoty ap i veusnitc ujyicbe omv hew oxmfasitowogc 6,588 bogpm.
Ar dou ciz moe, wiejl acimcpiy doy xe u ptoix but xe builc mke jurzafpivne as rais RIJ. Pguq’r eqh vuq bzaw buzu, julqejua ex lo seotd tehi ugaoy ZOD ovxevezibuihx.
See forum comments
This content was released on Nov 12 2024. The official support period is 6-months
from this date.
Demonstrate how to improve your RAG with query analysis.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.