一、点此标题进入阿里云百炼大模型首页

右上角如果有这个,点击进去开通一下

二、获取API Key

单击创建我的API-KEY即可通过API KEY调用大模型。

三、选择你的开发语言

这里我使用nodejs作为演示,没有安装nodejs的去nodejs官网安装
然后在命令行或者你的项目里,执行下面代码
npm install --save openai
# 或者
yarn add openai

如果安装失败,可以通过配置镜像源的方法来完成安装
npm config set registry https://registry.npmmirror.com/


npm install --save openai
# 或者
yarn add openai

四、调用大模型API

  1. 新建一个hello_qwen.mjs文件

  2. 插入下面代码

import OpenAI from "openai";

try {
    const openai = new OpenAI(
        {
            // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:apiKey: "sk-xxx",
            apiKey: process.env.DASHSCOPE_API_KEY,
            baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
        }
    );
    const completion = await openai.chat.completions.create({
        model: "qwen-plus",  //模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
        messages: [
            { role: "system", content: "You are a helpful assistant." },
            { role: "user", content: "你是谁?" }
        ],
    });
    console.log(completion.choices[0].message.content);
} catch (error) {
    console.log(`错误信息:${error}`);
    console.log("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}

        3.通过命令行运行以下命令来发送API请求 , 在你的文件目录下执行

node hello_qwen.mjs

运行成功后您将会看到输出结果:

如有其它问题可前往阿里云百炼大模型文档说明

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐