-
-
Save anderl80/5a593cbfc6d44283864917743557d8da to your computer and use it in GitHub Desktop.
LangChain + OpenAI + Azure SQL
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
OPENAI_API_KEY= | |
OPENAI_API_BASE= | |
DATABASE_USERNAME= | |
DATABASE_PASSWORD= | |
DATABASE_SERVER= | |
DATABASE_DB= |
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
// 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" | |
} |
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
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!") |
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
openai | |
langchain | |
pymssql | |
python-dotenv |
@UriDimant @rajaniesh did you check that there's a deployment with "text-davinci-003" in you Azure OpenAI instance?
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
@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.