unique_toolkit 0.5.43__tar.gz → 0.5.45__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.
Files changed (50) hide show
  1. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/CHANGELOG.md +4 -0
  2. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/PKG-INFO +6 -2
  3. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/pyproject.toml +2 -2
  4. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/app/verification.py +9 -8
  5. unique_toolkit-0.5.45/unique_toolkit/short_term_memory/schemas.py +49 -0
  6. unique_toolkit-0.5.45/unique_toolkit/short_term_memory/service.py +57 -0
  7. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/LICENSE +0 -0
  8. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/README.md +0 -0
  9. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/__init__.py +0 -0
  10. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/_common/_base_service.py +0 -0
  11. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/_common/_time_utils.py +0 -0
  12. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/_common/exception.py +0 -0
  13. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/_common/validators.py +0 -0
  14. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/app/__init__.py +0 -0
  15. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/app/init_logging.py +0 -0
  16. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/app/init_sdk.py +0 -0
  17. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/app/performance/async_tasks.py +0 -0
  18. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/app/performance/async_wrapper.py +0 -0
  19. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/app/schemas.py +0 -0
  20. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/chat/__init__.py +0 -0
  21. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/chat/schemas.py +0 -0
  22. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/chat/service.py +0 -0
  23. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/chat/state.py +0 -0
  24. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/chat/utils.py +0 -0
  25. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/content/__init__.py +0 -0
  26. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/content/schemas.py +0 -0
  27. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/content/service.py +0 -0
  28. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/content/utils.py +0 -0
  29. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/embedding/__init__.py +0 -0
  30. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/embedding/schemas.py +0 -0
  31. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/embedding/service.py +0 -0
  32. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/embedding/utils.py +0 -0
  33. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/config.py +0 -0
  34. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/context_relevancy/constants.py +0 -0
  35. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/context_relevancy/prompts.py +0 -0
  36. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/context_relevancy/service.py +0 -0
  37. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/context_relevancy/utils.py +0 -0
  38. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/exception.py +0 -0
  39. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/hallucination/constants.py +0 -0
  40. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/hallucination/prompts.py +0 -0
  41. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/hallucination/service.py +0 -0
  42. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/hallucination/utils.py +0 -0
  43. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/output_parser.py +0 -0
  44. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/evaluators/schemas.py +0 -0
  45. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/language_model/__init__.py +0 -0
  46. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/language_model/infos.py +0 -0
  47. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/language_model/prompt.py +0 -0
  48. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/language_model/schemas.py +0 -0
  49. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/language_model/service.py +0 -0
  50. {unique_toolkit-0.5.43 → unique_toolkit-0.5.45}/unique_toolkit/language_model/utils.py +0 -0
@@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.5.45] - 2025-01-03
9
+ - Added `ShortTermMemoryService` class to handle short term memory.
8
10
 
11
+ ## [0.5.44] - 2024-12-18
12
+ - Add `event_constructor` to `verify_signature_and_construct_event` to allow for custom event construction.
9
13
 
10
14
  ## [0.5.43] - 2024-12-13
11
15
  - Add `Prompt` class to handle templated prompts that can be formatted into LanguageModelSystemMessage and LanguageModelUserMessage.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: unique_toolkit
3
- Version: 0.5.43
3
+ Version: 0.5.45
4
4
  Summary:
5
5
  License: Proprietary
6
6
  Author: Martin Fadler
@@ -17,7 +17,7 @@ Requires-Dist: python-dotenv (>=1.0.1,<2.0.0)
17
17
  Requires-Dist: regex (>=2024.5.15,<2025.0.0)
18
18
  Requires-Dist: tiktoken (>=0.7.0,<0.8.0)
19
19
  Requires-Dist: typing-extensions (>=4.9.0,<5.0.0)
20
- Requires-Dist: unique-sdk (>=0.9.14,<0.10.0)
20
+ Requires-Dist: unique-sdk (>=0.9.17,<0.10.0)
21
21
  Description-Content-Type: text/markdown
22
22
 
23
23
  # Unique Toolkit
@@ -100,7 +100,11 @@ All notable changes to this project will be documented in this file.
100
100
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
101
101
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
102
102
 
103
+ ## [0.5.45] - 2025-01-03
104
+ - Added `ShortTermMemoryService` class to handle short term memory.
103
105
 
106
+ ## [0.5.44] - 2024-12-18
107
+ - Add `event_constructor` to `verify_signature_and_construct_event` to allow for custom event construction.
104
108
 
105
109
  ## [0.5.43] - 2024-12-13
106
110
  - Add `Prompt` class to handle templated prompts that can be formatted into LanguageModelSystemMessage and LanguageModelUserMessage.
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "unique_toolkit"
3
- version = "0.5.43"
3
+ version = "0.5.45"
4
4
  description = ""
5
5
  authors = [
6
6
  "Martin Fadler <martin.fadler@unique.ch>",
@@ -20,7 +20,7 @@ numpy = "^1.26.4"
20
20
  python-dotenv = "^1.0.1"
21
21
  regex = "^2024.5.15"
22
22
  tiktoken = "^0.7.0"
23
- unique-sdk = "^0.9.14"
23
+ unique-sdk = "^0.9.17"
24
24
 
25
25
  [tool.poetry.group.dev.dependencies]
26
26
  ruff = "0.5.0"
@@ -1,4 +1,5 @@
1
1
  import logging
2
+ from typing import Callable, TypeVar
2
3
 
3
4
  import unique_sdk
4
5
 
@@ -11,12 +12,16 @@ class WebhookVerificationError(Exception):
11
12
  pass
12
13
 
13
14
 
15
+ T = TypeVar("T")
16
+
17
+
14
18
  def verify_signature_and_construct_event(
15
19
  headers: dict[str, str],
16
20
  payload: bytes,
17
21
  endpoint_secret: str,
18
22
  logger: logging.Logger = logging.getLogger(__name__),
19
- ):
23
+ event_constructor: Callable[..., T] = Event,
24
+ ) -> T:
20
25
  """
21
26
  Verify the signature of a webhook and construct an event object.
22
27
 
@@ -25,18 +30,14 @@ def verify_signature_and_construct_event(
25
30
  payload (bytes): The raw payload of the webhook request.
26
31
  endpoint_secret (str): The secret used to verify the webhook signature.
27
32
  logger (logging.Logger): A logger instance for logging messages.
28
-
33
+ event_constructor (Callable[..., T]): A callable that constructs an event object.
29
34
  Returns:
30
- Union[Event, Tuple[Dict[str, bool], int]]:
31
- If successful, returns an Event object.
32
- If unsuccessful, returns a tuple with an error response and HTTP status code.
35
+ T: The constructed event object.
33
36
 
34
37
  Raises:
35
38
  WebhookVerificationError: If there's an error during verification or event construction.
36
39
  """
37
40
 
38
- # Only verify the event if there is an endpoint secret defined
39
- # Otherwise use the basic event deserialized with json
40
41
  sig_header = headers.get("X-Unique-Signature")
41
42
  timestamp = headers.get("X-Unique-Created-At")
42
43
 
@@ -52,7 +53,7 @@ def verify_signature_and_construct_event(
52
53
  endpoint_secret,
53
54
  )
54
55
  logger.info("✅ Webhook signature verification successful.")
55
- return Event(**event)
56
+ return event_constructor(**event)
56
57
  except unique_sdk.SignatureVerificationError as e:
57
58
  logger.error("⚠️ Webhook signature verification failed. " + str(e))
58
59
  raise WebhookVerificationError(f"Signature verification failed: {str(e)}")
@@ -0,0 +1,49 @@
1
+ import json
2
+
3
+ from humps import camelize
4
+ from pydantic import BaseModel, ConfigDict, Field, field_validator
5
+
6
+ model_config = ConfigDict(
7
+ alias_generator=camelize,
8
+ populate_by_name=True,
9
+ arbitrary_types_allowed=True,
10
+ )
11
+
12
+
13
+ class ShortTermMemory(BaseModel):
14
+ model_config = model_config
15
+
16
+ id: str
17
+ key: str = Field(alias="object")
18
+ chat_id: str | None
19
+ message_id: str | None
20
+ data: str | dict | int | float | bool | list | None
21
+
22
+ @field_validator("chat_id", "message_id", mode="before")
23
+ def validate_chat_id_or_message_id(cls, v, info):
24
+ field_name = info.field_name
25
+ data = info.data
26
+
27
+ # Get the other field's value
28
+ other_field = "message_id" if field_name == "chat_id" else "chat_id"
29
+ other_value = data.get(other_field)
30
+
31
+ # Check if both are None
32
+ if v is None and other_value is None:
33
+ camel_name = camelize(field_name)
34
+ raise ValueError(
35
+ f"Either {camel_name} or messageId must be provided"
36
+ if field_name == "chat_id"
37
+ else f"Either chatId or {camel_name} must be provided"
38
+ )
39
+
40
+ return v
41
+
42
+ @field_validator("data", mode="before")
43
+ def validate_data(cls, v):
44
+ if isinstance(v, str):
45
+ try:
46
+ return json.loads(v)
47
+ except json.JSONDecodeError:
48
+ return v
49
+ return v
@@ -0,0 +1,57 @@
1
+ import json
2
+
3
+ from unique_sdk.api_resources._short_term_memory import (
4
+ ShortTermMemory as ShortTermMemoryAPI,
5
+ )
6
+
7
+ from unique_toolkit.app import Event
8
+
9
+ from .schemas import ShortTermMemory
10
+
11
+
12
+ class ShortTermMemoryService:
13
+ def __init__(
14
+ self,
15
+ user_id: str,
16
+ company_id: str,
17
+ chat_id: str | None,
18
+ message_id: str | None = None,
19
+ ):
20
+ assert chat_id or message_id, "Either chat_id or message_id must be provided"
21
+ self.user_id = user_id
22
+ self.company_id = company_id
23
+ self.chat_id = chat_id
24
+ self.message_id = message_id
25
+
26
+ @classmethod
27
+ def from_chat_event(cls, chat_event: Event) -> "ShortTermMemoryService":
28
+ return cls(
29
+ user_id=chat_event.user_id,
30
+ company_id=chat_event.company_id,
31
+ chat_id=chat_event.payload.chat_id,
32
+ message_id=chat_event.payload.user_message.id,
33
+ )
34
+
35
+ async def get(self, key: str) -> ShortTermMemory:
36
+ stm = await ShortTermMemoryAPI.find_latest_async(
37
+ user_id=self.user_id,
38
+ company_id=self.company_id,
39
+ memoryName=key,
40
+ chatId=self.chat_id,
41
+ messageId=self.message_id,
42
+ )
43
+
44
+ return ShortTermMemory(**stm)
45
+
46
+ async def set(self, key: str, value: str | dict):
47
+ if isinstance(value, dict):
48
+ value = json.dumps(value)
49
+ stm = await ShortTermMemoryAPI.create_async(
50
+ user_id=self.user_id,
51
+ company_id=self.company_id,
52
+ memoryName=key,
53
+ chatId=self.chat_id,
54
+ messageId=self.message_id,
55
+ data=value,
56
+ )
57
+ return ShortTermMemory(**stm)
File without changes