|
import zhipuai |
|
|
|
aclient = None |
|
|
|
client = None |
|
|
|
import os |
|
from zhipuai import ZhipuAI |
|
|
|
def init_client(): |
|
|
|
|
|
global client |
|
|
|
|
|
api_key = os.getenv("ZHIPUAI_API_KEY") |
|
if api_key is None: |
|
raise ValueError("环境变量'ZHIPUAI_API_KEY'未设置,请确保已经定义了API密钥") |
|
|
|
client = ZhipuAI(api_key=api_key) |
|
|
|
|
|
def init_aclient(): |
|
|
|
|
|
global aclient |
|
|
|
|
|
api_key = os.getenv("ZHIPUAI_API_KEY") |
|
if api_key is None: |
|
raise ValueError("环境变量'ZHIPUAI_API_KEY'未设置,请确保已经定义了API密钥") |
|
|
|
def get_response( message, model_name = "glm-3-turbo" ): |
|
if client is None: |
|
init_client() |
|
response = client.chat.completions.create(\ |
|
model=model_name,\ |
|
messages = message, \ |
|
max_tokens = 300, \ |
|
temperature = 0.1 ) |
|
return response.choices[0].message.content |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|