Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Cody: Update Azure OpenAI client and force api-version to a stable one#61005

Merged
chwarwick merged 7 commits into
mainfrom
cw/update-azure-openai
Mar 12, 2024
Merged

Cody: Update Azure OpenAI client and force api-version to a stable one#61005
chwarwick merged 7 commits into
mainfrom
cw/update-azure-openai

Conversation

@chwarwick

Copy link
Copy Markdown
Contributor

This updates the Azure OpenAI client and also overrides the round tripper to force it to use the stable 2023-05-15 api version instead of the default which uses preview versions that keep being retired.

closes https://github.com/sourcegraph/sourcegraph/issues/60934

Test plan

Verify chat works and uses correct api version:

[       frontend] [Mar 11 17:37:11.359346] Response: ==> REQUEST/RESPONSE (Try=1/1.327581208s, OpTime=1.327591292s) -- RESPONSE RECEIVED
[       frontend]    POST https://{DEV_ENVIRONMENT}/openai/deployments/gpt-4-test/chat/completions?api-version=2023-05-15

Verify autocomplete works and uses correct api version

[       frontend] [Mar 11 17:38:03.682053] Response: ==> REQUEST/RESPONSE (Try=1/504.029208ms, OpTime=504.055333ms) -- RESPONSE RECEIVED
[       frontend]    POST https://sourcegraph-test-oai.openai.azure.com/openai/deployments/gpt-35-turbo-instruct-test/completions?api-version=2023-05-15

@cla-bot cla-bot Bot added the cla-signed label Mar 11, 2024
@chwarwick chwarwick requested review from a team and eseliger March 11, 2024 17:57
@chwarwick chwarwick force-pushed the cw/update-azure-openai branch from ad1d72e to 24044a1 Compare March 11, 2024 17:59
Comment on lines 60 to 65

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

in case we update the client again, could it be that their response parser breaks if it doesn't support that version anymore?

Not a blocker, this is still a good change, just a little scared.
Maybe we could use our HTTP recorder tests here to ensure that a newer version of the library is still able to parse the response alright?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see this ClientOptions struct has a field called APIVersion - I assume this is not useful for us?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see this ClientOptions struct has a field called APIVersion - I assume this is not useful for us?

Correct assumption: Request failed: this client doesn't support overriding its API version

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, unfortunate :( if the method looked like this, it would be configurable by us :(

// NewClientWithKeyCredential creates a new instance of Client that connects to an Azure OpenAI endpoint.
//   - endpoint - Azure OpenAI service endpoint, for example: https://{your-resource-name}.openai.azure.com
//   - credential - used to authorize requests with an API Key credential
//   - options - client options, pass nil to accept the default values.
func NewClientWithKeyCredential(endpoint string, credential *azcore.KeyCredential, options *ClientOptions) (*Client, error) {
	if options == nil {
		options = &ClientOptions{
// NEW:
			ClientOptions: azcore.ClientOptions{
				APIVersion: "2023-09-23",
			},
		}
	}

	authPolicy := runtime.NewKeyCredentialPolicy(credential, "api-key", nil)
	azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{
// NEW:
		APIVersion: runtime.APIVersionOptions{Location: runtime.APIVersionLocationQueryParam, Name: "api-version"},
		PerRetry:   []policy.Policy{authPolicy},
	}, &options.ClientOptions)
	if err != nil {
		return nil, err
	}

	return &Client{
		internal: azcoreClient,
		clientData: clientData{
			endpoint: endpoint,
			azure:    true,
		},
	}, nil
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

in case we update the client again, could it be that their response parser breaks if it doesn't support that version anymore?

Totally this is far from ideal but better then reimplementing service principals, workload identity and the others. I'll add a backlog item to add a recorder test, it's a bit of a pain to get the format of the events.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

works! thanks!

Comment thread internal/completions/client/azureopenai/openai.go Outdated
@chwarwick chwarwick force-pushed the cw/update-azure-openai branch from 0208c82 to e98bbc5 Compare March 12, 2024 14:16
@chwarwick chwarwick requested review from emidoots and eseliger March 12, 2024 14:26
Comment on lines +428 to +431
Transport: &apiVersionRoundTripper{
rt: azureClientDefaultTransport,
apiVersion: apiVersion,
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

one last thing, since this doesn't wrap our httpcli transport, these requests won't be counted accordingly and cannot appear in the outgoing request logs. Not a blocker for now, but would be nice to wrap that in here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not adding for now since it's not a regression i'll add to the follow up issue along with parsing tests.

@chwarwick

Copy link
Copy Markdown
Contributor Author

Follow up issues to improve solution https://github.com/sourcegraph/sourcegraph/issues/61031

@chwarwick chwarwick merged commit aa36a51 into main Mar 12, 2024
@chwarwick chwarwick deleted the cw/update-azure-openai branch March 12, 2024 14:40
@sourcegraph-release-bot

Copy link
Copy Markdown
Collaborator

The backport to 5.3 failed at https://github.com/sourcegraph/sourcegraph/actions/runs/8250779318:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-5.3 5.3
# Navigate to the new working tree
cd .worktrees/backport-5.3
# Create a new branch
git switch --create backport-61005-to-5.3
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 aa36a51eb76b0fa039ee3a0dda01743c726ac953
# Push it to GitHub
git push --set-upstream origin backport-61005-to-5.3
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-5.3

If you encouter conflict, first resolve the conflict and stage all files, then run the commands below:

git cherry-pick --continue
# Push it to GitHub
git push --set-upstream origin backport-61005-to-5.3
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-5.3
  • Follow above instructions to backport the commit.
  • Create a pull request where the base branch is 5.3 and the compare/head branch is backport-61005-to-5.3., click here to create the pull request.
  • Make sure to tag @sourcegraph/release in the pull request description.
  • Once the backport pull request is created, kindly remove the release-blocker from this pull request.

@sourcegraph-release-bot sourcegraph-release-bot added backports failed-backport-to-5.3 release-blocker Prevents us from releasing: https://about.sourcegraph.com/handbook/engineering/releases labels Mar 12, 2024
chwarwick pushed a commit that referenced this pull request Mar 12, 2024
chwarwick pushed a commit that referenced this pull request Mar 14, 2024
… to a stable one (#61032)

Cody: Update Azure OpenAI client and force api-version to a stable one (#61005)

(cherry picked from commit aa36a51)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

backport 5.3 backports cla-signed failed-backport-to-5.3 release-blocker Prevents us from releasing: https://about.sourcegraph.com/handbook/engineering/releases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cody: Azure OpenAI API version updates

3 participants