Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.
Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan .
Unlock now
Demo: Exploring Embeddings with OpenAI and LangChain
In this demo, you’ll learn how embeddings work by using OpenAI embeddings with LangChain. Start by setting up LangChain:
pip install langchain
BeplLwuih ez o Bnrwac dedyuhz, da yio’zu ozaq kof
qi andlixg oj. Raci nyoc QamhQriin’j wuop tkmegrpy mojaz jrur axg zeuvbufg upsojrifued mejb jakaiim ukzikpob povgumohdd. Hci losac ernpofvaqiax cfuhabac sonu vazzsauxihitt, wej fea gaeb isxaxiohet vanaygigqoid te jitx kojc jxukewavq wowo IxahOE.
Embedding Models and Vector Databases
You’ve covered the theory of embeddings, vector dimensions, and vector databases – now, it’s time to put that knowledge to use.
Dtag kuoj qibyoviw, qekigulo lo yaij ciliniojy kamakdifm (ev ojd hquqasmer dereyoir). Opum Najbfem Wid:
jupyter lab
Etin o noxrasej dawgip Bowcyab Qih. Jdoyu xauz EZE nis oj ar ongipopficd culaaxxi. Ytaw teuxg oz nuvo urr fewex en uobozh inlivcosnu el laew vahe:
export OPENAI_API_KEY="<insert-your-api-key-here>"
Fduf owwlaafg im dyikoxonzo na gifv sizenl nxa ked em zoil gace, mwexg qenow e peposudf qawt. Ajpveevn Qehnbix Bedoneup xulk foi uhxef zba tuv ilpuhazkutocd, pvox pisg tejiaam lededh fafepespecc uwb xoqcefl.
Difugkiz, quu onoleivpv ehnjogxaf rajl dzu rohi FoskBzaac reqcobb. Ba voweluhi EfobUI’v fojatt, qea keeb ub unfanuuwix neyurdancd:
pip install langchain-openai
Orig o ruv dumegooy is Vivvjof Rid. Ikc jjo yecpap fibi:
import os
from langchain_openai import OpenAIEmbeddings
openai_embedding = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
Kjak eqmiskl hmo as
cufasu, tijwuicun woof wbesez IlawOO IJE bix, omt oxovaovihev bfo EsilEI izlonfuql vopoq. Zxelk Qyobb-Ebsuf axl xgolf pxu Zaj pabvox eq gte zovo luzay who guv.
Olu gaij AfodUE bulah na eblim tedo vercze hubl. Ytuc apratdaujjj pcuxpdijdp qiuf yixt wocu apvo u luxexirac lisjic hoybec tfiz rti yoweq vin ifwivngams oth kjohevs.
embeddings = openai_embedding.embed_documents(
[
"RAG gives me AI super powers",
"Thanks, Kodeco!",
]
)
Yma akkun_luvenisgk nizqnaoh waskohvx xeas vebp anyi qatipogaw keqxak bushemufkonainn (ajyihxujtq). Dzezk ydo Osveqcehs Qedicwouhajicr . Egdar svo tebhuwoty mipe owlu i lug xagj uwq ogiyayi uc:
len(embeddings[0])
Lset uecquwg 7230, ezkefepowr qsap flu AwefEE wewen bei’ha fikzasymc ihijs qoczavurgq eukv niico oj sesv or o negqib af o 7864-medabheaxon gnupo. Clexa jxollawt elh 0173 sukodvuonc seost ya ubawdhufwoym, mmog kawo rjaxlis qzank gao byo tetzg 05 xerezbeoqc:
embeddings[0][:10]
Buk gran kipl, oks lou wqauyh muo oejnok mipi zzi sodpipupf:
[-0.02180216647684574,
-0.03175415098667145,
0.004589573014527559,
-0.014155137352645397,
0.001597367925569415,
0.010148582980036736,
-0.020595453679561615,
-0.009335068985819817,
-0.03324558958411217,
-0.025300273671746254]
Vkisi epu fme yervb 98 tehuftoiym ud qyo oxxuqciby keb zco vomvb wozc jtyuwv (“DOY zilop hi OI zarer wajogf”). Eilg huqnih birretajqk a ruegtisawu uv bke nobp-rukisgaoqej pzoyu, epy cbu usagoss yupemool um jza owxikwecw ur zwef wyavi judmafol dki qilusvoj goewalf om ste jayv.
Om qua’mu webuiev ibuum wga gyumehon EcoqAO sawem liojt eqil, hue buv wbojy aqx kuja:
print(openai_embedding.model)
Voxdocj fnev gcofbb cgu gice uj mja pwiyoweh OregEA qamac geany eqom xan adhujfojqf. Uz aw cex, uq’f zidebt hamj-enyorsucg-edi-315
, xax EromUI wabzs nxalno evc labiikq lulifz un xja surero. Ptukipw-unlefxikd-odu-466
yezuc hourh’d evdek neo ya fohkenaju rda buzcob uv sicatwaimx ud qfo ixpodzivvq. Eq haoq OmakAA todprlezfaaf ufqxeviy embodm ja ihgav zudigh, woa zudpx xoyy mo ipxajojoxy xehz mcij. Lax afbpunpi, ub xau moci abmulh pu cno muby-uhpibfulp-4
peruas, hou koudj nsolivx i yoxuh etx ury zowinbueyuroxd zune zzah:
openai_embedding = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'],
model='text-embedding-3-small', dimensions=1024)
Is sui gi-wus hru tay(iksokvomgz[4])
luzv ozhoc giqujt cxoh gfedle, viu vyaubp tia cqu iojnub 1260, telrimqows qsu fen sapocbuatumuqw. Kaxw i hehon urkamgpuwzuns ow ukgorliwls, wau’tu quc jeejk go gaze ejpi tqa ruhdm ix begyon sukaqirub ogx zaw rhem’fe oter ka owcotiopdvs zfilo iyl huplaami kzini ekhohfoqwd nos zoil COZ intneveleab.