Skip to content

Commit

Permalink
feat: Add support for deepseek model reasoning_content (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc authored Feb 28, 2025
1 parent 042c4e1 commit 7178c2c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 1 addition & 4 deletions cozepy/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,11 @@ class Message(CozeModel):
# The type of message.
type: MessageType = MessageType.UNKNOWN
# The content of the message. It supports various types of content, including plain text, multimodal (a mix of text, images, and files), message cards, and more.
# 消息的内容,支持纯文本、多模态(文本、图片、文件混合输入)、卡片等多种类型的内容。
content: str
# The type of message content.
# 消息内容的类型
content_type: MessageContentType
# Additional information when creating a message, and this additional information will also be returned when retrieving messages.
# Custom key-value pairs should be specified in Map object format, with a length of 16 key-value pairs. The length of the key should be between 1 and 64 characters, and the length of the value should be between 1 and 512 characters.
# 创建消息时的附加消息,获取消息时也会返回此附加消息。
# 自定义键值对,应指定为 Map 对象格式。长度为 16 对键值对,其中键(key)的长度范围为 1~64 个字符,值(value)的长度范围为 1~512 个字符。
meta_data: Optional[Dict[str, str]] = None

id: Optional[str] = None
Expand All @@ -159,6 +155,7 @@ class Message(CozeModel):
chat_id: Optional[str] = None
created_at: Optional[int] = None
updated_at: Optional[int] = None
reasoning_content: Optional[str] = None

@staticmethod
def build_user_question_text(content: str, meta_data: Optional[Dict[str, str]] = None) -> "Message":
Expand Down
21 changes: 19 additions & 2 deletions examples/chat_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
and handle chat events
"""

import logging
import os
from typing import Optional

from cozepy import COZE_CN_BASE_URL, ChatEventType, Coze, DeviceOAuthApp, Message, TokenAuth # noqa
from cozepy import COZE_CN_BASE_URL, ChatEventType, Coze, DeviceOAuthApp, Message, TokenAuth, setup_logging # noqa


def get_coze_api_base() -> str:
Expand Down Expand Up @@ -41,10 +42,17 @@ def get_coze_api_token(workspace_id: Optional[str] = None) -> str:
# The user id identifies the identity of a user. Developers can use a custom business ID
# or a random string.
user_id = "user id"
# Whether to print detailed logs
is_debug = os.getenv("DEBUG")

if is_debug:
setup_logging(logging.DEBUG)

# Call the coze.chat.stream method to create a chat. The create method is a streaming
# chat and will return a Chat Iterator. Developers should iterate the iterator to get
# chat event and handle them.
is_first_reasoning_content = True
is_first_content = True
for event in coze.chat.stream(
bot_id=bot_id,
user_id=user_id,
Expand All @@ -53,7 +61,16 @@ def get_coze_api_token(workspace_id: Optional[str] = None) -> str:
],
):
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
print(event.message.content, end="", flush=True)
if event.message.reasoning_content:
if is_first_reasoning_content:
is_first_reasoning_content = not is_first_reasoning_content
print("----- reasoning_content start -----\n> ", end="", flush=True)
print(event.message.reasoning_content, end="", flush=True)
else:
if is_first_content and not is_first_reasoning_content:
is_first_content = not is_first_content
print("----- reasoning_content end -----")
print(event.message.content, end="", flush=True)

if event.event == ChatEventType.CONVERSATION_CHAT_COMPLETED:
print()
Expand Down

0 comments on commit 7178c2c

Please sign in to comment.