behaviorgpt 0.6.0__tar.gz → 0.6.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: behaviorgpt
3
- Version: 0.6.0
3
+ Version: 0.6.2
4
4
  Summary: Python client for BehaviorGPT, UnboxAI's Large Behavioral Model for search, recommendations and personalization
5
5
  Keywords: recommendations,personalization,search,behavioral model,unboxai
6
6
  Author: Unbox AI
@@ -119,7 +119,7 @@ client.add_items("new_items.parquet", catalog_id=job.catalog_id, wait=True)
119
119
  client.list_catalogs()
120
120
  client.delete_catalog("cat_...")
121
121
 
122
- client.add_keywords(job.catalog_id, wait=True) # about 2 hours per 20,000 items
122
+ client.add_keywords(job.catalog_id, wait=True) # 12 minutes for 19,000 items
123
123
  for section in client.sections([Search("coat")], catalog_id=job.catalog_id).sections:
124
124
  print(section.title, [item.data["name"] for item in section.items])
125
125
  ```
@@ -102,7 +102,7 @@ client.add_items("new_items.parquet", catalog_id=job.catalog_id, wait=True)
102
102
  client.list_catalogs()
103
103
  client.delete_catalog("cat_...")
104
104
 
105
- client.add_keywords(job.catalog_id, wait=True) # about 2 hours per 20,000 items
105
+ client.add_keywords(job.catalog_id, wait=True) # 12 minutes for 19,000 items
106
106
  for section in client.sections([Search("coat")], catalog_id=job.catalog_id).sections:
107
107
  print(section.title, [item.data["name"] for item in section.items])
108
108
  ```
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "behaviorgpt"
3
- version = "0.6.0"
3
+ version = "0.6.2"
4
4
  description = "Python client for BehaviorGPT, UnboxAI's Large Behavioral Model for search, recommendations and personalization"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "behaviorgpt"
3
- version = "0.6.0"
3
+ version = "0.6.2"
4
4
  description = "Python client for BehaviorGPT, UnboxAI's Large Behavioral Model for search, recommendations and personalization"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,5 +1,7 @@
1
+ import asyncio
1
2
  import os
2
3
  import random
4
+ import time
3
5
  import warnings
4
6
  from collections import defaultdict
5
7
  from datetime import UTC, datetime, timedelta
@@ -9,7 +11,7 @@ from uuid import uuid4
9
11
 
10
12
  import httpx
11
13
 
12
- from behaviorgpt._exceptions import AuthenticationError
14
+ from behaviorgpt._exceptions import AuthenticationError, UnboxAIError
13
15
  from behaviorgpt.resources.catalogs import AsyncCatalogs, Catalogs
14
16
  from behaviorgpt.resources.recommendations import AsyncRecommender, Recommender
15
17
  from behaviorgpt.resources.requests import UnboxAIRequester
@@ -42,6 +44,7 @@ from behaviorgpt.types.http import RecommendationType
42
44
  DEFAULT_HTTP_TIMEOUT = httpx.Timeout(120.0, connect=10.0, write=300.0)
43
45
  DEFAULT_BASE_URL = "https://behaviorgpt-northeurope.api.unboxai.com/v1"
44
46
  DEFAULT_TRAILING_SEARCHES = 3
47
+ UMAP_POLL = 5.0
45
48
 
46
49
  Predict = Literal["view", "purchase"]
47
50
 
@@ -348,10 +351,11 @@ class UnboxAIClient(_BaseClient):
348
351
  time by default.
349
352
 
350
353
  `keywords=True` also generates the search keywords and browse
351
- categories `sections` groups items by. Allow a longer `timeout`: it
352
- adds time before the catalog is ready, about 2 hours per 20,000
353
- items. If it fails the catalog still goes live, without them. Off by
354
- default; `add_keywords` generates them for the catalog later.
354
+ categories `sections` groups items by, as `add_keywords` does by
355
+ default. Allow a longer `timeout`: it adds time before the catalog is
356
+ ready, 12 minutes for a 19,000-item catalog. If it fails the catalog
357
+ still goes live, without them. Off by default; `add_keywords`
358
+ generates them for the catalog later.
355
359
  """
356
360
  return self.catalogs.embed(
357
361
  Path(path),
@@ -403,7 +407,7 @@ class UnboxAIClient(_BaseClient):
403
407
  self,
404
408
  catalog_id: str,
405
409
  *,
406
- fast: bool = False,
410
+ fast: bool = True,
407
411
  wait: bool = False,
408
412
  interval: float = 5.0,
409
413
  timeout: float = 4 * 3600.0,
@@ -414,16 +418,16 @@ class UnboxAIClient(_BaseClient):
414
418
  groups items by, for a catalog embedded without them.
415
419
 
416
420
  Nothing is re-embedded, and the catalog keeps serving while the job
417
- runs; it takes about 2 hours per 20,000 items, so the default
418
- `timeout` of 4 hours covers about 35,000. Once the job is ready
421
+ runs; a 19,000-item catalog took 12 minutes. Once the job is ready
419
422
  the catalog serves with keywords. If it fails, the catalog stays as it
420
423
  was. Uploading the catalog again with `embed` replaces the keywords;
421
424
  pass `keywords=True` there to keep them.
422
425
 
423
- `fast=True` asks the language model once per group of about 16
424
- similar items instead of once per item: about 9 times faster, with
425
- fewer keywords per item (about 3 instead of 9), so sections have less
426
- to group by.
426
+ By default the language model is asked once per group of about 16
427
+ similar items. `fast=False` asks it once per item instead: each item
428
+ gets more keywords (about 9 instead of 3), so sections have more to
429
+ group by, but it is 6 to 10 times slower, 1 to 2 hours per 20,000
430
+ items; the default `timeout` of 4 hours covers at least 35,000.
427
431
 
428
432
  `wait` and the arguments after it work as for `embed`.
429
433
  """
@@ -498,7 +502,7 @@ class UnboxAIClient(_BaseClient):
498
502
  )
499
503
  return self.random_item(*args, **kwargs)
500
504
 
501
- def umap(self, *, catalog_id: Optional[str] = None):
505
+ def umap(self, *, catalog_id: Optional[str] = None, wait: float = 0.0) -> str:
502
506
  """The catalog's UMAP, a 2D map of its items, as an HTML page.
503
507
 
504
508
  Maps exist only for catalogs of up to 100,000 items, and appear within
@@ -506,9 +510,20 @@ class UnboxAIClient(_BaseClient):
506
510
  that does not fit in the embedding worker's memory, so larger catalogs
507
511
  (such as the shared `retail_catalog`) never get one. A missing map
508
512
  raises `UnboxAIError` with status 404 and says which case applies.
513
+
514
+ `wait` is how many seconds to keep asking while the map is missing,
515
+ for a catalog that just became ready. A catalog that never gets a map
516
+ raises only once that time is up.
509
517
  """
510
518
  active_catalog: str = catalog_id or self.default_catalog_id
511
- return self.catalogs.get_umap(catalog_id=active_catalog)
519
+ deadline = time.monotonic() + wait
520
+ while True:
521
+ try:
522
+ return self.catalogs.get_umap(catalog_id=active_catalog)
523
+ except UnboxAIError as err:
524
+ if err.status_code != 404 or time.monotonic() + UMAP_POLL > deadline:
525
+ raise
526
+ time.sleep(UMAP_POLL)
512
527
 
513
528
 
514
529
  class AsyncUnboxAIClient(_BaseClient):
@@ -658,7 +673,7 @@ class AsyncUnboxAIClient(_BaseClient):
658
673
  self,
659
674
  catalog_id: str,
660
675
  *,
661
- fast: bool = False,
676
+ fast: bool = True,
662
677
  wait: bool = False,
663
678
  interval: float = 5.0,
664
679
  timeout: float = 4 * 3600.0,
@@ -712,10 +727,17 @@ class AsyncUnboxAIClient(_BaseClient):
712
727
  top = await self.complete(history=[], catalog_id=catalog_id, limit=window)
713
728
  return _pick_random(top, catalog_id or self.default_catalog_id)
714
729
 
715
- async def umap(self, *, catalog_id: Optional[str] = None) -> str:
730
+ async def umap(self, *, catalog_id: Optional[str] = None, wait: float = 0.0) -> str:
716
731
  """The catalog's UMAP as an HTML page; see `UnboxAIClient.umap`."""
717
732
  active_catalog: str = catalog_id or self.default_catalog_id
718
- return await self.catalogs.get_umap(catalog_id=active_catalog)
733
+ deadline = time.monotonic() + wait
734
+ while True:
735
+ try:
736
+ return await self.catalogs.get_umap(catalog_id=active_catalog)
737
+ except UnboxAIError as err:
738
+ if err.status_code != 404 or time.monotonic() + UMAP_POLL > deadline:
739
+ raise
740
+ await asyncio.sleep(UMAP_POLL)
719
741
 
720
742
 
721
743
  def _pick_random(top: UnboxAIResponse, catalog_id: str) -> Item:
@@ -315,9 +315,9 @@ def keywords_path(catalog_id: str) -> str:
315
315
 
316
316
 
317
317
  def keywords_body(fast: bool) -> dict[str, Any]:
318
- """The keywords request's body: none for the server's default mode, so
319
- the request is the same as before modes existed."""
320
- return {"json": {"mode": "fast"}} if fast else {}
318
+ """The keywords request's body. It always names the mode, so the job
319
+ runs in the one asked for whatever the server's default."""
320
+ return {"json": {"mode": "fast" if fast else "product"}}
321
321
 
322
322
 
323
323
  def items_path(catalog_id: str) -> str:
@@ -541,7 +541,7 @@ class Catalogs:
541
541
  self,
542
542
  catalog_id: str,
543
543
  *,
544
- fast: bool = False,
544
+ fast: bool = True,
545
545
  wait: bool = False,
546
546
  interval: float = 5.0,
547
547
  timeout: float = 4 * 3600.0,
@@ -554,9 +554,9 @@ class Catalogs:
554
554
 
555
555
  The catalog keeps serving while the job runs and gets the keywords
556
556
  once it is ready. If the job fails, the catalog stays as it was.
557
- `fast` asks the language model once per group of similar items
558
- instead of once per item. `wait` and the arguments after it work as
559
- for `embed`.
557
+ `fast` (the default) asks the language model once per group of
558
+ similar items, `fast=False` once per item. `wait` and the arguments
559
+ after it work as for `embed`.
560
560
  """
561
561
  response = self._client.post(keywords_path(catalog_id), **keywords_body(fast))
562
562
  job = AddKeywordsJobDetails(**handle_response(response))
@@ -732,7 +732,7 @@ class AsyncCatalogs:
732
732
  self,
733
733
  catalog_id: str,
734
734
  *,
735
- fast: bool = False,
735
+ fast: bool = True,
736
736
  wait: bool = False,
737
737
  interval: float = 5.0,
738
738
  timeout: float = 4 * 3600.0,
File without changes