Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
olgavrou committed Oct 20, 2023
1 parent 8ea4093 commit 4b7da76
Show file tree
Hide file tree
Showing 10 changed files with 1,365 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
*.egg-info
32 changes: 32 additions & 0 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
Do Not Translate or Localize

This project incorporates components from the projects listed below.

1. langchain (https://github.com/langchain-ai/langchain)

langchain NOTICES AND INFORMATION BEGIN HERE
=========================================
The MIT License

Copyright (c) Harrison Chase

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=========================================
END OF langchain NOTICES AND INFORMATION
147 changes: 147 additions & 0 deletions notebooks/demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"! pip install ../\n",
"! pip install matplotlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import learn_to_pick"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class fake_llm_caller:\n",
" def predict(self, message):\n",
" return \"5\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pick = learn_to_pick.PickBest.create(llm=fake_llm_caller(), metrics_step=5, metrics_window_size=5)\n",
"random_pick = learn_to_pick.PickBest.create(\n",
" llm=fake_llm_caller(),\n",
" metrics_step=5,\n",
" metrics_window_size=5, # rolling window average\n",
" policy=learn_to_pick.PickBestRandomPolicy # set the random policy instead of default\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# four meals defined, some vegetarian some not\n",
"\n",
"meals = [\n",
" \"Beef Enchiladas with Feta cheese. Mexican-Greek fusion\",\n",
" \"Chicken Flatbreads with red sauce. Italian-Mexican fusion\",\n",
" \"Veggie sweet potato quesadillas with vegan cheese\",\n",
" \"One-Pan Tortelonni bake with peppers and onions\",\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO function hook to call LLM between predict and learn?\n",
"# TODO how to pass extra args to scorer? How to get LLMs response back to user and in scorer?\n",
"# TODO slates from the get go?\n",
"for _ in range(100):\n",
" r = pick.run(meal = learn_to_pick.ToSelectFrom(meals),\n",
" user = learn_to_pick.BasedOn(\"Anna\"),\n",
" preference = learn_to_pick.BasedOn([\"Meat eater\", \"loves beef\"]),\n",
" text_to_personalize = \"This is the weeks specialty dish, our master chefs \\\n",
" believe you will love it!\",)\n",
"\n",
" r = pick.run(meal = learn_to_pick.ToSelectFrom(meals),\n",
" user = learn_to_pick.BasedOn(\"Tom\"),\n",
" preference = learn_to_pick.BasedOn([\"Vegetarian\", \"regular dairy is ok\"]),\n",
" text_to_personalize = \"This is the weeks specialty dish, our master chefs \\\n",
" believe you will love it!\",)\n",
" \n",
" r = random_pick.run(meal = learn_to_pick.ToSelectFrom(meals),\n",
" user = learn_to_pick.BasedOn(\"Anna\"),\n",
" preference = learn_to_pick.BasedOn([\"Meat eater\", \"loves beef\"]),\n",
" text_to_personalize = \"This is the weeks specialty dish, our master chefs \\\n",
" believe you will love it!\",)\n",
"\n",
" r = random_pick.run(meal = learn_to_pick.ToSelectFrom(meals),\n",
" user = learn_to_pick.BasedOn(\"Tom\"),\n",
" preference = learn_to_pick.BasedOn([\"Vegetarian\", \"regular dairy is ok\"]),\n",
" text_to_personalize = \"This is the weeks specialty dish, our master chefs \\\n",
" believe you will love it!\",)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"pick.metrics.to_pandas()['score'].plot(label=\"vw learning policy\")\n",
"random_pick.metrics.to_pandas()['score'].plot(label=\"random learning policy\")\n",
"plt.legend()\n",
"\n",
"print(f\"VW, calculated over a rolling window, is: {pick.metrics.to_pandas()['score'].iloc[-1]}\")\n",
"print(f\"Random, calculated over a rolling window, is: {random_pick.metrics.to_pandas()['score'].iloc[-1]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(r[\"picked_metadata\"].selected.score)\n",
"print(r[\"picked\"])\n",
"print(r[\"picked_metadata\"].to_select_from)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
21 changes: 21 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from setuptools import setup, find_packages

setup(
name="learn_to_pick",
version="0.1",
install_requires=[
'numpy',
'pandas',
'vowpal-wabbit-next',
'sentence-transformers',
'torch',
'pyskiplist',
'parameterfree',
],
author="VowpalWabbit",
description="",
packages=find_packages(where="src"),
package_dir={"": "src"},
url="https://github.com/VowpalWabbit/learn_to_pick",
python_requires='>=3.8',
)
54 changes: 54 additions & 0 deletions src/learn_to_pick/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging

from learn_to_pick.base import (
AutoSelectionScorer,
BasedOn,
Embed,
Embedder,
Policy,
SelectionScorer,
ToSelectFrom,
VwPolicy,
embed,
stringify_embedding,
)
from learn_to_pick.pick_best import (
PickBest,
PickBestEvent,
PickBestFeatureEmbedder,
PickBestRandomPolicy,
PickBestSelected,
)


def configure_logger() -> None:
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
ch.setFormatter(formatter)
ch.setLevel(logging.INFO)
logger.addHandler(ch)


configure_logger()

__all__ = [
"PickBest",
"PickBestEvent",
"PickBestSelected",
"PickBestFeatureEmbedder",
"PickBestRandomPolicy",
"Embed",
"BasedOn",
"ToSelectFrom",
"SelectionScorer",
"AutoSelectionScorer",
"Embedder",
"Policy",
"VwPolicy",
"embed",
"stringify_embedding",
]
Loading

0 comments on commit 4b7da76

Please sign in to comment.