mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
109 lines
3.8 KiB
Python
109 lines
3.8 KiB
Python
"""SQL agent."""
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence
|
|
|
|
from langchain_core.callbacks import BaseCallbackManager
|
|
from langchain_core.language_models import BaseLanguageModel
|
|
from langchain_core.messages import AIMessage, SystemMessage
|
|
from langchain_core.prompts.chat import (
|
|
ChatPromptTemplate,
|
|
HumanMessagePromptTemplate,
|
|
MessagesPlaceholder,
|
|
)
|
|
|
|
from langchain_community.agent_toolkits.sql.prompt import (
|
|
SQL_FUNCTIONS_SUFFIX,
|
|
SQL_PREFIX,
|
|
SQL_SUFFIX,
|
|
)
|
|
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
|
|
from langchain_community.tools import BaseTool
|
|
|
|
if TYPE_CHECKING:
|
|
from langchain.agents.agent import AgentExecutor
|
|
from langchain.agents.agent_types import AgentType
|
|
|
|
|
|
def create_sql_agent(
|
|
llm: BaseLanguageModel,
|
|
toolkit: SQLDatabaseToolkit,
|
|
agent_type: Optional[AgentType] = None,
|
|
callback_manager: Optional[BaseCallbackManager] = None,
|
|
prefix: str = SQL_PREFIX,
|
|
suffix: Optional[str] = None,
|
|
format_instructions: Optional[str] = None,
|
|
input_variables: Optional[List[str]] = None,
|
|
top_k: int = 10,
|
|
max_iterations: Optional[int] = 15,
|
|
max_execution_time: Optional[float] = None,
|
|
early_stopping_method: str = "force",
|
|
verbose: bool = False,
|
|
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
|
|
extra_tools: Sequence[BaseTool] = (),
|
|
**kwargs: Any,
|
|
) -> AgentExecutor:
|
|
"""Construct an SQL agent from an LLM and tools."""
|
|
from langchain.agents.agent import AgentExecutor, BaseSingleActionAgent
|
|
from langchain.agents.agent_types import AgentType
|
|
from langchain.agents.mrkl.base import ZeroShotAgent
|
|
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
|
|
from langchain.chains.llm import LLMChain
|
|
|
|
agent_type = agent_type or AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
|
tools = toolkit.get_tools() + list(extra_tools)
|
|
prefix = prefix.format(dialect=toolkit.dialect, top_k=top_k)
|
|
agent: BaseSingleActionAgent
|
|
|
|
if agent_type == AgentType.ZERO_SHOT_REACT_DESCRIPTION:
|
|
prompt_params = (
|
|
{"format_instructions": format_instructions}
|
|
if format_instructions is not None
|
|
else {}
|
|
)
|
|
prompt = ZeroShotAgent.create_prompt(
|
|
tools,
|
|
prefix=prefix,
|
|
suffix=suffix or SQL_SUFFIX,
|
|
input_variables=input_variables,
|
|
**prompt_params,
|
|
)
|
|
llm_chain = LLMChain(
|
|
llm=llm,
|
|
prompt=prompt,
|
|
callback_manager=callback_manager,
|
|
)
|
|
tool_names = [tool.name for tool in tools]
|
|
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs)
|
|
|
|
elif agent_type == AgentType.OPENAI_FUNCTIONS:
|
|
messages = [
|
|
SystemMessage(content=prefix),
|
|
HumanMessagePromptTemplate.from_template("{input}"),
|
|
AIMessage(content=suffix or SQL_FUNCTIONS_SUFFIX),
|
|
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
|
]
|
|
input_variables = ["input", "agent_scratchpad"]
|
|
_prompt = ChatPromptTemplate(input_variables=input_variables, messages=messages)
|
|
|
|
agent = OpenAIFunctionsAgent(
|
|
llm=llm,
|
|
prompt=_prompt,
|
|
tools=tools,
|
|
callback_manager=callback_manager,
|
|
**kwargs,
|
|
)
|
|
else:
|
|
raise ValueError(f"Agent type {agent_type} not supported at the moment.")
|
|
|
|
return AgentExecutor.from_agent_and_tools(
|
|
agent=agent,
|
|
tools=tools,
|
|
callback_manager=callback_manager,
|
|
verbose=verbose,
|
|
max_iterations=max_iterations,
|
|
max_execution_time=max_execution_time,
|
|
early_stopping_method=early_stopping_method,
|
|
**(agent_executor_kwargs or {}),
|
|
)
|