langchain-ai / langchain

๐Ÿฆœ๐Ÿ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.57k stars 15.3k forks source link

Retriever Tool for Vertex AI Model #12185

Closed vikasrai92l closed 9 months ago

vikasrai92l commented 1 year ago

Issue you'd like to raise.

I am trying to add retriever tool to SQL Database Toolkit and run SQL Agent with Vertex AI Text Bison LLM, however I am facing error. I am following below documentation for implementation https://python.langchain.com/docs/use_cases/qa_structured/sql?ref=blog.langchain.dev#case-3-sql-agents

My Python Code `llm = VertexAI( model_name='text-bison@001', max_output_tokens=256, temperature=0, top_p=0.8, top_k=40, verbose=True, )

few_shots = {'a': sql a query;', "b '.": "sql b query", "c.": "sql c query;", "d": "sql d query; " }

from langchain.embeddings import VertexAIEmbeddings from langchain.vectorstores import FAISS from langchain.schema import Document

embeddings = VertexAIEmbeddings()

few_shot_docs = [Document(page_content=question, metadata={'sql_query': few_shots[question]}) for question in few_shots.keys()] vector_db = FAISS.from_documents(few_shot_docs, embeddings)

retriever = vector_db.as_retriever(search_type="similarity_score_threshold", search_kwargs={"score_threshold": .5})

retriever = vector_db.as_retriever()

from langchain.agents import initialize_agent, Tool from langchain.agents import load_tools from langchain.agents import AgentType from langchain.tools import BaseTool

from langchain.agents.agent_toolkits import create_retriever_tool

tool_description = """ This tool will help you understand similar examples to adapt them to the user question. Input to this tool should be the user question. """

retriever_tool = create_retriever_tool( retriever, name='sql_get_similar_examples', description=tool_description ) custom_tool_list = [retriever_tool]

from langchain.agents import create_sql_agent from langchain.agents.agent_toolkits import SQLDatabaseToolkit from langchain.sql_database import SQLDatabase from langchain.agents import AgentExecutor from langchain.agents.agent_types import AgentType

from sqlalchemy import from sqlalchemy.engine import create_engine from sqlalchemy.schema import import pandas as pd

table_uri = f"bigquery://{project}/{dataset_id}" engine = create_engine(f"bigquery://{project}/{dataset_id}") db = SQLDatabase(engine=engine,metadata=MetaData(bind=engine),include_tables=table_name,view_support=True)

toolkit = SQLDatabaseToolkit(db=db, llm=llm)

custom_suffix = """Begin! Question: {input} Thought: I should first get the similar examples I know. If the examples are enough to construct the query, I can build it. Otherwise, I can then 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}"""

agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, extra_tools=custom_tool_list, suffix=custom_suffix )

langchain.debug = True

Thought:The table does not have the column 'alloted_terminal'. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:I don't have enough information to construct the query. Action: sql_get_similar_examples Action Input: {Question} Observation: {Document(fewshots} Thought:

Finished chain. Agent stopped due to iteration limit or time limit.

{ "output": "Agent stopped due to iteration limit or time limit." } ' Agent stopped due to iteration limit or time limit.

Suggestion:

No response

dosubot[bot] commented 1 year ago

๐Ÿค–

Hello,

From the context you've provided, it seems like you're trying to use the LangChain framework to integrate with Vertex AI Text Bison LLM and interact with an SQL database. Based on the information you've shared, I can confirm that LangChain does support integration with Vertex AI, including the Text Bison LLM, and it also has built-in support for SQL database interactions.

However, you mentioned that you're encountering an error but the error logs do not provide any error messages. This could be due to a few reasons:

  1. Recursion Limit: In LangChain, the execution of an agent is handled through the _invoke and _ainvoke methods in the Runnable class. These methods call the function or async function with variable arguments and handle the output. If the output is a Runnable, it is invoked recursively with a patched configuration. The recursion limit is checked before each recursive invocation, and a RecursionError is raised if the limit is reached. If the recursion limit is reached, the RecursionError would be raised, but if it is not caught by the try-except block in the abatch method, no error messages would be provided. You can check the recursion limit in your code and try increasing it to see if it resolves the issue.

  2. Database Connection: The SQLDatabase class in LangChain is designed to interact with an SQL database, execute queries, and retrieve results. If there's an issue with the database connection, the _execute method might fail to execute the SQL commands, leading to an error. Please ensure that your database connection is set up correctly.

  3. SQL Query: You mentioned that you're trying to find distinct status types in the 'mundra_eitv_latest_vw' table. The query "SELECT DISTINCT cable_charger_status FROM mundra_eitv_latest_vw" returns the distinct values '0', '1', and '9'. Please ensure that this query is correctly formatted and the table and column names are correct.

If none of the above suggestions work, could you please provide more information about the error? Specifically, it would be helpful if you could share the exact code you're using, the full error message (if any), and any other relevant details about your setup. This will help me better understand the issue and provide a more accurate solution.

Best, Dosu

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] commented 9 months ago

Hi, @vikasrai92l! I'm helping the LangChain team manage their backlog and am marking this issue as stale.

It looks like you're facing an error while attempting to integrate a retriever tool with a SQL Database Toolkit and run SQL Agent with Vertex AI Text Bison LLM. Dosubot has provided some potential reasons for the error, including recursion limit, database connection issues, and SQL query formatting. However, it seems that the issue is still unresolved.

Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you!