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

Add embeddings #11

Merged
merged 10 commits into from
Nov 22, 2022
Merged

Add embeddings #11

merged 10 commits into from
Nov 22, 2022

Conversation

dabdine
Copy link
Contributor

@dabdine dabdine commented Nov 17, 2022

Adds embeddings. Tested it locally with a client I have to ensure it works.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

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

✅ This pull request was sent to the PullRequest network.


@dabdine you can click here to see the review status or cancel the code review job.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

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

PullRequest Breakdown

Reviewable lines of change

+ 111
- 0

79% Go
21% Go (tests)

Type of change

Feature - These changes are adding a new feature or improvement to existing code.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

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

This update looks good and complete. Have you considered writing any unit tests? It's good that you're using interfaces for dependencies to make it easier to mock things out for tests.

Image of Andy W Andy W


Was this helpful? Yes | No

Reviewers will be notified any time you reply to their comments or commit new changes.
You can also request a full follow-up review from your PullRequest dashboard.

gpt3.go Outdated
return nil, err
}

output := new(EmbeddingResponse)
Copy link

Choose a reason for hiding this comment

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

I usually just do output := EmbeddingRequest{} and then pass &output to the encoder but this works too

🔹 Giving Information (Nice to have)

Image of Andy W Andy W

Copy link
Contributor Author

@dabdine dabdine Nov 18, 2022

Choose a reason for hiding this comment

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

Yeah that's fair. When I write for public repos I try to copy the methodology used elsewhere in the same repository (this was almost identical to another method--though that also can just be refactored into a shared request method) so it follows a predictable tenor. I don't mind adjusting this though. It's typically how I would write it as well.

Copy link

Choose a reason for hiding this comment

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

Yeah, I mean, both ways initialize things to their zero values but EmbeddingRequest{} makes it easier to later substitute in values to override those zero values.

Image of Andy W Andy W

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be all set for both tests and reference passing.

Note: I opted to add tests using the existing methodology already in the repository. However, these tests are mostly duplicative, as they're testing the same underlying code over and over. My thought is that in a separate PR, someone can refactor the identical code blocks issuing the HTTP request for the majority of the calls, then test those methods, which would eliminate the per-API tests that don't test anything other than that common business logic.

Copy link

Choose a reason for hiding this comment

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

@tylermann We may want to consider adding this to the issues for this repo so it can be addressed in the future if its a real concern.

@dabdine
Copy link
Contributor Author

dabdine commented Nov 18, 2022

This update looks good and complete. Have you considered writing any unit tests? It's good that you're using interfaces for dependencies to make it easier to mock things out for tests.

Image of Andy W Andy W

Was this helpful? Yes | No

Reviewers will be notified any time you reply to their comments or commit new changes.
You can also request a full follow-up review from your PullRequest dashboard.

Yep, I'll add some tests.

func() (interface{}, error) {
return client.Embeddings(ctx, gpt3.EmbeddingsRequest{})
},
"Post \"https://api.openai.com/v1/embeddings\": request error",
Copy link

Choose a reason for hiding this comment

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

So does this test actually hit this URL? I typically try to isolate "unit" tests and fake out external things that might change, break or be down causing flaky tests.

🔸 Improve Test Coverage (Important)

Image of Andy W Andy W

Copy link
Contributor Author

@dabdine dabdine Nov 19, 2022

Choose a reason for hiding this comment

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

It does not. On line 34 in gpt3_test.go, TestRequestCreationFails sets a fake transport on the http client passed to the gpt3 client. The fake transport is a FakeRoundTripper (already defined in this code base), which just mocks a transport layer, so no network sockets are opened.

However, these test cases are not testing anything unique. You can see all the same cases are just testing an error response. The error response is exactly the same except for the URL in the error message. This implies the entire set of test cases can be simplified to a single test case that tests the constructed error message meets an expected string.

This is why I claimed in a separate comment that the test cases would benefit from a refactor. In the end, the only thing different for each API call (outside of the streaming completion request) is the struct type passed to functions that are doing the same thing. There's no reason to test the golang JSON unmarshaler or HTTP client over and over (edit: the specific thing you'd want to test is that unmarshaling/marshaling to the struct type works -- that struct tags are set properly). Since all the API methods use the same or copy pasted code (except for the streaming completion endpoint), the code could be refactored, we could test the refactored/reused API call method once and have the same or better confidence that the test cases cover what we need.

The main reason why I did not pick up refactoring the code base and test cases is to keep this PR clean so it's easy to review.

Copy link

Choose a reason for hiding this comment

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

That makes sense and it is tricky testing HTTP calls, especially because things like http.NewRequest returns an error but only in cases where url.Parse() returns an error so it can be tricky to force a test to cover that.
I missed where you use the FakeRoundTripper, that's a great way to set up http client tests.

Image of Andy W Andy W

Copy link
Contributor

@tylermann tylermann left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this PR! The code here looks good to me and matches existing patterns. I left one comment just on a response field that I didn't see in the docs so wanted to double check it is intended. Otherwise this looks good to merge.

models.go Outdated
type EmbeddingsResponse struct {
Object string `json:"object"`
Data []EmbeddingsResult `json:"data"`
Model string `json:"model"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to double check, is this field included in the response? I don't see this in the docs here: https://beta.openai.com/docs/api-reference/embeddings/create

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No! Great catch. Removed Model and added Usage. I updated the test cases as well.

@tylermann tylermann merged commit 6604991 into PullRequestInc:main Nov 22, 2022
Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

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

Hey @dabdine,
Was the feedback from PullRequest helpful? Yes | No

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.

3 participants