agentbyte 0.1.0__py3-none-any.whl

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.
agentbyte/__about__.py ADDED
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.0"
2
+ VERSION = __version__
agentbyte/__init__.py ADDED
@@ -0,0 +1,18 @@
1
+ """
2
+ Agentbyte - Enterprise-ready agent framework with DDD principles.
3
+
4
+ Building agents from scratch following Chapter 4 patterns with:
5
+ - Async-first architecture
6
+ - Event-based streaming
7
+ - Clean Architecture (Port/Adapter pattern)
8
+ - Domain-Driven Design
9
+
10
+ Architecture Layers:
11
+ - domain/ - Core business logic (entities, value objects, services)
12
+ - application/ - Use cases, ports (interfaces)
13
+ - infrastructure/ - Adapters, external integrations
14
+ """
15
+ from agentbyte.__about__ import VERSION
16
+
17
+ def main():
18
+ print("Agentbyte version:", VERSION)
@@ -0,0 +1,94 @@
1
+ """
2
+ LLM client implementations for Agentbyte framework.
3
+
4
+ This module provides abstract interfaces and implementations for
5
+ communicating with various LLM providers (OpenAI, Anthropic, etc.).
6
+
7
+ Core Components:
8
+ - BaseChatCompletionClient: Abstract base class for all LLM providers
9
+ - Message Types: SystemMessage, UserMessage, AssistantMessage, ToolMessage
10
+ - Response Types: ChatCompletionResult, ChatCompletionChunk
11
+ - Exception Types: ModelClientError, RateLimitError, AuthenticationError
12
+
13
+ Example:
14
+ from agentbyte.llm import (
15
+ BaseChatCompletionClient,
16
+ SystemMessage,
17
+ UserMessage
18
+ )
19
+
20
+ # Use with any concrete provider implementation
21
+ messages = [
22
+ SystemMessage(content="You are helpful."),
23
+ UserMessage(content="What is 2+2?")
24
+ ]
25
+ """
26
+
27
+ from .base import (
28
+ BaseChatCompletionClient,
29
+ BaseChatCompletionClientConfig,
30
+ RateLimitError,
31
+ AuthenticationError,
32
+ InvalidRequestError,
33
+ )
34
+ from .openai import OpenAIChatCompletionClient, OpenAIChatCompletionClientConfig
35
+ from .azure_openai import (
36
+ AzureOpenAIChatCompletionClient,
37
+ AzureOpenAIChatCompletionClientConfig,
38
+ )
39
+ from .auth import (
40
+ get_default_token_provider,
41
+ get_certificate_token_provider,
42
+ create_token_provider_from_string,
43
+ )
44
+ # Import message types and usage from root module (picoagents-aligned)
45
+ from agentbyte.messages import (
46
+ BaseMessage,
47
+ Message,
48
+ SystemMessage,
49
+ UserMessage,
50
+ AssistantMessage,
51
+ ToolMessage,
52
+ ToolCallRequest,
53
+ MultiModalMessage,
54
+ Usage,
55
+ )
56
+ # Import LLM response types from this module
57
+ from .types import (
58
+ ChatCompletionChunk,
59
+ ChatCompletionResult,
60
+ ModelClientError,
61
+ )
62
+
63
+ __all__ = [
64
+ # Base classes
65
+ "BaseChatCompletionClient",
66
+ "BaseChatCompletionClientConfig",
67
+ # Provider implementations
68
+ "OpenAIChatCompletionClient",
69
+ "OpenAIChatCompletionClientConfig",
70
+ "AzureOpenAIChatCompletionClient",
71
+ "AzureOpenAIChatCompletionClientConfig",
72
+ # Authentication utilities
73
+ "get_default_token_provider",
74
+ "get_certificate_token_provider",
75
+ "create_token_provider_from_string",
76
+ # Exception types
77
+ "ModelClientError",
78
+ "RateLimitError",
79
+ "AuthenticationError",
80
+ "InvalidRequestError",
81
+ # Message types (from agentbyte.messages)
82
+ "BaseMessage",
83
+ "Message",
84
+ "SystemMessage",
85
+ "UserMessage",
86
+ "AssistantMessage",
87
+ "ToolMessage",
88
+ "ToolCallRequest",
89
+ "MultiModalMessage",
90
+ # Result types
91
+ "Usage",
92
+ "ChatCompletionResult",
93
+ "ChatCompletionChunk",
94
+ ]
agentbyte/llm/auth.py ADDED
@@ -0,0 +1,184 @@
1
+ """
2
+ Authentication utility functions for creating token providers.
3
+
4
+ This module provides helper functions for setting up various authentication
5
+ methods with Azure OpenAI and other Azure services. Each function returns
6
+ a callable token provider that can be passed to AsyncAzureOpenAI.
7
+
8
+ Authentication Methods Supported:
9
+ 1. DefaultAzureCredential: Automatic credential detection (recommended for production)
10
+ 2. CertificateCredential: X.509 certificate-based authentication
11
+ 3. Direct Token: Pre-authenticated token for reuse
12
+ """
13
+
14
+ from typing import Callable
15
+
16
+
17
+ def get_default_token_provider() -> Callable[[], str]:
18
+ """
19
+ Create a token provider using DefaultAzureCredential.
20
+
21
+ DefaultAzureCredential automatically tries multiple authentication methods
22
+ in sequence (environment variables, managed identity, Azure CLI, etc.).
23
+ Recommended for production environments.
24
+
25
+ Returns:
26
+ Callable that provides a bearer token string
27
+
28
+ Raises:
29
+ ImportError: If azure-identity is not installed
30
+ azure.identity.CredentialUnavailableError: If no credentials found
31
+
32
+ Example:
33
+ >>> from agentbyte.llm import AzureOpenAIChatCompletionClient
34
+ >>> from agentbyte.llm.auth import get_default_token_provider
35
+ >>> from openai import AsyncAzureOpenAI
36
+ >>>
37
+ >>> token_provider = get_default_token_provider()
38
+ >>> azure_client = AsyncAzureOpenAI(
39
+ ... azure_endpoint="https://myresource.openai.azure.com/",
40
+ ... azure_ad_token_provider=token_provider,
41
+ ... api_version="2024-10-21"
42
+ ... )
43
+ >>> llm = AzureOpenAIChatCompletionClient(
44
+ ... model="gpt-4",
45
+ ... client=azure_client
46
+ ... )
47
+ """
48
+ try:
49
+ from azure.identity import DefaultAzureCredential, get_bearer_token_provider
50
+ except ImportError:
51
+ raise ImportError(
52
+ "azure-identity is required for DefaultAzureCredential. "
53
+ "Install with: pip install azure-identity"
54
+ )
55
+
56
+ credentials = DefaultAzureCredential()
57
+ token_provider = get_bearer_token_provider(
58
+ credentials, "https://cognitiveservices.azure.com/.default"
59
+ )
60
+ return token_provider
61
+
62
+
63
+ def get_certificate_token_provider(
64
+ tenant_id: str,
65
+ client_id: str,
66
+ certificate_data: bytes,
67
+ scope: str = "https://cognitiveservices.azure.com/.default",
68
+ ) -> Callable[[], str]:
69
+ """
70
+ Create a token provider using X.509 certificate authentication.
71
+
72
+ Certificate-based authentication is ideal for:
73
+ - Service principals in automated systems
74
+ - Non-interactive authentication with strong security
75
+ - Scenarios where you need deterministic auth (no user interaction)
76
+
77
+ Args:
78
+ tenant_id: Azure AD tenant ID
79
+ client_id: Service principal client ID (application ID)
80
+ certificate_data: Certificate data in PEM format (bytes)
81
+ Can include private key and certificate chain
82
+ scope: Token scope (default: cognitiveservices scope for Azure OpenAI)
83
+
84
+ Returns:
85
+ Callable that provides a bearer token string
86
+
87
+ Raises:
88
+ ImportError: If azure-identity is not installed
89
+ ValueError: If certificate_data is invalid
90
+ azure.identity.CredentialUnavailableError: If authentication fails
91
+
92
+ Example:
93
+ >>> from agentbyte.llm.auth import get_certificate_token_provider
94
+ >>> from openai import AsyncAzureOpenAI
95
+ >>> import os
96
+ >>>
97
+ >>> # Load certificate from environment or file
98
+ >>> cert_data = os.environ["PRIVATE_CERT_KEY"].encode()
99
+ >>>
100
+ >>> token_provider = get_certificate_token_provider(
101
+ ... tenant_id=os.environ["TENANT_ID"],
102
+ ... client_id=os.environ["CLIENT_ID"],
103
+ ... certificate_data=cert_data
104
+ ... )
105
+ >>>
106
+ >>> azure_client = AsyncAzureOpenAI(
107
+ ... azure_endpoint="https://myresource.openai.azure.com/",
108
+ ... azure_ad_token_provider=token_provider,
109
+ ... api_version="2024-10-21"
110
+ ... )
111
+ """
112
+ try:
113
+ from azure.identity import CertificateCredential
114
+ except ImportError:
115
+ raise ImportError(
116
+ "azure-identity is required for CertificateCredential. "
117
+ "Install with: pip install azure-identity"
118
+ )
119
+
120
+ credential = CertificateCredential(
121
+ tenant_id=tenant_id,
122
+ client_id=client_id,
123
+ certificate_data=certificate_data,
124
+ )
125
+
126
+ def token_provider() -> str:
127
+ """Get a fresh token from the certificate credential."""
128
+ token = credential.get_token(scope)
129
+ return token.token
130
+
131
+ return token_provider
132
+
133
+
134
+ def create_token_provider_from_string(
135
+ token: str,
136
+ ) -> Callable[[], str]:
137
+ """
138
+ Create a simple token provider from a pre-authenticated token.
139
+
140
+ Useful for:
141
+ - Testing and development
142
+ - Using pre-generated tokens
143
+ - Scenarios where you manage token lifecycle externally
144
+
145
+ Args:
146
+ token: Azure AD bearer token string
147
+
148
+ Returns:
149
+ Callable that always returns the provided token
150
+
151
+ Warning:
152
+ This approach has limitations:
153
+ - Token must be manually refreshed before expiration
154
+ - No automatic token refresh
155
+ - Use get_default_token_provider() for production instead
156
+
157
+ Example:
158
+ >>> from agentbyte.llm.auth import create_token_provider_from_string
159
+ >>> from openai import AsyncAzureOpenAI
160
+ >>> import os
161
+ >>>
162
+ >>> # Get token from environment (e.g., from CI/CD system)
163
+ >>> token = os.environ["AZURE_AD_TOKEN"]
164
+ >>>
165
+ >>> token_provider = create_token_provider_from_string(token)
166
+ >>> azure_client = AsyncAzureOpenAI(
167
+ ... azure_endpoint="https://myresource.openai.azure.com/",
168
+ ... azure_ad_token_provider=token_provider,
169
+ ... api_version="2024-10-21"
170
+ ... )
171
+ """
172
+
173
+ def token_provider() -> str:
174
+ """Return the static token."""
175
+ return token
176
+
177
+ return token_provider
178
+
179
+
180
+ __all__ = [
181
+ "get_default_token_provider",
182
+ "get_certificate_token_provider",
183
+ "create_token_provider_from_string",
184
+ ]