-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from phi-jkim/integrate-deepseek-r1-client
Add deepseek r1 client for integration
- Loading branch information
Showing
6 changed files
with
280 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 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
72 changes: 72 additions & 0 deletions
72
adalflow/adalflow/components/model_client/deepseek_client.py
This file contains 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,72 @@ | ||
import os | ||
import logging | ||
import backoff | ||
from typing import ( | ||
Dict, | ||
Sequence, | ||
Optional, | ||
List, | ||
Any, | ||
TypeVar, | ||
Callable, | ||
Literal, | ||
) | ||
|
||
from adalflow.utils.lazy_import import safe_import, OptionalPackages | ||
from adalflow.components.model_client.openai_client import OpenAIClient | ||
from openai.types import Completion | ||
|
||
openai = safe_import(OptionalPackages.OPENAI.value[0], OptionalPackages.OPENAI.value[1]) | ||
|
||
class DeepSeekClient(OpenAIClient): | ||
""" | ||
A component wrapper for the DeepSeek API client. | ||
DeepSeek's API is compatible with OpenAI's API, making it possible to use OpenAI SDKs | ||
or OpenAI-compatible software with DeepSeek by adjusting the API base URL. | ||
This client extends `OpenAIClient` but modifies the default `base_url` to use DeepSeek's API. | ||
Documentation: https://api-docs.deepseek.com/guides/reasoning_model | ||
Args: | ||
api_key (Optional[str], optional): DeepSeek API key. Defaults to `None`. | ||
chat_completion_parser (Callable[[Completion], Any], optional): A function to parse API responses. | ||
input_type (Literal["text", "messages"], optional): Defines how input is handled. Defaults to `"text"`. | ||
base_url (str, optional): API base URL, defaults to `"https://api.deepseek.com/v1/"`. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
api_key: Optional[str] = None, | ||
chat_completion_parser: Callable[[Completion], Any] = None, | ||
input_type: Literal["text", "messages"] = "messages", | ||
base_url: str = "https://api.deepseek.com/v1/", | ||
env_api_key_name: str = "DEEPSEEK_API_KEY" | ||
): | ||
"""Initializes DeepSeek API client with the correct base URL. The input_type is set to "messages" by default to be compatible with DeepSeek reasoner. | ||
""" | ||
super().__init__(api_key=api_key, chat_completion_parser=chat_completion_parser, input_type=input_type, base_url=base_url, env_api_key_name=env_api_key_name) | ||
|
||
# Example usage: | ||
if __name__ == "__main__": | ||
from adalflow.core import Generator | ||
from adalflow.utils import setup_env, get_logger | ||
|
||
log = get_logger(level="DEBUG") | ||
|
||
prompt_kwargs = {"input_str": "What is the meaning of life?"} | ||
|
||
setup_env() | ||
|
||
gen = Generator( | ||
model_client=DeepSeekClient(), | ||
model_kwargs={"model": "deepseek-reasoner", "stream": True}, | ||
) | ||
|
||
gen_response = gen(prompt_kwargs) | ||
print(f"gen_response: {gen_response}") | ||
|
||
for genout in gen_response.data: | ||
print(f"genout: {genout}") | ||
|
This file contains 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 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,41 @@ | ||
import unittest | ||
from unittest.mock import patch, Mock | ||
import os | ||
|
||
from openai import Stream | ||
from openai.types import CompletionUsage | ||
from openai.types.chat import ChatCompletion, ChatCompletionChunk | ||
from adalflow.core.types import ModelType, GeneratorOutput | ||
from adalflow.components.model_client.deepseek_client import DeepSeekClient | ||
from unittest.mock import AsyncMock | ||
|
||
def getenv_side_effect(key): | ||
env_vars = {"DEEPSEEK_API_KEY": "fake_api_key"} | ||
return env_vars.get(key, None) | ||
|
||
class TestDeepSeekClient(unittest.TestCase): | ||
def setUp(self): | ||
self.client = DeepSeekClient(api_key="fake_api_key") | ||
|
||
def test_deepseek_init(self): | ||
self.assertEqual(self.client.base_url, "https://api.deepseek.com/v1/") | ||
self.assertEqual(self.client._input_type, "messages") | ||
self.assertEqual(self.client._env_api_key_name, "DEEPSEEK_API_KEY") | ||
|
||
# mock os.getenv(self._env_api_key_name) with getenv_side_effect | ||
@patch("os.getenv") | ||
def test_deepseek_init_sync_client(self, mock_os_getenv): | ||
mock_os_getenv.return_value = "fake_api_key" | ||
self.client.init_sync_client() | ||
self.assertEqual(self.client.sync_client.api_key, "fake_api_key") | ||
self.assertEqual(self.client.sync_client.base_url, "https://api.deepseek.com/v1/") | ||
|
||
@patch("os.getenv") | ||
def test_deepseek_init_async_client(self, mock_os_getenv): | ||
mock_os_getenv.return_value = "fake_api_key" | ||
self.client.async_client = self.client.init_async_client() | ||
self.assertEqual(self.client.async_client.api_key, "fake_api_key") | ||
self.assertEqual(self.client.async_client.base_url, "https://api.deepseek.com/v1/") | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.