From b951ea3c5d38595a08497fac91179a5b87538fd5 Mon Sep 17 00:00:00 2001 From: ks6088ts Date: Fri, 13 Dec 2024 17:58:32 +0900 Subject: [PATCH] use ai foundry sdk --- .env.template | 3 ++ apps/1_call_azure_openai_chat/ai_foundry.py | 28 +++++++++++++++ poetry.lock | 38 ++++++++++++++++++++- pyproject.toml | 2 ++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 apps/1_call_azure_openai_chat/ai_foundry.py diff --git a/.env.template b/.env.template index 9bcd20b..1ecfc9f 100644 --- a/.env.template +++ b/.env.template @@ -40,3 +40,6 @@ BING_SEARCH_URL="https://api.bing.microsoft.com/v7.0/search" # LangSmith LANGCHAIN_TRACING_V2="true" LANGCHAIN_API_KEY="" + +# Azure AI Foundry +AZURE_AI_FOUNDRY_PROJECT_CONNECTION_STRING="" diff --git a/apps/1_call_azure_openai_chat/ai_foundry.py b/apps/1_call_azure_openai_chat/ai_foundry.py new file mode 100644 index 0000000..7a1023e --- /dev/null +++ b/apps/1_call_azure_openai_chat/ai_foundry.py @@ -0,0 +1,28 @@ +from os import getenv + +from azure.ai.projects import AIProjectClient +from azure.identity import DefaultAzureCredential +from dotenv import load_dotenv + +if __name__ == "__main__": + # Parse .env file and set environment variables + load_dotenv(override=True) + + project = AIProjectClient.from_connection_string( + conn_str=getenv("AZURE_AI_FOUNDRY_PROJECT_CONNECTION_STRING"), + credential=DefaultAzureCredential(), + ) + + chat = project.inference.get_chat_completions_client() + response = chat.complete( + model=getenv("AZURE_OPENAI_GPT_MODEL"), + messages=[ + { + "role": "system", + "content": "You are an AI assistant", + }, + {"role": "user", "content": "Hello, how are you?"}, + ], + ) + + print(response.choices[0].message.content) diff --git a/poetry.lock b/poetry.lock index ff7101c..e22a3e2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -327,6 +327,42 @@ azure-core = ">=1.30.0" isodate = ">=0.6.1" typing-extensions = ">=4.6.0" +[[package]] +name = "azure-ai-inference" +version = "1.0.0b6" +description = "Microsoft Azure AI Inference Client Library for Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "azure_ai_inference-1.0.0b6-py3-none-any.whl", hash = "sha256:5699ad78d70ec2d227a5eff2c1bafc845018f6624edc5b03589dfff861c54958"}, + {file = "azure_ai_inference-1.0.0b6.tar.gz", hash = "sha256:b8ac941de1e69151bad464191e18856d4e74f962ae03235da137a9a326143676"}, +] + +[package.dependencies] +azure-core = ">=1.30.0" +isodate = ">=0.6.1" +typing-extensions = ">=4.6.0" + +[package.extras] +opentelemetry = ["azure-core-tracing-opentelemetry"] +prompts = ["pyyaml"] + +[[package]] +name = "azure-ai-projects" +version = "1.0.0b2" +description = "Microsoft Azure AI Projects Client Library for Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "azure_ai_projects-1.0.0b2-py3-none-any.whl", hash = "sha256:5bdbc437817ff26582fb804e82cb23afd1b100d71a8cbd22e696d81916536fac"}, + {file = "azure_ai_projects-1.0.0b2.tar.gz", hash = "sha256:6f3585efecf91cda4d6bdbedc019e975c2c5c2e0852691f1a4396d3183a46529"}, +] + +[package.dependencies] +azure-core = ">=1.30.0" +isodate = ">=0.6.1" +typing-extensions = ">=4.12.2" + [[package]] name = "azure-cognitiveservices-speech" version = "1.40.0" @@ -7493,4 +7529,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "32bb35d2d3c987f1c3717ba10217ceace23f727fe1e93ecf6a2c957d6e148dcf" +content-hash = "51979a8904f189a410fea8afdf680511cabf523216a22b0c6d7d9acd81bff816" diff --git a/pyproject.toml b/pyproject.toml index 1c1b461..a213d02 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,8 @@ azure-cognitiveservices-speech = "^1.40.0" openai-whisper = "^20240930" pymupdf = "^1.24.14" pydantic = "^2.10.2" +azure-ai-projects = "^1.0.0b2" +azure-ai-inference = "^1.0.0b6" [tool.poetry.group.dev.dependencies] pre-commit = "^4.0.0"