You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library provides unofficial Go clients for [OpenAI API](https://platform.openai.com/). We support:
6
+
This library provides unofficial Go clients for [OpenAI API](https://platform.openai.com/). We support:
7
7
8
8
* ChatGPT 4o, o1
9
9
* GPT-3, GPT-4
@@ -720,7 +720,7 @@ if errors.As(err, &e) {
720
720
case 401:
721
721
// invalid auth or key (do not retry)
722
722
case 429:
723
-
// rate limiting or engine overload (wait and retry)
723
+
// rate limiting or engine overload (wait and retry)
724
724
case 500:
725
725
// openai server error (retry)
726
726
default:
@@ -867,6 +867,58 @@ func main() {
867
867
}
868
868
```
869
869
</details>
870
+
871
+
<details>
872
+
<summary>Using ExtraFields</summary>
873
+
874
+
```go
875
+
package main
876
+
877
+
import (
878
+
"context"
879
+
"fmt"
880
+
openai "github.com/sashabaranov/go-openai"
881
+
)
882
+
883
+
funcmain() {
884
+
client:= openai.NewClient("your token")
885
+
ctx:= context.Background()
886
+
887
+
// Create chat request
888
+
req:= openai.ChatCompletionRequest{
889
+
Model: openai.GPT3Dot5Turbo,
890
+
Messages: []openai.ChatCompletionMessage{
891
+
{
892
+
Role: openai.ChatMessageRoleUser,
893
+
Content: "Hello!",
894
+
},
895
+
},
896
+
}
897
+
898
+
// Add custom fields
899
+
extraFields:=map[string]any{
900
+
"custom_field": "test_value",
901
+
"numeric_field": 42,
902
+
"bool_field": true,
903
+
}
904
+
req.SetExtraFields(extraFields)
905
+
906
+
// Get custom fields
907
+
gotFields:= req.GetExtraFields()
908
+
fmt.Printf("Extra fields: %v\n", gotFields)
909
+
910
+
// Send request
911
+
resp, err:= client.CreateChatCompletion(ctx, req)
912
+
if err != nil {
913
+
fmt.Printf("ChatCompletion error: %v\n", err)
914
+
return
915
+
}
916
+
917
+
fmt.Println(resp.Choices[0].Message.Content)
918
+
}
919
+
```
920
+
</details>
921
+
870
922
See the `examples/` folder for more.
871
923
872
924
## Frequently Asked Questions
@@ -887,18 +939,18 @@ Due to the factors mentioned above, different answers may be returned even for t
887
939
888
940
By adopting these strategies, you can expect more consistent results.
889
941
890
-
**Related Issues:**
942
+
**Related Issues:**
891
943
[omitempty option of request struct will generate incorrect request when parameter is 0.](https://github.com/sashabaranov/go-openai/issues/9)
892
944
893
945
### Does Go OpenAI provide a method to count tokens?
894
946
895
947
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.
896
948
897
-
For counting tokens, you might find the following links helpful:
949
+
For counting tokens, you might find the following links helpful:
898
950
-[Counting Tokens For Chat API Calls](https://github.com/pkoukk/tiktoken-go#counting-tokens-for-chat-api-calls)
899
951
-[How to count tokens with tiktoken](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb)
900
952
901
-
**Related Issues:**
953
+
**Related Issues:**
902
954
[Is it possible to join the implementation of GPT3 Tokenizer](https://github.com/sashabaranov/go-openai/issues/62)
0 commit comments