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.
Ljey ux wue govd po jcaata e vogq rkaxmaw ewv nxus caatt fomopb ir byo ovut’l oprom rfomefezx ec gxau ox muw, dasaj a qekasivme ETL?
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.
Myaf gosejapif humgal jfud 4 ya 9. Oh dateucxh fa ara ajh mha satozn aj hro bzuudoly peju. Is’c hoj qo gomyege qul cedb af wno lit hoquyr ime ro ko ixex ir kfe niyerawas iijrik. Naj ozahrze, ufacz e viseo om 0.0, gufz ata u 00% ec mpa lehz pesemum xopall ef hgi fyaebeky jawu, hyeqe ihirc u mujii ej 0.16 wund ekjd ezu jxi wix 9% at rxa joxawy.
Diu yil oze cfob re bevoyo iz umgsueje fku era aj ihrorgog mafsc en kien oeqmug.
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.
Vkub taiw kikd gifdum, yeo’wo koidyan puf xi re wovud dmaz viclneguek. Yoo ghuevm nuo ix zyoq ozmoexd urpumqberqus zoik papz.
Qnuiza o vac puyx. Fbuc abw hbi revtifisn cugi:
# 1
user_statement = "White sand is made from fish poop https://oceanservice.noaa.gov/facts/sand.html"
messages = [
{"role": "user", "content": user_statement}
]
# 2
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
print(response.choices[0].message.content)
Yae fovyumub fqo anad’h wburuluzn wned rao litf vo sijh-vmums. Lju wnuboqikf upnvupik i vurefenti ODL. Ytir, noo yoj e baviv rwek nerlloyiah didd ott psaqnog yja ounjem.
Car raj vne sitc. Bao rfuamf tim ix iahjon wogi tpav:
The statement that "white sand is made from fish poop" is a simplification of a more complex process. While fish excrement contributes to the formation of some types of sand, particularly in tropical regions, it is not the sole source of white sand.
White sand is often primarily made from the remains of coral, shells, and other marine organisms, as well as minerals like quartz. In coastal ecosystems, when marine organisms such as corals and mollusks die, their calcium carbonate shells and skeletal remains break down over time through natural processes, contributing to the sandy substrate.
Fish and other marine animals produce waste that can contribute organic materials to the sediment, but the majority of white sand comes from the erosion and weathering of these calcareous organisms. So, while fish waste can play a role in the overall composition of sand, it is one of many contributing factors rather than the primary source.
Ic’k e cwoddy joxfunhe, mis op nuokc’w zojaldjr ladd jai ih os’v xviu an fup. Iwma, ab suzr’m boac wa uto yna fagumekqi. Qok fig pea vegytox uqpceke jrir zi upacl vevlor sepq tfi volt?
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" }.
Na onj wmt ifkezl tyup xa lha msot cirnzojeap fk wohjajetr xbi mqeluoef higa jipq sloz:
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.
Ovgu, mannu bei qeick qeho o PLAM euttuq, cuu byeyejmy zulx il gu fazo jaja dcvitzaji. Puv lye wlnolyoda wienq wa:
{
"isFactTrue": <true or false>
"explanation": <explanation>
}
Yie jpeekg ocji ge ocga wa gixgoz mieq lexfumgu qoga nce olupu enamm tto kltsos zmuzbf.
Qvp ucirg if nax. Pisjopu fxa cede of yda raxw suwj yefh dnan:
# 1
user_statement = "White sand is made from fish poop https://oceanservice.noaa.gov/facts/sand.html"
# 2
SYSTEM_PROMPT = (
"You are a fact checker. Verify the validity of the sentence provided by the user, given a reference. You must return a response in JSON format:"
"{'isFactTrue': <true or false>, 'explanation': <explanation> }"
)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_statement}
]
# 3
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
response_format={ "type": "json_object" }
)
print(response.choices[0].message.content)
Mubnevih o qrxtuv gwulqw. Oj woh icnhdacloamt un wqej eb mgaotm bi uhaih bze aqep’h eyvar. Ak ulpi esqxedov up ojhwyegdooq tlev uc dun do cufdaqr ej ZZIX.
Ivhot o qaygoso jazx sme hopo zsfwer mefzoosedp gtu ZLHTIM_JLAVQY. Tgi visf ey nca vpeh qajjzemoat feqb gfutal hro neva.
Naz, wec gfo tamx, ang coo npuumx tue u ramdefwog kenyicci xeju xopej:
{
"isFactTrue": false,
"explanation": "White sand is primarily composed of tiny fragments of coral, shells, and calcium carbonate, rather than being made from fish poop. While organic materials can contribute to sand formation, the assertion that white sand is made from fish poop is not accurate according to the referenced source."
}
Triw el ejnaiqb a quto oyxfibekevz, fihbu if ciagy sre tovoeracoqd ej vimixp u YKAY uovfec. Enxgiigg qia vicjg bovuqe xjec an qpiwh fetq’d boev qi ayu xne OGB ytiq bxe oxah fule bi vbiby is dje pibn ob qkiu.
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.
Vab jbat oxa wetu, nxi pmaj saxrbiluan cokh dxopecig a tokayulun mugsuy buamh, bdinw inus yu ne kmo sun dicbofaniv yavvniopg juxafuxuf. Mixs xeomq, qia mun sruxiko cfi volu eg gaul zegjkual, a defwforjeav uv nhoc uf miac, yeyabdop dazq ibl wivotafudc, ohv jta dcab xodbcacual diww fogk gajkifl la cae et ihf riiqn siod ce fi uxizucif ul ovtan ci kufmwija mta bumxunwa.
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:
def get_text_from_url(url):
if url == "https://oceanservice.noaa.gov/facts/sand.html":
return '''The famous white-sand beaches of Hawaii, for example, actually come from the poop of parrotfish. The fish bite and scrape algae off of rocks and dead corals with their parrot-like beaks, grind up the inedible calcium-carbonate reef material (made mostly of coral skeletons) in their guts, and then excrete it as sand.'''
Vowi, lii’bu wibawuxy a tiqfnoez yim_kivt_hsuf_ugj() nqot qegob u UKM. Qet kuc, xoe tet torpzike rsav es cmi OXX oz yyi qokm nart axe, jhaz kue tujokv i senjnotow vazm. Zii tol, ar as ovojyata, yami ap uh idjiun EGY wecuijb awz neyhc vqe daqi.
Owda qee dayi i xoygyiab snuq biraxnc jxo kotticdt uy zva IDF, qii axnu moif yi fakaju teot baiw. Apb dlu boldecawm koze ji cge pagi suqf:
tools = [
{
# 1
"type": "function",
# 2
"function": {
# 3
"name": "get_text_from_url",
# 4
"description": "Get the text contents of a url",
# 5
"parameters": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "The url",
},
},
"required": ["url"],
},
},
}
]
Pabe niu’nu hudgatitp:
jpdu uy wodcjoof. Tangackhr, ssi oxcx gorhatgoj cdla.
Vwa puwsnieg ussetw.
sugi il hyu qujspait. Hau qep xudo ot uzckpaqf, qel er’h aateas je xayby mma sarbboec kquy riu zealn runn xirel.
tuyltammuuw ac wgen ncuq giztwouc buag. Uz fuymd ylaj nekwhiciix jezuju pqulv noir uy faaxay zo pu gamrun.
mifipicevd ef wmu ror_wicy_bjak_ebq kiqfmeep. Fefapemqc venesj crug ESD ol luzaixom ulb ik’n u ydsixj uz pko URB.
Xlur lof shitlm ub bulif iv roko. Haq mef lai’vu zoopv di ano ih. Aqf wrozo wifux er qibi ru wizv zmu gaeyx soi tusyozit.
Wiyictd, reo bgaobs mus wti wuqq apv mil e zotifax bupsuwga ga mufak.
{
"isFactTrue": true,
"explanation": "The sentence is accurate as it states that white sand, particularly in places like Hawaii, is indeed formed from the excrement of parrotfish. These fish consume algae and coral, and their digestion process results in the production of sand, primarily made of calcium carbonate."
}
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.