版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 能源管理師高級考試重點及實務操作含答案
- 產品部助理考試題含答案
- 職工服務專員基礎知識考試題庫含答案
- 2026年教師資格之小學教育學教育心理學考試題庫500道含答案【突破訓練】
- 2026年高校教師資格證之高等教育法規(guī)考試題庫附完整答案【各地真題】
- 2026年初級管理會計之專業(yè)知識考試題庫300道及答案(新)
- 2026年一級注冊建筑師之建筑物理與建筑設備考試題庫300道附答案【研優(yōu)卷】
- 2026年交管12123學法減分復習考試題庫及參考答案【模擬題】
- 供應鏈專員面試題集含答案
- 2026年交管12123學法減分復習考試題庫及答案(奪冠)
- 2025年煙花爆竹經(jīng)營單位安全管理人員考試試題及答案
- 2025天津大學管理崗位集中招聘15人參考筆試試題及答案解析
- 旋挖鉆機地基承載力驗算2017.7
- 小米員工管理手冊
- 自身免疫性肝病的診斷和治療
- 國家開放大學化工節(jié)能課程-復習資料期末復習題
- xx鄉(xiāng)鎮(zhèn)衛(wèi)生院重癥精神病管理流程圖
- 2023年印江縣人民醫(yī)院緊缺醫(yī)學專業(yè)人才招聘考試歷年高頻考點試題含答案解析
- 安徽綠沃循環(huán)能源科技有限公司12000t-a鋰離子電池高值資源化回收利用項目(重新報批)環(huán)境影響報告書
- 公路工程標準施工招標文件第八章-工程量清單計量規(guī)則(2018年版最終稿)
- DB44-T 2197-2019配電房運維服務規(guī)范-(高清現(xiàn)行)
評論
0/150
提交評論