Skip to content

Commit

Permalink
fix: Fix chat api conversation parameter and Release v0.6.2 (#95)
Browse files Browse the repository at this point in the history
- Fixed issues with conversation parameter passing in `chat.create` and
`chat.stream`
- Added a translation example to demonstrate how to use conversation
- Updated version to v0.6.2
  • Loading branch information
chyroc authored Oct 21, 2024
1 parent 58baf9d commit 41dcfc9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
6 changes: 5 additions & 1 deletion cozepy/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,16 @@ def _create(
Conversation is an interaction between a bot and a user, including one or more messages.
"""
url = f"{self._base_url}/v3/chat"
params = {
"conversation_id": conversation_id if conversation_id else None,
}
body = {
"bot_id": bot_id,
"user_id": user_id,
"additional_messages": [i.model_dump() for i in additional_messages] if additional_messages else [],
"stream": stream,
"custom_variables": custom_variables,
"auto_save_history": auto_save_history,
"conversation_id": conversation_id if conversation_id else None,
"meta_data": meta_data,
}
if not stream:
Expand All @@ -580,6 +582,7 @@ def _create(
url,
False,
Chat,
params=params,
body=body,
)

Expand All @@ -588,6 +591,7 @@ def _create(
url,
True,
None,
params=params,
body=body,
)
return Stream(steam_iters, fields=["event", "data"], handler=_sync_chat_stream_handler, logid=logid)
Expand Down
2 changes: 1 addition & 1 deletion cozepy/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from functools import lru_cache

VERSION = "0.6.1"
VERSION = "0.6.2"


@lru_cache(maxsize=1)
Expand Down
48 changes: 48 additions & 0 deletions examples/chat_conversation_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
This example is about how to use conversation to pass context
"""

import os
from typing import Callable

from cozepy import COZE_COM_BASE_URL, ChatEventType, ChatStatus, Coze, Message, MessageContentType, TokenAuth # noqa


def build_translate_chinese_to_english_context(coze: Coze, bot_id: str) -> Callable[[str], str]:
conversation = coze.conversations.create(
messages=[
Message.build_user_question_text("You need to translate the Chinese to English."),
Message.build_assistant_answer("OK."),
]
)

# Call chat.stream and parse the return value to get the translation result.
def translate_on_translate_chinese_to_english_context(text: str) -> str:
for event in coze.chat.stream(
bot_id=bot_id,
user_id="fake user id",
additional_messages=[
Message.build_user_question_text(text),
],
conversation_id=conversation.id,
):
if event.event == ChatEventType.CONVERSATION_MESSAGE_COMPLETED:
return event.message.content

return translate_on_translate_chinese_to_english_context


if __name__ == "__main__":
# Get an access_token through personal access token or oauth.
coze_api_token = os.getenv("COZE_API_TOKEN")
# 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 = os.getenv("COZE_API_BASE") or COZE_COM_BASE_URL
# Init the Coze client through the access_token.
coze = Coze(auth=TokenAuth(token=coze_api_token), base_url=coze_api_base)
# Create a bot instance in Coze, copy the last number from the web link as the bot's ID.
bot_id = os.getenv("COZE_BOT_ID") or "bot id"

translate_func = build_translate_chinese_to_english_context(coze, bot_id)

print("translate:", translate_func("地球与宇宙"))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cozepy"
version = "0.6.1"
version = "0.6.2"
description = "OpenAPI SDK for Coze(coze.com/coze.cn)"
authors = ["chyroc <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 41dcfc9

Please sign in to comment.