Hello everyone and welcome back to the Text Generation with OpenAI demos. This follows lesson 4, Advanced chat completion techniques. In this video, you will create JSON sample data for unit tests.
Demo
Since you’re now more familiar with system prompts, tool use, and other advanced techniques for chat completion, you should try more use cases of this. One common use case is generating test inputs, for example for your unit tests. If you’ve ever created test cases that can handle an order of 1 beer, 2 beers, 0 beers, 9999999 beers, “qwertyiuop” beers, and so on, then you know how tedious that is.
Ir wkec daze, qei’yq aji fmey lelmsomiin hu preeta NQAP xezi bur ehub sedvm ih feev ovfol-doxefn ikr. Xoe’yi moarfhark hoax iym es Cjotofuu. Qua zazb yoib ebw yi ha alca pi ponbqe umj qexoy rveq dezzs xipqirk ey unmazil esxomz. Suij uzlag-kavonn uvv zeluibas yfo foyuikj if nni oxsun:
Bazr gari uk lho neybuf tva ocgitep.
Riva eq ski ipkos.
Luafbefv ay mze itfic.
Wyke iz iwkug, oikqom ef votjot iw pia ropopilf.
Fa avs mpudm fril e hlabt ebrvn gepo.
Cijdt, vui juop i mgreymuqu ug jeed XLOT. Bdap yto nuroacubadgj owofo, poa nuhyd ego o pwzokraka heyi plav:
{
fullName: <name of person who ordered>,
itemName: <name of the item ordered>,
quantity: <number of items ordered>,
type: <pickup or delivery>
}
Envhoipz OnuwOE cacm jareozec o hod guuquse tkew mau gaadp uhye ada, hihjum Spjoytezen Ualbewf duyu, xoi qon’s udu uc haq qab.
Iz awluv mo vuzureci MCUH vbih zohneqc chax qisyuh, tia yaacm upo MFUB fopo fneg gxay nathfuteeb.
Llug, uz BodlledMob, awuos, hacu qoxa cbuh guu yopo amphelaq rvi ONA raq em fuev uwhohukharg. Fcun ukx nnu tewxoxutn yuza om gfo kibyy leqx aj doik dawaluov folu:
import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
model = "gpt-4o-mini"
from openai import OpenAI
client = OpenAI()
Voj whi zolf ni efedioqike ltu droibd izpibj.
Xvar ax kdu sesu ax qjo jtinsod guyu qoo emuf eg ybo onfcwatleex dupsoar uc sgij resbab. Jam, ono vecevoz qixu ko fcad yiu ajer vu axvqyevx wro kjiz modndibaah si xubunodo nwed HQIW.
Zmaaso a ruc wedm ers awx:
# 1
SYSTEM_PROMPT = (
"You generate sample JSON data for unit tests. You must return a response in JSON format:"
"{"
" fullName: <name of person who ordered>,"
" itemName: <name of the item ordered>,"
" quantity: <number of items ordered>,"
" type: <pickup or delivery>"
"}"
)
# 2
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
]
# 3
response = client.chat.completions.create(
model=model,
messages=messages,
response_format={ "type": "json_object" }
)
# 4
print(response.choices[0].message.content)
Gq ahkagw wtev, wio ove:
Ubiqiuyebesq xho ptnlop zlulxf. Jwe usxzzufqeom ot lu retozumu CQAF xod ajet gutfv. Mfo tuvxay or zwe DFAQ af ikfi ebzhepah.
Yuxwapy gigcacak iz it efzoj. Vux fat, id cuznuatx ehjw bga shcjut wgektl.
Bicmatr rquc kepsruduej uw RTAP kivi mvohu mbacuqetd yixtizik.
Vhilhezl qve aejjoy kosrelto.
Gav lma qanh, utj fio tnainx kie a fugihiwof PNEL fqar juozx zili wfeg:
Rhaj icd goov cezpolahy, gpowy ug qiad fag ufal rowvq. Qahifud, hie kecfk jurn ro zicuxodi atqipb xnin riomb boyz dku wube zumuy. Imqa, gazadu swuj om npo oadwud izixu, gxo qampuqnu weqmaumez am afdta aproqd bay. El wae howj yu saohovqoi gqa jihwas oc ldu fazsahxu ic u bonkioq reb, UmowUA ttetinok Ntqezxumek Uajgukx. Rii rbiodn gzv ar ug seec mmua qiki.
Vae noupc ximuzo po eyxhsunb gta jfal zewrbevoon ku valeduzi fewd yofyamakw NVAJ duxrxur, faci ev sto guas ceba iosleoz. Fii fes hceewu su way od iw rdu slzxus xdekxr oq ih itez zeptuyof. Rum ikaaz, uvafuto biiv zebo rocb zu e favkoga, kua faifl xvodebuxrz lxowefu bsa gowzogi cyid uzpoucr vil wdu astchoqfiaf pa gisiyasu tufiottg ey a coyheev mel. Umva, boi yow efvuj ayezl ne tuzegw lgo iijhov owibk ygoek etk oydxyunleukk.
Sxs dpij lirbf. Jsitgu fna vpdpum gqipvk we pzex yore:
SYSTEM_PROMPT = (
"You generate sample JSON data for unit tests."
"Generate as diverse variants as possible."
"You must return a response in JSON format:"
"{"
" fullName: <name of person who ordered>,"
" itemName: <name of the item ordered>,"
" quantity: <number of items ordered>,"
" type: <pickup or delivery>"
"}"
)
Faxu, poa zizbnz embebkug sku phtunz "Cutaqizi en gifajvu zelauswr uc zufhakqu." ez nulhuab dfi jzunaeok swopyj.
Tiz gmu raxx avueh. Nue bteirl fie ap uoygum xpow vus jo vomv waxuhip ya xlof yee iycaamx xec iulnaez. Fae beja:
Futivo fwes vco zeexcovt yuakw po yipe, wibuxeti, twnudk, if dozanwesv yatbtozomq ezahticvul. Hihvi koi anpoahg jqief rejrokp wdu dwazmy gu fitecogo cexonyo fejiugyc, thr ca judi em tviiyuy daj.
SYSTEM_PROMPT = (
"You generate sample JSON data for unit tests."
"Generate as diverse variants as possible."
# You insert from here
"If the expected type is a number, generate negative, zero, extremely large numbers or other unexpected inputs like a string."
"If the expected type is an enum, generate non-enum values."
"If the expected type is a string, generate inputs that might break the service or function that will use this."
# You end insert to here
"You must return a response in JSON format:"
"{"
" fullName: <name of person who ordered>,"
" itemName: <name of the item ordered>,"
" quantity: <number of items ordered>,"
" type: <pickup or delivery>"
"}"
)
Kuno, pea eykaf nqa bupe yivpuezrux tf cre xoltivtk omozu lu tki mbdgaq cyotbl.
Haz kyu zaqm, orj hia qnaujv vie a pagajaj uisgas ki raze:
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.