human/test/test_nlp_only.py

85 lines
2.5 KiB
Python
Raw Normal View History

2024-10-13 14:49:17 +00:00
#encoding = utf8
2024-11-08 07:23:44 +00:00
import json
import logging
import os
2024-10-13 14:49:17 +00:00
import time
2024-11-08 07:23:44 +00:00
import requests
from nlp import PunctuationSplit, DouBao, NLPCallback
from utils import config_logging
logger = logging.getLogger(__name__)
current_file_path = os.path.dirname(os.path.abspath(__file__))
# 接入点和apiKey生成、模型选择部分请访问豆包官方文档本文仅给出请求体和访问路径
# 豆包大模型接口官方文档 https://console.volcengine.com/ark/region:ark+cn-beijing/endpoint/detail?Id=ep-20240826182225-kp7rp&Tab=api
# 部分内容参考通义千问文档实现 https://help.aliyun.com/zh/model-studio/developer-reference/use-qwen-by-calling-api?spm=a2c4g.11186623.0.0.28b919a1NbCP4i#e7932c7e33gvv
2024-10-13 14:49:17 +00:00
2024-11-08 07:23:44 +00:00
def __request(key, question):
url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/json"
}
data = {
"model": "ep-20241008152048-fsgzf",
"messages": question,
'stream': True
}
response = requests.post(url, headers=headers, json=data, stream=True)
return response
class DisplayNLP(NLPCallback):
def on_message(self, txt: str):
print(txt)
2024-10-13 14:49:17 +00:00
def main():
2024-11-08 07:23:44 +00:00
# 你的API_KEY
# api_key = "c9635f9e-0f9e-4ca1-ac90-8af25a541b74"
# __token = 'c9635f9e-0f9e-4ca1-ac90-8af25a541b74'
# # 问题列表
# msg_list = [
# {"role": "system", "content": "你是测试客服,是由字节跳动开发的 AI 人工智能助手"},
# {"role": "user", "content": "你好"}
# ]
# t = time.time()
# response = __request(__token, msg_list)
# if response.status_code != 200:
# print(f"请求失败,状态码:{response.status_code}")
# return
#
# for chunk in response.iter_lines():
# content = chunk.decode("utf-8").strip()
# if len(content) < 1:
# continue
#
# content = content[5:]
# content = json.loads(content)
# print(f'-------dou_bao ask time:{time.time() - t:.4f}s, response:{content["choices"][0]["delta"]["content"]}')
#
# print("文件下载完成")
2024-10-13 14:49:17 +00:00
split = PunctuationSplit()
2024-11-08 07:23:44 +00:00
nlp = DouBao(None, split, DisplayNLP())
2024-10-13 14:49:17 +00:00
nlp.ask('你好')
nlp.ask('你是谁')
nlp.ask('能做什么')
2024-11-08 07:23:44 +00:00
time.sleep(5)
2024-10-13 14:49:17 +00:00
nlp.stop()
2024-10-14 10:20:55 +00:00
print("stop")
2024-10-13 14:49:17 +00:00
if __name__ == "__main__":
2024-11-08 07:23:44 +00:00
config_logging('../logs/info.log', logging.INFO, logging.INFO)
2024-10-13 14:49:17 +00:00
try:
main()
except KeyboardInterrupt:
print("\nCaught Ctrl + C. Exiting")
2024-10-14 10:20:55 +00:00
except Exception as e:
print(e)