mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 05:27:59 +08:00
feat: bring in openai completion api
This commit is contained in:
@@ -4,6 +4,7 @@ import requests
|
||||
from bot.bot import Bot
|
||||
|
||||
|
||||
# Baidu Unit对话接口 (可用, 但能力较弱)
|
||||
class BaiduUnitBot(Bot):
|
||||
def reply(self, query, context=None):
|
||||
token = self.get_token()
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
channel factory
|
||||
"""
|
||||
|
||||
from bot.baidu.baidu_unit_bot import BaiduUnitBot
|
||||
from bot.chatgpt.chat_gpt_bot import ChatGPTBot
|
||||
|
||||
|
||||
def create_bot(bot_type):
|
||||
"""
|
||||
@@ -13,7 +10,17 @@ def create_bot(bot_type):
|
||||
:return: channel instance
|
||||
"""
|
||||
if bot_type == 'baidu':
|
||||
# Baidu Unit对话接口
|
||||
from bot.baidu.baidu_unit_bot import BaiduUnitBot
|
||||
return BaiduUnitBot()
|
||||
|
||||
elif bot_type == 'chatGPT':
|
||||
# ChatGPT 网页端web接口
|
||||
from bot.chatgpt.chat_gpt_bot import ChatGPTBot
|
||||
return ChatGPTBot()
|
||||
|
||||
elif bot_type == 'openAI':
|
||||
# OpenAI 官方对话模型API
|
||||
from bot.openai.open_ai_bot import OpenAIBot
|
||||
return OpenAIBot()
|
||||
raise RuntimeError
|
||||
|
||||
@@ -7,6 +7,8 @@ from config import conf
|
||||
user_session = dict()
|
||||
last_session_refresh = time.time()
|
||||
|
||||
|
||||
# ChatGPT web接口 (暂时不可用)
|
||||
class ChatGPTBot(Bot):
|
||||
def __init__(self):
|
||||
config = {
|
||||
|
||||
30
bot/openai/open_ai_bot.py
Normal file
30
bot/openai/open_ai_bot.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from bot.bot import Bot
|
||||
from config import conf
|
||||
from common.log import logger
|
||||
import openai
|
||||
|
||||
|
||||
# OpenAI对话模型API (可用)
|
||||
class OpenAIBot(Bot):
|
||||
def __init__(self):
|
||||
openai.api_key = conf().get('open_ai_api_key')
|
||||
|
||||
def reply(self, query, context=None):
|
||||
logger.info("[OPEN_AI] query={}".format(query))
|
||||
try:
|
||||
response = openai.Completion.create(
|
||||
model="text-davinci-003", #对话模型的名称
|
||||
prompt=query,
|
||||
temperature=0.9, #值在[0,1]之间,越大表示回复越具有不确定性
|
||||
max_tokens=1200, #回复最大的字符数
|
||||
top_p=1,
|
||||
frequency_penalty=0.0, #[-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||
presence_penalty=0.2, #[-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||
stop=["#"]
|
||||
)
|
||||
res_content = response.choices[0]["text"].strip()
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
logger.info("[OPEN_AI] reply={}".format(res_content))
|
||||
return res_content
|
||||
Reference in New Issue
Block a user