Skip to content

Instantly share code, notes, and snippets.

@anderl80
Last active June 21, 2023 06:17
Show Gist options
  • Save anderl80/5a593cbfc6d44283864917743557d8da to your computer and use it in GitHub Desktop.
Save anderl80/5a593cbfc6d44283864917743557d8da to your computer and use it in GitHub Desktop.
LangChain + OpenAI + Azure SQL
OPENAI_API_KEY=
OPENAI_API_BASE=
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_SERVER=
DATABASE_DB=
// PUT THIS IN .devcontainer FOLDER!
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:0-3.11",
"features": {
"ghcr.io/devcontainers/features/python:1": {
"installJupyterlab": true
},
"ghcr.io/devcontainers/features/azure-cli:1": {}
},
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install --user -r requirements.txt",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-toolsai.jupyter"
]
}
}
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
import os
from langchain.llms import AzureOpenAI
import openai
from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from dotenv import load_dotenv
load_dotenv()
# Configure OpenAI API
openai.api_type = "azure"
openai.api_version = "2022-12-01"
openai.api_base = os.getenv('OPENAI_API_BASE')
openai.api_key = os.getenv("OPENAI_API_KEY")
llm = AzureOpenAI(deployment_name="text-davinci-003", model_name="text-davinci-003")
database_user = os.getenv("DATABASE_USERNAME")
database_password = os.getenv("DATABASE_PASSWORD")
database_server = os.getenv("DATABASE_SERVER")
database_db = os.getenv("DATABASE_DB")
connection_string = f"mssql+pymssql://{database_user}:{database_password}@{database_server}.database.windows.net:1433/{database_db}"
db = SQLDatabase.from_uri(connection_string)
toolkit = SQLDatabaseToolkit(db=db, llm=llm, reduce_k_below_max_tokens=True)
agent_executor = create_sql_agent(
llm=llm,
toolkit=toolkit,
verbose=True
)
db = SQLDatabase.from_uri("mssql+pymssql://<username>:<password>@<server>.database.windows.net:1433/<database>")
toolkit = SQLDatabaseToolkit(db=db)
agent_executor = create_sql_agent(
llm=llm,
toolkit=toolkit,
verbose=True
)
agent_executor.run("Find the 5 most expensive records!")
openai
langchain
pymssql
python-dotenv
@anderl80
Copy link
Author

anderl80 commented Apr 1, 2023

Create an Azure SQL database and upload some data! Suggest this one: https://github.com/lerocha/chinook-database

@jshang11
Copy link

seems like by default, the LLM generate SQL with mysql syntax - for example SELECT * FROM cache_instances LIMIT 10. but that does't work in MS SQL database. MS SQL should be something like select top 10 * from cache_instances. Any idea how to specific database type?

@UriDimant
Copy link

Hello Andreas, I am getting error
openai\api_requestor.py", line 683, in _interpret_response_line
raise self.handle_error_response(
openai.error.InvalidRequestError: Resource not found

@rajaniesh
Copy link

Yes I am also getting the same error.

@anderl80
Copy link
Author

@jshang11, doesn't it self-correct after the error? https://python.langchain.com/en/latest/modules/chains/examples/sqlite.html#sql-chain-example gives more information, probably setting the query checker does the trick.

@anderl80
Copy link
Author

@UriDimant @rajaniesh did you check that there's a deployment with "text-davinci-003" in you Azure OpenAI instance?

@danielvi1787
Copy link

Hi, I was struggling with this too, but I could resolve it, on Azure AI Studio you can create a Deployment with a name different to the model name, if you do this, the code line llm = AzureOpenAI(deployment_name="deployment name", model_name="model name") fails with the Resource not found error, if you create the Deployment with a name exactly the same as the model name, it works. A bug? An expected behavior? Don't know but, in my case, that's the way I "fixed"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment