Skip to content

fix: OpenAI Vector Model Using Openai Supplier #2781

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 1 commit into from
Apr 2, 2025

Conversation

shaohuzhang1
Copy link
Contributor

fix: OpenAI Vector Model Using Openai Supplier

Copy link

f2c-ci-robot bot commented Apr 2, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Apr 2, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

self, texts: List[str], chunk_size: int | None = None
) -> List[List[float]]:
res = self.client.create(input=texts, model=self.model_name, encoding_format="float")
return [e.embedding for e in res.data]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has a few issues, main ones being:

  1. The openai module does not directly call any class method, but rather uses its attributes to perform operations (create).

  2. Using dynamic method calls (like in .embed_documents) can be less readable and maintainable.

  3. It lacks validation checks, particularly for required parameters such as apiKey, model_name, etc.

  4. There is no exception handling.

To improve the code, you could do these changes:

@@ -6,18 +6,34 @@
     @date2024/7/12 17:44
     @desc:
 """
-from typing import Dict
+from typing import Dict, List

-import openai
+from setting.models_provider.base_model_provider import MaxKBBaseModel


-class OpenAIEmbeddingModel(MaxKBBaseModel, openai.Embeddings):
+class OpenAIEmbeddingModel(MaxKBBaseModel):
     _client: openai.Completion
     api_key: str
     base_url: str
     model_name: str

+    def __init__(self, api_key, base_url, model_name: str = "text-davinci-003"):
         if not isinstance(api_key, str) or not api_key:
             raise ValueError("API key cannot be empty")
         if not isinstance(base_url, str) or not base_url:
             raise ValueError("Base URL cannot be empty")

         self.api_key = api_key
         self.base_url = base_url
-        super().__init__()
+        self._client = openai.OpenAI(api_key=api_key, base_url=base_url)

+        # set default model name if one wasn't provided
+        if not self.model_name:
+            self.model_name = "text-davinci-003"

     @staticmethod
     def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
         return OpenAIEmbeddingModel(
@@ -34,7 +49,7 @@
         return result.choices[0].message.content.split("\n\n")[:-1]

     def embed_query(self, text: str) -> List[float]:
-        response = self._client.create(prompt=text)
+        response = self.client.completions.create(prompt=text)
         token_ids = [token["id"] for token in response.choices[0]["tokens"]]
         embedding_values = [token["logprobs"]["probs"][i] for i in range(0, len(token_ids), 512)]
@@ -43,20 +58,24 @@

This revised version improves readability and error-handling by using explicit initialization and proper parameter checking.

@shaohuzhang1 shaohuzhang1 merged commit 15feca8 into main Apr 2, 2025
4 of 5 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_openai_embedding branch April 2, 2025 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant