管理primedio的python客户端
pyprimed的Python项目详细描述
pyprimed:管理primedio的python库
创建一个个性化的web应用程序,它是唯一的,并且与每个使用Primed.io的用户都相关。
安装
pip install pyprimed
快速启动
导入sdk并启动连接
frompyprimed.pioimportPiopio=Pio(uri='http://<user>:<password>@<api_url>:<port>')
创建一个宇宙,并附加几个目标
# 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.value
创建一个模型,并附加一些信号
# 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 predictions u=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 # continuing
创建一个活动,实验并设置ab测试,开始使用预测
frompyprimed.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 returned
更新实验
frompyprimed.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})
显影剂
制作文档:
cd docs && pydocmd build
的文档
cd docs && pydocmd serve