-
Notifications
You must be signed in to change notification settings - Fork 59
Add pluggable LLM API provider hook with tool calling #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
haileyok
wants to merge
8
commits into
main
Choose a base branch
from
llm-provider-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c12276e
Add pluggable LLM API provider hook with tool calling
haileyok c84d1ba
Cleanup: correct stale anthropic-missing error message
haileyok 9cdcd25
Address code-review findings on the LLM provider hook
haileyok c12248d
Fix stale docs from cycle-2 review
haileyok d2a586a
Address PR review comments on the LLM provider hook
haileyok 70f9ccf
Add generic tool-calling layer: @tool registry + run_tool_loop
haileyok 2da12d9
Move LLM docs to their own page; trim register_plugins comment
haileyok 824883c
Address code-review findings (tool loop + dispatch + deps)
haileyok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Example LLM provider plugins for Osprey. | ||
|
|
||
| See :mod:`llm.anthropic_provider` for a direct Anthropic Messages API implementation | ||
| of :class:`osprey.worker.lib.llm.base.BaseLLMProvider`. | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| """Example LLM provider backed directly by the Anthropic Messages API. | ||
|
|
||
| This demonstrates implementing :class:`osprey.worker.lib.llm.base.BaseLLMProvider`, | ||
| including tool calling: it translates the vendor-neutral ``LLMMessage`` / | ||
| ``ToolDefinition`` types into Anthropic's request format, and maps the response | ||
| (including ``tool_use`` blocks) back into ``LLMResponse`` / ``ToolCall``. | ||
|
|
||
| The ``anthropic`` SDK is an optional dependency (``example_plugins[llm]``). It is | ||
| imported lazily so the base example package, and Osprey's CI, do not require the | ||
| SDK, an API key, or network access unless this provider is actually used. | ||
|
|
||
| Configuration (via Osprey ``Config`` or environment): | ||
|
|
||
| - API key: ``LLM_ANTHROPIC_API_KEY`` config key, else the ``ANTHROPIC_API_KEY`` | ||
| environment variable (read by the SDK itself if neither is set explicitly). | ||
| - Default model: ``LLM_ANTHROPIC_MODEL`` config key | ||
| (default: ``claude-3-5-sonnet-latest``). | ||
| - Default max tokens: ``LLM_ANTHROPIC_MAX_TOKENS`` config key (default: ``1024``). | ||
| """ | ||
|
haileyok marked this conversation as resolved.
|
||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence | ||
|
|
||
| from osprey.worker.lib.config import Config | ||
| from osprey.worker.lib.llm.base import ( | ||
| BaseLLMProvider, | ||
| LLMMessage, | ||
| LLMResponse, | ||
| LLMUsage, | ||
| ToolCall, | ||
| ToolDefinition, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from anthropic import Anthropic | ||
|
|
||
| DEFAULT_MODEL = 'claude-3-5-sonnet-latest' | ||
|
haileyok marked this conversation as resolved.
Outdated
|
||
| DEFAULT_MAX_TOKENS = 1024 | ||
|
|
||
|
|
||
| class AnthropicLLMProvider(BaseLLMProvider): | ||
| """A :class:`BaseLLMProvider` that calls the Anthropic Messages API directly.""" | ||
|
|
||
| def __init__(self, config: Config, client: Optional['Anthropic'] = None) -> None: | ||
| self._config = config | ||
| self._default_model = config.get_str('LLM_ANTHROPIC_MODEL', DEFAULT_MODEL) | ||
| self._default_max_tokens = config.get_int('LLM_ANTHROPIC_MAX_TOKENS', DEFAULT_MAX_TOKENS) | ||
| # Allow injecting a client (used in tests); otherwise build lazily on first use. | ||
| self._client = client | ||
|
|
||
| def _get_client(self) -> 'Anthropic': | ||
| if self._client is not None: | ||
| return self._client | ||
|
|
||
| try: | ||
| import anthropic | ||
| except ImportError as exc: # pragma: no cover - exercised only without the optional dep | ||
| raise RuntimeError( | ||
| "The 'anthropic' package is required to use AnthropicLLMProvider. " | ||
| "Install it with the optional extra, e.g. `uv pip install 'example_plugins[llm]'`." | ||
| ) from exc | ||
|
|
||
| api_key = self._config.get_optional_str('LLM_ANTHROPIC_API_KEY') or os.environ.get('ANTHROPIC_API_KEY') | ||
| # If api_key is None the SDK still reads ANTHROPIC_API_KEY from the environment itself. | ||
| self._client = anthropic.Anthropic(api_key=api_key) if api_key else anthropic.Anthropic() | ||
| return self._client | ||
|
|
||
| def chat( | ||
| self, | ||
| *, | ||
| messages: Sequence[LLMMessage], | ||
| system: Optional[str] = None, | ||
| tools: Optional[Sequence[ToolDefinition]] = None, | ||
| model: Optional[str] = None, | ||
| max_tokens: Optional[int] = None, | ||
| temperature: Optional[float] = None, | ||
| **params: Any, | ||
| ) -> LLMResponse: | ||
| request: Dict[str, Any] = { | ||
| 'model': model or self._default_model, | ||
| 'max_tokens': max_tokens or self._default_max_tokens, | ||
| 'messages': self._to_anthropic_messages(messages), | ||
| } | ||
|
|
||
| system_text = self._collect_system_text(system, messages) | ||
| if system_text is not None: | ||
| request['system'] = system_text | ||
|
|
||
| if tools: | ||
| request['tools'] = [self._to_anthropic_tool(tool) for tool in tools] | ||
|
|
||
| if temperature is not None: | ||
| request['temperature'] = temperature | ||
|
|
||
| # Provider-specific passthrough (e.g. top_p, stop_sequences, tool_choice). | ||
| request.update(params) | ||
|
|
||
| response = self._get_client().messages.create(**request) | ||
| return self._from_anthropic_response(response) | ||
|
|
||
| # --- request translation ------------------------------------------------ | ||
|
|
||
| @staticmethod | ||
| def _collect_system_text(system: Optional[str], messages: Sequence[LLMMessage]) -> Optional[str]: | ||
| parts: List[str] = [] | ||
| if system: | ||
| parts.append(system) | ||
| # Anthropic carries the system prompt as a top-level field, not a message, | ||
| # so fold any role='system' messages into it. | ||
| for message in messages: | ||
| if message.role == 'system' and message.content: | ||
| parts.append(message.content) | ||
| if not parts: | ||
| return None | ||
| return '\n\n'.join(parts) | ||
|
|
||
| @staticmethod | ||
| def _to_anthropic_tool(tool: ToolDefinition) -> Dict[str, Any]: | ||
| return { | ||
| 'name': tool.name, | ||
| 'description': tool.description, | ||
| 'input_schema': tool.input_schema, | ||
| } | ||
|
|
||
| @classmethod | ||
| def _to_anthropic_messages(cls, messages: Sequence[LLMMessage]) -> List[Dict[str, Any]]: | ||
| out: List[Dict[str, Any]] = [] | ||
| for message in messages: | ||
| # System messages are handled separately via the top-level `system` field. | ||
| if message.role == 'system': | ||
| continue | ||
|
|
||
| blocks = cls._message_content_blocks(message) | ||
| if not blocks: | ||
| continue | ||
|
|
||
| if message.cache_control is not None: | ||
| cache_control: Dict[str, Any] = {'type': 'ephemeral'} | ||
| if message.cache_control.ttl is not None: | ||
| cache_control['ttl'] = message.cache_control.ttl | ||
| blocks[-1]['cache_control'] = cache_control | ||
|
|
||
| # Tool results are surfaced to Anthropic as a user-role message. | ||
| role = 'user' if message.role == 'tool' else message.role | ||
| out.append({'role': role, 'content': blocks}) | ||
| return out | ||
|
|
||
| @staticmethod | ||
| def _message_content_blocks(message: LLMMessage) -> List[Dict[str, Any]]: | ||
| blocks: List[Dict[str, Any]] = [] | ||
|
|
||
| if message.content: | ||
| blocks.append({'type': 'text', 'text': message.content}) | ||
|
|
||
| for tool_call in message.tool_calls: | ||
| blocks.append( | ||
| { | ||
| 'type': 'tool_use', | ||
| 'id': tool_call.id, | ||
| 'name': tool_call.name, | ||
| 'input': tool_call.arguments, | ||
| } | ||
| ) | ||
|
|
||
| for tool_result in message.tool_results: | ||
| blocks.append( | ||
| { | ||
| 'type': 'tool_result', | ||
| 'tool_use_id': tool_result.tool_call_id, | ||
| 'content': tool_result.content, | ||
| 'is_error': tool_result.is_error, | ||
| } | ||
| ) | ||
|
|
||
| return blocks | ||
|
|
||
| # --- response translation ------------------------------------------------ | ||
|
|
||
| @staticmethod | ||
| def _from_anthropic_response(response: Any) -> LLMResponse: | ||
| text_parts: List[str] = [] | ||
| tool_calls: List[ToolCall] = [] | ||
|
|
||
| for block in getattr(response, 'content', None) or []: | ||
| block_type = getattr(block, 'type', None) | ||
| if block_type == 'text': | ||
| text_parts.append(getattr(block, 'text', '') or '') | ||
| elif block_type == 'tool_use': | ||
| tool_calls.append( | ||
| ToolCall( | ||
| id=getattr(block, 'id', ''), | ||
| name=getattr(block, 'name', ''), | ||
| arguments=dict(getattr(block, 'input', {}) or {}), | ||
| ) | ||
| ) | ||
|
|
||
| usage: Optional[LLMUsage] = None | ||
| raw_usage = getattr(response, 'usage', None) | ||
| if raw_usage is not None: | ||
| usage = LLMUsage( | ||
| input_tokens=getattr(raw_usage, 'input_tokens', 0) or 0, | ||
| output_tokens=getattr(raw_usage, 'output_tokens', 0) or 0, | ||
| cache_read_tokens=getattr(raw_usage, 'cache_read_input_tokens', 0) or 0, | ||
| cache_write_tokens=getattr(raw_usage, 'cache_creation_input_tokens', 0) or 0, | ||
| ) | ||
|
|
||
| return LLMResponse( | ||
| text=''.join(text_parts), | ||
| tool_calls=tool_calls, | ||
| stop_reason=getattr(response, 'stop_reason', None), | ||
| usage=usage, | ||
| raw=response, | ||
| ) | ||
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.