-
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
Changes from 22 commits
aa48ff4
251ca2a
1db7000
d716392
bc4ce3c
63e2e5d
2d869c3
fcc32fa
d78f784
71fa839
4f47c74
7c41988
266c1ac
ee06e2f
630755e
7929e5b
09d9e18
c209562
96f2da6
30cae8f
658a91a
59c03eb
c20bc1e
dd89f56
b9f4447
50e0700
88e7cb4
e26d9ab
8acefc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
``` | ||
|
||
### 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
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
// Setting mock expectations | ||
|
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() | ||
} |
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.