0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
// Package core provides OpenAI API integration with support for reasoning effort control.
|
|
package core
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/promptfoo/promptfoo/examples/golang-provider/pkg1"
|
|
"github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
// Client wraps the OpenAI API client with custom functionality for reasoning control.
|
|
// It provides a simplified interface for making chat completion requests with
|
|
// configurable reasoning effort levels.
|
|
type Client struct {
|
|
api *openai.Client
|
|
}
|
|
|
|
// NewClient creates a new OpenAI client using the API key from OPENAI_API_KEY
|
|
// environment variable. Returns a Client configured with default settings.
|
|
func NewClient() *Client {
|
|
return &Client{
|
|
api: openai.NewClient(os.Getenv("OPENAI_API_KEY")),
|
|
}
|
|
}
|
|
|
|
// CreateCompletion generates a chat completion with reasoning effort control.
|
|
// It takes a prompt string and a reasoningEffort level ("low", "medium", "high")
|
|
// and returns the model's response as a string.
|
|
//
|
|
// The reasoning effort parameter controls how much computation the model spends
|
|
// on analyzing and solving the problem. Higher effort may result in more thorough
|
|
// or accurate responses at the cost of increased latency.
|
|
//
|
|
// Returns an error if the API call fails or if the response is invalid.
|
|
func (c *Client) CreateCompletion(prompt string, reasoningEffort string) (string, error) {
|
|
resp, err := c.api.CreateChatCompletion(
|
|
context.Background(),
|
|
openai.ChatCompletionRequest{
|
|
Model: pkg1.GetModel(),
|
|
Messages: []openai.ChatCompletionMessage{
|
|
{
|
|
Role: openai.ChatMessageRoleUser,
|
|
Content: prompt,
|
|
},
|
|
},
|
|
ReasoningEffort: reasoningEffort,
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return "", fmt.Errorf("chat completion error: %v", err)
|
|
}
|
|
|
|
return resp.Choices[0].Message.Content, nil
|
|
}
|