目录
背景:
成功设置自己的知识库,语音交互问答会优先选择自己的知识库的答案进行回答,减少了耗时
一、模型选择
商汤 商量日日新
二、操作流程
按照这个走就没事
三、核心代码展示
import sensenova
class SimpleChatBot:
def __init__(self, access_key_id, secret_access_key):
# 设置访问密钥
sensenova.access_key_id = access_key_id
sensenova.secret_access_key = secret_access_key
# 创建会话
resp = sensenova.ChatSession.create(
system_prompt=[
{
"role": "system",
"content": "You are a translation expert."
}
]
)
self.session_id = resp["session_id"]
def get_response(self, text):
# 生成对话
resp = sensenova.ChatConversation.create(
action="next",
max_new_tokens=10,
content=text,
model="SenseChat",
stream=False, # 非流式输出
session_id=self.session_id,
knowledge_config={
"control_level": "normal",
"knowledge_base_result": True,
"knowledge_base_configs": []
},
plugins={
"associated_knowledge": {
"content": " string",
"mode": "concatenate"
},
"web_search": {
"search_enable": True,
"result_enable": True
},
}
)
if __name__ == "__main__":
access_key_id = ""
secret_access_key = ""
bot = SimpleChatBot(access_key_id, secret_access_key)
# 提示用户输入文本并获取回答
while True:
user_input = input("User: ")
if user_input.lower() == "exit": # 输入exit退出对话
print("Conversation ended.")
break
response = bot.get_response(user_input)
print("Assistant:", response)