langchain glm
v0.0.2
本專案透過langchain的基礎元件,實現了完整的支援智能體和相關任務架構。底層採用智譜AI的最新的GLM-4 All Tools
, 透過智譜AI的API接口, 能夠自主理解用戶的意圖,規劃複雜的指令,並能夠調用一個或多個工具(例如網絡瀏覽器、Python解釋器和文字到圖像模型)以完成複雜的任務。
圖|GLM-4 All Tools 和客製化GLMs(智能體)的整體流程。
包路徑 | 說明 |
---|---|
agent_toolkits | 平台工具AdapterAllTool適配器, 是一個用於為各種工具提供統一介面的平台適配器工具,目的是在不同平台上實現無縫整合和執行。該工具透過適配特定的平台參數,確保相容性和一致的輸出。 |
agents | 定義AgentExecutor的輸入、輸出、智能體會話、工具參數、工具執行策略的封裝 |
callbacks | 抽象AgentExecutor過程中的一些互動事件,透過事件展示訊息 |
chat_models | zhipuai sdk的封裝層,提供langchain的BaseChatModel集成,格式化輸入輸出為訊息體 |
embeddings | zhipuai sdk的封裝層,提供langchain的Embeddings集成 |
utils | 一些會話工具 |
正式的Python (3.8, 3.9, 3.10, 3.11, 3.12)
使用前請設定環境變數
ZHIPUAI_API_KEY
,值為智譜AI的API Key。
import getpass
import os
os . environ [ "ZHIPUAI_API_KEY" ] = getpass . getpass ()
from langchain_glm import ChatZhipuAI
llm = ChatZhipuAI ( model = "glm-4" )
from langchain_core . tools import tool
@ tool
def multiply ( first_int : int , second_int : int ) -> int :
"""Multiply two integers together."""
return first_int * second_int
@ tool
def add ( first_int : int , second_int : int ) -> int :
"Add two integers."
return first_int + second_int
@ tool
def exponentiate ( base : int , exponent : int ) -> int :
"Exponentiate the base to the exponent power."
return base ** exponent
from operator import itemgetter
from typing import Dict , List , Union
from langchain_core . messages import AIMessage
from langchain_core . runnables import (
Runnable ,
RunnableLambda ,
RunnableMap ,
RunnablePassthrough ,
)
tools = [ multiply , exponentiate , add ]
llm_with_tools = llm . bind_tools ( tools )
tool_map = { tool . name : tool for tool in tools }
def call_tools ( msg : AIMessage ) -> Runnable :
"""Simple sequential tool calling helper."""
tool_map = { tool . name : tool for tool in tools }
tool_calls = msg . tool_calls . copy ()
for tool_call in tool_calls :
tool_call [ "output" ] = tool_map [ tool_call [ "name" ]]. invoke ( tool_call [ "args" ])
return tool_calls
chain = llm_with_tools | call_tools
chain . invoke (
"What's 23 times 7, and what's five times 18 and add a million plus a billion and cube thirty-seven"
)
code_interpreter:使用sandbox
指定代碼沙盒環境, 預設= auto,即自動呼叫沙盒環境執行代碼。 設定sandbox = none,不啟用沙盒環境。
web_browser:使用web_browser
指定瀏覽器工具。 drawing_tool:使用drawing_tool
指定繪圖工具。
from langchain_glm . agents . zhipuai_all_tools import ZhipuAIAllToolsRunnable
agent_executor = ZhipuAIAllToolsRunnable . create_agent_executor (
model_name = "glm-4-alltools" ,
tools = [
{ "type" : "code_interpreter" , "code_interpreter" : { "sandbox" : "none" }},
{ "type" : "web_browser" },
{ "type" : "drawing_tool" },
multiply , exponentiate , add
],
)
from langchain_glm . agents . zhipuai_all_tools . base import (
AllToolsAction ,
AllToolsActionToolEnd ,
AllToolsActionToolStart ,
AllToolsFinish ,
AllToolsLLMStatus
)
from langchain_glm . callbacks . agent_callback_handler import AgentStatus
chat_iterator = agent_executor . invoke (
chat_input = "看下本地文件有哪些,告诉我你用的是什么文件,查看当前目录"
)
async for item in chat_iterator :
if isinstance ( item , AllToolsAction ):
print ( "AllToolsAction:" + str ( item . to_json ()))
elif isinstance ( item , AllToolsFinish ):
print ( "AllToolsFinish:" + str ( item . to_json ()))
elif isinstance ( item , AllToolsActionToolStart ):
print ( "AllToolsActionToolStart:" + str ( item . to_json ()))
elif isinstance ( item , AllToolsActionToolEnd ):
print ( "AllToolsActionToolEnd:" + str ( item . to_json ()))
elif isinstance ( item , AllToolsLLMStatus ):
if item . status == AgentStatus . llm_end :
print ( "llm_end:" + item . text )
我們提供了一個整合的demo,可以直接運行,查看效果。
fastapi = " ~0.109.2 "
sse_starlette = " ~1.8.2 "
uvicorn = " >=0.27.0.post1 "
# webui
streamlit = " 1.34.0 "
streamlit-option-menu = " 0.3.12 "
streamlit-antd-components = " 0.3.1 "
streamlit-chatbox = " 1.1.12.post4 "
streamlit-modal = " 0.1.0 "
streamlit-aggrid = " 1.0.5 "
streamlit-extras = " 0.4.2 "
python tests/assistant/server/server.py
python tests/assistant/start_chat.py
展示