mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-15 06:26:12 +00:00
fix(ollama): reasoning should come before text content (#32476)
This commit is contained in:
@@ -185,6 +185,50 @@ class TestMessageConversion:
|
||||
assert result.content[0].get("text") == "Hello"
|
||||
assert result.response_metadata.get("context") == test_context
|
||||
|
||||
def test_reasoning_content_block_comes_before_text(self) -> None:
|
||||
"""Test that ReasoningContentBlock always comes before TextContentBlock."""
|
||||
ollama_response = {
|
||||
"model": MODEL_NAME,
|
||||
"created_at": "2024-01-01T00:00:00Z",
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "The final answer is 4.",
|
||||
"thinking": "Let me calculate: 2 + 2 = 4",
|
||||
},
|
||||
"done": True,
|
||||
"done_reason": "stop",
|
||||
}
|
||||
|
||||
result = _convert_to_v1_from_ollama_format(ollama_response)
|
||||
|
||||
assert isinstance(result, AIMessage)
|
||||
assert len(result.content) == 2
|
||||
assert result.content[0].get("type") == "reasoning"
|
||||
assert result.content[1].get("type") == "text"
|
||||
|
||||
def test_reasoning_content_block_comes_before_text_in_chunks(self) -> None:
|
||||
"""Test ReasoningContentBlock comes before TextContentBlock in chunks."""
|
||||
chunk = {
|
||||
"model": MODEL_NAME,
|
||||
"created_at": "2024-01-01T00:00:00Z",
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "The answer is 42.",
|
||||
"thinking": "I need to think about this carefully...",
|
||||
},
|
||||
"done": False,
|
||||
}
|
||||
|
||||
result = _convert_chunk_to_v1(chunk)
|
||||
|
||||
assert len(result.content) == 2
|
||||
assert result.content[0].get("type") == "reasoning"
|
||||
reasoning_text = "I need to think about this carefully..."
|
||||
assert result.content[0].get("reasoning") == reasoning_text
|
||||
|
||||
assert result.content[1].get("type") == "text"
|
||||
assert result.content[1].get("text") == "The answer is 42."
|
||||
|
||||
def test_convert_empty_content(self) -> None:
|
||||
"""Test converting empty content blocks."""
|
||||
message = HumanMessage(content=[])
|
||||
|
Reference in New Issue
Block a user