Skip to content

Commit

Permalink
F/sdk verb update (#40)
Browse files Browse the repository at this point in the history
* feat(python-sdk): use patch methods

* feat(ts-sdk): use patch methods

* feat(python-sdk): add overwrite tests

* feat(ts-sdk): add overwrite tests

* wip(python-sdk): Fix python sdk fixtures

Signed-off-by: Diwank Singh Tomer <[email protected]>

* wip: metadata in sdk (#39)

* feat(agents-api): Make user_id optional when creating a new session

Signed-off-by: Diwank Singh Tomer <[email protected]>

* wip: added metadata arg to sdk methods

Signed-off-by: Diwank Singh Tomer <[email protected]>

---------

Signed-off-by: Diwank Singh Tomer <[email protected]>

* wip(python-sdk): Make user tests pass

Signed-off-by: Diwank Singh Tomer <[email protected]>

* feat(python-sdk): update tests

* fix: remove unused deps

---------

Signed-off-by: Diwank Singh Tomer <[email protected]>
Co-authored-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
philipbalbas and creatorrr authored Apr 16, 2024
1 parent 0c503d9 commit f488389
Show file tree
Hide file tree
Showing 33 changed files with 753 additions and 591 deletions.
2 changes: 1 addition & 1 deletion agents-api/agents_api/common/protocol/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class AgentDefaultSettings(BaseModel):
"""Penalty that decreases the likelihood of frequently used words in the agent's responses."""
frequency_penalty: float = 0.0
"""Minimum probability threshold for including a word in the agent's response."""
min_p: float = 0.01
min_p: float = 0.01
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/create_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ def create_agent_query(
"metadata": metadata,
"instructions": instructions,
},
)
)
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/patch_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def patch_agent_query(
settings_cols, settings_vals = cozo_process_mutate_data(
{
**default_settings,
"agent_id": agent_id,
"agent_id": str(agent_id),
}
)

Expand Down
23 changes: 20 additions & 3 deletions agents-api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agents-api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cozo-migrate = "^0.2.0"
poethepoet = "^0.24.4"
pytype = ">=2024.4.11"
julep = "^0.2.4"
pyjwt = "^2.8.0"

[build-system]
requires = ["poetry-core"]
Expand Down
4 changes: 3 additions & 1 deletion sdks/python/julep/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Dict, Optional
from urllib.parse import urlparse

from beartype import beartype
Expand Down Expand Up @@ -81,6 +81,7 @@ def __init__(
api_key: Optional[str] = JULEP_API_KEY,
base_url: Optional[str] = JULEP_API_URL,
timeout: int = 300,
additional_headers: Dict[str, str] = {},
_httpx_client: Optional[httpx.Client] = None,
*args,
**kwargs,
Expand Down Expand Up @@ -115,6 +116,7 @@ def __init__(
# Create an httpz client that follows redirects and has a timeout
httpx_client = _httpx_client or httpx.Client(
timeout=timeout,
headers=additional_headers,
follow_redirects=True,
)

Expand Down
61 changes: 47 additions & 14 deletions sdks/python/julep/managers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .utils import rewrap_in_class

from .base import BaseManager
from .utils import is_valid_uuid4
from .utils import is_valid_uuid4, NotSet
from .types import (
ToolDict,
FunctionDefDict,
Expand All @@ -31,6 +31,7 @@
## TYPES ##
###########


ModelName = Literal[
"julep-ai/samantha-1",
"julep-ai/samantha-1-turbo",
Expand All @@ -39,13 +40,14 @@

class AgentCreateArgs(TypedDict):
name: str
about: str
instructions: List[str]
about: Optional[str]
instructions: Optional[List[str]]
tools: List[ToolDict] = []
functions: List[FunctionDefDict] = []
default_settings: DefaultSettingsDict = {}
model: ModelName = "julep-ai/samantha-1-turbo"
docs: List[DocDict] = []
metadata: Dict[str, Any] = {}


class AgentUpdateArgs(TypedDict):
Expand All @@ -54,6 +56,8 @@ class AgentUpdateArgs(TypedDict):
name: Optional[str] = None
model: Optional[str] = None
default_settings: Optional[DefaultSettingsDict] = None
metadata: Optional[Dict[str, Any]] = None
overwrite: bool = False


class BaseAgentsManager(BaseManager):
Expand All @@ -73,7 +77,7 @@ class BaseAgentsManager(BaseManager):
Returns:
The agent object or an awaitable that resolves to the agent object.
_create(self, name: str, about: str, instructions: List[str], tools: List[ToolDict] = [], functions: List[FunctionDefDict] = [], default_settings: DefaultSettingsDict = {}, model: ModelName = 'julep-ai/samantha-1-turbo', docs: List[DocDict] = []) -> Union[ResourceCreatedResponse, Awaitable[ResourceCreatedResponse]]:
_create(self, name: str, about: str, instructions: List[str], tools: List[ToolDict] = [], functions: List[FunctionDefDict] = [], default_settings: DefaultSettingsDict = {}, model: ModelName = 'julep-ai/samantha-1-turbo', docs: List[DocDict] = [], metadata: Dict[str, Any] = {}) -> Union[ResourceCreatedResponse, Awaitable[ResourceCreatedResponse]]:
Creates an agent with the given specifications.
Args:
name (str): The name of the new agent.
Expand All @@ -84,6 +88,7 @@ class BaseAgentsManager(BaseManager):
default_settings (DefaultSettingsDict, optional): Dictionary of default settings for the new agent. Defaults to an empty dictionary.
model (ModelName, optional): The model name for the new agent. Defaults to 'julep-ai/samantha-1-turbo'.
docs (List[DocDict], optional): List of document dictionaries for the new agent. Defaults to an empty list.
metadata (Dict[str, Any], optional): Dictionary of metadata for the new agent. Defaults to an empty dictionary.
Returns:
The response indicating creation or an awaitable that resolves to the creation response.
Expand All @@ -102,7 +107,7 @@ class BaseAgentsManager(BaseManager):
Returns:
None or an awaitable that resolves to None.
_update(self, agent_id: Union[str, UUID], about: Optional[str] = None, instructions: Optional[List[str]] = None, name: Optional[str] = None, model: Optional[str] = None, default_settings: Optional[DefaultSettingsDict] = None) -> Union[ResourceUpdatedResponse, Awaitable[ResourceUpdatedResponse]]:
_update(self, agent_id: Union[str, UUID], about: Optional[str] = None, instructions: Optional[List[str]] = None, name: Optional[str] = None, model: Optional[str] = None, default_settings: Optional[DefaultSettingsDict] = None, metadata: Dict[str, Any] = {}) -> Union[ResourceUpdatedResponse, Awaitable[ResourceUpdatedResponse]]:
Updates the specified fields of an agent.
Args:
agent_id (Union[str, UUID]): The UUID of the agent to update.
Expand All @@ -111,6 +116,7 @@ class BaseAgentsManager(BaseManager):
name (Optional[str], optional): The new name for the agent.
model (Optional[str], optional): The new model name for the agent.
default_settings (Optional[DefaultSettingsDict], optional): The new default settings dictionary for the agent.
metadata (Dict[str, Any])
Returns:
The response indicating successful update or an awaitable that resolves to the update response.
"""
Expand All @@ -134,13 +140,14 @@ def _get(self, id: Union[str, UUID]) -> Union[Agent, Awaitable[Agent]]:
def _create(
self,
name: str,
about: str,
instructions: List[str],
about: str = "",
instructions: List[str] = [],
tools: List[ToolDict] = [],
functions: List[FunctionDefDict] = [],
default_settings: DefaultSettingsDict = {},
model: ModelName = "julep-ai/samantha-1-turbo",
docs: List[DocDict] = [],
metadata: Dict[str, Any] = {},
) -> Union[ResourceCreatedResponse, Awaitable[ResourceCreatedResponse]]:
# Cast instructions to a list of Instruction objects
"""
Expand All @@ -155,6 +162,7 @@ def _create(
default_settings (DefaultSettingsDict, optional): Dictionary of default settings for the agent. Defaults to an empty dict.
model (ModelName, optional): The model name identifier. Defaults to 'julep-ai/samantha-1-turbo'.
docs (List[DocDict], optional): List of document configurations for the agent. Defaults to an empty list.
metadata (Dict[str, Any])
Returns:
Union[ResourceCreatedResponse, Awaitable[ResourceCreatedResponse]]: The response object indicating the resource has been created or a future of the response object if the creation is being awaited.
Expand Down Expand Up @@ -196,6 +204,7 @@ def _create(
default_settings=default_settings,
model=model,
docs=docs,
metadata=metadata,
)

def _list_items(
Expand Down Expand Up @@ -243,11 +252,13 @@ def _delete(self, agent_id: Union[str, UUID]) -> Union[None, Awaitable[None]]:
def _update(
self,
agent_id: Union[str, UUID],
about: Optional[str] = None,
instructions: List[str] = None,
name: Optional[str] = None,
model: Optional[str] = None,
default_settings: Optional[DefaultSettingsDict] = None,
about: Optional[str] = NotSet,
instructions: List[str] = NotSet,
name: Optional[str] = NotSet,
model: Optional[str] = NotSet,
default_settings: Optional[DefaultSettingsDict] = NotSet,
metadata: Dict[str, Any] = NotSet,
overwrite: bool = False,
) -> Union[ResourceUpdatedResponse, Awaitable[ResourceUpdatedResponse]]:
"""
Update the agent's properties.
Expand All @@ -259,6 +270,8 @@ def _update(
name (Optional[str], optional): The name of the agent. Defaults to None.
model (Optional[str], optional): The model identifier for the agent. Defaults to None.
default_settings (Optional[DefaultSettingsDict], optional): A dictionary of default settings to apply to the agent. Defaults to None.
metadata (Dict[str, Any])
overwrite (bool, optional): Whether to overwrite the existing agent settings. Defaults to False.
Returns:
Union[ResourceUpdatedResponse, Awaitable[ResourceUpdatedResponse]]: An object representing the response for the resource updated, which can also be an awaitable in asynchronous contexts.
Expand All @@ -271,20 +284,29 @@ def _update(
"""
assert is_valid_uuid4(agent_id), "id must be a valid UUID v4"

if default_settings is not None:
if default_settings is not NotSet:
default_settings: AgentDefaultSettings = AgentDefaultSettings(
**default_settings
)

return self.api_client.update_agent(
updateFn = (
self.api_client.update_agent if overwrite else self.api_client.patch_agent
)

update_payload = dict(
agent_id=agent_id,
about=about,
instructions=instructions,
name=name,
model=model,
default_settings=default_settings,
metadata=metadata,
)

update_payload = {k: v for k, v in update_payload.items() if v is not NotSet}

return updateFn(**update_payload)


class AgentsManager(BaseAgentsManager):
"""
Expand Down Expand Up @@ -314,6 +336,7 @@ class AgentsManager(BaseAgentsManager):
default_settings (DefaultSettingsDict, optional): A dictionary of default settings. Defaults to an empty dictionary.
model (ModelName, optional): The model name to be used. Defaults to 'julep-ai/samantha-1-turbo'.
docs (List[DocDict], optional): A list of dictionaries defining documentation. Defaults to an empty list.
metadata (Dict[str, Any])
Returns:
ResourceCreatedResponse: The response indicating the resource (agent) was successfully created.
Expand Down Expand Up @@ -344,6 +367,7 @@ class AgentsManager(BaseAgentsManager):
name (Optional[str], optional): A new name for the agent. Defaults to None (no change).
model (Optional[str], optional): A new model name to be used. Defaults to None (no change).
default_settings (Optional[DefaultSettingsDict], optional): A new dictionary of default settings. Defaults to None (no change).
metadata (Dict[str, Any])
Returns:
ResourceUpdatedResponse: The response indicating the resource (agent) was successfully updated.
Expand Down Expand Up @@ -381,6 +405,7 @@ def create(self, **kwargs: AgentCreateArgs) -> Agent:
default_settings (DefaultSettingsDict, optional): A dictionary with default settings. Defaults to an empty dictionary.
model (ModelName, optional): The name of the model to use. Defaults to 'julep-ai/samantha-1-turbo'.
docs (List[DocDict], optional): A list of dictionaries with documentation details. Defaults to an empty list.
metadata (Dict[str, Any])
Returns:
Agent: An instance of the Agent with the specified details
Expand Down Expand Up @@ -456,13 +481,16 @@ def update(self, *, agent_id: Union[str, UUID], **kwargs: AgentUpdateArgs) -> Ag
name (Optional[str], optional): The new name to assign to the agent. Defaults to None.
model (Optional[str], optional): The model identifier to associate with the agent. Defaults to None.
default_settings (Optional[DefaultSettingsDict], optional): A dictionary of default settings to apply to the agent. Defaults to None.
metadata (Dict[str, Any])
overwrite (bool, optional): Whether to overwrite the existing agent settings. Defaults to False.
Returns:
ResourceUpdatedResponse: An object representing the response to the update request.
Note:
This method is decorated with `beartype`, which means it enforces type annotations at runtime.
"""

result = self._update(agent_id=agent_id, **kwargs)
return result

Expand Down Expand Up @@ -500,6 +528,7 @@ class AsyncAgentsManager(BaseAgentsManager):
default_settings (DefaultSettingsDict, optional): Optional default settings for the agent.
model (ModelName, optional): The model name to associate with the agent, defaults to 'julep-ai/samantha-1-turbo'.
docs (List[DocDict], optional): An optional list of documents associated with the agent.
metadata (Dict[str, Any])
Returns:
ResourceCreatedResponse: A response indicating the agent was created successfully.
Expand Down Expand Up @@ -533,6 +562,7 @@ class AsyncAgentsManager(BaseAgentsManager):
name (Optional[str], optional): An optional new name for the agent.
model (Optional[str], optional): Optional new model associated with the agent.
default_settings (Optional[DefaultSettingsDict], optional): Optional new default settings for the agent.
metadata (Dict[str, Any])
Returns:
ResourceUpdatedResponse: A response indicating the agent was updated successfully.
Expand Down Expand Up @@ -574,6 +604,7 @@ async def create(self, **kwargs: AgentCreateArgs) -> Agent:
default_settings (DefaultSettingsDict, optional): A dictionary with default settings for the resource. Defaults to an empty dictionary.
model (ModelName, optional): The model identifier to use for the resource. Defaults to 'julep-ai/samantha-1-turbo'.
docs (List[DocDict], optional): A list of dictionaries containing documentation for the resource. Defaults to an empty list.
metadata (Dict[str, Any])
Returns:
Agent: An instance of the Agent with the specified details
Expand Down Expand Up @@ -647,6 +678,8 @@ async def update(
name (Optional[str]): The name of the agent. Default is None.
model (Optional[str]): The model identifier or name. Default is None.
default_settings (Optional[DefaultSettingsDict]): Dictionary with default settings for the agent. Default is None.
metadata (Dict[str, Any])
overwrite (bool): Whether to overwrite the existing agent settings. Default is False.
Returns:
ResourceUpdatedResponse: An object containing the details of the update response.
Expand Down
7 changes: 7 additions & 0 deletions sdks/python/julep/managers/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DocsCreateArgs(TypedDict):
agent_id: Optional[Union[str, UUID]]
user_id: Optional[Union[str, UUID]]
doc: DocDict
metadata: Dict[str, Any] = {}


class BaseDocsManager(BaseManager):
Expand Down Expand Up @@ -114,6 +115,7 @@ def _create(
doc: DocDict,
agent_id: Optional[Union[str, UUID]] = None,
user_id: Optional[Union[str, UUID]] = None,
metadata: Dict[str, Any] = {},
) -> Union[ResourceCreatedResponse, Awaitable[ResourceCreatedResponse]]:
"""
Create a new resource with docsrmation for either an agent or a user, but not both.
Expand Down Expand Up @@ -457,6 +459,11 @@ async def delete(
Note:
The `@beartype` decorator is used to enforce type checking on the function arguments.
"""

# Assert either user_id or agent_id is provided
if not agent_id and not user_id:
raise ValueError("Either agent_id or user_id must be provided.")

return await self._delete(
agent_id=agent_id,
user_id=user_id,
Expand Down
Loading

0 comments on commit f488389

Please sign in to comment.