diff --git a/README.MD b/README.MD index 811baa9..3cde901 100644 --- a/README.MD +++ b/README.MD @@ -133,6 +133,10 @@ xinghuo_web: # -----讯飞星火web模式api配置这行不填----- GtToken: # GtToken prompt: 你是智能聊天机器人,你叫 wcferry。请用这个角色回答我的问题 # 根据需要对角色进行设定 ``` +bard: # -----bard配置这行不填----- + api_key: # api-key 创建地址:https://ai.google.dev/pricing?hl=en,创建后复制过来即可 + model_name: gemini-pro # 新模型上线后可以选择模型 + ## HTTP 如需要使用 HTTP 接口,请参考 [wcfhttp](https://wechatferry.readthedocs.io/zh/latest/?badge=latest)。 diff --git a/base/func_bard.py b/base/func_bard.py index 970367c..b7fb8d3 100644 --- a/base/func_bard.py +++ b/base/func_bard.py @@ -1,34 +1,34 @@ -#! /usr/bin/env python3 -# -*- coding: utf-8 -*- - -import bardapi - - -class BardAssistant: - def __init__(self, conf: dict) -> None: - self._token = conf["token"] - self._bard = bardapi.core.Bard(self._token) - - def __repr__(self): - return 'BardAssistant' - - @staticmethod - def value_check(conf: dict) -> bool: - if conf: - return all(conf.values()) - return False - - def get_answer(self, msg: str, sender: str = None) -> str: - response = self._bard.get_answer(msg) - return response['content'] - - -if __name__ == "__main__": - from configuration import Config - config = Config().BardAssistant - if not config: - exit(0) - - bard_assistant = BardAssistant(config) - rsp = BardAssistant.get_answer("who are you?") - print(rsp) +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- +import google.generativeai as genai + +class BardAssistant: + def __init__(self, conf: dict) -> None: + self._api_key = conf["api_key"] + self._model_name = conf["model_name"] + genai.configure(api_key=self._api_key) + self._bard = genai.GenerativeModel(self._model_name) + + + def __repr__(self): + return 'BardAssistant' + + @staticmethod + def value_check(conf: dict) -> bool: + if conf: + return all(conf.values()) + return False + + def get_answer(self, msg: str, sender: str = None) -> str: + response = self._bard.generate_content(msg) + return response.text + +if __name__ == "__main__": + from configuration import Config + config = Config().BardAssistant + if not config: + exit(0) + + bard_assistant = BardAssistant(config) + rsp = BardAssistant.get_answer("who are you?") + print(rsp) diff --git a/config.yaml.template b/config.yaml.template index 897ae2c..5ce1fa8 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -71,9 +71,5 @@ xinghuo_web: # -----讯飞星火web模式api配置这行不填 抓取方式详 prompt: 你是智能聊天机器人,你叫wcferry。请用这个角色回答我的问题 # 根据需要对角色进行设定 bard: # -----bard配置这行不填----- - token: # token 长这样 eQhXAaCa3c3lUVXl-EvA210yikeNMsXHtDAX7doVyeFtW6ZbS-ENTSZg4-_713z1rWSXDQ. - # 你的bard的token,抓取方式见https://github.com/dsdanielpark/Bard-API,支持gemini(账号设置为美国),cooike值为__Secure-1PSID - # 特殊注意事项: - # 1、只需要登录gmail抓取账号的__Secure-1PSID即可 - # 2、如果网页登录了bard,已配置的机器人token会失效,需要清除浏览器上的google账号的cookie录后,重新抓取cooike值 - # 3、新建的账号可能无法登录bard,需要提前确认是否可以使用bard + api_key: # api-key 创建地址:https://ai.google.dev/pricing?hl=en,创建后复制过来即可 + model_name: gemini-pro # 新模型上线后可以选择模型 diff --git a/robot.py b/robot.py index d3d1ec1..486aa77 100644 --- a/robot.py +++ b/robot.py @@ -16,6 +16,7 @@ from base.func_chengyu import cy from base.func_news import News from base.func_tigerbot import TigerBot from base.func_xinghuo_web import XinghuoWeb +from base.func_bard import BardAssistant from configuration import Config from constants import ChatType from job_mgmt import Job