当前位置: 首页 > article >正文

小黑大语言模型应用探索:langchain智能体构造源码demo搭建1(初步流程)

导入工具包

rom langchain_core.tools import BaseTool
from typing import Sequence, Optional, List
from langchain_core.prompts import BasePromptTemplate
import re
from langchain_core.tools import tool
from langchain_core.prompts.chat import (ChatPromptTemplate,HumanMessagePromptTemplate,SystemMessagePromptTemplate,
)
from langchain.chains.llm import LLMChain
from langchain_openai import ChatOpenAI

langchain初始化智能体源码中prompt

PREFIX = 'Respond to the human as helpfully and accurately as possible. You have access to the following tools:'
SUFFIX = 'Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation:.\nThought:'
HUMAN_MESSAGE_TEMPLATE = '''{input}{agent_scratchpad}'''
FORMAT_INSTRUCTIONS = '''Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).Valid "action" values: "Final Answer" or {tool_names}Provide only ONE action per $JSON_BLOB, as shown:

{{{{
“action”: $TOOL_NAME,
“action_input”: $INPUT
}}}}


Follow this format:Question: input question to answer
Thought: consider previous and subsequent steps
Action:

$JSON_BLOB

Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:

{{{{
“action”: “Final Answer”,
“action_input”: “Final response to human”
}}}}


prompt生成函数

def create_prompt(tools: Sequence[BaseTool],prefix: str = PREFIX,suffix: str = SUFFIX,human_message_template: str = HUMAN_MESSAGE_TEMPLATE,format_instructions: str = FORMAT_INSTRUCTIONS,input_variables: Optional[List[str]] = None,memory_prompts: Optional[List[BasePromptTemplate]] = None,
) -> BasePromptTemplate:tool_strings = []for tool in tools:args_schema = re.sub("}", "}}", re.sub("{", "{{", str(tool.args)))tool_strings.append(f"{tool.name}: {tool.description}, args: {args_schema}")formatted_tools = "\n".join(tool_strings)tool_names = ", ".join([tool.name for tool in tools])format_instructions = format_instructions.format(tool_names=tool_names)template = "\n\n".join([prefix, formatted_tools, format_instructions, suffix])if input_variables is None:input_variables = ["input", "agent_scratchpad"]_memory_prompts = memory_prompts or []messages = [SystemMessagePromptTemplate.from_template(template),*_memory_prompts,HumanMessagePromptTemplate.from_template(human_message_template),]return ChatPromptTemplate(input_variables=input_variables, messages=messages)  # type: ignore[arg-type]

工具定义

@tool
def multiply(first_int: int, second_int: int) -> int:"""将两个整数相乘。"""print('---------multiply-----------------')return first_int * second_int@tool
def add(first_int: int, second_int: int) -> int:"将两个整数相加。"print('---------add-----------------')return first_int + second_int@tool
def exponentiate(base: int, exponent: int) -> int:"指数运算"print('---------exponentiate-----------------')with open('小黑黑.txt', 'w', encoding='utf-8') as f:f.write('小黑黑')return base**exponent

大语言模型接口初始化

zhipu_key = 'a66c6fc7748xxxxxxxxxxxxxxxx7ctC83zWJo'
llm = ChatOpenAI(temperature=0.01,model="glm-4-flash",openai_api_key=zhipu_key,openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
)

定义工作流

tools = [multiply, add, exponentiate]
prompt = create_prompt(tools=tools)
llm = ChatOpenAI(temperature=0.01,model="glm-4-flash",openai_api_key=zhipu_key,openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
)

定义智能体

from langchain.agents import StructuredChatAgent
from langchain.agents.structured_chat.output_parser import StructuredChatOutputParserWithRetries
# 定义智能体
structuredChatAgent = StructuredChatAgent(llm_chain=llm_chain,allowed_tools=[tool.name for tool in tools],output_parser=StructuredChatOutputParserWithRetries())

运行智能体

from langchain.agents.agent import AgentExecutor
# 执行智能体
excuter = AgentExecutor.from_agent_and_tools(agent=structuredChatAgent,tools=tools,callback_manager=None,verbose=True)excuter.invoke("调用api计算3加5乘2等于多少?")

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/2397635.html

相关文章:

  • 极客时间:用 FAISS、LangChain 和 Google Colab 模拟 LLM 的短期与长期记忆
  • leetcode hot100刷题日记——35.子集
  • MybatisPlus(含自定义SQL、@RequiredArgsConstructor、静态工具类Db)
  • React 组件异常捕获机制详解
  • 手眼标定:九点标定、十二点标定、OpenCV 手眼标定
  • [总结]前端性能指标分析、性能监控与分析、Lighthouse性能评分分析
  • React-native的新架构
  • 【Android】MT6835 + MT6631 WiFi进入Meta模式出现WiFi_HQA_OpenAdapter failed
  • Git 全平台安装指南:从 Linux 到 Windows 的详细教程
  • Tree 树形组件封装
  • AI书签管理工具开发全记录(五):后端服务搭建与API实现
  • netTAP 100:在机器人技术中将 POWERLINK 转换为 EtherNet/IP
  • 多模态大语言模型arxiv论文略读(九十八)
  • EXCEL--累加,获取大于某个值的第一个数
  • 【vscode】切换英文字母大小写快捷键如何配置
  • vue笔记-路由
  • 本地部署 DeepSeek R1(最新)【从下载、安装、使用和调用一条龙服务】
  • 域名解析怎么查询?有哪些域名解析查询方式?
  • win主机如何结束正在执行的任务进程并重启
  • maven中的maven-resources-plugin插件详解
  • ROS云课基础篇-01-Linux-250529
  • 通俗易懂解析:@ComponentScan 与 @MapperScan 的异同与用法
  • 深入了解 C# 异步编程库 AsyncEx
  • NodeJS全栈开发面试题讲解——P1Node.js 基础与核心机制
  • Vulhub靶场搭建(Ubuntu)
  • C++:参数传递方法(Parameter Passing Methods)
  • 大语言模型的推理能力
  • 基于BERT和GPT2的实现来理解Transformer的结构和原理
  • .net consul服务注册与发现
  • WifiEspNow库函数详解