版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
Agents
Authors:JuliaWiesinger,PatrickMarlowandVladimirVuskovic
Agents
Acknowledgements
ReviewersandContributors
EvanHuangEmilyXue
OlcanSercinogluSebastianRiedelSatinderBavejaAntonioGulliAnantNawalgaria
CuratorsandEditors
AntonioGulliAnantNawalgariaGraceMollison
TechnicalWriter
JoeyHaymaker
Designer
MichaelLanning
September2024 2
Endnotes
Tableofcontents
Introduction 4
Whatisanagent? 5
Themodel 6
Thetools 7
Theorchestrationlayer 7
Agentsvs.models 8
Cognitivearchitectures:Howagentsoperate 8
Tools:Ourkeystotheoutsideworld 12
Extensions 13
SampleExtensions 15
Functions 18
Usecases 21
Functionsamplecode 24
Datastores 27
Implementationandapplication 28
Toolsrecap 32
Enhancingmodelperformancewithtargetedlearning 33
AgentquickstafiwithLangChain 35
ProductionapplicationswithVefiexAIagents 38
Summary 40
42
Agents
September2024
PAGE
20
Thiscombinationofreasoning,logic,andaccesstoexternalinformationthatareallconnectedtoaGenerativeAImodelinvokestheconceptofanagent.
Introduction
Humansarefantasticatmessypatternrecognitiontasks.However,theyoftenrelyontools
-likebooks,GoogleSearch,oracalculator-tosupplementtheirpriorknowledgebeforearrivingataconclusion.Justlikehumans,GenerativeAImodelscanbetrainedtousetoolstoaccessreal-timeinformationorsuggestareal-worldaction.Forexample,amodelcanleverageadatabaseretrievaltooltoaccessspecificinformation,likeacustomer'spurchasehistory,soitcangeneratetailoredshoppingrecommendations.Alternatively,basedonauser'squery,amodelcanmakevariousAPIcallstosendanemailresponsetoacolleagueorcompleteafinancialtransactiononyourbehalf.Todoso,themodelmustnotonlyhaveaccesstoasetofexternaltools,itneedstheabilitytoplanandexecuteanytaskinaself-directedfashion.Thiscombinationofreasoning,logic,andaccesstoexternalinformationthatareallconnectedtoaGenerativeAImodelinvokestheconceptofanagent,oraprogramthatextendsbeyondthestandalonecapabilitiesofaGenerativeAImodel.Thiswhitepaperdivesintoalltheseandassociatedaspectsinmoredetail.
Whatisanagent?
Initsmostfundamentalform,aGenerativeAIagentcanbedefinedasanapplicationthatattemptstoachieveagoalbyobservingtheworldandactinguponitusingthetoolsthatithasatitsdisposal.Agentsareautonomousandcanactindependentlyofhumanintervention,especiallywhenprovidedwithpropergoalsorobjectivestheyaremeanttoachieve.Agentscanalsobeproactiveintheirapproachtoreachingtheirgoals.Evenintheabsenceofexplicitinstructionsetsfromahuman,anagentcanreasonaboutwhatitshoulddonexttoachieveitsultimategoal.WhilethenotionofagentsinAIisquitegeneralandpowerful,thiswhitepaperfocusesonthespecifictypesofagentsthatGenerativeAImodelsarecapableofbuildingatthetimeofpublication.
Inordertounderstandtheinnerworkingsofanagent,let’sfirstintroducethefoundationalcomponentsthatdrivetheagent’sbehavior,actions,anddecisionmaking.Thecombinationofthesecomponentscanbedescribedasacognitivearchitecture,andtherearemanysucharchitecturesthatcanbeachievedbythemixingandmatchingofthesecomponents.Focusingonthecorefunctionalities,therearethreeessentialcomponentsinanagent’scognitivearchitectureasshowninFigure1.
Figure1.Generalagentarchitectureandcomponents
Themodel
Inthescopeofanagent,amodelreferstothelanguagemodel(LM)thatwillbeutilizedasthecentralizeddecisionmakerforagentprocesses.ThemodelusedbyanagentcanbeoneormultipleLM’sofanysize(small/large)thatarecapableoffollowinginstructionbasedreasoningandlogicframeworks,likeReAct,Chain-of-Thought,orTree-of-Thoughts.Modelscanbegeneralpurpose,multimodalorfine-tunedbasedontheneedsofyourspecificagentarchitecture.Forbestproductionresults,youshouldleverageamodelthatbestfitsyourdesiredendapplicationand,ideally,hasbeentrainedondatasignaturesassociatedwiththetoolsthatyouplantouseinthecognitivearchitecture.It’simpofianttonotethatthemodelistypicallynottrainedwiththespecificconfigurationsettings(i.e.toolchoices,orchestration/reasoningsetup)oftheagent.However,it’spossibletofufiherrefinethemodelfortheagent’stasksbyprovidingitwithexamplesthatshowcasetheagent’scapabilities,includinginstancesoftheagentusingspecifictoolsorreasoningstepsinvariouscontexts.
Thetools
Foundationalmodels,despitetheirimpressivetextandimagegeneration,remainconstrainedbytheirinabilitytointeractwiththeoutsideworld.Toolsbridgethisgap,empoweringagentstointeractwithexternaldataandserviceswhileunlockingawiderrangeofactionsbeyondthatoftheunderlyingmodelalone.Toolscantakeavarietyofformsandhavevarying
depthsofcomplexity,buttypicallyalignwithcommonwebAPImethodslikeGET,POST,PATCH,andDELETE.Forexample,atoolcouldupdatecustomerinformationinadatabaseorfetchweatherdatatoinfluenceatravelrecommendationthattheagentisprovidingtotheuser.Withtools,agentscanaccessandprocessreal-worldinformation.Thisempowersthemtosuppofimorespecializedsystemslikeretrievalaugmentedgeneration(RAG),whichsignificantlyextendsanagent’scapabilitiesbeyondwhatthefoundationalmodelcanachieveonitsown.We’lldiscusstoolsinmoredetailbelow,butthemostimpofiantthing
tounderstandisthattoolsbridgethegapbetweentheagent’sinternalcapabilitiesandtheexternalworld,unlockingabroaderrangeofpossibilities.
Theorchestrationlayer
Theorchestrationlayerdescribesacyclicalprocessthatgovernshowtheagenttakesininformation,performssomeinternalreasoning,andusesthatreasoningtoinformitsnextactionordecision.Ingeneral,thisloopwillcontinueuntilanagenthasreacheditsgoalorastoppingpoint.Thecomplexityoftheorchestrationlayercanvarygreatlydependingontheagentandtaskit’sperforming.Someloopscanbesimplecalculationswithdecisionrules,whileothersmaycontainchainedlogic,involveadditionalmachinelearningalgorithms,orimplementotherprobabilisticreasoningtechniques.We’lldiscussmoreaboutthedetailedimplementationoftheagentorchestrationlayersinthecognitivearchitecturesection.
Agentsvs.models
Togainaclearerunderstandingofthedistinctionbetweenagentsandmodels,considerthefollowingchafi:
Models
Agents
Knowledgeislimitedtowhatisavailableintheirtrainingdata.
Knowledgeisextendedthroughtheconnectionwithexternalsystemsviatools
Singleinference/predictionbasedontheuserquery.Unlessexplicitlyimplementedforthemodel,thereisnomanagementofsession
historyorcontinuouscontext.(i.e.chathistory)
Managedsessionhistory(i.e.chathistory)toallowformultiturninference/predictionbasedonuserqueriesanddecisionsmadeintheorchestrationlayer.Inthiscontext,a‘turn’isdefinedasaninteractionbetweentheinteractingsystemandtheagent.(i.e.1incomingevent/queryand1agentresponse)
Nonativetoolimplementation.
Toolsarenativelyimplementedinagentarchitecture.
Nonativelogiclayerimplemented.Userscanformpromptsassimplequestionsorusereasoningframeworks(CoT,ReAct,etc.)toformcomplexpromptstoguidethemodelinprediction.
NativecognitivearchitecturethatusesreasoningframeworkslikeCoT,ReAct,orotherpre-builtagentframeworkslikeLangChain.
Cognitivearchitectures:Howagentsoperate
Imagineachefinabusykitchen.Theirgoalistocreatedeliciousdishesforrestaurantpatronswhichinvolvessomecycleofplanning,execution,andadjustment.
Theygatherinformation,likethepatron’sorderandwhatingredientsareinthepantryandrefrigerator.
Theyperformsomeinternalreasoningaboutwhatdishesandflavorprofilestheycancreatebasedontheinformationtheyhavejustgathered.
Theytakeactiontocreatethedish:choppingvegetables,blendingspices,searingmeat.
Ateachstageintheprocessthechefmakesadjustmentsasneeded,refiningtheirplanasingredientsaredepletedorcustomerfeedbackisreceived,andusesthesetofpreviousoutcomestodeterminethenextplanofaction.Thiscycleofinformationintake,planning,executing,andadjustingdescribesauniquecognitivearchitecturethatthechefemploystoreachtheirgoal.
Justlikethechef,agentscanusecognitivearchitecturestoreachtheirendgoalsbyiterativelyprocessinginformation,makinginformeddecisions,andrefiningnextactionsbasedonpreviousoutputs.Atthecoreofagentcognitivearchitecturesliestheorchestrationlayer,responsibleformaintainingmemory,state,reasoningandplanning.Itusestherapidlyevolvingfieldofpromptengineeringandassociatedframeworkstoguidereasoningandplanning,enablingtheagenttointeractmoreeffectivelywithitsenvironmentandcompletetasks.Researchintheareaofpromptengineeringframeworksandtaskplanningforlanguagemodelsisrapidlyevolving,yieldingavarietyofpromisingapproaches.Whilenotanexhaustivelist,theseareafewofthemostpopularframeworksandreasoningtechniquesavailableatthetimeofthispublication:
ReAct,apromptengineeringframeworkthatprovidesathoughtprocessstrategyforlanguagemodelstoReasonandtakeactiononauserquery,withorwithoutin-contextexamples.ReActpromptinghasshowntooutperformseveralSOTAbaselinesandimprovehumaninteroperabilityandtrustwofihinessofLLMs.
Chain-of-Thought(CoT),apromptengineeringframeworkthatenablesreasoningcapabilitiesthroughintermediatesteps.Therearevarioussub-techniquesofCoTincludingself-consistency,active-prompt,andmultimodalCoTthateachhavestrengthsandweaknessesdependingonthespecificapplication.
Tree-of-thoughts(ToT),,apromptengineeringframeworkthatiswellsuitedforexplorationorstrategiclookaheadtasks.Itgeneralizesoverchain-of-thoughtpromptingandallowsthemodeltoexplorevariousthoughtchainsthatserveasintermediatestepsforgeneralproblemsolvingwithlanguagemodels.
Agentscanutilizeoneoftheabovereasoningtechniques,ormanyothertechniques,tochoosethenextbestactionforthegivenuserrequest.Forexample,let’sconsideranagentthatisprogrammedtousetheReActframeworktochoosethecorrectactionsandtoolsfortheuserquery.Thesequenceofeventsmightgosomethinglikethis:
Usersendsquerytotheagent
AgentbeginstheReActsequence
Theagentprovidesaprompttothemodel,askingittogenerateoneofthenextReActstepsanditscorrespondingoutput:
Question:Theinputquestionfromtheuserquery,providedwiththeprompt
Thought:Themodel’sthoughtsaboutwhatitshoulddonext
Action:Themodel’sdecisiononwhatactiontotakenext
Thisiswheretoolchoicecanoccur
Forexample,anactioncouldbeoneof[Flights,Search,Code,None],wherethefirst3representaknowntoolthatthemodelcanchoose,andthelastrepresents“notoolchoice”
Actioninput:Themodel’sdecisiononwhatinputstoprovidetothetool(ifany)
Observation:Theresultoftheaction/actioninputsequence
Thisthought/action/actioninput/observationcouldrepeatN-timesasneeded
Finalanswer:Themodel’sfinalanswertoprovidetotheoriginaluserquery
TheReActloopconcludesandafinalanswerisprovidedbacktotheuser
Figure2.ExampleagentwithReActreasoningintheorchestrationlayer
AsshowninFigure2,themodel,tools,andagentconfigurationworktogethertoprovideagrounded,conciseresponsebacktotheuserbasedontheuser’soriginalquery.Whilethemodelcouldhaveguessedatananswer(hallucinated)basedonitspriorknowledge,itinsteadusedatool(Flights)tosearchforreal-timeexternalinformation.Thisadditional
informationwasprovidedtothemodel,allowingittomakeamoreinformeddecisionbasedonrealfactualdataandtosummarizethisinformationbacktotheuser.
Insummary,thequalityofagentresponsescanbetieddirectlytothemodel’sabilitytoreasonandactaboutthesevarioustasks,includingtheabilitytoselecttherighttools,andhowwellthattoolshasbeendefined.Likeachefcraftingadishwithfreshingredientsandattentivetocustomerfeedback,agentsrelyonsoundreasoningandreliableinformationtodeliveroptimalresults.Inthenextsection,we’lldiveintothevariouswaysagentsconnectwithfreshdata.
Tools:Ourkeystotheoutsideworld
Whilelanguagemodelsexcelatprocessinginformation,theylacktheabilitytodirectlyperceiveandinfluencetherealworld.Thislimitstheirusefulnessinsituationsrequiringinteractionwithexternalsystemsordata.Thismeansthat,inasense,alanguagemodelisonlyasgoodaswhatithaslearnedfromitstrainingdata.Butregardlessofhowmuch
datawethrowatamodel,theystilllackthefundamentalabilitytointeractwiththeoutsideworld.Sohowcanweempowerourmodelstohavereal-time,context-awareinteractionwithexternalsystems?Functions,Extensions,DataStoresandPluginsareallwaystoprovidethiscriticalcapabilitytothemodel.
Whiletheygobymanynames,toolsarewhatcreatealinkbetweenourfoundationalmodelsandtheoutsideworld.Thislinktoexternalsystemsanddataallowsouragenttoperformawidervarietyoftasksanddosowithmoreaccuracyandreliability.Forinstance,toolscanenableagentstoadjustsmafihomesettings,updatecalendars,fetchuserinformationfromadatabase,orsendemailsbasedonaspecificsetofinstructions.
Asofthedateofthispublication,therearethreeprimarytooltypesthatGooglemodelsareabletointeractwith:Extensions,Functions,andDataStores.Byequippingagentswithtools,weunlockavastpotentialforthemtonotonlyunderstandtheworldbutalsoactuponit,openingdoorstoamyriadofnewapplicationsandpossibilities.
Extensions
TheeasiestwaytounderstandExtensionsistothinkofthemasbridgingthegapbetweenanAPIandanagentinastandardizedway,allowingagentstoseamlesslyexecuteAPIs
regardlessoftheirunderlyingimplementation.Let’ssaythatyou’vebuiltanagentwithagoalofhelpingusersbookflights.YouknowthatyouwanttousetheGoogleFlightsAPItoretrieveflightinformation,butyou’renotsurehowyou’regoingtogetyouragenttomakecallstothisAPIendpoint.
Figure3.HowdoAgentsinteractwithExternalAPIs?
Oneapproachcouldbetoimplementcustomcodethatwouldtaketheincominguserquery,parsethequeryforrelevantinformation,thenmaketheAPIcall.Forexample,inaflightbookingusecaseausermightstate“IwanttobookaflightfromAustintoZurich.”Inthisscenario,ourcustomcodesolutionwouldneedtoextract“Austin”and“Zurich”asrelevantentitiesfromtheuserquerybeforeattemptingtomaketheAPIcall.Butwhathappensiftheusersays“IwanttobookaflighttoZurich”andneverprovidesadepafiurecity?TheAPIcallwouldfailwithouttherequireddataandmorecodewouldneedtobeimplementedinordertocatchedgeandcornercaseslikethis.Thisapproachisnotscalableandcouldeasilybreakinanyscenariothatfallsoutsideoftheimplementedcustomcode.
AmoreresilientapproachwouldbetouseanExtension.AnExtensionbridgesthegapbetweenanagentandanAPIby:
TeachingtheagenthowtousetheAPIendpointusingexamples.
TeachingtheagentwhatargumentsorparametersareneededtosuccessfullycalltheAPIendpoint.
Figure4.ExtensionsconnectAgentstoExternalAPIs
Extensionscanbecraftedindependentlyoftheagent,butshouldbeprovidedaspafioftheagent’sconfiguration.TheagentusesthemodelandexamplesatruntimetodecidewhichExtension,ifany,wouldbesuitableforsolvingtheuser’squery.ThishighlightsakeystrengthofExtensions,theirbuilt-inexampletypes,thatallowtheagenttodynamicallyselectthemostappropriateExtensionforthetask.
Figure5.1-to-manyrelationshipbetweenAgents,ExtensionsandAPIs
ThinkofthisthesamewaythatasoftwaredeveloperdecideswhichAPIendpointstousewhilesolvingandsolutioningforauser’sproblem.Iftheuserwantstobookaflight,thedevelopermightusetheGoogleFlightsAPI.Iftheuserwantstoknowwherethenearestcoffeeshopisrelativetotheirlocation,thedevelopermightusetheGoogleMapsAPI.Inthissameway,theagent/modelstackusesasetofknownExtensionstodecidewhichonewillbethebestfitfortheuser’squery.Ifyou’dliketoseeExtensionsinaction,youcantrythemoutontheGeminiapplicationbygoingtoSettings>Extensionsandthenenablinganyyouwouldliketotest.Forexample,youcouldenabletheGoogleFlightsextensionthenaskGemini“ShowmeflightsfromAustintoZurichleavingnextFriday.”
SampleExtensions
TosimplifytheusageofExtensions,Googleprovidessomeoutoftheboxextensionsthatcanbequicklyimpofiedintoyourprojectandusedwithminimalconfigurations.Forexample,theCodeInterpreterextensioninSnippet1allowsyoutogenerateandrunPythoncodefromanaturallanguagedescription.
Python
importvertexaiimportpprint
PROJECT_ID="YOUR_PROJECT_ID"
REGION="us-central1"vertexai.init(project=PROJECT_ID,location=REGION)
fromvertexai.preview.extensionsimportExtension
extension_code_interpreter=Extension.from_hub("code_interpreter")CODE_QUERY="""WriteapythonmethodtoinvertabinarytreeinO(n)time."""
response=extension_code_interpreter.execute(operation_id="generate_and_execute",operation_params={"query":CODE_QUERY}
)
print("GeneratedCode:")pprint.pprint({response['generated_code']})
#Theabovesnippetwillgeneratethefollowingcode.
```
GeneratedCode:
classTreeNode:
definit(self,val=0,left=None,right=None):self.val=val
self.left=leftself.right=right
Continuesnextpage...
Python
definvert_binary_tree(root):"""
Invertsabinarytree.Args:
root:Therootofthebinarytree.
Returns:
Therootoftheinvertedbinarytree.
"""
ifnotroot:
returnNone
#Swaptheleftandrightchildrenrecursivelyroot.left,root.right=
invert_binary_tree(root.right),invert_binary_tree(root.left)
returnroot
#Exampleusage:
#Constructasamplebinarytreeroot=TreeNode(4)
root.left=TreeNode(2)root.right=TreeNode(7)root.left.left=TreeNode(1)root.left.right=TreeNode(3)root.right.left=TreeNode(6)root.right.right=TreeNode(9)
#Invertthebinarytree
inverted_root=invert_binary_tree(root)
```
Snippet1.CodeInterpreterExtensioncangenerateandrunPythoncode
Tosummarize,Extensionsprovideawayforagentstoperceive,interact,andinfluencetheoutsideworldinamyriadofways.TheselectionandinvocationoftheseExtensionsisguidedbytheuseofExamples,allofwhicharedefinedaspafioftheExtensionconfiguration.
Functions
Intheworldofsoftwareengineering,functionsaredefinedasself-containedmodulesofcodethataccomplishaspecifictaskandcanbereusedasneeded.Whenasoftwaredeveloperiswritingaprogram,theywilloftencreatemanyfunctionstodovarioustasks.
Theywillalsodefinethelogicforwhentocallfunction_aversusfunction_b,aswellastheexpectedinputsandoutputs.
Functionsworkverysimilarlyintheworldofagents,butwecanreplacethesoftwaredeveloperwithamodel.AmodelcantakeasetofknownfunctionsanddecidewhentouseeachFunctionandwhatargumentstheFunctionneedsbasedonitsspecification.FunctionsdifferfromExtensionsinafewways,mostnotably:
AmodeloutputsaFunctionanditsarguments,butdoesn’tmakealiveAPIcall.
Functionsareexecutedontheclient-side,whileExtensionsareexecutedontheagent-side.
UsingourGoogleFlightsexampleagain,asimplesetupforfunctionsmightlookliketheexampleinFigure7.
Figure7.HowdofunctionsinteractwithexternalAPIs?
NotethatthemaindifferencehereisthatneithertheFunctionnortheagentinteractdirectlywiththeGoogleFlightsAPI.SohowdoestheAPIcallactuallyhappen?
Withfunctions,thelogicandexecutionofcallingtheactualAPIendpointisoffloadedawayfromtheagentandbacktotheclient-sideapplicationasseeninFigure8andFigure9below.Thisoffersthedevelopermoregranularcontrolovertheflowofdataintheapplication.TherearemanyreasonswhyaDevelopermightchoosetousefunctionsoverExtensions,butafewcommonusecasesare:
APIcallsneedtobemadeatanotherlayeroftheapplicationstack,outsideofthedirectagentarchitectureflow(e.g.amiddlewaresystem,afrontendframework,etc.)
SecurityorAuthenticationrestrictionsthatpreventtheagentfromcallinganAPIdirectly(e.gAPIisnotexposedtotheinternet,ornon-accessiblebyagentinfrastructure)
Timingororder-of-operationsconstraintsthatpreventtheagentfrommakingAPIcallsinreal-time.(i.e.batchoperations,human-in-the-loopreview,etc.)
AdditionaldatatransformationlogicneedstobeappliedtotheAPIResponsethattheagentcannotperform.Forexample,consideranAPIendpointthatdoesn’tprovideafilteringmechanismforlimitingthenumberofresultsreturned.UsingFunctionsontheclient-sideprovidesthedeveloperadditionaloppofiunitiestomakethesetransformations.
ThedeveloperwantstoiterateonagentdevelopmentwithoutdeployingadditionalinfrastructurefortheAPIendpoints(i.e.FunctionCallingcanactlike“stubbing”ofAPIs)
WhilethedifferenceininternalarchitecturebetweenthetwoapproachesissubtleasseeninFigure8,theadditionalcontrolanddecoupleddependencyonexternalinfrastructuremakesFunctionCallinganappealingoptionfortheDeveloper.
Figure8.Delineatingclientvs.agentsidecontrolforextensionsandfunctioncalling
Usecases
Amodelcanbeusedtoinvokefunctionsinordertohandlecomplex,client-sideexecutionflowsfortheenduser,wheretheagentDevelopermightnotwantthelanguagemodeltomanagetheAPIexecution(asisthecasewithExtensions).Let’sconsiderthefollowingexamplewhereanagentisbeingtrainedasatravelconciergetointeractwithusersthatwanttobookvacationtrips.Thegoalistogettheagenttoproducealistofcitiesthatwecanuseinourmiddlewareapplicationtodownloadimages,data,etc.fortheuser’stripplanning.Ausermightsaysomethinglike:
I’dliketotakeaskitripwithmyfamilybutI’mnotsurewheretogo.
Inatypicalprompttothemodel,theoutputmightlooklikethefollowing:Sure,here’salistofcitiesthatyoucanconsiderforfamilyskitrips:
CrestedButte,Colorado,USA
Whistler,BC,Canada
Zermatt,Switzerland
Whiletheaboveoutputcontainsthedatathatweneed(citynames),theformatisn’tidealforparsing.WithFunctionCalling,wecanteachamodeltoformatthisoutputinastructuredstyle(likeJSON)that’smoreconvenientforanothersystemtoparse.Giventhesameinputpromptfromtheuser,anexampleJSONoutputfromaFunctionmightlooklikeSnippet
5instead.
Unset
function_call{
name:"display_cities"args:{
"cities":["CrestedButte","Whistler","Zermatt"],"preferences":"skiing"
}
}
Snippet5.SampleFunctionCallpayloadfordisplayingalistofcitiesanduserpreferences
ThisJSONpayloadisgeneratedbythemodel,andthensenttoourClient-sideservertodowhateverwewouldliketodowithit.Inthisspecificcase,we’llcalltheGooglePlacesAPItotakethecitiesprovidedbythemodelandlookupImages,thenprovidethemasformattedrichcontentbacktoourUser.ConsiderthissequencediagraminFigure9showingtheaboveinteractioninstepbystepdetail.
Figure9.SequencediagramshowingthelifecycleofaFunctionCall
TheresultoftheexampleinFigure9isthatthemodelisleveragedto“fillintheblanks”withtheparametersrequiredfortheClientsideUItomakethecalltotheGooglePlacesAPI.TheClientsideUImanagestheactualAPIcallusingtheparametersprovidedbythemodelinthereturnedFunction.ThisisjustoneusecaseforFunctionCalling,buttherearemanyotherscenariostoconsiderlike:
Youwantalanguagemodeltosuggestafunctionthatyoucanuseinyourcode,butyoudon'twanttoincludecredentialsinyourcode.Becausefunctioncallingdoesn'trunthefunction,youdon'tneedtoincludecredentialsinyourcodewiththefunctioninformation.
Youarerunningasynchronousoperationsthatcantakemorethanafewseconds.Thesescenariosworkwellwithfunctioncallingbecauseit'sanasynchronousoperation.
Youwanttorunfunctionsonadevicethat'sdifferentfromthesystemproducingthefunctioncallsandtheirarguments.
OnekeythingtorememberaboutfunctionsisthattheyaremeanttoofferthedevelopermuchmorecontrolovernotonlytheexecutionofAPIcalls,butalsotheentireflowofdataintheapplicationasawhole.IntheexampleinFigure9,thedeveloperchosetonotreturnAPIinformationbacktotheagentasitwasnotpefiinentforfutureactionstheagentmighttake.However,basedonthearchitectureoftheapplication,itmaymakesensetoreturntheexternalAPIcalldatatotheagentinordertoinfluencefuturereasoning,logic,andactionchoices.Ultimately,itisuptotheapplicationdevelopertochoosewhatisrightforthespecificapplication.
Functionsamplecode
Toachievetheaboveoutputfromourskivacationscenario,let’sbuildouteachofthecomponentstomakethisworkwithourgemini-1.5-flash-001model.
First,we’lldefineourdisplay_citiesfunctionasasimplePythonmethod.
Python
defdisplay_cities(cities:list[str],preferences:Optional[str]=None):"""Providesalistofcitiesbasedontheuser'ssearchqueryandpreferences.
Args:
preferences(str):Theuser'spreferencesforthesearch,likeskiing,beach,restaurants,bbq,etc.
cities(list[str]):Thelistofcitiesbeingrecommendedtotheuser.
Returns:
list[str]:Thelistofcitiesbeingrecommendedtotheuser.
"""
returncities
Snippet6.Samplepythonmethodforafunctionthatwilldisplayalistofcities.
Next,we’llinstantiateourmodel,buildtheTool,thenpassinouruser’squeryandtoolstothemodel.Executingthecodebelowwouldresultintheoutputasseenatthebottomofthecodesni
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 文庫發(fā)布:技術紙手袋
- 護理操作中的無菌技術規(guī)范再培訓
- (2026年)鼻空腸營養(yǎng)管的護理課件
- 2025年全國低壓電工作業(yè)證理論考試題庫(含答案)
- 某發(fā)動機廠色譜儀管理辦法
- 2026美術教師資格考試題庫及答案
- 婦幼保健院培訓
- 司磅工崗前班組評比考核試卷含答案
- 婦女營養(yǎng)保健培訓
- 玻纖織布帶工持續(xù)改進模擬考核試卷含答案
- 云南省昆明市2026屆高三三診一模摸底診斷測試化學試卷(含答案)
- 2026年1月浙江省高考(首考)化學試題(含標準答案及解析)
- 建筑施工行業(yè)2026年春節(jié)節(jié)前全員安全教育培訓
- YDT 2323-2016 通信配電系統(tǒng)電能質量補償設備
- GB/T 44082-2024道路車輛汽車列車多車輛間連接裝置強度要求
- 控煙中醫(yī)科普知識講座
- GB/T 23986.2-2023色漆和清漆揮發(fā)性有機化合物(VOC)和/或半揮發(fā)性有機化合物(SVOC)含量的測定第2部分:氣相色譜法
- 脫碳塔CO2脫氣塔設計計算
- 產品報價單貨物報價表(通用版)
- 皰疹性咽峽炎臨床路徑
- 新人教版六年級數(shù)學上冊全冊堂堂清一課一練習題集
評論
0/150
提交評論