-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix chat api conversation parameter and Release v0.6.2 (#95)
- 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
Showing
4 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import sys | ||
from functools import lru_cache | ||
|
||
VERSION = "0.6.1" | ||
VERSION = "0.6.2" | ||
|
||
|
||
@lru_cache(maxsize=1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("地球与宇宙")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|