mistralai / client-python

Python client library for Mistral AI platform
Apache License 2.0
468 stars 97 forks source link

GCP client improvements #139

Closed hrjn closed 1 month ago

hrjn commented 1 month ago

These changes originate from some feedback highlighting the challenges of using the Python SDK in a Google Colab environment.

Issues

  1. The default environment of a Colab notebook comes with the google.colab package, which has a pinned version of the google.auth package in its dependencies (source). Trying to run !pip install mistralai[gcp]>=1.0.0 will result in the following error:

    Screenshot 2024-08-27 at 14 17 32
  2. Colab notebooks do not allow users to fully interact with the underlying shell session via a terminal. The Python SDK relies on the gcloud CLI for authentication, but it is impossible to run the authentication process end-to-end using only ! commands, even with gcloud auth application-default login --no-launch-browser because there is no way for the user to add inputs to a running cell:

Screenshot 2024-08-27 at 14 29 22

Fixes

  1. Pin google-auth to 2.27.0
  2. Allow the user to pass a custom access_token to MistralGoogleCloud() which is generated separately by the user.

Sample code to test in a Colab notebook:

!pip install git+https://github.com/mistralai/client-python.git@harizo/chore-improve-gcp-auth#egg=mistralai[gcp]
import os
import sys
from mistralai_gcp import MistralGoogleCloud
PROJECT_ID = "your-project-id"

if "google.colab" in sys.modules:
    from IPython.utils.io import capture_output
    from google.colab import auth

    auth.authenticate_user()
    # Capture the output of the shell command
    with capture_output() as captured:
      !gcloud auth application-default print-access-token
    # Store the captured output in a Python variable
    token = captured.stdout.rstrip()
    client = MistralGoogleCloud(access_token=token, project_id=PROJECT_ID)

model_name = "mistral-large"
model_version = "2407"

resp = client.chat.complete(
    model = f"{model_name}-{model_version}",
    messages=[
        {
            "role": "user",
            "content": "Who is the best French painter? Answer in one short sentence.",
        }
    ],
)

print(resp.choices[0].message.content)
socket-security[bot] commented 1 month ago

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
pypi/google-auth@2.27.0 environment, eval, filesystem, network, shell, unsafe +121 596 MB gcloudpypi, google_opensource

🚮 Removed packages: pypi/google-auth@2.32.0)

View full report↗︎

socket-security[bot] commented 1 month ago

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteSourceCI
AI warning pypi/pyu2f@0.1.5
  • Notes: The code contains multiple potential security risks, including unauthorized file writes, command injection, information leakage, and code injection. It should be reviewed and modified to ensure proper input validation, sanitization, and secure handling of user input. The presence of 'eval' raises concerns about the safety and security of the code.
  • Confidence: 0.80
  • Severity: 0.70
⚠︎
AI warning pypi/aiohttp@3.10.5
  • Notes: The code contains multiple potential security risks, including unauthorized file writes, command injection, information leakage, and code injection. It should be reviewed and modified to ensure proper input validation, sanitization, and secure handling of user input. The presence of 'eval' raises concerns about the safety and security of the code.
  • Confidence: 0.80
  • Severity: 0.70
⚠︎

View full report↗︎

Next steps

What is an AI-detected potential code anomaly?

AI has identified unusual behaviors that may pose a security risk.

An AI system found a low-risk anomaly in this package. It may still be fine to use, but you should check that it is safe before proceeding.

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/foo@1.0.0 or ignore all packages with @SocketSecurity ignore-all

  • @SocketSecurity ignore pypi/pyu2f@0.1.5
  • @SocketSecurity ignore pypi/aiohttp@3.10.5