Skip to content
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

Support Azure OpenAI #522

Merged
merged 1 commit into from
Jan 11, 2024
Merged

Support Azure OpenAI #522

merged 1 commit into from
Jan 11, 2024

Conversation

joeddav
Copy link
Contributor

@joeddav joeddav commented Jan 10, 2024

Adds a wrapper around the OpenAI class, creating an AsyncAzureOpenAI client instead of an AsyncOpenAI client.

Also required one change to openai.py: I put the tiktoken logic inside a computed tokenizer() property so that I could override this in the subclass. This is necessary because Azure OpenAI has a distinction between a deployment and a model, the former of which is used when making calls to the API, but the latter is what must be passed to tiktoken.

Usage:

model = outlines.models.azure_openai(
    model_name="gpt-4",
    deployment_name="gpt4-1106",   # my custom deployment name
    azure_endpoint="https://<myendpoint>.openai.azure.com/",
    api_key="<mykey>"
)
model.generate_choice(
    "What is the capital of the country where DaVinci is from?",
    choices=["Milan", "Rome", "Paris", "Italy"]
)
# 'Rome'

vs.

model = outlines.models.openai("gpt-4")
model.generate_choice(
    "What is the capital of the country where DaVinci is from?",
    choices=["Milan", "Rome", "Paris", "Italy"]
)
# 'Rome'

I coded this up for my own local use and but thought I'd share here in case you'd like to incorporate it, or in case someone else wanted to use it.

Resolves #271

@rlouf
Copy link
Member

rlouf commented Jan 11, 2024

This is a great addition, thank you so much for your contribution!

@rlouf rlouf merged commit 39441ea into outlines-dev:main Jan 11, 2024
5 checks passed
@joeddav joeddav deleted the azure-openai branch January 15, 2024 17:45
"The `openai` library needs to be installed in order to use Outlines' Azure OpenAI integration."
)
try:
client = openai.OpenAI()

Choose a reason for hiding this comment

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

Thanks for adding Azure support! When I try to use azure_openai, it fails at this line of code, since the OpenAI client attempts to authenticate. Is there a different way we can validate the model name, besides instantiating the client?

Choose a reason for hiding this comment

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

Just in case it's useful, here's a mapping that we maintain for an Azure repo of Azure model names to OpenAI names: https://github.com/Azure-Samples/azure-search-openai-demo/blob/3424475e6a61388c114f4a4db1de246734c37728/app/backend/core/modelhelper.py#L20

Copy link
Member

@rlouf rlouf Feb 24, 2024

Choose a reason for hiding this comment

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

I am now leaning towards "letting it crash" i.e. let users enter anything for the model name and then handle a potential error gracefully, letting them know that the model name is incorrect.

Copy link
Member

Choose a reason for hiding this comment

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

I don’t have the time to fix it right now, but happy to review a PR.

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.

Add support for Azure OpenAI API and Example in the documentation
3 participants