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

Custom http headers #93

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

benvolioT
Copy link

What

This pull request adds the ability to specify custom HTTP headers when integrating with OpenAI via proxy APIs such as Helicone. It also refactors OpenAi's performRequest() by encapsulating the logic for handling the session data task in its own class, URLSessionDataTaskManager. This should simplify the OpenAI class and make the code easier to manage. This pull request simplifies JSONRequest and MultipartFormDataRequest by abstracting out common code to remove repetition that previously existed in these structs. Finally, it simplifies the protocol URLRequestBuildable, moving configuration-driven parameter setting to the constructors of these struct instances, rather than in the call to that protocol's build() function.

Why

The motivation for this change was to allow users to integrate with OpenAI via the Helicone proxy. To do so, they not only need to point the OpenAI client at the Helicone host, but they also have to set an additional HTTP header on the requests. This pull request makes that possible.

Affected Areas

The affected areas of the library include:

  • The OpenAI class within the OpenAI.swift file.
  • The client configuration within the constructor of the OpenAI class.
  • The construction of requests in the following methods: completions(), completionsStream(), images(), embeddings(), chats(), chatsStream(), edits(), model(), models(), moderations(), audioTranscriptions(), and audioTranslations().

…e. Refactor URLRequestBuildable and its implementations to separate configuration concerns from URL building concerns.
… that the code better adheres to the Single Responsibility Principle.
@sonarcloud
Copy link

sonarcloud bot commented Aug 7, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@benvolioT
Copy link
Author

Hi! Is there anything more I need to do in order to get this merged?

@Krivoblotsky
Copy link
Collaborator

Hey, @benvolioT!
Thanks a lot for the contribution!
Please fix build and merge conflicts 🙏

// on the next pass in case that new data has completed the string to create valid (parsable) JSON, and so on.
// Note that while OpenAI's API doesn't appear to return partial string fragments, proxy service such as Helicone
// do seem to do so, making this logic necessary.
accumulatedData.append(stringContent)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you extract this change to a separate PR, please?
Looks like this is a separate fix, that doesn't apply to custom headers.

}

extension OpenAI {
internal func generateHeaders() -> [String: String] {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would change the naming to defaultHeaders to add the knowledge that it is going to be appended further.
What do you think?

Copy link
Collaborator

@Krivoblotsky Krivoblotsky left a comment

Choose a reason for hiding this comment

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

Overall looks good to me, thanks for the contribution!

  1. Please fix the build.
  2. Fix merge conflicts
  3. Extract StreamingSession fix into a separate PR.
    Thanks!

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.

None yet

2 participants