{"id":15055298,"url":"https://github.com/MetaGLM/langchain-zhipuai","last_synced_at":"2025-09-24T21:30:57.096Z","repository":{"id":245798229,"uuid":"814999775","full_name":"MetaGLM/langchain-zhipuai","owner":"MetaGLM","description":"基于 Langchain，快速集成GLM-4 AllTools 功能的插件","archived":false,"fork":false,"pushed_at":"2024-07-17T08:22:39.000Z","size":3755,"stargazers_count":48,"open_issues_count":1,"forks_count":7,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-09-08T21:11:42.674Z","etag":null,"topics":["chatbot","glm","gpt","gpt-4","langchain","llama","ollama","rag"],"latest_commit_sha":null,"homepage":"https://open.bigmodel.cn/?utm_campaign=open\u0026_channel_track_key=OWTVNma9","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MetaGLM.png","metadata":{"files":{"readme":"README.en.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-14T06:25:01.000Z","updated_at":"2025-06-06T03:15:08.000Z","dependencies_parsed_at":"2024-06-24T08:26:10.931Z","dependency_job_id":"dab6e6b1-1210-45a3-8ea8-e179766939fd","html_url":"https://github.com/MetaGLM/langchain-zhipuai","commit_stats":{"total_commits":99,"total_committers":2,"mean_commits":49.5,"dds":"0.010101010101010055","last_synced_commit":"29efaeacbcb4db7572c2f09e60a4196771eefd24"},"previous_names":["metaglm/langchain-zhipuai","metaglm/langchain-glm"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/MetaGLM/langchain-zhipuai","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaGLM%2Flangchain-zhipuai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaGLM%2Flangchain-zhipuai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaGLM%2Flangchain-zhipuai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaGLM%2Flangchain-zhipuai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MetaGLM","download_url":"https://codeload.github.com/MetaGLM/langchain-zhipuai/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaGLM%2Flangchain-zhipuai/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274720960,"owners_count":25337438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","glm","gpt","gpt-4","langchain","llama","ollama","rag"],"created_at":"2024-09-24T21:40:14.964Z","updated_at":"2025-09-24T21:30:56.532Z","avatar_url":"https://github.com/MetaGLM.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"#  \u003cimg height=\"30\" width=\"30\" src=\"docs/img/MetaGLM.png\"\u003e 🔗 LangChain-GLM\n\n\n## Project Overview\nThis project utilizes the foundational components of LangChain to implement a comprehensive framework\nthat supports intelligent agents and related tasks. The core is built on Zhiyuan AI's latest \nGLM-4 All Tools. Through Zhiyuan AI's API interface, it can autonomously understand user intentions,\nplan complex instructions, and invoke one or more tools (such as web browsers, Python interpreters, \nand text-to-image models) to accomplish intricate tasks.\n\n![all_tools.png](docs/img/all_tools.png)\n\n\u003e Fig. | The overall process of GLM-4 All Tools and custom GLMs (agents).\n\n## Project Structure\n\n| Package Path                                                       | Description                                                   |\n| ------------------------------------------------------------------ | ------------------------------------------------------------- |\n| [agent_toolkits](https://github.com/MetaGLM/langchain-zhipuai/tree/main/langchain_glm/agent_toolkits) | Platform tool AdapterAllTool adapter, a platform adapter tool that provides a unified interface for various tools, aiming for seamless integration and execution across different platforms. This tool adapts to specific platform parameters to ensure compatibility and consistent output. |\n| [agents](https://github.com/MetaGLM/langchain-zhipuai/tree/main/langchain_glm/agents) | Encapsulates the input, output, agent sessions, tool parameters, and tool execution strategies for the AgentExecutor. |\n| [callbacks](https://github.com/MetaGLM/langchain-zhipuai/tree/main/langchain_glm/callbacks) | Abstracts some interactive events during the AgentExecutor process, displaying information through events. |\n| [chat_models](https://github.com/MetaGLM/langchain-zhipuai/tree/main/langchain_glm/chat_models) | A wrapper layer for the Zhipu AI SDK, providing integration with LangChain's BaseChatModel and formatting input and output as message bodies. |\n| [embeddings](https://github.com/MetaGLM/langchain-zhipuai/tree/main/langchain_glm/embeddings) | A wrapper layer for the Zhipu AI SDK, providing integration with LangChain's Embeddings. |\n| [utils](https://github.com/MetaGLM/langchain-zhipuai/tree/main/langchain_glm/utils) | Various session tools.                                         |\n\n\n## 快速使用\n\n- Install from the repository\n  https://github.com/MetaGLM/langchain-glm/releases\n- Install directly from the source using pip\n  ```bash\n  pip install git+https://github.com/MetaGLM/langchain-glm.git -v\n  ```\n- Install from PyPI\n  ```bash\n  pip install langchain-glm\n  ```\n\n\u003e Before using, please set the environment variable `ZHIPUAI_API_KEY` with the value of your Zhipu AI API Key.\n\n#### Tool Usage\n- Set environment variables\n```python\nimport getpass\nimport os\n\nos.environ[\"ZHIPUAI_API_KEY\"] = getpass.getpass()\n\n```\n```python\nfrom langchain_glm import ChatZhipuAI\nllm = ChatZhipuAI(model=\"glm-4\")\n```\n\n\n- example tools:\n```python\nfrom langchain_core.tools import tool\n\n@tool\ndef multiply(first_int: int, second_int: int) -\u003e int:\n    \"\"\"Multiply two integers together.\"\"\"\n    return first_int * second_int\n\n@tool\ndef add(first_int: int, second_int: int) -\u003e int:\n    \"Add two integers.\"\n    return first_int + second_int\n\n@tool\ndef exponentiate(base: int, exponent: int) -\u003e int:\n    \"Exponentiate the base to the exponent power.\"\n    return base**exponent\n```\n- Build Chain\nBind tools to the language model and invoke:\n```python\nfrom operator import itemgetter\nfrom typing import Dict, List, Union\n\nfrom langchain_core.messages import AIMessage\nfrom langchain_core.runnables import (\n    Runnable,\n    RunnableLambda,\n    RunnableMap,\n    RunnablePassthrough,\n)\n\ntools = [multiply, exponentiate, add]\nllm_with_tools = llm.bind_tools(tools)\ntool_map = {tool.name: tool for tool in tools}\n\n\ndef call_tools(msg: AIMessage) -\u003e Runnable:\n    \"\"\"Simple sequential tool calling helper.\"\"\"\n    tool_map = {tool.name: tool for tool in tools}\n    tool_calls = msg.tool_calls.copy()\n    for tool_call in tool_calls:\n        tool_call[\"output\"] = tool_map[tool_call[\"name\"]].invoke(tool_call[\"args\"])\n    return tool_calls\n\n\nchain = llm_with_tools | call_tools\n```\n\n- invoke\n```python\nchain.invoke(\n    \"What's 23 times 7\"\n)\n```\n\n#### Example Code\n\n- Agent Executor\n  Our `glm-4-alltools` model provides platform tools. With `ZhipuAIAllToolsRunnable`, you can easily set up an executor to run multiple tools.\n\n  `code_interpreter`: Use `sandbox` to specify the code sandbox environment.\n    - Default = auto, which automatically uses the sandbox environment to execute code.\n    - Set `sandbox = none` to disable the sandbox environment.\n\n  `web_browser`: Use `web_browser` to specify the browser tool.\n  `drawing_tool`: Use `drawing_tool` to specify the drawing tool.\n\n\n```python\n\nfrom langchain_glm.agents.zhipuai_all_tools import ZhipuAIAllToolsRunnable\nagent_executor = ZhipuAIAllToolsRunnable.create_agent_executor(\n    model_name=\"glm-4-alltools\",\n    tools=[\n        {\"type\": \"code_interpreter\", \"code_interpreter\": {\"sandbox\": \"none\"}},\n        {\"type\": \"web_browser\"},\n        {\"type\": \"drawing_tool\"},\n        multiply, exponentiate, add\n    ],\n)\n\n```\n\n\n- Execute `agent_executor` and Print Results\n  This section uses an agent to run a shell command and prints the output once available. It checks the result type and prints the relevant information.\n  The `invoke` method returns an asynchronous iterator that can handle the agent's output.\n  You can call the `invoke` method multiple times, with each call returning a new iterator.\n  `ZhipuAIAllToolsRunnable` automatically handles state saving and recovery. Some state information is stored within the instance.\n  You can access the status of `intermediate_steps` through the `callback` attribute.\n\n\n```python\nfrom langchain_glm.agents.zhipuai_all_tools.base import (\n    AllToolsAction, \n    AllToolsActionToolEnd,\n    AllToolsActionToolStart,\n    AllToolsFinish, \n    AllToolsLLMStatus\n)\nfrom langchain_glm.callbacks.agent_callback_handler import AgentStatus\n\n\nchat_iterator = agent_executor.invoke(\n    chat_input=\"What's 23 times 7, and what's five times 18 and add a million plus a billion and cube thirty-seven\"\n)\nasync for item in chat_iterator:\n    if isinstance(item, AllToolsAction):\n        print(\"AllToolsAction:\" + str(item.to_json()))\n    elif isinstance(item, AllToolsFinish):\n        print(\"AllToolsFinish:\" + str(item.to_json()))\n    elif isinstance(item, AllToolsActionToolStart):\n        print(\"AllToolsActionToolStart:\" + str(item.to_json()))\n    elif isinstance(item, AllToolsActionToolEnd):\n        print(\"AllToolsActionToolEnd:\" + str(item.to_json()))\n    elif isinstance(item, AllToolsLLMStatus):\n        if item.status == AgentStatus.llm_end:\n            print(\"llm_end:\" + item.text)\n```\n\n## Integrated Demo\nWe provide an integrated demo that you can run directly to see the results.\n- Install dependencies\n```shell\nfastapi = \"~0.109.2\"\nsse_starlette = \"~1.8.2\" \nuvicorn = \"\u003e=0.27.0.post1\"\n# webui\nstreamlit = \"1.34.0\"\nstreamlit-option-menu = \"0.3.12\"\nstreamlit-antd-components = \"0.3.1\"\nstreamlit-chatbox = \"1.1.12.post4\"\nstreamlit-modal = \"0.1.0\"\nstreamlit-aggrid = \"1.0.5\"\nstreamlit-extras = \"0.4.2\"\n```\n\n- server[server.py](tests/assistant/server/server.py)\n```shell\npython tests/assistant/server/server.py\n```\n\n- client[start_chat.py](tests/assistant/start_chat.py)\n```shell\npython tests/assistant/start_chat.py\n```\n\n\u003e show\n\n\nhttps://github.com/MetaGLM/langchain-zhipuai/assets/16206043/06863f9c-cd03-4a74-b76a-daa315718104\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMetaGLM%2Flangchain-zhipuai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMetaGLM%2Flangchain-zhipuai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMetaGLM%2Flangchain-zhipuai/lists"}