Python SDK
更新时间:
Python SDK
环境依赖
1.Python>=3.7
安装
pip install 直接通过git安装
pip install xingchen==1.1.5
非固定角色对话
准备
from xingchen import Configuration, ApiClient, ChatApiSub, ChatReqParams, CharacterKey, Message, UserProfile, \
ModelParameters
def init_client():
configuration = Configuration(
host="https://nlp.aliyuncs.com"
)
configuration.access_token = "{API-KEY}"
with ApiClient(configuration) as api_client:
api_instance = ChatApiSub(api_client)
return api_instance
对话
非固定角色对话为用户自定义角色人设,无需在通义星尘平台创建角色,角色设置通过请求参数botProfile.name
和botProfile.content
分别定义角色名称和人设,并通过 sample_messages
定义对话示例,其他参数和固定角色保持一致。参数定义如下:
def build_chat_param():
return ChatReqParams(
bot_profile=CharacterKey(
name='小婉',
content="""
【你的人设】
姓名:小婉
性别:女
年龄:21岁
生日:6月1日
星座:双子座
性格:热情、开朗、大方、充满元气
""",
traits="强制要求"
),
model_parameters=ModelParameters(
model_name="xingchen-plus-v2",
top_p=0.95,
temperature=0.92,
seed=1683806810,
incrementalOutput=False
),
messages=[
Message(
name='小明',
role='user',
content='你叫什么名字?'
),
Message(
name='小婉',
role='assistant',
content='我叫小婉啊。'
),
Message(
name='小明',
role='user',
content='你今年多大?'
),
Message(
name='小婉',
role='assistant',
content='我今年17岁了。'
),
Message(
name='小明',
role='user',
content='可以详细介绍浙江有哪些好玩的地方吗?'
)
],
sample_messages=[
Message(
name='小明',
role='user',
content='你在干嘛啊?'
),
Message(
name='小婉',
role='assistant',
content='在想你啊~[[想你]] 你呢?'
),
Message(
name='小明',
role='user',
content='我在吃饭呢.'
),
Message(
name='小婉',
role='assistant',
content='我也是!你看我在吃沙拉~'
)
],
user_profile=UserProfile(
user_id='123456789',
user_name='test'
),
advanced_settings=AdvancedSettings(
enable_web_search= True,
search_enhanced_keyword= "网络搜索关键词",
enable_character_kb_search= True,
knowledge_bases=["fc80436eb092455fbfccd86bb51f1f80"] ## 选择知识库,需注意有权限校验,必须是调用者在星辰创建的知识库
),
memory=Memory(
summaries=[
"user和assistant计划一起去打篮球"
],
originals=[
Message(
name='小明',
role='user',
content='你叫什么名字'
),
Message(
name='小婉',
role='assistant',
content='我叫小婉啊'
)
],
tags=[
ExtractData(
key='职业',
value=[
"武术家"
],
role='user'
),
ExtractData(
key='喜欢',
value=[
"武术"
],
role='user'
)
]
)
)
# 非流式回复
def test_chat_sync():
chat_param = build_chat_param()
res = api.chat(chat_param)
print(res.to_str())
# 流式回复
def test_chat_async():
chat_param = build_chat_param()
chat_param.streaming = True
responses = api.chat(chat_param)
for res in responses:
print(res)
api = init_client()
test_chat_async()
test_chat_sync()
非增量输出
我今年
我今年17岁了,很快就要
我今年17岁了,很快就要步入大学了。
我今年17岁了,很快就要步入大学了。
增量输出
我今年
17岁了,很快就要
步入大学了。
其他
kv抽取
# 准备
from xingchen import Configuration, ApiClient, Message, ModelParameters, ChatExtractMessageApiSub
def init_client():
configuration = Configuration(
host="https://nlp.aliyuncs.com"
)
configuration.access_token = "{YOUR-API-KEY}"
with ApiClient(configuration) as api_client:
api_instance = ChatExtractMessageApiSub(api_client)
return api_instanc
api_instance = self.init_client()
body = ExtractMemoryRequest(
messages=[
Message(
role="user",
content="我喜欢打篮球"
),
Message(
role="assistant",
content="我也喜欢打篮球"
)
],
kv_memory_configs=[
KVMemoryConfig(
enabled=True,
memory_text="运动"
),
KVMemoryConfig(
enabled=True,
memory_text="职业"
)
],
model_parameter=ModelParameters(
model_name="xingchen-base"
)
)
result = api_instance.extract_memory_kv(extract_memory_request=body)
print(result.data)
summary抽取
# 准备
from xingchen import Configuration, ApiClient, Message, ModelParameters, ChatExtractMessageApiSub
def init_client():
configuration = Configuration(
host="https://nlp.aliyuncs.com"
)
configuration.access_token = "{YOUR-API-KEY}"
with ApiClient(configuration) as api_client:
api_instance = ChatExtractMessageApiSub(api_client)
return api_instanc
api_instance = self.init_client()
body = ExtractMemoryRequest(
messages=[
Message(
role="user",
content="我们一起去打篮球把"
),
Message(
role="assistant",
content="好的呀,那我们一起去打球吧,打完篮球我们去吃火锅"
)
],
model_parameter=ModelParameters(
model_name="xingchen-base"
)
)
result = api_instance.extract_memory_summary(extract_memory_request=body)
print(result.data)
角色描述自动生成
# 准备
from xingchen import Configuration, ApiClient, CharacterApiSub
def init_client():
configuration = Configuration(
host="https://nlp.aliyuncs.com"
)
configuration.access_token = "{YOUR-API-KEY}"
with ApiClient(configuration) as api_client:
api_instance = CharacterApiSub(api_client)
return api_instance
api = init_client()
body = CharacterDescGeneratedRequest(
type="file",
file_url="https://lang.alicdn.com/xingchen/guanyu.txt",
text="关云长",
file_name="test.txt",
model_parameter=ModelParameters(
model_name="xingchen-base"
)
)
result = api.auto_generate_desc(character_desc_generate_request=body)
print(result)
excel转docx文件上传
# 准备
from xingchen import Configuration, ApiClient, CommonApiSub
def init_client():
configuration = Configuration(
host="https://nlp.aliyuncs.com"
)
configuration.access_token = "{YOUR-API-KEY}"
with ApiClient(configuration) as api_client:
api_instance = CommonApiSub(api_client)
return api_instance
api_instance = self.init_client()
body = FileConvertRequest(
type="excelToDocx",
source_url="https://lang.alicdn.com/xingchen/%E6%B5%8B%E8%AF%95.xlsx"
)
result = api_instance.file_convert(body)
print(result)
excel转docx文件结果下载
# 准备
from xingchen import Configuration, ApiClient, CommonApiSub
def init_client():
configuration = Configuration(
host="https://nlp.aliyuncs.com"
)
configuration.access_token = "{YOUR-API-KEY}"
with ApiClient(configuration) as api_client:
api_instance = CommonApiSub(api_client)
return api_instance
api_instance = init_client()
result = api_instance.file_convert_result(task_id=66647)
print(result)
终止对话
# 准备
from xingchen import Configuration, ApiClient, ChatApiSub
def init_client():
configuration = Configuration(
host="https://nlp.aliyuncs.com"
)
configuration.access_token = "{YOUR-API-KEY}"
with ApiClient(configuration) as api_client:
api_instance = ChatApiSub(api_client)
return api_instance
api = self.init_client()
body = StopChatRequest(
request_id='123456789',
content='测试终止保存历史数据'
)
result = api.stop_chat(stop_chat_request=body)
print(result)
文档内容是否对您有帮助?