|
import openai |
|
|
|
aclient = None |
|
|
|
client = None |
|
|
|
import os |
|
from openai import OpenAI |
|
|
|
def init_client(): |
|
|
|
global client |
|
|
|
|
|
api_key = os.getenv("OPENAI_API_KEY") |
|
if api_key is None: |
|
raise ValueError("环境变量'OPENAI_API_KEY'未设置。请确保已经定义了API密钥。") |
|
|
|
|
|
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(): |
|
|
|
global aclient |
|
|
|
|
|
api_key = os.getenv("OPENAI_API_KEY") |
|
if api_key is None: |
|
raise ValueError("环境变量'OPENAI_API_KEY'未设置。请确保已经定义了API密钥。") |
|
|
|
|
|
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 |
|
|