Skip to content

Commit

Permalink
feat: Add submit_tool_outputs API
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Sep 28, 2024
1 parent 55213c5 commit 560fab7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cozepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
MessageObjectStringType,
MessageRole,
MessageType,
ToolOutput,
)
from .config import (
COZE_CN_BASE_URL,
Expand Down Expand Up @@ -91,6 +92,7 @@
"Chat",
"ChatEvent",
"ChatChatIterator",
"ToolOutput",
# conversations
"Conversation",
# files
Expand Down
43 changes: 43 additions & 0 deletions cozepy/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ def __next__(self) -> ChatEvent:
raise ValueError(f"invalid chat.event: {event}, {data}")


class ToolOutput(CozeModel):
# The ID for reporting the running results. You can get this ID under the tool_calls field in response of the Chat
# API.
tool_call_id: str

# The execution result of the tool.
output: str


class ChatClient(object):
def __init__(self, base_url: str, auth: Auth, requester: Requester):
self._base_url = base_url
Expand Down Expand Up @@ -425,6 +434,40 @@ def retrieve(
}
return self._requester.request("post", url, Chat, params=params)

def submit_tool_outputs(
self, *, conversation_id: str, chat_id: str, tool_outputs: List[ToolOutput], stream: bool
) -> Union[Chat, ChatChatIterator]:
"""
Call this API to submit the results of tool execution.
docs en: https://www.coze.com/docs/developer_guides/chat_submit_tool_outputs
docs zh: https://www.coze.cn/docs/developer_guides/chat_submit_tool_outputs
:param conversation_id: The Conversation ID can be viewed in the 'conversation_id' field of the Response when
initiating a conversation through the Chat API.
:param chat_id: The Chat ID can be viewed in the 'id' field of the Response when initiating a chat through the
Chat API. If it is a streaming response, check the 'id' field in the chat event of the Response.
:param tool_outputs: The execution result of the tool. For detailed instructions, refer to the ToolOutput Object
:param stream: Whether to enable streaming response.
true: Fill in the context of the previous conversation and continue with streaming response.
false: (Default) Non-streaming response, only reply with basic information of the conversation.
:return:
"""
url = f"{self._base_url}/v3/chat/submit_tool_outputs"
params = {

Check warning on line 457 in cozepy/chat/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/chat/__init__.py#L456-L457

Added lines #L456 - L457 were not covered by tests
"conversation_id": conversation_id,
"chat_id": chat_id,
}
body = {

Check warning on line 461 in cozepy/chat/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/chat/__init__.py#L461

Added line #L461 was not covered by tests
"tool_outputs": [i.model_dump() for i in tool_outputs],
"stream": stream,
}

if not stream:
return self._requester.request("post", url, Chat, params=params, body=body, stream=stream)

Check warning on line 467 in cozepy/chat/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/chat/__init__.py#L466-L467

Added lines #L466 - L467 were not covered by tests

return ChatChatIterator(self._requester.request("post", url, Chat, params=params, body=body, stream=stream))

Check warning on line 469 in cozepy/chat/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/chat/__init__.py#L469

Added line #L469 was not covered by tests

def cancel(
self,
*,
Expand Down

0 comments on commit 560fab7

Please sign in to comment.