本期分享一个知识点
无人带货直播中如何使用AI来回答观众的问题。
无人直播流程(简化)
- 收集直播间观众的问题
把问题发给AI,获得回答内容
- 把回答内容用TTS播放
- 用数字人朗读回答内容(可选)
与AI交互的两种方式
- 使用API与AI交互
- 使用网页与AI交互
使用API与AI交互
收费标准:100万token大概1块钱,可以理解成100万字1块钱,你发给AI的和AI给你回复的都计算字数。
from volcenginesdkarkruntime import Ark
def answer_question(question):
try: client = Ark( api_key = '5dd79c77-c78c-4ab4-a2c4-79d2f6f13fd8', base_url="https://ark.cn-beijing.volces.com/api/v3", )
intelligence = 'bot-20241220111057-glnr7' completion = client.bot_chat.completions.create( model=intelligence, messages = [ {"role": "user", "content": question}, ], timeout=10, )
return completion.choices[0].message.content
except Exception as e: print(e) return None
if __name__ == '__main__': while True: question = input("问题:") answer = answer_question(question) if answer: print(f"回复:{answer}") else: print("本次回复失败,请重试")
|
使用网页与AI交互
import time from DrissionPage import ChromiumPage, ChromiumOptions
def answer_question_by_web(page:ChromiumPage, question): try: break_button = page.ele('tag=div@data-testid=chat_input_local_break_button', timeout=1) if not break_button: print('找不到停止按钮') return None if break_button.states.is_displayed: print('停止按钮是显示状态,说明正在回答问题') return None
input_area = page.ele('tag=textarea@data-testid=chat_input_input', timeout=1) if not input_area: print('找不到输入框') return None input_area.input(question, clear=True) time.sleep(0.5)
send_btn = page.ele('tag=button@id=flow-end-msg-send', timeout=1) if not send_btn: print('找不到发送按钮') return None send_btn.click() time.sleep(1)
wait_result = page.wait.ele_hidden('tag=div@data-testid=chat_input_local_break_button', timeout=20) if not wait_result: print('没有等到停止按钮隐藏') page.refresh() time.sleep(1) return None
message_list = page.ele('tag=div@class:message-list-', timeout=1) if not message_list: print('找不到消息列表') return None container = message_list.ele('tag=div@class:inter-', timeout=1) if not container: print('找不到消息列表2') return None
last_one = container.children()[-1]
return last_one.text.strip() except Exception as e: print(e) return None
if __name__ == '__main__': co = ChromiumOptions() co.set_local_port(9222) co.use_system_user_path() co.set_argument('--start-maximized') page = ChromiumPage(co) page.get('https://www.doubao.com/chat/646320931514114')
while True: question = input("问题:") answer = answer_question_by_web(page, question) if answer: print(f"回复:{answer}") else: print("本次回复失败,请重试")
|
优缺点总结
API方式除了需要付费没别的毛病,快,稳,高并发。
web网页方式优点就是免费,缺点就是代码复杂、容易失效,不能并发,稍慢,有可能触发验证码。