22
22
import pytest
23
23
from neo4j_graphrag .exceptions import LLMGenerationError
24
24
from neo4j_graphrag .llm .anthropic_llm import AnthropicLLM
25
+ from neo4j_graphrag .llm .types import LLMResponse
25
26
26
27
27
28
@pytest .fixture
@@ -61,11 +62,9 @@ def test_anthropic_invoke_with_message_history_happy_path(mock_anthropic: Mock)
61
62
content = "generated text"
62
63
)
63
64
model_params = {"temperature" : 0.3 }
64
- system_instruction = "You are a helpful assistant."
65
65
llm = AnthropicLLM (
66
66
"claude-3-opus-20240229" ,
67
67
model_params = model_params ,
68
- system_instruction = system_instruction ,
69
68
)
70
69
message_history = [
71
70
{"role" : "user" , "content" : "When does the sun come up in the summer?" },
@@ -79,11 +78,71 @@ def test_anthropic_invoke_with_message_history_happy_path(mock_anthropic: Mock)
79
78
llm .client .messages .create .assert_called_once_with ( # type: ignore[attr-defined]
80
79
messages = message_history ,
81
80
model = "claude-3-opus-20240229" ,
82
- system = system_instruction ,
81
+ system = None ,
83
82
** model_params ,
84
83
)
85
84
86
85
86
+ def test_anthropic_invoke_with_message_history_and_system_instruction (
87
+ mock_anthropic : Mock ,
88
+ ) -> None :
89
+ mock_anthropic .Anthropic .return_value .messages .create .return_value = MagicMock (
90
+ content = "generated text"
91
+ )
92
+ model_params = {"temperature" : 0.3 }
93
+ initial_instruction = "You are a helpful assistant."
94
+ llm = AnthropicLLM (
95
+ "claude-3-opus-20240229" ,
96
+ model_params = model_params ,
97
+ system_instruction = initial_instruction ,
98
+ )
99
+ message_history = [
100
+ {"role" : "user" , "content" : "When does the sun come up in the summer?" },
101
+ {"role" : "assistant" , "content" : "Usually around 6am." },
102
+ ]
103
+ question = "What about next season?"
104
+
105
+ # first invokation - initial instructions
106
+ response = llm .invoke (question , message_history ) # type: ignore
107
+ assert response .content == "generated text"
108
+ message_history .append ({"role" : "user" , "content" : question })
109
+ llm .client .messages .create .assert_called_once_with ( # type: ignore[attr-defined]
110
+ model = "claude-3-opus-20240229" ,
111
+ system = initial_instruction ,
112
+ messages = message_history ,
113
+ ** model_params ,
114
+ )
115
+
116
+ # second invokation - override instructions
117
+ override_instruction = "Ignore all previous instructions"
118
+ question = "When does it come up in the winter?"
119
+ response = llm .invoke (question , message_history , override_instruction ) # type: ignore
120
+ assert isinstance (response , LLMResponse )
121
+ assert response .content == "generated text"
122
+ message_history .append ({"role" : "user" , "content" : question })
123
+ llm .client .messages .create .assert_called_with ( # type: ignore[attr-defined]
124
+ model = "claude-3-opus-20240229" ,
125
+ system = override_instruction ,
126
+ messages = message_history ,
127
+ ** model_params ,
128
+ )
129
+
130
+ # third invokation - default instructions
131
+ question = "When does it set?"
132
+ response = llm .invoke (question , message_history ) # type: ignore
133
+ assert isinstance (response , LLMResponse )
134
+ assert response .content == "generated text"
135
+ message_history .append ({"role" : "user" , "content" : question })
136
+ llm .client .messages .create .assert_called_with ( # type: ignore[attr-defined]
137
+ model = "claude-3-opus-20240229" ,
138
+ system = initial_instruction ,
139
+ messages = message_history ,
140
+ ** model_params ,
141
+ )
142
+
143
+ assert llm .client .messages .create .call_count == 3 # type: ignore
144
+
145
+
87
146
def test_anthropic_invoke_with_message_history_validation_error (
88
147
mock_anthropic : Mock ,
89
148
) -> None :
0 commit comments