-
Notifications
You must be signed in to change notification settings - Fork 507
Feat: More Llm API #113
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
Feat: More Llm API #113
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b0a511b
fix: language bug
CaralHsi d126f5d
feat: add qwen api
CaralHsi faa4c52
feat: modify qwen llm
CaralHsi 3c4fcd4
feat: add deepseek llm
CaralHsi 18f3bfc
feat: add stream output for openai
CaralHsi e268b82
test: add unit test for llms
CaralHsi 95fcd48
Merge branch 'dev' into feat/more-llm-api
CaralHsi 6b8583d
Update src/memos/llms/deepseek.py
CaralHsi 195ca64
Apply suggestion from @Copilot
CaralHsi bc0f25e
Apply suggestion from @Copilot
CaralHsi f621f80
fix: multi llm test bug
CaralHsi 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
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,56 @@ | ||
| from memos.configs.llm import DeepSeekLLMConfig | ||
| from memos.llms.openai import OpenAILLM | ||
| from memos.llms.utils import remove_thinking_tags | ||
| from memos.log import get_logger | ||
| from memos.types import MessageList | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class DeepSeekLLM(OpenAILLM): | ||
| """DeepSeek LLM via OpenAI-compatible API.""" | ||
|
|
||
| def __init__(self, config: DeepSeekLLMConfig): | ||
| super().__init__(config) | ||
|
|
||
| def generate(self, messages: MessageList) -> str: | ||
| """Generate a response from DeepSeek.""" | ||
| response = self.client.chat.completions.create( | ||
| model=self.config.model_name_or_path, | ||
| messages=messages, | ||
| temperature=self.config.temperature, | ||
| max_tokens=self.config.max_tokens, | ||
| top_p=self.config.top_p, | ||
| extra_body=self.config.extra_body, | ||
| ) | ||
| logger.info(f"Response from DeepSeek: {response.model_dump_json()}") | ||
| response_content = response.choices[0].message.content | ||
| if self.config.remove_think_prefix: | ||
| return remove_thinking_tags(response_content) | ||
| else: | ||
| return response_content | ||
|
|
||
| def generate_stream(self, messages: MessageList, **kwargs): | ||
| """Stream response from DeepSeek.""" | ||
| response = self.client.chat.completions.create( | ||
| model=self.config.model_name_or_path, | ||
| messages=messages, | ||
| stream=True, | ||
| temperature=self.config.temperature, | ||
| max_tokens=self.config.max_tokens, | ||
| top_p=self.config.top_p, | ||
| extra_body=self.config.extra_body, | ||
| ) | ||
| # Streaming chunks of text | ||
| reasoning_parts = "" | ||
| answer_parts = "" | ||
| for chunk in response: | ||
| delta = chunk.choices[0].delta | ||
| if hasattr(delta, "reasoning_content") and delta.reasoning_content: | ||
| reasoning_parts += delta.reasoning_content | ||
CaralHsi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| yield delta.reasoning_content | ||
|
|
||
| if hasattr(delta, "content") and delta.content: | ||
| answer_parts += delta.content | ||
Ki-Seki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Ki-Seki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
CaralHsi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| yield delta.content | ||
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
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,63 @@ | ||
| from collections.abc import Generator | ||
|
|
||
| from memos.configs.llm import QwenLLMConfig | ||
| from memos.llms.openai import OpenAILLM | ||
| from memos.llms.utils import remove_thinking_tags | ||
| from memos.log import get_logger | ||
| from memos.types import MessageList | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class QwenLLM(OpenAILLM): | ||
| """Qwen (DashScope) LLM class via OpenAI-compatible API.""" | ||
|
|
||
| def __init__(self, config: QwenLLMConfig): | ||
| super().__init__(config) | ||
|
|
||
| def generate(self, messages: MessageList) -> str: | ||
| """Generate a response from Qwen LLM.""" | ||
| response = self.client.chat.completions.create( | ||
| model=self.config.model_name_or_path, | ||
| messages=messages, | ||
| extra_body=self.config.extra_body, | ||
| temperature=self.config.temperature, | ||
| max_tokens=self.config.max_tokens, | ||
| top_p=self.config.top_p, | ||
| ) | ||
| logger.info(f"Response from Qwen: {response.model_dump_json()}") | ||
| response_content = response.choices[0].message.content | ||
| if self.config.remove_think_prefix: | ||
| return remove_thinking_tags(response_content) | ||
| else: | ||
| return response_content | ||
|
|
||
| def generate_stream(self, messages: MessageList, **kwargs) -> Generator[str, None, None]: | ||
| """Stream response from Qwen LLM.""" | ||
| response = self.client.chat.completions.create( | ||
| model=self.config.model_name_or_path, | ||
| messages=messages, | ||
| stream=True, | ||
| temperature=self.config.temperature, | ||
| max_tokens=self.config.max_tokens, | ||
| top_p=self.config.top_p, | ||
| extra_body=self.config.extra_body, | ||
| ) | ||
|
|
||
| reasoning_started = False | ||
| for chunk in response: | ||
| delta = chunk.choices[0].delta | ||
|
|
||
| # Some models may have separate `reasoning_content` vs `content` | ||
| # For Qwen (DashScope), likely only `content` is used | ||
| if hasattr(delta, "reasoning_content") and delta.reasoning_content: | ||
| if not reasoning_started and not self.config.remove_think_prefix: | ||
| yield "<think>" | ||
| reasoning_started = True | ||
| yield delta.reasoning_content | ||
| elif hasattr(delta, "content") and delta.content: | ||
| if reasoning_started and not self.config.remove_think_prefix: | ||
| yield "</think>" | ||
| reasoning_started = False | ||
| yield delta.content |
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
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.