Package: pyprimed
pyprimed: a python library to manage PrimedIOCreate a personalized web application that is unique and relevant for each and every user withPrimed.io.Installationpip install pyprimedQuickstartImport the SDK and initiate the connectionfrompyprimed.pioimportPiopio=Pio(uri='http://<user>:<password>@<api_url>:<port>')Create a Universe, and attach a few Targets# create a new universe and attach a single targetpio\.universes\.create(name='myfirstuniverse')\.targets\.upsert([{'key':'ARTICLE-1','value':{'url':'www.example.com/article-1'}}])# retrieving the newly created universeu=pio.universes.filter(name='myfirstuniverse').first# list all targets currently associated with this universefortargetinu.targets.all():print(target.key,target.created_at)# prepare a list of new targetsnew_targets=[{'key':'ARTICLE-2','value':{'url':'www.example.com/article-2'}},{'key':'ARTICLE-3','value':{'url':'www.example.com/article-3'}}]# upsert the new targetsu.targets.upsert(new_targets)# targets are upserted, which means that for a given key there# can be only one instance in the database. Trying to create an# instance with the same key will update the value of the record# in the databaseu.targets.upsert([{'key':'ARTICLE-1','value':{'url':'THIS IS NEW!'}}])u.targets.filter(key='ARTICLE-1').first.valueCreate a Model, and attach a few Signals# create a new model and attach a single signalpio\.models\.create(name='myfirstmodel')\.signals\.upsert([{'key':'ALICE'}])# retrieving the created modelm=pio.models.filter(name='myfirstmodel').first# list all signals currently associated with this modelforsignalinm.signals.all():print(signal.key,signal.created_at)# prepare a list of new signalsnew_signals=[{'key':'BOB'},{'key':'CHRIS'}]# create the new signalsm.signals.upsert(new_signals)# prepare a set of predictions (sk stand for signal.key, and tk for target.key)# WARNING: `sk` and `tk` should always be a string!predictions=[{'sk':'ALICE','tk':'ARTICLE-1','score':0.35},{'sk':'BOB','tk':'ARTICLE-1','score':0.75},{'sk':'CHRIS','tk':'ARTICLE-1','score':0.15}]# create the new predictionsu=pio.universes.filter(name='myfirstuniverse').firstpio\.predictions\.on(model=m,universe=u)\.upsert(predictions,asynchronous=False)# the `asynchronous=False` settings waits for this operation to end before# continuingCreate a Campaign, Experiment and set up an AB test to start using the Predictionsfrompyprimed.models.abvariantimportCustomAbvariant,RandomControlAbvariant,NullControlAbvariant# we create a custom abvariant that blends models m1 and m2 using a 60%/40% weight ratioab0=CustomAbvariant(label='A',models={m1:0.6,m2:0.4})ab1=RandomControlAbvariant(label='B')ab2=NullControlAbvariant(label='C')# we attach the abvariants to the experiment# `ab0` will receive 80% of traffic, `ab1` and `ab2`# receive 10% eache.abvariants.create({ab0:0.8,ab1:0.1,ab2:0.1})res=c.personalise(pubkey='mypubkey',secretkey='mysecretkey',signals={'userid':'BOB'},abvariant='A')# abvariant with label 'A' will be returnedUpdate Experimentfrompyprimed.models.abvariantimportCustomAbvariant,RandomControlAbvariant,NullControlAbvariant# obtain existing experimente=c.experiments.filter(name="myexperiment").first()# change propertye.salt="new_salt"e.update()# update abvariantsab1=CustomAbvariant(label="NEWLABEL",{m1:0.33,m2:0.77})ab2=RandomControlAbvariant(label="NEWRANDOMCONTROL")e.abvariants.update({ab1:0.5,ab2:0.5})DeveloperBuild the documentation:cd docs && pydocmd buildPreview documentation onhttp://localhost:8000cd docs && pydocmd serve