langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.61k stars 15.31k forks source link

getting error : type object ‘PromptTemplate’ has no attribute ‘from_messages #16735

Closed akashkumar398 closed 6 months ago

akashkumar398 commented 9 months ago

Checked other resources

Example Code

class SQLDbTool(BaseTool):

"""Tool SQLDB Agent"""

name = "@DSR"
description = "useful when the questions includes the term: @DSR.\n"

llm: AzureChatOpenAI

def _run(self, query: str) -> str:
    try:

    # Key Vault details 
        key_vault_name = 'XXXXXXXXXXX'
        vault_url = f"https://xxxxxxxxxxxx.vault.azure.net/"

    # Authenticate using DefaultAzureCredential
        credential = DefaultAzureCredential()

    #Create a SecretClient using your credentials
        client = SecretClient(vault_url, credential)

    # Access Key Vault secrets
        secret_name = 'source-XXXX-sql-XX'
        SQL_SERVER_PASSWORD = client.get_secret(secret_name).value

    # Update db_config with dynamic username and password
        db_config = {
        'drivername': 'mssql+pyodbc',
        'username': os.environ["SQL_SERVER_USERNAME"],
        'password': SQL_SERVER_PASSWORD,
        'host': os.environ["SQL_SERVER_ENDPOINT"],
        'port': 14XX,
        'database': os.environ["SQL_SERVER_DATABASE"],
        'query': {'driver': 'ODBC Driver 17 for SQL Server'}
        }

        db_url = URL.create(**db_config)
        db = SQLDatabase.from_uri(db_url)
        toolkit = SQLDatabaseToolkit(db=db, llm=self.llm)

        agent_executor = create_sql_agent(
        prefix=MSSQL_AGENT_PREFIX,
        format_instructions = MSSQL_AGENT_FORMAT_INSTRUCTIONS,
        llm=self.llm,
        toolkit=toolkit,
        callback_manager=self.callbacks,
        verbose=True
    )

    # Define your examples
        examples = [
            {
                "input": "what are the top 5 brands?",
                "query": "SELECT TOP 5 Brand",
            }
        ]

        # Define the prompt template for each example
        example_prompt = PromptTemplate.from_messages(
            [('human', '{input}'), ('ai', '{query}')]
        )

        # Create the FewShotPromptTemplate
        few_shot_prompt = FewShotPromptTemplate(
            examples=examples,
            example_prompt=example_prompt,
            prefix="You are a helpful AI Assistant",
            suffix="{input}",
            example_separator="\n\n",
            template_format="f-string",
            validate_template=True,
            input_variables=["input"]
        )

        # Add the FewShotPromptTemplate to your agent_executor
        agent_executor.add_prompt(few_shot_prompt)

        logging.info(f"Login successful: {db_config['username']}")
        response = agent_executor.run(query) 
        logger.info(f"Langchain log is:{response}")
        log_stream.seek(0)
        blob_client = blob_service_client.get_container_client(CONTAINER_NAME).get_blob_client(BLOB_NAME)
        blob_client.upload_blob(log_stream.read(), overwrite=True)
    except Exception as e:
        response = str(e)

    return response

Error Message and Stack Trace (if applicable)

No response

Description

getting error : type object ‘PromptTemplate’ has no attribute ‘from_messages'

System Info

langchain==0.0.352 langchain-core==0.1.11

dosubot[bot] commented 9 months ago

🤖

Hey there, @akashkumar398! Good to see you again! Looks like we're on another coding adventure, aren't we? 🚀

Based on the context provided, it seems like the from_messages method you're trying to use doesn't exist in the PromptTemplate class of the LangChain framework. This is why you're encountering the error "type object ‘PromptTemplate’ has no attribute ‘from_messages'".

The PromptTemplate class might have been updated or changed in the version of LangChain you're using, or the from_messages method might not have been a part of the PromptTemplate class to begin with.

To resolve this issue, you could try a few things:

  1. Check the documentation or source code of the PromptTemplate class in the version of LangChain you're using to see if the from_messages method is defined. If it's not, you'll need to use a different method or approach to achieve what you're trying to do.

  2. If the from_messages method is defined in a different class or module, make sure to import that class or module and use the method from there.

  3. If from_messages is a custom method you've defined elsewhere in your code, ensure that you've correctly imported it and are calling it on the right object.

Without the full code of the PromptTemplate class or the langchain_core.prompts module, it's difficult to provide a more specific solution. If you could provide more information about where the from_messages method is supposed to be defined, I might be able to help you further.

I hope this helps! If you have any other questions or need further clarification, feel free to ask.

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.