QChatGPT/pkg/openai/manager.py

98 lines
2.7 KiB
Python
Raw Normal View History

2022-12-19 22:51:56 +08:00
import logging
2022-12-07 22:27:05 +08:00
import openai
import pkg.openai.keymgr
import pkg.utils.context
import pkg.audit.gatherer
from pkg.openai.modelmgr import ModelRequest, create_openai_model_request
2022-12-07 22:27:05 +08:00
2023-03-05 15:39:13 +08:00
2022-12-07 22:27:05 +08:00
class OpenAIInteract:
2023-03-05 15:39:13 +08:00
"""OpenAI 接口封装
将文字接口和图片接口封装供调用方使用
"""
2022-12-07 22:27:05 +08:00
key_mgr: pkg.openai.keymgr.KeysManager = None
audit_mgr: pkg.audit.gatherer.DataGatherer = None
2022-12-27 22:52:53 +08:00
default_image_api_params = {
"size": "256x256",
}
def __init__(self, api_key: str):
2022-12-07 22:27:05 +08:00
self.key_mgr = pkg.openai.keymgr.KeysManager(api_key)
self.audit_mgr = pkg.audit.gatherer.DataGatherer()
2023-02-10 19:03:25 +08:00
logging.info("文字总使用量:%d", self.audit_mgr.get_total_text_length())
openai.api_key = self.key_mgr.get_using_key()
2022-12-07 22:27:05 +08:00
pkg.utils.context.set_openai_manager(self)
2022-12-07 22:27:05 +08:00
2022-12-11 16:10:12 +08:00
# 请求OpenAI Completion
2023-03-18 23:57:28 +08:00
def request_completion(self, prompts) -> tuple[str, int]:
2023-03-05 15:39:13 +08:00
"""请求补全接口回复
Parameters:
prompts (str): 提示语
Returns:
str: 回复
"""
config = pkg.utils.context.get_config()
2023-03-03 00:07:53 +08:00
# 根据模型选择使用的接口
2023-03-03 14:12:53 +08:00
ai: ModelRequest = create_openai_model_request(
2023-04-02 14:43:34 +08:00
config.completion_api_params['model'],
'user',
2023-03-03 15:20:42 +08:00
config.openai_config["http_proxy"] if "http_proxy" in config.openai_config else None
2023-03-03 14:12:53 +08:00
)
ai.request(
2023-03-03 00:07:53 +08:00
prompts,
2022-12-27 22:52:53 +08:00
**config.completion_api_params
2022-12-07 22:27:05 +08:00
)
response = ai.get_response()
logging.debug("OpenAI response: %s", response)
2023-03-18 23:57:28 +08:00
# 记录使用量
current_round_token = 0
if 'model' in config.completion_api_params:
self.audit_mgr.report_text_model_usage(config.completion_api_params['model'],
ai.get_total_tokens())
2023-03-18 23:57:28 +08:00
current_round_token = ai.get_total_tokens()
elif 'engine' in config.completion_api_params:
self.audit_mgr.report_text_model_usage(config.completion_api_params['engine'],
response['usage']['total_tokens'])
2023-03-18 23:57:28 +08:00
current_round_token = response['usage']['total_tokens']
2023-03-18 23:57:28 +08:00
return ai.get_message(), current_round_token
2022-12-07 22:27:05 +08:00
2023-03-05 15:39:13 +08:00
def request_image(self, prompt) -> dict:
"""请求图片接口回复
Parameters:
prompt (str): 提示语
2023-03-05 15:39:13 +08:00
Returns:
dict: 响应
"""
config = pkg.utils.context.get_config()
params = config.image_api_params
2022-12-27 22:52:53 +08:00
response = openai.Image.create(
prompt=prompt,
n=1,
**params
2022-12-27 22:52:53 +08:00
)
self.audit_mgr.report_image_model_usage(params['size'])
2022-12-27 22:52:53 +08:00
return response