Documentation
¶
Overview ¶
Package openai provides an OmniLLM adapter for OpenAI's API using the official openai-go SDK.
This package implements the core.Provider interface from omnillm-core, wrapping the official OpenAI Go SDK (github.com/openai/openai-go).
Features ¶
The adapter supports all major OpenAI features:
- Chat completions (GPT-4, GPT-4 Turbo, GPT-3.5 Turbo, etc.)
- Streaming responses
- Tool/function calling
- Vision (image inputs)
- JSON mode
Basic Usage ¶
import (
core "github.com/plexusone/omnillm-core"
"github.com/plexusone/omnillm-openai"
)
func main() {
provider, err := openai.New(openai.Config{
APIKey: os.Getenv("OPENAI_API_KEY"),
})
if err != nil {
log.Fatal(err)
}
defer provider.Close()
resp, err := provider.CreateChatCompletion(ctx, &core.ChatCompletionRequest{
Model: "gpt-4",
Messages: []core.Message{
{Role: core.RoleUser, Content: "Hello!"},
},
})
}
Configuration ¶
The Config struct supports:
- APIKey: Your OpenAI API key (required)
- BaseURL: Custom API endpoint (optional, for Azure or proxies)
- Organization: OpenAI organization ID (optional)
Streaming ¶
stream, err := provider.CreateChatCompletionStream(ctx, &core.ChatCompletionRequest{
Model: "gpt-4",
Messages: []core.Message{
{Role: core.RoleUser, Content: "Tell me a story"},
},
})
if err != nil {
log.Fatal(err)
}
defer stream.Close()
for {
chunk, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Print(chunk.Choices[0].Delta.Content)
}
Index ¶
- type Config
- type Provider
- func (p *Provider) Capabilities() core.Capabilities
- func (p *Provider) Close() error
- func (p *Provider) CreateChatCompletion(ctx context.Context, req *core.ChatCompletionRequest) (*core.ChatCompletionResponse, error)
- func (p *Provider) CreateChatCompletionStream(ctx context.Context, req *core.ChatCompletionRequest) (core.ChatCompletionStream, error)
- func (p *Provider) Name() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// APIKey is the OpenAI API key (required).
APIKey string
// BaseURL is an optional custom API endpoint.
// Use this for Azure OpenAI or proxy servers.
BaseURL string
// Organization is the optional OpenAI organization ID.
Organization string
}
Config holds configuration for the OpenAI provider.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements core.Provider using the official OpenAI SDK.
func (*Provider) Capabilities ¶
func (p *Provider) Capabilities() core.Capabilities
Capabilities returns the provider's supported features.
func (*Provider) CreateChatCompletion ¶
func (p *Provider) CreateChatCompletion(ctx context.Context, req *core.ChatCompletionRequest) (*core.ChatCompletionResponse, error)
CreateChatCompletion sends a chat completion request and returns the response.
func (*Provider) CreateChatCompletionStream ¶
func (p *Provider) CreateChatCompletionStream(ctx context.Context, req *core.ChatCompletionRequest) (core.ChatCompletionStream, error)
CreateChatCompletionStream creates a streaming chat completion.
Click to show internal directories.
Click to hide internal directories.