#! /usr/bin/python import tornado.web import tornado.ioloop import tornado.httpserver import tornado.options import os import datetime import openai import asyncio from tornado.web import RequestHandler from tornado.options import define,options from tornado.websocket import WebSocketHandler import openai_async import config import json import traceback import aiohttp def clearBaiChuanContent(content): for ck in config.agentBaiChuanFilterMoel['filterKeys']: content = content.replace(ck,'xx') return content async def talkBaiChuanWithStream(self,message): print("{} says {}".format(self.request.remote_ip,message)) #url = "https://api.baichuan-ai.com/v1/chat/completions" #api_key = config.agentServer['agentBaiChuanApiKey'] url = "https://api.baichuan-ai.com/v1/chat/completions" api_key = "9f7d5847897f8090195cdc2c3249d0a7" try: _msg = json.loads(message) try: if 'model' in _msg : _baichuanModel = _msg['model'] _real_msg = _msg['messages'] else: _baichuanModel = 'Baichuan2' _real_msg = _msg except Exception as gmex: _baichuanModel = 'Baichuan2' _real_msg = _msg _data = { "model": _baichuanModel, "messages": _real_msg, "stream": True } #json_data = json.dumps(_data) headers = { "Content-Type": "application/json", "Authorization": "Bearer " + api_key } #http_client = tornado.httpclient.AsyncHTTPClient() async with aiohttp.ClientSession() as http_client: async with http_client.post(url,headers=headers,json=_data) as resp: isDone=False while True: chunk = await resp.content.read(2048) if not chunk: break chunk_str = chunk.decode() #print('chunk_str {}',chunk_str) con_list = chunk_str.strip('\n').split('data:') for con in con_list: con = con.strip() if con == "": continue elif con == '[DONE]': isDone=True break else: con_dict = json.loads(con) for con_cho in con_dict['choices']: self.my_replay_content=self.my_replay_content+con_cho['delta']['content'] #print('resp',resp) #print("1111111111111111111") print("response {}",self.my_replay_content) if config.agentBaiChuanFilterMoel['isOpen']: if resp is not None: cContent = clearBaiChuanContent(self.my_replay_content) self.my_replay_content='' if isDone: cContent = cContent+"___talk_end___" await self.write_message(cContent) #await self.write_message(currentLineContents) else: if role_conent is not None: print('') else: self.my_replay_content='' await self.write_message("___talk_end___") else: if resp is not None: _cont = self.my_replay_content if isDone: _cont = _cont+"___talk_end___" self.my_replay_content='' await self.write_message(_cont) else: self.my_replay_content='' await self.write_message("___talk_end___") else: self.my_replay_content='' await self.write_message("___talk_end___") except Exception as es: print('onMessage error',es) self.close()