""" This example describes how to use the workflow interface to stream chat. """
import os # Our official coze sdk for Python [cozepy](https://github.com/coze-dev/coze-py) from cozepy import COZE_CN_BASE_URL
# Get an access_token through personal access token or oauth. coze_api_token = '{API_KEY}' # The default access is api.coze.com, but if you need to access api.coze.cn, # please use base_url to configure the api endpoint to access coze_api_base = COZE_CN_BASE_URL
from cozepy import Coze, TokenAuth, Stream, WorkflowEvent, WorkflowEventType # noqa
# Init the Coze client through the access_token. coze = Coze(auth=TokenAuth(token=coze_api_token), base_url=coze_api_base)
# Create a workflow instance in Coze, copy the last number from the web link as the workflow's ID. workflow_id = '{WORKFLOW_ID}'
# The stream interface will return an iterator of WorkflowEvent. Developers should iterate # through this iterator to obtain WorkflowEvent and handle them separately according to # the type of WorkflowEvent. defhandle_workflow_iterator(stream: Stream[WorkflowEvent]): for event in stream: if event.event == WorkflowEventType.MESSAGE: print("got message", event.message) elif event.event == WorkflowEventType.ERROR: print("got error", event.error) elif event.event == WorkflowEventType.INTERRUPT: handle_workflow_iterator( coze.workflows.runs.resume( workflow_id=workflow_id, event_id=event.interrupt.interrupt_data.event_id, resume_data="hey", interrupt_type=event.interrupt.interrupt_data.type, ) )
""" This example describes how to use the workflow interface to stream chat. """
import os # Our official coze sdk for Python [cozepy](https://github.com/coze-dev/coze-py) from cozepy import COZE_CN_BASE_URL
# Get an access_token through personal access token or oauth. coze_api_token = 'cztei_hXYOqnustyYyhrSuGFl4tgcxJ9E2KjYLPnHvcEcoWRwWvujWU0sPqka8xyQ1wsCyi' # The default access is api.coze.com, but if you need to access api.coze.cn, # please use base_url to configure the api endpoint to access coze_api_base = COZE_CN_BASE_URL
from cozepy import Coze, TokenAuth, Stream, WorkflowEvent, WorkflowEventType # noqa
# Init the Coze client through the access_token. coze = Coze(auth=TokenAuth(token=coze_api_token), base_url=coze_api_base)
# Create a workflow instance in Coze, copy the last number from the web link as the workflow's ID. workflow_id = '7537267958432858127'
# The stream interface will return an iterator of WorkflowEvent. Developers should iterate # through this iterator to obtain WorkflowEvent and handle them separately according to # the type of WorkflowEvent. defhandle_workflow_iterator(stream: Stream[WorkflowEvent]): for event in stream: if event.event == WorkflowEventType.MESSAGE: print("got message", event.message) elif event.event == WorkflowEventType.ERROR: print("got error", event.error) elif event.event == WorkflowEventType.INTERRUPT: handle_workflow_iterator( coze.workflows.runs.resume( workflow_id=workflow_id, event_id=event.interrupt.interrupt_data.event_id, resume_data="hey", interrupt_type=event.interrupt.interrupt_data.type, ) )