Skip to content

Commit

Permalink
Merge pull request #334 from Zac2049/master
Browse files Browse the repository at this point in the history
Add Tongyi API feature, and fix a typo-caused bug
  • Loading branch information
wzpan authored Jul 19, 2024
2 parents 3b568bd + 411f852 commit 87e3dbf
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
90 changes: 90 additions & 0 deletions robot/AI.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,96 @@ def chat(self, texts, parsed):
)
return "抱歉,OpenAI 回答失败"

class WenxinRobot(AbstractRobot):

SLUG = "wenxin"

def __init__(self, api_key, secret_key):
"""
Wenxin机器人
"""
super(self.__class__, self).__init__()
self.api_key = api_key
self.secret_key = secret_key

@classmethod
def get_config(cls):
return config.get("wenxin", {})

def chat(self, texts, _):
"""
使用Wenxin机器人聊天
Arguments:
texts -- user input, typically speech, to be parsed by a module
"""
msg = "".join(texts)
msg = utils.stripPunctuation(msg)
wenxinurl = f"https://aip.baidubce.com/oauth/2.0/token?client_id={self.api_key}&\
client_secret={self.secret_key}&grant_type=client_credentials"
try:
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
payload = json.dumps({
"question": [
{
"role": "user",
"content": msg,
}
]
})
response = requests.request("POST", wenxinurl, headers=headers)
logger.info(f"wenxin response: {response}")
return response.text

except Exception:
logger.critical("Wenxin robot failed to response for %r", msg, exc_info=True)
return "抱歉, Wenxin回答失败"

class TongyiRobot(AbstractRobot):
'''
usage:
pip install dashscope
echo "export DASHSCOPE_API_KEY=YOUR_KEY" >> /.bashrc
'''
SLUG = "tongyi"

def __init__(self, api_key):
"""
Tongyi机器人
"""
super(self.__class__, self).__init__()
self.api_key = api_key

@classmethod
def get_config(cls):
return config.get("tongyi", {})

def chat(self, texts, _):
"""
使用Tongyi机器人聊天
Arguments:
texts -- user input, typically speech, to be parsed by a module
"""
msg = "".join(texts)
msg = utils.stripPunctuation(msg)
msg = [{"role": "user", "content": msg}]
try:
response = dashscope.Generation.call(
model='qwen1.5-72b-chat',
messages=msg,
result_format='message', # set the result to be "message" format.
)
logger.info(f"tongyi response: {response}")
return response['output']['choices'][0]['message']['content']

except Exception:
logger.critical("Tongyi robot failed to response for %r", msg, exc_info=True)
return "抱歉, Tongyi回答失败"


def get_unknown_response():
"""
Expand Down
2 changes: 1 addition & 1 deletion server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def post(self):
utils.check_and_delete(tmpfile)
conversation.doConverse(
nfile,
onSay=lambda msg, audio, plugin: self.on_resp(msg, audio, plugin),
onSay=lambda msg, audio, plugin: self.onResp(msg, audio, plugin),
onStream=lambda data, resp_uuid: self.onStream(
data, resp_uuid)

Expand Down

0 comments on commit 87e3dbf

Please sign in to comment.