mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-21 23:17:48 +00:00
anthropic[patch]: always return content blocks if citations are generated (#29398)
We currently return string (and therefore no content blocks / citations) if the response is of the form ``` [ {"text": "a claim", "citations": [...]}, ] ``` There are other cases where we do return citations as-is: ``` [ {"text": "a claim", "citations": [...]}, {"text": "some other text"}, {"text": "another claim", "citations": [...]}, ] ``` Here we update to return content blocks including citations in the first case as well.
This commit is contained in:
parent
933b35b9c5
commit
ed797e17fb
@ -764,7 +764,11 @@ class ChatAnthropic(BaseChatModel):
|
|||||||
llm_output = {
|
llm_output = {
|
||||||
k: v for k, v in data_dict.items() if k not in ("content", "role", "type")
|
k: v for k, v in data_dict.items() if k not in ("content", "role", "type")
|
||||||
}
|
}
|
||||||
if len(content) == 1 and content[0]["type"] == "text":
|
if (
|
||||||
|
len(content) == 1
|
||||||
|
and content[0]["type"] == "text"
|
||||||
|
and "citations" not in content[0]
|
||||||
|
):
|
||||||
msg = AIMessage(content=content[0]["text"])
|
msg = AIMessage(content=content[0]["text"])
|
||||||
elif any(block["type"] == "tool_use" for block in content):
|
elif any(block["type"] == "tool_use" for block in content):
|
||||||
tool_calls = extract_tool_calls(content)
|
tool_calls = extract_tool_calls(content)
|
||||||
|
@ -622,3 +622,30 @@ def test_pdf_document_input() -> None:
|
|||||||
assert isinstance(result, AIMessage)
|
assert isinstance(result, AIMessage)
|
||||||
assert isinstance(result.content, str)
|
assert isinstance(result.content, str)
|
||||||
assert len(result.content) > 0
|
assert len(result.content) > 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_citations() -> None:
|
||||||
|
llm = ChatAnthropic(model="claude-3-5-haiku-latest")
|
||||||
|
messages = [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "document",
|
||||||
|
"source": {
|
||||||
|
"type": "content",
|
||||||
|
"content": [
|
||||||
|
{"type": "text", "text": "The grass is green"},
|
||||||
|
{"type": "text", "text": "The sky is blue"},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"citations": {"enabled": True},
|
||||||
|
},
|
||||||
|
{"type": "text", "text": "What color is the grass and sky?"},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
response = llm.invoke(messages)
|
||||||
|
assert isinstance(response, AIMessage)
|
||||||
|
assert isinstance(response.content, list)
|
||||||
|
assert any("citations" in block for block in response.content)
|
||||||
|
Loading…
Reference in New Issue
Block a user