-
Notifications
You must be signed in to change notification settings - Fork 690
feat: add new llm and embedding provider JiekouAI #257
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,131 @@ | ||
| import os | ||
| from typing import List, Union | ||
|
|
||
| import requests | ||
|
|
||
| from deepsearcher.embedding.base import BaseEmbedding | ||
|
|
||
| # TODO: Update with actual Jiekou.AI model dimensions when available | ||
| JIEKOUAI_MODEL_DIM_MAP = { | ||
| "qwen/qwen3-embedding-0.6b": 1024, | ||
| "qwen/qwen3-embedding-8b": 1024, | ||
| "baai/bge-m3": 1024, | ||
| } | ||
|
|
||
| JIEKOUAI_EMBEDDING_API = "https://api.jiekou.ai/openai/v1/embeddings" | ||
|
|
||
|
|
||
| class JiekouAIEmbedding(BaseEmbedding): | ||
| """ | ||
| Jiekou.AI embedding model implementation. | ||
|
|
||
| This class provides an interface to the Jiekou.AI embedding API, which offers | ||
| various embedding models for text processing. | ||
| """ | ||
|
|
||
| def __init__(self, model="qwen/qwen3-embedding-8b", batch_size=32, **kwargs): | ||
| """ | ||
| Initialize the Jiekou.AI embedding model. | ||
|
|
||
| Args: | ||
| model (str): The model identifier to use for embeddings. Default is "baai/bge-m3". | ||
| batch_size (int): Maximum number of texts to process in a single batch. Default is 32. | ||
| **kwargs: Additional keyword arguments. | ||
| - api_key (str, optional): The Jiekou.AI API key. If not provided, | ||
| it will be read from the JIEKOU_API_KEY environment variable. | ||
| - model_name (str, optional): Alternative way to specify the model. | ||
|
|
||
| Raises: | ||
| RuntimeError: If no API key is provided or found in environment variables. | ||
| """ | ||
| if "model_name" in kwargs and (not model or model == "qwen/qwen3-embedding-8b"): | ||
| model = kwargs.pop("model_name") | ||
| self.model = model | ||
|
|
||
| if "api_key" in kwargs: | ||
| api_key = kwargs.pop("api_key") | ||
| else: | ||
| api_key = os.getenv("JIEKOU_API_KEY") | ||
|
|
||
| if not api_key or len(api_key) == 0: | ||
| raise RuntimeError("api_key is required for JiekouAIEmbedding") | ||
| self.api_key = api_key | ||
| self.batch_size = batch_size | ||
|
|
||
| def embed_query(self, text: str) -> List[float]: | ||
| """ | ||
| Embed a single query text. | ||
|
|
||
| Args: | ||
| text (str): The query text to embed. | ||
|
|
||
| Returns: | ||
| List[float]: A list of floats representing the embedding vector. | ||
| """ | ||
| return self._embed_input(text)[0] | ||
|
|
||
| def embed_documents(self, texts: List[str]) -> List[List[float]]: | ||
| """ | ||
| Embed a list of document texts. | ||
|
|
||
| This method handles batching of document embeddings based on the configured | ||
| batch size to optimize API calls. | ||
|
|
||
| Args: | ||
| texts (List[str]): A list of document texts to embed. | ||
|
|
||
| Returns: | ||
| List[List[float]]: A list of embedding vectors, one for each input text. | ||
| """ | ||
| # batch embedding | ||
| if self.batch_size > 0: | ||
| if len(texts) > self.batch_size: | ||
| batch_texts = [ | ||
| texts[i : i + self.batch_size] for i in range(0, len(texts), self.batch_size) | ||
| ] | ||
| embeddings = [] | ||
| for batch_text in batch_texts: | ||
| batch_embeddings = self._embed_input(batch_text) | ||
| embeddings.extend(batch_embeddings) | ||
| return embeddings | ||
| return self._embed_input(texts) | ||
| return [self.embed_query(text) for text in texts] | ||
|
|
||
| def _embed_input(self, input: Union[str, List[str]]) -> List[List[float]]: | ||
| """ | ||
| Internal method to handle the API call for embedding inputs. | ||
|
|
||
| Args: | ||
| input (Union[str, List[str]]): Either a single text string or a list of text strings to embed. | ||
|
|
||
| Returns: | ||
| List[List[float]]: A list of embedding vectors for the input(s). | ||
|
|
||
| Raises: | ||
| HTTPError: If the API request fails. | ||
| """ | ||
| headers = { | ||
| "Authorization": f"Bearer {self.api_key}", | ||
| "Content-Type": "application/json", | ||
| } | ||
|
|
||
| # Handle both single string and list of strings | ||
| input_list = input if isinstance(input, list) else [input] | ||
|
|
||
| payload = {"model": self.model, "input": input_list} | ||
|
|
||
| response = requests.request("POST", JIEKOUAI_EMBEDDING_API, json=payload, headers=headers) | ||
| response.raise_for_status() | ||
| result = response.json()["data"] | ||
| sorted_results = sorted(result, key=lambda x: x["index"]) | ||
| return [res["embedding"] for res in sorted_results] | ||
|
|
||
| @property | ||
| def dimension(self) -> int: | ||
| """ | ||
| Get the dimensionality of the embeddings for the current model. | ||
|
|
||
| Returns: | ||
| int: The number of dimensions in the embedding vectors. | ||
| """ | ||
| return JIEKOUAI_MODEL_DIM_MAP[self.model] |
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,61 @@ | ||
| import os | ||
| from typing import Dict, List | ||
|
|
||
| from deepsearcher.llm.base import BaseLLM, ChatResponse | ||
|
|
||
|
|
||
| class JiekouAI(BaseLLM): | ||
| """ | ||
| Jiekou.AI language model implementation. | ||
|
|
||
| This class provides an interface to interact with language models | ||
| hosted on the Jiekou.AI platform. | ||
|
|
||
| Attributes: | ||
| model (str): The model identifier to use on Jiekou.AI platform. | ||
| client: The OpenAI-compatible client instance for Jiekou.AI API. | ||
| """ | ||
|
|
||
| def __init__(self, model: str = "claude-sonnet-4-5-20250929", **kwargs): | ||
| """ | ||
| Initialize a Jiekou.AI language model client. | ||
|
|
||
| Args: | ||
| model (str, optional): The model identifier to use. Defaults to "claude-sonnet-4-5-20250929". | ||
| **kwargs: Additional keyword arguments to pass to the OpenAI client. | ||
| - api_key: Jiekou.AI API key. If not provided, uses JIEKOU_API_KEY environment variable. | ||
| - base_url: Jiekou.AI API base URL. If not provided, defaults to "https://api.jiekou.ai/openai/v1". | ||
| """ | ||
| from openai import OpenAI as OpenAI_ | ||
|
|
||
| self.model = model | ||
| if "api_key" in kwargs: | ||
| api_key = kwargs.pop("api_key") | ||
| else: | ||
| api_key = os.getenv("JIEKOU_API_KEY") | ||
| if "base_url" in kwargs: | ||
| base_url = kwargs.pop("base_url") | ||
| else: | ||
| base_url = "https://api.jiekou.ai/openai/v1" | ||
| self.client = OpenAI_(api_key=api_key, base_url=base_url, **kwargs) | ||
|
|
||
| def chat(self, messages: List[Dict]) -> ChatResponse: | ||
| """ | ||
| Send a chat message to the Jiekou.AI model and get a response. | ||
|
|
||
| Args: | ||
| messages (List[Dict]): A list of message dictionaries, typically in the format | ||
| [{"role": "system", "content": "..."}, | ||
| {"role": "user", "content": "..."}] | ||
|
|
||
| Returns: | ||
| ChatResponse: An object containing the model's response and token usage information. | ||
| """ | ||
| completion = self.client.chat.completions.create( | ||
| model=self.model, | ||
| messages=messages, | ||
| ) | ||
| return ChatResponse( | ||
| content=completion.choices[0].message.content, | ||
| total_tokens=completion.usage.total_tokens, | ||
| ) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is not necessary, please keep this example file unchanged