Skip to content

Commit b83bd80

Browse files
authored
Merge pull request #16 from mozilla-ai/fix-integration-test
Update chat_models.py
2 parents cfddb6e + f9d93f9 commit b83bd80

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

langchain_anyllm/chat_models.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ def _create_chat_result(self, response: ChatCompletion) -> ChatResult:
268268
}
269269
return ChatResult(generations=generations, llm_output=llm_output)
270270

271+
def _is_anthropic_model(self) -> bool:
272+
"""Check if the model is an Anthropic model."""
273+
return self.model.startswith("anthropic:") or "claude" in self.model.lower()
274+
271275
def _create_params(
272276
self, stop: list[str] | None = None, **kwargs: Any
273277
) -> dict[str, Any]:
@@ -305,10 +309,14 @@ def _create_params(
305309
params["response_format"] = self.response_format
306310

307311
if stop is not None:
308-
if "stop" in params:
312+
if "stop" in params or "stop_sequences" in params:
309313
error_message = "`stop` found in both the input and default params."
310314
raise ValueError(error_message)
311-
params["stop"] = stop
315+
# Anthropic uses stop_sequences instead of stop
316+
if self._is_anthropic_model():
317+
params["stop_sequences"] = stop
318+
else:
319+
params["stop"] = stop
312320

313321
# Translate LangChain tool_choice to OpenAI-compatible values
314322
# Only include tool_choice if tools are present
@@ -365,8 +373,10 @@ def _stream(
365373
params = self._create_params(stop, **kwargs)
366374
params["stream"] = True
367375

368-
if "stream_options" not in params and self.stream_options:
369-
params["stream_options"] = self.stream_options
376+
# Anthropic doesn't support stream_options
377+
if not self._is_anthropic_model():
378+
if "stream_options" not in params and self.stream_options:
379+
params["stream_options"] = self.stream_options
370380

371381
default_chunk_class: type[BaseMessageChunk] = AIMessageChunk
372382

@@ -444,8 +454,10 @@ async def _astream(
444454
params = self._create_params(stop, **kwargs)
445455
params["stream"] = True
446456

447-
if "stream_options" not in params and self.stream_options:
448-
params["stream_options"] = self.stream_options
457+
# Anthropic doesn't support stream_options
458+
if not self._is_anthropic_model():
459+
if "stream_options" not in params and self.stream_options:
460+
params["stream_options"] = self.stream_options
449461

450462
default_chunk_class: type[BaseMessageChunk] = AIMessageChunk
451463

0 commit comments

Comments
 (0)