Skip to content

feat(go/plugins): working status of openai plugin #2440

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

Closed
wants to merge 111 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
3bba12b
Outline for plugin
kekoawong Mar 14, 2025
6dbfd2f
add openai client package
kekoawong Mar 14, 2025
c124510
Added outline for supported models
kekoawong Mar 14, 2025
d749d68
incomplete generate file
kekoawong Mar 17, 2025
db4caf3
Initial builder of generate functionality complete
kekoawong Mar 17, 2025
1afb36f
Added live tests for the generator function
kekoawong Mar 17, 2025
78c365a
Added generator functionality
kekoawong Mar 18, 2025
ec2383e
Added testing docs
kekoawong Mar 18, 2025
c699625
Added package changes
kekoawong Mar 18, 2025
eadab5b
Moved to compat_oai
kekoawong Mar 20, 2025
d4e4cc0
Moved to compat_oai
kekoawong Mar 20, 2025
5e2cb4b
Updated testing functions
kekoawong Mar 20, 2025
e8e8121
Renamed generator to ModelGenerator
kekoawong Mar 20, 2025
e731e3b
Added docstrings
kekoawong Mar 20, 2025
7c897c5
Added live test for plugin
kekoawong Mar 20, 2025
0849271
Updated documentation on how to run tests
kekoawong Mar 20, 2025
5e39216
Tests failing
kekoawong Mar 20, 2025
343d2c6
Tests still failing initially
kekoawong Mar 21, 2025
a7cf666
Added multimodal capabilities
kekoawong Mar 21, 2025
0d54e56
Added usage and request initialization
kekoawong Mar 23, 2025
e3caa0c
Merge pull request #1 from kekoawong/kekoawong/open-ai-plugin
kekoawong Mar 23, 2025
8eb80d7
Added embedding support
yukinagae Mar 24, 2025
ffe8955
Remove check for embedder, since users can add them
yukinagae Mar 24, 2025
33595be
Merge pull request #2 from kekoawong/yukinagae/openai-plugin-embedder
yukinagae Mar 24, 2025
4f08315
feat: make the plugin generic
xavidop Mar 26, 2025
a9f982d
fix: rename file
xavidop Mar 26, 2025
78c3661
Update openai.go
xavidop Mar 27, 2025
0286368
feat: make the plugin generic
xavidop Mar 27, 2025
4e45561
Merge branch 'main' into main
xavidop Mar 27, 2025
662f890
fix: deps
xavidop Mar 27, 2025
ca7f27c
feat: added refactor
xavidop Mar 27, 2025
f2ad3ec
fix: added headers
xavidop Mar 27, 2025
8ca9cd8
Merge pull request #4 from kekoawong/xavier/fix-deps
xavidop Mar 27, 2025
eebab4f
feat: added generic config
xavidop Mar 27, 2025
84521b4
fix: add boolean support
xavidop Mar 27, 2025
47a5dc2
fix: use pointer
xavidop Mar 27, 2025
e169e52
feat: add unit tests
xavidop Mar 28, 2025
c6ded6b
feat: support 2 types
xavidop Mar 29, 2025
259e677
Merge pull request #5 from kekoawong/xavier/generic-config
xavidop Mar 29, 2025
8f5a939
added function calling support
yukinagae Mar 28, 2025
5f45a2e
Merge pull request #6 from kekoawong/yukinagae/openai-function-calling
yukinagae Apr 2, 2025
12194a3
add OpenAI models
yukinagae Apr 5, 2025
24f1302
Merge pull request #7 from kekoawong/yukinagae/add-openai-models
yukinagae Apr 5, 2025
35ea21b
Refactored generators
kekoawong Apr 5, 2025
480825f
Added changing to generate
kekoawong Apr 5, 2025
72e477d
Refactored the config tests
kekoawong Apr 5, 2025
416e237
OpenAI: added support details and model versions
yukinagae Apr 6, 2025
a79338b
Merge pull request #9 from kekoawong/yukinagae/add-openai-model-detai…
yukinagae Apr 6, 2025
44b9f9b
Edited openai live tests to use different genkit initialization
kekoawong Apr 6, 2025
9592379
Moved embeddings around
kekoawong Apr 6, 2025
4c4ed48
Migrated oai initialization out of embedder
kekoawong Apr 9, 2025
057f5ea
Merge pull request #10 from kekoawong/kekoawong/fina-refactors
kekoawong Apr 9, 2025
1cb9cb4
fix: PR feedback
xavidop Apr 9, 2025
adec34b
fix: forward options
xavidop Apr 9, 2025
d251bb3
fix: test
xavidop Apr 9, 2025
efc7376
Merge pull request #11 from kekoawong/xavier/alex-feedback
xavidop Apr 11, 2025
4a72211
added Anthropic models
yukinagae Apr 5, 2025
dd0a365
Set the Anthropic base URL
yukinagae Apr 12, 2025
472e587
Merge pull request #8 from kekoawong/yukinagae/add-anthropic-models
yukinagae Apr 12, 2025
59d666c
Added OpenAI's GPT4.1 family models (gpt-4.1, gpt-4.1-mini and gpt-4.…
yukinagae Apr 16, 2025
53d1c57
Merge pull request #12 from kekoawong/yukinagae/add-openai-model-gpt-4-1
yukinagae Apr 16, 2025
36e6551
Changed order of provider and name
Apr 16, 2025
edb1fb4
Migrated config types
Apr 17, 2025
24b52d3
Updated config tests to pass
Apr 17, 2025
7415974
Added simplified compatibility for openai plugin
Apr 17, 2025
f1a7cc6
Altered sample
Apr 17, 2025
a24139e
Altered to max output tokens
Apr 17, 2025
85cd5e1
Added failure of invalid config type
Apr 17, 2025
c532f0e
Added simple usage
Apr 17, 2025
6bf85b8
Minor doc edits
Apr 17, 2025
d0e79f3
Fixed typo
Apr 17, 2025
25f5d3f
Added basic documentation
kekoawong Apr 17, 2025
231530b
Added doc string with link
kekoawong Apr 17, 2025
adaeaf9
Added supported models to docs
kekoawong Apr 17, 2025
bb095b6
Added usage for struct
kekoawong Apr 17, 2025
f600cf3
Removed helper from readme
kekoawong Apr 23, 2025
33b8374
Removed constructor
kekoawong Apr 23, 2025
14ab55b
Added request options and collecting env api key
kekoawong Apr 23, 2025
3d00fbc
Edited doc strings
kekoawong Apr 23, 2025
69c0db4
Added generate with config functionality
kekoawong Apr 23, 2025
a3bcde8
Added passing unit tests
kekoawong Apr 23, 2025
d535ef5
Testing complete
kekoawong Apr 25, 2025
639c219
Update go/plugins/compat_oai/generate.go
kekoawong Apr 26, 2025
2a6378b
Changed name of config
kekoawong Apr 26, 2025
289fbb9
fix: adds genkit common config to openai plugin
kekoawong Apr 26, 2025
c910eb4
Changed order of provider and name
kekoawong Apr 16, 2025
852b23c
Migrated config types
kekoawong Apr 17, 2025
2716839
Updated config tests to pass
kekoawong Apr 17, 2025
5c48a7b
Added simplified compatibility for openai plugin
kekoawong Apr 17, 2025
4b4639e
Altered sample
kekoawong Apr 17, 2025
3413e21
Altered to max output tokens
kekoawong Apr 17, 2025
a27f7a2
Added failure of invalid config type
kekoawong Apr 17, 2025
0c5a00a
Added simple usage
kekoawong Apr 17, 2025
0e120fe
Minor doc edits
kekoawong Apr 17, 2025
6f91a7c
Fixed typo
kekoawong Apr 17, 2025
384965a
Added basic documentation
kekoawong Apr 17, 2025
25d1aeb
Added doc string with link
kekoawong Apr 17, 2025
a15dd09
Added supported models to docs
kekoawong Apr 17, 2025
875560c
Added usage for struct
kekoawong Apr 17, 2025
77eb94c
Removed helper from readme
kekoawong Apr 23, 2025
7c502a8
Removed constructor
kekoawong Apr 23, 2025
7e5f564
Added request options and collecting env api key
kekoawong Apr 23, 2025
27c5216
Edited doc strings
kekoawong Apr 23, 2025
3797f9c
Added generate with config functionality
kekoawong Apr 23, 2025
841fc7e
Added passing unit tests
kekoawong Apr 23, 2025
57f5b63
Testing complete
kekoawong Apr 25, 2025
fe638f2
Update go/plugins/compat_oai/generate.go
kekoawong Apr 26, 2025
3998953
Changed name of config
kekoawong Apr 26, 2025
8ee4b67
Merge pull request #14 from kekoawong/kekoawong/edit-commit-author
kekoawong Apr 26, 2025
57cebaa
Revert "fix: edited commit author for kekoawong"
kekoawong Apr 26, 2025
17bfb19
Merge pull request #15 from kekoawong/revert-14-kekoawong/edit-commit…
kekoawong Apr 26, 2025
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
15 changes: 13 additions & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/jba/slog v0.2.0
github.com/lib/pq v1.10.9
github.com/pgvector/pgvector-go v0.2.0
github.com/stretchr/testify v1.10.0
github.com/weaviate/weaviate v1.26.0-rc.1
github.com/weaviate/weaviate-go-client/v4 v4.15.0
github.com/xeipuuv/gojsonschema v1.2.0
Expand All @@ -37,6 +38,16 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

require (
cloud.google.com/go v0.116.0 // indirect
cloud.google.com/go/auth v0.9.3 // indirect
Expand All @@ -53,7 +64,7 @@ require (
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/blues/jsonata-go v1.5.4 // indirect
github.com/blues/jsonata-go v1.5.4
github.com/buger/jsonparser v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand All @@ -78,6 +89,7 @@ require (
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/openai/openai-go v0.1.0-alpha.65
github.com/pkg/errors v0.9.1 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand All @@ -98,5 +110,4 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
12 changes: 12 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJ
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
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 v0.1.0-alpha.65 h1:G12sA6OaL+cVMElMO3m5RVFwKhhg40kmGeGhaYZIoYw=
github.com/openai/openai-go v0.1.0-alpha.65/go.mod h1:3SdE6BffOX9HPEQv8IL/fi3LYZ5TUpRYaqGQZbyk11A=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pgvector/pgvector-go v0.2.0 h1:NZdW4NxUxdSCzaev3LVHb9ORf+LdX+uZOQVqQ6s2Zyg=
github.com/pgvector/pgvector-go v0.2.0/go.mod h1:OQpvU5QZGQOPI9quIXAyHaRZ5yGk/RGUDbs9C3DPUNE=
Expand Down Expand Up @@ -256,7 +258,17 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/uptrace/bun v1.1.12 h1:sOjDVHxNTuM6dNGaba0wUuz7KvDE1BmNu9Gqs2gJSXQ=
Expand Down
77 changes: 77 additions & 0 deletions go/plugins/compat_oai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# OpenAI-Compatible Plugin Package

This directory contains a package for building plugins that are compatible with the OpenAI API specification, along with plugins built on top of this package.

## Package Overview

The `compat_oai` package provides a base implementation (`OpenAICompatible`) that handles:
- Model and embedder registration
- Message handling
- Tool support
- Configuration management

## Usage Example

Here's how to implement a new OpenAI-compatible plugin:

```go
type MyPlugin struct {
compat_oai.OpenAICompatible
// define other plugin-specific fields
}

var (
supportedModels = map[string]ai.ModelInfo{
// define supported models
}
)

// Implement required methods
func (p *MyPlugin) Init(ctx context.Context, g *genkit.Genkit) error {
// initialize the plugin with the common compatible package
if err := p.OpenAICompatible.Init(ctx, g); err != nil {
return err
}

// Define plugin-specific models
for model, info := range supportedModels {
if _, err := p.DefineModel(g, p.Provider, model, info); err != nil {
return err
}
}

// Define embedders, if applicable

return nil
}

func (p *MyPlugin) Name() string {
return p.Provider
}
```

See the `openai` and `anthropic` directories for complete implementations.

## Running Tests

Set your API keys:
```bash
export OPENAI_API_KEY=<your-openai-key>
export ANTHROPIC_API_KEY=<your-anthropic-key>
```

Run all tests:
```bash
go test -v ./...
```

Run specific plugin tests:
```bash
# OpenAI tests
go test -v ./openai

# Anthropic tests
go test -v ./anthropic
```

Note: Tests will be skipped if the required API keys are not set.
53 changes: 53 additions & 0 deletions go/plugins/compat_oai/anthropic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Anthropic Plugin

This plugin provides a simple interface for using Anthropic's services.

## Prerequisites

- Go installed on your system
- An Anthropic API key

## Running Tests

First, set your Anthropic API key as an environment variable:

```bash
export ANTHROPIC_API_KEY=<your-api-key>
```

### Running All Tests
To run all tests in the directory:
```bash
go test -v .
```

### Running Tests from Specific Files
To run tests from a specific file:
```bash
# Run only generate_live_test.go tests
go test -run "^TestGenerator"

# Run only anthropic_live_test.go tests
go test -run "^TestPlugin"
```

### Running Individual Tests
To run a specific test case:
```bash
# Run only the streaming test from anthropic_live_test.go
go test -run "TestPlugin/streaming"

# Run only the Complete test from generate_live_test.go
go test -run "TestGenerator_Complete"

# Run only the Stream test from generate_live_test.go
go test -run "TestGenerator_Stream"
```

### Test Output Verbosity
Add the `-v` flag for verbose output:
```bash
go test -v -run "TestPlugin/streaming"
```

Note: All live tests require the ANTHROPIC_API_KEY environment variable to be set. Tests will be skipped if the API key is not provided.
123 changes: 123 additions & 0 deletions go/plugins/compat_oai/anthropic/anthropic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package anthropic

import (
"context"

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

const (
provider = "anthropic"
baseURL = "https://api.anthropic.com/v1"
)

var (
// Supported models: https://docs.anthropic.com/en/docs/about-claude/models/all-models
supportedModels = map[string]ai.ModelInfo{
"claude-3-7-sonnet-20250219": {
Label: "Claude 3.7 Sonnet",
Supports: &ai.ModelSupports{
Multiturn: true,
Tools: false, // NOTE: Anthropic supports tool use, but it's not compatible with the OpenAI API
SystemRole: true,
Media: true,
},
Versions: []string{"claude-3-7-sonnet-latest", "claude-3-7-sonnet-20250219"},
},
"claude-3-5-haiku-20241022": {
Label: "Claude 3.5 Haiku",
Supports: &ai.ModelSupports{
Multiturn: true,
Tools: false, // NOTE: Anthropic supports tool use, but it's not compatible with the OpenAI API
SystemRole: true,
Media: true,
},
Versions: []string{"claude-3-5-haiku-latest", "claude-3-5-haiku-20241022"},
},
"claude-3-5-sonnet-20240620": {
Label: "Claude 3.5 Sonnet",
Supports: &ai.ModelSupports{
Multiturn: true,
Tools: false, // NOTE: Anthropic supports tool use, but it's not compatible with the OpenAI API
SystemRole: false, // NOTE: This model does not support system role
Media: true,
},
Versions: []string{"claude-3-5-sonnet-20240620"},
},
"claude-3-opus-20240229": {
Label: "Claude 3 Opus",
Supports: &ai.ModelSupports{
Multiturn: true,
Tools: false, // NOTE: Anthropic supports tool use, but it's not compatible with the OpenAI API
SystemRole: false, // NOTE: This model does not support system role
Media: true,
},
Versions: []string{"claude-3-opus-latest", "claude-3-opus-20240229"},
},
"claude-3-haiku-20240307": {
Label: "Claude 3 Haiku",
Supports: &ai.ModelSupports{
Multiturn: true,
Tools: false, // NOTE: Anthropic supports tool use, but it's not compatible with the OpenAI API
SystemRole: false, // NOTE: This model does not support system role
Media: true,
},
Versions: []string{"claude-3-haiku-20240307"},
},
}
)

type Anthropic struct {
Opts []option.RequestOption
openAICompatible compat_oai.OpenAICompatible
}

// Name implements genkit.Plugin.
func (a *Anthropic) Name() string {
return provider
}

func (a *Anthropic) Init(ctx context.Context, g *genkit.Genkit) error {
// Set the base URL
a.Opts = append(a.Opts, option.WithBaseURL(baseURL))

// initialize OpenAICompatible
a.openAICompatible.Opts = a.Opts
if err := a.openAICompatible.Init(ctx, g); err != nil {
return err
}

// define default models
for model, info := range supportedModels {
if _, err := a.DefineModel(g, model, info); err != nil {
return err
}
}

return nil
}

func (a *Anthropic) Model(g *genkit.Genkit, name string) ai.Model {
return a.openAICompatible.Model(g, name, provider)
}

func (a *Anthropic) DefineModel(g *genkit.Genkit, name string, info ai.ModelInfo) (ai.Model, error) {
return a.openAICompatible.DefineModel(g, provider, name, info)
}
Loading