pydantic-ai icon indicating copy to clipboard operation
pydantic-ai copied to clipboard

Anthropic support

Open samuelcolvin opened this issue 1 year ago • 20 comments

So far we have OpenAI and Gemini, adding Anthropic seems a pretty obvious next step.

I don't think there are any other models we need to add to the core library immediately?

samuelcolvin avatar Nov 18 '24 23:11 samuelcolvin

Perplexity AI could be an option?

adityaraute avatar Dec 01 '24 16:12 adityaraute

@adityaraute if you want another model or provider, please create another issue.

samuelcolvin avatar Dec 02 '24 19:12 samuelcolvin

I'll try to add it! Thats ok?

igor-silveira avatar Dec 03 '24 00:12 igor-silveira

@samuelcolvin i dont have permission to push a new branch. Can you give me permission to create new branches?

igor-silveira avatar Dec 03 '24 12:12 igor-silveira

@igor-silveira you have permission to create a fork and create a pull request from there - that's how GitHub works.

samuelcolvin avatar Dec 04 '24 10:12 samuelcolvin

I'd be happy to pick this up, hoping to have a PR up by EOD :)

sydney-runkle avatar Dec 04 '24 14:12 sydney-runkle

Think Mistral AI or Bedrock would be nice if you want to handle EU needs

MarcusElwin avatar Dec 05 '24 09:12 MarcusElwin

We have a separate issue about bedrock: #118.

Mistral models are already supported by groq, and will be supported by #112.

If you need something more, please create a new issue.

samuelcolvin avatar Dec 05 '24 10:12 samuelcolvin

I'd be happy to pick this up, hoping to have a PR up by EOD :)

Is there a link to a PR for this you could share, perchance? No pressure to finish it if you're onto other things, but I'm super curious to give it a whirl!

dazzaji avatar Dec 07 '24 23:12 dazzaji

@dazzaji,

Ah yeah, got tied up with some other things late last week. I'll prioritize this tomorrow. I can send a link then.

That being said, if you're really eager, feel free to start up, I'm happy to collaborate!

sydney-runkle avatar Dec 08 '24 23:12 sydney-runkle

Can’t wait for you guys to finish this :) Function calling only works great with Anthropic right now.

sagarspatil avatar Dec 09 '24 05:12 sagarspatil

@dazzaji,

I'm getting rolling on this now :).

sydney-runkle avatar Dec 09 '24 14:12 sydney-runkle

what about support for the Openai from azure ?

PRAYALANKAR avatar Dec 10 '24 10:12 PRAYALANKAR

Is this support ready? When trying to adopt the Anthropic shim, it looks like the anthropic model files aren’t included in the latest 0.0.12 release. I couldn’t tell if there was a bug in the docs, a bug in the packaging or a user error in my part?

mdelder avatar Dec 14 '24 17:12 mdelder

I'm getting the same issue that @mdelder raised.

pietermoller-glydepay avatar Dec 15 '24 15:12 pietermoller-glydepay

Same issue here as @mdelder. It seems documentation and code are not aligned?

ramonvg avatar Dec 15 '24 20:12 ramonvg

We haven't made a release yet, will do tomorrow.

You can always install from main if you're impatient.

samuelcolvin avatar Dec 15 '24 23:12 samuelcolvin

I ended up building from main and it worked for me.

Thanks!

mdelder avatar Dec 17 '24 03:12 mdelder

it made a release last night.

samuelcolvin avatar Dec 17 '24 09:12 samuelcolvin

The release now has the Anthropic models. Thanks so much!

pietermoller-glydepay avatar Dec 18 '24 09:12 pietermoller-glydepay

Thanks @sydney-runkle for anthropic support, regarding stream support you mentioned might come after an internal streaming refactor Is there more info on what is needed?

celeriev avatar Jan 09 '25 09:01 celeriev

I'd also love to find out what the ETA is on streaming implemention for Claude

seunggs avatar Jan 10 '25 04:01 seunggs

We'll get this across the line by early next week :)

sydney-runkle avatar Jan 17 '25 14:01 sydney-runkle

Awesome looking forward to it!

seunggs avatar Jan 17 '25 15:01 seunggs

@sydney-runkle If you'd like someone to test it out I'm excellent at accidentally breaking every piece of code presented to me and happy to share my rare talent.

dazzaji avatar Jan 17 '25 20:01 dazzaji

Marking as done, we've released anthropic streaming in v0.0.20!

sydney-runkle avatar Jan 24 '25 14:01 sydney-runkle

@sydney-runkle is there any intention to support the Anthropic models on Google Vertex?

samwit avatar Jan 29 '25 12:01 samwit

@sydney-runkle I just tried Anthropic streaming (pydantic-ai v0.0.20) and the stream comes in as one big chunk, not as an actual stream (although there is no longer an error about anthropic streaming not being supported). I can confirm the code is correct since a simple switch to "openai:gpt-4o" model creates an actual stream.

...
async with agent.run_stream(
    user_prompt=prompt,
    deps=agent_deps,
    message_history=message_history
) as result:
    # Stream the model's response
    async for chunk in result.stream():
        print("\nStreaming chunk:", chunk)
        yield f"event: document_messages\ndata: {json.dumps(chunk)}\n\n"
...

I discovered that this only happens if I specify the result_type (streaming works fine without):

class DocumentModelResponse(TypedDict, total=False):
    model_response: str
    document_md: str

agent = Agent(
    agent_data["model"],
    deps_type=AgentDeps,
    result_type=DocumentModelResponse,
    tools=tools,
    system_prompt=system_prompt
)

seunggs avatar Jan 29 '25 18:01 seunggs