上一篇文章我们已经介绍了智能体和大模型AI的区别,现在我们开始搭建自己的智能体进行工作
DeepSeek 提供强大的 大语言模型(LLM),可用于:
你可以基于 DeepSeek 的模型构建:
目前(2024年),DeepSeek 可能提供 API 访问(类似 OpenAI 的 GPT API),你可以:
import requests
api_key = "YOUR_DEEPSEEK_API_KEY"
url = "https://api.deepseek.com/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "deepseek-v3",
"messages": [
{"role": "user", "content": "你好,介绍一下你自己!"}
]
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
如果 DeepSeek 提供 开源模型(如 DeepSeek-V2/V3),你可以:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "deepseek-ai/deepseek-v3"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
input_text = "如何构建一个 AI 智能体?"
inputs = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**inputs, max_length=200)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
你可以使用 LangChain 或 LlamaIndex 这样的框架,让 DeepSeek 模型具备 记忆、工具调用、自主决策 等能力。
from langchain.agents import AgentExecutor, create_react_agent
from langchain.tools import Tool
from langchain_community.llms import DeepSeek
# 初始化 DeepSeek LLM
llm = DeepSeek(api_key="YOUR_API_KEY")
# 定义工具(如网络搜索、计算器)
tools = [
Tool(
name="Search",
func=lambda query: "搜索结果:" + query,
description="用于搜索网络信息"
)
]
# 构建智能体
agent = create_react_agent(llm, tools)
agent_executor = AgentExecutor(agent=agent, tools=tools)
# 运行智能体
response = agent_executor.invoke({"input": "2024年巴黎奥运会的举办时间?"})
print(response["output"])
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
memory=memory,
verbose=True
)
agent_executor.invoke({"input": "我叫张三,记住我!"})
agent_executor.invoke({"input": "我是谁?"}) # 输出 "你是张三!"
import gradio as gr
def respond(message, history):
response = agent_executor.invoke({"input": message})
return response["output"]
gr.ChatInterface(respond).launch()
步骤 | 方法 |
---|---|
1. 获取 DeepSeek 模型 | API 或 本地部署 |
2. 构建智能体逻辑 | LangChain / 自定义代码 |
3. 增强能力 | 工具调用、记忆存储 |
4. 部署应用 | Web/API/聊天机器人 |
下篇文章卓伊凡 实践给大家搭建一个智能体,为我写小说的智能体,
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。