Skip to content

Client abstraction over API resources (prototype) #2

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

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft

Conversation

johanste
Copy link
Owner

@johanste johanste commented Jun 9, 2023

Experiment showing what a client abstraction over the Stripe/ApiResource model could look like.

@johanste johanste requested a review from kristapratico June 12, 2023 21:03
elif isinstance(auth, str):
self.auth = ApiKeyAuth(auth or openai.api_key)
else:
self.auth = auth
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wonder if we should just do validation here if it's not one of the supported auth types. Otherwise we're going to get an AttributeError when we do the self.auth.get_token() call on L125.

Copy link
Owner Author

Choose a reason for hiding this comment

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

We could. I think we are side-stepping the biggest issue (passing in a str for api key based auth), but I wouldn't mind adding more validation here.

I was planning to make a better diagnostics story (adding the __repr__ methods started it) that would make it easier to see how your client was configured - as well as how to deal with OAI or AOAI specific endpoints.

openai/client.py Outdated
raise ValueError(
f'Unknown `backend` {backend} - expected one of "azure" or "openai"'
)
if not api_base and backend == 'azure':
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if I want to pass my azure api_base via environment variable?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yeah, that should work. Will fix.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ah, I now remember - the problem is that the default is openai, and for azure you always need to have overridden the value. I'll put a check to make sure the api_base isn't "https://api.openai.com/v1". I'll have to go back and re-untangle this mess I think.

openai/client.py Outdated
await openai.Completion.acreate(**kwargs),
)

def chatcompletion(self, messages, **kwargs) -> openai.ChatCompletion:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: since we snake_case the image API names, this kind of feels like it should follow that as well

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yeah, I do think we should review the names across the board. I used the convention of iter_ for server sent event streaming APIs and _<variation name> for "child"/custom actions beyond the base generation API. But that was just the first thing that popped into my head.

johanste and others added 5 commits June 15, 2023 14:45
Add missing `azuread` api_type support (for environment variables)

Co-authored-by: Krista Pratico <[email protected]>
Co-authored-by: Krista Pratico <[email protected]>

self.api_base = api_base or openai.api_base
self.organization = organization or openai.organization
if self.backend == 'azure' and self.api_base == "https://api.openai.com/v1":
Copy link
Collaborator

Choose a reason for hiding this comment

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

what do you think about only comparing the api_base string up until the v or excluding the version altogether? e.g. self.api_base.startswith("https://api.openai.com/v")? I'm not sure how often the version is bumped, but it could possibly save us a re-release...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants