Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
285764f
init openai plugin
hugoaguirre Jan 6, 2026
d6d1017
toToolChoice
hugoaguirre Jan 7, 2026
ef8ef11
add messages translation and test cases
hugoaguirre Jan 7, 2026
9a33b3c
add generate functions
hugoaguirre Jan 9, 2026
a15f83c
docs
hugoaguirre Jan 9, 2026
7d4c46f
test: config to schema
hugoaguirre Jan 9, 2026
cec8cad
fix schema parsing
hugoaguirre Jan 9, 2026
ebb59ba
fix usage tokens and tool calls
hugoaguirre Jan 9, 2026
c9bd210
fix stream and conv history
hugoaguirre Jan 12, 2026
145bce5
update file structure
hugoaguirre Jan 12, 2026
dd646ee
add more live test cases
hugoaguirre Jan 12, 2026
98aec5f
small refactor in translateResponse for type handling
hugoaguirre Jan 13, 2026
e95fc43
return error in handleResponseItem and minor doc updates
hugoaguirre Jan 13, 2026
8edbb96
add structured output
hugoaguirre Jan 13, 2026
ec3173e
remove invalid empty config test
hugoaguirre Jan 13, 2026
02f20b0
update output format handler in translator
hugoaguirre Jan 13, 2026
6d98964
add ModelRef function
hugoaguirre Jan 14, 2026
a2ae094
add ModelRef live tests
hugoaguirre Jan 14, 2026
cce711f
bump openai-go to v.3.16.0
hugoaguirre Jan 14, 2026
3543730
Merge branch 'main' into haguirre/openai
hugoaguirre Jan 14, 2026
a137acd
add OpenAI provider in README.md
hugoaguirre Jan 14, 2026
d98146c
fix system instructions and add Raw response
hugoaguirre Jan 20, 2026
f495046
fix outdated test case
hugoaguirre Jan 20, 2026
70bb6c0
add dynamic provider support
hugoaguirre Jan 21, 2026
44b20bc
Merge branch 'main' into haguirre/openai
hugoaguirre Jan 29, 2026
1012969
fix: review comments
hugoaguirre Jan 29, 2026
33f68af
remove unused code
hugoaguirre Jan 29, 2026
8a57553
Merge branch 'main' into haguirre/openai
hugoaguirre Jan 31, 2026
e210f3a
add README.md
hugoaguirre Jan 31, 2026
b93e4c5
add openrouter
hugoaguirre Jan 31, 2026
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
4 changes: 4 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ Genkit provides a unified interface across all major AI providers. Use whichever
| **Google AI** | `googlegenai.GoogleAI` | Gemini 2.5 Flash, Gemini 2.5 Pro, and more |
| **Vertex AI** | `vertexai.VertexAI` | Gemini models via Google Cloud |
| **Anthropic** | `anthropic.Anthropic` | Claude 3.5, Claude 3 Opus, and more |
| **OpenAI** | `openai.OpenAI` | GPT-5, GPT-5-mini, GPT-5-nano, GPT-4o and more |
| **Ollama** | `ollama.Ollama` | Llama, Mistral, and other open models |
| **OpenAI Compatible** | `compat_oai` | Any OpenAI-compatible API |

Expand All @@ -509,6 +510,9 @@ g := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{}))
// Anthropic
g := genkit.Init(ctx, genkit.WithPlugins(&anthropic.Anthropic{}))

// OpenAI
g := genkit.Init(ctx, genkit.WithPlugins(&openai.OpenAI{}))

// Ollama (local models)
g := genkit.Init(ctx, genkit.WithPlugins(&ollama.Ollama{
ServerAddress: "http://localhost:11434",
Expand Down
1 change: 1 addition & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/jba/slog v0.2.0
github.com/lib/pq v1.10.9
github.com/mark3labs/mcp-go v0.29.0
github.com/openai/openai-go/v3 v3.16.0
github.com/pgvector/pgvector-go v0.3.0
github.com/weaviate/weaviate v1.30.0
github.com/weaviate/weaviate-go-client/v5 v5.1.0
Expand Down
2 changes: 2 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/openai/openai-go v1.8.2 h1:UqSkJ1vCOPUpz9Ka5tS0324EJFEuOvMc+lA/EarJWP8=
github.com/openai/openai-go v1.8.2/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y=
github.com/openai/openai-go/v3 v3.16.0 h1:VdqS+GFZgAvEOBcWNyvLVwPlYEIboW5xwiUCcLrVf8c=
github.com/openai/openai-go/v3 v3.16.0/go.mod h1:cdufnVK14cWcT9qA1rRtrXx4FTRsgbDPW7Ia7SS5cZo=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
Expand Down
268 changes: 268 additions & 0 deletions go/plugins/openai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
# OpenAI Plugin for Genkit

The OpenAI plugin for Genkit provides integration with the [OpenAI API](https://platform.openai.com/docs/api-reference), allowing you to use models like GPT-4o, GPT-4 Turbo, and embeddings within your Genkit applications.

> [!NOTE]
> This plugin uses the modern OpenAI **Responses API**. For applications requiring the legacy **Chat Completions API**, please use the [`compat_oai`](../compat_oai) plugin.

This plugin uses the official [openai-go](https://github.com/openai/openai-go) SDK.

## Setup

First, ensure you have your OpenAI API key set in your environment variables:

```bash
export OPENAI_API_KEY=your-api-key
```

Then, initialize the plugin in your Genkit application:

```go
import (
"context"
"log"

"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/openai"
)

func main() {
ctx := context.Background()

// Initialize Genkit with the OpenAI plugin
g := genkit.Init(ctx, genkit.WithPlugins(&openai.OpenAI{}))

// ...
}
```

## OpenAI-Compatible Providers

You can use this plugin with other providers that support the OpenAI API by configuring the `BaseURL` and `Provider` fields.

### Groq

```go
g := genkit.Init(ctx, genkit.WithPlugins(&openai.OpenAI{
BaseURL: "https://api.groq.com/openai/v1",
APIKey: os.Getenv("GROQ_API_KEY"),
Provider: "groq",
}))

model := openai.Model(g, "llama-3.3-70b-versatile")
```

### Mistral

```go
g := genkit.Init(ctx, genkit.WithPlugins(&openai.OpenAI{
BaseURL: "https://api.mistral.ai/v1",
APIKey: os.Getenv("MISTRAL_API_KEY"),
Provider: "mistral",
}))

model := openai.Model(g, "mistral-large-latest")
```

### xAI (Grok)

```go
g := genkit.Init(ctx, genkit.WithPlugins(&openai.OpenAI{
BaseURL: "https://api.x.ai/v1",
APIKey: os.Getenv("XAI_API_KEY"),
Provider: "xai",
}))

model := openai.Model(g, "grok-4-1-fast-reasoning")
```

### OpenRouter

```go
g := genkit.Init(ctx, genkit.WithPlugins(&openai.OpenAI{
BaseURL: "https://openrouter.ai/api/v1",
APIKey: os.Getenv("OPENROUTER_API_KEY"),
Provider: "openrouter",
}))

model := openai.Model(g, "deepseek/deepseek-r1")
```

## Usage

### Generative Models

You can reference OpenAI models using the `openai.Model` helper.

```go
import (
"context"
"fmt"
"log"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/openai"
)

func main() {
ctx := context.Background()
g := genkit.Init(ctx, genkit.WithPlugins(&openai.OpenAI{}))

// Reference a model (e.g., gpt-4o)
model := openai.Model(g, "gpt-4o")

// Generate text
resp, err := genkit.GenerateText(ctx, g,
ai.WithModel(model),
ai.WithPrompt("Tell me a joke about Go programming."),
)
if err != nil {
log.Fatal(err)
}

fmt.Println(resp)
}
```

### Model Configuration

Pass configuration options like temperature and max output tokens using `ai.WithConfig`:

```go
import (
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)

// ...

resp, err := genkit.Generate(ctx, g,
ai.WithModel(model),
ai.WithPrompt("Write a creative story."),
ai.WithConfig(&responses.ResponseNewParams{
Temperature: openai.Float(1.2),
MaxOutputTokens: openai.Int(500),
}),
)
```

### Streaming

Stream responses for real-time interactions:

```go
import (
"context"
"fmt"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/openai"
)

// ...

model := openai.Model(g, "gpt-4o")

_, err := genkit.Generate(ctx, g,
ai.WithModel(model),
ai.WithPrompt("Write a long poem about the sea."),
ai.WithStreaming(func(ctx context.Context, chunk *ai.ModelResponseChunk) error {
fmt.Print(chunk.Text())
return nil
}),
)
```

### Structured Output

Request structured data (JSON) using `WithOutputType`. This leverages OpenAI's structured output capabilities.

```go
type MovieReview struct {
Title string `json:"title"`
Rating int `json:"rating"`
Reason string `json:"reason"`
}

// ...

model := openai.Model(g, "gpt-4o")

resp, err := genkit.Generate(ctx, g,
ai.WithModel(model),
ai.WithPrompt("Review the movie 'Inception'"),
ai.WithOutputType(MovieReview{}),
)
if err != nil {
log.Fatal(err)
}

var review MovieReview
if err := resp.Output(&review); err != nil {
log.Fatal(err)
}

fmt.Printf("Rating: %d/10\nReason: %s\n", review.Rating, review.Reason)
```

### Tool Calling

Register tools and provide them to the model.

```go
// Define a tool
weatherTool := genkit.DefineTool(g, "weather", "Returns the weather for the given location",
func(ctx *ai.ToolContext, input struct {
Location string `json:"location"`
}) (string, error) {
// Call actual weather API here
return fmt.Sprintf("The weather in %s is sunny", input.Location), nil
},
)

// Use the tool in generation
resp, err := genkit.Generate(ctx, g,
ai.WithModel(model),
ai.WithPrompt("What is the weather in San Francisco?"),
ai.WithTools(weatherTool),
)
```

### Multimodal Input

Pass images to models that support vision (like `gpt-4o`).

```go
import "github.com/firebase/genkit/go/ai"

// ...

resp, err := genkit.Generate(ctx, g,
ai.WithModel(model),
ai.WithMessages(
ai.NewUserMessage(
ai.NewTextPart("Describe this image"),
ai.NewMediaPart("image/jpeg", "data:image/jpeg;base64,iVBORw0KGgo..."),
),
),
)
```

### Embeddings

Use OpenAI embedding models for vector search tasks.

```go
import "github.com/firebase/genkit/go/plugins/openai"

// ...

embedder := openai.Embedder(g, "text-embedding-3-small")

res, err := ai.Embed(ctx, g,
ai.WithEmbedder(embedder),
ai.WithTextDocs("Genkit is awesome!"),
)
```
Loading
Loading