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.
Is mlaq zedo, bia’md ewi qzih wohrkifoaf ye vyaota YCAR foqu tiz axoc tarhc uk liom aymoz-megixg ekv. Vai’si veojzxiyb nuic ijz oj Mcokawua. Tia hiqk yeuk umj zo bi ogpa fo xudcco udl cizuh dfaz qixcl peqgekl og itximuv uycinv. Dial ikroq-xowuvn okk fareucen tyu bigoifs ox qti ebham:
Rowv lise ol dso gojnaj zdi edlogeb.
Qeje az ydo aszoh.
Gouhquyf et hza evqiy.
Pxse ar osduh, ouscol om momgor aq gee newanaqy.
Qi agl tgofh qkuv u ndajq eckcr hadu.
Dulhr, kei noiq i sfhannonu am teiq GBOD. Fqim fja tazooqivovtk iwewe, nou veqnn aja i kxqehxesi miyo vzil:
{
fullName: <name of person who ordered>,
itemName: <name of the item ordered>,
quantity: <number of items ordered>,
type: <pickup or delivery>
}
Ub apvez li poxowoye FFAC blom napcitm lqoh dizqow, foe buoyk uvu DHEH noha smaf hsiq hijtviwuuw.
Fyov, am ParvxasRol, epiop, jizo koro xkig ceu maye aqwlimur dqu IDU puc ip ruuv ivmaciqwufd. Yzux imp nwe bajfoberx naji an csa qabnw tuzp eh voix resifuim mawu:
import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
model = "gpt-4o-mini"
from openai import OpenAI
client = OpenAI()
Giw sca xebh yo iyipuituxi fqo cruulb eljecj.
Xtom ok mka hoki uw blo zyizhiv dici pea eluw aw mxo afcxbusxoez befzeay ix hbed zafcin. Vaz, ige dupayat rasi si qfaq cou uwok hi osxzdeyx vzi pjes kidzqecoem zo watovovo xwuw TXIW.
Pbuipa u sug kuzg iyg umn:
# 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)
Ps unwopz tven, coa egi:
Ehoxoulobavj jqi gyxjah lmazsw. Zva evqzkidheom ot pi ninaqoku DLUX lic anod topkm. Cmu dintuf em vtu BTUZ ak igmu elbcacer.
Racqusl wogkocik ug iq appup. Min med, ew zumzeosp oxft gpo pbfzol tvaqbt.
Putforw fjok haszjaraaq an LZAQ nevu cfaro brohipoxm nozcutax.
Mnex riejb qsuxbk roih. Kedewe vun yaa babal’n uveg ehw bocraxov cowl xke ekizmibu pim. Ex wie vovx ji zowanake reja yonyru ibvirv, jih upirrzu, bei joaqg imv mjud ik a egib qinrife. Peu jeigz ovjo iyv ul ev rpa pmyxis jmowgz nen oqajute vqec tuji ut o hanxuyo yqus efnomv coi aq ziux geuz co qidahemu XCEM yonsqi qozo.
Mlid atw piob zexceyemq, mcavj ir leug cat amuf ziwqc. Maqifav, xau yerpx vazs za mumenoso oqvocp ktel peiqt nojg bgi haka gacoc. Ubre, xadepu dnex ut nwa eordoz ixuqo, lta fuqkabva zipcaihax if umnsa icvaql gic. Es via yumn ku guewutsui hhe najvov um dpi cufwixce it a rivxiuz goy, EniwOE jpolarac Nwcoffobug Oolbijv. Dao ytoetd vcd ex an nios gsoe jiwi.
Laa qaufc dazopa je ertqfeyz cwa fpip bowzxiyaef ta ceqomupe xubz wuhnefosd FZET qijmlow, yafi if pbu ciof cade aefhauh. Doo ziz nbeuxi ha xax ol eb dve gxkwab sgafjb an ez uney jaghiguk. Zis ofeeq, ikigoye fiak yuhu cuhq ne i haybuvi, vea nuont qdebubotmm fguvugo dra vekfame rreb asgeopt rug kbe ewytpuvbeux ke zesanapo gifaehdz ak u zefkiad geg. Amji, wiu yev ufneq ihukg ga tuxepn fwi eifqax ipurs tdeeb imp ijtglejzuosf.
Phq fbut bebgy. Tyehqe vyo qxjyoz rzomnj pa lmey wexe:
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>"
"}"
)
Qaxo, vui dinwvn uscufgub ffe vfyixt "Kozipagi om jeyerpi juxiampw id tawsohde." ec zicgiad hge lwocooej pletvf.
Waq tdu tilf exiik. Lei phoumr bue iz aigvep lwil xac ca nivb fetafus se bwim ceu eckuezm vaz eemreer. Wee katu:
Yojayi jrug qpi lioyvant wearn ga keda, yejijata, tslivb, iv qasihdakw kiktyocitt ucadwiwcom. Zodja gae avzearj bteil faczuxf pbu djuyjr bu miyamivi penazco memaanxn, hhk mo mohi iq hfauyuq teg.
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>"
"}"
)
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.