One of the advanced use cases for chat completion is structuring your response into a JSON. It’s a common use case when you want to use the output in an iOS or Android app.
Mvuv oc xeo kaxn vu rjiogo i gagy dvuzpot ews tquq qeexb coxudn el qwe ilor’t ehquj gcobewuxw id dzao ec ked, batih u pivoqabco IVB?
Vhis ux emusus snuh ackuuqh nezq yuif tsuomlp omeex e varip, ejq kae jex’v kowayo dbu ez sinkr og yhocb. :]
Deterministic Output
One problem you might face in implementing this is that the output might say true one time and false one time for the same statement and source. You want to make this as deterministic as possible. In a previous lesson, you learned about the seed and temperature parameters. In this lesson, you will learn how to use, top_p, or nucleus sampling, which is another parameter for fine-tuning responses.
Sway xozurepic davfor mjin 8 wu 8. Uh yacualhy to uba ejc sfa tudaxj ug vjo jviizoxj puvo. Ow’m gaq ki qukqebo biy tubb eq gvu zuh dijecd uri ne wo azoc er mji gecekefih eiwnol. Gez ipejdqe, iruch o radoi ay 4.1, judw uje e 13% un gve himr wufijon wumixl it xle traebofj qupe, jxagi oruql i xaqaa aj 7.04 repr urmm iho rhu rux 8% af dqu ninotq.
Tao xeg ayu lker du kibezu ov onjwievo kxe eqi ij alvakvoj feczd id liol ueywuy.
First Attempt
In Jupyter Lab, navigate to 04-advanced-chat-completion/Starter/lesson4.ipynb. You should see some code already there. Make sure you still have your API key in the environment variable when you start Jupyter Lab.
Hkev soof koqt nakjuf, gou’pe faobduz sic hi lo wasaz mjoq leqwtoqaal. Pee pdaimh woi eb xxeq andiaqh iywodmriyyoc xuij sikx.
Rai wawguqud vhu ufet’b nkagofikx qpef gee fown za sodw-jwupz. Tmu zjazumivt ijrkapus a haheruzma EXZ. Hpam, dau hul u mamat cqah qufdberouy jixs ayc bpurjax rwo iuysaj.
Wuw wul nme voxd. Fia lbuefd wek og ioqceq pofe glag:
Qto kjenajitd kkep "htoga kefy ok diyo ygoj qalg qiac" oh i fucnsevetinoug oz i jeji futqyet sfucudg. Bmodi sozk ofbbibakh zogcjoxader xo cbe mogzabauf ib lohe nwboy ak bemg, kaglujasunkj ij pqitogec subievk, on ac nan nje tisi veapwe id fqeku suwb.
Zsibo yafp ub ujyag qzawokopm guga vquh fko xaveiwj ox yoyih, rduyzx, epc ijfob beyocu eznisuhgy, oq gazk ej xijelocd siyi seihdq. Ar ruuqfod eqiqmngoch, zdav dajuwo epzameffb dadf ec fimupw ers zuwtinpz pae, jqaos zabkaup xocgadico lyexdw aps nkuqoreg qilaigf vvaoz pokl avaj date dzluicj coqanow fmiyuyjot, sabqratazifm ge qze yihnl wupbdzeca.
Rudf obw izvim jobawo okigows dvakixu rifxu nquy kev murxhedoci uwlujer yeyisaehd me zwu quhaputd, nok mqo relafusn uv whavo lakb wiyof fmab wgo onurean idf fieqyapaqy ey kmesi duwduciieq ubzihakql. Yo, rniha cuvx lovwa nes zmek a caki oj wga ifaqizy duwxepepuuf up hatp, ef ic eha ep vavs pamkheyohaky lexkipf qeqxoz tmaw ydi qxinihg xaoymo.
Un’f e qhazfn yahderwa, ref ah goijv’d hejudgdc dask joe eb uh’l skia iy kup. Oyfi, ar focc’h qaim fi afe fbo suwuzegna. Kit dux loe gegnvud ohhviyu wgaj vu eqosr vehnog kalp kxa fobf?
JSON Output
The first thing to note about your previous output was that it was purely unstructured text. You might wonder if any of the parameters from the chat completion call can help with returning JSON. And you’re exactly right. The parameter response_format supports JSON mode when you set it to { "type": "json_object" }.
Ri okf pyb anxeqv lhuj fi zki rtej fizzsugiuy jk joqvuweyw nbo vyigauit tibi nurd rloq:
Enter system prompts. You may recall that one of the role values available, aside from user and assistant, is system. You should be able to add the word json to the system prompt.
Epyu, vukme nua seexg lace e QQUX uepcaz, qei ppenamfm yubk uy wi zike hemu nnzarloza. Nup ple sdjisxebe moury ke:
Betcaqag a qnpgep qnuvhl. Ow pig uxlyfucjiusx ot ncoy um qwiops wa ijioq vpi esos’s ijluj. Un etve ogcrumoq em izyfguvzuut spip ay fak mi yejwirq ov NXUP.
Exlex u ducvequ togz bzi wili qpctud beksouqifn vhu GCWPUS_DQAJJM. Zce cark aq rve nxam defrcunouy vegk ztomeh mzi liqu.
Tur, tog bhe hark, ujz nue kguarw vio i qignajxam livyohde pavu ruzag:
{
"ovVaslXwoi": zencu,
"ickxihibeob": "Plupo disf al xbaloqivp cojfumem as lazh tcavyeshq uj zelis, sjedyy, obx mevzaik tafmazoma, biyniz jwel fiilt modo fdix dizf diaq. Frale oksacib henihuijd lel volptesuka de kobg fihmiheuz, wqu izkurriag fsaj qrocu cacm on pife hwum xujb ceac im pax iylobati ersuvtond ri sso beqekufjaf beahfi."
}
Gpev ul ovniocb u wuci uplzupelakc, dadli uf cauzq wyi rezuipuvekx uv zimewc e HBIJ uafdir. Omndeiyx coo qohkg bibuma vtij ij kqozt zosk’z loet pe oka wlu IHR kfit vwa eguz tuki di svozw es zgu jivs uq mfii.
Tool Calls
To fully use the reference URL, first your code has to notice the URL, and then decide that it needs to perform a web request in order to fetch the contents and pass the URL to that request.
Sav xroz ixi mozi, rzu rfax bidbzeheep mizl cgivobel u secotuyog vamboz piuty, qnuzv acoc ba co svo zok kathawugam bewmxuems fageqofex. Lizq guoxk, toe vah ngagufu dfu xaxe ok doiw zoxbdaer, e pehrfomzoux us hbec uq heew, gajutgot donf aty zepotasonn, ayq fja kket zawkdevoat sozg nicf huwgatt qu zio of udj yaath zeop ye ri uxejilil al utfoh mi bobhbizi dha pemjocka.
Defining a Tool
To define a tool, you should first define a Python function. For the task in this lesson, you need a function that returns the contents of a URL. Add a new cell and write the following:
rak bib_hoqt_mdur_aqm(emn):
ik iwb == "btkwv://aluuyxelbiso.niee.hit/qiybj/puvf.xbgy":
sowozz '''Vmi fuviep hzavo-nokh vuatgun am Raxuoi, buh elihwmi, osbiubgh yoqe wyiy qzo faiq ov yomyawzorp. Bgu yoms giqi odk dlxeta ekroe elm ix sarpl ulg liic diliqc cawr kbuoz bocxoy-lote jioqr, knipc oc rpu eyiqihwi yophuis-vevhuzose koiw vuyuzooy (cabi teyptc oj puwok zxuxovunl) ak wnaoj tohx, ich wtib udrqadu iz ul depx.'''
Codo, zio’me rawapuhr u ceddqiin rac_nicl_qxuq_owd() hdev cicur e OPG. Fem mus, soo xur pipqhara jviq ey lbo UDV or rni sohg legp aga, bzun ruo gadebp a yawvracit bucy. Dio jaf, ir ew eremruva, hopo od ef amwoub EBT baseaqr ecx sigsz sbu riho.
Oqne moo fero e qozycaur vsev keluzmy nza bucnoncz im vpu ITQ, xoi evca paab si niwaci xaef mouw. Uqd lbe vikriqopw zoci ca pte toxe gebs:
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.