Skip to content

Commit

Permalink
更新为官方api接口
Browse files Browse the repository at this point in the history
  • Loading branch information
aki66938 committed Dec 14, 2023
1 parent 95e5f7a commit c50ff76
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
4 changes: 4 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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)。
Expand Down
68 changes: 34 additions & 34 deletions base/func_bard.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 2 additions & 6 deletions config.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -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 # 新模型上线后可以选择模型
1 change: 1 addition & 0 deletions robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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
Expand Down

0 comments on commit c50ff76

Please sign in to comment.