找回密码
 立即注册

1,大模型调用

[复制链接]
admin 发表于 2026-5-31 18:44:25 | 显示全部楼层 |阅读模式
阿里云百炼:https://bailian.console.aliyun.com/
1.jpg

拿到API-KEY:https://bailian.console.aliyun.com/cn-beijing?tab=model#/api-key

1,模型链接https://bailian.console.aliyun.c ... e=model&url=2840915
2.jpg


2,模型选中:https://bailian.console.aliyun.c ... e=model&url=2840914
3.jpg


3,安装依赖:
3.1 如果安装这个依赖那么只支持阿里云
  1. <dependency>
  2.     <groupId>com.alibaba</groupId>
  3.     <artifactId>dashscope-sdk-java</artifactId>
  4.     <!-- 请将 'the-latest-version' 替换为最新版本号:https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
  5.     <version>the-latest-version</version>
  6. </dependency>
复制代码
3.2 如果按照下面依赖就可以通用:
  1. <dependencies>
  2.         <!-- Open AI Java 客户端库 -->
  3.         <dependency>
  4.             <groupId>com.openai</groupId>
  5.             <artifactId>openai-java</artifactId>
  6.             <version>4.30.0</version>
  7.         </dependency>
  8.     </dependencies>
复制代码
4,实操案例:
4.1创建AiConfig配置类:
  1. package com.jinhei;
  2. /**
  3. * AI 配置类 - 用于存储连接AI服务所需的配置信息
  4. */
  5. public class AiConfig {
  6.     /**
  7.      * API密钥
  8.      */
  9.     public static final String API_KEY = "sk-a6bb7bb64xxxxxxxxx";
  10.     /**
  11.      * 服务地址
  12.      */
  13.     public static final String BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1";
  14.     /**
  15.      * AI模型名称
  16.      */
  17.     public static final String LLM_NAME = "qwen3.7-plus";
  18. }
复制代码
4.2调用大模型:
  1. package com.jinhei;
  2. import com.openai.client.OpenAIClient;
  3. import com.openai.client.okhttp.OpenAIOkHttpClient;
  4. import com.openai.models.chat.completions.ChatCompletion;
  5. import com.openai.models.chat.completions.ChatCompletionCreateParams;
  6. public class AiChat {
  7.     public static void main(String[] args) {
  8.         // 第1步:创建AI客户端,创建与AI服务的链接通道
  9.         OpenAIClient aiClient = OpenAIOkHttpClient.builder()
  10.                 .apiKey(AiConfig.API_KEY) // 设置API密钥
  11.                 .baseUrl(AiConfig.BASE_URL) // 设置API地址
  12.                 .build();// 建造完成后,创建客户端
  13.         // 第2步:准备要问的问题
  14.         String prompt = "你是谁";
  15.         // 第3步:ChatCompletionCreateParams对象,并设置参数,把问题打包成AI能理解的格式
  16.         ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
  17.                 .addUserMessage(prompt) //添加用户消息: 你说的话
  18.                 .model(AiConfig.LLM_NAME) //指定用哪个AI大模型来处理
  19.                 .build();//打包完成 - 准备发送
  20.         //第4步: 发送请求并获取回复 - 把信寄出去, 等待回信
  21.         ChatCompletion chatCompletion = aiClient.chat() //打开聊天功能
  22.                 .completions() //开启补全模式(AI回复)
  23.                 .create(params); //发送请求并等待响应
  24.         // System.out.println(chatCompletion);
  25. //        ChatCompletion{id=chatcmpl-e8e30a20-2cb3-90ab-ab05-34916c600954,
  26. //            choices=[Choice{finishReason=stop, index=0, logprobs=,
  27. //            message=ChatCompletionMessage{content=我是通义千问(Qwen),是由阿里巴巴集团通义实验室自主研发的大语言模型。
  28. //            作为一个人工智能,我致力于成为你的思考伙伴,为你提供清晰、准确、有帮助的回答。有什么我可以帮你的吗?,
  29. //            refusal=, role=assistant, annotations=, audio=, functionCall=, toolCalls=, additionalProperties={reasoning_content=用户问我“你是谁”。
  30. //            我需要回答我是通义千问(Qwen),由阿里巴巴集团通义实验室自主研发的大语言模型。
  31.          // 第5步: 提取AI的会多发 - 拆开回信, 取出内容
  32.         String message = chatCompletion.choices().get(0).message().content().get();
  33.         //第6步: 显示结果
  34.         System.out.println(message);
  35.     }
  36. }
复制代码
02.jpg
5,如果要切换成 deepseek模型 就把 AiConfig配置改下即可 https://platform.deepseek.com/
接口文档:https://api-docs.deepseek.com/zh-cn/

01.jpg 02.jpg 03.jpg
aiagent.zip (3.96 KB, 下载次数: 0, 售价: 40 金豆)

QQ|网站地图|Archiver|小黑屋|金黑网络 ( 粤ICP备2021124338号 )

网站建设,微信公众号小程序制作,商城系统开发,高端系统定制,app软件开发,智能物联网开发,直播带货系统等

Powered by Www.Jinhei.Cn

Copyright © 2013-2024 深圳市金黑网络技术有限公司 版权所有

快速回复 返回顶部 返回列表