feat(openai): add support for all endpoints#5857
Merged
Merged
Conversation
68b959b to
93722ba
Compare
93722ba to
d3a19df
Compare
d3a19df to
296784d
Compare
ffbfd27 to
73b052f
Compare
mabdinur
previously approved these changes
Jun 9, 2023
Kyle-Verhoog
previously approved these changes
Jun 15, 2023
mabdinur
reviewed
Jun 15, 2023
mabdinur
approved these changes
Jun 15, 2023
Kyle-Verhoog
approved these changes
Jun 15, 2023
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tips for reviewers
note: 99% of the line changes in this PR are non-code, in the form of snapshot files, riot lockfiles, test data (audio/image files), and test cassette files used to mock request/responses with the OpenAI API, which store binary image/audio file data in
.yamlfiles that Github counts as lines of code.The major files that reviewers should focus on are:
ddtrace/contrib/openai/patch.py- outlines which endpoint hooks are added, minor changes to overall patching to minimize coupling with endpoint hook information.ddtrace/contrib/openai/_endpoint_hooks.py- implementation of each endpoint hook, as well as minor refactor to minimize code duplication between endpoint hookstests/contrib/openai/test_openai.py. - shows how each endpoint is expected to be used.Summary
This PR adds tracing support for the remaining endpoints not implemented by #5488. This includes:
Changes in this PR include:
openai, directly added as a request param)openai(e.g.organization,api_type)openai.request.model,openai.request.endpoint, addedopenai.request.method)OpenAI Integration Design
Overall Patch Implementation
note: the overall patch implementation has not changed; this is simply a refresher on how the overall integration works.
The openai integration features patching via generators - each of the openAI API's endpoint methods (ex:
openai.Completions.create()) is wrapped by_patched_endpoint(endpoint_hook, ...), which itself takes a corresponding endpoint hook as an argument. A rough description of what happens when_patched_endpoint()is called is as follows:_patched_endpoint(endpoint_hook, ...)starts a trace and starts a generator_traced_endpoint(endpoint_hook, ...)_traced_endpoint(endpoint_hook, ...)startsendpoint_hook().handle_request(...), which performs endpoint-specific tracing logic, and yields back to_patched_endpoint()once the request has been processed by the endpoint hook._patched_endpoint()runs the underlying openai API method, then yields back the response/error back to_traced_endpoint(), which in turn yields the response/error back to theendpoint_hook.endpoint_hook()performs the endpoint-specific tracing logic for the response/error, then finishes the trace.Endpoint Hook Design
The internal implementation design of the endpoint hooks have been modified (building off of the refactor from #5865) to minimize code duplication. This means that each endpoint hook now stores the following:
_request_arg_paramsand_request_kwarg_params, which are tuples containing the arg/kwarg signature of the underlying openAI API endpoint method. These tuples are used in_EndpointHook._record_request(...)to add request arg/kwarg parameters as span tags/metrics.ENDPOINT_NAME,REQUEST_TYPE,OPERATION_IDconstants, which reflect the base endpoint name (e.g./completions), http request type (e.g.POST), and operation ID as specified by the openAI API specifcations (e.g.createCompletion). The operationID is used as the span resource name, and the endpoint/request_type values are added as span tags._record_request(...)and_record_response(...)to add any endpoint-specific span tagging logic.Testing
The testing for this PR involves snapshot testing for each endpoint, as well as
vcrpycassettes used to mock request/responses to OpenAI.Checklist
Reviewer Checklist