A Python wrapper for the RecomPI API
Project description
RecomPI API
A Python wrapper for the RecomPI API, providing methods to interact with the service for tracking user behavior and obtaining recommendations.
Installation
Install the RecomPI library using pip:
pip install recompi
Usage
Initialization
Create an instance of the RecomPI
class using your campaign's API key.
from recompi import RecomPI
api = RecomPI(api_key="YOUR_CAMPAIGN_TOKEN")
Campaign Token: To obtain
YOUR_CAMPAIGN_TOKEN
, register on the RecomPI panel. After registration, add a campaign in the panel, and a campaign token will be generated instantly. Use this token as your API key in the code.
Pushing User Behavior
Use the push
method to send user interaction data to the RecomPI API. You can include tags, profiles, location, and geographical information.
from recompi import RecomPI, Tag, Profile, SecureProfile, Location, Geo
api = RecomPI(api_key="YOUR_CAMPAIGN_TOKEN")
response = api.push(
label="click",
tags=[
Tag(id="1", name="Technology", desc="Technology News"),
Tag(
id="2",
name="Online Marketing Tools",
desc="Latest news on online marketing tools",
),
],
profiles=Profile("user_id", "123"),
location=Location(
ip="1.1.1.1", # Optional
url="https://www.example.com/some/path?param1=1¶m2=2",
referer="REFERER_URL", # Optional
useragent="USERAGENT", # Optional
),
geo=Geo(
country="Iran", # Optional: 'IR' or the unique country ID
province="Tehran", # Optional: Any unique string
), # Optional
)
print(response) # [success: true]
Parameters
label
(str): A label for the event, e.g., "click".tags
(list[Tag]): List ofTag
objects representing metadata.profiles
(list[Profile|SecureProfile] or Profile|SecureProfile): List ofProfile
orSecureProfile
objects or a singleProfile
or a singleSecureProfile
.location
(Location, optional): ALocation
object containing IP, URL, referer, and user-agent information.geo
(Geo, optional): AGeo
object containing geographical information like country and province.
Getting Recommendations
Use the recom
method to get recommendations based on user interactions, profiles, and geographical information.
from recompi import RecomPI, Profile, Geo
api = RecomPI(api_key="YOUR_CAMPAIGN_TOKEN")
response = api.recom(
labels=["click", "buy"],
profiles=Profile("user_id", "123"),
geo=Geo(
country="Iran", # Optional: 'IR' or unique country ID
province="Tehran", # Optional: Any unique string
),
)
print(response) # [success: true] {'click': {'18': 0.25, '19': 0.75}, 'buy': {'13': 0.89, '95': 0.11}}
Parameters
labels
(list[str]): List of event labels for which recommendations are requested.profiles
(list[Profile|SecureProfile] or Profile|SecureProfile, optional): List ofProfile
orSecureProfile
objects or a singleProfile
or a singleSecureProfile
.geo
(Geo, optional): AGeo
object containing geographical information.
Explanation of Response:
The response structure ({'click': {'18': 0.25, '19': 0.75}, 'buy': {'13': 0.89, '95': 0.11}}
) indicates:
- For the
click
label, the user is predicted to engage more withTag.id = 19
(with a probability of0.75
) compared toTag.id = 18
(0.25
). - For the
buy
label, the user is likely to make a purchase withTag.id = 13
(0.89
probability) rather thanTag.id = 95
(0.11
probability).
These predictions are derived from the user's past interactions (click
and buy
events) and other factors such as their profile attributes and geographical location. The recommendation engine uses this data to suggest items or actions that are likely to be of interest to the user, helping to optimize engagement or conversions based on predictive analytics.
Verifying API Connectivity
Use the verify
method to check if the API key and configuration are correct.
from recompi import RecomPI
api = RecomPI(api_key="YOUR_CAMPAIGN_TOKEN")
response = api.verify()
print("API is well configured and connected?", response.is_success()) # [success: true]
Usage with SecureProfile
You can use SecureProfile
to ensure that sensitive information such as user IDs are hashed before being sent to the API. This adds an extra layer of security by obfuscating the actual IDs.
from recompi import RecomPI, Tag, SecureProfile, Location, Geo
api = RecomPI(api_key="YOUR_CAMPAIGN_TOKEN", hash_salt="SOME_HASH_SALT")
response = api.push(
label="click",
tags=[
Tag(id="1", name="Technology", desc="Technology News"),
Tag(
id="2",
name="Online Marketing Tools",
desc="Latest news on online marketing tools",
),
],
profiles=SecureProfile(name="user_id", id="123"),
location=Location(
ip="1.1.1.1", # Optional
url="https://www.example.com/some/path?param1=1¶m2=2",
referer="REFERER_URL", # Optional
useragent="USERAGENT", # Optional
),
geo=Geo(
country="Iran", # Optional: 'IR' or the unique country ID
province="Tehran", # Optional: Any unique string
), # Optional
)
print(response) # [success: true]
Parameters
label
(str): A label for the event, e.g., "click".tags
(list[Tag]): List ofTag
objects representing metadata.profiles
(list[Profile|SecureProfile] or Profile|SecureProfile): List ofProfile
orSecureProfile
objects or a singleProfile
or a singleSecureProfile
.location
(Location, optional): ALocation
object containing IP, URL, referer, and user-agent information.geo
(Geo, optional): AGeo
object containing geographical information like country and province.
Additional Details:
- Initialization:
SecureProfile
is initialized withname
andid
. It extends the baseProfile
to add the capability of hashing theid
. to_json
Method: This method converts theSecureProfile
instance to a JSON-compatible dictionary, hashing theid
if ahash_salt
is provided. This is useful for securely sending profile data.recom
: If you useSecureProfile
in thepush
method, you must also useSecureProfile
to retrieve data with therecom
method. This ensures the consistency of hashed data between sending and retrieving.
Data Structures
Tag
Represents a tag with an ID, name, and description.
from recompi import Tag
tag = Tag(id="1", name="Technology", desc="Technology News")
Profile
Represents a user profile with an ID and name.
from recompi import Profile
profile = Profile(name="user_id", id="123")
print(profile.to_json()) # {'user_id': '123'}
SecureProfile
Represents a user profile with a secure ID and name.
from recompi import SecureProfile
profile = SecureProfile(name="user_id", id="123")
print(profile.to_json()) # {'user_id': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'}
print(profile.to_json("SOME_HASH_SALT")) # {'user_id': 'e6ee87b7300073f85bc86d817b6656d58443b23438faacc6737bde461ecf38cd'}
Location
Represents a location with IP, URL, referer, and user-agent information.
from recompi import Location
location = Location(ip="1.1.1.1", url="https://www.example.com", referer="REFERER_URL", useragent="USERAGENT")
Geo
Represents geographical information with country and province.
from recompi import Geo
geo = Geo(country="Iran", province="Tehran")
Error Handling
RecomPIFieldTypeError
Raised when an input field does not match the expected type.
from recompi import RecomPIFieldTypeError
try:
# Some operation that might raise an error
pass
except RecomPIFieldTypeError as e:
print(e)
RecomPIException
General exception class for other RecomPI errors.
from recompi import RecomPIException
try:
# Some operation that might raise an error
pass
except RecomPIException as e:
print(e)
Contributing and Development
We welcome contributions to RecomPI! If you'd like to contribute, please follow these steps:
- Fork the repository and clone it to your local environment.
- Install dependencies and set up a development environment.
python3 -m venv venv
source venv/bin/activate
pip install -r requirements-dev.txt
pre-commit install --hook-type pre-commit --hook-type pre-push
- Make changes, write tests, and ensure all tests pass.
- Submit a pull request with a detailed description of your changes.
Support
For support or questions, please submit a ticket or open an issue on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file recompi-2.0.20.tar.gz
.
File metadata
- Download URL: recompi-2.0.20.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcd3002059df4ec5c9e4990ca39a9c62a1ec596cea8bdf2190d0c40f8b4466b9 |
|
MD5 | 6a7c59ef59beb6541dab0f746e8b0323 |
|
BLAKE2b-256 | 2c37387bcaf4d8ea8bb21d5e66ffbbd85a55efa3f97161bcac5c87e584196dcf |
File details
Details for the file recompi-2.0.20-py3-none-any.whl
.
File metadata
- Download URL: recompi-2.0.20-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6423e292e8e419ec2f5dea3b2217d3a68f9d31483b1e8fd9dfaafe6e582b4928 |
|
MD5 | 01b0f7d5397ab737e6a0fb4494cc032f |
|
BLAKE2b-256 | 68f81e8cbf4da2f638e7b1baf4e987baf9119204d16f13ba1fc1cae78c4e6672 |