WIP: Add Claude API support alongside existing GPT API #14
Reference in New Issue
Block a user
Delete Branch "copilot/add-claude-api-support"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Adds Anthropic Claude models as an alternative AI provider, allowing users to choose between OpenAI and Claude models via the existing
/choose_modelor/set_modelcommands.Changes
New Claude models available:
claude/claude-3-5-sonnet- Best balance of intelligence and speedclaude/claude-3-5-haiku- Fastest, most affordableclaude/claude-3-opus- Most capable for complex tasksConfiguration:
ANTHROPIC_API_KEYenv var in.env.exampleandconfig.pypricing.pyMODEL_TOKEN_LIMITSNew module
src/utils/claude_utils.py:is_claude_model()- Detects Claude model selectionconvert_messages_for_claude()- Converts OpenAI message format to Claude's format (extracts system prompt, handles content arrays)call_claude_api()- Async wrapper for Anthropic API callsIntegration points:
bot.py- Initializes Claude client when API key presentmessage_handler.py- Routes requests to appropriate API based on model prefixcommands.py-/searchand/webcommands support ClaudeUsage
Users select Claude models the same way as OpenAI models. Note: Claude models don't currently support function calling/tools—text-only conversations.
from src.module.message_handler import MessageHandler
print('message_handler imported successfully')
from src.c` (dns block)
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Comment
@coderabbitai helpto get the list of available commands and usage tips.Pull request overview
This PR adds support for Anthropic's Claude models alongside the existing OpenAI GPT models, enabling users to select Claude models through the same
/choose_modelor/set_modelcommands. The implementation creates a parallel infrastructure for Claude API calls while maintaining backward compatibility with existing OpenAI functionality.Key changes:
claude_utils.pymodule provides Claude-specific API handling with message format conversionbot.pywith graceful degradation when API key is not providedmessage_handler.pyandcommands.pyto route requests to Claude or OpenAI based on model selectionReviewed changes
Copilot reviewed 8 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
src/utils/claude_utils.pybot.pysrc/module/message_handler.pysrc/commands/commands.py/searchand/webcommands with API routing logicsrc/config/config.pysrc/config/pricing.pyrequirements.txt.env.example.gitignoretests/test_bot.py💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Print statement may execute during import.
@@ -15,6 +15,7 @@ import base64import tracebackfrom datetime import datetime, timedeltafrom src.utils.openai_utils import process_tool_calls, prepare_messages_for_api, get_tools_for_modelfrom src.utils.claude_utils import is_claude_model, call_claude_api, convert_messages_for_claudeImport of 'convert_messages_for_claude' is not used.
@@ -0,0 +50,4 @@# Extract system messageif role == 'system':system_prompt = content if isinstance(content, str) else str(content)continueIf there are multiple system messages in the input, only the last one will be preserved since
system_promptis overwritten in each iteration. This could lead to loss of important system context.Consider either:
system_prompt = (system_prompt or "") + "\n" + content@@ -0,0 +61,4 @@converted_messages.append({"role": role,"content": content})When
contentis an empty string, an empty message is still added toconverted_messages. While Claude may handle empty content gracefully, it's better to skip empty messages to avoid potential API errors.Consider adding:
@@ -0,0 +151,4 @@api_params = {"model": anthropic_model,"max_tokens": max_tokens,"messages": claude_messages,If the input messages contain only system messages, tool calls, or function calls,
claude_messageswill be empty after filtering. The Claude API requires at least one message in the messages array, which would cause an API error.Consider adding validation:
@@ -378,0 +432,4 @@self.assertIsNone(system_prompt)self.assertEqual(len(converted), 2)The
call_claude_apifunction, which is a critical integration point with the Claude API, lacks test coverage. Consider adding tests for:Example test structure:
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.