-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deepseek-r1 integration using openai client
- Loading branch information
Showing
21 changed files
with
2,932 additions
and
1,961 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
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
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}") | ||
|
Oops, something went wrong.