File size: 1,305 Bytes
b2e325f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
from http import HTTPStatus
import dashscope

### 参考: 
## export DASHSCOPE_API_KEY="sk-948adb3e65414e55961a9ad9d22d186b"
dashscope.api_key = "sk-948adb3e65414e55961a9ad9d22d186b"


def call_with_messages(prompt):
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': prompt}]
                # {'role': 'user', 'content': '如何做西红柿炒鸡蛋?'}]
    response = dashscope.Generation.call(
        "qwen-turbo",
        messages=messages,
        # set the random seed, optional, default to 1234 if not set
        seed=random.randint(1, 10000),
        # set the result to be "message" format.
        result_format='message',
    )
    if response.status_code == HTTPStatus.OK:
        print(response)
        
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))

    return response['output']['choices'][0]['message']['content'] ### 这里是content的内容,不是message的全部内容。
    
    
# if __name__ == '__main__':
#     # call_with_messages() ### original code here.
#     res = call_with_messages() ## working. 
#     # print(res)