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

Open
wants to merge 29 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
aa48ff4
wrapped openai api chat completion endpoint
yash-sojitra Dec 24, 2024
251ca2a
formatting changes
yash-sojitra Dec 24, 2024
1db7000
increased code coverage
yash-sojitra Jan 7, 2025
d716392
Merge branch 'development' into OpenAI-wrapper
Umang01-hash Jan 8, 2025
bc4ce3c
resolved changes
yash-sojitra Jan 8, 2025
63e2e5d
added usage struct to package level, updated get method, and resolved…
yash-sojitra Jan 9, 2025
2d869c3
formatting changes
yash-sojitra Jan 9, 2025
fcc32fa
Merge branch 'development' into OpenAI-wrapper
yash-sojitra Jan 9, 2025
d78f784
Merge branch 'development' into OpenAI-wrapper
yash-sojitra Jan 10, 2025
71fa839
typo fix
yash-sojitra Jan 10, 2025
4f47c74
resolved changes, added more tests to client, increased code coverage
yash-sojitra Jan 13, 2025
7c41988
Merge branch 'development' into OpenAI-wrapper
Umang01-hash Jan 21, 2025
266c1ac
Merge branch 'development' into OpenAI-wrapper
Umang01-hash Jan 27, 2025
ee06e2f
Merge branch 'OpenAI-wrapper' of https://github.com/yash-sojitra/gofr…
yash-sojitra Jan 27, 2025
630755e
added code for injecting openai package into container. added a new p…
yash-sojitra Jan 30, 2025
7929e5b
added documentation and wrote injection code
yash-sojitra Jan 30, 2025
09d9e18
Merge branch 'development' into OpenAI-wrapper
Umang01-hash Jan 30, 2025
c209562
resolved changes and fixed mockcontainer_Test that was failing in cod…
yash-sojitra Jan 30, 2025
96f2da6
solved linting error in services.go file
yash-sojitra Jan 31, 2025
30cae8f
more linter changes
yash-sojitra Jan 31, 2025
658a91a
resolve linters
Umang01-hash Jan 31, 2025
59c03eb
remove deprecated methods in test
Umang01-hash Jan 31, 2025
c20bc1e
resolved changes
yash-sojitra Jan 31, 2025
dd89f56
Merge branch 'development' into OpenAI-wrapper
Umang01-hash Feb 4, 2025
b9f4447
Merge branch 'development' into OpenAI-wrapper
yash-sojitra Feb 4, 2025
50e0700
Merge branch 'development' into OpenAI-wrapper
Umang01-hash Feb 5, 2025
88e7cb4
resolved changes
yash-sojitra Feb 6, 2025
e26d9ab
resolved changes and conflics
yash-sojitra Feb 6, 2025
8acefc2
Merge branch 'OpenAI-wrapper' of https://github.com/yash-sojitra/gofr…
yash-sojitra Feb 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/advanced-guide/using-openai-api/page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Using OpenAI Api

GoFr provides an injectable module that integrates OpenAI's API into the GoFr applications. Since it doesn’t come bundled with the framework, this wrapper can be injected seamlessly to extend Gofr's capabilities, enabling developers to utilize OpenAI's powerful AI models effortlessly while maintaining flexibility and scalability.

GoFr supports any OpenAI API wrapper that implements the following interface. Any other wrapper that implements the interface can be added using `app.AddOpenAI()` method, and user's can use openai across application with `gofr.Context`.

```go
type OpenAI interface {
// implementation of chat endpoint of openai api
CreateCompletions(ctx context.Context, r any) (any, error)
}
Comment on lines +9 to +11
Copy link
Contributor

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.

Copy link
Author

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.

```

### Example
```go
package main

import (
"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/service/openai"
)

func main() {
app := gofr.New()

config := openai.Config{
APIKey: app.Config.Get("OPENAI_API_KEY"),
Model: "gpt-3.5-turbo",

// optional config parameters
// BaseURL: "https://api.custom.com",
// Timeout: 10 * time.Second,
// MaxIdleConns: 10,
}

openAIClient, err := openai.NewClient(&config)
if err != nil {
return
}

app.AddOpenAI(openAIClient)

app.POST("/chat", Chat)

app.Run()
}

func Chat(ctx *gofr.Context) (any, error) {

var req *openai.CreateCompletionsRequest

if err := ctx.Bind(&req); err != nil {
return nil, err
}

println(req)

resp, err := ctx.Openai.CreateCompletions(ctx, req)
if err != nil {
return nil, err
}

return resp, nil
}
```
yash-sojitra marked this conversation as resolved.
Show resolved Hide resolved

5 changes: 5 additions & 0 deletions docs/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export const navigation = [
title: 'Serving-Static Files',
href: '/docs/advanced-guide/serving-static-files',
desc: "Know how GoFr automatically serves static content from a static folder in the application directory."
},
{
title: 'Using OpenAI Api',
href: '/docs/advanced-guide/using-openai-api',
desc: "Know how to integrate OpenAI api into your applications easily with GoFr's very own OpenAI api wrapper."
}
],
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/gofr/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type Container struct {
ScyllaDB ScyllaDB
SurrealDB SurrealDB

OpenAI OpenAI

KVStore KVStore

File file.FileSystem
Expand Down
3 changes: 3 additions & 0 deletions pkg/gofr/container/mock_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func NewMockContainer(t *testing.T, options ...options) (*Container, *Mocks) {
opentsdbMock := NewMockOpenTSDBProvider(ctrl)
container.OpenTSDB = opentsdbMock

openAIMock := NewMockOpenAIProvider(ctrl)
container.OpenAI = openAIMock

var httpMock *service.MockHTTP

container.Services = make(map[string]service.HTTP)
Expand Down
143 changes: 143 additions & 0 deletions pkg/gofr/container/mock_services.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 1 addition & 14 deletions pkg/gofr/container/mockcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,13 @@ import (
)

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()
Comment on lines 17 to 25
Copy link
Contributor

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 ?

Copy link
Author

@yash-sojitra yash-sojitra Feb 5, 2025

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.

Copy link
Author

Choose a reason for hiding this comment

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

{2F078F26-22B9-426C-B391-12C0198AE163}

This gives linter error and breaks in code check.


// Setting mock expectations
Expand Down
28 changes: 28 additions & 0 deletions pkg/gofr/container/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package container

import (
"context"
)

// OpenAI is the interface that wraps the basic endpoint of OpenAI API.

type OpenAI interface {
yash-sojitra marked this conversation as resolved.
Show resolved Hide resolved
// implementation of chat endpoint of OpenAI API
CreateCompletions(ctx context.Context, r any) (any, error)
}

type OpenAIProvider interface {
OpenAI

// UseLogger set the logger for OpenAI client
UseLogger(logger any)

// UseMetrics set the logger for OpenAI client
UseMetrics(metrics any)

// UseTracer set the logger for OpenAI client
UseTracer(tracer any)

// InitMetrics is used to initializes metrics for the client
InitMetrics()
}
Loading
Loading