Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PUBLIC_API_VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3e9c6fa0a27454d8d5300c42e4215c475c3954b085dae9901f429d0c531e50f3
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "gradient-labs"
version = "0.12.2"
version = "0.13.0"
description = "Python bindings for the Gradient Labs API"
readme = "README.md"
requires-python = ">=3.9,<4.0"
Expand Down
4 changes: 4 additions & 0 deletions src/gradient_labs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .client import *
from ._tool_execute import Argument as Argument
from .article import *
from .conversation import *
from .handoff_target import *
Expand All @@ -16,3 +17,6 @@
from .topic import *
from .terminology_substitution import *
from .ip_addresses import *
from .customer_memory import *
from .file import *
from .knowledge_base import *
14 changes: 10 additions & 4 deletions src/gradient_labs/_article_topic_upsert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Optional
from typing import Any, Optional
from collections import defaultdict
from datetime import datetime

from ._datetime import parse_datetime

from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
from marshmallow import fields
Expand Down Expand Up @@ -30,7 +32,7 @@ class ArticleTopicUpsertParams:
created: datetime = field(
metadata=config(
encoder=datetime.isoformat,
decoder=datetime.fromisoformat,
decoder=parse_datetime,
mm_field=fields.DateTime(format="iso"),
)
)
Expand All @@ -39,7 +41,7 @@ class ArticleTopicUpsertParams:
last_edited: datetime = field(
metadata=config(
encoder=datetime.isoformat,
decoder=datetime.fromisoformat,
decoder=parse_datetime,
mm_field=fields.DateTime(format="iso"),
)
)
Expand All @@ -51,13 +53,17 @@ class ArticleTopicUpsertParams:
parent_id: Optional[str] = None

# data optionally gives additional meta-data about the topic.
data: Optional[dict] = field(default_factory=lambda: defaultdict(dict))
data: Any = field(default_factory=lambda: defaultdict(dict))

public_url: Optional[str] = None


def upsert_article_topic(
*, client: HttpClient, params: ArticleTopicUpsertParams
) -> None:
body = params.to_dict()
if params.public_url is None:
body.pop("public_url")
if params.parent_id is None:
body.pop("parent_id")
if params.description is None:
Expand Down
7 changes: 4 additions & 3 deletions src/gradient_labs/_article_topics_list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Optional
from dataclasses import dataclass
from dataclasses import dataclass, field

from dataclasses_json import dataclass_json
from dataclasses_json import dataclass_json, config

from .topic import Topic
from ._http_client import HttpClient
Expand All @@ -24,7 +24,8 @@ class ListTopicsParams:
class ListTopicsResponse:
"""Response containing a list of topics."""

topics: List[Topic]
# The API names this field `Topics`.
topics: List[Topic] = field(metadata=config(field_name="Topics"))


def list_topics(
Expand Down
21 changes: 18 additions & 3 deletions src/gradient_labs/_article_upsert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Optional, Any
from typing import Optional, Any, List
from collections import defaultdict
from datetime import datetime

from ._datetime import parse_datetime

from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
from marshmallow import fields
Expand All @@ -27,7 +29,7 @@ class UpsertArticleParams:
created: datetime = field(
metadata=config(
encoder=datetime.isoformat,
decoder=datetime.fromisoformat,
decoder=parse_datetime,
mm_field=fields.DateTime(format="iso"),
)
)
Expand All @@ -36,7 +38,7 @@ class UpsertArticleParams:
last_edited: datetime = field(
metadata=config(
encoder=datetime.isoformat,
decoder=datetime.fromisoformat,
decoder=parse_datetime,
mm_field=fields.DateTime(format="iso"),
)
)
Expand All @@ -61,6 +63,15 @@ class UpsertArticleParams:
# data optionally gives additional meta-data about the article.
data: Optional[Any] = field(default_factory=lambda: defaultdict(dict))

# public_url optionally links to where the article can be read online.
public_url: Optional[str] = None

# knowledge_base_ids optionally names the knowledge bases this article belongs to.
# Use the IDs of knowledge bases you created with create_knowledge_base, or
# "public-api". The list cannot be empty. If you leave it out, the article goes
# into the default "public-api" knowledge base, replacing any others it was in.
knowledge_base_ids: Optional[List[str]] = None


def upsert_article(*, client: HttpClient, params: UpsertArticleParams) -> None:
body = params.to_dict()
Expand All @@ -74,6 +85,10 @@ def upsert_article(*, client: HttpClient, params: UpsertArticleParams) -> None:
body.pop("author_id")
if params.body is None:
body.pop("body")
if params.public_url is None:
body.pop("public_url")
if params.knowledge_base_ids is None:
body.pop("knowledge_base_ids")

body["created"] = HttpClient.localize(params.created)
body["last_edited"] = HttpClient.localize(params.last_edited)
Expand Down
13 changes: 8 additions & 5 deletions src/gradient_labs/_back_office_task_create.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Optional, Dict, Any, List
from datetime import datetime
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config

from ._http_client import HttpClient
from ._without_none import without_none
from .back_office_task import BackOfficeTask


Expand All @@ -13,7 +14,9 @@ class BackOfficeTaskCreateAttachment:
file_name: str
# Provide either url (remote file) or base64_contents (inline upload), not both.
url: Optional[str] = None
base64_contents: Optional[str] = None
base64_contents: Optional[str] = field(
default=None, metadata=config(field_name="base_64_contents")
)


@dataclass_json
Expand All @@ -25,7 +28,7 @@ class BackOfficeTaskCreateParams:
# procedure_id is the procedure (prefix `proc_`) within the agent to start the
# task from.
procedure_id: str
input: Dict[str, Any]
input: Any
created: Optional[datetime] = None
metadata: Optional[Dict[str, str]] = None
attachments: Optional[List[BackOfficeTaskCreateAttachment]] = None
Expand All @@ -45,6 +48,6 @@ def create_back_office_task(
if params.metadata is not None:
body["metadata"] = params.metadata
if params.attachments is not None:
body["attachments"] = [a.to_dict() for a in params.attachments]
body["attachments"] = [without_none(a.to_dict()) for a in params.attachments]
rsp = client.post(path="back-office-tasks", body=body)
return BackOfficeTask.from_dict(rsp)
78 changes: 25 additions & 53 deletions src/gradient_labs/_conversation_add_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,12 @@
from datetime import datetime

from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
from marshmallow import fields
from dataclasses_json import dataclass_json

from ._http_client import HttpClient
from .conversation import AttachmentType, ParticipantType


@dataclass_json
@dataclass(frozen=True)
class Attachment:
# type of file that was uploaded.
type: AttachmentType

# file_name is the name of the file that was uploaded, or an
# adequate placeholder for that name, which can be shown to reviewers.
file_name: str

# url is the publicly accessible URL where the attachment can be downloaded
# from. This should be a fully qualified URL. If not given, the AI agent will
# only know that an attachment exists, but will be unable to process it.
url: Optional[str] = ""

# summary is an optional short summary of the attachment. This is only intended
# to be used if you cannot give us access to the raw attachment, but can
# run your own LLM completions on the attachment and send us a description instead.
#
# If a summary or description is provided, then there must be no URL.
summary: Optional[str] = ""

# description is an optional description of the attachment. This is only intended
# to be used if you cannot give us access to the raw attachment, but can
# run your own LLM completions on the attachment and send us a description instead.
#
# Chat to us first before using it!
description: Optional[str] = ""
from .conversation import Attachment, ParticipantType
from ._datetime import optional_datetime_config
from ._without_none import without_none


@dataclass_json
Expand All @@ -51,14 +22,14 @@ class AddMessageParams:
id: str

# body contains the message text.
body: str
body: Optional[str] = None

# participant_id identifies the message sender.
participant_id: str
participant_id: Optional[str] = None

# participant_type identifies the type of participant who sent this message.
# This cannot be set to ParticipantTypeAI.
participant_type: ParticipantType
# Use AI_AGENT only to replay a message already sent by the AI agent.
participant_type: Optional[ParticipantType] = None

# created is the time at which the message was sent.
created: Optional[datetime] = None
Expand All @@ -71,6 +42,14 @@ class AddMessageParams:
# as well as in HTTP Tools using templates.
conversation_token: Optional[str] = None

# subject is the email subject line. It is only valid for email conversations,
# and rejected for every other channel.
subject: Optional[str] = None

def __post_init__(self):
if self.participant_type is None:
raise TypeError("participant_type is required")


@dataclass_json
@dataclass(frozen=True)
Expand All @@ -86,22 +65,11 @@ class Message:
# body contains the message text.
body: str

# participant_id identifies the message sender.
participant_id: str

# participant_type identifies the type of participant who sent this message.
participant_type: ParticipantType

# Created is the time at which the message was sent.
created: datetime = field(
metadata=config(
encoder=datetime.isoformat,
decoder=datetime.fromisoformat,
mm_field=fields.DateTime(format="iso"),
)
)

# attachments contains any files that were uploaded with this message.
participant_id: str
created: Optional[datetime] = field(default=None, metadata=optional_datetime_config)
attachments: Optional[List[Attachment]] = None


Expand All @@ -110,16 +78,20 @@ def add_message(
) -> Message:
body = {
"id": params.id,
"body": params.body,
"participant_id": params.participant_id,
"participant_type": params.participant_type.value,
}
if params.body is not None:
body["body"] = params.body
if params.participant_id is not None:
body["participant_id"] = params.participant_id
if params.created is not None:
body["created"] = HttpClient.localize(params.created)
if params.attachments is not None:
body["attachments"] = [a.to_dict() for a in params.attachments]
body["attachments"] = [without_none(a.to_dict()) for a in params.attachments]
if params.conversation_token is not None:
body["conversation_token"] = params.conversation_token
if params.subject is not None:
body["subject"] = params.subject

rsp = client.post(path=f"conversations/{conversation_id}/messages", body=body)
return Message.from_dict(rsp)
6 changes: 4 additions & 2 deletions src/gradient_labs/_conversation_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ def assign_conversation(
*, client: HttpClient, conversation_id: str, params: AssignmentParams
) -> None:
"""assigns a conversation to a participant."""
body = {"assignee_type": params.participant_type.value}
body = {"assignee_type": params.assignee_type.value}
if params.assignee_id:
body["assignee_id"] = params.assignee_id
if params.timestamp:
body["timestamp"] = HttpClient.localize(params.timestamp)
if params.reason:
body["reason"] = params.reason

_ = client.put(
path=f"conversations/{conversation_id}/assignee",
body=params.to_dict(),
body=body,
)
3 changes: 1 addition & 2 deletions src/gradient_labs/_conversation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class EventParams:
# IdempotencyKey optionally enables you to safely retry requests
idempotency_key: Optional[str] = None

# Body optionally allows adding content (text) to the event. This is
# required for the
# Internal notes require a message body.
body: Optional[str] = None


Expand Down
8 changes: 4 additions & 4 deletions src/gradient_labs/_conversation_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class ReadParams:
def read_conversation(
*, client: HttpClient, conversation_id: str, params: Optional[ReadParams] = None
) -> Conversation:
path = f"conversations/{conversation_id}/read"
query_params = {}
if params and params.support_platform:
path += "?support_platform={params.support_platform}"
query_params["support_platform"] = params.support_platform

rsp = client.get(
path=path,
body=None,
path=f"conversations/{conversation_id}/read",
query_params=query_params,
)
return Conversation.from_dict(rsp)
11 changes: 11 additions & 0 deletions src/gradient_labs/_conversation_read_deprecated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ._http_client import HttpClient
from .conversation import Conversation


def read_conversation_deprecated(
*, client: HttpClient, conversation_id: str
) -> Conversation:
"""read_conversation_deprecated retrieves the conversation from the deprecated
`GET /conversations/{id}` endpoint. Use read_conversation instead."""
rsp = client.get(path=f"conversations/{conversation_id}")
return Conversation.from_dict(rsp)
Loading
Loading