Skip to content

python """ اسکریپت: اتصال به Vertex AI و اجرای مدل DeepSeek (MaaS) نویسنده: [نام شما/محسن باقری] توضیحات: احراز هویت از طریق Service Account و فراخوانی مدل‌های MaaS در پلتفرم Vertex AI گوگل """ #2975

Description

@themanofathousandfaces

I am confused, when i call the method to generate content with a service account authorization in place it works, but with the model list it doesn't

Here is how I get my credentials:

def get_credentials(GOOGLE_API_KEY) -> gapi_key.Credentials:
    # credentials = gapi_key.Credentials(GOOGLE_API_KEY)
    scopes = [
        "https://www.googleapis.com/auth/generative-language",
        "https://www.googleapis.com/auth/cloud-platform",
    ]
    service_account = json.loads(service_account_file)
    credentials = gservice_account.Credentials.from_service_account_info(service_account, scopes=scopes)

credentials = get_credentials("")
credentials.refresh(google.auth.transport.requests.Request())
print("token is ", credentials.token)
print(f"https://{location}-aiplatform.googleapis.com")


vertexai.init(credentials=credentials, project=project_id, api_endpoint=f"https://{location}-aiplatform.googleapis.com", location=location)

client = genai.Client(vertexai=True, credentials=credentials)

response = vertexai.generative_models.GenerativeModel("deepseek-ai/deepseek-r1-0528-maas").generate_content("What is 1 + 1")
print("From vertex ai directly", response.candidates[0].content)

print(client.models.list(config={'query_base':True}).page)

When I call the list method it still returns an error

token is  <SECRET>
https://us-central1-aiplatform.googleapis.com
Traceback (most recent call last):
  File "/workspaces/pipelines/test.py", line 90, in <module>
    client = genai.Client(vertexai=True, credentials=credentials)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/client.py", line 219, in __init__
    self._api_client = self._get_api_client(
                       ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/client.py", line 265, in _get_api_client
    return BaseApiClient(
           ^^^^^^^^^^^^^^
  File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/_api_client.py", line 537, in __init__
    credentials, self.project = _load_auth(project=None)
                                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/_api_client.py", line 184, in _load_auth
    credentials, loaded_project_id = google.auth.default(  # type: ignore[no-untyped-call]
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/codespace/.local/lib/python3.12/site-packages/google/auth/_default.py", line 685, in default
    raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.

Originally posted by @lmtr0 in #1126
import json
import google.auth.transport.requests
from google.oauth2 import service_account as gservice_account
import vertexai
from google import genai

--- تنظیمات ---

SERVICE_ACCOUNT_FILE = "path/to/service-account.json" # مسیر فایل JSON حساب سرویس
PROJECT_ID = "your-project-id"
LOCATION = "us-central1" # یا هر region که مدل MaaS در آن فعال است

SCOPES = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/generative-language",
]

def get_credentials(service_account_file: str) -> gservice_account.Credentials:
"""ساخت Credentials از فایل حساب سرویس."""
with open(service_account_file, "r", encoding="utf-8") as f:
service_account_info = json.load(f)

credentials = gservice_account.Credentials.from_service_account_info(
    service_account_info, scopes=SCOPES
)
return credentials  # ← این return جا افتاده بود

credentials = get_credentials(SERVICE_ACCOUNT_FILE)
credentials.refresh(google.auth.transport.requests.Request())
print("token is", credentials.token)

API_ENDPOINT = f"https://{LOCATION}-aiplatform.googleapis.com"
print(API_ENDPOINT)

vertexai.init(
credentials=credentials,
project=PROJECT_ID,
api_endpoint=API_ENDPOINT,
location=LOCATION,
)

client = genai.Client(vertexai=True, credentials=credentials)

مدل MaaS (DeepSeek) از طریق client.models صدا زده می‌شود

response = client.models.generate_content(
model="deepseek-ai/deepseek-r1-0528-maas",
contents="What is 1 + 1",
)
print("From Vertex AI directly:", response.candidates[0].content)

models_page = client.models.list(config={"query_base": True})
print(models_page)

Activity

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

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.status:awaiting user responsetype: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions