只需一个api就能使用python和php调用阿里云上所有ai大模型

只需一个api就能使用python和php调用阿里云上所有ai大模型

阿里云推出了自家的通义千问和开源的ai大模型调用平台,只要开通apikey就能使用这些模型,包含文生图、ai对话、llama2、sd等几十个ai大模型,后期估计会接入更多,api收费方式也很直接。

只需一个api就能使用python和php调用阿里云上所有ai大模型

我们来一步一步开通调用吧。

1、开通DashScope灵积模型服务:https://dashscope.console.aliyun.com/overview

只需一个api就能使用python和php调用阿里云上所有ai大模型

2、创建apikey :https://dashscope.console.aliyun.com/apiKey

只需一个api就能使用python和php调用阿里云上所有ai大模型

3、查看计费方式

只需一个api就能使用python和php调用阿里云上所有ai大模型

4、直接调用吧

我们以php为例

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = [
    'model' => 'qwen-turbo',
    // 'model' => 'qwen-plus-v1',
    'input' => ['prompt' => '你是谁?'],
];

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_UNICODE));


curl_setopt($ch, CURLOPT_POST, 1);

// Set the API key as an HTTP header
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer apikey";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Send the request and parse the response
$response = curl_exec($ch);
$response_data = json_decode($response, true);

if (curl_errno($ch)) {
  // If there was an error executing the cURL request, print it out
  echo 'Error: ' . curl_error($ch);
} else {
  // Otherwise, print the response from the GPT-3 API
  var_dump($response_data) ;
 // echo $response_data['data']['text'];
}

curl_close($ch);

?>

python

先导出key

export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

安装dashscope

pip install dashscope

开始使用吧

from http import HTTPStatus
from dashscope import Generation


def call_with_messages():
    messages = [{'role': 'system', 'content': '你是达摩院的生活助手机器人。'}, 
                {'role': 'user', 'content': '如何做西红柿鸡蛋?'}]
    gen = Generation()
    response = gen.call(
        Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message', # set the result is message format.
    )
    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
        ))     

if __name__ == '__main__':
    call_with_messages()

{{collectdata}}

网友评论0