Skip to content

feat: Add Siliconflow API support #96

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

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ ANTHROPIC_API_KEY=your_anthropic_api_key_here
DEEPSEEK_API_KEY=your_deepseek_api_key_here
GOOGLE_API_KEY=your_google_api_key_here
AZURE_OPENAI_API_KEY=your_azure_openai_api_key_here
AZURE_OPENAI_MODEL_DEPLOYMENT=gpt-4o-ms
AZURE_OPENAI_MODEL_DEPLOYMENT=gpt-4o-ms
SILICONFLOW_API_KEY=your_siliconflow_api_key_here
35 changes: 34 additions & 1 deletion tests/test_llm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,23 @@ def setUp(self):
self.mock_gemini_model.generate_content.return_value = self.mock_gemini_response
self.mock_gemini_client.GenerativeModel.return_value = self.mock_gemini_model

# Set up SiliconFlow-style response
self.mock_siliconflow_response = MagicMock()
self.mock_siliconflow_choice = MagicMock()
self.mock_siliconflow_message = MagicMock()
self.mock_siliconflow_message.content = "Test Siliconflow response"
self.mock_siliconflow_choice.message = self.mock_siliconflow_message
self.mock_siliconflow_response.choices = [self.mock_siliconflow_choice]

# Mock environment variables
self.env_patcher = patch.dict('os.environ', {
'OPENAI_API_KEY': 'test-openai-key',
'DEEPSEEK_API_KEY': 'test-deepseek-key',
'ANTHROPIC_API_KEY': 'test-anthropic-key',
'GOOGLE_API_KEY': 'test-google-key',
'AZURE_OPENAI_API_KEY': 'test-azure-key',
'AZURE_OPENAI_MODEL_DEPLOYMENT': 'test-model-deployment'
'AZURE_OPENAI_MODEL_DEPLOYMENT': 'test-model-deployment',
'SILICONFLOW_API_KEY': 'test-siliconflow-key'
})
self.env_patcher.start()

Expand Down Expand Up @@ -167,6 +176,17 @@ def test_create_deepseek_client(self, mock_openai):
)
self.assertEqual(client, self.mock_openai_client)

@unittest.skipIf(skip_llm_tests, skip_message)
@patch('tools.llm_api.OpenAI')
def test_create_siliconflow_client(self, mock_openai):
mock_openai.return_value = self.mock_openai_client
client = create_llm_client("siliconflow")
mock_openai.assert_called_once_with(
api_key='test-siliconflow-key',
base_url="https://api.siliconflow.cn/v1"
)
self.assertEqual(client, self.mock_openai_client)

@unittest.skipIf(skip_llm_tests, skip_message)
@patch('tools.llm_api.Anthropic')
def test_create_anthropic_client(self, mock_anthropic):
Expand Down Expand Up @@ -234,6 +254,19 @@ def test_query_deepseek(self, mock_create_client):
temperature=0.7
)

@unittest.skipIf(skip_llm_tests, skip_message)
@patch('tools.llm_api.create_llm_client')
def test_query_siliconflow(self, mock_create_client):
self.mock_openai_client.chat.completions.create.return_value = self.mock_siliconflow_response
mock_create_client.return_value = self.mock_openai_client
response = query_llm("Test prompt", provider="siliconflow")
self.assertEqual(response, "Test Siliconflow response")
self.mock_openai_client.chat.completions.create.assert_called_once_with(
model="deepseek-ai/DeepSeek-R1",
messages=[{"role": "user", "content": [{"type": "text", "text": "Test prompt"}]}],
temperature=0.7
)

@unittest.skipIf(skip_llm_tests, skip_message)
@patch('tools.llm_api.create_llm_client')
def test_query_anthropic(self, mock_create_client):
Expand Down
16 changes: 14 additions & 2 deletions tools/llm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def create_llm_client(provider="openai"):
api_key=api_key,
base_url="https://api.deepseek.com/v1",
)
elif provider == "siliconflow":
api_key = os.getenv('SILICONFLOW_API_KEY')
if not api_key:
raise ValueError("SILICONFLOW_API_KEY not found in environment variables")
return OpenAI(
api_key=api_key,
base_url="https://api.siliconflow.cn/v1"
)
elif provider == "anthropic":
api_key = os.getenv('ANTHROPIC_API_KEY')
if not api_key:
Expand Down Expand Up @@ -137,14 +145,16 @@ def query_llm(prompt: str, client=None, model=None, provider="openai", image_pat
model = os.getenv('AZURE_OPENAI_MODEL_DEPLOYMENT', 'gpt-4o-ms') # Get from env with fallback
elif provider == "deepseek":
model = "deepseek-chat"
elif provider == "siliconflow":
model = "deepseek-ai/DeepSeek-R1"
elif provider == "anthropic":
model = "claude-3-sonnet-20240229"
elif provider == "gemini":
model = "gemini-pro"
elif provider == "local":
model = "Qwen/Qwen2.5-32B-Instruct-AWQ"

if provider in ["openai", "local", "deepseek", "azure"]:
if provider in ["openai", "local", "deepseek", "azure", "siliconflow"]:
messages = [{"role": "user", "content": []}]

# Add text content
Expand Down Expand Up @@ -232,7 +242,7 @@ def query_llm(prompt: str, client=None, model=None, provider="openai", image_pat
def main():
parser = argparse.ArgumentParser(description='Query an LLM with a prompt')
parser.add_argument('--prompt', type=str, help='The prompt to send to the LLM', required=True)
parser.add_argument('--provider', choices=['openai','anthropic','gemini','local','deepseek','azure'], default='openai', help='The API provider to use')
parser.add_argument('--provider', choices=['openai','anthropic','gemini','local','deepseek','azure','siliconflow'], default='openai', help='The API provider to use')
parser.add_argument('--model', type=str, help='The model to use (default depends on provider)')
parser.add_argument('--image', type=str, help='Path to an image file to attach to the prompt')
args = parser.parse_args()
Expand All @@ -242,6 +252,8 @@ def main():
args.model = "gpt-4o"
elif args.provider == "deepseek":
args.model = "deepseek-chat"
elif args.provider == "siliconflow":
args.model = "deepseek-ai/DeepSeek-R1"
elif args.provider == 'anthropic':
args.model = "claude-3-5-sonnet-20241022"
elif args.provider == 'gemini':
Expand Down