Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/.env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EPPO_API_KEY='<insert SDK key here from https://eppo.cloud/feature-flags/keys>'
4 changes: 3 additions & 1 deletion example/main.py → example/01_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def init_eppo_and_assign():
subject = "user_1234"
flag_key = "my-flag-key"

assigned_variation = eppo.get_assignment(subject, flag_key)
assigned_variation = eppo.get_string_assignment(
flag_key, subject, subject_attributes={}, default="control"
)
if assigned_variation == "control":
print("Assigned to control")
elif assigned_variation == "treatment":
Expand Down
40 changes: 40 additions & 0 deletions example/02_fastapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Run using
`fastapi dev 02_fastapi.py`

Test using
`curl "http://127.0.0.1:8000/hello?name=bob"`
"""

from dotenv import load_dotenv
import os

from fastapi import FastAPI
import eppo_client
from eppo_client.config import Config, AssignmentLogger

load_dotenv()
EPPO_API_KEY = os.environ.get("EPPO_API_KEY")
print(EPPO_API_KEY[:5] + "...")

app = FastAPI()


class LocalLogger(AssignmentLogger):
def log_assignment(self, assignment_event):
print(assignment_event)


client_config = Config(api_key=EPPO_API_KEY, assignment_logger=LocalLogger())

eppo_client.init(client_config)


@app.get("/hello")
async def hello(name: str):
client = eppo_client.get_instance()

print(client.get_flag_keys())
greeting = client.get_string_assignment("hello-world-greeting", name, {}, "Hello")

return f"{greeting}, {name}!"
67 changes: 67 additions & 0 deletions example/03_bandit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Run using
`fastapi dev 03_bandit.py`

Test using
`curl "http://127.0.0.1:8000/bandit?name=bob&country=UK&age=25"`
"""

from dotenv import load_dotenv
import os

from fastapi import FastAPI
import eppo_client
from eppo_client.config import Config, AssignmentLogger
import eppo_client.bandit

load_dotenv()
EPPO_API_KEY = os.environ.get("EPPO_API_KEY")
print(EPPO_API_KEY[:5] + "...")

app = FastAPI()


class LocalLogger(AssignmentLogger):
def log_assignment(self, assignment_event):
print(assignment_event)

def log_bandit_assignment(self, bandit_assignment):
print(bandit_assignment)


client_config = Config(api_key=EPPO_API_KEY, assignment_logger=LocalLogger())

eppo_client.init(client_config)


@app.get("/bandit")
async def bandit(name: str, country: str, age: int):
client = eppo_client.get_instance()

print(client.get_flag_keys())

bandit_result = client.get_bandit_action(
"shoe-bandit",
name,
eppo_client.bandit.Attributes(
numeric_attributes={"age": age}, categorical_attributes={"country": country}
),
[
eppo_client.bandit.ActionContext.create(
"nike",
numeric_attributes={"brand_affinity": 2.3},
categorical_attributes={"aspect_ratio": "16:9"},
),
eppo_client.bandit.ActionContext.create(
"adidas",
numeric_attributes={"brand_affinity": 0.2},
categorical_attributes={"aspect_ratio": "16:9"},
),
],
"control",
)

if bandit_result.action:
return f"The bandit recommends {bandit_result.action} to {name}"

return f"{name} was assigned to {bandit_result.variation}"
35 changes: 32 additions & 3 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Running the Eppo SDK example
# Running the Eppo SDK examples
Before running the example, you need to install the dependencies for the Eppo SDK.

From the root of the repo, install the necessary requirements:
Expand All @@ -8,9 +8,38 @@ source venv/bin/activate
pip install -r requirements.txt
```

Set your API key in `main.py`:
Note that all these examples query feature flags set up within the Eppo UI.
Make sure to set up flags prior to running the examples and adjusting the scripts accordingly.


## 01: Local script

This is the simplest example that shows how to use the Eppo SDK. Set your API key in `01_script.py`:
```
API_KEY = "[replace with your API key]"
```

Run the example
Then run the example:
```
python 01_script.py
```

## 02: FastAPI

Generally, Eppo is configured as part of a server. This example shows how to use the Eppo SDK in a FastAPI server.
To follow best practices, create a .env file by copying .env.dist and put your API key in it.
We will use `python-dotenv` to read the SDK key from the .env file.

Then run the example:
```
fastapi dev 02_fastapi.py
```

## 03: Bandit

This example builds on the previous FastAPI example but instead of showing a simple feature flag, it shows how to use Eppo's contextual bandits.

Make sure you have your Eppo API key set in `.env` as instructed above, then run the example:
```
fastapi dev 03_bandit.py
```
3 changes: 3 additions & 0 deletions example/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eppo-server-sdk==3.1.0
python-dotenv
fastapi