openaifx

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Contributors Forks Stargazers Issues Apache 2.0 License


openaifx

Uber Fx wrapper for the OpenAI Go API
Explore the docs · Report Bug · Request Feature

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

Technology Badge
Go Go
OpenAI Go SDK OpenAI SDK
Uber Fx Uber Fx
Uber Zap Uber Zap

(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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the Apache 2.0 License. See LICENSE for more information.

(back to top)

Contact

go-core-fx -- https://github.com/go-core-fx/openaifx

(back to top)

Acknowledgments

(back to top)

Documentation

Index

Constants

View Source
const ModuleName = "openaifx"

Variables

View Source
var (
	// ErrInvalidConfig is returned when the OpenAI configuration is invalid.
	ErrInvalidConfig = errors.New("invalid config")
)

Functions

func Module

func Module() fx.Option

func New

func New(config Config) (*openai.Client, error)

New creates a new OpenAI client from the provided configuration.

Types

type Config

type Config struct {
	APIKey     string
	BaseURL    string
	HTTPClient *http.Client
}

Config holds the OpenAI client configuration. APIKey is the OpenAI API key (required). BaseURL is an optional custom API endpoint URL. HTTPClient is an optional custom HTTP client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址