-
Notifications
You must be signed in to change notification settings - Fork 282
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
base: development
Are you sure you want to change the base?
Open ai wrapper #1371
Conversation
Should we put this in datasource or in service package instead? |
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. |
@ccoVeille I have updated the PR. |
@ccoVeille I have updated the pr and replied to some of the conversations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
@Umang01-hash approval with write access required for the PR. it's already reviewed. |
@Umang01-hash @vikash a review required with write access. |
@ccoVeille @Umang01-hash in workflow-pipeline PKG unit-testing v1.22 passes but PKG unit-testing v1.21 fails and says was cancelled. |
There was a problem hiding this 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.
is it okay ? i have placed openai folder in service folder. |
@yash-sojitra I think you might have forgot to push your commit.. |
I was just asking i haven't yet committed changes. |
@yash-sojitra Please go ahead and commit the changes |
@yash-sojitra are you still active on the issue? Please let us know if you have any doubts regarding the PR or the issue |
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 Please resolve these code quality issues as well as test failure in your PR: |
@yash-sojitra But for all the remaining PR's that we have all checks are passing. You also need to update mockContainer if you have updated container. |
I fixed the above mentioned test but there is still one linting error that is occurring across several files. File is not `gci`-ed with --skip-generated -s standard -s default -s localmodule (gci)go-golangci-lint |
@yash-sojitra you can do it using this command : |
func New() *App {
app := &App{}
app.readConfig(false)
app.container = container.NewContainer(app.Config)
app.initTracer()
// Metrics Server
port, err := strconv.Atoi(app.Config.Get("METRICS_PORT"))
if err != nil || port <= 0 {
port = defaultMetricPort
}
if !isPortAvailable(port) {
app.container.Logger.Fatalf("metrics port %d is blocked or unreachable", port)
}
app.metricServer = newMetricServer(port)
// HTTP Server
port, err = strconv.Atoi(app.Config.Get("HTTP_PORT"))
if err != nil || port <= 0 {
port = defaultHTTPPort
}
app.httpServer = newHTTPServer(app.container, port, middleware.GetConfigs(app.Config))
app.httpServer.certFile = app.Config.GetOrDefault("CERT_FILE", "")
app.httpServer.keyFile = app.Config.GetOrDefault("KEY_FILE", "")
// Add Default routes
app.add(http.MethodGet, "/.well-known/health", healthHandler)
app.add(http.MethodGet, "/.well-known/alive", liveHandler)
app.add(http.MethodGet, "/favicon.ico", faviconHandler)
app.checkAndAddOpenAPIDocumentation()
if app.Config.Get("APP_ENV") == "DEBUG" {
app.httpServer.RegisterProfilingRoutes()
}
// gRPC Server
port, err = strconv.Atoi(app.Config.Get("GRPC_PORT"))
if err != nil || port <= 0 {
port = defaultGRPCPort
}
app.grpcServer = newGRPCServer(app.container, port)
app.subscriptionManager = newSubscriptionManager(app.container)
// static file server
currentWd, _ := os.Getwd()
checkDirectory := filepath.Join(currentWd, defaultPublicStaticDir)
if _, err = os.Stat(checkDirectory); err == nil {
app.AddStaticFiles(defaultPublicStaticDir, checkDirectory)
}
return app
} it added a newline after every line. and it modified all the files like this. |
![]() |
Thanks for baring with my iterative feedbacks. I know let @Umang01-hash or @coolwednesday take a look at the things I might have missed |
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. |
@Umang01-hash @coolwednesday PR is ready for review. |
func Test_HttpServiceMock(t *testing.T) { | ||
test := struct { | ||
desc string | ||
path string | ||
statusCode int | ||
expectedRes string | ||
}{ | ||
|
||
desc: "simple service handler", | ||
path: "/fact", | ||
expectedRes: `{"data":{"fact":"Cats have 3 eyelids.","length":20}}` + "\n", | ||
statusCode: 200, | ||
} | ||
|
||
httpservices := []string{"cat-facts", "cat-facts1", "cat-facts2"} | ||
|
||
_, mock := NewMockContainer(t, WithMockHTTPService(httpservices...)) | ||
|
||
res := httptest.NewRecorder() | ||
res.Body = bytes.NewBufferString(`{"fact":"Cats have 3 eyelids.","length":20}` + "\n") | ||
res.Code = test.statusCode | ||
res.Code = 200 | ||
result := res.Result() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I know why this test has been removed ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only statusCode field was used in test struct. So the linter was giving error about other three unused fields. Since only one field was used from the test struct I removed it and manually updated the status code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// implementation of chat endpoint of openai api | ||
CreateCompletions(ctx context.Context, r any) (any, error) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we give a better name, createCompletion sounds confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is OpenAI's nomenclature.
pkg/gofr/service/openai/openai.go
Outdated
// Get makes a get request. | ||
func (c *Client) Get(ctx context.Context, url string) (response []byte, err error) { | ||
resp, err := c.Call(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
c.logger.Errorf("%v", err) | ||
return response, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
response, err = io.ReadAll(resp.Body) | ||
if err != nil { | ||
c.logger.Errorf("%v", err) | ||
} | ||
|
||
return response, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I know where are we actually calling this function ? Even if I remove it I do not see any dependency. Did iI miss anything ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this chat completion is only one end point I have kept it for future use where we need to add more OpenAI endpoints.
@yash-sojitra Please complete the review changes suggested by @coolwednesday. And please also resolve the merge conflicts in your PR. |
@coolwednesday i have some comments on conversation if you can review about it. other changes i have done. |
#1242
Description:
Checklist:
goimport
andgolangci-lint
.