【阿里云-百炼大模型API方式接入】
·
注册及登录
通义千问:
网址:https://bailian.console.aliyun.com/。
模型用途:
文生文:qwen-turbo、qwen-plus、qwen-max、qwen-long等等
文生图片:wanx-v1等等
图生视频:wanx-style-repaint-v1等等
图生图:wanx2.1-imageedit
注册方式:
1、注册阿里云账号
2、实名认证
3、获取API key(https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=2712195)
接入方式
基于前后端分离的若依框架进行大模型API接入,仅限于同步返回结果,不适用于流式输出:
- 前端代码
const handleNormalRequest = async (params: RequestParams) => {
// 构造GET的Query参数
const queryString = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
queryString.append(key, String(value));
});
const fullUrl = `${apiUrl}?${queryString.toString()}`;
const response = await fetch(fullUrl, {
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
signal: AbortSignal.timeout(30000) // 30秒超时
});
if (!response.ok) {
throw new Error(`请求失败:${response.status} ${response.statusText}`);
}
const result: ApiResponse = await response.json();
if (result.code === 200) {
// 修正:取data中的content字段
setOutput(result.data);
} else {
throw new Error(result.message || '生成失败,请稍后重试');
}
};
- 后端controller
@Resource
private ILargeModelIntegrationService iLargeModelIntegrationService;
/**
* 文字处理
*/
@GetMapping("/textGeneration")
@Anonymous
public R<String> textGeneration(String model, String prompt)
{
return R.ok(iLargeModelIntegrationService.textGeneration(model, prompt));
}
- 后端service
@Override
public String textGeneration(String model, String prompt) {
return LargeModelIntegration.textGeneration(model, prompt);
}
- 后端LargeModelIntegration工具类
public static String textGeneration(String model, String content) {
Assert.isTrue(StringUtils.isNotBlank(model), "请选择模型名称");
Assert.isTrue(StringUtils.isNotBlank(content), "请输入提问内容");
Map<String,String> headers=new HashMap<>();
headers.put("Authorization","Bearer " + ALIYUN_API_KEY);
headers.put("Content-Type", "application/json");
JSONObject param=new JSONObject();
param.put("model",model);
JSONObject input =new JSONObject();
JSONArray messages = new JSONArray();
JSONObject userMessage = new JSONObject();
userMessage.put("role", "user"); // 用户角色
userMessage.put("content", content); // 用户提问内容
messages.add(userMessage); // 将单条消息加入数组
input.put("messages", messages); // 赋值给input的messages
param.put("input",input);
JSONObject parameters=new JSONObject();
parameters.put("result_format", "text");
parameters.put("temperature", 0.3);
parameters.put("top_p", 0.8);
parameters.put("max_tokens", 1024);
param.put("parameters", parameters);
try {
log.info("请求LargeModelIntegration#textGeneration入参model:{},content:{}:", model ,
URLDecoder.decode(content, StandardCharsets.UTF_8));
String result = HttpUtil.postJson(ALIYUN_TEXT_API_URL, headers, param.toJSONString());
log.info("请求LargeModelIntegration#textGeneration出参:"+result);
JSONObject resultJson=JSONObject.parseObject(result);
if ("qwen-long".equals(model)){
return resultJson.getJSONObject("output").getList("choices", JSONObject.class).get(0).getJSONObject("message").getString("content");
} else {
return resultJson.getJSONObject("output").getString("text");
}
}catch (Exception e){
log.error("LargeModelIntegration#textGeneration调用异常"+e);
throw new RuntimeException("LargeModelIntegration#textGeneration调用异常");
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)