forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge Existing changes from spc-langchain to skypoint-langchain (#3)
* Merge Existing changes from spc-langchain to skypoint-langchain * Update agent_toolkits imports and repository URL * Update tools and version in langchain library * Version dump
- Loading branch information
Showing
15 changed files
with
1,053 additions
and
7 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
1 change: 1 addition & 0 deletions
1
libs/langchain/langchain/agents/agent_toolkits/unitycatalog/__init__.py
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Unity Catalog SQL agent.""" |
93 changes: 93 additions & 0 deletions
93
libs/langchain/langchain/agents/agent_toolkits/unitycatalog/base.py
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"""SQL agent.""" | ||
from typing import Any, Dict, List, Optional | ||
|
||
from langchain.agents.agent import AgentExecutor, BaseSingleActionAgent | ||
from langchain.agents.agent_toolkits.sql.prompt import ( | ||
SQL_FUNCTIONS_SUFFIX, | ||
SQL_PREFIX, | ||
SQL_SUFFIX, | ||
) | ||
from langchain.agents.agent_toolkits.sql.toolkit import SQLDatabaseToolkit | ||
from langchain.agents.agent_types import AgentType | ||
from langchain.agents.mrkl.base import ZeroShotAgent | ||
from langchain.agents.mrkl.prompt import FORMAT_INSTRUCTIONS | ||
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent | ||
from langchain.callbacks.base import BaseCallbackManager | ||
from langchain.chains.llm import LLMChain | ||
from langchain.prompts.chat import ( | ||
ChatPromptTemplate, | ||
HumanMessagePromptTemplate, | ||
MessagesPlaceholder, | ||
) | ||
from langchain.schema.language_model import BaseLanguageModel | ||
from langchain.schema.messages import AIMessage, SystemMessage | ||
|
||
|
||
def create_sql_agent( | ||
llm: BaseLanguageModel, | ||
toolkit: SQLDatabaseToolkit, | ||
agent_type: AgentType = AgentType.ZERO_SHOT_REACT_DESCRIPTION, | ||
callback_manager: Optional[BaseCallbackManager] = None, | ||
prefix: str = SQL_PREFIX, | ||
suffix: Optional[str] = None, | ||
format_instructions: str = FORMAT_INSTRUCTIONS, | ||
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, | ||
**kwargs: Dict[str, Any], | ||
) -> AgentExecutor: | ||
"""Construct an SQL agent from an LLM and tools.""" | ||
tools = toolkit.get_tools() | ||
prefix = prefix.format(dialect=toolkit.dialect, top_k=top_k) | ||
agent: BaseSingleActionAgent | ||
|
||
if agent_type == AgentType.ZERO_SHOT_REACT_DESCRIPTION: | ||
prompt = ZeroShotAgent.create_prompt( | ||
tools, | ||
prefix=prefix, | ||
suffix=suffix or SQL_SUFFIX, | ||
format_instructions=format_instructions, | ||
input_variables=input_variables, | ||
) | ||
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 {}), | ||
) |
23 changes: 23 additions & 0 deletions
23
libs/langchain/langchain/agents/agent_toolkits/unitycatalog/prompt.py
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# flake8: noqa | ||
|
||
SQL_PREFIX = """You are an agent designed to interact with a SQL database. | ||
Given an input question, create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. | ||
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most {top_k} results. | ||
You can order the results by a relevant column to return the most interesting examples in the database. | ||
Never query for all the columns from a specific table, only ask for the relevant columns given the question. | ||
You have access to tools for interacting with the database. | ||
Only use the below tools. Only use the information returned by the below tools to construct your final answer. | ||
You MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again. | ||
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database. | ||
If the question does not seem related to the database, just return "I don't know" as the answer. | ||
""" | ||
|
||
SQL_SUFFIX = """Begin! | ||
Question: {input} | ||
Thought: I should look at the tables in the database to see what I can query. Then I should query the schema of the most relevant tables. | ||
{agent_scratchpad}""" | ||
|
||
SQL_FUNCTIONS_SUFFIX = """I should look at the tables in the database to see what I can query. Then I should query the schema of the most relevant tables.""" |
Oops, something went wrong.