
LangChain 官方文档https://reference.langchain.com/python/langchain models:https://docs.langchain.com/oss/python/langchain/models https://docs.langchain.com/oss/python/integrations/chat https://reference.langchain.com/python/langchain-core/language_models https://docs.langchain.com/oss/python/langchain/models#parametershttps://docs.langchain.com/oss/python/integrations/providers/ollama 提示词文档 https://api-docs.deepseek.com/zh-cn/prompt-library https://reference.langchain.com/python/langchain-core/prompts https://docs.langchain.com/langsmith/prompt-engineering-concepts https://docs.langchain.com/oss/python/langchain/messagespython环境安装想让当前虚拟环境以后默认走清华源可先执行 pip configsetglobal.index-url https://pypi.tuna.tsinghua.edu.cn/simple-安装langchain-安装langchain-openai-安装openai-安装dotenv-安装langchain-core# langchain 提供核心框架Chain, Agent, Memory, Retriever等pip install langchain-i https://pypi.tuna.tsinghua.edu.cn/simple# 提供OpenAI专用组件LLM, Chat, Embeddings等依赖 openai SDKpip install langchain-openai-i https://pypi.tuna.tsinghua.edu.cn/simple pip install openai-i https://pypi.tuna.tsinghua.edu.cn/simple# 通过 python-dotenv 库读取 env 文件中的环境变量并加载到当前运行的环境中pip install python-dotenv-i https://pypi.tuna.tsinghua.edu.cn/simple pip install langchain-core 本地大模型安装环境 pip install-qU langchain-ollama pip install-U ollamalangchain 0.3版本 与1.0版本的写法0.3版本fromlangchain_openaiimportChatOpenAIfromopenaiimportOpenAIimportosfromdotenvimportload_dotenv load_dotenv(encodingutf-8)llmChatOpenAI(modeldeepseek-v3.2,# 配置进环境变量api_keyos.getenv(QWEN_API_KEY),base_urlhttps://dashscope.aliyuncs.com/compatible-mode/v1)## # 2.提供问题并调用llmresponsellm.invoke(你是谁)print(response)print()print(response.content)1.0版本# LangChain1.0版本使用方式,目前主流# 1.导入依赖importosfromdotenvimportload_dotenvfromlangchain.chat_modelsimportinit_chat_model#通过 python-dotenv 库读取 env 文件中的环境变量并加载到当前运行的环境中load_dotenv(encodingutf-8)model2init_chat_model(modeldeepseek-v3,model_provideropenai,api_keyos.getenv(QWEN_API_KEY),base_urlhttps://dashscope.aliyuncs.com/compatible-mode/v1)# 3.调用模型print(model2.invoke(你是谁).content)Model I/O 大模型接口I/O三件套1输入提示词2调用模型3解析输出参数作用本地模型部署 OllamaOllama 本地安装教程OllamaSetup.exe /DIRD:\j\MySoftware\ollama手动配置拉去大模型存放位置环境变量 OLLAMA_MODELS D:\devSoft\Ollama\modelsollama 常用命令Ollama 常用命令速查表|命令|一句话说明||---|---||ollama pull llama3|下载指定模型例llama3。||ollama run llama3|启动并进入该模型交互对话。||ollamalist|列出本机已下载的所有模型。||ollama rm llama3|删除不再需要的模型以节省磁盘。||ollama cp llama3 my-llama3|本地复制/重命名模型。||ollama show llama3|查看模型详细信息参数、大小等。||ollama create my-model-f Modelfile|用自定义 Modelfile 构建新模型。||ollama serve|启动后台服务供 API 调用。||ollama ps|查看当前正在运行的模型进程。||ollama stop llama3|停止正在运行的模型。|/bye提示词langchain 内置消息类型spring AI SYSTEM(system)设定Ai 行为边界/角色/定位。指导AI 的行为和响应方式设置AI 如何解释和回复输入的 USER(user)用户原始提问输入代表用户的输入他们向AI提出的问题命令或陈述 ASSISTANT(assistant)ai返回的响应信息定义为 “助手角色”消息用它可以确保上下文能够连贯的交互记忆对话积累回答 TOOL(tool)桥接外部服务可以进行函数调用如支付数据查询等操作类似调用第3方util工具类LangchainSystemMessage:边界角色定位 HumanMessage用户输入 AIMessage模型输出 ToolMessage(v1.0)/FunctionMessage(v0.3)工具消息提示词模板PromptTemplate 提示词模板 分类1,PromptTemplate:文本生成模型提示词模板用户字符串拼接变量生成提示词2,ChatPromptTemplate聊天模型提示词模板使用与如gpt-3.5turbo,gpt-4等聊天模型 消息模板 a,ChatMessagePromptTemplate b,SystemMessagePromptTemplate c,HumanMessagePromptTemplate d,AIMessagePromptTemplate3,FewshotPromptTemplate4,PipelinePrompt 管道提示词模板用于几个提示词组合在一起使用提示词参数在 LangChain 中PromptTemplate是构建提示词的核心组件它允许我们创建可复用的模板通过变量占位符动态生成提示词。理解其参数对于高效使用至关重要。核心参数详解主要参数 template定义提示词模板的字符串其中包含文本和变量占位符如{name} input_variables 列表指定了模板中使用的变量名称在调用模板时被替换 partial_variables字典用于定义模板中一些固定的变量名。这些值不需要再每次调用时被替换。 函数方法介绍format()给input_variables变量赋值并返回提示词。利用format()进行格式化时就一定要赋值否则会报错。当在template中未设置input_variables则会自动忽略。提示词请看下一章