mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 19:57:04 +08:00
33 lines
647 B
Python
33 lines
647 B
Python
import openai
|
|
|
|
inst = None
|
|
|
|
|
|
# 为其他模块提供与OpenAI交互的接口
|
|
class OpenAIInteract:
|
|
api_key = ''
|
|
api_params = {}
|
|
|
|
def __init__(self, api_key: str, api_params: dict):
|
|
self.api_key = api_key
|
|
self.api_params = api_params
|
|
|
|
openai.api_key = self.api_key
|
|
|
|
global inst
|
|
inst = self
|
|
|
|
# 请求OpenAI Completion
|
|
def request_completion(self, prompt, stop):
|
|
response = openai.Completion.create(
|
|
prompt=prompt,
|
|
stop=stop,
|
|
**self.api_params
|
|
)
|
|
return response
|
|
|
|
|
|
def get_inst() -> OpenAIInteract:
|
|
global inst
|
|
return inst
|