File size: 1,073 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
import zhipuai

aclient = None

client = None

import os
from zhipuai import ZhipuAI

def init_client():
    
    # 将client设置为全局变量
    global client
    
    # 将ZHIPUAI_API_KEY作为参数值传递给OS
    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():
    
    # 将aclient设置为全局变量
    global aclient

    # 将ZHIPUAI_API_KEY作为参数值传递给OS
    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