內容來自黑馬程序員
這里寫目錄標題
- 認識AI和大模型
- 大模型應用開發
- 模型部署方案對比
- 模型部署-云服務
- 模型部署-本地部署
- 調用大模型
- 什么是大模型應用
- 傳統應用和大模型應用
- 大模型應用
- 大模型應用開發技術架構
- SpringAI
- 對話機器人
- 快速入門
- 會話日志
- 會話記憶
認識AI和大模型
- AI的發展
AI,人工智能(Artificial Intelligence),使機器能夠像人類一樣思考、學習和解決問題的技術。
- 大語言模型
我們所熟知的大模型(Large Language Models, LLM),例如GP、DeepSeek底層都是采用Transformer神經網絡模型。
- Transformer
大模型應用開發
模型部署方案對比
模型部署-云服務
國內知名的云服務平臺都提供了全球知名的大模型的私有部署功能,甚至還提供了這些模型的API開發平臺,無需部署就能提供。
云平臺 | 公司 | 地址 |
---|---|---|
阿里百煉 | 阿里巴巴 | https://bailian.console.aliyun.com |
干帆平臺 | 百度 | https://console.bce.baidu.com/qianfan/overview |
騰訊TI平臺 | 騰訊 | https://cloud.tencent.com/product/ti |
SiliconCloud | 硅基流動 | https://siliconflow.cn/zh-cn/siliconcloud |
火山方舟-火山引擎 | 字節跳動 | https://www.volcengine.com/product/ark |
模型部署-本地部署
本地部署最簡單的一種方案就是使用ollama,官網地址:https//ollama.com
調用大模型
以下是DeepSeek官方給出的一段API實例代碼:
# Please install OpenAI SDK first: `pip3 install openai`from openai import OpenAIclient = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")response = client.chat.completions.create(model="deepseek-chat",messages=[{"role": "system", "content": "You are a helpful assistant"},{"role": "user", "content": "Hello"},],stream=False
)print(response.choices[0].message.content)
什么是大模型應用
傳統應用和大模型應用
大模型應用是基于大模型的推理、分析、生成能力,結合傳統編程能力,開發出的各種應用。
大模型應用
大模型 | 對話產品 | 公司 | 地址 |
---|---|---|---|
GPT-3.5、GPT-4o | ChatGPT | OpenAI | https://chatgpt.com/ |
Claude 3.5 | Claude AI | Anthropic | https://claude.ai/chats |
DeepSeek-R1 | DeepSeek | DeepSeek | https://www.deepseek.com/ |
文心大模型3.5 | 文心一言 | 百度 | https://yiyan.baidu.com/ |
星火3.5 | 訊飛星火 | 科大訊飛 | https://xinghuo.xfyun.cn/desk |
Qwen-Max | 通義千問 | 阿里巴巴 | https://tongyi.aliyun.com/qianwen/ |
Moonshoot | Kimi | 月之暗面 | https://kimi.moonshot.cn/ |
Yi-Large | 零一萬物 | 零一萬物 | https://platform.lingyiwanwu.com/ |
大模型應用開發技術架構
SpringAI
對話機器人
快速入門
- 引入依賴
<dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>${spring-ai.version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
- 配置模型
spring:ai:ollama:base-url: http://localhost:11434chat:model: deepseek-r1:7b
spring:ai:openai:base-url: https://dashscope.aliyuncs.com/compatible-modelapi-key: ${OPENAI_API_KEY}chat:options:model: qwen-max # 模型名稱temperature: 0.8 # 模型溫柔度,值越大,輸出結果越隨機
- 配置客戶端
@Beanpublic ChatClient chatClient(OllamaChatModel model){return ChatClient.builder(model).defaultSystem("你是可愛的助手,名字叫小團團").build();}
String content = chatClient.prompt().user("你是誰?").call().content();// 流式
Flux<String> content = chatClient.prompt().user("你是誰?").stream().content();
會話日志
SpringAI利用了AOP原理提供了AI會話是的攔截、增強等功能,也就是Advisor。
@Bean
public ChatClient chatClient(OllamaChatModel model){return ChatClient.builder(model) // 創建ChatClient工廠實例.defaultSystem("你是可愛的助手,名字叫小團團") .defaultAdvisors(new SimpleLoggerAdvisor()) // 配置日志Advisor.build(); // 構建ChatClient實例
}
會話記憶
大模型是具備記憶能力的,要想讓大模型記住之前聊天的內容,唯一的辦法就是把之前聊天的內容與新的提示詞一起發給大模型。
from openai import OpenAI#1.初始化OpenAI客戶端
from openai import OpenAI#1.初始化OpenAI客戶端
client = OpenAI(api_key="<DeepSeek API Key>",base_url="https://api.deepseek.com")#2.發送http請求到大模型
response = client.chat.completions.create(model="deepseek-r1",temperature=0.7,messages=[{"role":"system","content":"你是一個熱心的AI助手,你的名字叫小團團"},{"role":"user","content":"你好,你是誰? "},],stream=False
)
# 3.打印返回結果
print(response.choices[o].message.content)
- 定義會話存儲方式
public interface ChatMemory {void add(String conversationId, List<Message> messages);List<Message> get(String conversationId, int lastN);void clear(String conversationId);}
@Beanpublic ChatMemory chatMemory(){return new InMemoryChatMemory();}
- 配置會話記憶
@Beanpublic ChatClient chatClient(OllamaChatModel model){return ChatClient.builder(model).defaultSystem("你是可愛的助手,名字叫小團團").defaultAdvisors(new SimpleLoggerAdvisor(),new MessageChatMemoryAdvisor(chatMemory())).build();}
- 添加會話id
Flux<String> content = chatclient.prompt().user("你好,我叫小明”).advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)).stream().content();
添加會話id到AdvisorContext上下文中