Skip to content

Feat/wdz/sync master #1052

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
Show file tree
Hide file tree
Changes from all commits
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
62 changes: 57 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/sashabaranov/go-openai)](https://goreportcard.com/report/github.com/sashabaranov/go-openai)
[![codecov](https://codecov.io/gh/sashabaranov/go-openai/branch/master/graph/badge.svg?token=bCbIfHLIsW)](https://codecov.io/gh/sashabaranov/go-openai)

This library provides unofficial Go clients for [OpenAI API](https://platform.openai.com/). We support:
This library provides unofficial Go clients for [OpenAI API](https://platform.openai.com/). We support:

* ChatGPT 4o, o1
* GPT-3, GPT-4
Expand Down Expand Up @@ -720,7 +720,7 @@ if errors.As(err, &e) {
case 401:
// invalid auth or key (do not retry)
case 429:
// rate limiting or engine overload (wait and retry)
// rate limiting or engine overload (wait and retry)
case 500:
// openai server error (retry)
default:
Expand Down Expand Up @@ -867,6 +867,58 @@ func main() {
}
```
</details>

<details>
<summary>Using ExtraFields</summary>

```go
package main

import (
"context"
"fmt"
openai "github.com/sashabaranov/go-openai"
)

func main() {
client := openai.NewClient("your token")
ctx := context.Background()

// Create chat request
req := openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Hello!",
},
},
}

// Add custom fields
extraFields := map[string]any{
"custom_field": "test_value",
"numeric_field": 42,
"bool_field": true,
}
req.SetExtraFields(extraFields)

// Get custom fields
gotFields := req.GetExtraFields()
fmt.Printf("Extra fields: %v\n", gotFields)

// Send request
resp, err := client.CreateChatCompletion(ctx, req)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return
}

fmt.Println(resp.Choices[0].Message.Content)
}
```
</details>

See the `examples/` folder for more.

## Frequently Asked Questions
Expand All @@ -887,18 +939,18 @@ Due to the factors mentioned above, different answers may be returned even for t

By adopting these strategies, you can expect more consistent results.

**Related Issues:**
**Related Issues:**
[omitempty option of request struct will generate incorrect request when parameter is 0.](https://github.com/sashabaranov/go-openai/issues/9)

### Does Go OpenAI provide a method to count tokens?

No, Go OpenAI does not offer a feature to count tokens, and there are no plans to provide such a feature in the future. However, if there's a way to implement a token counting feature with zero dependencies, it might be possible to merge that feature into Go OpenAI. Otherwise, it would be more appropriate to implement it in a dedicated library or repository.

For counting tokens, you might find the following links helpful:
For counting tokens, you might find the following links helpful:
- [Counting Tokens For Chat API Calls](https://github.com/pkoukk/tiktoken-go#counting-tokens-for-chat-api-calls)
- [How to count tokens with tiktoken](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb)

**Related Issues:**
**Related Issues:**
[Is it possible to join the implementation of GPT3 Tokenizer](https://github.com/sashabaranov/go-openai/issues/62)

## Contributing
Expand Down
6 changes: 3 additions & 3 deletions api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"os"
"testing"

"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test/checks"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/meguminnnnnnnnn/go-openai"
"github.com/meguminnnnnnnnn/go-openai/internal/test/checks"
"github.com/meguminnnnnnnnn/go-openai/jsonschema"
)

func TestAPI(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions assistant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package openai_test
import (
"context"

openai "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test/checks"
openai "github.com/meguminnnnnnnnn/go-openai"
"github.com/meguminnnnnnnnn/go-openai/internal/test/checks"

"encoding/json"
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"os"

utils "github.com/sashabaranov/go-openai/internal"
utils "github.com/meguminnnnnnnnn/go-openai/internal"
)

// Whisper Defines the models provided by OpenAI to use when processing audio with OpenAI.
Expand Down
6 changes: 3 additions & 3 deletions audio_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"strings"
"testing"

"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"
"github.com/meguminnnnnnnnn/go-openai"
"github.com/meguminnnnnnnnn/go-openai/internal/test"
"github.com/meguminnnnnnnnn/go-openai/internal/test/checks"
)

// TestAudio Tests the transcription and translation endpoints of the API using the mocked server.
Expand Down
6 changes: 3 additions & 3 deletions audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"path/filepath"
"testing"

utils "github.com/sashabaranov/go-openai/internal"
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"
utils "github.com/meguminnnnnnnnn/go-openai/internal"
"github.com/meguminnnnnnnnn/go-openai/internal/test"
"github.com/meguminnnnnnnnn/go-openai/internal/test/checks"
)

func TestAudioWithFailingFormBuilder(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"reflect"
"testing"

"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test/checks"
"github.com/meguminnnnnnnnn/go-openai"
"github.com/meguminnnnnnnnn/go-openai/internal/test/checks"
)

func TestUploadBatchFile(t *testing.T) {
Expand Down
43 changes: 35 additions & 8 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"errors"
"net/http"

"github.com/sashabaranov/go-openai/jsonschema"

Check failure on line 9 in chat.go

View workflow job for this annotation

GitHub Actions / Sanity check

no required module provides package github.com/sashabaranov/go-openai/jsonschema; to add it:
)

// Chat message role defined by the OpenAI API.
Expand Down Expand Up @@ -81,17 +81,30 @@
Detail ImageURLDetail `json:"detail,omitempty"`
}

type ChatMessageInputAudio struct {
Data string `json:"data,omitempty"`
Format string `json:"format,omitempty"`
}

type ChatMessageVideoURL struct {
URL string `json:"url,omitempty"`
}

type ChatMessagePartType string

const (
ChatMessagePartTypeText ChatMessagePartType = "text"
ChatMessagePartTypeImageURL ChatMessagePartType = "image_url"
ChatMessagePartTypeText ChatMessagePartType = "text"
ChatMessagePartTypeImageURL ChatMessagePartType = "image_url"
ChatMessagePartTypeInputAudio ChatMessagePartType = "input_audio"
ChatMessagePartTypeVideoURL ChatMessagePartType = "video_url"
)

type ChatMessagePart struct {
Type ChatMessagePartType `json:"type,omitempty"`
Text string `json:"text,omitempty"`
ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
Type ChatMessagePartType `json:"type,omitempty"`
Text string `json:"text,omitempty"`
ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
InputAudio *ChatMessageInputAudio `json:"input_audio,omitempty"`
VideoURL *ChatMessageVideoURL `json:"video_url,omitempty"`
}

type ChatCompletionMessage struct {
Expand Down Expand Up @@ -271,7 +284,7 @@
// MaxCompletionTokens An upper bound for the number of tokens that can be generated for a completion,
// including visible output tokens and reasoning tokens https://platform.openai.com/docs/guides/reasoning
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
Temperature *float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
Stream bool `json:"stream,omitempty"`
Expand Down Expand Up @@ -320,8 +333,22 @@
ChatTemplateKwargs map[string]any `json:"chat_template_kwargs,omitempty"`
// Specifies the latency tier to use for processing the request.
ServiceTier ServiceTier `json:"service_tier,omitempty"`
// Embedded struct for non-OpenAI extensions
ChatCompletionRequestExtensions
// Extra fields to be sent in the request.
// Useful for experimental features not yet officially supported.
extraFields map[string]any
}

// SetExtraFields adds extra fields to the JSON object.
//
// SetExtraFields will override any existing fields with the same key.
// For security reasons, ensure this is only used with trusted input data.
func (r *ChatCompletionRequest) SetExtraFields(extraFields map[string]any) {
r.extraFields = extraFields
}

// GetExtraFields returns the extra fields set in the request.
func (r ChatCompletionRequest) GetExtraFields() map[string]any {
return r.extraFields
}

type StreamOptions struct {
Expand Down
4 changes: 2 additions & 2 deletions chat_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strconv"
"testing"

"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test/checks"
"github.com/meguminnnnnnnnn/go-openai"
"github.com/meguminnnnnnnnn/go-openai/internal/test/checks"
)

func TestChatCompletionsStreamWrongModel(t *testing.T) {
Expand Down
Loading
Loading