修改 Agent 配置方法
This commit is contained in:
parent
ba89d2fc84
commit
98bfa82b24
@ -22,9 +22,3 @@ class ChatbotConfiguration(Configuration):
|
|||||||
model_provider: str = "zhipu"
|
model_provider: str = "zhipu"
|
||||||
model_name: str = "glm-4-plus"
|
model_name: str = "glm-4-plus"
|
||||||
|
|
||||||
def __post_init__(self):
|
|
||||||
# TODO 需要确保这里的模型是支持 tools 的
|
|
||||||
if self.llm is None:
|
|
||||||
self.llm = select_model(config=config,
|
|
||||||
model_provider=self.model_provider,
|
|
||||||
model_name=self.model_name).chat_open_ai
|
|
||||||
|
|||||||
@ -15,19 +15,19 @@ class ChatbotAgent(BaseAgent):
|
|||||||
name = "chatbot"
|
name = "chatbot"
|
||||||
description = "A chatbot that can answer questions and help with tasks."
|
description = "A chatbot that can answer questions and help with tasks."
|
||||||
_graph_cache = None
|
_graph_cache = None
|
||||||
|
config_schema = ChatbotConfiguration
|
||||||
|
|
||||||
def __init__(self, configuration: ChatbotConfiguration = None):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(configuration)
|
super().__init__(**kwargs)
|
||||||
self.configuration = configuration or ChatbotConfiguration()
|
|
||||||
self.llm = self.configuration.llm
|
|
||||||
|
|
||||||
def _get_tools(self, config: RunnableConfig):
|
def _get_tools(self, config_schema: RunnableConfig):
|
||||||
"""根据配置获取工具"""
|
"""根据配置获取工具"""
|
||||||
|
conf = ChatbotConfiguration.from_runnable_config(config_schema)
|
||||||
tools = [multiply]
|
tools = [multiply]
|
||||||
if not config:
|
if not conf:
|
||||||
return tools
|
return tools
|
||||||
|
|
||||||
if config.get("configurable", {}).get("use_web", None):
|
if conf.get("use_web", None):
|
||||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||||
tools.append(TavilySearchResults(max_results=10))
|
tools.append(TavilySearchResults(max_results=10))
|
||||||
|
|
||||||
@ -39,11 +39,11 @@ class ChatbotAgent(BaseAgent):
|
|||||||
res = model.invoke(state["messages"])
|
res = model.invoke(state["messages"])
|
||||||
return {"messages": [res]}
|
return {"messages": [res]}
|
||||||
|
|
||||||
def get_graph(self, config: RunnableConfig = None):
|
def get_graph(self, config_schema: RunnableConfig = None):
|
||||||
"""构建图"""
|
"""构建图"""
|
||||||
workflow = StateGraph(State)
|
workflow = StateGraph(State)
|
||||||
workflow.add_node("chatbot", self.llm_call)
|
workflow.add_node("chatbot", self.llm_call)
|
||||||
workflow.add_node("tools", ToolNode(tools=self._get_tools(config)))
|
workflow.add_node("tools", ToolNode(tools=self._get_tools(config_schema)))
|
||||||
workflow.add_edge(START, "chatbot")
|
workflow.add_edge(START, "chatbot")
|
||||||
workflow.add_conditional_edges(
|
workflow.add_conditional_edges(
|
||||||
"chatbot",
|
"chatbot",
|
||||||
@ -55,9 +55,9 @@ class ChatbotAgent(BaseAgent):
|
|||||||
graph = workflow.compile(checkpointer=MemorySaver())
|
graph = workflow.compile(checkpointer=MemorySaver())
|
||||||
return graph
|
return graph
|
||||||
|
|
||||||
def stream_values(self, messages: list[str], config: RunnableConfig = None):
|
def stream_values(self, messages: list[str], config_schema: RunnableConfig = None):
|
||||||
graph = self.get_graph(config)
|
graph = self.get_graph(config_schema)
|
||||||
for event in graph.stream({"messages": messages}, stream_mode="values", config=config):
|
for event in graph.stream({"messages": messages}, stream_mode="values", config=config_schema):
|
||||||
yield event["messages"]
|
yield event["messages"]
|
||||||
|
|
||||||
def stream_messages(self, messages: list[str], config: RunnableConfig = None):
|
def stream_messages(self, messages: list[str], config: RunnableConfig = None):
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Type, Annotated, Optional, TypedDict
|
from typing import Type, Annotated, Optional, TypedDict
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
|
from dataclasses import dataclass, fields, field
|
||||||
|
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_core.runnables import RunnableConfig
|
||||||
|
|
||||||
from langchain_core.messages import BaseMessage
|
from langchain_core.messages import BaseMessage
|
||||||
from langgraph.graph.state import CompiledStateGraph
|
from langgraph.graph.state import CompiledStateGraph
|
||||||
from langgraph.graph.message import add_messages
|
from langgraph.graph.message import add_messages
|
||||||
|
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
class State(TypedDict):
|
class State(TypedDict):
|
||||||
"""
|
"""
|
||||||
定义一个基础 State 供 各类 graph 继承, 其中:
|
定义一个基础 State 供 各类 graph 继承, 其中:
|
||||||
@ -25,14 +26,28 @@ class Configuration:
|
|||||||
"""
|
"""
|
||||||
定义一个基础 Configuration 供 各类 graph 继承
|
定义一个基础 Configuration 供 各类 graph 继承
|
||||||
"""
|
"""
|
||||||
pass
|
|
||||||
|
user_id: str = field(metadata={"description": "Unique identifier for the user."})
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_runnable_config(
|
||||||
|
cls, config: Optional[RunnableConfig] = None
|
||||||
|
) -> Configuration:
|
||||||
|
"""Create a Configuration instance from a RunnableConfig object."""
|
||||||
|
configurable = (config.get("configurable") or {}) if config else {}
|
||||||
|
_fields = {f.name for f in fields(cls) if f.init}
|
||||||
|
return cls(**{k: v for k, v in configurable.items() if k in _fields})
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def to_dict(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BaseAgent():
|
class BaseAgent():
|
||||||
|
|
||||||
def __init__(self, configuration: Configuration):
|
def __init__(self, **kwargs):
|
||||||
self.configuration = configuration
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_graph(self) -> CompiledStateGraph:
|
def get_graph(self) -> CompiledStateGraph:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user