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

导入工具包

python 复制代码
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

python 复制代码
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}'''
python 复制代码
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生成函数

python 复制代码
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]

工具定义

python 复制代码
@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

大语言模型接口初始化

python 复制代码
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/"
)

定义工作流

python 复制代码
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/"
)

定义智能体

python 复制代码
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())

运行智能体

python 复制代码
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等于多少?")






相关推荐
夜晚中的人海几秒前
【C++】异常介绍
android·java·c++
Le1Yu32 分钟前
2025-9-28学习笔记
java·笔记·学习
C++chaofan36 分钟前
项目中为AI添加对话记忆
java·数据结构·人工智能·redis·缓存·个人开发·caffeine
老华带你飞37 分钟前
机电公司管理小程序|基于微信小程序的机电公司管理小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·小程序·机电公司管理小程序
拾忆,想起1 小时前
AMQP协议深度解析:消息队列背后的通信魔法
java·开发语言·spring boot·后端·spring cloud
PH = 71 小时前
Spring Ai Alibaba开发指南
java·后端·spring
AI浩2 小时前
大型语言模型的门控注意力:非线性、稀疏性与无注意力沉没
人工智能·语言模型·自然语言处理
涛声依旧2 小时前
基于springBoot鲜花商城小程序
java·spring·微信小程序
HyperAI超神经3 小时前
AI 论文周报丨视觉语言模型应用/不稳定奇点族新发现/强化学习……一文了解多领域创新趋势与前沿动态
人工智能·ai·语言模型
尘埃不入你眼眸3 小时前
服务器安装Java与nginx与nacos
java·服务器·nginx