File size: 1,892 Bytes
d319ff8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import openai
aclient = None
client = None
import os
from openai import OpenAI
def init_client():
# 将client设置为全局变量,以便在其他函数中使用
global client
# 检查是否存在API_KEY环境变量
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
raise ValueError("环境变量'OPENAI_API_KEY'未设置。请确保已经定义了API密钥。")
# 检查是否存在API_BASE环境变量,并据此设置base_url参数
api_base = os.getenv("OPENAI_API_BASE")
if api_base:
client = OpenAI(base_url=api_base, api_key=api_key)
else:
client = OpenAI(api_key=api_key)
def get_response( message ):
if client is None:
init_client()
response = client.chat.completions.create(\
model="gpt-3.5-turbo",\
messages = message, \
max_tokens = 300, \
temperature = 0.1 )
return response.choices[0].message.content
from openai import AsyncOpenAI
def init_aclient():
# 将aclient设置为全局变量,以便在其他函数中使用
global aclient
# 检查是否存在API_KEY环境变量
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
raise ValueError("环境变量'OPENAI_API_KEY'未设置。请确保已经定义了API密钥。")
# 检查是否存在API_BASE环境变量,并据此设置base_url参数
api_base = os.getenv("OPENAI_API_BASE")
if api_base:
aclient = AsyncOpenAI(base_url=api_base, api_key=api_key)
else:
aclient = AsyncOpenAI(api_key=api_key)
async def async_get_response( message ):
if aclient is None:
init_aclient()
response = await aclient.chat.completions.create(\
model="gpt-3.5-turbo",\
messages = message, \
max_tokens = 300, \
temperature = 0.1 )
return response.choices[0].message.content
|