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

Open ai wrapper #1371

Closed
wants to merge 30 commits into from
Closed

Conversation

yash-sojitra
Copy link

#1242

Description:

  • wrote wrapper for chat completions endpoint of openai api
  • issue no 1242
  • used raw api endpoint instead of openai's own wrapper to eliminate an additional layer

Checklist:

  • [✅] I have formatted my code using goimport and golangci-lint.
  • [✅] All new code is covered by unit tests.
  • [✅] This PR does not decrease the overall code coverage.
  • [✅] I have reviewed the code comments and documentation for clarity.

@vikash
Copy link
Contributor

vikash commented Jan 8, 2025

Should we put this in datasource or in service package instead?

@yash-sojitra
Copy link
Author

Currently it's in data source. I had some confusions about this. So at last after that issue discussion I decided to build it in data source.

pkg/gofr/datasource/openai/chatcompletion.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/chatcompletion.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/chatcompletion.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/logger.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/logger.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client_test.go Outdated Show resolved Hide resolved
@yash-sojitra
Copy link
Author

@ccoVeille I have updated the PR.

pkg/gofr/datasource/openai/chatcompletion.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/chatcompletion.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/chatcompletion_test.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/chatcompletion_test.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/chatcompletion_test.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client.go Outdated Show resolved Hide resolved
@yash-sojitra
Copy link
Author

@ccoVeille I have updated the pr and replied to some of the conversations.

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@yash-sojitra
Copy link
Author

@Umang01-hash approval with write access required for the PR. it's already reviewed.

@yash-sojitra
Copy link
Author

@Umang01-hash @vikash a review required with write access.

@yash-sojitra
Copy link
Author

@ccoVeille @Umang01-hash in workflow-pipeline PKG unit-testing v1.22 passes but PKG unit-testing v1.21 fails and says was cancelled.

Copy link
Member

@Umang01-hash Umang01-hash left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! I have reviewed the changes, and I have a suggestion regarding the directory structure:

Since OpenAI is primarily a communication service and not a database, it would be more appropriate to place this under the service directory instead of datasource. This would better reflect its role as a service for generating responses.

Would you please consider moving the OpenAI integration to the service directory?

Also we need to make updates in the documentation regarding any new feature addition so that other users can know about it easily. Please also add the documenttaion regarding this wrapper and an example of how to use it.

pkg/gofr/datasource/openai/chatcompletion_test.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/chatcompletion.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/openai/client_test.go Outdated Show resolved Hide resolved
@yash-sojitra
Copy link
Author

is it okay ? i have placed openai folder in service folder.

@Umang01-hash
Copy link
Member

@yash-sojitra I think you might have forgot to push your commit..

@yash-sojitra
Copy link
Author

I was just asking i haven't yet committed changes.

@Umang01-hash
Copy link
Member

@yash-sojitra Please go ahead and commit the changes

@Umang01-hash
Copy link
Member

@yash-sojitra are you still active on the issue? Please let us know if you have any doubts regarding the PR or the issue

@yash-sojitra
Copy link
Author

Yupp I have done the changes to code. I didn't got much time to complete documentation. Will be doing in short time.

Also a thing I noticed since this is an optional service package I thought to add it to container struct just like external dbs are added and this open ai service will be added and accessible via gofr.context just like data sources. Is this approach to add OpenAI services good ?

@yash-sojitra
Copy link
Author

No problems. It was very insightful as it was my first issue into open source.

@ccoVeille
Copy link
Contributor

No problems. It was very insightful as it was my first issue into open source.

You did right then. I can tell by the way you reacted to the feedbacks that you were open to critics. You confirm it by telling you find feedbacks insightful.

We all started with open-source PR. Yours is pretty good.

@yash-sojitra
Copy link
Author

yash-sojitra commented Feb 1, 2025

@Umang01-hash @coolwednesday PR is ready for review.

pkg/gofr/container/mockcontainer_test.go Show resolved Hide resolved
docs/advanced-guide/using-openai-api/page.md Show resolved Hide resolved
pkg/gofr/service/openai/openai.go Outdated Show resolved Hide resolved
pkg/gofr/service/openai/openai.go Outdated Show resolved Hide resolved
pkg/gofr/service/openai/logger.go Outdated Show resolved Hide resolved
docs/advanced-guide/using-openai-api/page.md Outdated Show resolved Hide resolved
@Umang01-hash
Copy link
Member

@yash-sojitra Please complete the review changes suggested by @coolwednesday. And please also resolve the merge conflicts in your PR.

@yash-sojitra
Copy link
Author

@coolwednesday i have some comments on conversation if you can review about it. other changes i have done.

Copy link
Contributor

@coolwednesday coolwednesday left a comment

Choose a reason for hiding this comment

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

Can you also add the link to the documentation where you can generate API keys......in contributing.md .

docs/advanced-guide/using-openai-api/page.md Outdated Show resolved Hide resolved
@yash-sojitra
Copy link
Author

Can you also add the link to the documentation where you can generate API keys......in contributing.md .

I think it will be better to add link to the documentation page of OpenAI package.

@coolwednesday
Copy link
Contributor

@yash-sojitra , Thanks for bearing with me. I just have few more doubts .... Do you plan to extend this interface in future ?

If yes, how would you address the breaking change when you would be changing the interface .... ?

@yash-sojitra
Copy link
Author

I will be adding more function rather than changing existing implementations.

I have currently implemented only one endpoint That is create completions.

@coolwednesday
Copy link
Contributor

coolwednesday commented Feb 11, 2025

Screenshot 2025-02-11 at 3 10 58 PM

Here are the logs, it is actually trying to print the reference to whatever you are passing there, please fix it.... you can just give meaningful information there whatever fits the usecase.....

Also, here my API key had less limit so an error was returned in the response but a 200 code is shown ...to handle this you must fix check for error in your received response. ...

If present you return the error.... or else you return the response and nil for error ....this would help set proper status codes in GoFr.

Also is v1/chat/completions requiring you to have a api key with a plan attached to it. If yes, please do add that also in Documentation.

Comment on lines +190 to +194

c.metrics.RecordHistogram(ctx, "openai_api_request_duration", float64(duration))
c.metrics.IncrementCounter(ctx, "openai_api_total_request_count")
c.metrics.DeltaUpDownCounter(ctx, "openai_api_token_usage", float64(ql.Usage.TotalTokens))

Copy link
Contributor

Choose a reason for hiding this comment

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

Add an attribute operation that is createCompletion for some context in the metric .....

@yash-sojitra
Copy link
Author

yash-sojitra commented Feb 11, 2025

I want to just confirm one thing. do I need to make error struct in openai package ? because i saw that how http status codes are passed to the user and in that, error was implementing certain interface so do i need to implement it in my package ?

@yash-sojitra
Copy link
Author

I am making an error struct that implements built in error interface and also StatusCodeResponder that the framework uses to set the status code of the current request.

And now all the errors will be passed wrapped in this struct with appropriate status code. It will also be a valid error type since it implements built in error interface.

This will be my approach to handle particular status codes will it be okay?

@coolwednesday
Copy link
Contributor

I am making an error struct that implements built in error interface and also StatusCodeResponder that the framework uses to set the status code of the current request.

And now all the errors will be passed wrapped in this struct with appropriate status code. It will also be a valid error type since it implements built in error interface.

This will be my approach to handle particular status codes will it be okay?

Hey !

Yeah that works. Do add some tests so that, we can verify how are you setting status codes through examples.

@yash-sojitra
Copy link
Author

it like ErrorDB (errors.go) in datasoruce. with just one more field of Statuscode and some minor change to the implementation of StatusCode() function

@coolwednesday
Copy link
Contributor

coolwednesday commented Feb 12, 2025

Hi @yash-sojitra,

Thank you for your effort on this PR! After carefully reviewing the changes, I feel that the current implementation might be quite complex from an end developer’s perspective. To improve the developer experience, we should consider some design refinements—specifically, introducing a unified interface for all AI agents to abstract away the underlying complexity.

Given this, I’d suggest keeping this PR on hold for now. It would be great to start a discussion (on Discord or GitHub) to align on the best approach for integrating AI agent usage in GoFr through a unified interface before proceeding with the implementation.

Apologies for any inconvenience, and I truly appreciate your contributions. I also recommend closing this PR for now and revisiting it after we finalize the design direction. Looking forward to your thoughts!

@yash-sojitra
Copy link
Author

Yes, it will be best to provide interface for all ai agents in the market. I am looking forwards to this discussion on discord.

@coolwednesday
Copy link
Contributor

Yes, it will be best to provide interface for all ai agents in the market. I am looking forwards to this discussion on discord.

I have created a discussion on discord. Please let me know what you feel about the approach there ....

@coolwednesday
Copy link
Contributor

coolwednesday commented Feb 13, 2025

Yes, it will be best to provide interface for all ai agents in the market. I am looking forwards to this discussion on discord.

I have created a discussion on discord. Please let me know what you feel about the approach there ....

Also, let's close this PR for the time being It can be refactored when you push the refactored code....

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.

5 participants