Skip to content
目录

Python 代码调用

调用语言模型

非流式

python
import requests
if __name__ == '__main__':
    params = {
        'model': 'taichu_llm',
        'messages': [{"role": "user", "content": "你好"}],
        'stream': False
    }
    api = 'https://ai-maas.wair.ac.cn/maas/v1/chat/completions'
    headers = {'Authorization': 'Bearer $API_KEY'}
    response = requests.post(api, json=params, headers=headers)
    if response.status_code == 200:
        print(response.json())
    else:
        body = response.content.decode('utf-8')
        print(f'request failed,status_code:{response.status_code},body:{body}')

流式调用

python
import requests    
if __name__ == '__main__':
    params = {
        'model': 'taichu_llm',
        'messages': [{"role": "user", "content": "你好"}],
        'stream': True
    }
    api = 'https://ai-maas.wair.ac.cn/maas/v1/chat/completions'
    headers = {'Authorization': 'Bearer $API_KEY'}
    response = requests.post(api, json=params, headers=headers, stream=True)
    if response.status_code == 200:
        response.encoding = 'utf-8'
        for line in response.iter_lines(decode_unicode=True):
            print(line)
    else:
        body = response.content.decode('utf-8')
        print(f'request failed,status_code:{response.status_code},body:{body}')

调用图文模型

非流式

python
import requests
if __name__ == '__main__':
    params = {
        "stream": False,
        "model": "taichu2_mm",
        "messages": [
            {
                "content": [
                    {
                        "type": "text",
                        "text": "图片中的人口总量是多少?男性多还是女性多?"
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "https://zdtc-cdn.wair.ac.cn/assets/caption_2_file_1.jpeg"
                        }
                    }
                ],
                "role": "user"
            }
        ]
    }
    api = 'https://ai-maas.wair.ac.cn/maas/v1/chat/completions'
    headers = {'Authorization': 'Bearer $API_KEY'}
    response = requests.post(api, json=params, headers=headers)
    if response.status_code == 200:
        print(response.json())
    else:
        body = response.content.decode('utf-8')
        print(f'request failed,status_code:{response.status_code},body:{body}')

流式调用

python
import requests    
if __name__ == '__main__':
    params = {
        "stream": True,
        "model": "taichu2_mm",
        "messages": [
            {
                "content": [
                    {
                        "type": "text",
                        "text": "图片中的人口总量是多少?男性多还是女性多?"
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "https://zdtc-cdn.wair.ac.cn/assets/caption_2_file_1.jpeg"
                        }
                    }
                ],
                "role": "user"
            }
        ]
    }
    api = 'https://ai-maas.wair.ac.cn/maas/v1/chat/completions'
    headers = {'Authorization': 'Bearer $API_KEY'}
    response = requests.post(api, json=params, headers=headers, stream=True)
    if response.status_code == 200:
        response.encoding = 'utf-8'
        for line in response.iter_lines(decode_unicode=True):
            print(line)
    else:
        body = response.content.decode('utf-8')
        print(f'request failed,status_code:{response.status_code},body:{body}')