Skip to content

Commit 4cc9020

Browse files
authored
feat: Support parameters for chat (#233)
1 parent 15094fa commit 4cc9020

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

cozepy/chat/__init__.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import time
44
from enum import Enum
5-
from typing import TYPE_CHECKING, AsyncIterator, Dict, List, Optional, Union, overload
5+
from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, List, Optional, Union, overload
66

77
import httpx
88
from typing_extensions import Literal
@@ -442,6 +442,7 @@ def create(
442442
custom_variables: Optional[Dict[str, str]] = None,
443443
auto_save_history: bool = True,
444444
meta_data: Optional[Dict[str, str]] = None,
445+
parameters: Optional[Dict[str, Any]] = None,
445446
) -> Chat:
446447
"""
447448
Call the Chat API with non-streaming to send messages to a published Coze bot.
@@ -458,6 +459,7 @@ def create(
458459
:param custom_variables: The customized variable in a key-value pair.
459460
:param auto_save_history: Whether to automatically save the history of conversation records.
460461
:param meta_data: Additional information, typically used to encapsulate some business-related fields.
462+
:param parameters: Additional parameters for the chat API. pass through to the workflow.
461463
:return: chat object
462464
"""
463465
return self._create(
@@ -469,6 +471,7 @@ def create(
469471
auto_save_history=auto_save_history,
470472
meta_data=meta_data,
471473
conversation_id=conversation_id,
474+
parameters=parameters,
472475
)
473476

474477
def stream(
@@ -481,6 +484,7 @@ def stream(
481484
auto_save_history: bool = True,
482485
meta_data: Optional[Dict[str, str]] = None,
483486
conversation_id: Optional[str] = None,
487+
parameters: Optional[Dict[str, Any]] = None,
484488
**kwargs,
485489
) -> Stream[ChatEvent]:
486490
"""
@@ -498,6 +502,7 @@ def stream(
498502
:param custom_variables: The customized variable in a key-value pair.
499503
:param auto_save_history: Whether to automatically save the history of conversation records.
500504
:param meta_data: Additional information, typically used to encapsulate some business-related fields.
505+
:param parameters: Additional parameters for the chat API. pass through to the workflow.
501506
:return: iterator of ChatEvent
502507
"""
503508
return self._create(
@@ -509,6 +514,7 @@ def stream(
509514
auto_save_history=auto_save_history,
510515
meta_data=meta_data,
511516
conversation_id=conversation_id,
517+
parameters=parameters,
512518
**kwargs,
513519
)
514520

@@ -523,6 +529,7 @@ def create_and_poll(
523529
auto_save_history: bool = True,
524530
meta_data: Optional[Dict[str, str]] = None,
525531
poll_timeout: Optional[int] = None,
532+
parameters: Optional[Dict[str, Any]] = None,
526533
) -> ChatPoll:
527534
"""
528535
Call the Chat API with non-streaming to send messages to a published Coze bot and
@@ -541,6 +548,7 @@ def create_and_poll(
541548
:param auto_save_history: Whether to automatically save the history of conversation records.
542549
:param meta_data: Additional information, typically used to encapsulate some business-related fields.
543550
:param poll_timeout: poll timeout in seconds
551+
:param parameters: Additional parameters for the chat API. pass through to the workflow.
544552
:return: chat object
545553
"""
546554
chat = self.create(
@@ -551,6 +559,7 @@ def create_and_poll(
551559
custom_variables=custom_variables,
552560
auto_save_history=auto_save_history,
553561
meta_data=meta_data,
562+
parameters=parameters,
554563
)
555564

556565
start = int(time.time())
@@ -579,6 +588,7 @@ def _create(
579588
auto_save_history: bool = ...,
580589
meta_data: Optional[Dict[str, str]] = ...,
581590
conversation_id: Optional[str] = ...,
591+
parameters: Optional[Dict[str, Any]] = ...,
582592
) -> Stream[ChatEvent]: ...
583593

584594
@overload
@@ -593,6 +603,7 @@ def _create(
593603
auto_save_history: bool = ...,
594604
meta_data: Optional[Dict[str, str]] = ...,
595605
conversation_id: Optional[str] = ...,
606+
parameters: Optional[Dict[str, Any]] = ...,
596607
) -> Chat: ...
597608

598609
def _create(
@@ -606,11 +617,11 @@ def _create(
606617
auto_save_history: bool = True,
607618
meta_data: Optional[Dict[str, str]] = None,
608619
conversation_id: Optional[str] = None,
620+
parameters: Optional[Dict[str, Any]] = None,
609621
**kwargs,
610622
) -> Union[Chat, Stream[ChatEvent]]:
611623
"""
612-
Create a conversation.
613-
Conversation is an interaction between a bot and a user, including one or more messages.
624+
Create a chat.
614625
"""
615626
url = f"{self._base_url}/v3/chat"
616627
params = {
@@ -624,6 +635,7 @@ def _create(
624635
"custom_variables": custom_variables,
625636
"auto_save_history": auto_save_history,
626637
"meta_data": meta_data,
638+
"parameters": parameters,
627639
}
628640
headers: Optional[dict] = kwargs.get("headers")
629641
if not stream:
@@ -777,6 +789,7 @@ async def create(
777789
custom_variables: Optional[Dict[str, str]] = None,
778790
auto_save_history: bool = True,
779791
meta_data: Optional[Dict[str, str]] = None,
792+
parameters: Optional[Dict[str, Any]] = None,
780793
) -> Chat:
781794
"""
782795
Call the Chat API with non-streaming to send messages to a published Coze bot.
@@ -793,6 +806,7 @@ async def create(
793806
:param custom_variables: The customized variable in a key-value pair.
794807
:param auto_save_history: Whether to automatically save the history of conversation records.
795808
:param meta_data: Additional information, typically used to encapsulate some business-related fields.
809+
:param parameters: Additional parameters for the chat API. pass through to the workflow.
796810
:return: chat object
797811
"""
798812
return await self._create(
@@ -804,6 +818,7 @@ async def create(
804818
auto_save_history=auto_save_history,
805819
meta_data=meta_data,
806820
conversation_id=conversation_id,
821+
parameters=parameters,
807822
)
808823

809824
async def stream(
@@ -816,6 +831,7 @@ async def stream(
816831
auto_save_history: bool = True,
817832
meta_data: Optional[Dict[str, str]] = None,
818833
conversation_id: Optional[str] = None,
834+
parameters: Optional[Dict[str, Any]] = None,
819835
) -> AsyncIterator[ChatEvent]:
820836
"""
821837
Call the Chat API with streaming to send messages to a published Coze bot.
@@ -832,6 +848,7 @@ async def stream(
832848
:param custom_variables: The customized variable in a key-value pair.
833849
:param auto_save_history: Whether to automatically save the history of conversation records.
834850
:param meta_data: Additional information, typically used to encapsulate some business-related fields.
851+
:param parameters: Additional parameters for the chat API. pass through to the workflow.
835852
:return: iterator of ChatEvent
836853
"""
837854
async for item in await self._create(
@@ -843,6 +860,7 @@ async def stream(
843860
auto_save_history=auto_save_history,
844861
meta_data=meta_data,
845862
conversation_id=conversation_id,
863+
parameters=parameters,
846864
):
847865
yield item
848866

@@ -858,6 +876,7 @@ async def _create(
858876
auto_save_history: bool = ...,
859877
meta_data: Optional[Dict[str, str]] = ...,
860878
conversation_id: Optional[str] = ...,
879+
parameters: Optional[Dict[str, Any]] = ...,
861880
) -> AsyncStream[ChatEvent]: ...
862881

863882
@overload
@@ -872,6 +891,7 @@ async def _create(
872891
auto_save_history: bool = ...,
873892
meta_data: Optional[Dict[str, str]] = ...,
874893
conversation_id: Optional[str] = ...,
894+
parameters: Optional[Dict[str, Any]] = ...,
875895
) -> Chat: ...
876896

877897
async def _create(
@@ -885,6 +905,7 @@ async def _create(
885905
auto_save_history: bool = True,
886906
meta_data: Optional[Dict[str, str]] = None,
887907
conversation_id: Optional[str] = None,
908+
parameters: Optional[Dict[str, Any]] = None,
888909
) -> Union[Chat, AsyncStream[ChatEvent]]:
889910
"""
890911
Create a conversation.
@@ -902,6 +923,7 @@ async def _create(
902923
"custom_variables": custom_variables,
903924
"auto_save_history": auto_save_history,
904925
"meta_data": meta_data,
926+
"parameters": parameters,
905927
}
906928
if not stream:
907929
return await self._requester.arequest(

examples/chat_stream.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
and handle chat events
44
"""
55

6+
import json
67
import logging
78
import os
89
from typing import Optional
@@ -45,6 +46,8 @@ def get_coze_api_token(workspace_id: Optional[str] = None) -> str:
4546
# Whether to print detailed logs
4647
is_debug = os.getenv("DEBUG")
4748

49+
parameters = json.loads(os.getenv("COZE_PARAMETERS") or "{}")
50+
4851
if is_debug:
4952
setup_logging(logging.DEBUG)
5053

@@ -53,13 +56,16 @@ def get_coze_api_token(workspace_id: Optional[str] = None) -> str:
5356
# chat event and handle them.
5457
is_first_reasoning_content = True
5558
is_first_content = True
56-
for event in coze.chat.stream(
59+
stream = coze.chat.stream(
5760
bot_id=bot_id,
5861
user_id=user_id,
5962
additional_messages=[
6063
Message.build_user_question_text("Tell a 500-word story."),
6164
],
62-
):
65+
parameters=parameters,
66+
)
67+
print("logid:", stream.response.logid)
68+
for event in stream:
6369
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
6470
if event.message.reasoning_content:
6571
if is_first_reasoning_content:
@@ -75,3 +81,8 @@ def get_coze_api_token(workspace_id: Optional[str] = None) -> str:
7581
if event.event == ChatEventType.CONVERSATION_CHAT_COMPLETED:
7682
print()
7783
print("token usage:", event.chat.usage.token_count)
84+
85+
if event.event == ChatEventType.CONVERSATION_CHAT_FAILED:
86+
print()
87+
print("chat failed", event.chat.last_error)
88+
break

0 commit comments

Comments
 (0)