
About The Project
openaifx is an Uber Fx module that provides the OpenAI Go SDK client via dependency injection. It handles client construction, configuration validation, and named logger integration so you can focus on building features.
Key features:
- Zero-boilerplate client setup -- supply a
Config and get a ready-to-use *openai.Client
- Custom API endpoint -- point
BaseURL at proxies, mocks, or OpenAI-compatible services
- Custom HTTP client -- inject your own
*http.Client for timeouts, TLS, or transport tweaks
- Named logger -- automatically decorates the Fx logger with the
openaifx component name
- Consistent conventions -- follows the same patterns as
openrouterfx, sqlfx, and other go-core-fx modules
(back to top)
Built With
(back to top)
Getting Started
Prerequisites
Installation
go get github.com/go-core-fx/openaifx
(back to top)
Usage
Configuration
The Config struct controls client construction:
| Field |
Type |
Required |
Description |
APIKey |
string |
Yes |
Your OpenAI API key |
BaseURL |
string |
No |
Custom API endpoint (e.g. a proxy or mock server) |
HTTPClient |
*http.Client |
No |
Custom HTTP client for transport configuration |
Environment Variables
Config.APIKey is required. New rejects an empty API key before the SDK can fall back to OPENAI_API_KEY, so the environment variable alone is not sufficient through this wrapper.
Integration with Uber Fx
package main
import (
"go.uber.org/fx"
"github.com/go-core-fx/openaifx"
"github.com/openai/openai-go/v3"
)
func main() {
fx.New(
// Provide the OpenAI configuration
fx.Supply(openaifx.Config{
APIKey: "sk-...",
BaseURL: "", // leave empty to use the default OpenAI endpoint
}),
// Register the openaifx module
openaifx.Module(),
// Your application modules...
fx.Invoke(func(client *openai.Client) {
// Use client here
}),
).Run()
}
Injecting the Client
Once the module is registered, inject *openai.Client into any constructor or invoke:
func NewService(client *openai.Client) *Service {
return &Service{client: client}
}
Using with a Custom HTTP Client
import (
"net/http"
"time"
)
fx.Supply(openaifx.Config{
APIKey: "sk-...",
HTTPClient: &http.Client{
Timeout: 60 * time.Second,
},
})
Using with a Custom Base URL
Useful for testing with wiremock or compatible OpenAI-mocking services:
fx.Supply(openaifx.Config{
APIKey: "sk-...",
BaseURL: "http://localhost:8080/v1/",
})
(back to top)
Roadmap
- Basic client construction from
Config
- Custom
BaseURL support
- Custom
*http.Client injection
- Named logger integration via
go-core-fx/logger
- Lifecycle health check (ping on application start)
- Multiple named client instances
See the open issues for a full list of proposed features (and known issues).
(back to top)
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature)
- Commit your Changes (
git commit -m 'Add some AmazingFeature')
- Push to the Branch (
git push origin feature/AmazingFeature)
- Open a Pull Request
(back to top)
License
Distributed under the Apache 2.0 License. See LICENSE for more information.
(back to top)
go-core-fx -- https://github.com/go-core-fx/openaifx
(back to top)
Acknowledgments
(back to top)