anthropics / anthropic-sdk-python

MIT License
1.29k stars 157 forks source link

When using with VertexAI getting error: ValueError: Could not resolve project_id #422

Closed nikolamilosevic86 closed 5 months ago

nikolamilosevic86 commented 5 months ago

I am following the instruction given and tried both

client = AnthropicVertex(region=LOCATION, project_id=project_idea)

    message = client.messages.create(
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": query.prompt,
            }
        ],
        model="claude-3-sonnet@20240229",
    )
    answer = json.loads(message.model_dump_json(indent=2))

and with

os.environ["ANTHROPIC_VERTEX_PROJECT_ID"] = "xxxxxx"
File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/routing.py", line 758, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/routing.py", line 778, in app
    await route.handle(scope, receive, send)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/routing.py", line 299, in handle
    await self.app(scope, receive, send)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/routing.py", line 79, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/routing.py", line 74, in app
    response = await func(request)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/fastapi/routing.py", line 278, in app
    raw_response = await run_endpoint_function(
  File "/home/ubuntu/.local/lib/python3.9/site-packages/fastapi/routing.py", line 193, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/starlette/concurrency.py", line 42, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread
    return await future
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 851, in run
    result = context.run(func, *args)
  File "/home/ubuntu/linguist-llm-api/api.py", line 1613, in ask_claude3
    message = client.messages.create(
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/_utils/_utils.py", line 275, in wrapper
    return func(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/resources/messages.py", line 678, in create
    return self._post(
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/_base_client.py", line 1208, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/_base_client.py", line 897, in request
    return self._request(
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/_base_client.py", line 919, in _request
    self._prepare_request(request)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/lib/vertex/_client.py", line 170, in _prepare_request
    access_token = self._ensure_access_token()
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/lib/vertex/_client.py", line 183, in _ensure_access_token
    self._credentials, project_id = load_auth()
  File "/home/ubuntu/.local/lib/python3.9/site-packages/anthropic/lib/vertex/_auth.py", line 27, in load_auth
    raise ValueError("Could not resolve project_id")
ValueError: Could not resolve project_id

I am using Python 3.9 and this is pip freeze:

aiohttp==3.9.3
aiosignal==1.3.1
annotated-types==0.6.0
anthropic==0.21.3
anyio==4.3.0
async-timeout==4.0.3
attrs==23.2.0
boto3==1.34.69
botocore==1.34.69
cachetools==5.3.3
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
distro==1.9.0
elastic-transport==8.12.0
elasticsearch==8.12.1
exceptiongroup==1.2.0
fastapi==0.110.0
filelock==3.13.3
frozenlist==1.4.1
fsspec==2024.3.1
google-api-core==2.18.0
google-auth==2.29.0
google-cloud-aiplatform==1.44.0
google-cloud-bigquery==3.19.0
google-cloud-core==2.4.1
google-cloud-resource-manager==1.12.3
google-cloud-storage==2.16.0
google-crc32c==1.5.0
google-resumable-media==2.7.0
googleapis-common-protos==1.63.0
grpc-google-iam-v1==0.13.0
grpcio==1.62.1
grpcio-status==1.62.1
h11==0.14.0
httpcore==1.0.4
httpx==0.27.0
huggingface-hub==0.22.0
idna==3.6
Jinja2==3.1.3
jmespath==1.0.1
joblib==1.3.2
MarkupSafe==2.1.5
mpmath==1.3.0
multidict==6.0.5
networkx==3.2.1
nltk==3.8.1
numpy==1.26.4
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu12==8.9.2.26
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu12==12.1.0.106
nvidia-nccl-cu12==2.19.3
nvidia-nvjitlink-cu12==12.4.99
nvidia-nvtx-cu12==12.1.105
openai==0.28.0
packaging==24.0
proto-plus==1.23.0
protobuf==4.25.3
pyasn1==0.5.1
pyasn1-modules==0.3.0
pydantic==2.6.4
pydantic-core==2.16.3
python-dateutil==2.9.0.post0
PyYAML==6.0.1
regex==2023.12.25
requests==2.31.0
rsa==4.9
s3transfer==0.10.1
safetensors==0.4.2
shapely==2.0.3
six==1.16.0
sniffio==1.3.1
starlette==0.36.3
sympy==1.12
tokenizers==0.15.2
torch==2.2.1
tqdm==4.66.2
transformers==4.39.1
triton==2.2.0
typing-extensions==4.10.0
urllib3==1.26.18
uvicorn==0.29.0
vertexai==1.43.0
yarl==1.9.4

I cannot find what is the problem. Can you please help?

rattrayalex commented 5 months ago

can you verify that in this code client = AnthropicVertex(region=LOCATION, project_id=project_idea) there is a valid string at project_idea?

nikolamilosevic86 commented 5 months ago

Yes, project_idea is a string of my project name in Google cloud. Interestingly, I have created a venv environment and installed libraries that I pip freezed from my local mac to the server where I had an issue. The libraries are:

annotated-types==0.6.0
anthropic==0.21.3
anyio==4.3.0
boto3==1.34.69
botocore==1.34.69
cachetools==5.3.3
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
distro==1.9.0
elastic-transport==8.12.0
elasticsearch==8.12.1
exceptiongroup==1.2.0
fastapi==0.110.0
filelock==3.13.1
fsspec==2024.3.1
google-api-core==2.18.0
google-auth==2.29.0
google-cloud-aiplatform==1.44.0
google-cloud-bigquery==3.19.0
google-cloud-core==2.4.1
google-cloud-resource-manager==1.12.3
google-cloud-storage==2.16.0
google-crc32c==1.5.0
google-resumable-media==2.7.0
googleapis-common-protos==1.63.0
grpc-google-iam-v1==0.13.0
grpcio==1.62.1
grpcio-status==1.62.1
h11==0.14.0
httpcore==1.0.4
httpx==0.27.0
huggingface-hub==0.22.0
idna==3.6
Jinja2==3.1.3
jmespath==1.0.1
joblib==1.3.2
MarkupSafe==2.1.5
mpmath==1.3.0
networkx==3.2.1
nltk==3.8.1
numpy==1.26.4
openai==1.14.2
packaging==24.0
pillow==10.2.0
proto-plus==1.23.0
protobuf==4.25.3
pyasn1==0.5.1
pyasn1-modules==0.3.0
pydantic==2.6.4
pydantic_core==2.16.3
python-dateutil==2.9.0.post0
PyYAML==6.0.1
regex==2023.12.25
requests==2.31.0
rsa==4.9
s3transfer==0.10.1
safetensors==0.4.2
shapely==2.0.3
six==1.16.0
sniffio==1.3.1
starlette==0.36.3
sympy==1.12
tokenizers==0.15.2
torch==2.2.1
torchaudio==2.2.1
torchvision==0.17.1
tqdm==4.66.2
transformers==4.39.1
typing_extensions==4.10.0
urllib3==1.26.18
uvicorn==0.29.0
vertexai==1.43.0

And it works. Not sure what is exactly the difference, but there seems to be some compatibility issue. But now it works for me. It may make sense if you check what is the issue for future. But as I said now I do not have issue anymore.

rattrayalex commented 5 months ago

Great, glad to hear it! I'll close for now, but if others run into this and it continues to mystify, we can reopen.