langchain-ai / langchain-google

MIT License
105 stars 121 forks source link

Support `with_structured_output` in `ChatAnthropicVertex` #176

Closed shotarok closed 3 months ago

shotarok commented 5 months ago

ChatAnthropicVertex doesn't support with_structured_output although ChatVertexAI support it.

lkuligin commented 5 months ago

would you like to work on this one?

shotarok commented 5 months ago

Yes, I'm happy to contribute to this project! I plan to use PydanticOutputParser and JsonOutputParser because ChatAnthropicVertex doesn't support function_calling.

lkuligin commented 5 months ago

we should probably start with adding function calling support first.

shotarok commented 5 months ago

Oh, I had assumed Claude API on Vertex AI doesn't support function calling since the doc doesn't mention it, but I'll test a function call. I agree. If it works, supporting function call would be the first task.

shotarok commented 5 months ago

I'm afraid I couldn't confirm that Claude on Vertex AI supports function calling. I got this error: "tools: Extra inputs are not permitted" with claude-3-haiku@20240307 on Vertex AI. So, I'll create a PR with PydanticOutputParser and JsonOutputParser. I attached the succeeded and failed curl commands, just in case.

A succeeded curl without function calling ```console $ curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "anthropic_version": "vertex-2023-10-16", "max_tokens": 100, "messages": [{"role": "user", "content": "What is the weather like in San Francisco?"}] }' \ "https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/anthropic/models/${MODEL}:rawPredict" > {"id":"msg_vrtx_01RYFRNxnQZRDsp71Jq1aKXH","type":"message","role":"assistant","model":"claude-3-haiku-20240307","stop_sequence":null,"usage":{"input_tokens":16,"output_tokens":100},"content":[{"type":"text","text":"The weather in San Francisco can be quite variable, but here's a general overview:\n\n- Mild Temperatures - San Francisco has a Mediterranean climate, with average highs around 65°F (18°C) year-round. Summers are cool, with highs in the 60s or 70s Fahrenheit (15-25°C). Winters are mild, with highs in the 50s and 60s Fahrenheit (10-20"}],"stop_reason":"max_tokens"} ```
A failed curl with function calling ```console $ curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "anthropic_version": "vertex-2023-10-16", "max_tokens": 100, "tools": [{ "name": "get_weather", "description": "Get the current weather in a given location", "input_schema": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "The unit of temperature, either \"celsius\" or \"fahrenheit\"" } }, "required": ["location"] } }],"messages": [{"role": "user", "content": "What is the weather like in San Francisco?"}] }' \ "https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/anthropic/models/${MODEL}:rawPredict" > {"type":"error","error":{"type":"invalid_request_error","message":"tools: Extra inputs are not permitted"}} ```
shotarok commented 5 months ago

After reading langchain-core, I understand the other chat models using PydanticOutputParser and JsonOutputParser assume the LLM API can control output format as JSON like the JSON mode in OpenAI.

Until Claude on Vertex AI supports tools, we may be unable to support with_structured_output in ChatAnthropicVertex. Instead, we can use StructuredOutputParser with giving a prompt by ourselves 1.

Please let me know if you have a different implementation idea. Thanks!

lkuligin commented 5 months ago

yes, until tools are supported we should rely on a model following the prompt. you can even try to add an optional extra step to the chain to convert the output to a desired format like here: https://github.com/langchain-ai/langchain-google/blob/3b34f652d00bac0a814d0b63829f5b7c67e71057/libs/vertexai/langchain_google_vertexai/chains.py#L92

rvndbalaji commented 4 months ago

@shotarok @lkuligin
Anthropic made tool-use now generally available in their latest version as well as in VertexAI Can bind_tools and with_structured_output be made available via langchain now?

https://www.anthropic.com/news/tool-use-ga https://github.com/anthropics/anthropic-sdk-python/releases/tag/v0.27.0

rvndbalaji commented 3 months ago

@shotarok and @lkuligin I've made the required changes and opened a PR - https://github.com/langchain-ai/langchain-google/pull/288

This is my first contribution, so please do let me know if i've missed anything