llm_anthropic#
- class baf.nlp.llm.llm_anthropic.LLMAnthropic(agent, name, parameters, num_previous_messages=1, global_context=None)[source]#
Bases:
LLMLLM wrapper for Anthropic Claude models using the native
anthropicSDK.Anthropic’s API differs enough from the OpenAI shape (system-prompt placement, tool-use schema, required
max_tokens, streaming model) that a dedicated implementation is cleaner than relying on the OpenAI-compat endpoint.Requires
nlp.anthropic.api_keyto be set in the agent config file (or passed as"api_key"insideparametersfor a one-time override).- Parameters:
agent (Agent) – the agent the LLM belongs to
name (str) – the model identifier (e.g.
"claude-opus-4-5","claude-sonnet-4-5","claude-haiku-4-5-20251001").parameters (dict) – parameters forwarded to
messages.create(e.g.{"temperature": 0.7, "max_tokens": 2048}). May also contain"api_key"which is consumed byinitialize()and not forwarded.num_previous_messages (int) – number of previous conversation turns to include in
chat()calls (must be > 0).global_context (str) – system-level context injected into every request.
- client#
the
anthropic.Anthropicclient, instantiated ininitialize().
- _abc_impl = <_abc._abc_data object>#
- _build_system(system_message=None, session=None)[source]#
Assemble the Anthropic
systemparameter.Anthropic’s API requires a single top-level
systemstring (orNOT_GIVEN), not a list of system-role messages. We concatenate global context, session user context, and the per-call system message in that order.
- static _openai_tools_to_anthropic(tools)[source]#
Convert OpenAI-style tool schemas to Anthropic tool format.
OpenAI:
{"type": "function", "function": {"name", "description", "parameters"}}Anthropic:{"name", "description", "input_schema"}
- _resolve_params(parameters)[source]#
Merge call-time parameters with defaults.
Ensures
max_tokensis always present (Anthropic requires it).
- chat(session, parameters=None, system_message=None)[source]#
Make a prediction, i.e., generate an output.
This function can provide the chat history to the LLM for the output generation, simulating a conversation or remembering previous messages.
- initialize()[source]#
Instantiate the Anthropic client.
Reads
api_keyfromself.parametersfirst (popping it so it is not forwarded to the API call), then falls back to thenlp.anthropic.api_keyconfig-file property.
- intent_classification(intent_classifier, message, parameters=None)[source]#
Classify intent via the Anthropic API, returning the same shape as LLMOpenAI.
The output is fed to
default_json_to_intent_classifier_predictions()so that intent classifiers remain provider-agnostic.
- predict(message, parameters=None, session=None, system_message=None)[source]#
Make a prediction, i.e., generate an output.
- Parameters:
message (Any) – the LLM input text
session (Session) – the ongoing session, can be None if no context needs to be applied
parameters (dict) – the LLM parameters to use in the prediction. If none is provided, the default LLM parameters will be used
system_message (str) – system message to give high priority context to the LLM
- Returns:
the LLM output
- Return type:
- predict_with_tools(messages, tools, parameters=None, system_message=None)[source]#
Make a tool-calling prediction using the native Anthropic SDK.
Input
messagesandtoolsuse OpenAI schema; this method translates them to the Anthropic format and maps the response back to the sharedLLMResponse/ToolCalltypes so callers remain provider-agnostic.- Parameters:
- Returns:
final text or list of tool calls.
- Return type:
- baf.nlp.llm.llm_anthropic._DEFAULT_MAX_TOKENS = 4096#
Default
max_tokensused when the caller does not supply one.The Anthropic Messages API requires
max_tokensto be provided explicitly; this constant is used as a safe default when it is absent fromself.parameters.