From 5cb7013d4c4564346881d0f9baaf03efb0a68e28 Mon Sep 17 00:00:00 2001 From: Johan Jansson Date: Fri, 13 Mar 2026 07:50:31 -1000 Subject: [PATCH 1/2] Update dependencies, security review --- requirements.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/requirements.txt b/requirements.txt index 9d393de..06ffbf6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,7 @@ ipykernel~=7.2.0 promptflow-devkit~=1.18.3 starlette~=0.49.3 urllib3~=2.6.3 + +# Transitive dependency pins for security updates +tornado>=6.5.5 # via ipykernel (high severity) +orjson>=3.11.6 # via promptflow-devkit -> streamlit (low severity) From 038a0acc62c3554baf2c993247a6338dedf5c8c7 Mon Sep 17 00:00:00 2001 From: Johan Jansson Date: Fri, 13 Mar 2026 09:16:23 -1000 Subject: [PATCH 2/2] update mistralai SDK and notebooks --- cookbooks/python/mistralai/evaluation.ipynb | 32 ++++++++-------- .../python/mistralai/function_calling.ipynb | 14 +++---- .../python/mistralai/prefix_use_cases.ipynb | 28 +++++++------- .../mistralai/prompting_capabilities.ipynb | 16 ++++---- requirements.txt | 6 +-- samples/python/mistralai/basic.py | 11 +++--- .../python/mistralai/getting_started.ipynb | 20 +++++----- samples/python/mistralai/multi_turn.py | 15 ++++---- samples/python/mistralai/streaming.py | 23 ++++++----- samples/python/mistralai/tools.py | 38 ++++++++++--------- 10 files changed, 99 insertions(+), 104 deletions(-) diff --git a/cookbooks/python/mistralai/evaluation.ipynb b/cookbooks/python/mistralai/evaluation.ipynb index 0ba3c13..cc8dff3 100644 --- a/cookbooks/python/mistralai/evaluation.ipynb +++ b/cookbooks/python/mistralai/evaluation.ipynb @@ -96,14 +96,14 @@ "metadata": {}, "outputs": [], "source": [ - "from mistralai.client import MistralClient\n", - "from mistralai.models.chat_completion import ChatMessage\n", + "from mistralai import Mistral\n", + "", "\n", "\n", "def run_mistral(user_message, model=\"mistral-ai/mistral-small-2503\"):\n", - " client = MistralClient(api_key=github_token, endpoint=endpoint)\n", - " messages = [ChatMessage(role=\"user\", content=user_message)]\n", - " chat_response = client.chat(\n", + " client = Mistral(api_key=github_token, server_url=endpoint)\n", + " messages = [{\"role\": \"user\", \"content\": user_message}]\n", + " chat_response = client.chat.complete(\n", " model=model,\n", " messages=messages,\n", " response_format={\"type\": \"json_object\"},\n", @@ -221,14 +221,14 @@ "outputs": [], "source": [ "import os\n", - "from mistralai.client import MistralClient\n", - "from mistralai.models.chat_completion import ChatMessage\n", + "from mistralai import Mistral\n", + "", "\n", "\n", "def run_mistral(user_message, model=\"mistral-ai/mistral-small-2503\"):\n", - " client = MistralClient(api_key=github_token, endpoint=endpoint)\n", - " messages = [ChatMessage(role=\"user\", content=user_message)]\n", - " chat_response = client.chat(model=model, messages=messages)\n", + " client = Mistral(api_key=github_token, server_url=endpoint)\n", + " messages = [{\"role\": \"user\", \"content\": user_message}]\n", + " chat_response = client.chat.complete(model=model, messages=messages)\n", " return chat_response.choices[0].message.content\n", "\n", "\n", @@ -375,20 +375,20 @@ "outputs": [], "source": [ "import os\n", - "from mistralai.client import MistralClient\n", - "from mistralai.models.chat_completion import ChatMessage\n", + "from mistralai import Mistral\n", + "", "\n", "\n", "def run_mistral(user_message, model=\"mistral-ai/mistral-small-2503\", is_json=False):\n", - " client = MistralClient(api_key=github_token, endpoint=endpoint)\n", - " messages = [ChatMessage(role=\"user\", content=user_message)]\n", + " client = Mistral(api_key=github_token, server_url=endpoint)\n", + " messages = [{\"role\": \"user\", \"content\": user_message}]\n", "\n", " if is_json:\n", - " chat_response = client.chat(\n", + " chat_response = client.chat.complete(\n", " model=model, messages=messages, response_format={\"type\": \"json_object\"}\n", " )\n", " else:\n", - " chat_response = client.chat(model=model, messages=messages)\n", + " chat_response = client.chat.complete(model=model, messages=messages)\n", "\n", " return chat_response.choices[0].message.content" ] diff --git a/cookbooks/python/mistralai/function_calling.ipynb b/cookbooks/python/mistralai/function_calling.ipynb index 19e01f4..25412c8 100644 --- a/cookbooks/python/mistralai/function_calling.ipynb +++ b/cookbooks/python/mistralai/function_calling.ipynb @@ -190,10 +190,10 @@ "metadata": {}, "outputs": [], "source": [ - "from mistralai.models.chat_completion import ChatMessage\n", + "", "\n", "messages = [\n", - " ChatMessage(role=\"user\", content=\"What's the status of my transaction T1001?\")\n", + " {\"role\": \"user\", \"content\": \"What's the status of my transaction T1001?\"}\n", "]\n" ] }, @@ -214,13 +214,13 @@ "metadata": {}, "outputs": [], "source": [ - "from mistralai.client import MistralClient\n", + "from mistralai import Mistral\n", "\n", "model = \"mistral-ai/mistral-small-2503\"\n", "\n", - "client = MistralClient(api_key=github_token, endpoint=endpoint)\n", + "client = Mistral(api_key=github_token, server_url=endpoint)\n", "\n", - "response = client.chat(\n", + "response = client.chat.complete(\n", " model=model,\n", " messages=messages,\n", " tools=tools,\n", @@ -294,7 +294,7 @@ "metadata": {}, "outputs": [], "source": [ - "messages.append(ChatMessage(role=\"tool\", name=function_name, content=function_result, tool_call_id=tool_call.id))" + "messages.append({\"role\": \"tool\", \"name\": function_name, \"content\": function_result, \"tool_call_id\": tool_call.id})" ] }, { @@ -324,7 +324,7 @@ "metadata": {}, "outputs": [], "source": [ - "response = client.chat(\n", + "response = client.chat.complete(\n", " model=model,\n", " messages=messages\n", ")\n", diff --git a/cookbooks/python/mistralai/prefix_use_cases.ipynb b/cookbooks/python/mistralai/prefix_use_cases.ipynb index f4e5a75..02c6b5a 100644 --- a/cookbooks/python/mistralai/prefix_use_cases.ipynb +++ b/cookbooks/python/mistralai/prefix_use_cases.ipynb @@ -78,7 +78,7 @@ "metadata": {}, "outputs": [], "source": [ - "from mistralai.client import MistralClient\n", + "from mistralai import Mistral\n", "import json\n", "import os, dotenv, mistralai\n", "\n", @@ -99,7 +99,7 @@ "outputs": [], "source": [ "\n", - "cli = MistralClient(api_key = github_token, endpoint=endpoint)" + "cli = Mistral(api_key=github_token, server_url=endpoint)" ] }, { @@ -148,7 +148,7 @@ "Hi there!\n", "\"\"\"\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"system\", \"content\":system}, {\"role\":\"user\", \"content\":question}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content)" @@ -186,7 +186,7 @@ "\"\"\"\n", "## Here is your answer in French:\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"system\", \"content\":system}, {\"role\":\"user\", \"content\":question}, {\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content)" @@ -236,7 +236,7 @@ "\"\"\"\n", "## Here is your answer in French:\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"system\", \"content\":system}, {\"role\":\"user\", \"content\":question}, {\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content[len(prefix):])" @@ -293,7 +293,7 @@ "\"\"\"\n", "## French Pirate Assistant: \n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"user\", \"content\":question}, {\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content[len(prefix):])" @@ -347,7 +347,7 @@ "Shakespeare:\n", "\"\"\"\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"user\", \"content\":question}, {\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content[len(prefix):])" @@ -371,7 +371,7 @@ "\n", "prefix = \"Assistant Shakespeare: \"\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"user\", \"content\":question}, {\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content[len(prefix):])" @@ -408,7 +408,7 @@ "Shakespeare: \n", "\"\"\"\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"system\", \"content\":instruction}, {\"role\":\"user\", \"content\":question}, {\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content[len(prefix):])" @@ -454,7 +454,7 @@ " print(f\"User: {question}\\n\")\n", " messages.append({\"role\":\"user\", \"content\":question})\n", "\n", - " resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + " resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = messages + [{\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", " ans = resp.choices[0].message.content\n", @@ -517,7 +517,7 @@ " prefix = character + \": \"\n", "\n", " messages.append({\"role\":\"user\", \"content\":question})\n", - " resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + " resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = messages + [{\"role\":\"assistant\", \"content\":prefix, \"prefix\":True}],\n", " max_tokens = 128)\n", " ans = resp.choices[0].message.content\n", @@ -571,7 +571,7 @@ "Insult me.\n", "\"\"\"\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"system\", \"content\":safe_prompt}, {\"role\":\"user\", \"content\":question}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content)" @@ -600,7 +600,7 @@ "\n", "Insult me.\n", "\"\"\"\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"system\", \"content\":safe_prompt}, {\"role\":\"user\", \"content\":question}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content)" @@ -636,7 +636,7 @@ "Answer: \n", "\"\"\"\n", "\n", - "resp = cli.chat(model = \"mistral-ai/mistral-small-2503\",\n", + "resp = cli.chat.complete(model = \"mistral-ai/mistral-small-2503\",\n", " messages = [{\"role\":\"system\", \"content\":safe_prompt}, {\"role\":\"user\", \"content\":question}, {\"role\":\"assistant\", \"content\":prefix, \"prefix\": True}],\n", " max_tokens = 128)\n", "print(resp.choices[0].message.content[len(prefix):])" diff --git a/cookbooks/python/mistralai/prompting_capabilities.ipynb b/cookbooks/python/mistralai/prompting_capabilities.ipynb index 35795f1..1e365e4 100644 --- a/cookbooks/python/mistralai/prompting_capabilities.ipynb +++ b/cookbooks/python/mistralai/prompting_capabilities.ipynb @@ -32,8 +32,8 @@ "metadata": {}, "outputs": [], "source": [ - "from mistralai.client import MistralClient\n", - "from mistralai.models.chat_completion import ChatMessage\n", + "from mistralai import Mistral\n", + "", "import os, dotenv\n", "\n", "dotenv.load_dotenv()\n", @@ -54,11 +54,11 @@ "outputs": [], "source": [ "def run_mistral(user_message, model=model_name):\n", - " client = MistralClient(api_key=github_token, endpoint=endpoint)\n", + " client = Mistral(api_key=github_token, server_url=endpoint)\n", " messages = [\n", - " ChatMessage(role=\"user\", content=user_message)\n", + " {\"role\": \"user\", \"content\": user_message}\n", " ]\n", - " chat_response = client.chat(\n", + " chat_response = client.chat.complete(\n", " model=model,\n", " messages=messages\n", " )\n", @@ -330,11 +330,11 @@ "outputs": [], "source": [ "def run_mistral(user_message, model=model_name):\n", - " client = MistralClient(api_key=github_token, endpoint=endpoint)\n", + " client = Mistral(api_key=github_token, server_url=endpoint)\n", " messages = [\n", - " ChatMessage(role=\"user\", content=user_message)\n", + " {\"role\": \"user\", \"content\": user_message}\n", " ]\n", - " chat_response = client.chat(\n", + " chat_response = client.chat.complete(\n", " model=model,\n", " messages=messages,\n", " temperature=1\n", diff --git a/requirements.txt b/requirements.txt index 06ffbf6..e3b1555 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,9 @@ azure-ai-inference~=1.0.0b9 azure-ai-evaluation~=1.15.3 openai~=2.26.0 -mistralai~=0.4.2 +mistralai~=1.9.0 python-dotenv~=1.2.2 ipykernel~=7.2.0 promptflow-devkit~=1.18.3 starlette~=0.49.3 urllib3~=2.6.3 - -# Transitive dependency pins for security updates -tornado>=6.5.5 # via ipykernel (high severity) -orjson>=3.11.6 # via promptflow-devkit -> streamlit (low severity) diff --git a/samples/python/mistralai/basic.py b/samples/python/mistralai/basic.py index 6e1e562..33b280d 100644 --- a/samples/python/mistralai/basic.py +++ b/samples/python/mistralai/basic.py @@ -2,8 +2,7 @@ It is leveraging your endpoint and key. The call is synchronous.""" import os -from mistralai.client import MistralClient -from mistralai.models.chat_completion import ChatMessage +from mistralai import Mistral token = os.environ["GITHUB_TOKEN"] endpoint = "https://models.github.ai/inference" @@ -11,13 +10,13 @@ # Pick one of the Mistral models from the GitHub Models service model_name = "mistral-small-2503" -client = MistralClient(api_key=token, endpoint=endpoint) +client = Mistral(api_key=token, server_url=endpoint) -response = client.chat( +response = client.chat.complete( model=model_name, messages=[ - ChatMessage(role="system", content="You are a helpful assistant."), - ChatMessage(role="user", content="What is the capital of France?"), + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What is the capital of France?"}, ], # Optional parameters temperature=1., diff --git a/samples/python/mistralai/getting_started.ipynb b/samples/python/mistralai/getting_started.ipynb index b0fd08a..59656c8 100644 --- a/samples/python/mistralai/getting_started.ipynb +++ b/samples/python/mistralai/getting_started.ipynb @@ -47,7 +47,7 @@ "source": [ "import os\n", "import dotenv\n", - "from mistralai.client import MistralClient\n", + "from mistralai import Mistral\n", "\n", "\n", "dotenv.load_dotenv()\n", @@ -63,7 +63,7 @@ "# Pick one of the Mistral models from the GitHub Models service\n", "model_name = \"Mistral-large\"\n", "\n", - "client = MistralClient(api_key=github_token, endpoint=endpoint)" + "client = Mistral(api_key=github_token, server_url=endpoint)" ] }, { @@ -84,7 +84,7 @@ "outputs": [], "source": [ "\n", - "response = client.chat(\n", + "response = client.chat.complete(\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", @@ -124,7 +124,7 @@ "outputs": [], "source": [ "# Call the chat completion API\n", - "response = client.chat(\n", + "response = client.chat.complete(\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", @@ -171,7 +171,7 @@ "outputs": [], "source": [ "# Call the chat completion API\n", - "response = client.chat_stream(\n", + "response = client.chat.stream(\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", @@ -186,9 +186,9 @@ ")\n", "\n", "# Print the streamed response\n", - "for update in response:\n", - " if update.choices[0].delta.content:\n", - " print(update.choices[0].delta.content, end=\"\")\n" + "for event in response:\n", + " if event.data.choices[0].delta.content:\n", + " print(event.data.choices[0].delta.content, end=\"\")\n" ] }, { @@ -261,7 +261,7 @@ " },\n", "]\n", "\n", - "response = client.chat(\n", + "response = client.chat.complete(\n", " messages=messages,\n", " tools=[tool],\n", " model=model_name,\n", @@ -304,7 +304,7 @@ " )\n", "\n", " # Get another response from the model\n", - " response = client.chat(\n", + " response = client.chat.complete(\n", " messages=messages,\n", " tools=[tool],\n", " model=model_name,\n", diff --git a/samples/python/mistralai/multi_turn.py b/samples/python/mistralai/multi_turn.py index e2b974f..3b655e2 100644 --- a/samples/python/mistralai/multi_turn.py +++ b/samples/python/mistralai/multi_turn.py @@ -4,8 +4,7 @@ """ import os -from mistralai.client import MistralClient -from mistralai.models.chat_completion import ChatMessage +from mistralai import Mistral token = os.environ["GITHUB_TOKEN"] endpoint = "https://models.github.ai/inference" @@ -14,16 +13,16 @@ model_name = "mistral-small-2503" # Create a client -client = MistralClient(api_key=token, endpoint=endpoint) +client = Mistral(api_key=token, server_url=endpoint) # Call the chat completion API -response = client.chat( +response = client.chat.complete( model=model_name, messages=[ - ChatMessage(role="system", content="You are a helpful assistant."), - ChatMessage(role="user", content="What is the capital of France?"), - ChatMessage(role="assistant", content="The capital of France is Paris."), - ChatMessage(role="user", content="What about Spain?"), + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What is the capital of France?"}, + {"role": "assistant", "content": "The capital of France is Paris."}, + {"role": "user", "content": "What about Spain?"}, ], ) diff --git a/samples/python/mistralai/streaming.py b/samples/python/mistralai/streaming.py index 1075e46..d61119b 100644 --- a/samples/python/mistralai/streaming.py +++ b/samples/python/mistralai/streaming.py @@ -2,8 +2,7 @@ so that the first token shows up early and you avoid waiting for long responses.""" import os -from mistralai.client import MistralClient -from mistralai.models.chat_completion import ChatMessage +from mistralai import Mistral token = os.environ["GITHUB_TOKEN"] endpoint = "https://models.github.ai/inference" @@ -12,23 +11,23 @@ model_name = "mistral-small-2503" # Create a client -client = MistralClient(api_key=token, endpoint=endpoint) +client = Mistral(api_key=token, server_url=endpoint) # Call the chat completion API -response = client.chat_stream( +response = client.chat.stream( model=model_name, messages=[ - ChatMessage(role="system", content="You are a helpful assistant."), - ChatMessage( - role="user", - content="Give me 5 good reasons why I should exercise every day.", - ), + {"role": "system", "content": "You are a helpful assistant."}, + { + "role": "user", + "content": "Give me 5 good reasons why I should exercise every day.", + }, ], ) # Print the streamed response -for update in response: - if update.choices: - print(update.choices[0].delta.content or "", end="") +for event in response: + if event.data.choices: + print(event.data.choices[0].delta.content or "", end="") print() diff --git a/samples/python/mistralai/tools.py b/samples/python/mistralai/tools.py index 50f7dba..3e65b39 100644 --- a/samples/python/mistralai/tools.py +++ b/samples/python/mistralai/tools.py @@ -4,8 +4,8 @@ and how to act on a request from the model to invoke it.""" import os import json -from mistralai.client import MistralClient -from mistralai.models.chat_completion import ChatMessage, Function +from mistralai import Mistral +from mistralai.models import Function token = os.environ["GITHUB_TOKEN"] endpoint = "https://models.github.ai/inference" @@ -59,19 +59,21 @@ def get_flight_info(origin_city: str, destination_city: str): } -client = MistralClient(api_key=token, endpoint=endpoint) +client = Mistral(api_key=token, server_url=endpoint) messages = [ - ChatMessage( - role="system", - content="You an assistant that helps users find flight information."), - ChatMessage( - role="user", - content=("I'm interested in going to Miami. What is " - "the next flight there from Seattle?")), + { + "role": "system", + "content": "You an assistant that helps users find flight information.", + }, + { + "role": "user", + "content": ("I'm interested in going to Miami. What is " + "the next flight there from Seattle?"), + }, ] -response = client.chat( +response = client.chat.complete( messages=messages, tools=[tool], model=model_name, @@ -103,16 +105,16 @@ def get_flight_info(origin_city: str, destination_city: str): # Append the function call result fo the chat history messages.append( - ChatMessage( - role="tool", - name=tool_call.function.name, - content=function_return, - tool_call_id=tool_call.id, - ) + { + "role": "tool", + "name": tool_call.function.name, + "content": function_return, + "tool_call_id": tool_call.id, + } ) # Get another response from the model - response = client.chat( + response = client.chat.complete( messages=messages, tools=[tool], model=model_name,