修改 Agent 配置方法
This commit is contained in:
parent
ba89d2fc84
commit
98bfa82b24
@ -22,9 +22,3 @@ class ChatbotConfiguration(Configuration):
|
||||
model_provider: str = "zhipu"
|
||||
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"
|
||||
description = "A chatbot that can answer questions and help with tasks."
|
||||
_graph_cache = None
|
||||
config_schema = ChatbotConfiguration
|
||||
|
||||
def __init__(self, configuration: ChatbotConfiguration = None):
|
||||
super().__init__(configuration)
|
||||
self.configuration = configuration or ChatbotConfiguration()
|
||||
self.llm = self.configuration.llm
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def _get_tools(self, config: RunnableConfig):
|
||||
def _get_tools(self, config_schema: RunnableConfig):
|
||||
"""根据配置获取工具"""
|
||||
conf = ChatbotConfiguration.from_runnable_config(config_schema)
|
||||
tools = [multiply]
|
||||
if not config:
|
||||
if not conf:
|
||||
return tools
|
||||
|
||||
if config.get("configurable", {}).get("use_web", None):
|
||||
if conf.get("use_web", None):
|
||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||
tools.append(TavilySearchResults(max_results=10))
|
||||
|
||||
@ -39,11 +39,11 @@ class ChatbotAgent(BaseAgent):
|
||||
res = model.invoke(state["messages"])
|
||||
return {"messages": [res]}
|
||||
|
||||
def get_graph(self, config: RunnableConfig = None):
|
||||
def get_graph(self, config_schema: RunnableConfig = None):
|
||||
"""构建图"""
|
||||
workflow = StateGraph(State)
|
||||
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_conditional_edges(
|
||||
"chatbot",
|
||||
@ -55,9 +55,9 @@ class ChatbotAgent(BaseAgent):
|
||||
graph = workflow.compile(checkpointer=MemorySaver())
|
||||
return graph
|
||||
|
||||
def stream_values(self, messages: list[str], config: RunnableConfig = None):
|
||||
graph = self.get_graph(config)
|
||||
for event in graph.stream({"messages": messages}, stream_mode="values", config=config):
|
||||
def stream_values(self, messages: list[str], config_schema: RunnableConfig = None):
|
||||
graph = self.get_graph(config_schema)
|
||||
for event in graph.stream({"messages": messages}, stream_mode="values", config=config_schema):
|
||||
yield event["messages"]
|
||||
|
||||
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 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 langgraph.graph.state import CompiledStateGraph
|
||||
from langgraph.graph.message import add_messages
|
||||
|
||||
|
||||
from dataclasses import dataclass
|
||||
class State(TypedDict):
|
||||
"""
|
||||
定义一个基础 State 供 各类 graph 继承, 其中:
|
||||
@ -25,14 +26,28 @@ class Configuration:
|
||||
"""
|
||||
定义一个基础 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():
|
||||
|
||||
def __init__(self, configuration: Configuration):
|
||||
self.configuration = configuration
|
||||
def __init__(self, **kwargs):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_graph(self) -> CompiledStateGraph:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user