Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/google/adk/models/anthropic_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,14 @@
import base64
import copy
import dataclasses
from functools import cached_property
import json
import logging
import os
import re
from typing import Any
from typing import AsyncGenerator
from typing import Iterable
from typing import Literal
from typing import Optional
from typing import TYPE_CHECKING
from typing import Union

from anthropic import AsyncAnthropic
from anthropic import AsyncAnthropicVertex
from anthropic import NOT_GIVEN
from anthropic import NotGiven
from functools import cached_property
from typing import TYPE_CHECKING, Any, AsyncGenerator, Iterable, Literal, Optional, Union

from anthropic import NOT_GIVEN, AsyncAnthropic, AsyncAnthropicVertex, NotGiven
from anthropic import types as anthropic_types
from google.genai import types
from pydantic import BaseModel
Expand Down Expand Up @@ -401,11 +392,16 @@ async def generate_content_async(
if llm_request.tools_dict
else NOT_GIVEN
)
system = (
llm_request.config.system_instruction
if llm_request.config and llm_request.config.system_instruction
else NOT_GIVEN
)

if not stream:
message = await self._anthropic_client.messages.create(
model=model_to_use,
system=llm_request.config.system_instruction,
system=system,
messages=messages,
tools=tools,
tool_choice=tool_choice,
Expand All @@ -414,7 +410,7 @@ async def generate_content_async(
yield message_to_generate_content_response(message)
else:
async for response in self._generate_content_streaming(
llm_request, messages, tools, tool_choice
llm_request, messages, tools, tool_choice, system
):
yield response

Expand All @@ -424,6 +420,7 @@ async def _generate_content_streaming(
messages: list[anthropic_types.MessageParam],
tools: Union[Iterable[anthropic_types.ToolUnionParam], NotGiven],
tool_choice: Union[anthropic_types.ToolChoiceParam, NotGiven],
system: Union[str, NotGiven] = NOT_GIVEN,
) -> AsyncGenerator[LlmResponse, None]:
"""Handles streaming responses from Anthropic models.

Expand All @@ -433,7 +430,7 @@ async def _generate_content_streaming(
model_to_use = self._resolve_model_name(llm_request.model)
raw_stream = await self._anthropic_client.messages.create(
model=model_to_use,
system=llm_request.config.system_instruction,
system=system,
messages=messages,
tools=tools,
tool_choice=tool_choice,
Expand Down
Loading