mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 21:57:14 +08:00
chore: save model args as a dict
This commit is contained in:
@@ -28,6 +28,16 @@ class ChatGPTBot(Bot,OpenAIImage):
|
|||||||
self.tb4chatgpt = TokenBucket(conf().get('rate_limit_chatgpt', 20))
|
self.tb4chatgpt = TokenBucket(conf().get('rate_limit_chatgpt', 20))
|
||||||
|
|
||||||
self.sessions = SessionManager(ChatGPTSession, model= conf().get("model") or "gpt-3.5-turbo")
|
self.sessions = SessionManager(ChatGPTSession, model= conf().get("model") or "gpt-3.5-turbo")
|
||||||
|
self.args ={
|
||||||
|
"model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称
|
||||||
|
"temperature":conf().get('temperature', 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性
|
||||||
|
# "max_tokens":4096, # 回复最大的字符数
|
||||||
|
"top_p":1,
|
||||||
|
"frequency_penalty":conf().get('frequency_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
|
"presence_penalty":conf().get('presence_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
|
"request_timeout": conf().get('request_timeout', None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间
|
||||||
|
"timeout": conf().get('request_timeout', None), #重试超时时间,在这个时间内,将会自动重试
|
||||||
|
}
|
||||||
|
|
||||||
def reply(self, query, context=None):
|
def reply(self, query, context=None):
|
||||||
# acquire reply content
|
# acquire reply content
|
||||||
@@ -82,18 +92,6 @@ class ChatGPTBot(Bot,OpenAIImage):
|
|||||||
reply = Reply(ReplyType.ERROR, 'Bot不支持处理{}类型的消息'.format(context.type))
|
reply = Reply(ReplyType.ERROR, 'Bot不支持处理{}类型的消息'.format(context.type))
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
def compose_args(self):
|
|
||||||
return {
|
|
||||||
"model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称
|
|
||||||
"temperature":conf().get('temperature', 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性
|
|
||||||
# "max_tokens":4096, # 回复最大的字符数
|
|
||||||
"top_p":1,
|
|
||||||
"frequency_penalty":conf().get('frequency_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
|
||||||
"presence_penalty":conf().get('presence_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
|
||||||
"request_timeout": conf().get('request_timeout', None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间
|
|
||||||
"timeout": conf().get('request_timeout', None), #重试超时时间,在这个时间内,将会自动重试
|
|
||||||
}
|
|
||||||
|
|
||||||
def reply_text(self, session:ChatGPTSession, api_key=None, retry_count=0) -> dict:
|
def reply_text(self, session:ChatGPTSession, api_key=None, retry_count=0) -> dict:
|
||||||
'''
|
'''
|
||||||
call openai's ChatCompletion to get the answer
|
call openai's ChatCompletion to get the answer
|
||||||
@@ -107,7 +105,7 @@ class ChatGPTBot(Bot,OpenAIImage):
|
|||||||
raise openai.error.RateLimitError("RateLimitError: rate limit exceeded")
|
raise openai.error.RateLimitError("RateLimitError: rate limit exceeded")
|
||||||
# if api_key == None, the default openai.api_key will be used
|
# if api_key == None, the default openai.api_key will be used
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
api_key=api_key, messages=session.messages, **self.compose_args()
|
api_key=api_key, messages=session.messages, **self.args
|
||||||
)
|
)
|
||||||
# logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"]))
|
# logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"]))
|
||||||
return {"total_tokens": response["usage"]["total_tokens"],
|
return {"total_tokens": response["usage"]["total_tokens"],
|
||||||
@@ -147,10 +145,4 @@ class AzureChatGPTBot(ChatGPTBot):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
openai.api_type = "azure"
|
openai.api_type = "azure"
|
||||||
openai.api_version = "2023-03-15-preview"
|
openai.api_version = "2023-03-15-preview"
|
||||||
|
self.args["deployment_id"] = conf().get("azure_deployment_id")
|
||||||
def compose_args(self):
|
|
||||||
args = super().compose_args()
|
|
||||||
args["deployment_id"] = conf().get("azure_deployment_id")
|
|
||||||
#args["engine"] = args["model"]
|
|
||||||
#del(args["model"])
|
|
||||||
return args
|
|
||||||
@@ -26,6 +26,17 @@ class OpenAIBot(Bot, OpenAIImage):
|
|||||||
openai.proxy = proxy
|
openai.proxy = proxy
|
||||||
|
|
||||||
self.sessions = SessionManager(OpenAISession, model= conf().get("model") or "text-davinci-003")
|
self.sessions = SessionManager(OpenAISession, model= conf().get("model") or "text-davinci-003")
|
||||||
|
self.args = {
|
||||||
|
"model": conf().get("model") or "text-davinci-003", # 对话模型的名称
|
||||||
|
"temperature":conf().get('temperature', 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性
|
||||||
|
"max_tokens":1200, # 回复最大的字符数
|
||||||
|
"top_p":1,
|
||||||
|
"frequency_penalty":conf().get('frequency_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
|
"presence_penalty":conf().get('presence_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
|
"request_timeout": conf().get('request_timeout', None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间
|
||||||
|
"timeout": conf().get('request_timeout', None), #重试超时时间,在这个时间内,将会自动重试
|
||||||
|
"stop":["\n\n\n"]
|
||||||
|
}
|
||||||
|
|
||||||
def reply(self, query, context=None):
|
def reply(self, query, context=None):
|
||||||
# acquire reply content
|
# acquire reply content
|
||||||
@@ -64,14 +75,7 @@ class OpenAIBot(Bot, OpenAIImage):
|
|||||||
def reply_text(self, session:OpenAISession, retry_count=0):
|
def reply_text(self, session:OpenAISession, retry_count=0):
|
||||||
try:
|
try:
|
||||||
response = openai.Completion.create(
|
response = openai.Completion.create(
|
||||||
model= conf().get("model") or "text-davinci-003", # 对话模型的名称
|
prompt=str(session), **self.args
|
||||||
prompt=str(session),
|
|
||||||
temperature=0.9, # 值在[0,1]之间,越大表示回复越具有不确定性
|
|
||||||
max_tokens=1200, # 回复最大的字符数
|
|
||||||
top_p=1,
|
|
||||||
frequency_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
|
||||||
presence_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
|
||||||
stop=["\n\n\n"]
|
|
||||||
)
|
)
|
||||||
res_content = response.choices[0]['text'].strip().replace('<|endoftext|>', '')
|
res_content = response.choices[0]['text'].strip().replace('<|endoftext|>', '')
|
||||||
total_tokens = response["usage"]["total_tokens"]
|
total_tokens = response["usage"]["total_tokens"]
|
||||||
|
|||||||
Reference in New Issue
Block a user