-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,61 @@ | ||
from typing import TYPE_CHECKING, Any, Dict | ||
from typing import TYPE_CHECKING, Any, Dict, Generic, TypeVar, Union | ||
|
||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
|
||
if TYPE_CHECKING: | ||
from langchain.schema import AgentAction as LangchainAgentAction | ||
from langchain.schema import AgentFinish as LangchainAgentFinish | ||
from langchain.schema import HumanMessage as LangchainHumanMessage | ||
from langchain_core.schema import AgentAction as LangchainAgentAction | ||
from langchain_core.schema import AgentFinish as LangchainAgentFinish | ||
from langchain_core.schema import HumanMessage as LangchainHumanMessage | ||
|
||
|
||
# Create base classes that match langchain's structure | ||
class BaseAgentAction(BaseModel): | ||
tool: str | ||
tool_input: Dict[str, Any] | str | ||
log: str | ||
tool_input: Union[str, Dict[str, Any]] = Field(default_factory=dict) | ||
log: str = "" | ||
|
||
|
||
class BaseAgentFinish(BaseModel): | ||
return_values: Dict[str, Any] | ||
log: str | ||
return_values: Dict[str, Any] = Field(default_factory=dict) | ||
log: str = "" | ||
|
||
|
||
class BaseHumanMessage(BaseModel): | ||
content: str | ||
content: str = "" | ||
|
||
|
||
# Initialize with our base implementations first | ||
AgentAction = BaseAgentAction | ||
AgentFinish = BaseAgentFinish | ||
HumanMessage = BaseHumanMessage | ||
T = TypeVar("T") | ||
|
||
|
||
def use_langchain_types(): | ||
class Proxy(Generic[T]): | ||
def __init__(self, default_impl: T): | ||
self._impl: T = default_impl | ||
|
||
def __call__(self, *args: Any, **kwargs: Any) -> Any: | ||
return self._impl(*args, **kwargs) | ||
|
||
def set_implementation(self, impl: T) -> None: | ||
self._impl = impl | ||
|
||
|
||
# Initialize with our base implementations | ||
AgentAction = Proxy[Union[BaseAgentAction, "LangchainAgentAction"]](BaseAgentAction) | ||
AgentFinish = Proxy[Union[BaseAgentFinish, "LangchainAgentFinish"]](BaseAgentFinish) | ||
HumanMessage = Proxy[Union[BaseHumanMessage, "LangchainHumanMessage"]](BaseHumanMessage) | ||
|
||
|
||
def use_langchain_types() -> None: | ||
"""Switch to using langchain types after langchain is imported""" | ||
global AgentAction, AgentFinish, HumanMessage | ||
from langchain.schema import AgentAction as LangchainAgentAction | ||
from langchain.schema import AgentFinish as LangchainAgentFinish | ||
from langchain.schema import HumanMessage as LangchainHumanMessage | ||
|
||
AgentAction = LangchainAgentAction | ||
AgentFinish = LangchainAgentFinish | ||
HumanMessage = LangchainHumanMessage | ||
from langchain_core.schema import AgentAction as LangchainAgentAction | ||
from langchain_core.schema import AgentFinish as LangchainAgentFinish | ||
from langchain_core.schema import HumanMessage as LangchainHumanMessage | ||
|
||
# Call model_rebuild on these imported classes to resolve forward references | ||
LangchainAgentAction.model_rebuild() | ||
LangchainAgentFinish.model_rebuild() | ||
LangchainHumanMessage.model_rebuild() | ||
|
||
AgentAction.set_implementation(LangchainAgentAction) | ||
AgentFinish.set_implementation(LangchainAgentFinish) | ||
HumanMessage.set_implementation(LangchainHumanMessage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters