Skip to content
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

Generic OpenAI API provider #112

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
22 changes: 2 additions & 20 deletions provider/ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ollama

import (
"context"
"fmt"
"net/url"
"strings"

Expand All @@ -13,6 +12,7 @@ import (
"github.com/symflower/eval-dev-quality/model"
"github.com/symflower/eval-dev-quality/model/llm"
"github.com/symflower/eval-dev-quality/provider"
openaiapi "github.com/symflower/eval-dev-quality/provider/openai-api"
"github.com/symflower/eval-dev-quality/tools"
)

Expand Down Expand Up @@ -85,25 +85,7 @@ func (p *Provider) Query(ctx context.Context, modelIdentifier string, promptText
client := p.client()
modelIdentifier = strings.TrimPrefix(modelIdentifier, p.ID()+provider.ProviderModelSeparator)

apiResponse, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: modelIdentifier,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: promptText,
},
},
},
)
if err != nil {
return "", pkgerrors.WithStack(err)
} else if len(apiResponse.Choices) == 0 {
return "", pkgerrors.WithStack(fmt.Errorf("empty LLM %q response: %+v", modelIdentifier, apiResponse))
}

return apiResponse.Choices[0].Message.Content, nil
return openaiapi.QueryOpenAIAPIModel(ctx, client, modelIdentifier, promptText)
}

// client returns a new client with the current configuration.
Expand Down
22 changes: 1 addition & 21 deletions provider/openai-api/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package openaiapi

import (
"context"
"fmt"
"strings"

pkgerrors "github.com/pkg/errors"
"github.com/sashabaranov/go-openai"

"github.com/symflower/eval-dev-quality/log"
Expand Down Expand Up @@ -66,25 +64,7 @@ func (p *Provider) Query(ctx context.Context, modelIdentifier string, promptText
client := p.client()
modelIdentifier = strings.TrimPrefix(modelIdentifier, p.ID()+provider.ProviderModelSeparator)

apiResponse, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: modelIdentifier,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: promptText,
},
},
},
)
if err != nil {
return "", pkgerrors.WithStack(err)
} else if len(apiResponse.Choices) == 0 {
return "", pkgerrors.WithStack(fmt.Errorf("empty LLM %q response: %+v", modelIdentifier, apiResponse))
}

return apiResponse.Choices[0].Message.Content, nil
return QueryOpenAIAPIModel(ctx, client, modelIdentifier, promptText)
}

// client returns a new client with the current configuration.
Expand Down
32 changes: 32 additions & 0 deletions provider/openai-api/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package openaiapi

import (
"context"
"fmt"

pkgerrors "github.com/pkg/errors"
"github.com/sashabaranov/go-openai"
)

// QueryOpenAIModel queries an OpenAI API model.
func QueryOpenAIAPIModel(ctx context.Context, client *openai.Client, modelIdentifier string, promptText string) (response string, err error) {
Munsio marked this conversation as resolved.
Show resolved Hide resolved
apiResponse, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: modelIdentifier,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: promptText,
},
},
},
)
if err != nil {
return "", pkgerrors.WithStack(err)
} else if len(apiResponse.Choices) == 0 {
return "", pkgerrors.WithStack(fmt.Errorf("empty LLM %q response: %+v", modelIdentifier, apiResponse))
}

return apiResponse.Choices[0].Message.Content, nil
}
22 changes: 2 additions & 20 deletions provider/openrouter/openrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package openrouter
import (
"context"
"errors"
"fmt"
"strings"

pkgerrors "github.com/pkg/errors"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/symflower/eval-dev-quality/model"
"github.com/symflower/eval-dev-quality/model/llm"
"github.com/symflower/eval-dev-quality/provider"
openaiapi "github.com/symflower/eval-dev-quality/provider/openai-api"
)

// Provider holds an "openrouter.ai" provider using its public REST API.
Expand Down Expand Up @@ -79,25 +79,7 @@ func (p *Provider) Query(ctx context.Context, modelIdentifier string, promptText
client := p.client()
modelIdentifier = strings.TrimPrefix(modelIdentifier, p.ID()+provider.ProviderModelSeparator)

apiResponse, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: modelIdentifier,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: promptText,
},
},
},
)
if err != nil {
return "", pkgerrors.WithStack(err)
} else if len(apiResponse.Choices) == 0 {
return "", pkgerrors.WithStack(fmt.Errorf("empty LLM %q response: %+v", modelIdentifier, apiResponse))
}

return apiResponse.Choices[0].Message.Content, nil
return openaiapi.QueryOpenAIAPIModel(ctx, client, modelIdentifier, promptText)
}

// client returns a new client with the current configuration.
Expand Down
Loading