Skip to content

AnthropicVertex in not in the vertex modul #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shdmitry2000 opened this issue Dec 4, 2024 · 7 comments
Closed

AnthropicVertex in not in the vertex modul #140

shdmitry2000 opened this issue Dec 4, 2024 · 7 comments

Comments

@shdmitry2000
Copy link

trying to work with Claude from model garden of goole and revive error
ImportError: cannot import name 'AnthropicVertex' from 'pydantic_ai.models.vertexai' (/usr/local/lib/python3.10/dist-packages/pydantic_ai/models/vertexai.py)

the code I trying to use:

from pydantic_ai import Agent
from pydantic_ai.models.vertexai import VertexAIModel, AnthropicVertex

model =AnthropicVertex('claude-3-5-sonnet-v2@20241022',project_id="me-sb-dgcp-dpoc-pocyosh-pr",region="us-east5")
agent = Agent(model)
result = agent.run_sync('Tell me a joke.')
print(result.data)

by default I use code :
from anthropic import AnthropicVertex

LOCATION=os.getenv("GOOGLE_CLOUDE_LOCATION","us-east5")
PROJECT=os.getenv("GOOGLE_CLOUDE_PROJECT_ID","me-sb-dgcp-dpoc-pocyosh-pr")
MODEL=os.getenv("ANTHROPIC_LLM_MODEL_NAME","claude-3-5-haiku@20241022")

client = AnthropicVertex(region=location, project_id=project_id)
response = self.client.messages.create(
model=self.model,
max_tokens=max_tokens,
temperature=temperature,
messages=messages
)
print( response.content[0].text )

@samuelcolvin
Copy link
Member

samuelcolvin commented Dec 4, 2024

Please format code in issues properly, this is unreadable.

What (I think) you're asking for is not currently possible since we don't yet support Anthropic #63.

Our VertexAIModel only supports gemini models since it relies on the format of the vertex API endpoints exactly matching the Gemini endpoints.

@emorling
Copy link

@samuelcolvin can this be revisited with support for Anthropic coming in today?

@acehand
Copy link

acehand commented Feb 4, 2025

any update on supporting anthropic via vertex ai ? @samuelcolvin

@shdmitry2000
Copy link
Author

any updates?

@Kludex
Copy link
Member

Kludex commented Apr 10, 2025

There's a new issue about this, and a PR. I'll check soon.

I'm not providing references because I'm on my phone. I trust your search skills. 😎👍

@acostajohn
Copy link

#1392 for reference

@shdmitry2000
Copy link
Author

i have question: is my implementation is right or i need to do it other way? it's work 100% but sure it right way!

`
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_vertex import GoogleVertexProvider
from pydantic_ai import Agent, UserError
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.providers.anthropic import AnthropicProvider,Provider
from pydantic_ai.models import cached_async_http_client
from anthropic import AnthropicVertex, AsyncAnthropicVertex
from anthropic import AsyncAnthropic

class VertexAnthropicProvider(AnthropicProvider):
"""Provider for Anthropic vertex API."""

@property
def client(self) -> AsyncAnthropicVertex:
    return self._client

@overload
def __init__(self, *, anthropic_client: AsyncAnthropicVertex | None = None) -> None: ...

@overload
def __init__(self, *, project_id: str | None = None, location: str | None = None, http_client: httpx.AsyncClient | None = None) -> None: ...

def __init__(
    self,
    *,
    project_id: str | None = None,
    region: str | None = None,
    anthropic_client: AsyncAnthropic | None = None,
    http_client: httpx.AsyncClient | None = None,
) -> None:
    """Create a new Anthropic provider.

    Args:
        api_key: The API key to use for authentication, if not provided, the `ANTHROPIC_API_KEY` environment variable
            will be used if available.
        anthropic_client: An existing [`AsyncAnthropic`](https://github.com/anthropics/anthropic-sdk-python)
            client to use. If provided, the `api_key` and `http_client` arguments will be ignored.
        http_client: An existing `httpx.AsyncClient` to use for making HTTP requests.
    """
    if anthropic_client is not None:
        assert http_client is None, 'Cannot provide both `anthropic_client` and `http_client`'
        assert project_id is None, 'Cannot provide both `anthropic_client` and `project_id`'
        assert region is None, 'Cannot provide both `anthropic_client` and `location`'
        self._client = anthropic_client
    else:
        if not project_id:
            raise UserError(
                'Set the `GOOGLE_CLOUD_PROJECT_ID` environment variable or pass it via `VertexAnthropicProvider(project_id=...)`'
                'to use the Anthropic provider.'
            )
        if not region:
            raise UserError(
                'Set the `VERTEX_LOCATION` environment variable or pass it via `VertexAnthropicProvider(location=...)`'
                'to use the Anthropic provider.'
            )   

        if http_client is not None:
            self._client = AsyncAnthropicVertex(project_id=project_id, region=region, http_client=http_client)
        else:
            http_client = cached_async_http_client(provider='anthropic')
            self._client = AsyncAnthropicVertex(project_id=project_id, region=region, http_client=http_client)

def get_anthropic_model(reasoner=False):
logger.info("Getting Anthropic model configuration...")
if reasoner:
model_name = config.get("ANTHROPIC_LLM_REASONER_MODEL", 'claude-3-7-sonnet@20250219')
else:
model_name = config.get("ANTHROPIC_LLM_MODEL_NAME", 'claude-3-5-sonnet-v2@20241022')

project_id = config.GOOGLE_CLOUD_PROJECT_ID
location = config.ANTHROPIC_VERTEX_LOCATION  # Use Anthropic-specific location

logger.info(f"Using model: {model_name}")
logger.info(f"Using location: {location}")
logger.info(f"Using project: {project_id}")

model = AnthropicModel(
    model_name=model_name, 
    provider=VertexAnthropicProvider(
        project_id=project_id, 
        region=location
    )
)    
return model

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants