weaviate-python-client icon indicating copy to clipboard operation
weaviate-python-client copied to clipboard

Creates the function to set external API key

Open ankur0904 opened this issue 1 year ago • 16 comments

Implemented the set_apikey function within the Client class to set the different external API keys.

def set_apikey(self, openai_key: Optional[str] = None, cohere_key: Optional[str] = None, huggingface_key: Optional[str] = None, palm_key: Optional[str] = None):
    ...

This PR can close the issue #382

cc @tsmith023 @databyjp

ankur0904 avatar Sep 16 '23 09:09 ankur0904

To avoid any confusion in the future about your contribution to Weaviate, we work with a Contributor License Agreement. If you agree, you can simply add a comment to this PR that you agree with the CLA so that we can merge.

beep boop - the Weaviate bot 👋🤖

PS:
Are you already a member of the Weaviate Slack channel?

weaviate-git-bot avatar Sep 17 '23 00:09 weaviate-git-bot

Note also the mypy errors in the CI pipeline. If you've not used mypy before then they're telling you that your function needs to have a return marked, i.e. -> None: and it cannot determine the type of _headers, which I think is solvable by adding self._headers: Dict[str, str] = {} here

tsmith023 avatar Sep 22 '23 15:09 tsmith023

Codecov Report

Patch coverage: 11.11% and project coverage change: -0.58% :warning:

Comparison is base (7528726) 94.75% compared to head (848c5d6) 94.18%. Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #527      +/-   ##
==========================================
- Coverage   94.75%   94.18%   -0.58%     
==========================================
  Files          70       70              
  Lines        8492     8501       +9     
==========================================
- Hits         8047     8007      -40     
- Misses        445      494      +49     
Files Changed Coverage Δ
weaviate/client.py 89.28% <11.11%> (-9.39%) :arrow_down:

... and 5 files with indirect coverage changes

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Sep 22 '23 15:09 codecov-commenter

@tsmith023 Thanks for the review. Soon making the desired changes.

ankur0904 avatar Sep 22 '23 15:09 ankur0904

@tsmith023 Hi Tommy I have made the following changes-

  • Add self._headers: Dict[str, str] = {} line in connect/connection.py file
  • Add -> None in the function signature

Thanks Ankur

ankur0904 avatar Sep 27 '23 04:09 ankur0904

I am unable to add the tests in the required file. I am afraid that it will take some time because I have my University exams.

ankur0904 avatar Sep 27 '23 04:09 ankur0904

@tsmith023 I have added unit tests.

ankur0904 avatar Sep 28 '23 05:09 ankur0904

I have my University exams

You should concentrate on these first! The PR can stay open until you have time to resume it, there's no pressure 😁

tsmith023 avatar Sep 28 '23 10:09 tsmith023

I have my University exams

You should concentrate on these first! The PR can stay open until you have time to resume it, there's no pressure 😁

Thanks for the patience, I am now available. If there are any changes required then please let me know.

ankur0904 avatar Oct 07 '23 17:10 ankur0904

@tsmith023 Any feedback regarding the PR.

ankur0904 avatar Oct 24 '23 18:10 ankur0904

Hi @ankur0904, sorry for not replying promptly! Because the CI pipeline is not fully green, it needs to be fixed before I will review it again. Could you look into the linting problems and unit test failures? Cheers 😁

tsmith023 avatar Oct 25 '23 10:10 tsmith023

@tsmith023 Ofcourse I will look into the failing CI pipeline.

ankur0904 avatar Oct 30 '23 05:10 ankur0904

Hey @tsmith023 any other changes required?

ankur0904 avatar Nov 06 '23 04:11 ankur0904

Hey @tsmith023 any other changes required?

Hi @ankur0904 - I can see that quite a few tests are failing here. Please take a look at the individual pipelines to see what is failing and try to address them.

As you can imagine, libraries like this one are our core products. And these automated tests are important parts of our quality assurance processes that help us ensure that any potential bugs or regressions do not get introduced into our products.

If you click on the Details tab in the PR CI checklist, you can see the logs and stack trace, which should help you to see what is failing.

I see that the pipeline indicates the linting process to be failing, and some unit tests.

You can run these tests locally before committing / testing the changes through the CI/CD pipeline. You might have read this already, but the contributor guide includes a section on linting, and tests. So, please review these sections, and run those tests locally, which should mean that they pass on GH.

I hope that helps!

databyjp avatar Nov 06 '23 10:11 databyjp

@databyjp Thanks for the guidance.

I checked each CI pipeline test, I found this test failure and confused how to fix it?

After running

$ pytest test --full-trace
self = <weaviate.client.Client object at 0x7f00886e77f0>, openai_key = 'your_key'
cohere_key = None, huggingface_key = None, palm_key = None

    def set_apikey(self, openai_key: Optional[str] = None, cohere_key: Optional[str] = None, huggingface_key: Optional[str] = None, palm_key: Optional[str] = None) -> None:
        """
        Set the external API key.
    
        Parameters
        ----------
        openai_key: str, optional
            It is OpenAI API key.
        cohere_key: str, optional
            It is Cohere API key.
        huggingface_key: str, optional
            It is Hugging Face API key.
        palm_key: str, optional
            It is Palm API key.
    
        Returns
        -------
        None
        """
        if openai_key:
>           self._connection._headers["x-openai-api-key"] = openai_key
E           TypeError: 'Mock' object does not support item assignment

cohere_key = None
huggingface_key = None
openai_key = 'your_key'
palm_key   = None
self       = <weaviate.client.Client object at 0x7f00886e77f0>

weaviate/client.py:320: TypeError

Related File: client.py

P.S. Meanwhile, I have fixed the linting issue.

Thanks Ankur

ankur0904 avatar Nov 07 '23 18:11 ankur0904

I checked each CI pipeline test, I found this test failure and confused how to fix it?

Hi there, I'm not a Weaviate contributor - just passing by. It appears the Connection class is patched out in line 13 of test_client.py. This patched version is just an empty Mock object, so it isn't aware that it has a dictionary-like _headers attribute. Try initializing the _headers attribute with client._connection._headers = {} prior to calling client.set_apikey() in the test.

Also I believe the assertions in the new test will fail because the Client class doesn't have the attribute additional_headers - it only passes the init parameter to Connection. Likewise, the Connection class doesn't store additional_headers, but parses into self._headers. So instead of asserting client.additional_headers == ..., I believe it should be client._connection._headers == ...

g-parki avatar Dec 07 '23 07:12 g-parki

superseeded by https://github.com/weaviate/weaviate-python-client/pull/981

dirkkul avatar May 06 '24 22:05 dirkkul