Repository: sashabaranov/go-openai Branch: master Commit: 5d7a276f4c0e Files: 105 Total size: 529.2 KB Directory structure: gitextract_r1hjdmvj/ ├── .codecov.yml ├── .github/ │ ├── FUNDING.yml │ ├── ISSUE_TEMPLATE/ │ │ ├── bug_report.md │ │ └── feature_request.md │ ├── PULL_REQUEST_TEMPLATE.md │ └── workflows/ │ ├── close-inactive-issues.yml │ ├── integration-tests.yml │ └── pr.yml ├── .gitignore ├── .golangci.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── api_integration_test.go ├── api_internal_test.go ├── assistant.go ├── assistant_test.go ├── audio.go ├── audio_api_test.go ├── audio_test.go ├── batch.go ├── batch_test.go ├── chat.go ├── chat_stream.go ├── chat_stream_test.go ├── chat_test.go ├── client.go ├── client_test.go ├── common.go ├── completion.go ├── completion_test.go ├── config.go ├── config_test.go ├── edits.go ├── edits_test.go ├── embeddings.go ├── embeddings_test.go ├── engines.go ├── engines_test.go ├── error.go ├── error_test.go ├── example_test.go ├── examples/ │ ├── README.md │ ├── chatbot/ │ │ └── main.go │ ├── completion/ │ │ └── main.go │ ├── completion-with-tool/ │ │ └── main.go │ ├── images/ │ │ └── main.go │ └── voice-to-text/ │ └── main.go ├── files.go ├── files_api_test.go ├── files_test.go ├── fine_tunes.go ├── fine_tunes_test.go ├── fine_tuning_job.go ├── fine_tuning_job_test.go ├── go.mod ├── image.go ├── image_api_test.go ├── image_test.go ├── internal/ │ ├── error_accumulator.go │ ├── error_accumulator_test.go │ ├── form_builder.go │ ├── form_builder_test.go │ ├── marshaller.go │ ├── marshaller_test.go │ ├── request_builder.go │ ├── request_builder_test.go │ ├── test/ │ │ ├── checks/ │ │ │ ├── checks.go │ │ │ └── checks_test.go │ │ ├── failer.go │ │ ├── failer_test.go │ │ ├── helpers.go │ │ ├── helpers_test.go │ │ ├── server.go │ │ └── server_test.go │ ├── unmarshaler.go │ └── unmarshaler_test.go ├── jsonschema/ │ ├── containsref_test.go │ ├── json.go │ ├── json_additional_test.go │ ├── json_errors_test.go │ ├── json_test.go │ ├── validate.go │ └── validate_test.go ├── messages.go ├── messages_test.go ├── models.go ├── models_test.go ├── moderation.go ├── moderation_test.go ├── openai_test.go ├── ratelimit.go ├── reasoning_validator.go ├── run.go ├── run_test.go ├── speech.go ├── speech_test.go ├── stream.go ├── stream_reader.go ├── stream_reader_test.go ├── stream_test.go ├── thread.go ├── thread_test.go ├── vector_store.go └── vector_store_test.go ================================================ FILE CONTENTS ================================================ ================================================ FILE: .codecov.yml ================================================ coverage: ignore: - "examples/**" - "internal/test/**" ================================================ FILE: .github/FUNDING.yml ================================================ # These are supported funding model platforms github: [sashabaranov, vvatanabe] ================================================ FILE: .github/ISSUE_TEMPLATE/bug_report.md ================================================ --- name: Bug report about: Create a report to help us improve title: '' labels: bug assignees: '' --- Your issue may already be reported! Please search on the [issue tracker](https://github.com/sashabaranov/go-openai/issues) before creating one. **Describe the bug** A clear and concise description of what the bug is. If it's an API-related bug, please provide relevant endpoint(s). **To Reproduce** Steps to reproduce the behavior, including any relevant code snippets. **Expected behavior** A clear and concise description of what you expected to happen. **Screenshots/Logs** If applicable, add screenshots to help explain your problem. For non-graphical issues, please provide any relevant logs or stack traces. **Environment (please complete the following information):** - go-openai version: [e.g. v1.12.0] - Go version: [e.g. 1.18] - OpenAI API version: [e.g. v1] - OS: [e.g. Ubuntu 20.04] **Additional context** Add any other context about the problem here. ================================================ FILE: .github/ISSUE_TEMPLATE/feature_request.md ================================================ --- name: Feature request about: Suggest an idea for this project title: '' labels: enhancement assignees: '' --- Your issue may already be reported! Please search on the [issue tracker](https://github.com/sashabaranov/go-openai/issues) before creating one. **Is your feature request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Describe the solution you'd like** A clear and concise description of what you want to happen. **Describe alternatives you've considered** A clear and concise description of any alternative solutions or features you've considered. **Additional context** Add any other context or screenshots about the feature request here. ================================================ FILE: .github/PULL_REQUEST_TEMPLATE.md ================================================ A similar PR may already be submitted! Please search among the [Pull request](https://github.com/sashabaranov/go-openai/pulls) before creating one. If your changes introduce breaking changes, please prefix the title of your pull request with "[BREAKING_CHANGES]". This allows for clear identification of such changes in the 'What's Changed' section on the release page, making it developer-friendly. Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. **Describe the change** Please provide a clear and concise description of the changes you're proposing. Explain what problem it solves or what feature it adds. **Provide OpenAI documentation link** Provide a relevant API doc from https://platform.openai.com/docs/api-reference **Describe your solution** Describe how your changes address the problem or how they add the feature. This should include a brief description of your approach and any new libraries or dependencies you're using. **Tests** Briefly describe how you have tested these changes. If possible — please add integration tests. **Additional context** Add any other context or screenshots or logs about your pull request here. If the pull request relates to an open issue, please link to it. Issue: #XXXX ================================================ FILE: .github/workflows/close-inactive-issues.yml ================================================ name: Close inactive issues on: schedule: - cron: "30 1 * * *" jobs: close-issues: runs-on: ubuntu-latest permissions: issues: write pull-requests: write steps: - uses: actions/stale@v9 with: days-before-issue-stale: 30 days-before-issue-close: 14 stale-issue-label: "stale" exempt-issue-labels: 'bug,enhancement' stale-issue-message: "This issue is stale because it has been open for 30 days with no activity." close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale." days-before-pr-stale: -1 days-before-pr-close: -1 repo-token: ${{ secrets.GITHUB_TOKEN }} ================================================ FILE: .github/workflows/integration-tests.yml ================================================ name: Integration tests on: push: branches: - master jobs: integration_tests: name: Run integration tests runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v5 with: go-version: '1.21' - name: Run integration tests env: OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }} run: go test -v -tags=integration ./api_integration_test.go ================================================ FILE: .github/workflows/pr.yml ================================================ name: Sanity check on: - push - pull_request jobs: prcheck: name: Sanity check runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v5 with: go-version: '1.24' - name: Run vet run: | go vet -stdversion ./... - name: Run golangci-lint uses: golangci/golangci-lint-action@v7 with: version: v2.1.5 - name: Run tests run: go test -race -covermode=atomic -coverprofile=coverage.out -v ./... - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v5 with: token: ${{ secrets.CODECOV_TOKEN }} ================================================ FILE: .gitignore ================================================ # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ # Auth token for tests .openai-token .idea # Generated by tests test.mp3 ================================================ FILE: .golangci.yml ================================================ version: "2" linters: default: none enable: - asciicheck - bidichk - bodyclose - contextcheck - cyclop - dupl - durationcheck - errcheck - errname - errorlint - exhaustive - forbidigo - funlen - gochecknoinits - gocognit - goconst - gocritic - gocyclo - godot - gomoddirectives - gomodguard - goprintffuncname - gosec - govet - ineffassign - lll - makezero - mnd - nestif - nilerr - nilnil - nolintlint - nosprintfhostport - predeclared - promlinter - revive - rowserrcheck - sqlclosecheck - staticcheck - testpackage - tparallel - unconvert - unparam - unused - usetesting - wastedassign - whitespace settings: cyclop: max-complexity: 30 package-average: 10 errcheck: check-type-assertions: true funlen: lines: 100 statements: 50 gocognit: min-complexity: 20 gocritic: settings: captLocal: paramsOnly: false underef: skipRecvDeref: false gomodguard: blocked: modules: - github.com/golang/protobuf: recommendations: - google.golang.org/protobuf reason: see https://developers.google.com/protocol-buffers/docs/reference/go/faq#modules - github.com/satori/go.uuid: recommendations: - github.com/google/uuid reason: satori's package is not maintained - github.com/gofrs/uuid: recommendations: - github.com/google/uuid reason: 'see recommendation from dev-infra team: https://confluence.gtforge.com/x/gQI6Aw' govet: disable: - fieldalignment enable-all: true settings: shadow: strict: true mnd: ignored-functions: - os.Chmod - os.Mkdir - os.MkdirAll - os.OpenFile - os.WriteFile - prometheus.ExponentialBuckets - prometheus.ExponentialBucketsRange - prometheus.LinearBuckets - strconv.FormatFloat - strconv.FormatInt - strconv.FormatUint - strconv.ParseFloat - strconv.ParseInt - strconv.ParseUint nakedret: max-func-lines: 0 nolintlint: require-explanation: true require-specific: true allow-no-explanation: - funlen - gocognit - lll rowserrcheck: packages: - github.com/jmoiron/sqlx exclusions: generated: lax presets: - comments - common-false-positives - legacy - std-error-handling rules: - linters: - forbidigo - mnd - revive path : ^examples/.*\.go$ - linters: - lll source: ^//\s*go:generate\s - linters: - godot source: (noinspection|TODO) - linters: - gocritic source: //noinspection - linters: - errorlint source: ^\s+if _, ok := err\.\([^.]+\.InternalError\); ok { - linters: - bodyclose - dupl - funlen - goconst - gosec - noctx - wrapcheck - staticcheck path: _test\.go paths: - third_party$ - builtin$ - examples$ issues: max-same-issues: 50 formatters: enable: - goimports exclusions: generated: lax paths: - third_party$ - builtin$ - examples$ ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing Guidelines ## Overview Thank you for your interest in contributing to the "Go OpenAI" project! By following this guideline, we hope to ensure that your contributions are made smoothly and efficiently. The Go OpenAI project is licensed under the [Apache 2.0 License](https://github.com/sashabaranov/go-openai/blob/master/LICENSE), and we welcome contributions through GitHub pull requests. ## Reporting Bugs If you discover a bug, first check the [GitHub Issues page](https://github.com/sashabaranov/go-openai/issues) to see if the issue has already been reported. If you're reporting a new issue, please use the "Bug report" template and provide detailed information about the problem, including steps to reproduce it. ## Suggesting Features If you want to suggest a new feature or improvement, first check the [GitHub Issues page](https://github.com/sashabaranov/go-openai/issues) to ensure a similar suggestion hasn't already been made. Use the "Feature request" template to provide a detailed description of your suggestion. ## Reporting Vulnerabilities If you identify a security concern, please use the "Report a security vulnerability" template on the [GitHub Issues page](https://github.com/sashabaranov/go-openai/issues) to share the details. This report will only be viewable to repository maintainers. You will be credited if the advisory is published. ## Questions for Users If you have questions, please utilize [StackOverflow](https://stackoverflow.com/) or the [GitHub Discussions page](https://github.com/sashabaranov/go-openai/discussions). ## Contributing Code There might already be a similar pull requests submitted! Please search for [pull requests](https://github.com/sashabaranov/go-openai/pulls) before creating one. ### Requirements for Merging a Pull Request The requirements to accept a pull request are as follows: - Features not provided by the OpenAI API will not be accepted. - The functionality of the feature must match that of the official OpenAI API. - All pull requests should be written in Go according to common conventions, formatted with `goimports`, and free of warnings from tools like `golangci-lint`. - Include tests and ensure all tests pass. - Maintain test coverage without any reduction. - All pull requests require approval from at least one Go OpenAI maintainer. **Note:** The merging method for pull requests in this repository is squash merge. ### Creating a Pull Request - Fork the repository. - Create a new branch and commit your changes. - Push that branch to GitHub. - Start a new Pull Request on GitHub. (Please use the pull request template to provide detailed information.) **Note:** If your changes introduce breaking changes, please prefix your pull request title with "[BREAKING_CHANGES]". ### Code Style In this project, we adhere to the standard coding style of Go. Your code should maintain consistency with the rest of the codebase. To achieve this, please format your code using tools like `goimports` and resolve any syntax or style issues with `golangci-lint`. **Run goimports:** ``` go install golang.org/x/tools/cmd/goimports@latest ``` ``` goimports -w . ``` **Run golangci-lint:** ``` go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest ``` ``` golangci-lint run --out-format=github-actions ``` ### Unit Test Please create or update tests relevant to your changes. Ensure all tests run successfully to verify that your modifications do not adversely affect other functionalities. **Run test:** ``` go test -v ./... ``` ### Integration Test Integration tests are requested against the production version of the OpenAI API. These tests will verify that the library is properly coded against the actual behavior of the API, and will fail upon any incompatible change in the API. **Notes:** These tests send real network traffic to the OpenAI API and may reach rate limits. Temporary network problems may also cause the test to fail. **Run integration test:** ``` OPENAI_TOKEN=XXX go test -v -tags=integration ./api_integration_test.go ``` If the `OPENAI_TOKEN` environment variable is not available, integration tests will be skipped. --- We wholeheartedly welcome your active participation. Let's build an amazing project together! ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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. ================================================ FILE: README.md ================================================ # Go OpenAI [![Go Reference](https://pkg.go.dev/badge/github.com/sashabaranov/go-openai.svg)](https://pkg.go.dev/github.com/sashabaranov/go-openai) [![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: * ChatGPT 4o, o1 * GPT-3, GPT-4 * DALL·E 2, DALL·E 3, GPT Image 1 * Whisper ## Installation ``` go get github.com/sashabaranov/go-openai ``` Currently, go-openai requires Go version 1.18 or greater. ## Usage ### ChatGPT example usage: ```go package main import ( "context" "fmt" openai "github.com/sashabaranov/go-openai" ) func main() { client := openai.NewClient("your token") resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) return } fmt.Println(resp.Choices[0].Message.Content) } ``` ### Getting an OpenAI API Key: 1. Visit the OpenAI website at [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys). 2. If you don't have an account, click on "Sign Up" to create one. If you do, click "Log In". 3. Once logged in, navigate to your API key management page. 4. Click on "Create new secret key". 5. Enter a name for your new key, then click "Create secret key". 6. Your new API key will be displayed. Use this key to interact with the OpenAI API. **Note:** Your API key is sensitive information. Do not share it with anyone. ### Other examples:
ChatGPT streaming completion ```go package main import ( "context" "errors" "fmt" "io" openai "github.com/sashabaranov/go-openai" ) func main() { c := openai.NewClient("your token") ctx := context.Background() req := openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, MaxTokens: 20, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Lorem ipsum", }, }, Stream: true, } stream, err := c.CreateChatCompletionStream(ctx, req) if err != nil { fmt.Printf("ChatCompletionStream error: %v\n", err) return } defer stream.Close() fmt.Printf("Stream response: ") for { response, err := stream.Recv() if errors.Is(err, io.EOF) { fmt.Println("\nStream finished") return } if err != nil { fmt.Printf("\nStream error: %v\n", err) return } fmt.Printf(response.Choices[0].Delta.Content) } } ```
GPT-3 completion ```go package main import ( "context" "fmt" openai "github.com/sashabaranov/go-openai" ) func main() { c := openai.NewClient("your token") ctx := context.Background() req := openai.CompletionRequest{ Model: openai.GPT3Babbage002, MaxTokens: 5, Prompt: "Lorem ipsum", } resp, err := c.CreateCompletion(ctx, req) if err != nil { fmt.Printf("Completion error: %v\n", err) return } fmt.Println(resp.Choices[0].Text) } ```
GPT-3 streaming completion ```go package main import ( "errors" "context" "fmt" "io" openai "github.com/sashabaranov/go-openai" ) func main() { c := openai.NewClient("your token") ctx := context.Background() req := openai.CompletionRequest{ Model: openai.GPT3Babbage002, MaxTokens: 5, Prompt: "Lorem ipsum", Stream: true, } stream, err := c.CreateCompletionStream(ctx, req) if err != nil { fmt.Printf("CompletionStream error: %v\n", err) return } defer stream.Close() for { response, err := stream.Recv() if errors.Is(err, io.EOF) { fmt.Println("Stream finished") return } if err != nil { fmt.Printf("Stream error: %v\n", err) return } fmt.Printf("Stream response: %v\n", response) } } ```
Audio Speech-To-Text ```go package main import ( "context" "fmt" openai "github.com/sashabaranov/go-openai" ) func main() { c := openai.NewClient("your token") ctx := context.Background() req := openai.AudioRequest{ Model: openai.Whisper1, FilePath: "recording.mp3", } resp, err := c.CreateTranscription(ctx, req) if err != nil { fmt.Printf("Transcription error: %v\n", err) return } fmt.Println(resp.Text) } ```
Audio Captions ```go package main import ( "context" "fmt" "os" openai "github.com/sashabaranov/go-openai" ) func main() { c := openai.NewClient(os.Getenv("OPENAI_KEY")) req := openai.AudioRequest{ Model: openai.Whisper1, FilePath: os.Args[1], Format: openai.AudioResponseFormatSRT, } resp, err := c.CreateTranscription(context.Background(), req) if err != nil { fmt.Printf("Transcription error: %v\n", err) return } f, err := os.Create(os.Args[1] + ".srt") if err != nil { fmt.Printf("Could not open file: %v\n", err) return } defer f.Close() if _, err := f.WriteString(resp.Text); err != nil { fmt.Printf("Error writing to file: %v\n", err) return } } ```
DALL-E 2 image generation ```go package main import ( "bytes" "context" "encoding/base64" "fmt" openai "github.com/sashabaranov/go-openai" "image/png" "os" ) func main() { c := openai.NewClient("your token") ctx := context.Background() // Sample image by link reqUrl := openai.ImageRequest{ Prompt: "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail", Size: openai.CreateImageSize256x256, ResponseFormat: openai.CreateImageResponseFormatURL, N: 1, } respUrl, err := c.CreateImage(ctx, reqUrl) if err != nil { fmt.Printf("Image creation error: %v\n", err) return } fmt.Println(respUrl.Data[0].URL) // Example image as base64 reqBase64 := openai.ImageRequest{ Prompt: "Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine", Size: openai.CreateImageSize256x256, ResponseFormat: openai.CreateImageResponseFormatB64JSON, N: 1, } respBase64, err := c.CreateImage(ctx, reqBase64) if err != nil { fmt.Printf("Image creation error: %v\n", err) return } imgBytes, err := base64.StdEncoding.DecodeString(respBase64.Data[0].B64JSON) if err != nil { fmt.Printf("Base64 decode error: %v\n", err) return } r := bytes.NewReader(imgBytes) imgData, err := png.Decode(r) if err != nil { fmt.Printf("PNG decode error: %v\n", err) return } file, err := os.Create("example.png") if err != nil { fmt.Printf("File creation error: %v\n", err) return } defer file.Close() if err := png.Encode(file, imgData); err != nil { fmt.Printf("PNG encode error: %v\n", err) return } fmt.Println("The image was saved as example.png") } ```
GPT Image 1 image generation ```go package main import ( "context" "encoding/base64" "fmt" "os" openai "github.com/sashabaranov/go-openai" ) func main() { c := openai.NewClient("your token") ctx := context.Background() req := openai.ImageRequest{ Prompt: "Parrot on a skateboard performing a trick. Large bold text \"SKATE MASTER\" banner at the bottom of the image. Cartoon style, natural light, high detail, 1:1 aspect ratio.", Background: openai.CreateImageBackgroundOpaque, Model: openai.CreateImageModelGptImage1, Size: openai.CreateImageSize1024x1024, N: 1, Quality: openai.CreateImageQualityLow, OutputCompression: 100, OutputFormat: openai.CreateImageOutputFormatJPEG, // Moderation: openai.CreateImageModerationLow, // User: "", } resp, err := c.CreateImage(ctx, req) if err != nil { fmt.Printf("Image creation Image generation with GPT Image 1error: %v\n", err) return } fmt.Println("Image Base64:", resp.Data[0].B64JSON) // Decode the base64 data imgBytes, err := base64.StdEncoding.DecodeString(resp.Data[0].B64JSON) if err != nil { fmt.Printf("Base64 decode error: %v\n", err) return } // Write image to file outputPath := "generated_image.jpg" err = os.WriteFile(outputPath, imgBytes, 0644) if err != nil { fmt.Printf("Failed to write image file: %v\n", err) return } fmt.Printf("The image was saved as %s\n", outputPath) } ```
Configuring proxy ```go config := openai.DefaultConfig("token") proxyUrl, err := url.Parse("http://localhost:{port}") if err != nil { panic(err) } transport := &http.Transport{ Proxy: http.ProxyURL(proxyUrl), } config.HTTPClient = &http.Client{ Transport: transport, } c := openai.NewClientWithConfig(config) ``` See also: https://pkg.go.dev/github.com/sashabaranov/go-openai#ClientConfig
ChatGPT support context ```go package main import ( "bufio" "context" "fmt" "os" "strings" "github.com/sashabaranov/go-openai" ) func main() { client := openai.NewClient("your token") messages := make([]openai.ChatCompletionMessage, 0) reader := bufio.NewReader(os.Stdin) fmt.Println("Conversation") fmt.Println("---------------------") for { fmt.Print("-> ") text, _ := reader.ReadString('\n') // convert CRLF to LF text = strings.Replace(text, "\n", "", -1) messages = append(messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleUser, Content: text, }) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: messages, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) continue } content := resp.Choices[0].Message.Content messages = append(messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleAssistant, Content: content, }) fmt.Println(content) } } ```
Azure OpenAI ChatGPT ```go package main import ( "context" "fmt" openai "github.com/sashabaranov/go-openai" ) func main() { config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint") // If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function // config.AzureModelMapperFunc = func(model string) string { // azureModelMapping := map[string]string{ // "gpt-3.5-turbo": "your gpt-3.5-turbo deployment name", // } // return azureModelMapping[model] // } client := openai.NewClientWithConfig(config) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello Azure OpenAI!", }, }, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) return } fmt.Println(resp.Choices[0].Message.Content) } ```
Embedding Semantic Similarity ```go package main import ( "context" "log" openai "github.com/sashabaranov/go-openai" ) func main() { client := openai.NewClient("your-token") // Create an EmbeddingRequest for the user query queryReq := openai.EmbeddingRequest{ Input: []string{"How many chucks would a woodchuck chuck"}, Model: openai.AdaEmbeddingV2, } // Create an embedding for the user query queryResponse, err := client.CreateEmbeddings(context.Background(), queryReq) if err != nil { log.Fatal("Error creating query embedding:", err) } // Create an EmbeddingRequest for the target text targetReq := openai.EmbeddingRequest{ Input: []string{"How many chucks would a woodchuck chuck if the woodchuck could chuck wood"}, Model: openai.AdaEmbeddingV2, } // Create an embedding for the target text targetResponse, err := client.CreateEmbeddings(context.Background(), targetReq) if err != nil { log.Fatal("Error creating target embedding:", err) } // Now that we have the embeddings for the user query and the target text, we // can calculate their similarity. queryEmbedding := queryResponse.Data[0] targetEmbedding := targetResponse.Data[0] similarity, err := queryEmbedding.DotProduct(&targetEmbedding) if err != nil { log.Fatal("Error calculating dot product:", err) } log.Printf("The similarity score between the query and the target is %f", similarity) } ```
Azure OpenAI Embeddings ```go package main import ( "context" "fmt" openai "github.com/sashabaranov/go-openai" ) func main() { config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint") config.APIVersion = "2023-05-15" // optional update to latest API version //If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function //config.AzureModelMapperFunc = func(model string) string { // azureModelMapping := map[string]string{ // "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name", // } // return azureModelMapping[model] //} input := "Text to vectorize" client := openai.NewClientWithConfig(config) resp, err := client.CreateEmbeddings( context.Background(), openai.EmbeddingRequest{ Input: []string{input}, Model: openai.AdaEmbeddingV2, }) if err != nil { fmt.Printf("CreateEmbeddings error: %v\n", err) return } vectors := resp.Data[0].Embedding // []float32 with 1536 dimensions fmt.Println(vectors[:10], "...", vectors[len(vectors)-10:]) } ```
JSON Schema for function calling It is now possible for chat completion to choose to call a function for more information ([see developer docs here](https://platform.openai.com/docs/guides/gpt/function-calling)). In order to describe the type of functions that can be called, a JSON schema must be provided. Many JSON schema libraries exist and are more advanced than what we can offer in this library, however we have included a simple `jsonschema` package for those who want to use this feature without formatting their own JSON schema payload. The developer documents give this JSON schema definition as an example: ```json { "name":"get_current_weather", "description":"Get the current weather in a given location", "parameters":{ "type":"object", "properties":{ "location":{ "type":"string", "description":"The city and state, e.g. San Francisco, CA" }, "unit":{ "type":"string", "enum":[ "celsius", "fahrenheit" ] } }, "required":[ "location" ] } } ``` Using the `jsonschema` package, this schema could be created using structs as such: ```go FunctionDefinition{ Name: "get_current_weather", Parameters: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "location": { Type: jsonschema.String, Description: "The city and state, e.g. San Francisco, CA", }, "unit": { Type: jsonschema.String, Enum: []string{"celsius", "fahrenheit"}, }, }, Required: []string{"location"}, }, } ``` The `Parameters` field of a `FunctionDefinition` can accept either of the above styles, or even a nested struct from another library (as long as it can be marshalled into JSON).
Error handling Open-AI maintains clear documentation on how to [handle API errors](https://platform.openai.com/docs/guides/error-codes/api-errors) example: ``` e := &openai.APIError{} if errors.As(err, &e) { switch e.HTTPStatusCode { case 401: // invalid auth or key (do not retry) case 429: // rate limiting or engine overload (wait and retry) case 500: // openai server error (retry) default: // unhandled } } ```
Fine Tune Model ```go package main import ( "context" "fmt" "github.com/sashabaranov/go-openai" ) func main() { client := openai.NewClient("your token") ctx := context.Background() // create a .jsonl file with your training data for conversational model // {"prompt": "", "completion": ""} // {"prompt": "", "completion": ""} // {"prompt": "", "completion": ""} // chat models are trained using the following file format: // {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "What's the capital of France?"}, {"role": "assistant", "content": "Paris, as if everyone doesn't know that already."}]} // {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "Who wrote 'Romeo and Juliet'?"}, {"role": "assistant", "content": "Oh, just some guy named William Shakespeare. Ever heard of him?"}]} // {"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "How far is the Moon from Earth?"}, {"role": "assistant", "content": "Around 384,400 kilometers. Give or take a few, like that really matters."}]} // you can use openai cli tool to validate the data // For more info - https://platform.openai.com/docs/guides/fine-tuning file, err := client.CreateFile(ctx, openai.FileRequest{ FilePath: "training_prepared.jsonl", Purpose: "fine-tune", }) if err != nil { fmt.Printf("Upload JSONL file error: %v\n", err) return } // create a fine tuning job // Streams events until the job is done (this often takes minutes, but can take hours if there are many jobs in the queue or your dataset is large) // use below get method to know the status of your model fineTuningJob, err := client.CreateFineTuningJob(ctx, openai.FineTuningJobRequest{ TrainingFile: file.ID, Model: "davinci-002", // gpt-3.5-turbo-0613, babbage-002. }) if err != nil { fmt.Printf("Creating new fine tune model error: %v\n", err) return } fineTuningJob, err = client.RetrieveFineTuningJob(ctx, fineTuningJob.ID) if err != nil { fmt.Printf("Getting fine tune model error: %v\n", err) return } fmt.Println(fineTuningJob.FineTunedModel) // once the status of fineTuningJob is `succeeded`, you can use your fine tune model in Completion Request or Chat Completion Request // resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{ // Model: fineTuningJob.FineTunedModel, // Prompt: "your prompt", // }) // if err != nil { // fmt.Printf("Create completion error %v\n", err) // return // } // // fmt.Println(resp.Choices[0].Text) } ```
Structured Outputs ```go package main import ( "context" "fmt" "log" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/jsonschema" ) func main() { client := openai.NewClient("your token") ctx := context.Background() type Result struct { Steps []struct { Explanation string `json:"explanation"` Output string `json:"output"` } `json:"steps"` FinalAnswer string `json:"final_answer"` } var result Result schema, err := jsonschema.GenerateSchemaForType(result) if err != nil { log.Fatalf("GenerateSchemaForType error: %v", err) } resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ Model: openai.GPT4oMini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleSystem, Content: "You are a helpful math tutor. Guide the user through the solution step by step.", }, { Role: openai.ChatMessageRoleUser, Content: "how can I solve 8x + 7 = -23", }, }, ResponseFormat: &openai.ChatCompletionResponseFormat{ Type: openai.ChatCompletionResponseFormatTypeJSONSchema, JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{ Name: "math_reasoning", Schema: schema, Strict: true, }, }, }) if err != nil { log.Fatalf("CreateChatCompletion error: %v", err) } err = schema.Unmarshal(resp.Choices[0].Message.Content, &result) if err != nil { log.Fatalf("Unmarshal schema error: %v", err) } fmt.Println(result) } ```
See the `examples/` folder for more. ## Frequently Asked Questions ### Why don't we get the same answer when specifying a temperature field of 0 and asking the same question? Even when specifying a temperature field of 0, it doesn't guarantee that you'll always get the same response. Several factors come into play. 1. Go OpenAI Behavior: When you specify a temperature field of 0 in Go OpenAI, the omitempty tag causes that field to be removed from the request. Consequently, the OpenAI API applies the default value of 1. 2. Token Count for Input/Output: If there's a large number of tokens in the input and output, setting the temperature to 0 can still result in non-deterministic behavior. In particular, when using around 32k tokens, the likelihood of non-deterministic behavior becomes highest even with a temperature of 0. Due to the factors mentioned above, different answers may be returned even for the same question. **Workarounds:** 1. As of November 2023, use [the new `seed` parameter](https://platform.openai.com/docs/guides/text-generation/reproducible-outputs) in conjunction with the `system_fingerprint` response field, alongside Temperature management. 2. Try using `math.SmallestNonzeroFloat32`: By specifying `math.SmallestNonzeroFloat32` in the temperature field instead of 0, you can mimic the behavior of setting it to 0. 3. Limiting Token Count: By limiting the number of tokens in the input and output and especially avoiding large requests close to 32k tokens, you can reduce the risk of non-deterministic behavior. By adopting these strategies, you can expect more consistent results. **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: - [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:** [Is it possible to join the implementation of GPT3 Tokenizer](https://github.com/sashabaranov/go-openai/issues/62) ## Contributing By following [Contributing Guidelines](https://github.com/sashabaranov/go-openai/blob/master/CONTRIBUTING.md), we hope to ensure that your contributions are made smoothly and efficiently. ## Thank you We want to take a moment to express our deepest gratitude to the [contributors](https://github.com/sashabaranov/go-openai/graphs/contributors) and sponsors of this project: - [Carson Kahn](https://carsonkahn.com) of [Spindle AI](https://spindleai.com) To all of you: thank you. You've helped us achieve more than we ever imagined possible. Can't wait to see where we go next, together! ================================================ FILE: api_integration_test.go ================================================ //go:build integration package openai_test import ( "context" "encoding/json" "errors" "io" "os" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" "github.com/sashabaranov/go-openai/jsonschema" ) func TestAPI(t *testing.T) { apiToken := os.Getenv("OPENAI_TOKEN") if apiToken == "" { t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.") } var err error c := openai.NewClient(apiToken) ctx := context.Background() _, err = c.ListEngines(ctx) checks.NoError(t, err, "ListEngines error") _, err = c.GetEngine(ctx, openai.GPT3Davinci002) checks.NoError(t, err, "GetEngine error") fileRes, err := c.ListFiles(ctx) checks.NoError(t, err, "ListFiles error") if len(fileRes.Files) > 0 { _, err = c.GetFile(ctx, fileRes.Files[0].ID) checks.NoError(t, err, "GetFile error") } // else skip embeddingReq := openai.EmbeddingRequest{ Input: []string{ "The food was delicious and the waiter", "Other examples of embedding request", }, Model: openai.AdaEmbeddingV2, } _, err = c.CreateEmbeddings(ctx, embeddingReq) checks.NoError(t, err, "Embedding error") _, err = c.CreateChatCompletion( ctx, openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }, ) checks.NoError(t, err, "CreateChatCompletion (without name) returned error") _, err = c.CreateChatCompletion( ctx, openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Name: "John_Doe", Content: "Hello!", }, }, }, ) checks.NoError(t, err, "CreateChatCompletion (with name) returned error") _, err = c.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "What is the weather like in Boston?", }, }, Functions: []openai.FunctionDefinition{{ Name: "get_current_weather", Parameters: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "location": { Type: jsonschema.String, Description: "The city and state, e.g. San Francisco, CA", }, "unit": { Type: jsonschema.String, Enum: []string{"celsius", "fahrenheit"}, }, }, Required: []string{"location"}, }, }}, }, ) checks.NoError(t, err, "CreateChatCompletion (with functions) returned error") } func TestCompletionStream(t *testing.T) { apiToken := os.Getenv("OPENAI_TOKEN") if apiToken == "" { t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.") } c := openai.NewClient(apiToken) ctx := context.Background() stream, err := c.CreateCompletionStream(ctx, openai.CompletionRequest{ Prompt: "Ex falso quodlibet", Model: openai.GPT3Babbage002, MaxTokens: 5, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() counter := 0 for { _, err = stream.Recv() if err != nil { if errors.Is(err, io.EOF) { break } t.Errorf("Stream error: %v", err) } else { counter++ } } if counter == 0 { t.Error("Stream did not return any responses") } } func TestAPIError(t *testing.T) { apiToken := os.Getenv("OPENAI_TOKEN") if apiToken == "" { t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.") } var err error c := openai.NewClient(apiToken + "_invalid") ctx := context.Background() _, err = c.ListEngines(ctx) checks.HasError(t, err, "ListEngines should fail with an invalid key") var apiErr *openai.APIError if !errors.As(err, &apiErr) { t.Fatalf("Error is not an APIError: %+v", err) } if apiErr.HTTPStatusCode != 401 { t.Fatalf("Unexpected API error status code: %d", apiErr.HTTPStatusCode) } switch v := apiErr.Code.(type) { case string: if v != "invalid_api_key" { t.Fatalf("Unexpected API error code: %s", v) } default: t.Fatalf("Unexpected API error code type: %T", v) } if apiErr.Error() == "" { t.Fatal("Empty error message occurred") } } func TestChatCompletionResponseFormat_JSONSchema(t *testing.T) { apiToken := os.Getenv("OPENAI_TOKEN") if apiToken == "" { t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.") } var err error c := openai.NewClient(apiToken) ctx := context.Background() type MyStructuredResponse struct { PascalCase string `json:"pascal_case" required:"true" description:"PascalCase"` CamelCase string `json:"camel_case" required:"true" description:"CamelCase"` KebabCase string `json:"kebab_case" required:"true" description:"KebabCase"` SnakeCase string `json:"snake_case" required:"true" description:"SnakeCase"` } var result MyStructuredResponse schema, err := jsonschema.GenerateSchemaForType(result) if err != nil { t.Fatal("CreateChatCompletion (use json_schema response) GenerateSchemaForType error") } resp, err := c.CreateChatCompletion( ctx, openai.ChatCompletionRequest{ Model: openai.GPT4oMini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleSystem, Content: "Please enter a string, and we will convert it into the following naming conventions:" + "1. PascalCase: Each word starts with an uppercase letter, with no spaces or separators." + "2. CamelCase: The first word starts with a lowercase letter, " + "and subsequent words start with an uppercase letter, with no spaces or separators." + "3. KebabCase: All letters are lowercase, with words separated by hyphens `-`." + "4. SnakeCase: All letters are lowercase, with words separated by underscores `_`.", }, { Role: openai.ChatMessageRoleUser, Content: "Hello World", }, }, ResponseFormat: &openai.ChatCompletionResponseFormat{ Type: openai.ChatCompletionResponseFormatTypeJSONSchema, JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{ Name: "cases", Schema: schema, Strict: true, }, }, }, ) checks.NoError(t, err, "CreateChatCompletion (use json_schema response) returned error") if err == nil { err = schema.Unmarshal(resp.Choices[0].Message.Content, &result) checks.NoError(t, err, "CreateChatCompletion (use json_schema response) unmarshal error") } } func TestChatCompletionStructuredOutputsFunctionCalling(t *testing.T) { apiToken := os.Getenv("OPENAI_TOKEN") if apiToken == "" { t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.") } var err error c := openai.NewClient(apiToken) ctx := context.Background() resp, err := c.CreateChatCompletion( ctx, openai.ChatCompletionRequest{ Model: openai.GPT4oMini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleSystem, Content: "Please enter a string, and we will convert it into the following naming conventions:" + "1. PascalCase: Each word starts with an uppercase letter, with no spaces or separators." + "2. CamelCase: The first word starts with a lowercase letter, " + "and subsequent words start with an uppercase letter, with no spaces or separators." + "3. KebabCase: All letters are lowercase, with words separated by hyphens `-`." + "4. SnakeCase: All letters are lowercase, with words separated by underscores `_`.", }, { Role: openai.ChatMessageRoleUser, Content: "Hello World", }, }, Tools: []openai.Tool{ { Type: openai.ToolTypeFunction, Function: &openai.FunctionDefinition{ Name: "display_cases", Strict: true, Parameters: &jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "PascalCase": { Type: jsonschema.String, }, "CamelCase": { Type: jsonschema.String, }, "KebabCase": { Type: jsonschema.String, }, "SnakeCase": { Type: jsonschema.String, }, }, Required: []string{"PascalCase", "CamelCase", "KebabCase", "SnakeCase"}, AdditionalProperties: false, }, }, }, }, ToolChoice: openai.ToolChoice{ Type: openai.ToolTypeFunction, Function: openai.ToolFunction{ Name: "display_cases", }, }, }, ) checks.NoError(t, err, "CreateChatCompletion (use structured outputs response) returned error") var result = make(map[string]string) err = json.Unmarshal([]byte(resp.Choices[0].Message.ToolCalls[0].Function.Arguments), &result) checks.NoError(t, err, "CreateChatCompletion (use structured outputs response) unmarshal error") for _, key := range []string{"PascalCase", "CamelCase", "KebabCase", "SnakeCase"} { if _, ok := result[key]; !ok { t.Errorf("key:%s does not exist.", key) } } } ================================================ FILE: api_internal_test.go ================================================ package openai import ( "context" "testing" ) func TestOpenAIFullURL(t *testing.T) { cases := []struct { Name string Suffix string Expect string }{ { "ChatCompletionsURL", "/chat/completions", "https://api.openai.com/v1/chat/completions", }, { "CompletionsURL", "/completions", "https://api.openai.com/v1/completions", }, } for _, c := range cases { t.Run(c.Name, func(t *testing.T) { az := DefaultConfig("dummy") cli := NewClientWithConfig(az) actual := cli.fullURL(c.Suffix) if actual != c.Expect { t.Errorf("Expected %s, got %s", c.Expect, actual) } t.Logf("Full URL: %s", actual) }) } } func TestRequestAuthHeader(t *testing.T) { cases := []struct { Name string APIType APIType HeaderKey string Token string OrgID string Expect string }{ { "OpenAIDefault", "", "Authorization", "dummy-token-openai", "", "Bearer dummy-token-openai", }, { "OpenAIOrg", APITypeOpenAI, "Authorization", "dummy-token-openai", "dummy-org-openai", "Bearer dummy-token-openai", }, { "OpenAI", APITypeOpenAI, "Authorization", "dummy-token-openai", "", "Bearer dummy-token-openai", }, { "AzureAD", APITypeAzureAD, "Authorization", "dummy-token-azure", "", "Bearer dummy-token-azure", }, { "Azure", APITypeAzure, AzureAPIKeyHeader, "dummy-api-key-here", "", "dummy-api-key-here", }, } for _, c := range cases { t.Run(c.Name, func(t *testing.T) { az := DefaultConfig(c.Token) az.APIType = c.APIType az.OrgID = c.OrgID cli := NewClientWithConfig(az) req, err := cli.newRequest(context.Background(), "POST", "/chat/completions") if err != nil { t.Errorf("Failed to create request: %v", err) } actual := req.Header.Get(c.HeaderKey) if actual != c.Expect { t.Errorf("Expected %s, got %s", c.Expect, actual) } t.Logf("%s: %s", c.HeaderKey, actual) }) } } func TestAzureFullURL(t *testing.T) { cases := []struct { Name string BaseURL string AzureModelMapper map[string]string Suffix string Model string Expect string }{ { "AzureBaseURLWithSlashAutoStrip", "https://httpbin.org/", nil, "/chat/completions", "chatgpt-demo", "https://httpbin.org/" + "openai/deployments/chatgpt-demo" + "/chat/completions?api-version=2023-05-15", }, { "AzureBaseURLWithoutSlashOK", "https://httpbin.org", nil, "/chat/completions", "chatgpt-demo", "https://httpbin.org/" + "openai/deployments/chatgpt-demo" + "/chat/completions?api-version=2023-05-15", }, { "", "https://httpbin.org", nil, "/assistants?limit=10", "chatgpt-demo", "https://httpbin.org/openai/assistants?api-version=2023-05-15&limit=10", }, } for _, c := range cases { t.Run(c.Name, func(t *testing.T) { az := DefaultAzureConfig("dummy", c.BaseURL) cli := NewClientWithConfig(az) // /openai/deployments/{engine}/chat/completions?api-version={api_version} actual := cli.fullURL(c.Suffix, withModel(c.Model)) if actual != c.Expect { t.Errorf("Expected %s, got %s", c.Expect, actual) } t.Logf("Full URL: %s", actual) }) } } func TestCloudflareAzureFullURL(t *testing.T) { cases := []struct { Name string BaseURL string Suffix string Expect string }{ { "CloudflareAzureBaseURLWithSlashAutoStrip", "https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo/", "/chat/completions", "https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo/" + "chat/completions?api-version=2023-05-15", }, { "", "https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo", "/assistants?limit=10", "https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo" + "/assistants?api-version=2023-05-15&limit=10", }, } for _, c := range cases { t.Run(c.Name, func(t *testing.T) { az := DefaultAzureConfig("dummy", c.BaseURL) az.APIType = APITypeCloudflareAzure cli := NewClientWithConfig(az) actual := cli.fullURL(c.Suffix) if actual != c.Expect { t.Errorf("Expected %s, got %s", c.Expect, actual) } t.Logf("Full URL: %s", actual) }) } } ================================================ FILE: assistant.go ================================================ package openai import ( "context" "encoding/json" "fmt" "net/http" "net/url" ) const ( assistantsSuffix = "/assistants" assistantsFilesSuffix = "/files" ) type Assistant struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` Name *string `json:"name,omitempty"` Description *string `json:"description,omitempty"` Model string `json:"model"` Instructions *string `json:"instructions,omitempty"` Tools []AssistantTool `json:"tools"` ToolResources *AssistantToolResource `json:"tool_resources,omitempty"` FileIDs []string `json:"file_ids,omitempty"` // Deprecated in v2 Metadata map[string]any `json:"metadata,omitempty"` Temperature *float32 `json:"temperature,omitempty"` TopP *float32 `json:"top_p,omitempty"` ResponseFormat any `json:"response_format,omitempty"` httpHeader } type AssistantToolType string const ( AssistantToolTypeCodeInterpreter AssistantToolType = "code_interpreter" AssistantToolTypeRetrieval AssistantToolType = "retrieval" AssistantToolTypeFunction AssistantToolType = "function" AssistantToolTypeFileSearch AssistantToolType = "file_search" ) type AssistantTool struct { Type AssistantToolType `json:"type"` Function *FunctionDefinition `json:"function,omitempty"` } type AssistantToolFileSearch struct { VectorStoreIDs []string `json:"vector_store_ids"` } type AssistantToolCodeInterpreter struct { FileIDs []string `json:"file_ids"` } type AssistantToolResource struct { FileSearch *AssistantToolFileSearch `json:"file_search,omitempty"` CodeInterpreter *AssistantToolCodeInterpreter `json:"code_interpreter,omitempty"` } // AssistantRequest provides the assistant request parameters. // When modifying the tools the API functions as the following: // If Tools is undefined, no changes are made to the Assistant's tools. // If Tools is empty slice it will effectively delete all of the Assistant's tools. // If Tools is populated, it will replace all of the existing Assistant's tools with the provided tools. type AssistantRequest struct { Model string `json:"model"` Name *string `json:"name,omitempty"` Description *string `json:"description,omitempty"` Instructions *string `json:"instructions,omitempty"` Tools []AssistantTool `json:"-"` FileIDs []string `json:"file_ids,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` ToolResources *AssistantToolResource `json:"tool_resources,omitempty"` ResponseFormat any `json:"response_format,omitempty"` Temperature *float32 `json:"temperature,omitempty"` TopP *float32 `json:"top_p,omitempty"` } // MarshalJSON provides a custom marshaller for the assistant request to handle the API use cases // If Tools is nil, the field is omitted from the JSON. // If Tools is an empty slice, it's included in the JSON as an empty array ([]). // If Tools is populated, it's included in the JSON with the elements. func (a AssistantRequest) MarshalJSON() ([]byte, error) { type Alias AssistantRequest assistantAlias := &struct { Tools *[]AssistantTool `json:"tools,omitempty"` *Alias }{ Alias: (*Alias)(&a), } if a.Tools != nil { assistantAlias.Tools = &a.Tools } return json.Marshal(assistantAlias) } // AssistantsList is a list of assistants. type AssistantsList struct { Assistants []Assistant `json:"data"` LastID *string `json:"last_id"` FirstID *string `json:"first_id"` HasMore bool `json:"has_more"` httpHeader } type AssistantDeleteResponse struct { ID string `json:"id"` Object string `json:"object"` Deleted bool `json:"deleted"` httpHeader } type AssistantFile struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` AssistantID string `json:"assistant_id"` httpHeader } type AssistantFileRequest struct { FileID string `json:"file_id"` } type AssistantFilesList struct { AssistantFiles []AssistantFile `json:"data"` httpHeader } // CreateAssistant creates a new assistant. func (c *Client) CreateAssistant(ctx context.Context, request AssistantRequest) (response Assistant, err error) { req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(assistantsSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // RetrieveAssistant retrieves an assistant. func (c *Client) RetrieveAssistant( ctx context.Context, assistantID string, ) (response Assistant, err error) { urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // ModifyAssistant modifies an assistant. func (c *Client) ModifyAssistant( ctx context.Context, assistantID string, request AssistantRequest, ) (response Assistant, err error) { urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID) req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // DeleteAssistant deletes an assistant. func (c *Client) DeleteAssistant( ctx context.Context, assistantID string, ) (response AssistantDeleteResponse, err error) { urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID) req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // ListAssistants Lists the currently available assistants. func (c *Client) ListAssistants( ctx context.Context, limit *int, order *string, after *string, before *string, ) (response AssistantsList, err error) { urlValues := url.Values{} if limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *limit)) } if order != nil { urlValues.Add("order", *order) } if after != nil { urlValues.Add("after", *after) } if before != nil { urlValues.Add("before", *before) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("%s%s", assistantsSuffix, encodedValues) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // CreateAssistantFile creates a new assistant file. func (c *Client) CreateAssistantFile( ctx context.Context, assistantID string, request AssistantFileRequest, ) (response AssistantFile, err error) { urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix) req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // RetrieveAssistantFile retrieves an assistant file. func (c *Client) RetrieveAssistantFile( ctx context.Context, assistantID string, fileID string, ) (response AssistantFile, err error) { urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // DeleteAssistantFile deletes an existing file. func (c *Client) DeleteAssistantFile( ctx context.Context, assistantID string, fileID string, ) (err error) { urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID) req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, nil) return } // ListAssistantFiles Lists the currently available files for an assistant. func (c *Client) ListAssistantFiles( ctx context.Context, assistantID string, limit *int, order *string, after *string, before *string, ) (response AssistantFilesList, err error) { urlValues := url.Values{} if limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *limit)) } if order != nil { urlValues.Add("order", *order) } if after != nil { urlValues.Add("after", *after) } if before != nil { urlValues.Add("before", *before) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("%s/%s%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix, encodedValues) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: assistant_test.go ================================================ package openai_test import ( "context" openai "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" "encoding/json" "fmt" "net/http" "testing" ) // TestAssistant Tests the assistant endpoint of the API using the mocked server. func TestAssistant(t *testing.T) { assistantID := "asst_abc123" assistantName := "Ambrogio" assistantDescription := "Ambrogio is a friendly assistant." assistantInstructions := `You are a personal math tutor. When asked a question, write and run Python code to answer the question.` assistantFileID := "file-wB6RM6wHdA49HfS2DJ9fEyrH" limit := 20 order := "desc" after := "asst_abc122" before := "asst_abc124" client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler( "/v1/assistants/"+assistantID+"/files/"+assistantFileID, func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.AssistantFile{ ID: assistantFileID, Object: "assistant.file", CreatedAt: 1234567890, AssistantID: assistantID, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodDelete { fmt.Fprintln(w, `{ id: "file-wB6RM6wHdA49HfS2DJ9fEyrH", object: "assistant.file.deleted", deleted: true }`) } }, ) server.RegisterHandler( "/v1/assistants/"+assistantID+"/files", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.AssistantFilesList{ AssistantFiles: []openai.AssistantFile{ { ID: assistantFileID, Object: "assistant.file", CreatedAt: 1234567890, AssistantID: assistantID, }, }, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodPost { var request openai.AssistantFileRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.AssistantFile{ ID: request.FileID, Object: "assistant.file", CreatedAt: 1234567890, AssistantID: assistantID, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/assistants/"+assistantID, func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: resBytes, _ := json.Marshal(openai.Assistant{ ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: &assistantName, Model: openai.GPT4TurboPreview, Description: &assistantDescription, Instructions: &assistantInstructions, }) fmt.Fprintln(w, string(resBytes)) case http.MethodPost: var request openai.Assistant err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Assistant{ ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: request.Name, Model: request.Model, Description: request.Description, Instructions: request.Instructions, Tools: request.Tools, }) fmt.Fprintln(w, string(resBytes)) case http.MethodDelete: fmt.Fprintln(w, `{ "id": "asst_abc123", "object": "assistant.deleted", "deleted": true }`) } }, ) server.RegisterHandler( "/v1/assistants", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.AssistantRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Assistant{ ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: request.Name, Model: request.Model, Description: request.Description, Instructions: request.Instructions, Tools: request.Tools, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.AssistantsList{ LastID: &assistantID, FirstID: &assistantID, Assistants: []openai.Assistant{ { ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: &assistantName, Model: openai.GPT4TurboPreview, Description: &assistantDescription, Instructions: &assistantInstructions, }, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) ctx := context.Background() t.Run("create_assistant", func(t *testing.T) { _, err := client.CreateAssistant(ctx, openai.AssistantRequest{ Name: &assistantName, Description: &assistantDescription, Model: openai.GPT4TurboPreview, Instructions: &assistantInstructions, }) checks.NoError(t, err, "CreateAssistant error") }) t.Run("retrieve_assistant", func(t *testing.T) { _, err := client.RetrieveAssistant(ctx, assistantID) checks.NoError(t, err, "RetrieveAssistant error") }) t.Run("delete_assistant", func(t *testing.T) { _, err := client.DeleteAssistant(ctx, assistantID) checks.NoError(t, err, "DeleteAssistant error") }) t.Run("list_assistant", func(t *testing.T) { _, err := client.ListAssistants(ctx, &limit, &order, &after, &before) checks.NoError(t, err, "ListAssistants error") }) t.Run("create_assistant_file", func(t *testing.T) { _, err := client.CreateAssistantFile(ctx, assistantID, openai.AssistantFileRequest{ FileID: assistantFileID, }) checks.NoError(t, err, "CreateAssistantFile error") }) t.Run("list_assistant_files", func(t *testing.T) { _, err := client.ListAssistantFiles(ctx, assistantID, &limit, &order, &after, &before) checks.NoError(t, err, "ListAssistantFiles error") }) t.Run("retrieve_assistant_file", func(t *testing.T) { _, err := client.RetrieveAssistantFile(ctx, assistantID, assistantFileID) checks.NoError(t, err, "RetrieveAssistantFile error") }) t.Run("delete_assistant_file", func(t *testing.T) { err := client.DeleteAssistantFile(ctx, assistantID, assistantFileID) checks.NoError(t, err, "DeleteAssistantFile error") }) t.Run("modify_assistant_no_tools", func(t *testing.T) { assistant, err := client.ModifyAssistant(ctx, assistantID, openai.AssistantRequest{ Name: &assistantName, Description: &assistantDescription, Model: openai.GPT4TurboPreview, Instructions: &assistantInstructions, }) checks.NoError(t, err, "ModifyAssistant error") if assistant.Tools != nil { t.Errorf("expected nil got %v", assistant.Tools) } }) t.Run("modify_assistant_with_tools", func(t *testing.T) { assistant, err := client.ModifyAssistant(ctx, assistantID, openai.AssistantRequest{ Name: &assistantName, Description: &assistantDescription, Model: openai.GPT4TurboPreview, Instructions: &assistantInstructions, Tools: []openai.AssistantTool{{Type: openai.AssistantToolTypeFunction}}, }) checks.NoError(t, err, "ModifyAssistant error") if assistant.Tools == nil || len(assistant.Tools) != 1 { t.Errorf("expected a slice got %v", assistant.Tools) } }) t.Run("modify_assistant_empty_tools", func(t *testing.T) { assistant, err := client.ModifyAssistant(ctx, assistantID, openai.AssistantRequest{ Name: &assistantName, Description: &assistantDescription, Model: openai.GPT4TurboPreview, Instructions: &assistantInstructions, Tools: make([]openai.AssistantTool, 0), }) checks.NoError(t, err, "ModifyAssistant error") if assistant.Tools == nil { t.Errorf("expected a slice got %v", assistant.Tools) } }) } func TestAzureAssistant(t *testing.T) { assistantID := "asst_abc123" assistantName := "Ambrogio" assistantDescription := "Ambrogio is a friendly assistant." assistantInstructions := `You are a personal math tutor. When asked a question, write and run Python code to answer the question.` assistantFileID := "file-wB6RM6wHdA49HfS2DJ9fEyrH" limit := 20 order := "desc" after := "asst_abc122" before := "asst_abc124" client, server, teardown := setupAzureTestServer() defer teardown() server.RegisterHandler( "/openai/assistants/"+assistantID+"/files/"+assistantFileID, func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.AssistantFile{ ID: assistantFileID, Object: "assistant.file", CreatedAt: 1234567890, AssistantID: assistantID, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodDelete { fmt.Fprintln(w, `{ id: "file-wB6RM6wHdA49HfS2DJ9fEyrH", object: "assistant.file.deleted", deleted: true }`) } }, ) server.RegisterHandler( "/openai/assistants/"+assistantID+"/files", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.AssistantFilesList{ AssistantFiles: []openai.AssistantFile{ { ID: assistantFileID, Object: "assistant.file", CreatedAt: 1234567890, AssistantID: assistantID, }, }, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodPost { var request openai.AssistantFileRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.AssistantFile{ ID: request.FileID, Object: "assistant.file", CreatedAt: 1234567890, AssistantID: assistantID, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/openai/assistants/"+assistantID, func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: resBytes, _ := json.Marshal(openai.Assistant{ ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: &assistantName, Model: openai.GPT4TurboPreview, Description: &assistantDescription, Instructions: &assistantInstructions, }) fmt.Fprintln(w, string(resBytes)) case http.MethodPost: var request openai.AssistantRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Assistant{ ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: request.Name, Model: request.Model, Description: request.Description, Instructions: request.Instructions, Tools: request.Tools, }) fmt.Fprintln(w, string(resBytes)) case http.MethodDelete: fmt.Fprintln(w, `{ "id": "asst_abc123", "object": "assistant.deleted", "deleted": true }`) } }, ) server.RegisterHandler( "/openai/assistants", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.AssistantRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Assistant{ ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: request.Name, Model: request.Model, Description: request.Description, Instructions: request.Instructions, Tools: request.Tools, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.AssistantsList{ LastID: &assistantID, FirstID: &assistantID, Assistants: []openai.Assistant{ { ID: assistantID, Object: "assistant", CreatedAt: 1234567890, Name: &assistantName, Model: openai.GPT4TurboPreview, Description: &assistantDescription, Instructions: &assistantInstructions, }, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) ctx := context.Background() _, err := client.CreateAssistant(ctx, openai.AssistantRequest{ Name: &assistantName, Description: &assistantDescription, Model: openai.GPT4TurboPreview, Instructions: &assistantInstructions, }) checks.NoError(t, err, "CreateAssistant error") _, err = client.RetrieveAssistant(ctx, assistantID) checks.NoError(t, err, "RetrieveAssistant error") _, err = client.ModifyAssistant(ctx, assistantID, openai.AssistantRequest{ Name: &assistantName, Description: &assistantDescription, Model: openai.GPT4TurboPreview, Instructions: &assistantInstructions, }) checks.NoError(t, err, "ModifyAssistant error") _, err = client.DeleteAssistant(ctx, assistantID) checks.NoError(t, err, "DeleteAssistant error") _, err = client.ListAssistants(ctx, &limit, &order, &after, &before) checks.NoError(t, err, "ListAssistants error") _, err = client.CreateAssistantFile(ctx, assistantID, openai.AssistantFileRequest{ FileID: assistantFileID, }) checks.NoError(t, err, "CreateAssistantFile error") _, err = client.ListAssistantFiles(ctx, assistantID, &limit, &order, &after, &before) checks.NoError(t, err, "ListAssistantFiles error") _, err = client.RetrieveAssistantFile(ctx, assistantID, assistantFileID) checks.NoError(t, err, "RetrieveAssistantFile error") err = client.DeleteAssistantFile(ctx, assistantID, assistantFileID) checks.NoError(t, err, "DeleteAssistantFile error") } ================================================ FILE: audio.go ================================================ package openai import ( "bytes" "context" "fmt" "io" "net/http" "os" utils "github.com/sashabaranov/go-openai/internal" ) // Whisper Defines the models provided by OpenAI to use when processing audio with OpenAI. const ( Whisper1 = "whisper-1" ) // Response formats; Whisper uses AudioResponseFormatJSON by default. type AudioResponseFormat string const ( AudioResponseFormatJSON AudioResponseFormat = "json" AudioResponseFormatText AudioResponseFormat = "text" AudioResponseFormatSRT AudioResponseFormat = "srt" AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json" AudioResponseFormatVTT AudioResponseFormat = "vtt" ) type TranscriptionTimestampGranularity string const ( TranscriptionTimestampGranularityWord TranscriptionTimestampGranularity = "word" TranscriptionTimestampGranularitySegment TranscriptionTimestampGranularity = "segment" ) // AudioRequest represents a request structure for audio API. type AudioRequest struct { Model string // FilePath is either an existing file in your filesystem or a filename representing the contents of Reader. FilePath string // Reader is an optional io.Reader when you do not want to use an existing file. Reader io.Reader Prompt string Temperature float32 Language string // Only for transcription. Format AudioResponseFormat TimestampGranularities []TranscriptionTimestampGranularity // Only for transcription. } // AudioResponse represents a response structure for audio API. type AudioResponse struct { Task string `json:"task"` Language string `json:"language"` Duration float64 `json:"duration"` Segments []struct { ID int `json:"id"` Seek int `json:"seek"` Start float64 `json:"start"` End float64 `json:"end"` Text string `json:"text"` Tokens []int `json:"tokens"` Temperature float64 `json:"temperature"` AvgLogprob float64 `json:"avg_logprob"` CompressionRatio float64 `json:"compression_ratio"` NoSpeechProb float64 `json:"no_speech_prob"` Transient bool `json:"transient"` } `json:"segments"` Words []struct { Word string `json:"word"` Start float64 `json:"start"` End float64 `json:"end"` } `json:"words"` Text string `json:"text"` httpHeader } type audioTextResponse struct { Text string `json:"text"` httpHeader } func (r *audioTextResponse) ToAudioResponse() AudioResponse { return AudioResponse{ Text: r.Text, httpHeader: r.httpHeader, } } // CreateTranscription — API call to create a transcription. Returns transcribed text. func (c *Client) CreateTranscription( ctx context.Context, request AudioRequest, ) (response AudioResponse, err error) { return c.callAudioAPI(ctx, request, "transcriptions") } // CreateTranslation — API call to translate audio into English. func (c *Client) CreateTranslation( ctx context.Context, request AudioRequest, ) (response AudioResponse, err error) { return c.callAudioAPI(ctx, request, "translations") } // callAudioAPI — API call to an audio endpoint. func (c *Client) callAudioAPI( ctx context.Context, request AudioRequest, endpointSuffix string, ) (response AudioResponse, err error) { var formBody bytes.Buffer builder := c.createFormBuilder(&formBody) if err = audioMultipartForm(request, builder); err != nil { return AudioResponse{}, err } urlSuffix := fmt.Sprintf("/audio/%s", endpointSuffix) req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix, withModel(request.Model)), withBody(&formBody), withContentType(builder.FormDataContentType()), ) if err != nil { return AudioResponse{}, err } if request.HasJSONResponse() { err = c.sendRequest(req, &response) } else { var textResponse audioTextResponse err = c.sendRequest(req, &textResponse) response = textResponse.ToAudioResponse() } if err != nil { return AudioResponse{}, err } return } // HasJSONResponse returns true if the response format is JSON. func (r AudioRequest) HasJSONResponse() bool { return r.Format == "" || r.Format == AudioResponseFormatJSON || r.Format == AudioResponseFormatVerboseJSON } // audioMultipartForm creates a form with audio file contents and the name of the model to use for // audio processing. func audioMultipartForm(request AudioRequest, b utils.FormBuilder) error { err := createFileField(request, b) if err != nil { return err } err = b.WriteField("model", request.Model) if err != nil { return fmt.Errorf("writing model name: %w", err) } // Create a form field for the prompt (if provided) if request.Prompt != "" { err = b.WriteField("prompt", request.Prompt) if err != nil { return fmt.Errorf("writing prompt: %w", err) } } // Create a form field for the format (if provided) if request.Format != "" { err = b.WriteField("response_format", string(request.Format)) if err != nil { return fmt.Errorf("writing format: %w", err) } } // Create a form field for the temperature (if provided) if request.Temperature != 0 { err = b.WriteField("temperature", fmt.Sprintf("%.2f", request.Temperature)) if err != nil { return fmt.Errorf("writing temperature: %w", err) } } // Create a form field for the language (if provided) if request.Language != "" { err = b.WriteField("language", request.Language) if err != nil { return fmt.Errorf("writing language: %w", err) } } if len(request.TimestampGranularities) > 0 { for _, tg := range request.TimestampGranularities { err = b.WriteField("timestamp_granularities[]", string(tg)) if err != nil { return fmt.Errorf("writing timestamp_granularities[]: %w", err) } } } // Close the multipart writer return b.Close() } // createFileField creates the "file" form field from either an existing file or by using the reader. func createFileField(request AudioRequest, b utils.FormBuilder) error { if request.Reader != nil { err := b.CreateFormFileReader("file", request.Reader, request.FilePath) if err != nil { return fmt.Errorf("creating form using reader: %w", err) } return nil } f, err := os.Open(request.FilePath) if err != nil { return fmt.Errorf("opening audio file: %w", err) } defer f.Close() err = b.CreateFormFile("file", f) if err != nil { return fmt.Errorf("creating form file: %w", err) } return nil } ================================================ FILE: audio_api_test.go ================================================ package openai_test import ( "bytes" "context" "errors" "io" "mime" "mime/multipart" "net/http" "path/filepath" "strings" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test" "github.com/sashabaranov/go-openai/internal/test/checks" ) // TestAudio Tests the transcription and translation endpoints of the API using the mocked server. func TestAudio(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/audio/transcriptions", handleAudioEndpoint) server.RegisterHandler("/v1/audio/translations", handleAudioEndpoint) testcases := []struct { name string createFn func(context.Context, openai.AudioRequest) (openai.AudioResponse, error) }{ { "transcribe", client.CreateTranscription, }, { "translate", client.CreateTranslation, }, } ctx := context.Background() for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { path := filepath.Join(t.TempDir(), "fake.mp3") test.CreateTestFile(t, path) req := openai.AudioRequest{ FilePath: path, Model: "whisper-3", } _, err := tc.createFn(ctx, req) checks.NoError(t, err, "audio API error") }) t.Run(tc.name+" (with reader)", func(t *testing.T) { req := openai.AudioRequest{ FilePath: "fake.webm", Reader: bytes.NewBuffer([]byte(`some webm binary data`)), Model: "whisper-3", } _, err := tc.createFn(ctx, req) checks.NoError(t, err, "audio API error") }) } } func TestAudioWithOptionalArgs(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/audio/transcriptions", handleAudioEndpoint) server.RegisterHandler("/v1/audio/translations", handleAudioEndpoint) testcases := []struct { name string createFn func(context.Context, openai.AudioRequest) (openai.AudioResponse, error) }{ { "transcribe", client.CreateTranscription, }, { "translate", client.CreateTranslation, }, } ctx := context.Background() for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { path := filepath.Join(t.TempDir(), "fake.mp3") test.CreateTestFile(t, path) req := openai.AudioRequest{ FilePath: path, Model: "whisper-3", Prompt: "用简体中文", Temperature: 0.5, Language: "zh", Format: openai.AudioResponseFormatSRT, TimestampGranularities: []openai.TranscriptionTimestampGranularity{ openai.TranscriptionTimestampGranularitySegment, openai.TranscriptionTimestampGranularityWord, }, } _, err := tc.createFn(ctx, req) checks.NoError(t, err, "audio API error") }) } } // handleAudioEndpoint Handles the completion endpoint by the test server. func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) { var err error // audio endpoints only accept POST requests if r.Method != "POST" { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type")) if err != nil { http.Error(w, "failed to parse media type", http.StatusBadRequest) return } if !strings.HasPrefix(mediaType, "multipart") { http.Error(w, "request is not multipart", http.StatusBadRequest) } boundary, ok := params["boundary"] if !ok { http.Error(w, "no boundary in params", http.StatusBadRequest) return } fileData := &bytes.Buffer{} mr := multipart.NewReader(r.Body, boundary) part, err := mr.NextPart() if err != nil && errors.Is(err, io.EOF) { http.Error(w, "error accessing file", http.StatusBadRequest) return } if _, err = io.Copy(fileData, part); err != nil { http.Error(w, "failed to copy file", http.StatusInternalServerError) return } if len(fileData.Bytes()) == 0 { w.WriteHeader(http.StatusInternalServerError) http.Error(w, "received empty file data", http.StatusBadRequest) return } if _, err = w.Write([]byte(`{"body": "hello"}`)); err != nil { http.Error(w, "failed to write body", http.StatusInternalServerError) return } } ================================================ FILE: audio_test.go ================================================ package openai //nolint:testpackage // testing private field import ( "bytes" "context" "errors" "fmt" "io" "net/http" "os" "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" ) func TestAudioWithFailingFormBuilder(t *testing.T) { path := filepath.Join(t.TempDir(), "fake.mp3") test.CreateTestFile(t, path) req := AudioRequest{ FilePath: path, Prompt: "test", Temperature: 0.5, Language: "en", Format: AudioResponseFormatSRT, TimestampGranularities: []TranscriptionTimestampGranularity{ TranscriptionTimestampGranularitySegment, TranscriptionTimestampGranularityWord, }, } mockFailedErr := fmt.Errorf("mock form builder fail") mockBuilder := &mockFormBuilder{} mockBuilder.mockCreateFormFile = func(string, *os.File) error { return mockFailedErr } err := audioMultipartForm(req, mockBuilder) checks.ErrorIs(t, err, mockFailedErr, "audioMultipartForm should return error if form builder fails") mockBuilder.mockCreateFormFile = func(string, *os.File) error { return nil } var failForField string mockBuilder.mockWriteField = func(fieldname, _ string) error { if fieldname == failForField { return mockFailedErr } return nil } failOn := []string{"model", "prompt", "temperature", "language", "response_format", "timestamp_granularities[]"} for _, failingField := range failOn { failForField = failingField mockFailedErr = fmt.Errorf("mock form builder fail on field %s", failingField) err = audioMultipartForm(req, mockBuilder) checks.ErrorIs(t, err, mockFailedErr, "audioMultipartForm should return error if form builder fails") } } func TestCreateFileField(t *testing.T) { t.Run("createFileField failing file", func(t *testing.T) { path := filepath.Join(t.TempDir(), "fake.mp3") test.CreateTestFile(t, path) req := AudioRequest{ FilePath: path, } mockFailedErr := fmt.Errorf("mock form builder fail") mockBuilder := &mockFormBuilder{ mockCreateFormFile: func(string, *os.File) error { return mockFailedErr }, } err := createFileField(req, mockBuilder) checks.ErrorIs(t, err, mockFailedErr, "createFileField using a file should return error if form builder fails") }) t.Run("createFileField failing reader", func(t *testing.T) { req := AudioRequest{ FilePath: "test.wav", Reader: bytes.NewBuffer([]byte(`wav test contents`)), } mockFailedErr := fmt.Errorf("mock form builder fail") mockBuilder := &mockFormBuilder{ mockCreateFormFileReader: func(string, io.Reader, string) error { return mockFailedErr }, } err := createFileField(req, mockBuilder) checks.ErrorIs(t, err, mockFailedErr, "createFileField using a reader should return error if form builder fails") }) t.Run("createFileField failing open", func(t *testing.T) { req := AudioRequest{ FilePath: "non_existing_file.wav", } mockBuilder := &mockFormBuilder{} err := createFileField(req, mockBuilder) checks.HasError(t, err, "createFileField using file should return error when open file fails") }) } // failingFormBuilder always returns an error when creating form files. type failingFormBuilder struct{ err error } func (f *failingFormBuilder) CreateFormFile(_ string, _ *os.File) error { return f.err } func (f *failingFormBuilder) CreateFormFileReader(_ string, _ io.Reader, _ string) error { return f.err } func (f *failingFormBuilder) WriteField(_, _ string) error { return nil } func (f *failingFormBuilder) Close() error { return nil } func (f *failingFormBuilder) FormDataContentType() string { return "multipart/form-data" } // failingAudioRequestBuilder simulates an error during HTTP request construction. type failingAudioRequestBuilder struct{ err error } func (f *failingAudioRequestBuilder) Build( _ context.Context, _, _ string, _ any, _ http.Header, ) (*http.Request, error) { return nil, f.err } // errorHTTPClient always returns an error when making HTTP calls. type errorHTTPClient struct{ err error } func (e *errorHTTPClient) Do(_ *http.Request) (*http.Response, error) { return nil, e.err } func TestCallAudioAPIMultipartFormError(t *testing.T) { client := NewClient("test-token") errForm := errors.New("mock create form file failure") // Override form builder to force an error during multipart form creation. client.createFormBuilder = func(_ io.Writer) utils.FormBuilder { return &failingFormBuilder{err: errForm} } // Provide a reader so createFileField uses the reader path (no file open). req := AudioRequest{FilePath: "fake.mp3", Reader: bytes.NewBuffer([]byte("dummy")), Model: Whisper1} _, err := client.callAudioAPI(context.Background(), req, "transcriptions") if err == nil { t.Fatal("expected error but got none") } if !errors.Is(err, errForm) { t.Errorf("expected error %v, got %v", errForm, err) } } func TestCallAudioAPINewRequestError(t *testing.T) { client := NewClient("test-token") // Create a real temp file so multipart form succeeds. tmp := t.TempDir() path := filepath.Join(tmp, "file.mp3") if err := os.WriteFile(path, []byte("content"), 0644); err != nil { t.Fatalf("failed to write temp file: %v", err) } errBuild := errors.New("mock build failure") client.requestBuilder = &failingAudioRequestBuilder{err: errBuild} req := AudioRequest{FilePath: path, Model: Whisper1} _, err := client.callAudioAPI(context.Background(), req, "translations") if err == nil { t.Fatal("expected error but got none") } if !errors.Is(err, errBuild) { t.Errorf("expected error %v, got %v", errBuild, err) } } func TestCallAudioAPISendRequestErrorJSON(t *testing.T) { client := NewClient("test-token") // Create a real temp file so multipart form succeeds. tmp := t.TempDir() path := filepath.Join(tmp, "file.mp3") if err := os.WriteFile(path, []byte("content"), 0644); err != nil { t.Fatalf("failed to write temp file: %v", err) } errHTTP := errors.New("mock HTTPClient failure") // Override HTTP client to simulate a network error. client.config.HTTPClient = &errorHTTPClient{err: errHTTP} req := AudioRequest{FilePath: path, Model: Whisper1} _, err := client.callAudioAPI(context.Background(), req, "transcriptions") if err == nil { t.Fatal("expected error but got none") } if !errors.Is(err, errHTTP) { t.Errorf("expected error %v, got %v", errHTTP, err) } } func TestCallAudioAPISendRequestErrorText(t *testing.T) { client := NewClient("test-token") tmp := t.TempDir() path := filepath.Join(tmp, "file.mp3") if err := os.WriteFile(path, []byte("content"), 0644); err != nil { t.Fatalf("failed to write temp file: %v", err) } errHTTP := errors.New("mock HTTPClient failure") client.config.HTTPClient = &errorHTTPClient{err: errHTTP} // Use a non-JSON response format to exercise the text path. req := AudioRequest{FilePath: path, Model: Whisper1, Format: AudioResponseFormatText} _, err := client.callAudioAPI(context.Background(), req, "translations") if err == nil { t.Fatal("expected error but got none") } if !errors.Is(err, errHTTP) { t.Errorf("expected error %v, got %v", errHTTP, err) } } ================================================ FILE: batch.go ================================================ package openai import ( "bytes" "context" "encoding/json" "fmt" "net/http" "net/url" ) const batchesSuffix = "/batches" type BatchEndpoint string const ( BatchEndpointChatCompletions BatchEndpoint = "/v1/chat/completions" BatchEndpointCompletions BatchEndpoint = "/v1/completions" BatchEndpointEmbeddings BatchEndpoint = "/v1/embeddings" ) type BatchLineItem interface { MarshalBatchLineItem() []byte } type BatchChatCompletionRequest struct { CustomID string `json:"custom_id"` Body ChatCompletionRequest `json:"body"` Method string `json:"method"` URL BatchEndpoint `json:"url"` } func (r BatchChatCompletionRequest) MarshalBatchLineItem() []byte { marshal, _ := json.Marshal(r) return marshal } type BatchCompletionRequest struct { CustomID string `json:"custom_id"` Body CompletionRequest `json:"body"` Method string `json:"method"` URL BatchEndpoint `json:"url"` } func (r BatchCompletionRequest) MarshalBatchLineItem() []byte { marshal, _ := json.Marshal(r) return marshal } type BatchEmbeddingRequest struct { CustomID string `json:"custom_id"` Body EmbeddingRequest `json:"body"` Method string `json:"method"` URL BatchEndpoint `json:"url"` } func (r BatchEmbeddingRequest) MarshalBatchLineItem() []byte { marshal, _ := json.Marshal(r) return marshal } type Batch struct { ID string `json:"id"` Object string `json:"object"` Endpoint BatchEndpoint `json:"endpoint"` Errors *struct { Object string `json:"object,omitempty"` Data []struct { Code string `json:"code,omitempty"` Message string `json:"message,omitempty"` Param *string `json:"param,omitempty"` Line *int `json:"line,omitempty"` } `json:"data"` } `json:"errors"` InputFileID string `json:"input_file_id"` CompletionWindow string `json:"completion_window"` Status string `json:"status"` OutputFileID *string `json:"output_file_id"` ErrorFileID *string `json:"error_file_id"` CreatedAt int `json:"created_at"` InProgressAt *int `json:"in_progress_at"` ExpiresAt *int `json:"expires_at"` FinalizingAt *int `json:"finalizing_at"` CompletedAt *int `json:"completed_at"` FailedAt *int `json:"failed_at"` ExpiredAt *int `json:"expired_at"` CancellingAt *int `json:"cancelling_at"` CancelledAt *int `json:"cancelled_at"` RequestCounts BatchRequestCounts `json:"request_counts"` Metadata map[string]any `json:"metadata"` } type BatchRequestCounts struct { Total int `json:"total"` Completed int `json:"completed"` Failed int `json:"failed"` } type CreateBatchRequest struct { InputFileID string `json:"input_file_id"` Endpoint BatchEndpoint `json:"endpoint"` CompletionWindow string `json:"completion_window"` Metadata map[string]any `json:"metadata"` } type BatchResponse struct { httpHeader Batch } // CreateBatch — API call to Create batch. func (c *Client) CreateBatch( ctx context.Context, request CreateBatchRequest, ) (response BatchResponse, err error) { if request.CompletionWindow == "" { request.CompletionWindow = "24h" } req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(batchesSuffix), withBody(request)) if err != nil { return } err = c.sendRequest(req, &response) return } type UploadBatchFileRequest struct { FileName string Lines []BatchLineItem } func (r *UploadBatchFileRequest) MarshalJSONL() []byte { buff := bytes.Buffer{} for i, line := range r.Lines { if i != 0 { buff.Write([]byte("\n")) } buff.Write(line.MarshalBatchLineItem()) } return buff.Bytes() } func (r *UploadBatchFileRequest) AddChatCompletion(customerID string, body ChatCompletionRequest) { r.Lines = append(r.Lines, BatchChatCompletionRequest{ CustomID: customerID, Body: body, Method: "POST", URL: BatchEndpointChatCompletions, }) } func (r *UploadBatchFileRequest) AddCompletion(customerID string, body CompletionRequest) { r.Lines = append(r.Lines, BatchCompletionRequest{ CustomID: customerID, Body: body, Method: "POST", URL: BatchEndpointCompletions, }) } func (r *UploadBatchFileRequest) AddEmbedding(customerID string, body EmbeddingRequest) { r.Lines = append(r.Lines, BatchEmbeddingRequest{ CustomID: customerID, Body: body, Method: "POST", URL: BatchEndpointEmbeddings, }) } // UploadBatchFile — upload batch file. func (c *Client) UploadBatchFile(ctx context.Context, request UploadBatchFileRequest) (File, error) { if request.FileName == "" { request.FileName = "@batchinput.jsonl" } return c.CreateFileBytes(ctx, FileBytesRequest{ Name: request.FileName, Bytes: request.MarshalJSONL(), Purpose: PurposeBatch, }) } type CreateBatchWithUploadFileRequest struct { Endpoint BatchEndpoint `json:"endpoint"` CompletionWindow string `json:"completion_window"` Metadata map[string]any `json:"metadata"` UploadBatchFileRequest } // CreateBatchWithUploadFile — API call to Create batch with upload file. func (c *Client) CreateBatchWithUploadFile( ctx context.Context, request CreateBatchWithUploadFileRequest, ) (response BatchResponse, err error) { var file File file, err = c.UploadBatchFile(ctx, UploadBatchFileRequest{ FileName: request.FileName, Lines: request.Lines, }) if err != nil { return } return c.CreateBatch(ctx, CreateBatchRequest{ InputFileID: file.ID, Endpoint: request.Endpoint, CompletionWindow: request.CompletionWindow, Metadata: request.Metadata, }) } // RetrieveBatch — API call to Retrieve batch. func (c *Client) RetrieveBatch( ctx context.Context, batchID string, ) (response BatchResponse, err error) { urlSuffix := fmt.Sprintf("%s/%s", batchesSuffix, batchID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &response) return } // CancelBatch — API call to Cancel batch. func (c *Client) CancelBatch( ctx context.Context, batchID string, ) (response BatchResponse, err error) { urlSuffix := fmt.Sprintf("%s/%s/cancel", batchesSuffix, batchID) req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &response) return } type ListBatchResponse struct { httpHeader Object string `json:"object"` Data []Batch `json:"data"` FirstID string `json:"first_id"` LastID string `json:"last_id"` HasMore bool `json:"has_more"` } // ListBatch API call to List batch. func (c *Client) ListBatch(ctx context.Context, after *string, limit *int) (response ListBatchResponse, err error) { urlValues := url.Values{} if limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *limit)) } if after != nil { urlValues.Add("after", *after) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("%s%s", batchesSuffix, encodedValues) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: batch_test.go ================================================ package openai_test import ( "context" "fmt" "net/http" "reflect" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestUploadBatchFile(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files", handleCreateFile) req := openai.UploadBatchFileRequest{} req.AddChatCompletion("req-1", openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) _, err := client.UploadBatchFile(context.Background(), req) checks.NoError(t, err, "UploadBatchFile error") } func TestCreateBatch(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/batches", handleBatchEndpoint) _, err := client.CreateBatch(context.Background(), openai.CreateBatchRequest{ InputFileID: "file-abc", Endpoint: openai.BatchEndpointChatCompletions, CompletionWindow: "24h", }) checks.NoError(t, err, "CreateBatch error") } func TestCreateBatchWithUploadFile(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files", handleCreateFile) server.RegisterHandler("/v1/batches", handleBatchEndpoint) req := openai.CreateBatchWithUploadFileRequest{ Endpoint: openai.BatchEndpointChatCompletions, } req.AddChatCompletion("req-1", openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) _, err := client.CreateBatchWithUploadFile(context.Background(), req) checks.NoError(t, err, "CreateBatchWithUploadFile error") } func TestRetrieveBatch(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/batches/file-id-1", handleRetrieveBatchEndpoint) _, err := client.RetrieveBatch(context.Background(), "file-id-1") checks.NoError(t, err, "RetrieveBatch error") } func TestCancelBatch(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/batches/file-id-1/cancel", handleCancelBatchEndpoint) _, err := client.CancelBatch(context.Background(), "file-id-1") checks.NoError(t, err, "RetrieveBatch error") } func TestListBatch(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/batches", handleBatchEndpoint) after := "batch_abc123" limit := 10 _, err := client.ListBatch(context.Background(), &after, &limit) checks.NoError(t, err, "RetrieveBatch error") } func TestUploadBatchFileRequest_AddChatCompletion(t *testing.T) { type args struct { customerID string body openai.ChatCompletionRequest } tests := []struct { name string args []args want []byte }{ {"", []args{ { customerID: "req-1", body: openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }, }, { customerID: "req-2", body: openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }, }, }, []byte("{\"custom_id\":\"req-1\",\"body\":{\"model\":\"gpt-3.5-turbo\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello!\"}],\"max_tokens\":5},\"method\":\"POST\",\"url\":\"/v1/chat/completions\"}\n{\"custom_id\":\"req-2\",\"body\":{\"model\":\"gpt-3.5-turbo\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello!\"}],\"max_tokens\":5},\"method\":\"POST\",\"url\":\"/v1/chat/completions\"}")}, //nolint:lll } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := &openai.UploadBatchFileRequest{} for _, arg := range tt.args { r.AddChatCompletion(arg.customerID, arg.body) } got := r.MarshalJSONL() if !reflect.DeepEqual(got, tt.want) { t.Errorf("Marshal() got = %v, want %v", got, tt.want) } }) } } func TestUploadBatchFileRequest_AddCompletion(t *testing.T) { type args struct { customerID string body openai.CompletionRequest } tests := []struct { name string args []args want []byte }{ {"", []args{ { customerID: "req-1", body: openai.CompletionRequest{ Model: openai.GPT3Dot5Turbo, User: "Hello", }, }, { customerID: "req-2", body: openai.CompletionRequest{ Model: openai.GPT3Dot5Turbo, User: "Hello", }, }, }, []byte("{\"custom_id\":\"req-1\",\"body\":{\"model\":\"gpt-3.5-turbo\",\"user\":\"Hello\"},\"method\":\"POST\",\"url\":\"/v1/completions\"}\n{\"custom_id\":\"req-2\",\"body\":{\"model\":\"gpt-3.5-turbo\",\"user\":\"Hello\"},\"method\":\"POST\",\"url\":\"/v1/completions\"}")}, //nolint:lll } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := &openai.UploadBatchFileRequest{} for _, arg := range tt.args { r.AddCompletion(arg.customerID, arg.body) } got := r.MarshalJSONL() if !reflect.DeepEqual(got, tt.want) { t.Errorf("Marshal() got = %v, want %v", got, tt.want) } }) } } func TestUploadBatchFileRequest_AddEmbedding(t *testing.T) { type args struct { customerID string body openai.EmbeddingRequest } tests := []struct { name string args []args want []byte }{ {"", []args{ { customerID: "req-1", body: openai.EmbeddingRequest{ Model: openai.GPT3Dot5Turbo, Input: []string{"Hello", "World"}, }, }, { customerID: "req-2", body: openai.EmbeddingRequest{ Model: openai.AdaEmbeddingV2, Input: []string{"Hello", "World"}, }, }, }, []byte("{\"custom_id\":\"req-1\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"gpt-3.5-turbo\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}\n{\"custom_id\":\"req-2\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"text-embedding-ada-002\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}")}, //nolint:lll } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := &openai.UploadBatchFileRequest{} for _, arg := range tt.args { r.AddEmbedding(arg.customerID, arg.body) } got := r.MarshalJSONL() if !reflect.DeepEqual(got, tt.want) { t.Errorf("Marshal() got = %v, want %v", got, tt.want) } }) } } func handleBatchEndpoint(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { _, _ = fmt.Fprintln(w, `{ "id": "batch_abc123", "object": "batch", "endpoint": "/v1/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "completed", "output_file_id": "file-cvaTdG", "error_file_id": "file-HOWS94", "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": 1711493133, "completed_at": 1711493163, "failed_at": null, "expired_at": null, "cancelling_at": null, "cancelled_at": null, "request_counts": { "total": 100, "completed": 95, "failed": 5 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly eval job" } }`) } else if r.Method == http.MethodGet { _, _ = fmt.Fprintln(w, `{ "object": "list", "data": [ { "id": "batch_abc123", "object": "batch", "endpoint": "/v1/chat/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "completed", "output_file_id": "file-cvaTdG", "error_file_id": "file-HOWS94", "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": 1711493133, "completed_at": 1711493163, "failed_at": null, "expired_at": null, "cancelling_at": null, "cancelled_at": null, "request_counts": { "total": 100, "completed": 95, "failed": 5 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly job" } } ], "first_id": "batch_abc123", "last_id": "batch_abc456", "has_more": true }`) } } func handleRetrieveBatchEndpoint(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { _, _ = fmt.Fprintln(w, `{ "id": "batch_abc123", "object": "batch", "endpoint": "/v1/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "completed", "output_file_id": "file-cvaTdG", "error_file_id": "file-HOWS94", "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": 1711493133, "completed_at": 1711493163, "failed_at": null, "expired_at": null, "cancelling_at": null, "cancelled_at": null, "request_counts": { "total": 100, "completed": 95, "failed": 5 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly eval job" } }`) } } func handleCancelBatchEndpoint(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { _, _ = fmt.Fprintln(w, `{ "id": "batch_abc123", "object": "batch", "endpoint": "/v1/chat/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "cancelling", "output_file_id": null, "error_file_id": null, "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": null, "completed_at": null, "failed_at": null, "expired_at": null, "cancelling_at": 1711475133, "cancelled_at": null, "request_counts": { "total": 100, "completed": 23, "failed": 1 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly eval job" } }`) } } ================================================ FILE: chat.go ================================================ package openai import ( "context" "encoding/json" "errors" "net/http" "github.com/sashabaranov/go-openai/jsonschema" ) // Chat message role defined by the OpenAI API. const ( ChatMessageRoleSystem = "system" ChatMessageRoleUser = "user" ChatMessageRoleAssistant = "assistant" ChatMessageRoleFunction = "function" ChatMessageRoleTool = "tool" ChatMessageRoleDeveloper = "developer" ) const chatCompletionsSuffix = "/chat/completions" var ( ErrChatCompletionInvalidModel = errors.New("this model is not supported with this method, please use CreateCompletion client method instead") //nolint:lll ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream") //nolint:lll ErrContentFieldsMisused = errors.New("can't use both Content and MultiContent properties simultaneously") ) type Hate struct { Filtered bool `json:"filtered"` Severity string `json:"severity,omitempty"` } type SelfHarm struct { Filtered bool `json:"filtered"` Severity string `json:"severity,omitempty"` } type Sexual struct { Filtered bool `json:"filtered"` Severity string `json:"severity,omitempty"` } type Violence struct { Filtered bool `json:"filtered"` Severity string `json:"severity,omitempty"` } type JailBreak struct { Filtered bool `json:"filtered"` Detected bool `json:"detected"` } type Profanity struct { Filtered bool `json:"filtered"` Detected bool `json:"detected"` } type ContentFilterResults struct { Hate Hate `json:"hate,omitempty"` SelfHarm SelfHarm `json:"self_harm,omitempty"` Sexual Sexual `json:"sexual,omitempty"` Violence Violence `json:"violence,omitempty"` JailBreak JailBreak `json:"jailbreak,omitempty"` Profanity Profanity `json:"profanity,omitempty"` } type PromptAnnotation struct { PromptIndex int `json:"prompt_index,omitempty"` ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` } type ImageURLDetail string const ( ImageURLDetailHigh ImageURLDetail = "high" ImageURLDetailLow ImageURLDetail = "low" ImageURLDetailAuto ImageURLDetail = "auto" ) type ChatMessageImageURL struct { URL string `json:"url,omitempty"` Detail ImageURLDetail `json:"detail,omitempty"` } type ChatMessagePartType string const ( ChatMessagePartTypeText ChatMessagePartType = "text" ChatMessagePartTypeImageURL ChatMessagePartType = "image_url" ) type ChatMessagePart struct { Type ChatMessagePartType `json:"type,omitempty"` Text string `json:"text,omitempty"` ImageURL *ChatMessageImageURL `json:"image_url,omitempty"` } type ChatCompletionMessage struct { Role string `json:"role"` Content string `json:"content,omitempty"` Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart // This property isn't in the official documentation, but it's in // the documentation for the official library for python: // - https://github.com/openai/openai-python/blob/main/chatml.md // - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb Name string `json:"name,omitempty"` // This property is used for the "reasoning" feature supported by deepseek-reasoner // which is not in the official documentation. // the doc from deepseek: // - https://api-docs.deepseek.com/api/create-chat-completion#responses ReasoningContent string `json:"reasoning_content,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` // For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls. ToolCalls []ToolCall `json:"tool_calls,omitempty"` // For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool. ToolCallID string `json:"tool_call_id,omitempty"` } func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) { if m.Content != "" && m.MultiContent != nil { return nil, ErrContentFieldsMisused } if len(m.MultiContent) > 0 { msg := struct { Role string `json:"role"` Content string `json:"-"` Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart `json:"content,omitempty"` Name string `json:"name,omitempty"` ReasoningContent string `json:"reasoning_content,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` ToolCalls []ToolCall `json:"tool_calls,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"` }(m) return json.Marshal(msg) } msg := struct { Role string `json:"role"` Content string `json:"content,omitempty"` Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart `json:"-"` Name string `json:"name,omitempty"` ReasoningContent string `json:"reasoning_content,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` ToolCalls []ToolCall `json:"tool_calls,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"` }(m) return json.Marshal(msg) } func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error { msg := struct { Role string `json:"role"` Content string `json:"content"` Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart Name string `json:"name,omitempty"` ReasoningContent string `json:"reasoning_content,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` ToolCalls []ToolCall `json:"tool_calls,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"` }{} if err := json.Unmarshal(bs, &msg); err == nil { *m = ChatCompletionMessage(msg) return nil } multiMsg := struct { Role string `json:"role"` Content string Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart `json:"content"` Name string `json:"name,omitempty"` ReasoningContent string `json:"reasoning_content,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` ToolCalls []ToolCall `json:"tool_calls,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"` }{} if err := json.Unmarshal(bs, &multiMsg); err != nil { return err } *m = ChatCompletionMessage(multiMsg) return nil } type ToolCall struct { // Index is not nil only in chat completion chunk object Index *int `json:"index,omitempty"` ID string `json:"id,omitempty"` Type ToolType `json:"type"` Function FunctionCall `json:"function"` } type FunctionCall struct { Name string `json:"name,omitempty"` // call function with arguments in JSON format Arguments string `json:"arguments,omitempty"` } type ChatCompletionResponseFormatType string const ( ChatCompletionResponseFormatTypeJSONObject ChatCompletionResponseFormatType = "json_object" ChatCompletionResponseFormatTypeJSONSchema ChatCompletionResponseFormatType = "json_schema" ChatCompletionResponseFormatTypeText ChatCompletionResponseFormatType = "text" ) type ChatCompletionResponseFormat struct { Type ChatCompletionResponseFormatType `json:"type,omitempty"` JSONSchema *ChatCompletionResponseFormatJSONSchema `json:"json_schema,omitempty"` } type ChatCompletionResponseFormatJSONSchema struct { Name string `json:"name"` Description string `json:"description,omitempty"` Schema json.Marshaler `json:"schema"` Strict bool `json:"strict"` } func (r *ChatCompletionResponseFormatJSONSchema) UnmarshalJSON(data []byte) error { type rawJSONSchema struct { Name string `json:"name"` Description string `json:"description,omitempty"` Schema json.RawMessage `json:"schema"` Strict bool `json:"strict"` } var raw rawJSONSchema if err := json.Unmarshal(data, &raw); err != nil { return err } r.Name = raw.Name r.Description = raw.Description r.Strict = raw.Strict if len(raw.Schema) > 0 && string(raw.Schema) != "null" { var d jsonschema.Definition err := json.Unmarshal(raw.Schema, &d) if err != nil { return err } r.Schema = &d } return nil } // ChatCompletionRequestExtensions contains third-party OpenAI API extensions // (e.g., vendor-specific implementations like vLLM). type ChatCompletionRequestExtensions struct { // GuidedChoice is a vLLM-specific extension that restricts the model's output // to one of the predefined string choices provided in this field. This feature // is used to constrain the model's responses to a controlled set of options, // ensuring predictable and consistent outputs in scenarios where specific // choices are required. GuidedChoice []string `json:"guided_choice,omitempty"` } // ChatCompletionRequest represents a request structure for chat completion API. type ChatCompletionRequest struct { Model string `json:"model"` Messages []ChatCompletionMessage `json:"messages"` // MaxTokens The maximum number of tokens that can be generated in the chat completion. // This value can be used to control costs for text generated via API. // Deprecated: use MaxCompletionTokens. Not compatible with o1-series models. // refs: https://platform.openai.com/docs/api-reference/chat/create#chat-create-max_tokens MaxTokens int `json:"max_tokens,omitempty"` // 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"` TopP float32 `json:"top_p,omitempty"` N int `json:"n,omitempty"` Stream bool `json:"stream,omitempty"` Stop []string `json:"stop,omitempty"` PresencePenalty float32 `json:"presence_penalty,omitempty"` ResponseFormat *ChatCompletionResponseFormat `json:"response_format,omitempty"` Seed *int `json:"seed,omitempty"` FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string. // incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}` // refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias LogitBias map[string]int `json:"logit_bias,omitempty"` // LogProbs indicates whether to return log probabilities of the output tokens or not. // If true, returns the log probabilities of each output token returned in the content of message. // This option is currently not available on the gpt-4-vision-preview model. LogProbs bool `json:"logprobs,omitempty"` // TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each // token position, each with an associated log probability. // logprobs must be set to true if this parameter is used. TopLogProbs int `json:"top_logprobs,omitempty"` User string `json:"user,omitempty"` // Deprecated: use Tools instead. Functions []FunctionDefinition `json:"functions,omitempty"` // Deprecated: use ToolChoice instead. FunctionCall any `json:"function_call,omitempty"` Tools []Tool `json:"tools,omitempty"` // This can be either a string or an ToolChoice object. ToolChoice any `json:"tool_choice,omitempty"` // Options for streaming response. Only set this when you set stream: true. StreamOptions *StreamOptions `json:"stream_options,omitempty"` // Disable the default behavior of parallel tool calls by setting it: false. ParallelToolCalls any `json:"parallel_tool_calls,omitempty"` // Store can be set to true to store the output of this completion request for use in distillations and evals. // https://platform.openai.com/docs/api-reference/chat/create#chat-create-store Store bool `json:"store,omitempty"` // Controls effort on reasoning for reasoning models. It can be set to "low", "medium", or "high". ReasoningEffort string `json:"reasoning_effort,omitempty"` // Metadata to store with the completion. Metadata map[string]string `json:"metadata,omitempty"` // Configuration for a predicted output. Prediction *Prediction `json:"prediction,omitempty"` // ChatTemplateKwargs provides a way to add non-standard parameters to the request body. // Additional kwargs to pass to the template renderer. Will be accessible by the chat template. // Such as think mode for qwen3. "chat_template_kwargs": {"enable_thinking": false} // https://qwen.readthedocs.io/en/latest/deployment/vllm.html#thinking-non-thinking-modes 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"` // Verbosity determines how many output tokens are generated. Lowering the number of // tokens reduces overall latency. It can be set to "low", "medium", or "high". // Note: This field is only confirmed to work with gpt-5, gpt-5-mini and gpt-5-nano. // Also, it is not in the API reference of chat completion at the time of writing, // though it is supported by the API. Verbosity string `json:"verbosity,omitempty"` // A stable identifier used to help detect users of your application that may be violating OpenAI's usage policies. // The IDs should be a string that uniquely identifies each user. // We recommend hashing their username or email address, in order to avoid sending us any identifying information. // https://platform.openai.com/docs/api-reference/chat/create#chat_create-safety_identifier SafetyIdentifier string `json:"safety_identifier,omitempty"` // Embedded struct for non-OpenAI extensions ChatCompletionRequestExtensions } type StreamOptions struct { // If set, an additional chunk will be streamed before the data: [DONE] message. // The usage field on this chunk shows the token usage statistics for the entire request, // and the choices field will always be an empty array. // All other chunks will also include a usage field, but with a null value. IncludeUsage bool `json:"include_usage,omitempty"` } type ToolType string const ( ToolTypeFunction ToolType = "function" ) type Tool struct { Type ToolType `json:"type"` Function *FunctionDefinition `json:"function,omitempty"` } type ToolChoice struct { Type ToolType `json:"type"` Function ToolFunction `json:"function,omitempty"` } type ToolFunction struct { Name string `json:"name"` } type FunctionDefinition struct { Name string `json:"name"` Description string `json:"description,omitempty"` Strict bool `json:"strict,omitempty"` // Parameters is an object describing the function. // You can pass json.RawMessage to describe the schema, // or you can pass in a struct which serializes to the proper JSON schema. // The jsonschema package is provided for convenience, but you should // consider another specialized library if you require more complex schemas. Parameters any `json:"parameters"` } // Deprecated: use FunctionDefinition instead. type FunctionDefine = FunctionDefinition type TopLogProbs struct { Token string `json:"token"` LogProb float64 `json:"logprob"` Bytes []byte `json:"bytes,omitempty"` } // LogProb represents the probability information for a token. type LogProb struct { Token string `json:"token"` LogProb float64 `json:"logprob"` Bytes []byte `json:"bytes,omitempty"` // Omitting the field if it is null // TopLogProbs is a list of the most likely tokens and their log probability, at this token position. // In rare cases, there may be fewer than the number of requested top_logprobs returned. TopLogProbs []TopLogProbs `json:"top_logprobs"` } // LogProbs is the top-level structure containing the log probability information. type LogProbs struct { // Content is a list of message content tokens with log probability information. Content []LogProb `json:"content"` } type Prediction struct { Content string `json:"content"` Type string `json:"type"` } type FinishReason string const ( FinishReasonStop FinishReason = "stop" FinishReasonLength FinishReason = "length" FinishReasonFunctionCall FinishReason = "function_call" FinishReasonToolCalls FinishReason = "tool_calls" FinishReasonContentFilter FinishReason = "content_filter" FinishReasonNull FinishReason = "null" ) type ServiceTier string const ( ServiceTierAuto ServiceTier = "auto" ServiceTierDefault ServiceTier = "default" ServiceTierFlex ServiceTier = "flex" ServiceTierPriority ServiceTier = "priority" ) func (r FinishReason) MarshalJSON() ([]byte, error) { if r == FinishReasonNull || r == "" { return []byte("null"), nil } return []byte(`"` + string(r) + `"`), nil // best effort to not break future API changes } type ChatCompletionChoice struct { Index int `json:"index"` Message ChatCompletionMessage `json:"message"` // FinishReason // stop: API returned complete message, // or a message terminated by one of the stop sequences provided via the stop parameter // length: Incomplete model output due to max_tokens parameter or token limit // function_call: The model decided to call a function // content_filter: Omitted content due to a flag from our content filters // null: API response still in progress or incomplete FinishReason FinishReason `json:"finish_reason"` LogProbs *LogProbs `json:"logprobs,omitempty"` ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` } // ChatCompletionResponse represents a response structure for chat completion API. type ChatCompletionResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Choices []ChatCompletionChoice `json:"choices"` Usage Usage `json:"usage"` SystemFingerprint string `json:"system_fingerprint"` PromptFilterResults []PromptFilterResult `json:"prompt_filter_results,omitempty"` ServiceTier ServiceTier `json:"service_tier,omitempty"` httpHeader } // CreateChatCompletion — API call to Create a completion for the chat message. func (c *Client) CreateChatCompletion( ctx context.Context, request ChatCompletionRequest, ) (response ChatCompletionResponse, err error) { if request.Stream { err = ErrChatCompletionStreamNotSupported return } urlSuffix := chatCompletionsSuffix if !checkEndpointSupportsModel(urlSuffix, request.Model) { err = ErrChatCompletionInvalidModel return } reasoningValidator := NewReasoningValidator() if err = reasoningValidator.Validate(request); err != nil { return } req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix, withModel(request.Model)), withBody(request), ) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: chat_stream.go ================================================ package openai import ( "context" "net/http" ) type ChatCompletionStreamChoiceDelta struct { Content string `json:"content,omitempty"` Role string `json:"role,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` ToolCalls []ToolCall `json:"tool_calls,omitempty"` Refusal string `json:"refusal,omitempty"` // This property is used for the "reasoning" feature supported by deepseek-reasoner // which is not in the official documentation. // the doc from deepseek: // - https://api-docs.deepseek.com/api/create-chat-completion#responses ReasoningContent string `json:"reasoning_content,omitempty"` } type ChatCompletionStreamChoiceLogprobs struct { Content []ChatCompletionTokenLogprob `json:"content,omitempty"` Refusal []ChatCompletionTokenLogprob `json:"refusal,omitempty"` } type ChatCompletionTokenLogprob struct { Token string `json:"token"` Bytes []int64 `json:"bytes,omitempty"` Logprob float64 `json:"logprob,omitempty"` TopLogprobs []ChatCompletionTokenLogprobTopLogprob `json:"top_logprobs"` } type ChatCompletionTokenLogprobTopLogprob struct { Token string `json:"token"` Bytes []int64 `json:"bytes"` Logprob float64 `json:"logprob"` } type ChatCompletionStreamChoice struct { Index int `json:"index"` Delta ChatCompletionStreamChoiceDelta `json:"delta"` Logprobs *ChatCompletionStreamChoiceLogprobs `json:"logprobs,omitempty"` FinishReason FinishReason `json:"finish_reason"` ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` } type PromptFilterResult struct { Index int `json:"index"` ContentFilterResults ContentFilterResults `json:"content_filter_results,omitempty"` } type ChatCompletionStreamResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Choices []ChatCompletionStreamChoice `json:"choices"` SystemFingerprint string `json:"system_fingerprint"` PromptAnnotations []PromptAnnotation `json:"prompt_annotations,omitempty"` PromptFilterResults []PromptFilterResult `json:"prompt_filter_results,omitempty"` // An optional field that will only be present when you set stream_options: {"include_usage": true} in your request. // When present, it contains a null value except for the last chunk which contains the token usage statistics // for the entire request. Usage *Usage `json:"usage,omitempty"` } // ChatCompletionStream // Note: Perhaps it is more elegant to abstract Stream using generics. type ChatCompletionStream struct { *streamReader[ChatCompletionStreamResponse] } // CreateChatCompletionStream — API call to create a chat completion w/ streaming // support. It sets whether to stream back partial progress. If set, tokens will be // sent as data-only server-sent events as they become available, with the // stream terminated by a data: [DONE] message. func (c *Client) CreateChatCompletionStream( ctx context.Context, request ChatCompletionRequest, ) (stream *ChatCompletionStream, err error) { urlSuffix := chatCompletionsSuffix if !checkEndpointSupportsModel(urlSuffix, request.Model) { err = ErrChatCompletionInvalidModel return } request.Stream = true reasoningValidator := NewReasoningValidator() if err = reasoningValidator.Validate(request); err != nil { return } req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix, withModel(request.Model)), withBody(request), ) if err != nil { return nil, err } resp, err := sendRequestStream[ChatCompletionStreamResponse](c, req) if err != nil { return } stream = &ChatCompletionStream{ streamReader: resp, } return } ================================================ FILE: chat_stream_test.go ================================================ package openai_test import ( "context" "encoding/json" "errors" "fmt" "io" "net/http" "strconv" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestChatCompletionsStreamWrongModel(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) ctx := context.Background() req := openai.ChatCompletionRequest{ MaxTokens: 5, Model: "ada", Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, } _, err := client.CreateChatCompletionStream(ctx, req) if !errors.Is(err, openai.ErrChatCompletionInvalidModel) { t.Fatalf("CreateChatCompletion should return ErrChatCompletionInvalidModel, but returned: %v", err) } } func TestCreateChatCompletionStream(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data := `{"id":"1","object":"completion","created":1598069254,"model":"gpt-3.5-turbo","system_fingerprint": "fp_d9767fc5b9","choices":[{"index":0,"delta":{"content":"response1"},"finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data = `{"id":"2","object":"completion","created":1598069255,"model":"gpt-3.5-turbo","system_fingerprint": "fp_d9767fc5b9","choices":[{"index":0,"delta":{"content":"response2"},"finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) dataBytes = append(dataBytes, []byte("event: done\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() expectedResponses := []openai.ChatCompletionStreamResponse{ { ID: "1", Object: "completion", Created: 1598069254, Model: openai.GPT3Dot5Turbo, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Delta: openai.ChatCompletionStreamChoiceDelta{ Content: "response1", }, FinishReason: "max_tokens", }, }, }, { ID: "2", Object: "completion", Created: 1598069255, Model: openai.GPT3Dot5Turbo, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Delta: openai.ChatCompletionStreamChoiceDelta{ Content: "response2", }, FinishReason: "max_tokens", }, }, }, } for ix, expectedResponse := range expectedResponses { b, _ := json.Marshal(expectedResponse) t.Logf("%d: %s", ix, string(b)) receivedResponse, streamErr := stream.Recv() checks.NoError(t, streamErr, "stream.Recv() failed") if !compareChatResponses(expectedResponse, receivedResponse) { t.Errorf("Stream response %v is %v, expected %v", ix, receivedResponse, expectedResponse) } } _, streamErr := stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr) } _, streamErr = stream.Recv() checks.ErrorIs(t, streamErr, io.EOF, "stream.Recv() did not return EOF when the stream is finished") if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF when the stream is finished: %v", streamErr) } } func TestCreateChatCompletionStreamError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} dataStr := []string{ `{`, `"error": {`, `"message": "Incorrect API key provided: sk-***************************************",`, `"type": "invalid_request_error",`, `"param": null,`, `"code": "invalid_api_key"`, `}`, `}`, } for _, str := range dataStr { dataBytes = append(dataBytes, []byte(str+"\n")...) } _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() _, streamErr := stream.Recv() checks.HasError(t, streamErr, "stream.Recv() did not return error") var apiErr *openai.APIError if !errors.As(streamErr, &apiErr) { t.Errorf("stream.Recv() did not return APIError") } t.Logf("%+v\n", apiErr) } func TestCreateChatCompletionStreamWithHeaders(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") w.Header().Set(xCustomHeader, xCustomHeaderValue) // Send test responses //nolint:lll dataBytes := []byte(`data: {"error":{"message":"The server had an error while processing your request. Sorry about that!", "type":"server_ error", "param":null,"code":null}}`) dataBytes = append(dataBytes, []byte("\n\ndata: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() value := stream.Header().Get(xCustomHeader) if value != xCustomHeaderValue { t.Errorf("expected %s to be %s", xCustomHeaderValue, value) } } func TestCreateChatCompletionStreamWithRatelimitHeaders(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") for k, v := range rateLimitHeaders { switch val := v.(type) { case int: w.Header().Set(k, strconv.Itoa(val)) default: w.Header().Set(k, fmt.Sprintf("%s", v)) } } // Send test responses //nolint:lll dataBytes := []byte(`data: {"error":{"message":"The server had an error while processing your request. Sorry about that!", "type":"server_ error", "param":null,"code":null}}`) dataBytes = append(dataBytes, []byte("\n\ndata: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() headers := stream.GetRateLimitHeaders() bs1, _ := json.Marshal(headers) bs2, _ := json.Marshal(rateLimitHeaders) if string(bs1) != string(bs2) { t.Errorf("expected rate limit header %s to be %s", bs2, bs1) } } func TestCreateChatCompletionStreamErrorWithDataPrefix(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses //nolint:lll dataBytes := []byte(`data: {"error":{"message":"The server had an error while processing your request. Sorry about that!", "type":"server_ error", "param":null,"code":null}}`) dataBytes = append(dataBytes, []byte("\n\ndata: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() _, streamErr := stream.Recv() checks.HasError(t, streamErr, "stream.Recv() did not return error") var apiErr *openai.APIError if !errors.As(streamErr, &apiErr) { t.Errorf("stream.Recv() did not return APIError") } t.Logf("%+v\n", apiErr) } func TestCreateChatCompletionStreamRateLimitError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(429) // Send test responses dataBytes := []byte(`{"error":{` + `"message": "You are sending requests too quickly.",` + `"type":"rate_limit_reached",` + `"param":null,` + `"code":"rate_limit_reached"}}`) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) _, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) var apiErr *openai.APIError if !errors.As(err, &apiErr) { t.Errorf("TestCreateChatCompletionStreamRateLimitError did not return APIError") } t.Logf("%+v\n", apiErr) } func TestCreateChatCompletionStreamWithRefusal(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") dataBytes := []byte{} //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"1","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"2","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{"refusal":"Hello"},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"3","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{"refusal":" World"},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"4","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 2000, Model: openai.GPT4oMini20240718, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() expectedResponses := []openai.ChatCompletionStreamResponse{ { ID: "1", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{}, }, }, }, { ID: "2", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Refusal: "Hello", }, }, }, }, { ID: "3", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Refusal: " World", }, }, }, }, { ID: "4", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, FinishReason: "stop", }, }, }, } for ix, expectedResponse := range expectedResponses { b, _ := json.Marshal(expectedResponse) t.Logf("%d: %s", ix, string(b)) receivedResponse, streamErr := stream.Recv() checks.NoError(t, streamErr, "stream.Recv() failed") if !compareChatResponses(expectedResponse, receivedResponse) { t.Errorf("Stream response %v is %v, expected %v", ix, receivedResponse, expectedResponse) } } _, streamErr := stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr) } } func TestCreateChatCompletionStreamWithLogprobs(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"1","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":{"content":[],"refusal":null},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"2","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":{"content":[{"token":"Hello","logprob":-0.000020458236,"bytes":[72,101,108,108,111],"top_logprobs":[]}],"refusal":null},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"3","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{"content":" World"},"logprobs":{"content":[{"token":" World","logprob":-0.00055303273,"bytes":[32,87,111,114,108,100],"top_logprobs":[]}],"refusal":null},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"4","object":"chat.completion.chunk","created":1729585728,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_d9767fc5b9","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 2000, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() expectedResponses := []openai.ChatCompletionStreamResponse{ { ID: "1", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{}, Logprobs: &openai.ChatCompletionStreamChoiceLogprobs{ Content: []openai.ChatCompletionTokenLogprob{}, }, }, }, }, { ID: "2", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Content: "Hello", }, Logprobs: &openai.ChatCompletionStreamChoiceLogprobs{ Content: []openai.ChatCompletionTokenLogprob{ { Token: "Hello", Logprob: -0.000020458236, Bytes: []int64{72, 101, 108, 108, 111}, TopLogprobs: []openai.ChatCompletionTokenLogprobTopLogprob{}, }, }, }, }, }, }, { ID: "3", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Content: " World", }, Logprobs: &openai.ChatCompletionStreamChoiceLogprobs{ Content: []openai.ChatCompletionTokenLogprob{ { Token: " World", Logprob: -0.00055303273, Bytes: []int64{32, 87, 111, 114, 108, 100}, TopLogprobs: []openai.ChatCompletionTokenLogprobTopLogprob{}, }, }, }, }, }, }, { ID: "4", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.GPT4oMini20240718, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{}, FinishReason: "stop", }, }, }, } for ix, expectedResponse := range expectedResponses { b, _ := json.Marshal(expectedResponse) t.Logf("%d: %s", ix, string(b)) receivedResponse, streamErr := stream.Recv() checks.NoError(t, streamErr, "stream.Recv() failed") if !compareChatResponses(expectedResponse, receivedResponse) { t.Errorf("Stream response %v is %v, expected %v", ix, receivedResponse, expectedResponse) } } _, streamErr := stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr) } } func TestAzureCreateChatCompletionStreamRateLimitError(t *testing.T) { wantCode := "429" wantMessage := "Requests to the Creates a completion for the chat message Operation under Azure OpenAI API " + "version 2023-03-15-preview have exceeded token rate limit of your current OpenAI S0 pricing tier. " + "Please retry after 20 seconds. " + "Please go here: https://aka.ms/oai/quotaincrease if you would like to further increase the default rate limit." client, server, teardown := setupAzureTestServer() defer teardown() server.RegisterHandler("/openai/deployments/gpt-35-turbo/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusTooManyRequests) // Send test responses dataBytes := []byte(`{"error": { "code": "` + wantCode + `", "message": "` + wantMessage + `"}}`) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) apiErr := &openai.APIError{} _, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) if !errors.As(err, &apiErr) { t.Errorf("Did not return APIError: %+v\n", apiErr) return } if apiErr.HTTPStatusCode != http.StatusTooManyRequests { t.Errorf("Did not return HTTPStatusCode got = %d, want = %d\n", apiErr.HTTPStatusCode, http.StatusTooManyRequests) return } code, ok := apiErr.Code.(string) if !ok || code != wantCode { t.Errorf("Did not return Code. got = %v, want = %s\n", apiErr.Code, wantCode) return } if apiErr.Message != wantMessage { t.Errorf("Did not return Message. got = %s, want = %s\n", apiErr.Message, wantMessage) return } } func TestCreateChatCompletionStreamStreamOptions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses var dataBytes []byte //nolint:lll data := `{"id":"1","object":"completion","created":1598069254,"model":"gpt-3.5-turbo","system_fingerprint": "fp_d9767fc5b9","choices":[{"index":0,"delta":{"content":"response1"},"finish_reason":"max_tokens"}],"usage":null}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) //nolint:lll data = `{"id":"2","object":"completion","created":1598069255,"model":"gpt-3.5-turbo","system_fingerprint": "fp_d9767fc5b9","choices":[{"index":0,"delta":{"content":"response2"},"finish_reason":"max_tokens"}],"usage":null}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) //nolint:lll data = `{"id":"3","object":"completion","created":1598069256,"model":"gpt-3.5-turbo","system_fingerprint": "fp_d9767fc5b9","choices":[],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, StreamOptions: &openai.StreamOptions{ IncludeUsage: true, }, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() expectedResponses := []openai.ChatCompletionStreamResponse{ { ID: "1", Object: "completion", Created: 1598069254, Model: openai.GPT3Dot5Turbo, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Delta: openai.ChatCompletionStreamChoiceDelta{ Content: "response1", }, FinishReason: "max_tokens", }, }, }, { ID: "2", Object: "completion", Created: 1598069255, Model: openai.GPT3Dot5Turbo, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{ { Delta: openai.ChatCompletionStreamChoiceDelta{ Content: "response2", }, FinishReason: "max_tokens", }, }, }, { ID: "3", Object: "completion", Created: 1598069256, Model: openai.GPT3Dot5Turbo, SystemFingerprint: "fp_d9767fc5b9", Choices: []openai.ChatCompletionStreamChoice{}, Usage: &openai.Usage{ PromptTokens: 1, CompletionTokens: 1, TotalTokens: 2, }, }, } for ix, expectedResponse := range expectedResponses { b, _ := json.Marshal(expectedResponse) t.Logf("%d: %s", ix, string(b)) receivedResponse, streamErr := stream.Recv() checks.NoError(t, streamErr, "stream.Recv() failed") if !compareChatResponses(expectedResponse, receivedResponse) { t.Errorf("Stream response %v is %v, expected %v", ix, receivedResponse, expectedResponse) } } _, streamErr := stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr) } _, streamErr = stream.Recv() checks.ErrorIs(t, streamErr, io.EOF, "stream.Recv() did not return EOF when the stream is finished") if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF when the stream is finished: %v", streamErr) } } // Helper funcs. func compareChatResponses(r1, r2 openai.ChatCompletionStreamResponse) bool { if r1.ID != r2.ID || r1.Object != r2.Object || r1.Created != r2.Created || r1.Model != r2.Model { return false } if len(r1.Choices) != len(r2.Choices) { return false } for i := range r1.Choices { if !compareChatStreamResponseChoices(r1.Choices[i], r2.Choices[i]) { return false } } if r1.Usage != nil || r2.Usage != nil { if r1.Usage == nil || r2.Usage == nil { return false } if r1.Usage.PromptTokens != r2.Usage.PromptTokens || r1.Usage.CompletionTokens != r2.Usage.CompletionTokens || r1.Usage.TotalTokens != r2.Usage.TotalTokens { return false } } return true } func TestCreateChatCompletionStreamWithReasoningModel(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") dataBytes := []byte{} //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"1","object":"chat.completion.chunk","created":1729585728,"model":"o3-mini-2025-01-31","system_fingerprint":"fp_mini","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"2","object":"chat.completion.chunk","created":1729585728,"model":"o3-mini-2025-01-31","system_fingerprint":"fp_mini","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"3","object":"chat.completion.chunk","created":1729585728,"model":"o3-mini-2025-01-31","system_fingerprint":"fp_mini","choices":[{"index":0,"delta":{"content":" from"},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"4","object":"chat.completion.chunk","created":1729585728,"model":"o3-mini-2025-01-31","system_fingerprint":"fp_mini","choices":[{"index":0,"delta":{"content":" O3Mini"},"finish_reason":null}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) //nolint:lll dataBytes = append(dataBytes, []byte(`data: {"id":"5","object":"chat.completion.chunk","created":1729585728,"model":"o3-mini-2025-01-31","system_fingerprint":"fp_mini","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`)...) dataBytes = append(dataBytes, []byte("\n\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxCompletionTokens: 2000, Model: openai.O3Mini20250131, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() expectedResponses := []openai.ChatCompletionStreamResponse{ { ID: "1", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.O3Mini20250131, SystemFingerprint: "fp_mini", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Role: "assistant", }, }, }, }, { ID: "2", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.O3Mini20250131, SystemFingerprint: "fp_mini", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Content: "Hello", }, }, }, }, { ID: "3", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.O3Mini20250131, SystemFingerprint: "fp_mini", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Content: " from", }, }, }, }, { ID: "4", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.O3Mini20250131, SystemFingerprint: "fp_mini", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{ Content: " O3Mini", }, }, }, }, { ID: "5", Object: "chat.completion.chunk", Created: 1729585728, Model: openai.O3Mini20250131, SystemFingerprint: "fp_mini", Choices: []openai.ChatCompletionStreamChoice{ { Index: 0, Delta: openai.ChatCompletionStreamChoiceDelta{}, FinishReason: "stop", }, }, }, } for ix, expectedResponse := range expectedResponses { b, _ := json.Marshal(expectedResponse) t.Logf("%d: %s", ix, string(b)) receivedResponse, streamErr := stream.Recv() checks.NoError(t, streamErr, "stream.Recv() failed") if !compareChatResponses(expectedResponse, receivedResponse) { t.Errorf("Stream response %v is %v, expected %v", ix, receivedResponse, expectedResponse) } } _, streamErr := stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr) } } func TestCreateChatCompletionStreamReasoningValidatorFails(t *testing.T) { client, _, _ := setupOpenAITestServer() stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 100, // This will trigger the validator to fail Model: openai.O3Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) if stream != nil { t.Error("Expected nil stream when validation fails") stream.Close() } if !errors.Is(err, openai.ErrReasoningModelMaxTokensDeprecated) { t.Errorf("Expected ErrReasoningModelMaxTokensDeprecated, got: %v", err) } } func TestCreateChatCompletionStreamO3ReasoningValidatorFails(t *testing.T) { client, _, _ := setupOpenAITestServer() stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 100, // This will trigger the validator to fail Model: openai.O3, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) if stream != nil { t.Error("Expected nil stream when validation fails") stream.Close() } if !errors.Is(err, openai.ErrReasoningModelMaxTokensDeprecated) { t.Errorf("Expected ErrReasoningModelMaxTokensDeprecated for O3, got: %v", err) } } func TestCreateChatCompletionStreamO4MiniReasoningValidatorFails(t *testing.T) { client, _, _ := setupOpenAITestServer() stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 100, // This will trigger the validator to fail Model: openai.O4Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Stream: true, }) if stream != nil { t.Error("Expected nil stream when validation fails") stream.Close() } if !errors.Is(err, openai.ErrReasoningModelMaxTokensDeprecated) { t.Errorf("Expected ErrReasoningModelMaxTokensDeprecated for O4Mini, got: %v", err) } } func compareChatStreamResponseChoices(c1, c2 openai.ChatCompletionStreamChoice) bool { if c1.Index != c2.Index { return false } if c1.Delta.Content != c2.Delta.Content { return false } if c1.FinishReason != c2.FinishReason { return false } return true } ================================================ FILE: chat_test.go ================================================ package openai_test import ( "context" "encoding/json" "errors" "fmt" "io" "net/http" "strconv" "strings" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" "github.com/sashabaranov/go-openai/jsonschema" ) const ( xCustomHeader = "X-CUSTOM-HEADER" xCustomHeaderValue = "test" ) var rateLimitHeaders = map[string]any{ "x-ratelimit-limit-requests": 60, "x-ratelimit-limit-tokens": 150000, "x-ratelimit-remaining-requests": 59, "x-ratelimit-remaining-tokens": 149984, "x-ratelimit-reset-requests": "1s", "x-ratelimit-reset-tokens": "6m0s", } func TestChatCompletionsWrongModel(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) ctx := context.Background() req := openai.ChatCompletionRequest{ MaxTokens: 5, Model: "ada", Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, } _, err := client.CreateChatCompletion(ctx, req) msg := fmt.Sprintf("CreateChatCompletion should return wrong model error, returned: %s", err) checks.ErrorIs(t, err, openai.ErrChatCompletionInvalidModel, msg) } func TestO1ModelsChatCompletionsDeprecatedFields(t *testing.T) { tests := []struct { name string in openai.ChatCompletionRequest expectedError error }{ { name: "o1-preview_MaxTokens_deprecated", in: openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.O1Preview, }, expectedError: openai.ErrReasoningModelMaxTokensDeprecated, }, { name: "o1-mini_MaxTokens_deprecated", in: openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.O1Mini, }, expectedError: openai.ErrReasoningModelMaxTokensDeprecated, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) ctx := context.Background() _, err := client.CreateChatCompletion(ctx, tt.in) checks.HasError(t, err) msg := fmt.Sprintf("CreateChatCompletion should return wrong model error, returned: %s", err) checks.ErrorIs(t, err, tt.expectedError, msg) }) } } func TestO1ModelsChatCompletionsBetaLimitations(t *testing.T) { tests := []struct { name string in openai.ChatCompletionRequest expectedError error }{ { name: "log_probs_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, LogProbs: true, Model: openai.O1Preview, }, expectedError: openai.ErrReasoningModelLimitationsLogprobs, }, { name: "set_temperature_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O1Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(2), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_top_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O1Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(1), TopP: float32(0.1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_n_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O1Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(1), TopP: float32(1), N: 2, }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_presence_penalty_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O1Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, PresencePenalty: float32(1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_frequency_penalty_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O1Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, FrequencyPenalty: float32(0.1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) ctx := context.Background() _, err := client.CreateChatCompletion(ctx, tt.in) checks.HasError(t, err) msg := fmt.Sprintf("CreateChatCompletion should return wrong model error, returned: %s", err) checks.ErrorIs(t, err, tt.expectedError, msg) }) } } func TestO3ModelsChatCompletionsBetaLimitations(t *testing.T) { tests := []struct { name string in openai.ChatCompletionRequest expectedError error }{ { name: "log_probs_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, LogProbs: true, Model: openai.O3Mini, }, expectedError: openai.ErrReasoningModelLimitationsLogprobs, }, { name: "set_temperature_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O3Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(2), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_top_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O3Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(1), TopP: float32(0.1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_n_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O3Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(1), TopP: float32(1), N: 2, }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_presence_penalty_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O3Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, PresencePenalty: float32(1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_frequency_penalty_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.O3Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, FrequencyPenalty: float32(0.1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) ctx := context.Background() _, err := client.CreateChatCompletion(ctx, tt.in) checks.HasError(t, err) msg := fmt.Sprintf("CreateChatCompletion should return wrong model error, returned: %s", err) checks.ErrorIs(t, err, tt.expectedError, msg) }) } } func TestGPT5ModelsChatCompletionsBetaLimitations(t *testing.T) { tests := []struct { name string in openai.ChatCompletionRequest expectedError error }{ { name: "log_probs_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, LogProbs: true, Model: openai.GPT5, }, expectedError: openai.ErrReasoningModelLimitationsLogprobs, }, { name: "set_temperature_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.GPT5Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(2), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_top_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.GPT5Nano, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(1), TopP: float32(0.1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_n_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.GPT5ChatLatest, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, Temperature: float32(1), TopP: float32(1), N: 2, }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_presence_penalty_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.GPT5, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, PresencePenalty: float32(0.1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, { name: "set_frequency_penalty_unsupported", in: openai.ChatCompletionRequest{ MaxCompletionTokens: 1000, Model: openai.GPT5Mini, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, }, { Role: openai.ChatMessageRoleAssistant, }, }, FrequencyPenalty: float32(0.1), }, expectedError: openai.ErrReasoningModelLimitationsOther, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) ctx := context.Background() _, err := client.CreateChatCompletion(ctx, tt.in) checks.HasError(t, err) msg := fmt.Sprintf("CreateChatCompletion should return wrong model error, returned: %s", err) checks.ErrorIs(t, err, tt.expectedError, msg) }) } } func TestChatRequestOmitEmpty(t *testing.T) { data, err := json.Marshal(openai.ChatCompletionRequest{ // We set model b/c it's required, so omitempty doesn't make sense Model: "gpt-4", }) checks.NoError(t, err) // messages is also required so isn't omitted const expected = `{"model":"gpt-4","messages":null}` if string(data) != expected { t.Errorf("expected JSON with all empty fields to be %v but was %v", expected, string(data)) } } func TestChatCompletionsWithStream(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) ctx := context.Background() req := openai.ChatCompletionRequest{ Stream: true, } _, err := client.CreateChatCompletion(ctx, req) checks.ErrorIs(t, err, openai.ErrChatCompletionStreamNotSupported, "unexpected error") } // TestCompletions Tests the completions endpoint of the API using the mocked server. func TestChatCompletions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", handleChatCompletionEndpoint) _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) checks.NoError(t, err, "CreateChatCompletion error") } // TestCompletions Tests the completions endpoint of the API using the mocked server. func TestO1ModelChatCompletions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", handleChatCompletionEndpoint) _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ Model: openai.O1Preview, MaxCompletionTokens: 1000, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) checks.NoError(t, err, "CreateChatCompletion error") } func TestO3ModelChatCompletions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", handleChatCompletionEndpoint) _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ Model: openai.O3Mini, MaxCompletionTokens: 1000, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) checks.NoError(t, err, "CreateChatCompletion error") } func TestDeepseekR1ModelChatCompletions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", handleDeepseekR1ChatCompletionEndpoint) _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ Model: "deepseek-reasoner", MaxCompletionTokens: 100, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) checks.NoError(t, err, "CreateChatCompletion error") } // TestCompletions Tests the completions endpoint of the API using the mocked server. func TestChatCompletionsWithHeaders(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", handleChatCompletionEndpoint) resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) checks.NoError(t, err, "CreateChatCompletion error") a := resp.Header().Get(xCustomHeader) _ = a if resp.Header().Get(xCustomHeader) != xCustomHeaderValue { t.Errorf("expected header %s to be %s", xCustomHeader, xCustomHeaderValue) } } // TestChatCompletionsWithRateLimitHeaders Tests the completions endpoint of the API using the mocked server. func TestChatCompletionsWithRateLimitHeaders(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", handleChatCompletionEndpoint) resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) checks.NoError(t, err, "CreateChatCompletion error") headers := resp.GetRateLimitHeaders() resetRequests := headers.ResetRequests.String() if resetRequests != rateLimitHeaders["x-ratelimit-reset-requests"] { t.Errorf("expected resetRequests %s to be %s", resetRequests, rateLimitHeaders["x-ratelimit-reset-requests"]) } resetRequestsTime := headers.ResetRequests.Time() if resetRequestsTime.Before(time.Now()) { t.Errorf("unexpected reset requests: %v", resetRequestsTime) } bs1, _ := json.Marshal(headers) bs2, _ := json.Marshal(rateLimitHeaders) if string(bs1) != string(bs2) { t.Errorf("expected rate limit header %s to be %s", bs2, bs1) } } // TestChatCompletionsFunctions tests including a function call. func TestChatCompletionsFunctions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/chat/completions", handleChatCompletionEndpoint) t.Run("bytes", func(t *testing.T) { //nolint:lll msg := json.RawMessage(`{"properties":{"count":{"type":"integer","description":"total number of words in sentence"},"words":{"items":{"type":"string"},"type":"array","description":"list of words in sentence"}},"type":"object","required":["count","words"]}`) _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo0613, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Functions: []openai.FunctionDefinition{{ Name: "test", Parameters: &msg, }}, }) checks.NoError(t, err, "CreateChatCompletion with functions error") }) t.Run("struct", func(t *testing.T) { type testMessage struct { Count int `json:"count"` Words []string `json:"words"` } msg := testMessage{ Count: 2, Words: []string{"hello", "world"}, } _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo0613, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Functions: []openai.FunctionDefinition{{ Name: "test", Parameters: &msg, }}, }) checks.NoError(t, err, "CreateChatCompletion with functions error") }) t.Run("JSONSchemaDefinition", func(t *testing.T) { _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo0613, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Functions: []openai.FunctionDefinition{{ Name: "test", Parameters: &jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "count": { Type: jsonschema.Number, Description: "total number of words in sentence", }, "words": { Type: jsonschema.Array, Description: "list of words in sentence", Items: &jsonschema.Definition{ Type: jsonschema.String, }, }, "enumTest": { Type: jsonschema.String, Enum: []string{"hello", "world"}, }, }, }, }}, }) checks.NoError(t, err, "CreateChatCompletion with functions error") }) t.Run("JSONSchemaDefinitionWithFunctionDefine", func(t *testing.T) { // this is a compatibility check _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo0613, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Functions: []openai.FunctionDefine{{ Name: "test", Parameters: &jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "count": { Type: jsonschema.Number, Description: "total number of words in sentence", }, "words": { Type: jsonschema.Array, Description: "list of words in sentence", Items: &jsonschema.Definition{ Type: jsonschema.String, }, }, "enumTest": { Type: jsonschema.String, Enum: []string{"hello", "world"}, }, }, }, }}, }) checks.NoError(t, err, "CreateChatCompletion with functions error") }) t.Run("StructuredOutputs", func(t *testing.T) { type testMessage struct { Count int `json:"count"` Words []string `json:"words"` } msg := testMessage{ Count: 2, Words: []string{"hello", "world"}, } _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo0613, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, Functions: []openai.FunctionDefinition{{ Name: "test", Strict: true, Parameters: &msg, }}, }) checks.NoError(t, err, "CreateChatCompletion with functions error") }) } func TestAzureChatCompletions(t *testing.T) { client, server, teardown := setupAzureTestServer() defer teardown() server.RegisterHandler("/openai/deployments/*", handleChatCompletionEndpoint) _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }) checks.NoError(t, err, "CreateAzureChatCompletion error") } func TestMultipartChatCompletions(t *testing.T) { client, server, teardown := setupAzureTestServer() defer teardown() server.RegisterHandler("/openai/deployments/*", handleChatCompletionEndpoint) _, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, MultiContent: []openai.ChatMessagePart{ { Type: openai.ChatMessagePartTypeText, Text: "Hello!", }, { Type: openai.ChatMessagePartTypeImageURL, ImageURL: &openai.ChatMessageImageURL{ URL: "URL", Detail: openai.ImageURLDetailLow, }, }, }, }, }, }) checks.NoError(t, err, "CreateAzureChatCompletion error") } func TestMultipartChatMessageSerialization(t *testing.T) { jsonText := `[{"role":"system","content":"system-message"},` + `{"role":"user","content":[{"type":"text","text":"nice-text"},` + `{"type":"image_url","image_url":{"url":"URL","detail":"high"}}]}]` var msgs []openai.ChatCompletionMessage err := json.Unmarshal([]byte(jsonText), &msgs) if err != nil { t.Fatalf("Expected no error: %s", err) } if len(msgs) != 2 { t.Errorf("unexpected number of messages") } if msgs[0].Role != "system" || msgs[0].Content != "system-message" || msgs[0].MultiContent != nil { t.Errorf("invalid user message: %v", msgs[0]) } if msgs[1].Role != "user" || msgs[1].Content != "" || len(msgs[1].MultiContent) != 2 { t.Errorf("invalid user message") } parts := msgs[1].MultiContent if parts[0].Type != "text" || parts[0].Text != "nice-text" { t.Errorf("invalid text part: %v", parts[0]) } if parts[1].Type != "image_url" || parts[1].ImageURL.URL != "URL" || parts[1].ImageURL.Detail != "high" { t.Errorf("invalid image_url part") } s, err := json.Marshal(msgs) if err != nil { t.Fatalf("Expected no error: %s", err) } res := strings.ReplaceAll(string(s), " ", "") if res != jsonText { t.Fatalf("invalid message: %s", string(s)) } invalidMsg := []openai.ChatCompletionMessage{ { Role: "user", Content: "some-text", MultiContent: []openai.ChatMessagePart{ { Type: "text", Text: "nice-text", }, }, }, } _, err = json.Marshal(invalidMsg) if !errors.Is(err, openai.ErrContentFieldsMisused) { t.Fatalf("Expected error: %s", err) } err = json.Unmarshal([]byte(`["not-a-message"]`), &msgs) if err == nil { t.Fatalf("Expected error") } emptyMultiContentMsg := openai.ChatCompletionMessage{ Role: "user", MultiContent: []openai.ChatMessagePart{}, } s, err = json.Marshal(emptyMultiContentMsg) if err != nil { t.Fatalf("Unexpected error") } res = strings.ReplaceAll(string(s), " ", "") if res != `{"role":"user"}` { t.Fatalf("invalid message: %s", string(s)) } } // handleChatCompletionEndpoint Handles the ChatGPT completion endpoint by the test server. func handleChatCompletionEndpoint(w http.ResponseWriter, r *http.Request) { var err error var resBytes []byte // completions only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } var completionReq openai.ChatCompletionRequest if completionReq, err = getChatCompletionBody(r); err != nil { http.Error(w, "could not read request", http.StatusInternalServerError) return } res := openai.ChatCompletionResponse{ ID: strconv.Itoa(int(time.Now().Unix())), Object: "test-object", Created: time.Now().Unix(), // would be nice to validate Model during testing, but // this may not be possible with how much upkeep // would be required / wouldn't make much sense Model: completionReq.Model, } // create completions n := completionReq.N if n == 0 { n = 1 } for i := 0; i < n; i++ { // if there are functions, include them if len(completionReq.Functions) > 0 { var fcb []byte b := completionReq.Functions[0].Parameters fcb, err = json.Marshal(b) if err != nil { http.Error(w, "could not marshal function parameters", http.StatusInternalServerError) return } res.Choices = append(res.Choices, openai.ChatCompletionChoice{ Message: openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleFunction, // this is valid json so it should be fine FunctionCall: &openai.FunctionCall{ Name: completionReq.Functions[0].Name, Arguments: string(fcb), }, }, Index: i, }) continue } // generate a random string of length completionReq.Length completionStr := strings.Repeat("a", completionReq.MaxTokens) res.Choices = append(res.Choices, openai.ChatCompletionChoice{ Message: openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleAssistant, Content: completionStr, }, Index: i, }) } inputTokens := numTokens(completionReq.Messages[0].Content) * n completionTokens := completionReq.MaxTokens * n res.Usage = openai.Usage{ PromptTokens: inputTokens, CompletionTokens: completionTokens, TotalTokens: inputTokens + completionTokens, } resBytes, _ = json.Marshal(res) w.Header().Set(xCustomHeader, xCustomHeaderValue) for k, v := range rateLimitHeaders { switch val := v.(type) { case int: w.Header().Set(k, strconv.Itoa(val)) default: w.Header().Set(k, fmt.Sprintf("%s", v)) } } fmt.Fprintln(w, string(resBytes)) } func handleDeepseekR1ChatCompletionEndpoint(w http.ResponseWriter, r *http.Request) { var err error var resBytes []byte // completions only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } var completionReq openai.ChatCompletionRequest if completionReq, err = getChatCompletionBody(r); err != nil { http.Error(w, "could not read request", http.StatusInternalServerError) return } res := openai.ChatCompletionResponse{ ID: strconv.Itoa(int(time.Now().Unix())), Object: "test-object", Created: time.Now().Unix(), // would be nice to validate Model during testing, but // this may not be possible with how much upkeep // would be required / wouldn't make much sense Model: completionReq.Model, } // create completions n := completionReq.N if n == 0 { n = 1 } if completionReq.MaxCompletionTokens == 0 { completionReq.MaxCompletionTokens = 1000 } for i := 0; i < n; i++ { reasoningContent := "User says hello! And I need to reply" completionStr := strings.Repeat("a", completionReq.MaxCompletionTokens-numTokens(reasoningContent)) res.Choices = append(res.Choices, openai.ChatCompletionChoice{ Message: openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleAssistant, ReasoningContent: reasoningContent, Content: completionStr, }, Index: i, }) } inputTokens := numTokens(completionReq.Messages[0].Content) * n completionTokens := completionReq.MaxTokens * n res.Usage = openai.Usage{ PromptTokens: inputTokens, CompletionTokens: completionTokens, TotalTokens: inputTokens + completionTokens, } resBytes, _ = json.Marshal(res) w.Header().Set(xCustomHeader, xCustomHeaderValue) for k, v := range rateLimitHeaders { switch val := v.(type) { case int: w.Header().Set(k, strconv.Itoa(val)) default: w.Header().Set(k, fmt.Sprintf("%s", v)) } } fmt.Fprintln(w, string(resBytes)) } // getChatCompletionBody Returns the body of the request to create a completion. func getChatCompletionBody(r *http.Request) (openai.ChatCompletionRequest, error) { completion := openai.ChatCompletionRequest{} // read the request body reqBody, err := io.ReadAll(r.Body) if err != nil { return openai.ChatCompletionRequest{}, err } err = json.Unmarshal(reqBody, &completion) if err != nil { return openai.ChatCompletionRequest{}, err } return completion, nil } func TestFinishReason(t *testing.T) { c := &openai.ChatCompletionChoice{ FinishReason: openai.FinishReasonNull, } resBytes, _ := json.Marshal(c) if !strings.Contains(string(resBytes), `"finish_reason":null`) { t.Error("null should not be quoted") } c.FinishReason = "" resBytes, _ = json.Marshal(c) if !strings.Contains(string(resBytes), `"finish_reason":null`) { t.Error("null should not be quoted") } otherReasons := []openai.FinishReason{ openai.FinishReasonStop, openai.FinishReasonLength, openai.FinishReasonFunctionCall, openai.FinishReasonContentFilter, } for _, r := range otherReasons { c.FinishReason = r resBytes, _ = json.Marshal(c) if !strings.Contains(string(resBytes), fmt.Sprintf(`"finish_reason":"%s"`, r)) { t.Errorf("%s should be quoted", r) } } } func TestChatCompletionResponseFormatJSONSchema_UnmarshalJSON(t *testing.T) { type args struct { data []byte } tests := []struct { name string args args wantErr bool }{ { "", args{ data: []byte(`{ "name": "math_response", "strict": true, "schema": { "type": "object", "properties": { "steps": { "type": "array", "items": { "type": "object", "properties": { "explanation": { "type": "string" }, "output": { "type": "string" } }, "required": ["explanation","output"], "additionalProperties": false } }, "final_answer": { "type": "string" } }, "required": ["steps","final_answer"], "additionalProperties": false } }`), }, false, }, { "", args{ data: []byte(`{ "name": "math_response", "strict": true, "schema": null }`), }, false, }, { "", args{ data: []byte(`[123,456]`), }, true, }, { "", args{ data: []byte(`{ "name": "math_response", "strict": true, "schema": 123456 }`), }, true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var r openai.ChatCompletionResponseFormatJSONSchema err := r.UnmarshalJSON(tt.args.data) if (err != nil) != tt.wantErr { t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestChatCompletionRequest_UnmarshalJSON(t *testing.T) { type args struct { bs []byte } tests := []struct { name string args args wantErr bool }{ { "", args{bs: []byte(`{ "model": "llama3-1b", "messages": [ { "role": "system", "content": "You are a helpful math tutor." }, { "role": "user", "content": "solve 8x + 31 = 2" } ], "response_format": { "type": "json_schema", "json_schema": { "name": "math_response", "strict": true, "schema": { "type": "object", "properties": { "steps": { "type": "array", "items": { "type": "object", "properties": { "explanation": { "type": "string" }, "output": { "type": "string" } }, "required": ["explanation","output"], "additionalProperties": false } }, "final_answer": { "type": "string" } }, "required": ["steps","final_answer"], "additionalProperties": false } } } }`)}, false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var m openai.ChatCompletionRequest err := json.Unmarshal(tt.args.bs, &m) if err != nil { t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) } }) } } ================================================ FILE: client.go ================================================ package openai import ( "bufio" "context" "encoding/json" "fmt" "io" "net/http" "net/url" "strings" utils "github.com/sashabaranov/go-openai/internal" ) // Client is OpenAI GPT-3 API client. type Client struct { config ClientConfig requestBuilder utils.RequestBuilder createFormBuilder func(io.Writer) utils.FormBuilder } type Response interface { SetHeader(http.Header) } type httpHeader http.Header func (h *httpHeader) SetHeader(header http.Header) { *h = httpHeader(header) } func (h *httpHeader) Header() http.Header { return http.Header(*h) } func (h *httpHeader) GetRateLimitHeaders() RateLimitHeaders { return newRateLimitHeaders(h.Header()) } type RawResponse struct { io.ReadCloser httpHeader } // NewClient creates new OpenAI API client. func NewClient(authToken string) *Client { config := DefaultConfig(authToken) return NewClientWithConfig(config) } // NewClientWithConfig creates new OpenAI API client for specified config. func NewClientWithConfig(config ClientConfig) *Client { return &Client{ config: config, requestBuilder: utils.NewRequestBuilder(), createFormBuilder: func(body io.Writer) utils.FormBuilder { return utils.NewFormBuilder(body) }, } } // NewOrgClient creates new OpenAI API client for specified Organization ID. // // Deprecated: Please use NewClientWithConfig. func NewOrgClient(authToken, org string) *Client { config := DefaultConfig(authToken) config.OrgID = org return NewClientWithConfig(config) } type requestOptions struct { body any header http.Header } type requestOption func(*requestOptions) func withBody(body any) requestOption { return func(args *requestOptions) { args.body = body } } func withExtraBody(extraBody map[string]any) requestOption { return func(args *requestOptions) { // Assert that args.body is a map[string]any. bodyMap, ok := args.body.(map[string]any) if ok { // If it's a map[string]any then only add extraBody // fields to args.body otherwise keep only fields in request struct. for key, value := range extraBody { bodyMap[key] = value } } } } func withContentType(contentType string) requestOption { return func(args *requestOptions) { args.header.Set("Content-Type", contentType) } } func withBetaAssistantVersion(version string) requestOption { return func(args *requestOptions) { args.header.Set("OpenAI-Beta", fmt.Sprintf("assistants=%s", version)) } } func (c *Client) newRequest(ctx context.Context, method, url string, setters ...requestOption) (*http.Request, error) { // Default Options args := &requestOptions{ body: nil, header: make(http.Header), } for _, setter := range setters { setter(args) } req, err := c.requestBuilder.Build(ctx, method, url, args.body, args.header) if err != nil { return nil, err } c.setCommonHeaders(req) return req, nil } func (c *Client) sendRequest(req *http.Request, v Response) error { req.Header.Set("Accept", "application/json") // Check whether Content-Type is already set, Upload Files API requires // Content-Type == multipart/form-data contentType := req.Header.Get("Content-Type") if contentType == "" { req.Header.Set("Content-Type", "application/json") } res, err := c.config.HTTPClient.Do(req) if err != nil { return err } defer res.Body.Close() if v != nil { v.SetHeader(res.Header) } if isFailureStatusCode(res) { return c.handleErrorResp(res) } return decodeResponse(res.Body, v) } func (c *Client) sendRequestRaw(req *http.Request) (response RawResponse, err error) { resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body should be closed by outer function if err != nil { return } if isFailureStatusCode(resp) { err = c.handleErrorResp(resp) return } response.SetHeader(resp.Header) response.ReadCloser = resp.Body return } func sendRequestStream[T streamable](client *Client, req *http.Request) (*streamReader[T], error) { req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "text/event-stream") req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Connection", "keep-alive") resp, err := client.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close() if err != nil { return new(streamReader[T]), err } if isFailureStatusCode(resp) { return new(streamReader[T]), client.handleErrorResp(resp) } return &streamReader[T]{ emptyMessagesLimit: client.config.EmptyMessagesLimit, reader: bufio.NewReader(resp.Body), response: resp, errAccumulator: utils.NewErrorAccumulator(), unmarshaler: &utils.JSONUnmarshaler{}, httpHeader: httpHeader(resp.Header), }, nil } func (c *Client) setCommonHeaders(req *http.Request) { // https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication switch c.config.APIType { case APITypeAzure, APITypeCloudflareAzure: // Azure API Key authentication req.Header.Set(AzureAPIKeyHeader, c.config.authToken) case APITypeAnthropic: // https://docs.anthropic.com/en/api/versioning req.Header.Set("anthropic-version", c.config.APIVersion) case APITypeOpenAI, APITypeAzureAD: fallthrough default: if c.config.authToken != "" { req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken)) } } if c.config.OrgID != "" { req.Header.Set("OpenAI-Organization", c.config.OrgID) } } func isFailureStatusCode(resp *http.Response) bool { return resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest } func decodeResponse(body io.Reader, v any) error { if v == nil { return nil } switch o := v.(type) { case *string: return decodeString(body, o) case *audioTextResponse: return decodeString(body, &o.Text) default: return json.NewDecoder(body).Decode(v) } } func decodeString(body io.Reader, output *string) error { b, err := io.ReadAll(body) if err != nil { return err } *output = string(b) return nil } type fullURLOptions struct { model string } type fullURLOption func(*fullURLOptions) func withModel(model string) fullURLOption { return func(args *fullURLOptions) { args.model = model } } var azureDeploymentsEndpoints = []string{ "/completions", "/embeddings", "/chat/completions", "/audio/transcriptions", "/audio/translations", "/audio/speech", "/images/generations", } // fullURL returns full URL for request. func (c *Client) fullURL(suffix string, setters ...fullURLOption) string { baseURL := strings.TrimRight(c.config.BaseURL, "/") args := fullURLOptions{} for _, setter := range setters { setter(&args) } if c.config.APIType == APITypeAzure || c.config.APIType == APITypeAzureAD { baseURL = c.baseURLWithAzureDeployment(baseURL, suffix, args.model) } if c.config.APIVersion != "" { suffix = c.suffixWithAPIVersion(suffix) } return fmt.Sprintf("%s%s", baseURL, suffix) } func (c *Client) suffixWithAPIVersion(suffix string) string { parsedSuffix, err := url.Parse(suffix) if err != nil { panic("failed to parse url suffix") } query := parsedSuffix.Query() query.Add("api-version", c.config.APIVersion) return fmt.Sprintf("%s?%s", parsedSuffix.Path, query.Encode()) } func (c *Client) baseURLWithAzureDeployment(baseURL, suffix, model string) (newBaseURL string) { baseURL = fmt.Sprintf("%s/%s", strings.TrimRight(baseURL, "/"), azureAPIPrefix) if containsSubstr(azureDeploymentsEndpoints, suffix) { azureDeploymentName := c.config.GetAzureDeploymentByModel(model) if azureDeploymentName == "" { azureDeploymentName = "UNKNOWN" } baseURL = fmt.Sprintf("%s/%s/%s", baseURL, azureDeploymentsPrefix, azureDeploymentName) } return baseURL } func (c *Client) handleErrorResp(resp *http.Response) error { body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("error, reading response body: %w", err) } var errRes ErrorResponse err = json.Unmarshal(body, &errRes) if err != nil || errRes.Error == nil { reqErr := &RequestError{ HTTPStatus: resp.Status, HTTPStatusCode: resp.StatusCode, Err: err, Body: body, } if errRes.Error != nil { reqErr.Err = errRes.Error } return reqErr } errRes.Error.HTTPStatus = resp.Status errRes.Error.HTTPStatusCode = resp.StatusCode return errRes.Error } func containsSubstr(s []string, e string) bool { for _, v := range s { if strings.Contains(e, v) { return true } } return false } ================================================ FILE: client_test.go ================================================ package openai //nolint:testpackage // testing private field import ( "bytes" "context" "errors" "fmt" "io" "net/http" "reflect" "testing" "github.com/sashabaranov/go-openai/internal/test" "github.com/sashabaranov/go-openai/internal/test/checks" ) var errTestRequestBuilderFailed = errors.New("test request builder failed") type failingRequestBuilder struct{} func (*failingRequestBuilder) Build(_ context.Context, _, _ string, _ any, _ http.Header) (*http.Request, error) { return nil, errTestRequestBuilderFailed } func TestClient(t *testing.T) { const mockToken = "mock token" client := NewClient(mockToken) if client.config.authToken != mockToken { t.Errorf("Client does not contain proper token") } const mockOrg = "mock org" client = NewOrgClient(mockToken, mockOrg) if client.config.authToken != mockToken { t.Errorf("Client does not contain proper token") } if client.config.OrgID != mockOrg { t.Errorf("Client does not contain proper orgID") } } func TestSetCommonHeadersAnthropic(t *testing.T) { config := DefaultAnthropicConfig("mock-token", "") client := NewClientWithConfig(config) req, err := http.NewRequest("GET", "http://example.com", nil) if err != nil { t.Fatalf("Failed to create request: %v", err) } client.setCommonHeaders(req) if got := req.Header.Get("anthropic-version"); got != AnthropicAPIVersion { t.Errorf("Expected anthropic-version header to be %q, got %q", AnthropicAPIVersion, got) } } func TestDecodeResponse(t *testing.T) { stringInput := "" testCases := []struct { name string value interface{} expected interface{} body io.Reader hasError bool }{ { name: "nil input", value: nil, body: bytes.NewReader([]byte("")), expected: nil, }, { name: "string input", value: &stringInput, body: bytes.NewReader([]byte("test")), expected: "test", }, { name: "map input", value: &map[string]interface{}{}, body: bytes.NewReader([]byte(`{"test": "test"}`)), expected: map[string]interface{}{ "test": "test", }, }, { name: "reader return error", value: &stringInput, body: &errorReader{err: errors.New("dummy")}, hasError: true, }, { name: "audio text input", value: &audioTextResponse{}, body: bytes.NewReader([]byte("test")), expected: audioTextResponse{ Text: "test", }, }, } assertEqual := func(t *testing.T, expected, actual interface{}) { t.Helper() if expected == actual { return } v := reflect.ValueOf(actual).Elem().Interface() if !reflect.DeepEqual(v, expected) { t.Fatalf("Unexpected value: %v, expected: %v", v, expected) } } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { err := decodeResponse(tc.body, tc.value) if tc.hasError { checks.HasError(t, err, "Unexpected nil error") return } if err != nil { t.Fatalf("Unexpected error: %v", err) } assertEqual(t, tc.expected, tc.value) }) } } type errorReader struct { err error } func (e *errorReader) Read(_ []byte) (n int, err error) { return 0, e.err } func TestHandleErrorResp(t *testing.T) { // var errRes *ErrorResponse var errRes ErrorResponse var reqErr RequestError t.Log(errRes, errRes.Error) if errRes.Error != nil { reqErr.Err = errRes.Error } t.Log(fmt.Errorf("error, %w", &reqErr)) t.Log(errRes.Error, "nil pointer check Pass") const mockToken = "mock token" client := NewClient(mockToken) testCases := []struct { name string httpCode int httpStatus string contentType string body io.Reader expected string }{ { name: "401 Invalid Authentication", httpCode: http.StatusUnauthorized, contentType: "application/json", body: bytes.NewReader([]byte( `{ "error":{ "message":"You didn't provide an API key. ....", "type":"invalid_request_error", "param":null, "code":null } }`, )), expected: "error, status code: 401, status: , message: You didn't provide an API key. ....", }, { name: "401 Azure Access Denied", httpCode: http.StatusUnauthorized, contentType: "application/json", body: bytes.NewReader([]byte( `{ "error":{ "code":"AccessDenied", "message":"Access denied due to Virtual Network/Firewall rules." } }`, )), expected: "error, status code: 401, status: , message: Access denied due to Virtual Network/Firewall rules.", }, { name: "503 Model Overloaded", httpCode: http.StatusServiceUnavailable, contentType: "application/json", body: bytes.NewReader([]byte(` { "error":{ "message":"That model...", "type":"server_error", "param":null, "code":null } }`)), expected: "error, status code: 503, status: , message: That model...", }, { name: "503 no message (Unknown response)", httpCode: http.StatusServiceUnavailable, contentType: "application/json", body: bytes.NewReader([]byte(` { "error":{} }`)), expected: `error, status code: 503, status: , message: , body: { "error":{} }`, }, { name: "413 Request Entity Too Large", httpCode: http.StatusRequestEntityTooLarge, contentType: "text/html", body: bytes.NewReader([]byte(` 413 Request Entity Too Large

413 Request Entity Too Large


nginx
`)), expected: `error, status code: 413, status: , message: invalid character '<' looking for beginning of value, body: 413 Request Entity Too Large

413 Request Entity Too Large


nginx
`, }, { name: "errorReader", httpCode: http.StatusRequestEntityTooLarge, contentType: "text/html", body: &errorReader{err: errors.New("errorReader")}, expected: "error, reading response body: errorReader", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { testCase := &http.Response{ Header: map[string][]string{ "Content-Type": {tc.contentType}, }, } testCase.StatusCode = tc.httpCode testCase.Body = io.NopCloser(tc.body) err := client.handleErrorResp(testCase) t.Log(err.Error()) if err.Error() != tc.expected { t.Errorf("Unexpected error: %v , expected: %s", err, tc.expected) t.Fail() } }) } } func TestClientReturnsRequestBuilderErrors(t *testing.T) { config := DefaultConfig(test.GetTestToken()) client := NewClientWithConfig(config) client.requestBuilder = &failingRequestBuilder{} ctx := context.Background() type TestCase struct { Name string TestFunc func() (any, error) } testCases := []TestCase{ {"CreateCompletion", func() (any, error) { return client.CreateCompletion(ctx, CompletionRequest{Prompt: "testing"}) }}, {"CreateCompletionStream", func() (any, error) { return client.CreateCompletionStream(ctx, CompletionRequest{Prompt: ""}) }}, {"CreateChatCompletion", func() (any, error) { return client.CreateChatCompletion(ctx, ChatCompletionRequest{Model: GPT3Dot5Turbo}) }}, {"CreateChatCompletionStream", func() (any, error) { return client.CreateChatCompletionStream(ctx, ChatCompletionRequest{Model: GPT3Dot5Turbo}) }}, {"CreateFineTune", func() (any, error) { return client.CreateFineTune(ctx, FineTuneRequest{}) }}, {"ListFineTunes", func() (any, error) { return client.ListFineTunes(ctx) }}, {"CancelFineTune", func() (any, error) { return client.CancelFineTune(ctx, "") }}, {"GetFineTune", func() (any, error) { return client.GetFineTune(ctx, "") }}, {"DeleteFineTune", func() (any, error) { return client.DeleteFineTune(ctx, "") }}, {"ListFineTuneEvents", func() (any, error) { return client.ListFineTuneEvents(ctx, "") }}, {"CreateFineTuningJob", func() (any, error) { return client.CreateFineTuningJob(ctx, FineTuningJobRequest{}) }}, {"CancelFineTuningJob", func() (any, error) { return client.CancelFineTuningJob(ctx, "") }}, {"RetrieveFineTuningJob", func() (any, error) { return client.RetrieveFineTuningJob(ctx, "") }}, {"ListFineTuningJobEvents", func() (any, error) { return client.ListFineTuningJobEvents(ctx, "") }}, {"Moderations", func() (any, error) { return client.Moderations(ctx, ModerationRequest{}) }}, {"Edits", func() (any, error) { return client.Edits(ctx, EditsRequest{}) }}, {"CreateEmbeddings", func() (any, error) { return client.CreateEmbeddings(ctx, EmbeddingRequest{}) }}, {"CreateImage", func() (any, error) { return client.CreateImage(ctx, ImageRequest{}) }}, {"CreateFileBytes", func() (any, error) { return client.CreateFileBytes(ctx, FileBytesRequest{}) }}, {"DeleteFile", func() (any, error) { return nil, client.DeleteFile(ctx, "") }}, {"GetFile", func() (any, error) { return client.GetFile(ctx, "") }}, {"GetFileContent", func() (any, error) { return client.GetFileContent(ctx, "") }}, {"ListFiles", func() (any, error) { return client.ListFiles(ctx) }}, {"ListEngines", func() (any, error) { return client.ListEngines(ctx) }}, {"GetEngine", func() (any, error) { return client.GetEngine(ctx, "") }}, {"ListModels", func() (any, error) { return client.ListModels(ctx) }}, {"GetModel", func() (any, error) { return client.GetModel(ctx, "text-davinci-003") }}, {"DeleteFineTuneModel", func() (any, error) { return client.DeleteFineTuneModel(ctx, "") }}, {"CreateAssistant", func() (any, error) { return client.CreateAssistant(ctx, AssistantRequest{}) }}, {"RetrieveAssistant", func() (any, error) { return client.RetrieveAssistant(ctx, "") }}, {"ModifyAssistant", func() (any, error) { return client.ModifyAssistant(ctx, "", AssistantRequest{}) }}, {"DeleteAssistant", func() (any, error) { return client.DeleteAssistant(ctx, "") }}, {"ListAssistants", func() (any, error) { return client.ListAssistants(ctx, nil, nil, nil, nil) }}, {"CreateAssistantFile", func() (any, error) { return client.CreateAssistantFile(ctx, "", AssistantFileRequest{}) }}, {"ListAssistantFiles", func() (any, error) { return client.ListAssistantFiles(ctx, "", nil, nil, nil, nil) }}, {"RetrieveAssistantFile", func() (any, error) { return client.RetrieveAssistantFile(ctx, "", "") }}, {"DeleteAssistantFile", func() (any, error) { return nil, client.DeleteAssistantFile(ctx, "", "") }}, {"CreateMessage", func() (any, error) { return client.CreateMessage(ctx, "", MessageRequest{}) }}, {"ListMessage", func() (any, error) { return client.ListMessage(ctx, "", nil, nil, nil, nil, nil) }}, {"RetrieveMessage", func() (any, error) { return client.RetrieveMessage(ctx, "", "") }}, {"ModifyMessage", func() (any, error) { return client.ModifyMessage(ctx, "", "", nil) }}, {"DeleteMessage", func() (any, error) { return client.DeleteMessage(ctx, "", "") }}, {"RetrieveMessageFile", func() (any, error) { return client.RetrieveMessageFile(ctx, "", "", "") }}, {"ListMessageFiles", func() (any, error) { return client.ListMessageFiles(ctx, "", "") }}, {"CreateThread", func() (any, error) { return client.CreateThread(ctx, ThreadRequest{}) }}, {"RetrieveThread", func() (any, error) { return client.RetrieveThread(ctx, "") }}, {"ModifyThread", func() (any, error) { return client.ModifyThread(ctx, "", ModifyThreadRequest{}) }}, {"DeleteThread", func() (any, error) { return client.DeleteThread(ctx, "") }}, {"CreateRun", func() (any, error) { return client.CreateRun(ctx, "", RunRequest{}) }}, {"RetrieveRun", func() (any, error) { return client.RetrieveRun(ctx, "", "") }}, {"ModifyRun", func() (any, error) { return client.ModifyRun(ctx, "", "", RunModifyRequest{}) }}, {"ListRuns", func() (any, error) { return client.ListRuns(ctx, "", Pagination{}) }}, {"SubmitToolOutputs", func() (any, error) { return client.SubmitToolOutputs(ctx, "", "", SubmitToolOutputsRequest{}) }}, {"CancelRun", func() (any, error) { return client.CancelRun(ctx, "", "") }}, {"CreateThreadAndRun", func() (any, error) { return client.CreateThreadAndRun(ctx, CreateThreadAndRunRequest{}) }}, {"RetrieveRunStep", func() (any, error) { return client.RetrieveRunStep(ctx, "", "", "") }}, {"ListRunSteps", func() (any, error) { return client.ListRunSteps(ctx, "", "", Pagination{}) }}, {"CreateSpeech", func() (any, error) { return client.CreateSpeech(ctx, CreateSpeechRequest{Model: TTSModel1, Voice: VoiceAlloy}) }}, {"CreateBatch", func() (any, error) { return client.CreateBatch(ctx, CreateBatchRequest{}) }}, {"CreateBatchWithUploadFile", func() (any, error) { return client.CreateBatchWithUploadFile(ctx, CreateBatchWithUploadFileRequest{}) }}, {"RetrieveBatch", func() (any, error) { return client.RetrieveBatch(ctx, "") }}, {"CancelBatch", func() (any, error) { return client.CancelBatch(ctx, "") }}, {"ListBatch", func() (any, error) { return client.ListBatch(ctx, nil, nil) }}, } for _, testCase := range testCases { _, err := testCase.TestFunc() if !errors.Is(err, errTestRequestBuilderFailed) { t.Fatalf("%s did not return error when request builder failed: %v", testCase.Name, err) } } } func TestClientReturnsRequestBuilderErrorsAddition(t *testing.T) { config := DefaultConfig(test.GetTestToken()) client := NewClientWithConfig(config) client.requestBuilder = &failingRequestBuilder{} ctx := context.Background() _, err := client.CreateCompletion(ctx, CompletionRequest{Prompt: 1}) if !errors.Is(err, ErrCompletionRequestPromptTypeNotSupported) { t.Fatalf("Did not return error when request builder failed: %v", err) } _, err = client.CreateCompletionStream(ctx, CompletionRequest{Prompt: 1}) if !errors.Is(err, ErrCompletionRequestPromptTypeNotSupported) { t.Fatalf("Did not return error when request builder failed: %v", err) } } func TestClient_suffixWithAPIVersion(t *testing.T) { type fields struct { apiVersion string } type args struct { suffix string } tests := []struct { name string fields fields args args want string wantPanic string }{ { "", fields{apiVersion: "2023-05"}, args{suffix: "/assistants"}, "/assistants?api-version=2023-05", "", }, { "", fields{apiVersion: "2023-05"}, args{suffix: "/assistants?limit=5"}, "/assistants?api-version=2023-05&limit=5", "", }, { "", fields{apiVersion: "2023-05"}, args{suffix: "123:assistants?limit=5"}, "/assistants?api-version=2023-05&limit=5", "failed to parse url suffix", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c := &Client{ config: ClientConfig{APIVersion: tt.fields.apiVersion}, } defer func() { if r := recover(); r != nil { // Check if the panic message matches the expected panic message if rStr, ok := r.(string); ok { if rStr != tt.wantPanic { t.Errorf("suffixWithAPIVersion() = %v, want %v", rStr, tt.wantPanic) } } else { // If the panic is not a string, log it t.Errorf("suffixWithAPIVersion() panicked with non-string value: %v", r) } } }() if got := c.suffixWithAPIVersion(tt.args.suffix); got != tt.want { t.Errorf("suffixWithAPIVersion() = %v, want %v", got, tt.want) } }) } } func TestClient_baseURLWithAzureDeployment(t *testing.T) { type args struct { baseURL string suffix string model string } tests := []struct { name string args args wantNewBaseURL string }{ { "", args{baseURL: "https://test.openai.azure.com/", suffix: assistantsSuffix, model: GPT4oMini}, "https://test.openai.azure.com/openai", }, { "", args{baseURL: "https://test.openai.azure.com/", suffix: chatCompletionsSuffix, model: GPT4oMini}, "https://test.openai.azure.com/openai/deployments/gpt-4o-mini", }, { "", args{baseURL: "https://test.openai.azure.com/", suffix: chatCompletionsSuffix, model: ""}, "https://test.openai.azure.com/openai/deployments/UNKNOWN", }, } client := NewClient("") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if gotNewBaseURL := client.baseURLWithAzureDeployment( tt.args.baseURL, tt.args.suffix, tt.args.model, ); gotNewBaseURL != tt.wantNewBaseURL { t.Errorf("baseURLWithAzureDeployment() = %v, want %v", gotNewBaseURL, tt.wantNewBaseURL) } }) } } ================================================ FILE: common.go ================================================ package openai // common.go defines common types used throughout the OpenAI API. // Usage Represents the total token usage per request to OpenAI. type Usage struct { PromptTokens int `json:"prompt_tokens"` CompletionTokens int `json:"completion_tokens"` TotalTokens int `json:"total_tokens"` PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details"` CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details"` } // CompletionTokensDetails Breakdown of tokens used in a completion. type CompletionTokensDetails struct { AudioTokens int `json:"audio_tokens"` ReasoningTokens int `json:"reasoning_tokens"` AcceptedPredictionTokens int `json:"accepted_prediction_tokens"` RejectedPredictionTokens int `json:"rejected_prediction_tokens"` } // PromptTokensDetails Breakdown of tokens used in the prompt. type PromptTokensDetails struct { AudioTokens int `json:"audio_tokens"` CachedTokens int `json:"cached_tokens"` } ================================================ FILE: completion.go ================================================ package openai import ( "context" "net/http" ) // GPT3 Defines the models provided by OpenAI to use when generating // completions from OpenAI. // GPT3 Models are designed for text-based tasks. For code-specific // tasks, please refer to the Codex series of models. const ( O1Mini = "o1-mini" O1Mini20240912 = "o1-mini-2024-09-12" O1Preview = "o1-preview" O1Preview20240912 = "o1-preview-2024-09-12" O1 = "o1" O120241217 = "o1-2024-12-17" O3 = "o3" O320250416 = "o3-2025-04-16" O3Mini = "o3-mini" O3Mini20250131 = "o3-mini-2025-01-31" O4Mini = "o4-mini" O4Mini20250416 = "o4-mini-2025-04-16" GPT432K0613 = "gpt-4-32k-0613" GPT432K0314 = "gpt-4-32k-0314" GPT432K = "gpt-4-32k" GPT40613 = "gpt-4-0613" GPT40314 = "gpt-4-0314" GPT4o = "gpt-4o" GPT4o20240513 = "gpt-4o-2024-05-13" GPT4o20240806 = "gpt-4o-2024-08-06" GPT4o20241120 = "gpt-4o-2024-11-20" GPT4oLatest = "chatgpt-4o-latest" GPT4oMini = "gpt-4o-mini" GPT4oMini20240718 = "gpt-4o-mini-2024-07-18" GPT4Turbo = "gpt-4-turbo" GPT4Turbo20240409 = "gpt-4-turbo-2024-04-09" GPT4Turbo0125 = "gpt-4-0125-preview" GPT4Turbo1106 = "gpt-4-1106-preview" GPT4TurboPreview = "gpt-4-turbo-preview" GPT4VisionPreview = "gpt-4-vision-preview" GPT4 = "gpt-4" GPT4Dot1 = "gpt-4.1" GPT4Dot120250414 = "gpt-4.1-2025-04-14" GPT4Dot1Mini = "gpt-4.1-mini" GPT4Dot1Mini20250414 = "gpt-4.1-mini-2025-04-14" GPT4Dot1Nano = "gpt-4.1-nano" GPT4Dot1Nano20250414 = "gpt-4.1-nano-2025-04-14" GPT4Dot5Preview = "gpt-4.5-preview" GPT4Dot5Preview20250227 = "gpt-4.5-preview-2025-02-27" GPT5 = "gpt-5" GPT5Mini = "gpt-5-mini" GPT5Nano = "gpt-5-nano" GPT5ChatLatest = "gpt-5-chat-latest" GPT3Dot5Turbo0125 = "gpt-3.5-turbo-0125" GPT3Dot5Turbo1106 = "gpt-3.5-turbo-1106" GPT3Dot5Turbo0613 = "gpt-3.5-turbo-0613" GPT3Dot5Turbo0301 = "gpt-3.5-turbo-0301" GPT3Dot5Turbo16K = "gpt-3.5-turbo-16k" GPT3Dot5Turbo16K0613 = "gpt-3.5-turbo-16k-0613" GPT3Dot5Turbo = "gpt-3.5-turbo" GPT3Dot5TurboInstruct = "gpt-3.5-turbo-instruct" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3TextDavinci003 = "text-davinci-003" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3TextDavinci002 = "text-davinci-002" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3TextCurie001 = "text-curie-001" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3TextBabbage001 = "text-babbage-001" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3TextAda001 = "text-ada-001" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3TextDavinci001 = "text-davinci-001" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3DavinciInstructBeta = "davinci-instruct-beta" // Deprecated: Model is shutdown. Use davinci-002 instead. GPT3Davinci = "davinci" GPT3Davinci002 = "davinci-002" // Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead. GPT3CurieInstructBeta = "curie-instruct-beta" GPT3Curie = "curie" GPT3Curie002 = "curie-002" // Deprecated: Model is shutdown. Use babbage-002 instead. GPT3Ada = "ada" GPT3Ada002 = "ada-002" // Deprecated: Model is shutdown. Use babbage-002 instead. GPT3Babbage = "babbage" GPT3Babbage002 = "babbage-002" ) // Codex Defines the models provided by OpenAI. // These models are designed for code-specific tasks, and use // a different tokenizer which optimizes for whitespace. const ( CodexCodeDavinci002 = "code-davinci-002" CodexCodeCushman001 = "code-cushman-001" CodexCodeDavinci001 = "code-davinci-001" ) var disabledModelsForEndpoints = map[string]map[string]bool{ "/completions": { O1Mini: true, O1Mini20240912: true, O1Preview: true, O1Preview20240912: true, O3Mini: true, O3Mini20250131: true, O4Mini: true, O4Mini20250416: true, O3: true, O320250416: true, GPT3Dot5Turbo: true, GPT3Dot5Turbo0301: true, GPT3Dot5Turbo0613: true, GPT3Dot5Turbo1106: true, GPT3Dot5Turbo0125: true, GPT3Dot5Turbo16K: true, GPT3Dot5Turbo16K0613: true, GPT4: true, GPT4Dot5Preview: true, GPT4Dot5Preview20250227: true, GPT4o: true, GPT4o20240513: true, GPT4o20240806: true, GPT4o20241120: true, GPT4oLatest: true, GPT4oMini: true, GPT4oMini20240718: true, GPT4TurboPreview: true, GPT4VisionPreview: true, GPT4Turbo1106: true, GPT4Turbo0125: true, GPT4Turbo: true, GPT4Turbo20240409: true, GPT40314: true, GPT40613: true, GPT432K: true, GPT432K0314: true, GPT432K0613: true, O1: true, GPT4Dot1: true, GPT4Dot120250414: true, GPT4Dot1Mini: true, GPT4Dot1Mini20250414: true, GPT4Dot1Nano: true, GPT4Dot1Nano20250414: true, GPT5: true, GPT5Mini: true, GPT5Nano: true, GPT5ChatLatest: true, }, chatCompletionsSuffix: { CodexCodeDavinci002: true, CodexCodeCushman001: true, CodexCodeDavinci001: true, GPT3TextDavinci003: true, GPT3TextDavinci002: true, GPT3TextCurie001: true, GPT3TextBabbage001: true, GPT3TextAda001: true, GPT3TextDavinci001: true, GPT3DavinciInstructBeta: true, GPT3Davinci: true, GPT3CurieInstructBeta: true, GPT3Curie: true, GPT3Ada: true, GPT3Babbage: true, }, } func checkEndpointSupportsModel(endpoint, model string) bool { return !disabledModelsForEndpoints[endpoint][model] } func checkPromptType(prompt any) bool { _, isString := prompt.(string) _, isStringSlice := prompt.([]string) if isString || isStringSlice { return true } // check if it is prompt is []string hidden under []any slice, isSlice := prompt.([]any) if !isSlice { return false } for _, item := range slice { _, itemIsString := item.(string) if !itemIsString { return false } } return true // all items in the slice are string, so it is []string } // CompletionRequest represents a request structure for completion API. type CompletionRequest struct { Model string `json:"model"` Prompt any `json:"prompt,omitempty"` BestOf int `json:"best_of,omitempty"` Echo bool `json:"echo,omitempty"` FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string. // incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}` // refs: https://platform.openai.com/docs/api-reference/completions/create#completions/create-logit_bias LogitBias map[string]int `json:"logit_bias,omitempty"` // Store can be set to true to store the output of this completion request for use in distillations and evals. // https://platform.openai.com/docs/api-reference/chat/create#chat-create-store Store bool `json:"store,omitempty"` // Metadata to store with the completion. Metadata map[string]string `json:"metadata,omitempty"` LogProbs int `json:"logprobs,omitempty"` MaxTokens int `json:"max_tokens,omitempty"` N int `json:"n,omitempty"` PresencePenalty float32 `json:"presence_penalty,omitempty"` Seed *int `json:"seed,omitempty"` Stop []string `json:"stop,omitempty"` Stream bool `json:"stream,omitempty"` Suffix string `json:"suffix,omitempty"` Temperature float32 `json:"temperature,omitempty"` TopP float32 `json:"top_p,omitempty"` User string `json:"user,omitempty"` // Options for streaming response. Only set this when you set stream: true. StreamOptions *StreamOptions `json:"stream_options,omitempty"` } // CompletionChoice represents one of possible completions. type CompletionChoice struct { Text string `json:"text"` Index int `json:"index"` FinishReason string `json:"finish_reason"` LogProbs LogprobResult `json:"logprobs"` } // LogprobResult represents logprob result of Choice. type LogprobResult struct { Tokens []string `json:"tokens"` TokenLogprobs []float32 `json:"token_logprobs"` TopLogprobs []map[string]float32 `json:"top_logprobs"` TextOffset []int `json:"text_offset"` } // CompletionResponse represents a response structure for completion API. type CompletionResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Choices []CompletionChoice `json:"choices"` Usage *Usage `json:"usage,omitempty"` httpHeader } // CreateCompletion — API call to create a completion. This is the main endpoint of the API. Returns new text as well // as, if requested, the probabilities over each alternative token at each position. // // If using a fine-tuned model, simply provide the model's ID in the CompletionRequest object, // and the server will use the model's parameters to generate the completion. func (c *Client) CreateCompletion( ctx context.Context, request CompletionRequest, ) (response CompletionResponse, err error) { if request.Stream { err = ErrCompletionStreamNotSupported return } urlSuffix := "/completions" if !checkEndpointSupportsModel(urlSuffix, request.Model) { err = ErrCompletionUnsupportedModel return } if !checkPromptType(request.Prompt) { err = ErrCompletionRequestPromptTypeNotSupported return } req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix, withModel(request.Model)), withBody(request), ) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: completion_test.go ================================================ package openai_test import ( "context" "encoding/json" "errors" "fmt" "io" "net/http" "strconv" "strings" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestCompletionsWrongModel(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) _, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel, but returned: %v", err) } } // TestCompletionsWrongModelO3 Tests the completions endpoint with O3 model which is not supported. func TestCompletionsWrongModelO3(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) _, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: openai.O3, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel for O3, but returned: %v", err) } } // TestCompletionsWrongModelO4Mini Tests the completions endpoint with O4Mini model which is not supported. func TestCompletionsWrongModelO4Mini(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) _, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: openai.O4Mini, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel for O4Mini, but returned: %v", err) } } func TestCompletionWithStream(t *testing.T) { config := openai.DefaultConfig("whatever") client := openai.NewClientWithConfig(config) ctx := context.Background() req := openai.CompletionRequest{Stream: true} _, err := client.CreateCompletion(ctx, req) if !errors.Is(err, openai.ErrCompletionStreamNotSupported) { t.Fatalf("CreateCompletion didn't return ErrCompletionStreamNotSupported") } } // TestCompletions Tests the completions endpoint of the API using the mocked server. func TestCompletions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", handleCompletionEndpoint) req := openai.CompletionRequest{ MaxTokens: 5, Model: "ada", Prompt: "Lorem ipsum", } _, err := client.CreateCompletion(context.Background(), req) checks.NoError(t, err, "CreateCompletion error") } // TestMultiplePromptsCompletionsWrong Tests the completions endpoint of the API using the mocked server // where the completions requests has a list of prompts with wrong type. func TestMultiplePromptsCompletionsWrong(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", handleCompletionEndpoint) req := openai.CompletionRequest{ MaxTokens: 5, Model: "ada", Prompt: []interface{}{"Lorem ipsum", 9}, } _, err := client.CreateCompletion(context.Background(), req) if !errors.Is(err, openai.ErrCompletionRequestPromptTypeNotSupported) { t.Fatalf("CreateCompletion should return ErrCompletionRequestPromptTypeNotSupported, but returned: %v", err) } } // TestMultiplePromptsCompletions Tests the completions endpoint of the API using the mocked server // where the completions requests has a list of prompts. func TestMultiplePromptsCompletions(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", handleCompletionEndpoint) req := openai.CompletionRequest{ MaxTokens: 5, Model: "ada", Prompt: []interface{}{"Lorem ipsum", "Lorem ipsum"}, } _, err := client.CreateCompletion(context.Background(), req) checks.NoError(t, err, "CreateCompletion error") } // handleCompletionEndpoint Handles the completion endpoint by the test server. func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) { var err error var resBytes []byte // completions only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } var completionReq openai.CompletionRequest if completionReq, err = getCompletionBody(r); err != nil { http.Error(w, "could not read request", http.StatusInternalServerError) return } res := openai.CompletionResponse{ ID: strconv.Itoa(int(time.Now().Unix())), Object: "test-object", Created: time.Now().Unix(), // would be nice to validate Model during testing, but // this may not be possible with how much upkeep // would be required / wouldn't make much sense Model: completionReq.Model, } // create completions n := completionReq.N if n == 0 { n = 1 } // Handle different types of prompts: single string or list of strings prompts := []string{} switch v := completionReq.Prompt.(type) { case string: prompts = append(prompts, v) case []interface{}: for _, item := range v { if str, ok := item.(string); ok { prompts = append(prompts, str) } } default: http.Error(w, "Invalid prompt type", http.StatusBadRequest) return } for i := 0; i < n; i++ { for _, prompt := range prompts { // Generate a random string of length completionReq.MaxTokens completionStr := strings.Repeat("a", completionReq.MaxTokens) if completionReq.Echo { completionStr = prompt + completionStr } res.Choices = append(res.Choices, openai.CompletionChoice{ Text: completionStr, Index: len(res.Choices), }) } } inputTokens := 0 for _, prompt := range prompts { inputTokens += numTokens(prompt) } inputTokens *= n completionTokens := completionReq.MaxTokens * len(prompts) * n res.Usage = &openai.Usage{ PromptTokens: inputTokens, CompletionTokens: completionTokens, TotalTokens: inputTokens + completionTokens, } // Serialize the response and send it back resBytes, _ = json.Marshal(res) fmt.Fprintln(w, string(resBytes)) } // getCompletionBody Returns the body of the request to create a completion. func getCompletionBody(r *http.Request) (openai.CompletionRequest, error) { completion := openai.CompletionRequest{} // read the request body reqBody, err := io.ReadAll(r.Body) if err != nil { return openai.CompletionRequest{}, err } err = json.Unmarshal(reqBody, &completion) if err != nil { return openai.CompletionRequest{}, err } return completion, nil } // TestCompletionWithO1Model Tests that O1 model is not supported for completion endpoint. func TestCompletionWithO1Model(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) _, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: openai.O1, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel for O1 model, but returned: %v", err) } } // TestCompletionWithGPT4DotModels Tests that newer GPT4 models are not supported for completion endpoint. func TestCompletionWithGPT4DotModels(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) models := []string{ openai.GPT4Dot1, openai.GPT4Dot120250414, openai.GPT4Dot1Mini, openai.GPT4Dot1Mini20250414, openai.GPT4Dot1Nano, openai.GPT4Dot1Nano20250414, openai.GPT4Dot5Preview, openai.GPT4Dot5Preview20250227, } for _, model := range models { t.Run(model, func(t *testing.T) { _, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: model, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel for %s model, but returned: %v", model, err) } }) } } // TestCompletionWithGPT4oModels Tests that GPT4o models are not supported for completion endpoint. func TestCompletionWithGPT4oModels(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) models := []string{ openai.GPT4o, openai.GPT4o20240513, openai.GPT4o20240806, openai.GPT4o20241120, openai.GPT4oLatest, openai.GPT4oMini, openai.GPT4oMini20240718, } for _, model := range models { t.Run(model, func(t *testing.T) { _, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: model, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel for %s model, but returned: %v", model, err) } }) } } // TestCompletionWithGPT5Models Tests that GPT5 models are not supported for completion endpoint. func TestCompletionWithGPT5Models(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) models := []string{ openai.GPT5, openai.GPT5Mini, openai.GPT5Nano, openai.GPT5ChatLatest, } for _, model := range models { t.Run(model, func(t *testing.T) { _, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: model, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel for %s model, but returned: %v", model, err) } }) } } ================================================ FILE: config.go ================================================ package openai import ( "net/http" "regexp" "strings" ) const ( openaiAPIURLv1 = "https://api.openai.com/v1" defaultEmptyMessagesLimit uint = 300 azureAPIPrefix = "openai" azureDeploymentsPrefix = "deployments" AnthropicAPIVersion = "2023-06-01" ) type APIType string const ( APITypeOpenAI APIType = "OPEN_AI" APITypeAzure APIType = "AZURE" APITypeAzureAD APIType = "AZURE_AD" APITypeCloudflareAzure APIType = "CLOUDFLARE_AZURE" APITypeAnthropic APIType = "ANTHROPIC" ) const AzureAPIKeyHeader = "api-key" const defaultAssistantVersion = "v2" // upgrade to v2 to support vector store type HTTPDoer interface { Do(req *http.Request) (*http.Response, error) } // ClientConfig is a configuration of a client. type ClientConfig struct { authToken string BaseURL string OrgID string APIType APIType APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD or APITypeAnthropic AssistantVersion string AzureModelMapperFunc func(model string) string // replace model to azure deployment name func HTTPClient HTTPDoer EmptyMessagesLimit uint } func DefaultConfig(authToken string) ClientConfig { return ClientConfig{ authToken: authToken, BaseURL: openaiAPIURLv1, APIType: APITypeOpenAI, AssistantVersion: defaultAssistantVersion, OrgID: "", HTTPClient: &http.Client{}, EmptyMessagesLimit: defaultEmptyMessagesLimit, } } func DefaultAzureConfig(apiKey, baseURL string) ClientConfig { return ClientConfig{ authToken: apiKey, BaseURL: baseURL, OrgID: "", APIType: APITypeAzure, APIVersion: "2023-05-15", AzureModelMapperFunc: func(model string) string { // only 3.5 models have the "." stripped in their names if strings.Contains(model, "3.5") { return regexp.MustCompile(`[.:]`).ReplaceAllString(model, "") } return strings.ReplaceAll(model, ":", "") }, HTTPClient: &http.Client{}, EmptyMessagesLimit: defaultEmptyMessagesLimit, } } func DefaultAnthropicConfig(apiKey, baseURL string) ClientConfig { if baseURL == "" { baseURL = "https://api.anthropic.com/v1" } return ClientConfig{ authToken: apiKey, BaseURL: baseURL, OrgID: "", APIType: APITypeAnthropic, APIVersion: AnthropicAPIVersion, HTTPClient: &http.Client{}, EmptyMessagesLimit: defaultEmptyMessagesLimit, } } func (ClientConfig) String() string { return "" } func (c ClientConfig) GetAzureDeploymentByModel(model string) string { if c.AzureModelMapperFunc != nil { return c.AzureModelMapperFunc(model) } return model } ================================================ FILE: config_test.go ================================================ package openai_test import ( "testing" "github.com/sashabaranov/go-openai" ) func TestGetAzureDeploymentByModel(t *testing.T) { cases := []struct { Model string AzureModelMapperFunc func(model string) string Expect string }{ { Model: "gpt-3.5-turbo", Expect: "gpt-35-turbo", }, { Model: "gpt-3.5-turbo-0301", Expect: "gpt-35-turbo-0301", }, { Model: "gpt-4.1", Expect: "gpt-4.1", }, { Model: "text-embedding-ada-002", Expect: "text-embedding-ada-002", }, { Model: "", Expect: "", }, { Model: "models", Expect: "models", }, { Model: "gpt-3.5-turbo", Expect: "my-gpt35", AzureModelMapperFunc: func(model string) string { modelmapper := map[string]string{ "gpt-3.5-turbo": "my-gpt35", } if val, ok := modelmapper[model]; ok { return val } return model }, }, } for _, c := range cases { t.Run(c.Model, func(t *testing.T) { conf := openai.DefaultAzureConfig("", "https://test.openai.azure.com/") if c.AzureModelMapperFunc != nil { conf.AzureModelMapperFunc = c.AzureModelMapperFunc } actual := conf.GetAzureDeploymentByModel(c.Model) if actual != c.Expect { t.Errorf("Expected %s, got %s", c.Expect, actual) } }) } } func TestDefaultAnthropicConfig(t *testing.T) { apiKey := "test-key" baseURL := "https://api.anthropic.com/v1" config := openai.DefaultAnthropicConfig(apiKey, baseURL) if config.APIType != openai.APITypeAnthropic { t.Errorf("Expected APIType to be %v, got %v", openai.APITypeAnthropic, config.APIType) } if config.APIVersion != openai.AnthropicAPIVersion { t.Errorf("Expected APIVersion to be 2023-06-01, got %v", config.APIVersion) } if config.BaseURL != baseURL { t.Errorf("Expected BaseURL to be %v, got %v", baseURL, config.BaseURL) } if config.EmptyMessagesLimit != 300 { t.Errorf("Expected EmptyMessagesLimit to be 300, got %v", config.EmptyMessagesLimit) } } func TestDefaultAnthropicConfigWithEmptyValues(t *testing.T) { config := openai.DefaultAnthropicConfig("", "") if config.APIType != openai.APITypeAnthropic { t.Errorf("Expected APIType to be %v, got %v", openai.APITypeAnthropic, config.APIType) } if config.APIVersion != openai.AnthropicAPIVersion { t.Errorf("Expected APIVersion to be %s, got %v", openai.AnthropicAPIVersion, config.APIVersion) } expectedBaseURL := "https://api.anthropic.com/v1" if config.BaseURL != expectedBaseURL { t.Errorf("Expected BaseURL to be %v, got %v", expectedBaseURL, config.BaseURL) } } func TestClientConfigString(t *testing.T) { // String() should always return the constant value conf := openai.DefaultConfig("dummy-token") expected := "" got := conf.String() if got != expected { t.Errorf("ClientConfig.String() = %q; want %q", got, expected) } } func TestGetAzureDeploymentByModel_NoMapper(t *testing.T) { // On a zero-value or DefaultConfig, AzureModelMapperFunc is nil, // so GetAzureDeploymentByModel should just return the input model. conf := openai.DefaultConfig("dummy-token") model := "some-model" got := conf.GetAzureDeploymentByModel(model) if got != model { t.Errorf("GetAzureDeploymentByModel(%q) = %q; want %q", model, got, model) } } ================================================ FILE: edits.go ================================================ package openai import ( "context" "fmt" "net/http" ) // EditsRequest represents a request structure for Edits API. type EditsRequest struct { Model *string `json:"model,omitempty"` Input string `json:"input,omitempty"` Instruction string `json:"instruction,omitempty"` N int `json:"n,omitempty"` Temperature float32 `json:"temperature,omitempty"` TopP float32 `json:"top_p,omitempty"` } // EditsChoice represents one of possible edits. type EditsChoice struct { Text string `json:"text"` Index int `json:"index"` } // EditsResponse represents a response structure for Edits API. type EditsResponse struct { Object string `json:"object"` Created int64 `json:"created"` Usage Usage `json:"usage"` Choices []EditsChoice `json:"choices"` httpHeader } // Edits Perform an API call to the Edits endpoint. /* Deprecated: Users of the Edits API and its associated models (e.g., text-davinci-edit-001 or code-davinci-edit-001) will need to migrate to GPT-3.5 Turbo by January 4, 2024. You can use CreateChatCompletion or CreateChatCompletionStream instead. */ func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error) { req, err := c.newRequest( ctx, http.MethodPost, c.fullURL("/edits", withModel(fmt.Sprint(request.Model))), withBody(request), ) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: edits_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "io" "net/http" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) // TestEdits Tests the edits endpoint of the API using the mocked server. func TestEdits(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/edits", handleEditEndpoint) // create an edit request model := "ada" editReq := openai.EditsRequest{ Model: &model, Input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim" + " ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip" + " ex ea commodo consequat. Duis aute irure dolor in reprehe", Instruction: "test instruction", N: 3, } response, err := client.Edits(context.Background(), editReq) checks.NoError(t, err, "Edits error") if len(response.Choices) != editReq.N { t.Fatalf("edits does not properly return the correct number of choices") } } // handleEditEndpoint Handles the edit endpoint by the test server. func handleEditEndpoint(w http.ResponseWriter, r *http.Request) { var err error var resBytes []byte // edits only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } var editReq openai.EditsRequest editReq, err = getEditBody(r) if err != nil { http.Error(w, "could not read request", http.StatusInternalServerError) return } // create a response res := openai.EditsResponse{ Object: "test-object", Created: time.Now().Unix(), } // edit and calculate token usage editString := "edited by mocked OpenAI server :)" inputTokens := numTokens(editReq.Input+editReq.Instruction) * editReq.N completionTokens := int(float32(len(editString))/4) * editReq.N for i := 0; i < editReq.N; i++ { // instruction will be hidden and only seen by OpenAI res.Choices = append(res.Choices, openai.EditsChoice{ Text: editReq.Input + editString, Index: i, }) } res.Usage = openai.Usage{ PromptTokens: inputTokens, CompletionTokens: completionTokens, TotalTokens: inputTokens + completionTokens, } resBytes, _ = json.Marshal(res) fmt.Fprint(w, string(resBytes)) } // getEditBody Returns the body of the request to create an edit. func getEditBody(r *http.Request) (openai.EditsRequest, error) { edit := openai.EditsRequest{} // read the request body reqBody, err := io.ReadAll(r.Body) if err != nil { return openai.EditsRequest{}, err } err = json.Unmarshal(reqBody, &edit) if err != nil { return openai.EditsRequest{}, err } return edit, nil } ================================================ FILE: embeddings.go ================================================ package openai import ( "context" "encoding/base64" "encoding/binary" "encoding/json" "errors" "math" "net/http" ) var ErrVectorLengthMismatch = errors.New("vector length mismatch") // EmbeddingModel enumerates the models which can be used // to generate Embedding vectors. type EmbeddingModel string const ( // Deprecated: The following block is shut down. Use text-embedding-ada-002 instead. AdaSimilarity EmbeddingModel = "text-similarity-ada-001" BabbageSimilarity EmbeddingModel = "text-similarity-babbage-001" CurieSimilarity EmbeddingModel = "text-similarity-curie-001" DavinciSimilarity EmbeddingModel = "text-similarity-davinci-001" AdaSearchDocument EmbeddingModel = "text-search-ada-doc-001" AdaSearchQuery EmbeddingModel = "text-search-ada-query-001" BabbageSearchDocument EmbeddingModel = "text-search-babbage-doc-001" BabbageSearchQuery EmbeddingModel = "text-search-babbage-query-001" CurieSearchDocument EmbeddingModel = "text-search-curie-doc-001" CurieSearchQuery EmbeddingModel = "text-search-curie-query-001" DavinciSearchDocument EmbeddingModel = "text-search-davinci-doc-001" DavinciSearchQuery EmbeddingModel = "text-search-davinci-query-001" AdaCodeSearchCode EmbeddingModel = "code-search-ada-code-001" AdaCodeSearchText EmbeddingModel = "code-search-ada-text-001" BabbageCodeSearchCode EmbeddingModel = "code-search-babbage-code-001" BabbageCodeSearchText EmbeddingModel = "code-search-babbage-text-001" AdaEmbeddingV2 EmbeddingModel = "text-embedding-ada-002" SmallEmbedding3 EmbeddingModel = "text-embedding-3-small" LargeEmbedding3 EmbeddingModel = "text-embedding-3-large" ) // Embedding is a special format of data representation that can be easily utilized by machine // learning models and algorithms. The embedding is an information dense representation of the // semantic meaning of a piece of text. Each embedding is a vector of floating point numbers, // such that the distance between two embeddings in the vector space is correlated with semantic similarity // between two inputs in the original format. For example, if two texts are similar, // then their vector representations should also be similar. type Embedding struct { Object string `json:"object"` Embedding []float32 `json:"embedding"` Index int `json:"index"` } // DotProduct calculates the dot product of the embedding vector with another // embedding vector. Both vectors must have the same length; otherwise, an // ErrVectorLengthMismatch is returned. The method returns the calculated dot // product as a float32 value. func (e *Embedding) DotProduct(other *Embedding) (float32, error) { if len(e.Embedding) != len(other.Embedding) { return 0, ErrVectorLengthMismatch } var dotProduct float32 for i := range e.Embedding { dotProduct += e.Embedding[i] * other.Embedding[i] } return dotProduct, nil } // EmbeddingResponse is the response from a Create embeddings request. type EmbeddingResponse struct { Object string `json:"object"` Data []Embedding `json:"data"` Model EmbeddingModel `json:"model"` Usage Usage `json:"usage"` httpHeader } type base64String string func (b base64String) Decode() ([]float32, error) { decodedData, err := base64.StdEncoding.DecodeString(string(b)) if err != nil { return nil, err } const sizeOfFloat32 = 4 floats := make([]float32, len(decodedData)/sizeOfFloat32) for i := 0; i < len(floats); i++ { floats[i] = math.Float32frombits(binary.LittleEndian.Uint32(decodedData[i*4 : (i+1)*4])) } return floats, nil } // Base64Embedding is a container for base64 encoded embeddings. type Base64Embedding struct { Object string `json:"object"` Embedding base64String `json:"embedding"` Index int `json:"index"` } // EmbeddingResponseBase64 is the response from a Create embeddings request with base64 encoding format. type EmbeddingResponseBase64 struct { Object string `json:"object"` Data []Base64Embedding `json:"data"` Model EmbeddingModel `json:"model"` Usage Usage `json:"usage"` httpHeader } // ToEmbeddingResponse converts an embeddingResponseBase64 to an EmbeddingResponse. func (r *EmbeddingResponseBase64) ToEmbeddingResponse() (EmbeddingResponse, error) { data := make([]Embedding, len(r.Data)) for i, base64Embedding := range r.Data { embedding, err := base64Embedding.Embedding.Decode() if err != nil { return EmbeddingResponse{}, err } data[i] = Embedding{ Object: base64Embedding.Object, Embedding: embedding, Index: base64Embedding.Index, } } return EmbeddingResponse{ Object: r.Object, Model: r.Model, Data: data, Usage: r.Usage, }, nil } type EmbeddingRequestConverter interface { // Needs to be of type EmbeddingRequestStrings or EmbeddingRequestTokens Convert() EmbeddingRequest } // EmbeddingEncodingFormat is the format of the embeddings data. // Currently, only "float" and "base64" are supported, however, "base64" is not officially documented. // If not specified OpenAI will use "float". type EmbeddingEncodingFormat string const ( EmbeddingEncodingFormatFloat EmbeddingEncodingFormat = "float" EmbeddingEncodingFormatBase64 EmbeddingEncodingFormat = "base64" ) type EmbeddingRequest struct { Input any `json:"input"` Model EmbeddingModel `json:"model"` User string `json:"user,omitempty"` EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"` // Dimensions The number of dimensions the resulting output embeddings should have. // Only supported in text-embedding-3 and later models. Dimensions int `json:"dimensions,omitempty"` // The ExtraBody field allows for the inclusion of arbitrary key-value pairs // in the request body that may not be explicitly defined in this struct. ExtraBody map[string]any `json:"extra_body,omitempty"` } func (r EmbeddingRequest) Convert() EmbeddingRequest { return r } // EmbeddingRequestStrings is the input to a create embeddings request with a slice of strings. type EmbeddingRequestStrings struct { // Input is a slice of strings for which you want to generate an Embedding vector. // Each input must not exceed 8192 tokens in length. // OpenAPI suggests replacing newlines (\n) in your input with a single space, as they // have observed inferior results when newlines are present. // E.g. // "The food was delicious and the waiter..." Input []string `json:"input"` // ID of the model to use. You can use the List models API to see all of your available models, // or see our Model overview for descriptions of them. Model EmbeddingModel `json:"model"` // A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. User string `json:"user"` // EmbeddingEncodingFormat is the format of the embeddings data. // Currently, only "float" and "base64" are supported, however, "base64" is not officially documented. // If not specified OpenAI will use "float". EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"` // Dimensions The number of dimensions the resulting output embeddings should have. // Only supported in text-embedding-3 and later models. Dimensions int `json:"dimensions,omitempty"` // The ExtraBody field allows for the inclusion of arbitrary key-value pairs // in the request body that may not be explicitly defined in this struct. ExtraBody map[string]any `json:"extra_body,omitempty"` } func (r EmbeddingRequestStrings) Convert() EmbeddingRequest { return EmbeddingRequest{ Input: r.Input, Model: r.Model, User: r.User, EncodingFormat: r.EncodingFormat, Dimensions: r.Dimensions, ExtraBody: r.ExtraBody, } } type EmbeddingRequestTokens struct { // Input is a slice of slices of ints ([][]int) for which you want to generate an Embedding vector. // Each input must not exceed 8192 tokens in length. // OpenAPI suggests replacing newlines (\n) in your input with a single space, as they // have observed inferior results when newlines are present. // E.g. // "The food was delicious and the waiter..." Input [][]int `json:"input"` // ID of the model to use. You can use the List models API to see all of your available models, // or see our Model overview for descriptions of them. Model EmbeddingModel `json:"model"` // A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. User string `json:"user"` // EmbeddingEncodingFormat is the format of the embeddings data. // Currently, only "float" and "base64" are supported, however, "base64" is not officially documented. // If not specified OpenAI will use "float". EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"` // Dimensions The number of dimensions the resulting output embeddings should have. // Only supported in text-embedding-3 and later models. Dimensions int `json:"dimensions,omitempty"` // The ExtraBody field allows for the inclusion of arbitrary key-value pairs // in the request body that may not be explicitly defined in this struct. ExtraBody map[string]any `json:"extra_body,omitempty"` } func (r EmbeddingRequestTokens) Convert() EmbeddingRequest { return EmbeddingRequest{ Input: r.Input, Model: r.Model, User: r.User, EncodingFormat: r.EncodingFormat, Dimensions: r.Dimensions, ExtraBody: r.ExtraBody, } } // CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |body.Input|. // https://beta.openai.com/docs/api-reference/embeddings/create // // Body should be of type EmbeddingRequestStrings for embedding strings or EmbeddingRequestTokens // for embedding groups of text already converted to tokens. func (c *Client) CreateEmbeddings( ctx context.Context, conv EmbeddingRequestConverter, ) (res EmbeddingResponse, err error) { baseReq := conv.Convert() // The body map is used to dynamically construct the request payload for the embedding API. // Instead of relying on a fixed struct, the body map allows for flexible inclusion of fields // based on their presence, avoiding unnecessary or empty fields in the request. extraBody := baseReq.ExtraBody baseReq.ExtraBody = nil // Serialize baseReq to JSON jsonData, err := json.Marshal(baseReq) if err != nil { return } // Deserialize JSON to map[string]any var body map[string]any _ = json.Unmarshal(jsonData, &body) req, err := c.newRequest( ctx, http.MethodPost, c.fullURL("/embeddings", withModel(string(baseReq.Model))), withBody(body), // Main request body. withExtraBody(extraBody), // Merge ExtraBody fields. ) if err != nil { return } if baseReq.EncodingFormat != EmbeddingEncodingFormatBase64 { err = c.sendRequest(req, &res) return } base64Response := &EmbeddingResponseBase64{} err = c.sendRequest(req, base64Response) if err != nil { return } res, err = base64Response.ToEmbeddingResponse() return } ================================================ FILE: embeddings_test.go ================================================ package openai_test import ( "bytes" "context" "encoding/json" "errors" "fmt" "math" "net/http" "reflect" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestEmbedding(t *testing.T) { embeddedModels := []openai.EmbeddingModel{ openai.AdaSimilarity, openai.BabbageSimilarity, openai.CurieSimilarity, openai.DavinciSimilarity, openai.AdaSearchDocument, openai.AdaSearchQuery, openai.BabbageSearchDocument, openai.BabbageSearchQuery, openai.CurieSearchDocument, openai.CurieSearchQuery, openai.DavinciSearchDocument, openai.DavinciSearchQuery, openai.AdaCodeSearchCode, openai.AdaCodeSearchText, openai.BabbageCodeSearchCode, openai.BabbageCodeSearchText, } for _, model := range embeddedModels { // test embedding request with strings (simple embedding request) embeddingReq := openai.EmbeddingRequest{ Input: []string{ "The food was delicious and the waiter", "Other examples of embedding request", }, Model: model, } // marshal embeddingReq to JSON and confirm that the model field equals // the AdaSearchQuery type marshaled, err := json.Marshal(embeddingReq) checks.NoError(t, err, "Could not marshal embedding request") if !bytes.Contains(marshaled, []byte(`"model":"`+model+`"`)) { t.Fatalf("Expected embedding request to contain model field") } // test embedding request with strings and extra_body param embeddingReqWithExtraBody := openai.EmbeddingRequest{ Input: []string{ "The food was delicious and the waiter", "Other examples of embedding request", }, Model: model, ExtraBody: map[string]any{ "input_type": "query", "truncate": "NONE", }, } marshaled, err = json.Marshal(embeddingReqWithExtraBody) checks.NoError(t, err, "Could not marshal embedding request") if !bytes.Contains(marshaled, []byte(`"model":"`+model+`"`)) { t.Fatalf("Expected embedding request to contain model field") } // test embedding request with strings embeddingReqStrings := openai.EmbeddingRequestStrings{ Input: []string{ "The food was delicious and the waiter", "Other examples of embedding request", }, Model: model, } marshaled, err = json.Marshal(embeddingReqStrings) checks.NoError(t, err, "Could not marshal embedding request") if !bytes.Contains(marshaled, []byte(`"model":"`+model+`"`)) { t.Fatalf("Expected embedding request to contain model field") } // test embedding request with tokens embeddingReqTokens := openai.EmbeddingRequestTokens{ Input: [][]int{ {464, 2057, 373, 12625, 290, 262, 46612}, {6395, 6096, 286, 11525, 12083, 2581}, }, Model: model, } marshaled, err = json.Marshal(embeddingReqTokens) checks.NoError(t, err, "Could not marshal embedding request") if !bytes.Contains(marshaled, []byte(`"model":"`+model+`"`)) { t.Fatalf("Expected embedding request to contain model field") } } } func TestEmbeddingEndpoint(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() sampleEmbeddings := []openai.Embedding{ {Embedding: []float32{1.23, 4.56, 7.89}}, {Embedding: []float32{-0.006968617, -0.0052718227, 0.011901081}}, } sampleBase64Embeddings := []openai.Base64Embedding{ {Embedding: "pHCdP4XrkUDhevxA"}, {Embedding: "/1jku0G/rLvA/EI8"}, } server.RegisterHandler( "/v1/embeddings", func(w http.ResponseWriter, r *http.Request) { var req struct { EncodingFormat openai.EmbeddingEncodingFormat `json:"encoding_format"` User string `json:"user"` } _ = json.NewDecoder(r.Body).Decode(&req) var resBytes []byte switch { case req.User == "invalid": w.WriteHeader(http.StatusBadRequest) return case req.EncodingFormat == openai.EmbeddingEncodingFormatBase64: resBytes, _ = json.Marshal(openai.EmbeddingResponseBase64{Data: sampleBase64Embeddings}) default: resBytes, _ = json.Marshal(openai.EmbeddingResponse{Data: sampleEmbeddings}) } fmt.Fprintln(w, string(resBytes)) }, ) // test create embeddings with strings (simple embedding request) res, err := client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{}) checks.NoError(t, err, "CreateEmbeddings error") if !reflect.DeepEqual(res.Data, sampleEmbeddings) { t.Errorf("Expected %#v embeddings, got %#v", sampleEmbeddings, res.Data) } // test create embeddings with strings (ExtraBody in request) res, err = client.CreateEmbeddings( context.Background(), openai.EmbeddingRequest{ ExtraBody: map[string]any{ "input_type": "query", "truncate": "NONE", }, Dimensions: 1, }, ) checks.NoError(t, err, "CreateEmbeddings error") if !reflect.DeepEqual(res.Data, sampleEmbeddings) { t.Errorf("Expected %#v embeddings, got %#v", sampleEmbeddings, res.Data) } // test create embeddings with strings (ExtraBody in request and ) _, err = client.CreateEmbeddings( context.Background(), openai.EmbeddingRequest{ Input: make(chan int), // Channels are not serializable Model: "example_model", }, ) checks.HasError(t, err, "CreateEmbeddings error") // test failed (Serialize JSON error) res, err = client.CreateEmbeddings( context.Background(), openai.EmbeddingRequest{ EncodingFormat: openai.EmbeddingEncodingFormatBase64, }, ) checks.NoError(t, err, "CreateEmbeddings error") if !reflect.DeepEqual(res.Data, sampleEmbeddings) { t.Errorf("Expected %#v embeddings, got %#v", sampleEmbeddings, res.Data) } // test create embeddings with strings res, err = client.CreateEmbeddings(context.Background(), openai.EmbeddingRequestStrings{}) checks.NoError(t, err, "CreateEmbeddings strings error") if !reflect.DeepEqual(res.Data, sampleEmbeddings) { t.Errorf("Expected %#v embeddings, got %#v", sampleEmbeddings, res.Data) } // test create embeddings with tokens res, err = client.CreateEmbeddings(context.Background(), openai.EmbeddingRequestTokens{}) checks.NoError(t, err, "CreateEmbeddings tokens error") if !reflect.DeepEqual(res.Data, sampleEmbeddings) { t.Errorf("Expected %#v embeddings, got %#v", sampleEmbeddings, res.Data) } // test failed sendRequest _, err = client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{ User: "invalid", EncodingFormat: openai.EmbeddingEncodingFormatBase64, }) checks.HasError(t, err, "CreateEmbeddings error") } func TestAzureEmbeddingEndpoint(t *testing.T) { client, server, teardown := setupAzureTestServer() defer teardown() sampleEmbeddings := []openai.Embedding{ {Embedding: []float32{1.23, 4.56, 7.89}}, {Embedding: []float32{-0.006968617, -0.0052718227, 0.011901081}}, } server.RegisterHandler( "/openai/deployments/text-embedding-ada-002/embeddings", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.EmbeddingResponse{Data: sampleEmbeddings}) fmt.Fprintln(w, string(resBytes)) }, ) // test create embeddings with strings (simple embedding request) res, err := client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{ Model: openai.AdaEmbeddingV2, }) checks.NoError(t, err, "CreateEmbeddings error") if !reflect.DeepEqual(res.Data, sampleEmbeddings) { t.Errorf("Expected %#v embeddings, got %#v", sampleEmbeddings, res.Data) } } func TestEmbeddingResponseBase64_ToEmbeddingResponse(t *testing.T) { type fields struct { Object string Data []openai.Base64Embedding Model openai.EmbeddingModel Usage openai.Usage } tests := []struct { name string fields fields want openai.EmbeddingResponse wantErr bool }{ { name: "test embedding response base64 to embedding response", fields: fields{ Data: []openai.Base64Embedding{ {Embedding: "pHCdP4XrkUDhevxA"}, {Embedding: "/1jku0G/rLvA/EI8"}, }, }, want: openai.EmbeddingResponse{ Data: []openai.Embedding{ {Embedding: []float32{1.23, 4.56, 7.89}}, {Embedding: []float32{-0.006968617, -0.0052718227, 0.011901081}}, }, }, wantErr: false, }, { name: "Invalid embedding", fields: fields{ Data: []openai.Base64Embedding{ { Embedding: "----", }, }, }, want: openai.EmbeddingResponse{}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := &openai.EmbeddingResponseBase64{ Object: tt.fields.Object, Data: tt.fields.Data, Model: tt.fields.Model, Usage: tt.fields.Usage, } got, err := r.ToEmbeddingResponse() if (err != nil) != tt.wantErr { t.Errorf("EmbeddingResponseBase64.ToEmbeddingResponse() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("EmbeddingResponseBase64.ToEmbeddingResponse() = %v, want %v", got, tt.want) } }) } } func TestDotProduct(t *testing.T) { v1 := &openai.Embedding{Embedding: []float32{1, 2, 3}} v2 := &openai.Embedding{Embedding: []float32{2, 4, 6}} expected := float32(28.0) result, err := v1.DotProduct(v2) if err != nil { t.Errorf("Unexpected error: %v", err) } if math.Abs(float64(result-expected)) > 1e-12 { t.Errorf("Unexpected result. Expected: %v, but got %v", expected, result) } v1 = &openai.Embedding{Embedding: []float32{1, 0, 0}} v2 = &openai.Embedding{Embedding: []float32{0, 1, 0}} expected = float32(0.0) result, err = v1.DotProduct(v2) if err != nil { t.Errorf("Unexpected error: %v", err) } if math.Abs(float64(result-expected)) > 1e-12 { t.Errorf("Unexpected result. Expected: %v, but got %v", expected, result) } // Test for VectorLengthMismatchError v1 = &openai.Embedding{Embedding: []float32{1, 0, 0}} v2 = &openai.Embedding{Embedding: []float32{0, 1}} _, err = v1.DotProduct(v2) if !errors.Is(err, openai.ErrVectorLengthMismatch) { t.Errorf("Expected Vector Length Mismatch Error, but got: %v", err) } } ================================================ FILE: engines.go ================================================ package openai import ( "context" "fmt" "net/http" ) // Engine struct represents engine from OpenAPI API. type Engine struct { ID string `json:"id"` Object string `json:"object"` Owner string `json:"owner"` Ready bool `json:"ready"` httpHeader } // EnginesList is a list of engines. type EnginesList struct { Engines []Engine `json:"data"` httpHeader } // ListEngines Lists the currently available engines, and provides basic // information about each option such as the owner and availability. func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error) { req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/engines")) if err != nil { return } err = c.sendRequest(req, &engines) return } // GetEngine Retrieves an engine instance, providing basic information about // the engine such as the owner and availability. func (c *Client) GetEngine( ctx context.Context, engineID string, ) (engine Engine, err error) { urlSuffix := fmt.Sprintf("/engines/%s", engineID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &engine) return } ================================================ FILE: engines_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "net/http" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) // TestGetEngine Tests the retrieve engine endpoint of the API using the mocked server. func TestGetEngine(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/engines/text-davinci-003", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.Engine{}) fmt.Fprintln(w, string(resBytes)) }) _, err := client.GetEngine(context.Background(), "text-davinci-003") checks.NoError(t, err, "GetEngine error") } // TestListEngines Tests the list engines endpoint of the API using the mocked server. func TestListEngines(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/engines", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.EnginesList{}) fmt.Fprintln(w, string(resBytes)) }) _, err := client.ListEngines(context.Background()) checks.NoError(t, err, "ListEngines error") } func TestListEnginesReturnError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/engines", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusTeapot) }) _, err := client.ListEngines(context.Background()) checks.HasError(t, err, "ListEngines did not fail") } ================================================ FILE: error.go ================================================ package openai import ( "encoding/json" "fmt" "strings" ) // APIError provides error information returned by the OpenAI API. // InnerError struct is only valid for Azure OpenAI Service. type APIError struct { Code any `json:"code,omitempty"` Message string `json:"message"` Param *string `json:"param,omitempty"` Type string `json:"type"` HTTPStatus string `json:"-"` HTTPStatusCode int `json:"-"` InnerError *InnerError `json:"innererror,omitempty"` } // InnerError Azure Content filtering. Only valid for Azure OpenAI Service. type InnerError struct { Code string `json:"code,omitempty"` ContentFilterResults ContentFilterResults `json:"content_filter_result,omitempty"` } // RequestError provides information about generic request errors. type RequestError struct { HTTPStatus string HTTPStatusCode int Err error Body []byte } type ErrorResponse struct { Error *APIError `json:"error,omitempty"` } func (e *APIError) Error() string { if e.HTTPStatusCode > 0 { return fmt.Sprintf("error, status code: %d, status: %s, message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Message) } return e.Message } func (e *APIError) UnmarshalJSON(data []byte) (err error) { var rawMap map[string]json.RawMessage err = json.Unmarshal(data, &rawMap) if err != nil { return } err = json.Unmarshal(rawMap["message"], &e.Message) if err != nil { // If the parameter field of a function call is invalid as a JSON schema // refs: https://github.com/sashabaranov/go-openai/issues/381 var messages []string err = json.Unmarshal(rawMap["message"], &messages) if err != nil { return } e.Message = strings.Join(messages, ", ") } // optional fields for azure openai // refs: https://github.com/sashabaranov/go-openai/issues/343 if _, ok := rawMap["type"]; ok { err = json.Unmarshal(rawMap["type"], &e.Type) if err != nil { return } } if _, ok := rawMap["innererror"]; ok { err = json.Unmarshal(rawMap["innererror"], &e.InnerError) if err != nil { return } } // optional fields if _, ok := rawMap["param"]; ok { err = json.Unmarshal(rawMap["param"], &e.Param) if err != nil { return } } if _, ok := rawMap["code"]; !ok { return nil } // if the api returned a number, we need to force an integer // since the json package defaults to float64 var intCode int err = json.Unmarshal(rawMap["code"], &intCode) if err == nil { e.Code = intCode return nil } return json.Unmarshal(rawMap["code"], &e.Code) } func (e *RequestError) Error() string { return fmt.Sprintf( "error, status code: %d, status: %s, message: %s, body: %s", e.HTTPStatusCode, e.HTTPStatus, e.Err, e.Body, ) } func (e *RequestError) Unwrap() error { return e.Err } ================================================ FILE: error_test.go ================================================ package openai_test import ( "errors" "net/http" "reflect" "testing" "github.com/sashabaranov/go-openai" ) func TestAPIErrorUnmarshalJSON(t *testing.T) { type testCase struct { name string response string hasError bool checkFunc func(t *testing.T, apiErr openai.APIError) } testCases := []testCase{ // testcase for message field { name: "parse succeeds when the message is string", response: `{"message":"foo","type":"invalid_request_error","param":null,"code":null}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorMessage(t, apiErr, "foo") }, }, { name: "parse succeeds when the message is array with single item", response: `{"message":["foo"],"type":"invalid_request_error","param":null,"code":null}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorMessage(t, apiErr, "foo") }, }, { name: "parse succeeds when the message is array with multiple items", response: `{"message":["foo", "bar", "baz"],"type":"invalid_request_error","param":null,"code":null}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorMessage(t, apiErr, "foo, bar, baz") }, }, { name: "parse succeeds when the message is empty array", response: `{"message":[],"type":"invalid_request_error","param":null,"code":null}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorMessage(t, apiErr, "") }, }, { name: "parse succeeds when the message is null", response: `{"message":null,"type":"invalid_request_error","param":null,"code":null}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorMessage(t, apiErr, "") }, }, { name: "parse succeeds when the innerError is not exists (Azure Openai)", response: `{ "message": "test message", "type": null, "param": "prompt", "code": "content_filter", "status": 400, "innererror": { "code": "ResponsibleAIPolicyViolation", "content_filter_result": { "hate": { "filtered": false, "severity": "safe" }, "self_harm": { "filtered": false, "severity": "safe" }, "sexual": { "filtered": true, "severity": "medium" }, "violence": { "filtered": false, "severity": "safe" } } } }`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorInnerError(t, apiErr, &openai.InnerError{ Code: "ResponsibleAIPolicyViolation", ContentFilterResults: openai.ContentFilterResults{ Hate: openai.Hate{ Filtered: false, Severity: "safe", }, SelfHarm: openai.SelfHarm{ Filtered: false, Severity: "safe", }, Sexual: openai.Sexual{ Filtered: true, Severity: "medium", }, Violence: openai.Violence{ Filtered: false, Severity: "safe", }, }, }) }, }, { name: "parse succeeds when the innerError is empty (Azure Openai)", response: `{"message": "","type": null,"param": "","code": "","status": 0,"innererror": {}}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorInnerError(t, apiErr, &openai.InnerError{}) }, }, { name: "parse succeeds when the innerError is not InnerError struct (Azure Openai)", response: `{"message": "","type": null,"param": "","code": "","status": 0,"innererror": "test"}`, hasError: true, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorInnerError(t, apiErr, &openai.InnerError{}) }, }, { name: "parse failed when the message is object", response: `{"message":{},"type":"invalid_request_error","param":null,"code":null}`, hasError: true, }, { name: "parse failed when the message is int", response: `{"message":1,"type":"invalid_request_error","param":null,"code":null}`, hasError: true, }, { name: "parse failed when the message is float", response: `{"message":0.1,"type":"invalid_request_error","param":null,"code":null}`, hasError: true, }, { name: "parse failed when the message is bool", response: `{"message":true,"type":"invalid_request_error","param":null,"code":null}`, hasError: true, }, { name: "parse failed when the message is not exists", response: `{"type":"invalid_request_error","param":null,"code":null}`, hasError: true, }, // testcase for code field { name: "parse succeeds when the code is int", response: `{"code":418,"message":"I'm a teapot","param":"prompt","type":"teapot_error"}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorCode(t, apiErr, 418) }, }, { name: "parse succeeds when the code is string", response: `{"code":"teapot","message":"I'm a teapot","param":"prompt","type":"teapot_error"}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorCode(t, apiErr, "teapot") }, }, { name: "parse succeeds when the code is not exists", response: `{"message":"I'm a teapot","param":"prompt","type":"teapot_error"}`, hasError: false, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorCode(t, apiErr, nil) }, }, // testcase for param field { name: "parse failed when the param is bool", response: `{"code":418,"message":"I'm a teapot","param":true,"type":"teapot_error"}`, hasError: true, }, // testcase for type field { name: "parse failed when the type is bool", response: `{"code":418,"message":"I'm a teapot","param":"prompt","type":true}`, hasError: true, }, // testcase for error response { name: "parse failed when the response is invalid json", response: `--- {"code":418,"message":"I'm a teapot","param":"prompt","type":"teapot_error"}`, hasError: true, checkFunc: func(t *testing.T, apiErr openai.APIError) { assertAPIErrorCode(t, apiErr, nil) assertAPIErrorMessage(t, apiErr, "") assertAPIErrorParam(t, apiErr, nil) assertAPIErrorType(t, apiErr, "") }, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var apiErr openai.APIError err := apiErr.UnmarshalJSON([]byte(tc.response)) if (err != nil) != tc.hasError { t.Errorf("Unexpected error: %v", err) } if tc.checkFunc != nil { tc.checkFunc(t, apiErr) } }) } } func assertAPIErrorMessage(t *testing.T, apiErr openai.APIError, expected string) { if apiErr.Message != expected { t.Errorf("Unexpected APIError message: %v; expected: %s", apiErr, expected) } } func assertAPIErrorInnerError(t *testing.T, apiErr openai.APIError, expected interface{}) { if !reflect.DeepEqual(apiErr.InnerError, expected) { t.Errorf("Unexpected APIError InnerError: %v; expected: %v; ", apiErr, expected) } } func assertAPIErrorCode(t *testing.T, apiErr openai.APIError, expected interface{}) { switch v := apiErr.Code.(type) { case int: if v != expected { t.Errorf("Unexpected APIError code integer: %d; expected %d", v, expected) } case string: if v != expected { t.Errorf("Unexpected APIError code string: %s; expected %s", v, expected) } case nil: default: t.Errorf("Unexpected APIError error code type: %T", v) } } func assertAPIErrorParam(t *testing.T, apiErr openai.APIError, expected *string) { if apiErr.Param != expected { t.Errorf("Unexpected APIError param: %v; expected: %s", apiErr, *expected) } } func assertAPIErrorType(t *testing.T, apiErr openai.APIError, typ string) { if apiErr.Type != typ { t.Errorf("Unexpected API type: %v; expected: %s", apiErr, typ) } } func TestRequestError(t *testing.T) { var err error = &openai.RequestError{ HTTPStatusCode: http.StatusTeapot, Err: errors.New("i am a teapot"), } var reqErr *openai.RequestError if !errors.As(err, &reqErr) { t.Fatalf("Error is not a RequestError: %+v", err) } if reqErr.HTTPStatusCode != 418 { t.Fatalf("Unexpected request error status code: %d", reqErr.HTTPStatusCode) } if reqErr.Unwrap() == nil { t.Fatalf("Empty request error occurred") } } ================================================ FILE: example_test.go ================================================ package openai_test import ( "bufio" "context" "encoding/base64" "errors" "fmt" "io" "net/http" "net/url" "os" "github.com/sashabaranov/go-openai" ) func Example() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello!", }, }, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) return } fmt.Println(resp.Choices[0].Message.Content) } func ExampleClient_CreateChatCompletionStream() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) stream, err := client.CreateChatCompletionStream( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, MaxTokens: 20, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Lorem ipsum", }, }, Stream: true, }, ) if err != nil { fmt.Printf("ChatCompletionStream error: %v\n", err) return } defer stream.Close() fmt.Print("Stream response: ") for { var response openai.ChatCompletionStreamResponse response, err = stream.Recv() if errors.Is(err, io.EOF) { fmt.Println("\nStream finished") return } if err != nil { fmt.Printf("\nStream error: %v\n", err) return } fmt.Println(response.Choices[0].Delta.Content) } } func ExampleClient_CreateCompletion() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ Model: openai.GPT3Babbage002, MaxTokens: 5, Prompt: "Lorem ipsum", }, ) if err != nil { fmt.Printf("Completion error: %v\n", err) return } fmt.Println(resp.Choices[0].Text) } func ExampleClient_CreateCompletionStream() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) stream, err := client.CreateCompletionStream( context.Background(), openai.CompletionRequest{ Model: openai.GPT3Babbage002, MaxTokens: 5, Prompt: "Lorem ipsum", Stream: true, }, ) if err != nil { fmt.Printf("CompletionStream error: %v\n", err) return } defer stream.Close() for { var response openai.CompletionResponse response, err = stream.Recv() if errors.Is(err, io.EOF) { fmt.Println("Stream finished") return } if err != nil { fmt.Printf("Stream error: %v\n", err) return } fmt.Printf("Stream response: %#v\n", response) } } func ExampleClient_CreateTranscription() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateTranscription( context.Background(), openai.AudioRequest{ Model: openai.Whisper1, FilePath: "recording.mp3", }, ) if err != nil { fmt.Printf("Transcription error: %v\n", err) return } fmt.Println(resp.Text) } func ExampleClient_CreateTranscription_captions() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateTranscription( context.Background(), openai.AudioRequest{ Model: openai.Whisper1, FilePath: os.Args[1], Format: openai.AudioResponseFormatSRT, }, ) if err != nil { fmt.Printf("Transcription error: %v\n", err) return } f, err := os.Create(os.Args[1] + ".srt") if err != nil { fmt.Printf("Could not open file: %v\n", err) return } defer f.Close() if _, err = f.WriteString(resp.Text); err != nil { fmt.Printf("Error writing to file: %v\n", err) return } } func ExampleClient_CreateTranslation() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateTranslation( context.Background(), openai.AudioRequest{ Model: openai.Whisper1, FilePath: "recording.mp3", }, ) if err != nil { fmt.Printf("Translation error: %v\n", err) return } fmt.Println(resp.Text) } func ExampleClient_CreateImage() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) respURL, err := client.CreateImage( context.Background(), openai.ImageRequest{ Prompt: "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail", Size: openai.CreateImageSize256x256, ResponseFormat: openai.CreateImageResponseFormatURL, N: 1, }, ) if err != nil { fmt.Printf("Image creation error: %v\n", err) return } fmt.Println(respURL.Data[0].URL) } func ExampleClient_CreateImage_base64() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateImage( context.Background(), openai.ImageRequest{ Prompt: "Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine", Size: openai.CreateImageSize512x512, ResponseFormat: openai.CreateImageResponseFormatB64JSON, N: 1, }, ) if err != nil { fmt.Printf("Image creation error: %v\n", err) return } b, err := base64.StdEncoding.DecodeString(resp.Data[0].B64JSON) if err != nil { fmt.Printf("Base64 decode error: %v\n", err) return } f, err := os.Create("example.png") if err != nil { fmt.Printf("File creation error: %v\n", err) return } defer f.Close() _, err = f.Write(b) if err != nil { fmt.Printf("File write error: %v\n", err) return } fmt.Println("The image was saved as example.png") } func ExampleClientConfig_clientWithProxy() { config := openai.DefaultConfig(os.Getenv("OPENAI_API_KEY")) port := os.Getenv("OPENAI_PROXY_PORT") proxyURL, err := url.Parse(fmt.Sprintf("http://localhost:%s", port)) if err != nil { panic(err) } transport := &http.Transport{ Proxy: http.ProxyURL(proxyURL), } config.HTTPClient = &http.Client{ Transport: transport, } client := openai.NewClientWithConfig(config) client.CreateChatCompletion( //nolint:errcheck // outside of the scope of this example. context.Background(), openai.ChatCompletionRequest{ // etc... }, ) } func Example_chatbot() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) req := openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleSystem, Content: "you are a helpful chatbot", }, }, } fmt.Println("Conversation") fmt.Println("---------------------") fmt.Print("> ") s := bufio.NewScanner(os.Stdin) for s.Scan() { req.Messages = append(req.Messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleUser, Content: s.Text(), }) resp, err := client.CreateChatCompletion(context.Background(), req) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) continue } fmt.Printf("%s\n\n", resp.Choices[0].Message.Content) req.Messages = append(req.Messages, resp.Choices[0].Message) fmt.Print("> ") } } func ExampleDefaultAzureConfig() { azureKey := os.Getenv("AZURE_OPENAI_API_KEY") // Your azure API key azureEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT") // Your azure OpenAI endpoint config := openai.DefaultAzureConfig(azureKey, azureEndpoint) client := openai.NewClientWithConfig(config) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser, Content: "Hello Azure OpenAI!", }, }, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) return } fmt.Println(resp.Choices[0].Message.Content) } // Open-AI maintains clear documentation on how to handle API errors. // // see: https://platform.openai.com/docs/guides/error-codes/api-errors func ExampleAPIError() { var err error // Assume this is the error you are checking. e := &openai.APIError{} if errors.As(err, &e) { switch e.HTTPStatusCode { case 401: // invalid auth or key (do not retry) case 429: // rate limiting or engine overload (wait and retry) case 500: // openai server error (retry) default: // unhandled } } } ================================================ FILE: examples/README.md ================================================ To run an example: ``` export OPENAI_API_KEY="" go run ./example/ ``` ================================================ FILE: examples/chatbot/main.go ================================================ package main import ( "bufio" "context" "fmt" "os" "github.com/sashabaranov/go-openai" ) func main() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) req := openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleSystem, Content: "you are a helpful chatbot", }, }, } fmt.Println("Conversation") fmt.Println("---------------------") fmt.Print("> ") s := bufio.NewScanner(os.Stdin) for s.Scan() { req.Messages = append(req.Messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleUser, Content: s.Text(), }) resp, err := client.CreateChatCompletion(context.Background(), req) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) continue } fmt.Printf("%s\n\n", resp.Choices[0].Message.Content) req.Messages = append(req.Messages, resp.Choices[0].Message) fmt.Print("> ") } } ================================================ FILE: examples/completion/main.go ================================================ package main import ( "context" "fmt" "os" "github.com/sashabaranov/go-openai" ) func main() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateCompletion( context.Background(), openai.CompletionRequest{ Model: openai.GPT3Babbage002, MaxTokens: 5, Prompt: "Lorem ipsum", }, ) if err != nil { fmt.Printf("Completion error: %v\n", err) return } fmt.Println(resp.Choices[0].Text) } ================================================ FILE: examples/completion-with-tool/main.go ================================================ package main import ( "context" "fmt" "os" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/jsonschema" ) func main() { ctx := context.Background() client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) // describe the function & its inputs params := jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "location": { Type: jsonschema.String, Description: "The city and state, e.g. San Francisco, CA", }, "unit": { Type: jsonschema.String, Enum: []string{"celsius", "fahrenheit"}, }, }, Required: []string{"location"}, } f := openai.FunctionDefinition{ Name: "get_current_weather", Description: "Get the current weather in a given location", Parameters: params, } t := openai.Tool{ Type: openai.ToolTypeFunction, Function: &f, } // simulate user asking a question that requires the function dialogue := []openai.ChatCompletionMessage{ {Role: openai.ChatMessageRoleUser, Content: "What is the weather in Boston today?"}, } fmt.Printf("Asking OpenAI '%v' and providing it a '%v()' function...\n", dialogue[0].Content, f.Name) resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ Model: openai.GPT4TurboPreview, Messages: dialogue, Tools: []openai.Tool{t}, }, ) if err != nil || len(resp.Choices) != 1 { fmt.Printf("Completion error: err:%v len(choices):%v\n", err, len(resp.Choices)) return } msg := resp.Choices[0].Message if len(msg.ToolCalls) != 1 { fmt.Printf("Completion error: len(toolcalls): %v\n", len(msg.ToolCalls)) return } // simulate calling the function & responding to OpenAI dialogue = append(dialogue, msg) fmt.Printf("OpenAI called us back wanting to invoke our function '%v' with params '%v'\n", msg.ToolCalls[0].Function.Name, msg.ToolCalls[0].Function.Arguments) dialogue = append(dialogue, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleTool, Content: "Sunny and 80 degrees.", Name: msg.ToolCalls[0].Function.Name, ToolCallID: msg.ToolCalls[0].ID, }) fmt.Printf("Sending OpenAI our '%v()' function's response and requesting the reply to the original question...\n", f.Name) resp, err = client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ Model: openai.GPT4TurboPreview, Messages: dialogue, Tools: []openai.Tool{t}, }, ) if err != nil || len(resp.Choices) != 1 { fmt.Printf("2nd completion error: err:%v len(choices):%v\n", err, len(resp.Choices)) return } // display OpenAI's response to the original question utilizing our function msg = resp.Choices[0].Message fmt.Printf("OpenAI answered the original request with: %v\n", msg.Content) } ================================================ FILE: examples/images/main.go ================================================ package main import ( "context" "fmt" "os" "github.com/sashabaranov/go-openai" ) func main() { client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) respUrl, err := client.CreateImage( context.Background(), openai.ImageRequest{ Prompt: "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail", Size: openai.CreateImageSize256x256, ResponseFormat: openai.CreateImageResponseFormatURL, N: 1, }, ) if err != nil { fmt.Printf("Image creation error: %v\n", err) return } fmt.Println(respUrl.Data[0].URL) } ================================================ FILE: examples/voice-to-text/main.go ================================================ package main import ( "context" "errors" "fmt" "os" "github.com/sashabaranov/go-openai" ) func main() { if len(os.Args) < 2 { fmt.Println("please provide a filename to convert to text") return } if _, err := os.Stat(os.Args[1]); errors.Is(err, os.ErrNotExist) { fmt.Printf("file %s does not exist\n", os.Args[1]) return } client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) resp, err := client.CreateTranscription( context.Background(), openai.AudioRequest{ Model: openai.Whisper1, FilePath: os.Args[1], }, ) if err != nil { fmt.Printf("Transcription error: %v\n", err) return } fmt.Println(resp.Text) } ================================================ FILE: files.go ================================================ package openai import ( "bytes" "context" "fmt" "net/http" "os" ) type FileRequest struct { FileName string `json:"file"` FilePath string `json:"-"` Purpose string `json:"purpose"` } // PurposeType represents the purpose of the file when uploading. type PurposeType string const ( PurposeFineTune PurposeType = "fine-tune" PurposeFineTuneResults PurposeType = "fine-tune-results" PurposeAssistants PurposeType = "assistants" PurposeAssistantsOutput PurposeType = "assistants_output" PurposeBatch PurposeType = "batch" ) // FileBytesRequest represents a file upload request. type FileBytesRequest struct { // the name of the uploaded file in OpenAI Name string // the bytes of the file Bytes []byte // the purpose of the file Purpose PurposeType } // File struct represents an OpenAPI file. type File struct { Bytes int `json:"bytes"` CreatedAt int64 `json:"created_at"` ID string `json:"id"` FileName string `json:"filename"` Object string `json:"object"` Status string `json:"status"` Purpose string `json:"purpose"` StatusDetails string `json:"status_details"` httpHeader } // FilesList is a list of files that belong to the user or organization. type FilesList struct { Files []File `json:"data"` httpHeader } // CreateFileBytes uploads bytes directly to OpenAI without requiring a local file. func (c *Client) CreateFileBytes(ctx context.Context, request FileBytesRequest) (file File, err error) { var b bytes.Buffer reader := bytes.NewReader(request.Bytes) builder := c.createFormBuilder(&b) err = builder.WriteField("purpose", string(request.Purpose)) if err != nil { return } err = builder.CreateFormFileReader("file", reader, request.Name) if err != nil { return } err = builder.Close() if err != nil { return } req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/files"), withBody(&b), withContentType(builder.FormDataContentType())) if err != nil { return } err = c.sendRequest(req, &file) return } // CreateFile uploads a jsonl file to GPT3 // FilePath must be a local file path. func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File, err error) { var b bytes.Buffer builder := c.createFormBuilder(&b) err = builder.WriteField("purpose", request.Purpose) if err != nil { return } fileData, err := os.Open(request.FilePath) if err != nil { return } defer fileData.Close() err = builder.CreateFormFile("file", fileData) if err != nil { return } err = builder.Close() if err != nil { return } req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/files"), withBody(&b), withContentType(builder.FormDataContentType())) if err != nil { return } err = c.sendRequest(req, &file) return } // DeleteFile deletes an existing file. func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) { req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/files/"+fileID)) if err != nil { return } err = c.sendRequest(req, nil) return } // ListFiles Lists the currently available files, // and provides basic information about each file such as the file name and purpose. func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) { req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/files")) if err != nil { return } err = c.sendRequest(req, &files) return } // GetFile Retrieves a file instance, providing basic information about the file // such as the file name and purpose. func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) { urlSuffix := fmt.Sprintf("/files/%s", fileID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &file) return } func (c *Client) GetFileContent(ctx context.Context, fileID string) (content RawResponse, err error) { urlSuffix := fmt.Sprintf("/files/%s/content", fileID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } return c.sendRequestRaw(req) } ================================================ FILE: files_api_test.go ================================================ package openai_test import ( "context" "encoding/json" "errors" "fmt" "io" "net/http" "os" "strconv" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestFileBytesUpload(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files", handleCreateFile) req := openai.FileBytesRequest{ Name: "foo", Bytes: []byte("foo"), Purpose: openai.PurposeFineTune, } _, err := client.CreateFileBytes(context.Background(), req) checks.NoError(t, err, "CreateFile error") } func TestFileUpload(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files", handleCreateFile) req := openai.FileRequest{ FileName: "test.go", FilePath: "client.go", Purpose: "fine-tune", } _, err := client.CreateFile(context.Background(), req) checks.NoError(t, err, "CreateFile error") } // handleCreateFile Handles the images endpoint by the test server. func handleCreateFile(w http.ResponseWriter, r *http.Request) { var err error var resBytes []byte // edits only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } err = r.ParseMultipartForm(1024 * 1024 * 1024) if err != nil { http.Error(w, "file is more than 1GB", http.StatusInternalServerError) return } values := r.Form var purpose string for key, value := range values { if key == "purpose" { purpose = value[0] } } file, header, err := r.FormFile("file") if err != nil { return } defer file.Close() fileReq := openai.File{ Bytes: int(header.Size), ID: strconv.Itoa(int(time.Now().Unix())), FileName: header.Filename, Purpose: purpose, CreatedAt: time.Now().Unix(), Object: "test-objecct", } resBytes, _ = json.Marshal(fileReq) fmt.Fprint(w, string(resBytes)) } func TestDeleteFile(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files/deadbeef", func(http.ResponseWriter, *http.Request) {}) err := client.DeleteFile(context.Background(), "deadbeef") checks.NoError(t, err, "DeleteFile error") } func TestListFile(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.FilesList{}) fmt.Fprintln(w, string(resBytes)) }) _, err := client.ListFiles(context.Background()) checks.NoError(t, err, "ListFiles error") } func TestGetFile(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files/deadbeef", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.File{}) fmt.Fprintln(w, string(resBytes)) }) _, err := client.GetFile(context.Background(), "deadbeef") checks.NoError(t, err, "GetFile error") } func TestGetFileContent(t *testing.T) { wantRespJsonl := `{"prompt": "foo", "completion": "foo"} {"prompt": "bar", "completion": "bar"} {"prompt": "baz", "completion": "baz"} ` client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files/deadbeef/content", func(w http.ResponseWriter, r *http.Request) { // edits only accepts GET requests if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } fmt.Fprint(w, wantRespJsonl) }) content, err := client.GetFileContent(context.Background(), "deadbeef") checks.NoError(t, err, "GetFileContent error") defer content.Close() actual, _ := io.ReadAll(content) if string(actual) != wantRespJsonl { t.Errorf("Expected %s, got %s", wantRespJsonl, string(actual)) } } func TestGetFileContentReturnError(t *testing.T) { wantMessage := "To help mitigate abuse, downloading of fine-tune training files is disabled for free accounts." wantType := "invalid_request_error" wantErrorResp := `{ "error": { "message": "` + wantMessage + `", "type": "` + wantType + `", "param": null, "code": null } }` client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files/deadbeef/content", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusBadRequest) fmt.Fprint(w, wantErrorResp) }) _, err := client.GetFileContent(context.Background(), "deadbeef") if err == nil { t.Fatal("Did not return error") } apiErr := &openai.APIError{} if !errors.As(err, &apiErr) { t.Fatalf("Did not return APIError: %+v\n", apiErr) } if apiErr.Message != wantMessage { t.Fatalf("Expected %s Message, got = %s\n", wantMessage, apiErr.Message) return } if apiErr.Type != wantType { t.Fatalf("Expected %s Type, got = %s\n", wantType, apiErr.Type) return } } func TestGetFileContentReturnTimeoutError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/files/deadbeef/content", func(http.ResponseWriter, *http.Request) { time.Sleep(10 * time.Nanosecond) }) ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, time.Nanosecond) defer cancel() _, err := client.GetFileContent(ctx, "deadbeef") if err == nil { t.Fatal("Did not return error") } if !os.IsTimeout(err) { t.Fatal("Did not return timeout error") } } ================================================ FILE: files_test.go ================================================ package openai //nolint:testpackage // testing private field import ( "context" "fmt" "io" "os" "testing" utils "github.com/sashabaranov/go-openai/internal" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestFileBytesUploadWithFailingFormBuilder(t *testing.T) { config := DefaultConfig("") config.BaseURL = "" client := NewClientWithConfig(config) mockBuilder := &mockFormBuilder{} client.createFormBuilder = func(io.Writer) utils.FormBuilder { return mockBuilder } ctx := context.Background() req := FileBytesRequest{ Name: "foo", Bytes: []byte("foo"), Purpose: PurposeAssistants, } mockError := fmt.Errorf("mockWriteField error") mockBuilder.mockWriteField = func(string, string) error { return mockError } _, err := client.CreateFileBytes(ctx, req) checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails") mockError = fmt.Errorf("mockCreateFormFile error") mockBuilder.mockWriteField = func(string, string) error { return nil } mockBuilder.mockCreateFormFileReader = func(string, io.Reader, string) error { return mockError } _, err = client.CreateFileBytes(ctx, req) checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails") mockError = fmt.Errorf("mockClose error") mockBuilder.mockWriteField = func(string, string) error { return nil } mockBuilder.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } mockBuilder.mockClose = func() error { return mockError } _, err = client.CreateFileBytes(ctx, req) checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails") } func TestFileUploadWithFailingFormBuilder(t *testing.T) { config := DefaultConfig("") config.BaseURL = "" client := NewClientWithConfig(config) mockBuilder := &mockFormBuilder{} client.createFormBuilder = func(io.Writer) utils.FormBuilder { return mockBuilder } ctx := context.Background() req := FileRequest{ FileName: "test.go", FilePath: "client.go", Purpose: "fine-tune", } mockError := fmt.Errorf("mockWriteField error") mockBuilder.mockWriteField = func(string, string) error { return mockError } _, err := client.CreateFile(ctx, req) checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails") mockError = fmt.Errorf("mockCreateFormFile error") mockBuilder.mockWriteField = func(string, string) error { return nil } mockBuilder.mockCreateFormFile = func(string, *os.File) error { return mockError } _, err = client.CreateFile(ctx, req) checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails") mockError = fmt.Errorf("mockClose error") mockBuilder.mockWriteField = func(string, string) error { return nil } mockBuilder.mockCreateFormFile = func(string, *os.File) error { return nil } mockBuilder.mockClose = func() error { return mockError } _, err = client.CreateFile(ctx, req) if err == nil { t.Fatal("CreateFile should return error if form builder fails") } checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails") } func TestFileUploadWithNonExistentPath(t *testing.T) { config := DefaultConfig("") config.BaseURL = "" client := NewClientWithConfig(config) ctx := context.Background() req := FileRequest{ FilePath: "some non existent file path/F616FD18-589E-44A8-BF0C-891EAE69C455", } _, err := client.CreateFile(ctx, req) checks.ErrorIs(t, err, os.ErrNotExist, "CreateFile should return error if file does not exist") } func TestCreateFileRequestBuilderFailure(t *testing.T) { config := DefaultConfig("") config.BaseURL = "" client := NewClientWithConfig(config) client.requestBuilder = &failingRequestBuilder{} client.createFormBuilder = func(io.Writer) utils.FormBuilder { return &mockFormBuilder{ mockWriteField: func(string, string) error { return nil }, mockCreateFormFile: func(string, *os.File) error { return nil }, mockClose: func() error { return nil }, } } _, err := client.CreateFile(context.Background(), FileRequest{FilePath: "client.go"}) checks.ErrorIs(t, err, errTestRequestBuilderFailed, "CreateFile should return error if request builder fails") } ================================================ FILE: fine_tunes.go ================================================ package openai import ( "context" "fmt" "net/http" ) // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. type FineTuneRequest struct { TrainingFile string `json:"training_file"` ValidationFile string `json:"validation_file,omitempty"` Model string `json:"model,omitempty"` Epochs int `json:"n_epochs,omitempty"` BatchSize int `json:"batch_size,omitempty"` LearningRateMultiplier float32 `json:"learning_rate_multiplier,omitempty"` PromptLossRate float32 `json:"prompt_loss_rate,omitempty"` ComputeClassificationMetrics bool `json:"compute_classification_metrics,omitempty"` ClassificationClasses int `json:"classification_n_classes,omitempty"` ClassificationPositiveClass string `json:"classification_positive_class,omitempty"` ClassificationBetas []float32 `json:"classification_betas,omitempty"` Suffix string `json:"suffix,omitempty"` } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. type FineTune struct { ID string `json:"id"` Object string `json:"object"` Model string `json:"model"` CreatedAt int64 `json:"created_at"` FineTuneEventList []FineTuneEvent `json:"events,omitempty"` FineTunedModel string `json:"fine_tuned_model"` HyperParams FineTuneHyperParams `json:"hyperparams"` OrganizationID string `json:"organization_id"` ResultFiles []File `json:"result_files"` Status string `json:"status"` ValidationFiles []File `json:"validation_files"` TrainingFiles []File `json:"training_files"` UpdatedAt int64 `json:"updated_at"` httpHeader } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. type FineTuneEvent struct { Object string `json:"object"` CreatedAt int64 `json:"created_at"` Level string `json:"level"` Message string `json:"message"` } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. type FineTuneHyperParams struct { BatchSize int `json:"batch_size"` LearningRateMultiplier float64 `json:"learning_rate_multiplier"` Epochs int `json:"n_epochs"` PromptLossWeight float64 `json:"prompt_loss_weight"` } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. type FineTuneList struct { Object string `json:"object"` Data []FineTune `json:"data"` httpHeader } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. type FineTuneEventList struct { Object string `json:"object"` Data []FineTuneEvent `json:"data"` httpHeader } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. type FineTuneDeleteResponse struct { ID string `json:"id"` Object string `json:"object"` Deleted bool `json:"deleted"` httpHeader } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (response FineTune, err error) { urlSuffix := "/fine-tunes" req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request)) if err != nil { return } err = c.sendRequest(req, &response) return } // CancelFineTune cancel a fine-tune job. // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) { req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel")) //nolint:lll //this method is deprecated if err != nil { return } err = c.sendRequest(req, &response) return } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. func (c *Client) ListFineTunes(ctx context.Context) (response FineTuneList, err error) { req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/fine-tunes")) if err != nil { return } err = c.sendRequest(req, &response) return } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. func (c *Client) GetFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) { urlSuffix := fmt.Sprintf("/fine-tunes/%s", fineTuneID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &response) return } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. func (c *Client) DeleteFineTune(ctx context.Context, fineTuneID string) (response FineTuneDeleteResponse, err error) { req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/fine-tunes/"+fineTuneID)) if err != nil { return } err = c.sendRequest(req, &response) return } // Deprecated: On August 22nd, 2023, OpenAI announced the deprecation of the /v1/fine-tunes API. // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. func (c *Client) ListFineTuneEvents(ctx context.Context, fineTuneID string) (response FineTuneEventList, err error) { req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/fine-tunes/"+fineTuneID+"/events")) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: fine_tunes_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "net/http" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) const testFineTuneID = "fine-tune-id" // TestFineTunes Tests the fine tunes endpoint of the API using the mocked server. func TestFineTunes(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler( "/v1/fine-tunes", func(w http.ResponseWriter, r *http.Request) { var resBytes []byte if r.Method == http.MethodGet { resBytes, _ = json.Marshal(openai.FineTuneList{}) } else { resBytes, _ = json.Marshal(openai.FineTune{}) } fmt.Fprintln(w, string(resBytes)) }, ) server.RegisterHandler( "/v1/fine-tunes/"+testFineTuneID+"/cancel", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.FineTune{}) fmt.Fprintln(w, string(resBytes)) }, ) server.RegisterHandler( "/v1/fine-tunes/"+testFineTuneID, func(w http.ResponseWriter, r *http.Request) { var resBytes []byte if r.Method == http.MethodDelete { resBytes, _ = json.Marshal(openai.FineTuneDeleteResponse{}) } else { resBytes, _ = json.Marshal(openai.FineTune{}) } fmt.Fprintln(w, string(resBytes)) }, ) server.RegisterHandler( "/v1/fine-tunes/"+testFineTuneID+"/events", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.FineTuneEventList{}) fmt.Fprintln(w, string(resBytes)) }, ) ctx := context.Background() _, err := client.ListFineTunes(ctx) checks.NoError(t, err, "ListFineTunes error") _, err = client.CreateFineTune(ctx, openai.FineTuneRequest{}) checks.NoError(t, err, "CreateFineTune error") _, err = client.CancelFineTune(ctx, testFineTuneID) checks.NoError(t, err, "CancelFineTune error") _, err = client.GetFineTune(ctx, testFineTuneID) checks.NoError(t, err, "GetFineTune error") _, err = client.DeleteFineTune(ctx, testFineTuneID) checks.NoError(t, err, "DeleteFineTune error") _, err = client.ListFineTuneEvents(ctx, testFineTuneID) checks.NoError(t, err, "ListFineTuneEvents error") } ================================================ FILE: fine_tuning_job.go ================================================ package openai import ( "context" "fmt" "net/http" "net/url" ) type FineTuningJob struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` FinishedAt int64 `json:"finished_at"` Model string `json:"model"` FineTunedModel string `json:"fine_tuned_model,omitempty"` OrganizationID string `json:"organization_id"` Status string `json:"status"` Hyperparameters Hyperparameters `json:"hyperparameters"` TrainingFile string `json:"training_file"` ValidationFile string `json:"validation_file,omitempty"` ResultFiles []string `json:"result_files"` TrainedTokens int `json:"trained_tokens"` httpHeader } type Hyperparameters struct { Epochs any `json:"n_epochs,omitempty"` LearningRateMultiplier any `json:"learning_rate_multiplier,omitempty"` BatchSize any `json:"batch_size,omitempty"` } type FineTuningJobRequest struct { TrainingFile string `json:"training_file"` ValidationFile string `json:"validation_file,omitempty"` Model string `json:"model,omitempty"` Hyperparameters *Hyperparameters `json:"hyperparameters,omitempty"` Suffix string `json:"suffix,omitempty"` } type FineTuningJobEventList struct { Object string `json:"object"` Data []FineTuneEvent `json:"data"` HasMore bool `json:"has_more"` httpHeader } type FineTuningJobEvent struct { Object string `json:"object"` ID string `json:"id"` CreatedAt int `json:"created_at"` Level string `json:"level"` Message string `json:"message"` Data any `json:"data"` Type string `json:"type"` } // CreateFineTuningJob create a fine tuning job. func (c *Client) CreateFineTuningJob( ctx context.Context, request FineTuningJobRequest, ) (response FineTuningJob, err error) { urlSuffix := "/fine_tuning/jobs" req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request)) if err != nil { return } err = c.sendRequest(req, &response) return } // CancelFineTuningJob cancel a fine tuning job. func (c *Client) CancelFineTuningJob(ctx context.Context, fineTuningJobID string) (response FineTuningJob, err error) { req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine_tuning/jobs/"+fineTuningJobID+"/cancel")) if err != nil { return } err = c.sendRequest(req, &response) return } // RetrieveFineTuningJob retrieve a fine tuning job. func (c *Client) RetrieveFineTuningJob( ctx context.Context, fineTuningJobID string, ) (response FineTuningJob, err error) { urlSuffix := fmt.Sprintf("/fine_tuning/jobs/%s", fineTuningJobID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &response) return } type listFineTuningJobEventsParameters struct { after *string limit *int } type ListFineTuningJobEventsParameter func(*listFineTuningJobEventsParameters) func ListFineTuningJobEventsWithAfter(after string) ListFineTuningJobEventsParameter { return func(args *listFineTuningJobEventsParameters) { args.after = &after } } func ListFineTuningJobEventsWithLimit(limit int) ListFineTuningJobEventsParameter { return func(args *listFineTuningJobEventsParameters) { args.limit = &limit } } // ListFineTuningJobs list fine tuning jobs events. func (c *Client) ListFineTuningJobEvents( ctx context.Context, fineTuningJobID string, setters ...ListFineTuningJobEventsParameter, ) (response FineTuningJobEventList, err error) { parameters := &listFineTuningJobEventsParameters{ after: nil, limit: nil, } for _, setter := range setters { setter(parameters) } urlValues := url.Values{} if parameters.after != nil { urlValues.Add("after", *parameters.after) } if parameters.limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *parameters.limit)) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } req, err := c.newRequest( ctx, http.MethodGet, c.fullURL("/fine_tuning/jobs/"+fineTuningJobID+"/events"+encodedValues), ) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: fine_tuning_job_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "net/http" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) const testFineTuninigJobID = "fine-tuning-job-id" // TestFineTuningJob Tests the fine tuning job endpoint of the API using the mocked server. func TestFineTuningJob(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler( "/v1/fine_tuning/jobs", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.FineTuningJob{ Object: "fine_tuning.job", ID: testFineTuninigJobID, Model: "davinci-002", CreatedAt: 1692661014, FinishedAt: 1692661190, FineTunedModel: "ft:davinci-002:my-org:custom_suffix:7q8mpxmy", OrganizationID: "org-123", ResultFiles: []string{"file-abc123"}, Status: "succeeded", ValidationFile: "", TrainingFile: "file-abc123", Hyperparameters: openai.Hyperparameters{ Epochs: "auto", LearningRateMultiplier: "auto", BatchSize: "auto", }, TrainedTokens: 5768, }) fmt.Fprintln(w, string(resBytes)) }, ) server.RegisterHandler( "/v1/fine_tuning/jobs/"+testFineTuninigJobID+"/cancel", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.FineTuningJob{}) fmt.Fprintln(w, string(resBytes)) }, ) server.RegisterHandler( "/v1/fine_tuning/jobs/"+testFineTuninigJobID, func(w http.ResponseWriter, _ *http.Request) { var resBytes []byte resBytes, _ = json.Marshal(openai.FineTuningJob{}) fmt.Fprintln(w, string(resBytes)) }, ) server.RegisterHandler( "/v1/fine_tuning/jobs/"+testFineTuninigJobID+"/events", func(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.FineTuningJobEventList{}) fmt.Fprintln(w, string(resBytes)) }, ) ctx := context.Background() _, err := client.CreateFineTuningJob(ctx, openai.FineTuningJobRequest{}) checks.NoError(t, err, "CreateFineTuningJob error") _, err = client.CancelFineTuningJob(ctx, testFineTuninigJobID) checks.NoError(t, err, "CancelFineTuningJob error") _, err = client.RetrieveFineTuningJob(ctx, testFineTuninigJobID) checks.NoError(t, err, "RetrieveFineTuningJob error") _, err = client.ListFineTuningJobEvents(ctx, testFineTuninigJobID) checks.NoError(t, err, "ListFineTuningJobEvents error") _, err = client.ListFineTuningJobEvents( ctx, testFineTuninigJobID, openai.ListFineTuningJobEventsWithAfter("last-event-id"), ) checks.NoError(t, err, "ListFineTuningJobEvents error") _, err = client.ListFineTuningJobEvents( ctx, testFineTuninigJobID, openai.ListFineTuningJobEventsWithLimit(10), ) checks.NoError(t, err, "ListFineTuningJobEvents error") _, err = client.ListFineTuningJobEvents( ctx, testFineTuninigJobID, openai.ListFineTuningJobEventsWithAfter("last-event-id"), openai.ListFineTuningJobEventsWithLimit(10), ) checks.NoError(t, err, "ListFineTuningJobEvents error") } ================================================ FILE: go.mod ================================================ module github.com/sashabaranov/go-openai go 1.18 ================================================ FILE: image.go ================================================ package openai import ( "bytes" "context" "io" "net/http" "strconv" ) // Image sizes defined by the OpenAI API. const ( CreateImageSize256x256 = "256x256" CreateImageSize512x512 = "512x512" CreateImageSize1024x1024 = "1024x1024" // dall-e-3 supported only. CreateImageSize1792x1024 = "1792x1024" CreateImageSize1024x1792 = "1024x1792" // gpt-image-1 supported only. CreateImageSize1536x1024 = "1536x1024" // Landscape CreateImageSize1024x1536 = "1024x1536" // Portrait ) const ( // dall-e-2 and dall-e-3 only. CreateImageResponseFormatB64JSON = "b64_json" CreateImageResponseFormatURL = "url" ) const ( CreateImageModelDallE2 = "dall-e-2" CreateImageModelDallE3 = "dall-e-3" CreateImageModelGptImage1 = "gpt-image-1" ) const ( CreateImageQualityHD = "hd" CreateImageQualityStandard = "standard" // gpt-image-1 only. CreateImageQualityHigh = "high" CreateImageQualityMedium = "medium" CreateImageQualityLow = "low" ) const ( // dall-e-3 only. CreateImageStyleVivid = "vivid" CreateImageStyleNatural = "natural" ) const ( // gpt-image-1 only. CreateImageBackgroundTransparent = "transparent" CreateImageBackgroundOpaque = "opaque" ) const ( // gpt-image-1 only. CreateImageModerationLow = "low" ) const ( // gpt-image-1 only. CreateImageOutputFormatPNG = "png" CreateImageOutputFormatJPEG = "jpeg" CreateImageOutputFormatWEBP = "webp" ) // ImageRequest represents the request structure for the image API. type ImageRequest struct { Prompt string `json:"prompt,omitempty"` Model string `json:"model,omitempty"` N int `json:"n,omitempty"` Quality string `json:"quality,omitempty"` Size string `json:"size,omitempty"` Style string `json:"style,omitempty"` ResponseFormat string `json:"response_format,omitempty"` User string `json:"user,omitempty"` Background string `json:"background,omitempty"` Moderation string `json:"moderation,omitempty"` OutputCompression int `json:"output_compression,omitempty"` OutputFormat string `json:"output_format,omitempty"` } // ImageResponse represents a response structure for image API. type ImageResponse struct { Created int64 `json:"created,omitempty"` Data []ImageResponseDataInner `json:"data,omitempty"` Usage ImageResponseUsage `json:"usage,omitempty"` httpHeader } // ImageResponseInputTokensDetails represents the token breakdown for input tokens. type ImageResponseInputTokensDetails struct { TextTokens int `json:"text_tokens,omitempty"` ImageTokens int `json:"image_tokens,omitempty"` } // ImageResponseUsage represents the token usage information for image API. type ImageResponseUsage struct { TotalTokens int `json:"total_tokens,omitempty"` InputTokens int `json:"input_tokens,omitempty"` OutputTokens int `json:"output_tokens,omitempty"` InputTokensDetails ImageResponseInputTokensDetails `json:"input_tokens_details,omitempty"` } // ImageResponseDataInner represents a response data structure for image API. type ImageResponseDataInner struct { URL string `json:"url,omitempty"` B64JSON string `json:"b64_json,omitempty"` RevisedPrompt string `json:"revised_prompt,omitempty"` } // CreateImage - API call to create an image. This is the main endpoint of the DALL-E API. func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error) { urlSuffix := "/images/generations" req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix, withModel(request.Model)), withBody(request), ) if err != nil { return } err = c.sendRequest(req, &response) return } // WrapReader wraps an io.Reader with filename and Content-type. func WrapReader(rdr io.Reader, filename string, contentType string) io.Reader { return file{rdr, filename, contentType} } type file struct { io.Reader name string contentType string } func (f file) Name() string { if f.name != "" { return f.name } else if named, ok := f.Reader.(interface{ Name() string }); ok { return named.Name() } return "" } func (f file) ContentType() string { return f.contentType } // ImageEditRequest represents the request structure for the image API. // Use WrapReader to wrap an io.Reader with filename and Content-type. type ImageEditRequest struct { Image io.Reader `json:"image,omitempty"` Mask io.Reader `json:"mask,omitempty"` Prompt string `json:"prompt,omitempty"` Model string `json:"model,omitempty"` N int `json:"n,omitempty"` Size string `json:"size,omitempty"` ResponseFormat string `json:"response_format,omitempty"` Quality string `json:"quality,omitempty"` User string `json:"user,omitempty"` } // CreateEditImage - API call to create an image. This is the main endpoint of the DALL-E API. func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error) { body := &bytes.Buffer{} builder := c.createFormBuilder(body) // image, filename verification can be postponed err = builder.CreateFormFileReader("image", request.Image, "") if err != nil { return } // mask, it is optional if request.Mask != nil { // filename verification can be postponed err = builder.CreateFormFileReader("mask", request.Mask, "") if err != nil { return } } err = builder.WriteField("prompt", request.Prompt) if err != nil { return } err = builder.WriteField("n", strconv.Itoa(request.N)) if err != nil { return } err = builder.WriteField("size", request.Size) if err != nil { return } err = builder.WriteField("response_format", request.ResponseFormat) if err != nil { return } err = builder.Close() if err != nil { return } req, err := c.newRequest( ctx, http.MethodPost, c.fullURL("/images/edits", withModel(request.Model)), withBody(body), withContentType(builder.FormDataContentType()), ) if err != nil { return } err = c.sendRequest(req, &response) return } // ImageVariRequest represents the request structure for the image API. // Use WrapReader to wrap an io.Reader with filename and Content-type. type ImageVariRequest struct { Image io.Reader `json:"image,omitempty"` Model string `json:"model,omitempty"` N int `json:"n,omitempty"` Size string `json:"size,omitempty"` ResponseFormat string `json:"response_format,omitempty"` User string `json:"user,omitempty"` } // CreateVariImage - API call to create an image variation. This is the main endpoint of the DALL-E API. // Use abbreviations(vari for variation) because ci-lint has a single-line length limit ... func (c *Client) CreateVariImage(ctx context.Context, request ImageVariRequest) (response ImageResponse, err error) { body := &bytes.Buffer{} builder := c.createFormBuilder(body) // image, filename verification can be postponed err = builder.CreateFormFileReader("image", request.Image, "") if err != nil { return } err = builder.WriteField("n", strconv.Itoa(request.N)) if err != nil { return } err = builder.WriteField("size", request.Size) if err != nil { return } err = builder.WriteField("response_format", request.ResponseFormat) if err != nil { return } err = builder.Close() if err != nil { return } req, err := c.newRequest( ctx, http.MethodPost, c.fullURL("/images/variations", withModel(request.Model)), withBody(body), withContentType(builder.FormDataContentType()), ) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: image_api_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "io" "net/http" "os" "path/filepath" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestImages(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/images/generations", handleImageEndpoint) _, err := client.CreateImage(context.Background(), openai.ImageRequest{ Prompt: "Lorem ipsum", Model: openai.CreateImageModelDallE3, N: 1, Quality: openai.CreateImageQualityHD, Size: openai.CreateImageSize1024x1024, Style: openai.CreateImageStyleVivid, ResponseFormat: openai.CreateImageResponseFormatURL, User: "user", }) checks.NoError(t, err, "CreateImage error") } // handleImageEndpoint Handles the images endpoint by the test server. func handleImageEndpoint(w http.ResponseWriter, r *http.Request) { var err error var resBytes []byte // images only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } var imageReq openai.ImageRequest if imageReq, err = getImageBody(r); err != nil { http.Error(w, "could not read request", http.StatusInternalServerError) return } res := openai.ImageResponse{ Created: time.Now().Unix(), } for i := 0; i < imageReq.N; i++ { imageData := openai.ImageResponseDataInner{} switch imageReq.ResponseFormat { case openai.CreateImageResponseFormatURL, "": imageData.URL = "https://example.com/image.png" case openai.CreateImageResponseFormatB64JSON: // This decodes to "{}" in base64. imageData.B64JSON = "e30K" default: http.Error(w, "invalid response format", http.StatusBadRequest) return } res.Data = append(res.Data, imageData) } resBytes, _ = json.Marshal(res) fmt.Fprintln(w, string(resBytes)) } // getImageBody Returns the body of the request to create a image. func getImageBody(r *http.Request) (openai.ImageRequest, error) { image := openai.ImageRequest{} // read the request body reqBody, err := io.ReadAll(r.Body) if err != nil { return openai.ImageRequest{}, err } err = json.Unmarshal(reqBody, &image) if err != nil { return openai.ImageRequest{}, err } return image, nil } func TestImageEdit(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/images/edits", handleEditImageEndpoint) origin, err := os.Create(filepath.Join(t.TempDir(), "image.png")) if err != nil { t.Fatalf("open origin file error: %v", err) } defer origin.Close() mask, err := os.Create(filepath.Join(t.TempDir(), "mask.png")) if err != nil { t.Fatalf("open mask file error: %v", err) } defer mask.Close() _, err = client.CreateEditImage(context.Background(), openai.ImageEditRequest{ Image: origin, Mask: mask, Prompt: "There is a turtle in the pool", N: 3, Size: openai.CreateImageSize1024x1024, ResponseFormat: openai.CreateImageResponseFormatURL, }) checks.NoError(t, err, "CreateImage error") } func TestImageEditWithoutMask(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/images/edits", handleEditImageEndpoint) origin, err := os.Create(filepath.Join(t.TempDir(), "image.png")) if err != nil { t.Fatalf("open origin file error: %v", err) } defer origin.Close() _, err = client.CreateEditImage(context.Background(), openai.ImageEditRequest{ Image: origin, Prompt: "There is a turtle in the pool", N: 3, Size: openai.CreateImageSize1024x1024, ResponseFormat: openai.CreateImageResponseFormatURL, }) checks.NoError(t, err, "CreateImage error") } // handleEditImageEndpoint Handles the images endpoint by the test server. func handleEditImageEndpoint(w http.ResponseWriter, r *http.Request) { var resBytes []byte // images only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } responses := openai.ImageResponse{ Created: time.Now().Unix(), Data: []openai.ImageResponseDataInner{ { URL: "test-url1", B64JSON: "", }, { URL: "test-url2", B64JSON: "", }, { URL: "test-url3", B64JSON: "", }, }, } resBytes, _ = json.Marshal(responses) fmt.Fprintln(w, string(resBytes)) } func TestImageVariation(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/images/variations", handleVariateImageEndpoint) origin, err := os.Create(filepath.Join(t.TempDir(), "image.png")) if err != nil { t.Fatalf("open origin file error: %v", err) } defer origin.Close() _, err = client.CreateVariImage(context.Background(), openai.ImageVariRequest{ Image: origin, N: 3, Size: openai.CreateImageSize1024x1024, ResponseFormat: openai.CreateImageResponseFormatURL, }) checks.NoError(t, err, "CreateImage error") } // handleVariateImageEndpoint Handles the images endpoint by the test server. func handleVariateImageEndpoint(w http.ResponseWriter, r *http.Request) { var resBytes []byte // images only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } responses := openai.ImageResponse{ Created: time.Now().Unix(), Data: []openai.ImageResponseDataInner{ { URL: "test-url1", B64JSON: "", }, { URL: "test-url2", B64JSON: "", }, { URL: "test-url3", B64JSON: "", }, }, } resBytes, _ = json.Marshal(responses) fmt.Fprintln(w, string(resBytes)) } ================================================ FILE: image_test.go ================================================ package openai //nolint:testpackage // testing private field import ( utils "github.com/sashabaranov/go-openai/internal" "github.com/sashabaranov/go-openai/internal/test/checks" "bytes" "context" "fmt" "io" "os" "testing" ) type mockFormBuilder struct { mockCreateFormFile func(string, *os.File) error mockCreateFormFileReader func(string, io.Reader, string) error mockWriteField func(string, string) error mockClose func() error } func (fb *mockFormBuilder) CreateFormFile(fieldname string, file *os.File) error { return fb.mockCreateFormFile(fieldname, file) } func (fb *mockFormBuilder) CreateFormFileReader(fieldname string, r io.Reader, filename string) error { return fb.mockCreateFormFileReader(fieldname, r, filename) } func (fb *mockFormBuilder) WriteField(fieldname, value string) error { return fb.mockWriteField(fieldname, value) } func (fb *mockFormBuilder) Close() error { return fb.mockClose() } func (fb *mockFormBuilder) FormDataContentType() string { return "" } func TestImageFormBuilderFailures(t *testing.T) { ctx := context.Background() mockFailedErr := fmt.Errorf("mock form builder fail") newClient := func(fb *mockFormBuilder) *Client { cfg := DefaultConfig("") cfg.BaseURL = "" c := NewClientWithConfig(cfg) c.createFormBuilder = func(io.Writer) utils.FormBuilder { return fb } return c } tests := []struct { name string setup func(*mockFormBuilder) req ImageEditRequest }{ { name: "image", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return mockFailedErr } fb.mockWriteField = func(string, string) error { return nil } fb.mockClose = func() error { return nil } }, req: ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}, }, { name: "mask", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(name string, _ io.Reader, _ string) error { if name == "mask" { return mockFailedErr } return nil } fb.mockWriteField = func(string, string) error { return nil } fb.mockClose = func() error { return nil } }, req: ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}, }, { name: "prompt", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(field, _ string) error { if field == "prompt" { return mockFailedErr } return nil } fb.mockClose = func() error { return nil } }, req: ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}, }, { name: "n", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(field, _ string) error { if field == "n" { return mockFailedErr } return nil } fb.mockClose = func() error { return nil } }, req: ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}, }, { name: "size", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(field, _ string) error { if field == "size" { return mockFailedErr } return nil } fb.mockClose = func() error { return nil } }, req: ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}, }, { name: "response_format", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(field, _ string) error { if field == "response_format" { return mockFailedErr } return nil } fb.mockClose = func() error { return nil } }, req: ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}, }, { name: "close", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(string, string) error { return nil } fb.mockClose = func() error { return mockFailedErr } }, req: ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { fb := &mockFormBuilder{} tc.setup(fb) client := newClient(fb) _, err := client.CreateEditImage(ctx, tc.req) checks.ErrorIs(t, err, mockFailedErr, "CreateEditImage should return error if form builder fails") }) } t.Run("new request", func(t *testing.T) { fb := &mockFormBuilder{ mockCreateFormFileReader: func(string, io.Reader, string) error { return nil }, mockWriteField: func(string, string) error { return nil }, mockClose: func() error { return nil }, } client := newClient(fb) client.requestBuilder = &failingRequestBuilder{} _, err := client.CreateEditImage(ctx, ImageEditRequest{Image: bytes.NewBuffer(nil), Mask: bytes.NewBuffer(nil)}) checks.ErrorIs(t, err, errTestRequestBuilderFailed, "CreateEditImage should return error if request builder fails") }) } func TestVariImageFormBuilderFailures(t *testing.T) { ctx := context.Background() mockFailedErr := fmt.Errorf("mock form builder fail") newClient := func(fb *mockFormBuilder) *Client { cfg := DefaultConfig("") cfg.BaseURL = "" c := NewClientWithConfig(cfg) c.createFormBuilder = func(io.Writer) utils.FormBuilder { return fb } return c } tests := []struct { name string setup func(*mockFormBuilder) req ImageVariRequest }{ { name: "image", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return mockFailedErr } fb.mockWriteField = func(string, string) error { return nil } fb.mockClose = func() error { return nil } }, req: ImageVariRequest{Image: bytes.NewBuffer(nil)}, }, { name: "n", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(field string, _ string) error { if field == "n" { return mockFailedErr } return nil } fb.mockClose = func() error { return nil } }, req: ImageVariRequest{Image: bytes.NewBuffer(nil)}, }, { name: "size", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(field string, _ string) error { if field == "size" { return mockFailedErr } return nil } fb.mockClose = func() error { return nil } }, req: ImageVariRequest{Image: bytes.NewBuffer(nil)}, }, { name: "response_format", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(field string, _ string) error { if field == "response_format" { return mockFailedErr } return nil } fb.mockClose = func() error { return nil } }, req: ImageVariRequest{Image: bytes.NewBuffer(nil)}, }, { name: "close", setup: func(fb *mockFormBuilder) { fb.mockCreateFormFileReader = func(string, io.Reader, string) error { return nil } fb.mockWriteField = func(string, string) error { return nil } fb.mockClose = func() error { return mockFailedErr } }, req: ImageVariRequest{Image: bytes.NewBuffer(nil)}, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { fb := &mockFormBuilder{} tc.setup(fb) client := newClient(fb) _, err := client.CreateVariImage(ctx, tc.req) checks.ErrorIs(t, err, mockFailedErr, "CreateVariImage should return error if form builder fails") }) } t.Run("new request", func(t *testing.T) { fb := &mockFormBuilder{ mockCreateFormFileReader: func(string, io.Reader, string) error { return nil }, mockWriteField: func(string, string) error { return nil }, mockClose: func() error { return nil }, } client := newClient(fb) client.requestBuilder = &failingRequestBuilder{} _, err := client.CreateVariImage(ctx, ImageVariRequest{Image: bytes.NewBuffer(nil)}) checks.ErrorIs(t, err, errTestRequestBuilderFailed, "CreateVariImage should return error if request builder fails") }) } type testNamedReader struct{ io.Reader } func (testNamedReader) Name() string { return "named.txt" } func TestWrapReader(t *testing.T) { r := bytes.NewBufferString("data") wrapped := WrapReader(r, "file.png", "image/png") f, ok := wrapped.(interface { Name() string ContentType() string }) if !ok { t.Fatal("wrapped reader missing Name or ContentType") } if f.Name() != "file.png" { t.Fatalf("expected name file.png, got %s", f.Name()) } if f.ContentType() != "image/png" { t.Fatalf("expected content type image/png, got %s", f.ContentType()) } // test name from underlying reader nr := testNamedReader{Reader: bytes.NewBufferString("d")} wrapped = WrapReader(nr, "", "text/plain") f, ok = wrapped.(interface { Name() string ContentType() string }) if !ok { t.Fatal("wrapped named reader missing Name or ContentType") } if f.Name() != "named.txt" { t.Fatalf("expected name named.txt, got %s", f.Name()) } if f.ContentType() != "text/plain" { t.Fatalf("expected content type text/plain, got %s", f.ContentType()) } // no name provided wrapped = WrapReader(bytes.NewBuffer(nil), "", "") f2, ok := wrapped.(interface{ Name() string }) if !ok { t.Fatal("wrapped anonymous reader missing Name") } if f2.Name() != "" { t.Fatalf("expected empty name, got %s", f2.Name()) } } ================================================ FILE: internal/error_accumulator.go ================================================ package openai import ( "bytes" "fmt" "io" ) type ErrorAccumulator interface { Write(p []byte) error Bytes() []byte } type errorBuffer interface { io.Writer Len() int Bytes() []byte } type DefaultErrorAccumulator struct { Buffer errorBuffer } func NewErrorAccumulator() ErrorAccumulator { return &DefaultErrorAccumulator{ Buffer: &bytes.Buffer{}, } } func (e *DefaultErrorAccumulator) Write(p []byte) error { _, err := e.Buffer.Write(p) if err != nil { return fmt.Errorf("error accumulator write error, %w", err) } return nil } func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte) { if e.Buffer.Len() == 0 { return } errBytes = e.Buffer.Bytes() return } ================================================ FILE: internal/error_accumulator_test.go ================================================ package openai_test import ( "testing" openai "github.com/sashabaranov/go-openai/internal" "github.com/sashabaranov/go-openai/internal/test" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestDefaultErrorAccumulator_WriteMultiple(t *testing.T) { ea, ok := openai.NewErrorAccumulator().(*openai.DefaultErrorAccumulator) if !ok { t.Fatal("type assertion to *DefaultErrorAccumulator failed") } checks.NoError(t, ea.Write([]byte("{\"error\": \"test1\"}"))) checks.NoError(t, ea.Write([]byte("{\"error\": \"test2\"}"))) expected := "{\"error\": \"test1\"}{\"error\": \"test2\"}" if string(ea.Bytes()) != expected { t.Fatalf("Expected %q, got %q", expected, ea.Bytes()) } } func TestDefaultErrorAccumulator_EmptyBuffer(t *testing.T) { ea, ok := openai.NewErrorAccumulator().(*openai.DefaultErrorAccumulator) if !ok { t.Fatal("type assertion to *DefaultErrorAccumulator failed") } if len(ea.Bytes()) != 0 { t.Fatal("Buffer should be empty initially") } } func TestDefaultErrorAccumulator_WriteError(t *testing.T) { ea := &openai.DefaultErrorAccumulator{Buffer: &test.FailingErrorBuffer{}} err := ea.Write([]byte("fail")) checks.ErrorIs(t, err, test.ErrTestErrorAccumulatorWriteFailed, "Write should propagate buffer errors") } ================================================ FILE: internal/form_builder.go ================================================ package openai import ( "fmt" "io" "mime/multipart" "net/textproto" "os" "path/filepath" "strings" ) type FormBuilder interface { CreateFormFile(fieldname string, file *os.File) error CreateFormFileReader(fieldname string, r io.Reader, filename string) error WriteField(fieldname, value string) error Close() error FormDataContentType() string } type DefaultFormBuilder struct { writer *multipart.Writer } func NewFormBuilder(body io.Writer) *DefaultFormBuilder { return &DefaultFormBuilder{ writer: multipart.NewWriter(body), } } func (fb *DefaultFormBuilder) CreateFormFile(fieldname string, file *os.File) error { return fb.createFormFile(fieldname, file, file.Name()) } var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") func escapeQuotes(s string) string { return quoteEscaper.Replace(s) } // CreateFormFileReader creates a form field with a file reader. // The filename in Content-Disposition is required. func (fb *DefaultFormBuilder) CreateFormFileReader(fieldname string, r io.Reader, filename string) error { if filename == "" { if f, ok := r.(interface{ Name() string }); ok { filename = f.Name() } } var contentType string if f, ok := r.(interface{ ContentType() string }); ok { contentType = f.ContentType() } h := make(textproto.MIMEHeader) h.Set( "Content-Disposition", fmt.Sprintf( `form-data; name="%s"; filename="%s"`, escapeQuotes(fieldname), escapeQuotes(filepath.Base(filename)), ), ) // content type is optional, but it can be set if contentType != "" { h.Set("Content-Type", contentType) } fieldWriter, err := fb.writer.CreatePart(h) if err != nil { return err } _, err = io.Copy(fieldWriter, r) if err != nil { return err } return nil } func (fb *DefaultFormBuilder) createFormFile(fieldname string, r io.Reader, filename string) error { if filename == "" { return fmt.Errorf("filename cannot be empty") } fieldWriter, err := fb.writer.CreateFormFile(fieldname, filename) if err != nil { return err } _, err = io.Copy(fieldWriter, r) if err != nil { return err } return nil } func (fb *DefaultFormBuilder) WriteField(fieldname, value string) error { if fieldname == "" { return fmt.Errorf("fieldname cannot be empty") } return fb.writer.WriteField(fieldname, value) } func (fb *DefaultFormBuilder) Close() error { return fb.writer.Close() } func (fb *DefaultFormBuilder) FormDataContentType() string { return fb.writer.FormDataContentType() } ================================================ FILE: internal/form_builder_test.go ================================================ package openai //nolint:testpackage // testing private field import ( "errors" "io" "github.com/sashabaranov/go-openai/internal/test/checks" "bytes" "os" "strings" "testing" ) type mockFormBuilder struct { mockCreateFormFile func(string, *os.File) error mockWriteField func(string, string) error mockClose func() error } func (m *mockFormBuilder) CreateFormFile(fieldname string, file *os.File) error { return m.mockCreateFormFile(fieldname, file) } func (m *mockFormBuilder) WriteField(fieldname, value string) error { return m.mockWriteField(fieldname, value) } func (m *mockFormBuilder) Close() error { return m.mockClose() } func (m *mockFormBuilder) FormDataContentType() string { return "" } func TestCloseMethod(t *testing.T) { t.Run("NormalClose", func(t *testing.T) { body := &bytes.Buffer{} builder := NewFormBuilder(body) checks.NoError(t, builder.Close(), "正常关闭应成功") }) t.Run("ErrorPropagation", func(t *testing.T) { errorMock := errors.New("mock close error") mockBuilder := &mockFormBuilder{ mockClose: func() error { return errorMock }, } err := mockBuilder.Close() checks.ErrorIs(t, err, errorMock, "应传递关闭错误") }) } type failingWriter struct { } var errMockFailingWriterError = errors.New("mock writer failed") func (*failingWriter) Write([]byte) (int, error) { return 0, errMockFailingWriterError } func TestFormBuilderWithFailingWriter(t *testing.T) { file, err := os.CreateTemp(t.TempDir(), "") if err != nil { t.Fatalf("Error creating tmp file: %v", err) } defer file.Close() builder := NewFormBuilder(&failingWriter{}) err = builder.CreateFormFile("file", file) checks.ErrorIs(t, err, errMockFailingWriterError, "formbuilder should return error if writer fails") } func TestFormBuilderWithClosedFile(t *testing.T) { file, err := os.CreateTemp(t.TempDir(), "") if err != nil { t.Fatalf("Error creating tmp file: %v", err) } file.Close() body := &bytes.Buffer{} builder := NewFormBuilder(body) err = builder.CreateFormFile("file", file) checks.HasError(t, err, "formbuilder should return error if file is closed") checks.ErrorIs(t, err, os.ErrClosed, "formbuilder should return error if file is closed") } type failingReader struct { } var errMockFailingReaderError = errors.New("mock reader failed") func (*failingReader) Read([]byte) (int, error) { return 0, errMockFailingReaderError } type readerWithNameAndContentType struct { io.Reader } func (*readerWithNameAndContentType) Name() string { return "" } func (*readerWithNameAndContentType) ContentType() string { return "image/png" } func TestFormBuilderWithReader(t *testing.T) { file, err := os.CreateTemp(t.TempDir(), "") if err != nil { t.Fatalf("Error creating tmp file: %v", err) } defer file.Close() builder := NewFormBuilder(&failingWriter{}) err = builder.CreateFormFileReader("file", file, file.Name()) checks.ErrorIs(t, err, errMockFailingWriterError, "formbuilder should return error if writer fails") builder = NewFormBuilder(&bytes.Buffer{}) reader := &failingReader{} err = builder.CreateFormFileReader("file", reader, "") checks.ErrorIs(t, err, errMockFailingReaderError, "formbuilder should return error if copy reader fails") successReader := &bytes.Buffer{} err = builder.CreateFormFileReader("file", successReader, "") checks.NoError(t, err, "formbuilder should not return error") rnc := &readerWithNameAndContentType{Reader: &bytes.Buffer{}} err = builder.CreateFormFileReader("file", rnc, "") checks.NoError(t, err, "formbuilder should not return error") } func TestFormDataContentType(t *testing.T) { t.Run("ReturnsUnderlyingWriterContentType", func(t *testing.T) { buf := &bytes.Buffer{} builder := NewFormBuilder(buf) contentType := builder.FormDataContentType() if contentType == "" { t.Errorf("expected non-empty content type, got empty string") } }) } func TestWriteField(t *testing.T) { t.Run("EmptyFieldNameShouldReturnError", func(t *testing.T) { buf := &bytes.Buffer{} builder := NewFormBuilder(buf) err := builder.WriteField("", "some value") checks.HasError(t, err, "fieldname is required") }) t.Run("ValidFieldNameShouldSucceed", func(t *testing.T) { buf := &bytes.Buffer{} builder := NewFormBuilder(buf) err := builder.WriteField("key", "value") checks.NoError(t, err, "should write field without error") }) } func TestCreateFormFile(t *testing.T) { buf := &bytes.Buffer{} builder := NewFormBuilder(buf) err := builder.createFormFile("file", bytes.NewBufferString("data"), "") if err == nil { t.Fatal("expected error for empty filename") } builder = NewFormBuilder(&failingWriter{}) err = builder.createFormFile("file", bytes.NewBufferString("data"), "name") checks.ErrorIs(t, err, errMockFailingWriterError, "should propagate writer error") } func TestCreateFormFileSuccess(t *testing.T) { buf := &bytes.Buffer{} builder := NewFormBuilder(buf) err := builder.createFormFile("file", bytes.NewBufferString("data"), "foo.txt") checks.NoError(t, err, "createFormFile should succeed") if !strings.Contains(buf.String(), "filename=\"foo.txt\"") { t.Fatalf("expected filename header, got %q", buf.String()) } } ================================================ FILE: internal/marshaller.go ================================================ package openai import ( "encoding/json" ) type Marshaller interface { Marshal(value any) ([]byte, error) } type JSONMarshaller struct{} func (jm *JSONMarshaller) Marshal(value any) ([]byte, error) { return json.Marshal(value) } ================================================ FILE: internal/marshaller_test.go ================================================ package openai_test import ( "testing" openai "github.com/sashabaranov/go-openai/internal" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestJSONMarshaller_Normal(t *testing.T) { jm := &openai.JSONMarshaller{} data := map[string]string{"key": "value"} b, err := jm.Marshal(data) checks.NoError(t, err) if len(b) == 0 { t.Fatal("should return non-empty bytes") } } func TestJSONMarshaller_InvalidInput(t *testing.T) { jm := &openai.JSONMarshaller{} _, err := jm.Marshal(make(chan int)) checks.HasError(t, err, "should return error for unsupported type") } func TestJSONMarshaller_EmptyValue(t *testing.T) { jm := &openai.JSONMarshaller{} b, err := jm.Marshal(nil) checks.NoError(t, err) if string(b) != "null" { t.Fatalf("unexpected marshaled value: %s", string(b)) } } ================================================ FILE: internal/request_builder.go ================================================ package openai import ( "bytes" "context" "io" "net/http" ) type RequestBuilder interface { Build(ctx context.Context, method, url string, body any, header http.Header) (*http.Request, error) } type HTTPRequestBuilder struct { marshaller Marshaller } func NewRequestBuilder() *HTTPRequestBuilder { return &HTTPRequestBuilder{ marshaller: &JSONMarshaller{}, } } func (b *HTTPRequestBuilder) Build( ctx context.Context, method string, url string, body any, header http.Header, ) (req *http.Request, err error) { var bodyReader io.Reader if body != nil { if v, ok := body.(io.Reader); ok { bodyReader = v } else { var reqBytes []byte reqBytes, err = b.marshaller.Marshal(body) if err != nil { return } bodyReader = bytes.NewBuffer(reqBytes) } } req, err = http.NewRequestWithContext(ctx, method, url, bodyReader) if err != nil { return } if header != nil { req.Header = header } return } ================================================ FILE: internal/request_builder_test.go ================================================ package openai //nolint:testpackage // testing private field import ( "bytes" "context" "errors" "io" "net/http" "reflect" "testing" ) var errTestMarshallerFailed = errors.New("test marshaller failed") type failingMarshaller struct{} func (*failingMarshaller) Marshal(_ any) ([]byte, error) { return []byte{}, errTestMarshallerFailed } func TestRequestBuilderReturnsMarshallerErrors(t *testing.T) { builder := HTTPRequestBuilder{ marshaller: &failingMarshaller{}, } _, err := builder.Build(context.Background(), "", "", struct{}{}, nil) if !errors.Is(err, errTestMarshallerFailed) { t.Fatalf("Did not return error when marshaller failed: %v", err) } } func TestRequestBuilderReturnsRequest(t *testing.T) { b := NewRequestBuilder() var ( ctx = context.Background() method = http.MethodPost url = "/foo" request = map[string]string{"foo": "bar"} reqBytes, _ = b.marshaller.Marshal(request) want, _ = http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(reqBytes)) ) got, _ := b.Build(ctx, method, url, request, nil) if !reflect.DeepEqual(got.Body, want.Body) || !reflect.DeepEqual(got.URL, want.URL) || !reflect.DeepEqual(got.Method, want.Method) { t.Errorf("Build() got = %v, want %v", got, want) } } func TestRequestBuilderReturnsRequestWhenRequestOfArgsIsNil(t *testing.T) { var ( ctx = context.Background() method = http.MethodGet url = "/foo" want, _ = http.NewRequestWithContext(ctx, method, url, nil) ) b := NewRequestBuilder() got, _ := b.Build(ctx, method, url, nil, nil) if !reflect.DeepEqual(got, want) { t.Errorf("Build() got = %v, want %v", got, want) } } func TestRequestBuilderWithReaderBodyAndHeader(t *testing.T) { b := NewRequestBuilder() ctx := context.Background() method := http.MethodPost url := "/reader" bodyContent := "hello" body := bytes.NewBufferString(bodyContent) header := http.Header{"X-Test": []string{"val"}} req, err := b.Build(ctx, method, url, body, header) if err != nil { t.Fatalf("Build returned error: %v", err) } gotBody, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("cannot read body: %v", err) } if string(gotBody) != bodyContent { t.Fatalf("expected body %q, got %q", bodyContent, string(gotBody)) } if req.Header.Get("X-Test") != "val" { t.Fatalf("expected header set to val, got %q", req.Header.Get("X-Test")) } } func TestRequestBuilderInvalidURL(t *testing.T) { b := NewRequestBuilder() _, err := b.Build(context.Background(), http.MethodGet, ":", nil, nil) if err == nil { t.Fatal("expected error for invalid URL") } } ================================================ FILE: internal/test/checks/checks.go ================================================ package checks import ( "errors" "testing" ) func NoError(t *testing.T, err error, message ...string) { t.Helper() if err != nil { t.Error(err, message) } } func NoErrorF(t *testing.T, err error, message ...string) { t.Helper() if err != nil { t.Fatal(err, message) } } func HasError(t *testing.T, err error, message ...string) { t.Helper() if err == nil { t.Error(err, message) } } func ErrorIs(t *testing.T, err, target error, msg ...string) { t.Helper() if !errors.Is(err, target) { t.Fatal(msg) } } func ErrorIsF(t *testing.T, err, target error, format string, msg ...string) { t.Helper() if !errors.Is(err, target) { t.Fatalf(format, msg) } } func ErrorIsNot(t *testing.T, err, target error, msg ...string) { t.Helper() if errors.Is(err, target) { t.Fatal(msg) } } func ErrorIsNotf(t *testing.T, err, target error, format string, msg ...string) { t.Helper() if errors.Is(err, target) { t.Fatalf(format, msg) } } ================================================ FILE: internal/test/checks/checks_test.go ================================================ package checks_test import ( "errors" "testing" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestChecksSuccessPaths(t *testing.T) { checks.NoError(t, nil) checks.NoErrorF(t, nil) checks.HasError(t, errors.New("err")) target := errors.New("x") checks.ErrorIs(t, target, target) checks.ErrorIsF(t, target, target, "msg") checks.ErrorIsNot(t, errors.New("y"), target) checks.ErrorIsNotf(t, errors.New("y"), target, "msg") } ================================================ FILE: internal/test/failer.go ================================================ package test import "errors" var ( ErrTestErrorAccumulatorWriteFailed = errors.New("test error accumulator failed") ) type FailingErrorBuffer struct{} func (b *FailingErrorBuffer) Write(_ []byte) (n int, err error) { return 0, ErrTestErrorAccumulatorWriteFailed } func (b *FailingErrorBuffer) Len() int { return 0 } func (b *FailingErrorBuffer) Bytes() []byte { return []byte{} } ================================================ FILE: internal/test/failer_test.go ================================================ //nolint:testpackage // need access to unexported fields and types for testing package test import ( "errors" "testing" ) func TestFailingErrorBuffer(t *testing.T) { buf := &FailingErrorBuffer{} n, err := buf.Write([]byte("test")) if !errors.Is(err, ErrTestErrorAccumulatorWriteFailed) { t.Fatalf("expected %v, got %v", ErrTestErrorAccumulatorWriteFailed, err) } if n != 0 { t.Fatalf("expected n=0, got %d", n) } if buf.Len() != 0 { t.Fatalf("expected Len 0, got %d", buf.Len()) } if len(buf.Bytes()) != 0 { t.Fatalf("expected empty bytes") } } ================================================ FILE: internal/test/helpers.go ================================================ package test import ( "github.com/sashabaranov/go-openai/internal/test/checks" "net/http" "os" "testing" ) // CreateTestFile creates a fake file with "hello" as the content. func CreateTestFile(t *testing.T, path string) { file, err := os.Create(path) checks.NoError(t, err, "failed to create file") if _, err = file.WriteString("hello"); err != nil { t.Fatalf("failed to write to file %v", err) } file.Close() } // TokenRoundTripper is a struct that implements the RoundTripper // interface, specifically to handle the authentication token by adding a token // to the request header. We need this because the API requires that each // request include a valid API token in the headers for authentication and // authorization. type TokenRoundTripper struct { Token string Fallback http.RoundTripper } // RoundTrip takes an *http.Request as input and returns an // *http.Response and an error. // // It is expected to use the provided request to create a connection to an HTTP // server and return the response, or an error if one occurred. The returned // Response should have its Body closed. If the RoundTrip method returns an // error, the Client's Get, Head, Post, and PostForm methods return the same // error. func (t *TokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { req.Header.Set("Authorization", "Bearer "+t.Token) return t.Fallback.RoundTrip(req) } ================================================ FILE: internal/test/helpers_test.go ================================================ package test_test import ( "io" "net/http" "net/http/httptest" "os" "path/filepath" "testing" internaltest "github.com/sashabaranov/go-openai/internal/test" ) func TestCreateTestFile(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "file.txt") internaltest.CreateTestFile(t, path) data, err := os.ReadFile(path) if err != nil { t.Fatalf("failed to read created file: %v", err) } if string(data) != "hello" { t.Fatalf("unexpected file contents: %q", string(data)) } } func TestTokenRoundTripperAddsHeader(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") != "Bearer "+internaltest.GetTestToken() { t.Fatalf("authorization header not set") } w.WriteHeader(http.StatusOK) })) defer srv.Close() client := srv.Client() client.Transport = &internaltest.TokenRoundTripper{Token: internaltest.GetTestToken(), Fallback: client.Transport} req, err := http.NewRequest(http.MethodGet, srv.URL, nil) if err != nil { t.Fatalf("request error: %v", err) } resp, err := client.Do(req) if err != nil { t.Fatalf("client request error: %v", err) } if _, err = io.Copy(io.Discard, resp.Body); err != nil { t.Fatalf("read body: %v", err) } resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("unexpected status: %d", resp.StatusCode) } } ================================================ FILE: internal/test/server.go ================================================ package test import ( "log" "net/http" "net/http/httptest" "regexp" "strings" ) const testAPI = "this-is-my-secure-token-do-not-steal!!" func GetTestToken() string { return testAPI } type ServerTest struct { handlers map[string]handler } type handler func(w http.ResponseWriter, r *http.Request) func NewTestServer() *ServerTest { return &ServerTest{handlers: make(map[string]handler)} } // HandlerCount returns the number of registered handlers. func (ts *ServerTest) HandlerCount() int { return len(ts.handlers) } // HasHandler checks if a handler was registered for the given path. func (ts *ServerTest) HasHandler(path string) bool { path = strings.ReplaceAll(path, "*", ".*") _, ok := ts.handlers[path] return ok } func (ts *ServerTest) RegisterHandler(path string, handler handler) { // to make the registered paths friendlier to a regex match in the route handler // in OpenAITestServer path = strings.ReplaceAll(path, "*", ".*") ts.handlers[path] = handler } // OpenAITestServer Creates a mocked OpenAI server which can pretend to handle requests during testing. func (ts *ServerTest) OpenAITestServer() *httptest.Server { return httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf("received a %s request at path %q\n", r.Method, r.URL.Path) // check auth if r.Header.Get("Authorization") != "Bearer "+GetTestToken() && r.Header.Get("api-key") != GetTestToken() { w.WriteHeader(http.StatusUnauthorized) return } // Handle /path/* routes. // Note: the * is converted to a .* in register handler for proper regex handling for route, handler := range ts.handlers { // Adding ^ and $ to make path matching deterministic since go map iteration isn't ordered pattern, _ := regexp.Compile("^" + route + "$") if pattern.MatchString(r.URL.Path) { handler(w, r) return } } http.Error(w, "the resource path doesn't exist", http.StatusNotFound) })) } ================================================ FILE: internal/test/server_test.go ================================================ package test_test import ( "io" "net/http" "testing" internaltest "github.com/sashabaranov/go-openai/internal/test" ) func TestGetTestToken(t *testing.T) { if internaltest.GetTestToken() != "this-is-my-secure-token-do-not-steal!!" { t.Fatalf("unexpected token") } } func TestNewTestServer(t *testing.T) { ts := internaltest.NewTestServer() if ts == nil { t.Fatalf("server not properly initialized") } if ts.HandlerCount() != 0 { t.Fatalf("expected no handlers initially") } } func TestRegisterHandlerTransformsPath(t *testing.T) { ts := internaltest.NewTestServer() h := func(_ http.ResponseWriter, _ *http.Request) {} ts.RegisterHandler("/foo/*", h) if !ts.HasHandler("/foo/*") { t.Fatalf("handler not registered with transformed path") } } func TestOpenAITestServer(t *testing.T) { ts := internaltest.NewTestServer() ts.RegisterHandler("/v1/test/*", func(w http.ResponseWriter, _ *http.Request) { if _, err := io.WriteString(w, "ok"); err != nil { t.Fatalf("write: %v", err) } }) srv := ts.OpenAITestServer() srv.Start() defer srv.Close() base := srv.Client().Transport client := &http.Client{Transport: &internaltest.TokenRoundTripper{Token: internaltest.GetTestToken(), Fallback: base}} resp, err := client.Get(srv.URL + "/v1/test/123") if err != nil { t.Fatalf("request error: %v", err) } body, err := io.ReadAll(resp.Body) resp.Body.Close() if err != nil { t.Fatalf("read response body: %v", err) } if resp.StatusCode != http.StatusOK || string(body) != "ok" { t.Fatalf("unexpected response: %d %q", resp.StatusCode, string(body)) } // unregistered path resp, err = client.Get(srv.URL + "/unknown") if err != nil { t.Fatalf("request error: %v", err) } if resp.StatusCode != http.StatusNotFound { t.Fatalf("expected 404, got %d", resp.StatusCode) } // missing token should return unauthorized clientNoToken := srv.Client() resp, err = clientNoToken.Get(srv.URL + "/v1/test/123") if err != nil { t.Fatalf("request error: %v", err) } if resp.StatusCode != http.StatusUnauthorized { t.Fatalf("expected 401, got %d", resp.StatusCode) } } ================================================ FILE: internal/unmarshaler.go ================================================ package openai import ( "encoding/json" ) type Unmarshaler interface { Unmarshal(data []byte, v any) error } type JSONUnmarshaler struct{} func (jm *JSONUnmarshaler) Unmarshal(data []byte, v any) error { return json.Unmarshal(data, v) } ================================================ FILE: internal/unmarshaler_test.go ================================================ package openai_test import ( "testing" openai "github.com/sashabaranov/go-openai/internal" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestJSONUnmarshaler_Normal(t *testing.T) { jm := &openai.JSONUnmarshaler{} data := []byte(`{"key":"value"}`) var v map[string]string err := jm.Unmarshal(data, &v) checks.NoError(t, err) if v["key"] != "value" { t.Fatal("unmarshal result mismatch") } } func TestJSONUnmarshaler_InvalidJSON(t *testing.T) { jm := &openai.JSONUnmarshaler{} data := []byte(`{invalid}`) var v map[string]interface{} err := jm.Unmarshal(data, &v) checks.HasError(t, err, "should return error for invalid JSON") } func TestJSONUnmarshaler_EmptyInput(t *testing.T) { jm := &openai.JSONUnmarshaler{} var v interface{} err := jm.Unmarshal(nil, &v) checks.HasError(t, err, "should return error for nil input") } ================================================ FILE: jsonschema/containsref_test.go ================================================ package jsonschema_test import ( "testing" "github.com/sashabaranov/go-openai/jsonschema" ) // SelfRef struct used to produce a self-referential schema. type SelfRef struct { Friends []SelfRef `json:"friends"` } // Address struct referenced by Person without self-reference. type Address struct { Street string `json:"street"` } type Person struct { Address Address `json:"address"` } // TestGenerateSchemaForType_SelfRef ensures that self-referential types are not // flattened during schema generation. func TestGenerateSchemaForType_SelfRef(t *testing.T) { schema, err := jsonschema.GenerateSchemaForType(SelfRef{}) if err != nil { t.Fatalf("unexpected error: %v", err) } if _, ok := schema.Defs["SelfRef"]; !ok { t.Fatal("expected defs to contain SelfRef for self reference") } } // TestGenerateSchemaForType_NoSelfRef ensures that non-self-referential types // are flattened and do not reappear in $defs. func TestGenerateSchemaForType_NoSelfRef(t *testing.T) { schema, err := jsonschema.GenerateSchemaForType(Person{}) if err != nil { t.Fatalf("unexpected error: %v", err) } if _, ok := schema.Defs["Person"]; ok { t.Fatal("unexpected Person definition in defs") } if _, ok := schema.Defs["Address"]; !ok { t.Fatal("expected Address definition in defs") } } ================================================ FILE: jsonschema/json.go ================================================ // Package jsonschema provides very simple functionality for representing a JSON schema as a // (nested) struct. This struct can be used with the chat completion "function call" feature. // For more complicated schemas, it is recommended to use a dedicated JSON schema library // and/or pass in the schema in []byte format. package jsonschema import ( "encoding/json" "fmt" "reflect" "strconv" "strings" ) type DataType string const ( Object DataType = "object" Number DataType = "number" Integer DataType = "integer" String DataType = "string" Array DataType = "array" Null DataType = "null" Boolean DataType = "boolean" ) // Definition is a struct for describing a JSON Schema. // It is fairly limited, and you may have better luck using a third-party library. type Definition struct { // Type specifies the data type of the schema. Type DataType `json:"type,omitempty"` // Description is the description of the schema. Description string `json:"description,omitempty"` // Enum is used to restrict a value to a fixed set of values. It must be an array with at least // one element, where each element is unique. You will probably only use this with strings. Enum []string `json:"enum,omitempty"` // Properties describes the properties of an object, if the schema type is Object. Properties map[string]Definition `json:"properties,omitempty"` // Required specifies which properties are required, if the schema type is Object. Required []string `json:"required,omitempty"` // Items specifies which data type an array contains, if the schema type is Array. Items *Definition `json:"items,omitempty"` // AdditionalProperties is used to control the handling of properties in an object // that are not explicitly defined in the properties section of the schema. example: // additionalProperties: true // additionalProperties: false // additionalProperties: jsonschema.Definition{Type: jsonschema.String} AdditionalProperties any `json:"additionalProperties,omitempty"` // Whether the schema is nullable or not. Nullable bool `json:"nullable,omitempty"` // Ref Reference to a definition in $defs or external schema. Ref string `json:"$ref,omitempty"` // Defs A map of reusable schema definitions. Defs map[string]Definition `json:"$defs,omitempty"` } func (d *Definition) MarshalJSON() ([]byte, error) { if d.Properties == nil { d.Properties = make(map[string]Definition) } type Alias Definition return json.Marshal(struct { Alias }{ Alias: (Alias)(*d), }) } func (d *Definition) Unmarshal(content string, v any) error { return VerifySchemaAndUnmarshal(*d, []byte(content), v) } func GenerateSchemaForType(v any) (*Definition, error) { var defs = make(map[string]Definition) def, err := reflectSchema(reflect.TypeOf(v), defs) if err != nil { return nil, err } // If the schema has a root $ref, resolve it by: // 1. Extracting the key from the $ref. // 2. Detaching the referenced definition from $defs. // 3. Checking for self-references in the detached definition. // - If a self-reference is found, restore the original $defs structure. // 4. Flattening the referenced definition into the root schema. // 5. Clearing the $ref field in the root schema. if def.Ref != "" { origRef := def.Ref key := strings.TrimPrefix(origRef, "#/$defs/") if root, ok := defs[key]; ok { delete(defs, key) root.Defs = defs if containsRef(root, origRef) { root.Defs = nil defs[key] = root } *def = root } def.Ref = "" } def.Defs = defs return def, nil } func reflectSchema(t reflect.Type, defs map[string]Definition) (*Definition, error) { var d Definition switch t.Kind() { case reflect.String: d.Type = String case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: d.Type = Integer case reflect.Float32, reflect.Float64: d.Type = Number case reflect.Bool: d.Type = Boolean case reflect.Slice, reflect.Array: d.Type = Array items, err := reflectSchema(t.Elem(), defs) if err != nil { return nil, err } d.Items = items case reflect.Struct: if t.Name() != "" { if _, ok := defs[t.Name()]; !ok { defs[t.Name()] = Definition{} object, err := reflectSchemaObject(t, defs) if err != nil { return nil, err } defs[t.Name()] = *object } return &Definition{Ref: "#/$defs/" + t.Name()}, nil } d.Type = Object d.AdditionalProperties = false object, err := reflectSchemaObject(t, defs) if err != nil { return nil, err } d = *object case reflect.Ptr: definition, err := reflectSchema(t.Elem(), defs) if err != nil { return nil, err } d = *definition case reflect.Invalid, reflect.Uintptr, reflect.Complex64, reflect.Complex128, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.UnsafePointer: return nil, fmt.Errorf("unsupported type: %s", t.Kind().String()) default: } return &d, nil } func reflectSchemaObject(t reflect.Type, defs map[string]Definition) (*Definition, error) { var d = Definition{ Type: Object, AdditionalProperties: false, } properties := make(map[string]Definition) var requiredFields []string for i := 0; i < t.NumField(); i++ { field := t.Field(i) if !field.IsExported() { continue } jsonTag := field.Tag.Get("json") var required = true switch { case jsonTag == "-": continue case jsonTag == "": jsonTag = field.Name case strings.HasSuffix(jsonTag, ",omitempty"): jsonTag = strings.TrimSuffix(jsonTag, ",omitempty") required = false } item, err := reflectSchema(field.Type, defs) if err != nil { return nil, err } description := field.Tag.Get("description") if description != "" { item.Description = description } enum := field.Tag.Get("enum") if enum != "" { item.Enum = strings.Split(enum, ",") } if n := field.Tag.Get("nullable"); n != "" { nullable, _ := strconv.ParseBool(n) item.Nullable = nullable } properties[jsonTag] = *item if s := field.Tag.Get("required"); s != "" { required, _ = strconv.ParseBool(s) } if required { requiredFields = append(requiredFields, jsonTag) } } d.Required = requiredFields d.Properties = properties return &d, nil } func containsRef(def Definition, targetRef string) bool { if def.Ref == targetRef { return true } for _, d := range def.Defs { if containsRef(d, targetRef) { return true } } for _, prop := range def.Properties { if containsRef(prop, targetRef) { return true } } if def.Items != nil && containsRef(*def.Items, targetRef) { return true } return false } ================================================ FILE: jsonschema/json_additional_test.go ================================================ package jsonschema_test import ( "testing" "github.com/sashabaranov/go-openai/jsonschema" ) // Test Definition.Unmarshal, including success path, validation error, // JSON syntax error and type mismatch during unmarshalling. func TestDefinitionUnmarshal(t *testing.T) { schema := jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "name": {Type: jsonschema.String}, }, } var dst struct { Name string `json:"name"` } if err := schema.Unmarshal(`{"name":"foo"}`, &dst); err != nil { t.Fatalf("unexpected error: %v", err) } if dst.Name != "foo" { t.Errorf("expected name to be foo, got %q", dst.Name) } if err := schema.Unmarshal(`{`, &dst); err == nil { t.Error("expected error for malformed json") } if err := schema.Unmarshal(`{"name":1}`, &dst); err == nil { t.Error("expected validation error") } numSchema := jsonschema.Definition{Type: jsonschema.Number} var s string if err := numSchema.Unmarshal(`123`, &s); err == nil { t.Error("expected unmarshal type error") } } // Ensure GenerateSchemaForType returns an error when encountering unsupported types. func TestGenerateSchemaForTypeUnsupported(t *testing.T) { type Bad struct { Ch chan int `json:"ch"` } if _, err := jsonschema.GenerateSchemaForType(Bad{}); err == nil { t.Fatal("expected error for unsupported type") } } // Validate should fail when provided data does not match the expected container types. func TestValidateInvalidContainers(t *testing.T) { objSchema := jsonschema.Definition{Type: jsonschema.Object} if jsonschema.Validate(objSchema, 1) { t.Error("expected object validation to fail for non-map input") } arrSchema := jsonschema.Definition{Type: jsonschema.Array, Items: &jsonschema.Definition{Type: jsonschema.String}} if jsonschema.Validate(arrSchema, 1) { t.Error("expected array validation to fail for non-slice input") } } // Validate should return false when $ref cannot be resolved. func TestValidateRefNotFound(t *testing.T) { refSchema := jsonschema.Definition{Ref: "#/$defs/Missing"} if jsonschema.Validate(refSchema, "data", jsonschema.WithDefs(map[string]jsonschema.Definition{})) { t.Error("expected validation to fail when reference is missing") } } ================================================ FILE: jsonschema/json_errors_test.go ================================================ package jsonschema_test import ( "testing" "github.com/sashabaranov/go-openai/jsonschema" ) // TestGenerateSchemaForType_ErrorPaths verifies error handling for unsupported types. func TestGenerateSchemaForType_ErrorPaths(t *testing.T) { type anon struct{ Ch chan int } tests := []struct { name string v any }{ {"slice", []chan int{}}, {"anon struct", anon{}}, {"pointer", (*chan int)(nil)}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if _, err := jsonschema.GenerateSchemaForType(tt.v); err == nil { t.Errorf("expected error for %s", tt.name) } }) } } ================================================ FILE: jsonschema/json_test.go ================================================ package jsonschema_test import ( "encoding/json" "reflect" "testing" "github.com/sashabaranov/go-openai/jsonschema" ) func TestDefinition_MarshalJSON(t *testing.T) { tests := []struct { name string def jsonschema.Definition want string }{ { name: "Test with empty Definition", def: jsonschema.Definition{}, want: `{}`, }, { name: "Test with Definition properties set", def: jsonschema.Definition{ Type: jsonschema.String, Description: "A string type", Properties: map[string]jsonschema.Definition{ "name": { Type: jsonschema.String, }, }, }, want: `{ "type":"string", "description":"A string type", "properties":{ "name":{ "type":"string" } } }`, }, { name: "Test with nested Definition properties", def: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "user": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "name": { Type: jsonschema.String, }, "age": { Type: jsonschema.Integer, }, }, }, }, }, want: `{ "type":"object", "properties":{ "user":{ "type":"object", "properties":{ "name":{ "type":"string" }, "age":{ "type":"integer" } } } } }`, }, { name: "Test with complex nested Definition", def: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "user": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "name": { Type: jsonschema.String, }, "age": { Type: jsonschema.Integer, }, "address": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "city": { Type: jsonschema.String, }, "country": { Type: jsonschema.String, }, }, }, }, }, }, }, want: `{ "type":"object", "properties":{ "user":{ "type":"object", "properties":{ "name":{ "type":"string" }, "age":{ "type":"integer" }, "address":{ "type":"object", "properties":{ "city":{ "type":"string" }, "country":{ "type":"string" } } } } } } }`, }, { name: "Test with Array type Definition", def: jsonschema.Definition{ Type: jsonschema.Array, Items: &jsonschema.Definition{ Type: jsonschema.String, }, Properties: map[string]jsonschema.Definition{ "name": { Type: jsonschema.String, }, }, }, want: `{ "type":"array", "items":{ "type":"string" }, "properties":{ "name":{ "type":"string" } } }`, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { wantBytes := []byte(tt.want) var want map[string]interface{} err := json.Unmarshal(wantBytes, &want) if err != nil { t.Errorf("Failed to Unmarshal JSON: error = %v", err) return } got := structToMap(t, tt.def) gotPtr := structToMap(t, &tt.def) if !reflect.DeepEqual(got, want) { t.Errorf("MarshalJSON() got = %v, want %v", got, want) } if !reflect.DeepEqual(gotPtr, want) { t.Errorf("MarshalJSON() gotPtr = %v, want %v", gotPtr, want) } }) } } type User struct { ID int `json:"id,omitempty"` Name string `json:"name,omitempty"` Orders []*Order `json:"orders,omitempty"` } type Order struct { ID int `json:"id,omitempty"` Amount float64 `json:"amount,omitempty"` Buyer *User `json:"buyer,omitempty"` } func TestStructToSchema(t *testing.T) { type Tweet struct { Text string `json:"text"` } type Person struct { Name string `json:"name,omitempty"` Age int `json:"age,omitempty"` Friends []Person `json:"friends,omitempty"` Tweets []Tweet `json:"tweets,omitempty"` } type MyStructuredResponse struct { PascalCase string `json:"pascal_case" required:"true" description:"PascalCase"` CamelCase string `json:"camel_case" required:"true" description:"CamelCase"` KebabCase string `json:"kebab_case" required:"true" description:"KebabCase"` SnakeCase string `json:"snake_case" required:"true" description:"SnakeCase"` } tests := []struct { name string in any want string }{ { name: "Test with empty struct", in: struct{}{}, want: `{ "type":"object", "additionalProperties":false }`, }, { name: "Test with struct containing many fields", in: struct { Name string `json:"name"` Age int `json:"age"` Active bool `json:"active"` Height float64 `json:"height"` Cities []struct { Name string `json:"name"` State string `json:"state"` } `json:"cities"` }{ Name: "John Doe", Age: 30, Cities: []struct { Name string `json:"name"` State string `json:"state"` }{ {Name: "New York", State: "NY"}, {Name: "Los Angeles", State: "CA"}, }, }, want: `{ "type":"object", "properties":{ "name":{ "type":"string" }, "age":{ "type":"integer" }, "active":{ "type":"boolean" }, "height":{ "type":"number" }, "cities":{ "type":"array", "items":{ "additionalProperties":false, "type":"object", "properties":{ "name":{ "type":"string" }, "state":{ "type":"string" } }, "required":["name","state"] } } }, "required":["name","age","active","height","cities"], "additionalProperties":false }`, }, { name: "Test with description tag", in: struct { Name string `json:"name" description:"The name of the person"` }{ Name: "John Doe", }, want: `{ "type":"object", "properties":{ "name":{ "type":"string", "description":"The name of the person" } }, "required":["name"], "additionalProperties":false }`, }, { name: "Test with required tag", in: struct { Name string `json:"name" required:"false"` }{ Name: "John Doe", }, want: `{ "type":"object", "properties":{ "name":{ "type":"string" } }, "additionalProperties":false }`, }, { name: "Test with enum tag", in: struct { Color string `json:"color" enum:"red,green,blue"` }{ Color: "red", }, want: `{ "type":"object", "properties":{ "color":{ "type":"string", "enum":["red","green","blue"] } }, "required":["color"], "additionalProperties":false }`, }, { name: "Test with nullable tag", in: struct { Name *string `json:"name" nullable:"true"` }{ Name: nil, }, want: `{ "type":"object", "properties":{ "name":{ "type":"string", "nullable":true } }, "required":["name"], "additionalProperties":false }`, }, { name: "Test with exclude mark", in: struct { Name string `json:"-"` }{ Name: "Name", }, want: `{ "type":"object", "additionalProperties":false }`, }, { name: "Test with no json tag", in: struct { Name string }{ Name: "", }, want: `{ "type":"object", "properties":{ "Name":{ "type":"string" } }, "required":["Name"], "additionalProperties":false }`, }, { name: "Test with omitempty tag", in: struct { Name string `json:"name,omitempty"` }{ Name: "", }, want: `{ "type":"object", "properties":{ "name":{ "type":"string" } }, "additionalProperties":false }`, }, { name: "Test with $ref and $defs", in: struct { Person Person `json:"person"` Tweets []Tweet `json:"tweets"` }{}, want: `{ "type" : "object", "properties" : { "person" : { "$ref" : "#/$defs/Person" }, "tweets" : { "type" : "array", "items" : { "$ref" : "#/$defs/Tweet" } } }, "required" : [ "person", "tweets" ], "additionalProperties" : false, "$defs" : { "Person" : { "type" : "object", "properties" : { "age" : { "type" : "integer" }, "friends" : { "type" : "array", "items" : { "$ref" : "#/$defs/Person" } }, "name" : { "type" : "string" }, "tweets" : { "type" : "array", "items" : { "$ref" : "#/$defs/Tweet" } } }, "additionalProperties" : false }, "Tweet" : { "type" : "object", "properties" : { "text" : { "type" : "string" } }, "required" : [ "text" ], "additionalProperties" : false } } }`, }, { name: "Test Person", in: Person{}, want: `{ "type": "object", "properties": { "age": { "type": "integer" }, "friends": { "type": "array", "items": { "$ref": "#/$defs/Person" } }, "name": { "type": "string" }, "tweets": { "type": "array", "items": { "$ref": "#/$defs/Tweet" } } }, "additionalProperties": false, "$defs": { "Person": { "type": "object", "properties": { "age": { "type": "integer" }, "friends": { "type": "array", "items": { "$ref": "#/$defs/Person" } }, "name": { "type": "string" }, "tweets": { "type": "array", "items": { "$ref": "#/$defs/Tweet" } } }, "additionalProperties": false }, "Tweet": { "type": "object", "properties": { "text": { "type": "string" } }, "required": [ "text" ], "additionalProperties": false } } }`, }, { name: "Test MyStructuredResponse", in: MyStructuredResponse{}, want: `{ "type": "object", "properties": { "camel_case": { "type": "string", "description": "CamelCase" }, "kebab_case": { "type": "string", "description": "KebabCase" }, "pascal_case": { "type": "string", "description": "PascalCase" }, "snake_case": { "type": "string", "description": "SnakeCase" } }, "required": [ "pascal_case", "camel_case", "kebab_case", "snake_case" ], "additionalProperties": false }`, }, { name: "Test User", in: User{}, want: `{ "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" }, "orders": { "type": "array", "items": { "$ref": "#/$defs/Order" } } }, "additionalProperties": false, "$defs": { "Order": { "type": "object", "properties": { "amount": { "type": "number" }, "buyer": { "$ref": "#/$defs/User" }, "id": { "type": "integer" } }, "additionalProperties": false }, "User": { "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" }, "orders": { "type": "array", "items": { "$ref": "#/$defs/Order" } } }, "additionalProperties": false } } }`, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { wantBytes := []byte(tt.want) schema, err := jsonschema.GenerateSchemaForType(tt.in) if err != nil { t.Errorf("Failed to generate schema: error = %v", err) return } var want map[string]interface{} err = json.Unmarshal(wantBytes, &want) if err != nil { t.Errorf("Failed to Unmarshal JSON: error = %v", err) return } got := structToMap(t, schema) gotPtr := structToMap(t, &schema) if !reflect.DeepEqual(got, want) { t.Errorf("MarshalJSON() got = %v, want %v", got, want) } if !reflect.DeepEqual(gotPtr, want) { t.Errorf("MarshalJSON() gotPtr = %v, want %v", gotPtr, want) } }) } } func structToMap(t *testing.T, v any) map[string]any { t.Helper() gotBytes, err := json.Marshal(v) if err != nil { t.Errorf("Failed to Marshal JSON: error = %v", err) return nil } var got map[string]interface{} err = json.Unmarshal(gotBytes, &got) if err != nil { t.Errorf("Failed to Unmarshal JSON: error = %v", err) return nil } return got } ================================================ FILE: jsonschema/validate.go ================================================ package jsonschema import ( "encoding/json" "errors" ) func CollectDefs(def Definition) map[string]Definition { result := make(map[string]Definition) collectDefsRecursive(def, result, "#") return result } func collectDefsRecursive(def Definition, result map[string]Definition, prefix string) { for k, v := range def.Defs { path := prefix + "/$defs/" + k result[path] = v collectDefsRecursive(v, result, path) } for k, sub := range def.Properties { collectDefsRecursive(sub, result, prefix+"/properties/"+k) } if def.Items != nil { collectDefsRecursive(*def.Items, result, prefix) } } func VerifySchemaAndUnmarshal(schema Definition, content []byte, v any) error { var data any err := json.Unmarshal(content, &data) if err != nil { return err } if !Validate(schema, data, WithDefs(CollectDefs(schema))) { return errors.New("data validation failed against the provided schema") } return json.Unmarshal(content, &v) } type validateArgs struct { Defs map[string]Definition } type ValidateOption func(*validateArgs) func WithDefs(defs map[string]Definition) ValidateOption { return func(option *validateArgs) { option.Defs = defs } } func Validate(schema Definition, data any, opts ...ValidateOption) bool { args := validateArgs{} for _, opt := range opts { opt(&args) } if len(opts) == 0 { args.Defs = CollectDefs(schema) } switch schema.Type { case Object: return validateObject(schema, data, args.Defs) case Array: return validateArray(schema, data, args.Defs) case String: v, ok := data.(string) if ok && len(schema.Enum) > 0 { return contains(schema.Enum, v) } return ok case Number: // float64 and int _, ok := data.(float64) if !ok { _, ok = data.(int) } return ok case Boolean: _, ok := data.(bool) return ok case Integer: // Golang unmarshals all numbers as float64, so we need to check if the float64 is an integer if num, ok := data.(float64); ok { return num == float64(int64(num)) } _, ok := data.(int) return ok case Null: return data == nil default: if schema.Ref != "" && args.Defs != nil { if v, ok := args.Defs[schema.Ref]; ok { return Validate(v, data, WithDefs(args.Defs)) } } return false } } func validateObject(schema Definition, data any, defs map[string]Definition) bool { dataMap, ok := data.(map[string]any) if !ok { return false } for _, field := range schema.Required { if _, exists := dataMap[field]; !exists { return false } } for key, valueSchema := range schema.Properties { value, exists := dataMap[key] if exists && !Validate(valueSchema, value, WithDefs(defs)) { return false } else if !exists && contains(schema.Required, key) { return false } } return true } func validateArray(schema Definition, data any, defs map[string]Definition) bool { dataArray, ok := data.([]any) if !ok { return false } for _, item := range dataArray { if !Validate(*schema.Items, item, WithDefs(defs)) { return false } } return true } func contains[S ~[]E, E comparable](s S, v E) bool { for i := range s { if v == s[i] { return true } } return false } ================================================ FILE: jsonschema/validate_test.go ================================================ package jsonschema_test import ( "reflect" "testing" "github.com/sashabaranov/go-openai/jsonschema" ) func Test_Validate(t *testing.T) { type args struct { data any schema jsonschema.Definition } tests := []struct { name string args args want bool }{ // string integer number boolean {"", args{data: "ABC", schema: jsonschema.Definition{Type: jsonschema.String}}, true}, {"", args{data: 123, schema: jsonschema.Definition{Type: jsonschema.String}}, false}, {"", args{data: 123, schema: jsonschema.Definition{Type: jsonschema.Integer}}, true}, {"", args{data: 123.4, schema: jsonschema.Definition{Type: jsonschema.Integer}}, false}, {"", args{data: "ABC", schema: jsonschema.Definition{Type: jsonschema.Number}}, false}, {"", args{data: 123, schema: jsonschema.Definition{Type: jsonschema.Number}}, true}, {"", args{data: false, schema: jsonschema.Definition{Type: jsonschema.Boolean}}, true}, {"", args{data: 123, schema: jsonschema.Definition{Type: jsonschema.Boolean}}, false}, {"", args{data: nil, schema: jsonschema.Definition{Type: jsonschema.Null}}, true}, {"", args{data: 0, schema: jsonschema.Definition{Type: jsonschema.Null}}, false}, // array {"", args{data: []any{"a", "b", "c"}, schema: jsonschema.Definition{ Type: jsonschema.Array, Items: &jsonschema.Definition{Type: jsonschema.String}}, }, true}, {"", args{data: []any{1, 2, 3}, schema: jsonschema.Definition{ Type: jsonschema.Array, Items: &jsonschema.Definition{Type: jsonschema.String}}, }, false}, {"", args{data: []any{1, 2, 3}, schema: jsonschema.Definition{ Type: jsonschema.Array, Items: &jsonschema.Definition{Type: jsonschema.Integer}}, }, true}, {"", args{data: []any{1, 2, 3.4}, schema: jsonschema.Definition{ Type: jsonschema.Array, Items: &jsonschema.Definition{Type: jsonschema.Integer}}, }, false}, // object {"", args{data: map[string]any{ "string": "abc", "integer": 123, "number": 123.4, "boolean": false, "array": []any{1, 2, 3}, }, schema: jsonschema.Definition{Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "string": {Type: jsonschema.String}, "integer": {Type: jsonschema.Integer}, "number": {Type: jsonschema.Number}, "boolean": {Type: jsonschema.Boolean}, "array": {Type: jsonschema.Array, Items: &jsonschema.Definition{Type: jsonschema.Number}}, }, Required: []string{"string"}, }}, true}, {"", args{data: map[string]any{ "integer": 123, "number": 123.4, "boolean": false, "array": []any{1, 2, 3}, }, schema: jsonschema.Definition{Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "string": {Type: jsonschema.String}, "integer": {Type: jsonschema.Integer}, "number": {Type: jsonschema.Number}, "boolean": {Type: jsonschema.Boolean}, "array": {Type: jsonschema.Array, Items: &jsonschema.Definition{Type: jsonschema.Number}}, }, Required: []string{"string"}, }}, false}, { "test schema with ref and defs", args{data: map[string]any{ "person": map[string]any{ "name": "John", "gender": "male", "age": 28, "profile": map[string]any{ "full_name": "John Doe", }, }, }, schema: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "person": {Ref: "#/$defs/Person"}, }, Required: []string{"person"}, Defs: map[string]jsonschema.Definition{ "Person": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "name": {Type: jsonschema.String}, "gender": {Type: jsonschema.String, Enum: []string{"male", "female", "unknown"}}, "age": {Type: jsonschema.Integer}, "profile": {Ref: "#/$defs/Person/$defs/Profile"}, "tweets": {Type: jsonschema.Array, Items: &jsonschema.Definition{Ref: "#/$defs/Tweet"}}, }, Required: []string{"name", "gender", "age", "profile"}, Defs: map[string]jsonschema.Definition{ "Profile": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "full_name": {Type: jsonschema.String}, }, }, }, }, "Tweet": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "text": {Type: jsonschema.String}, "person": {Ref: "#/$defs/Person"}, }, }, }, }}, true}, { "test enum invalid value", args{data: map[string]any{ "person": map[string]any{ "name": "John", "gender": "other", "age": 28, "profile": map[string]any{ "full_name": "John Doe", }, }, }, schema: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "person": {Ref: "#/$defs/Person"}, }, Required: []string{"person"}, Defs: map[string]jsonschema.Definition{ "Person": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "name": {Type: jsonschema.String}, "gender": {Type: jsonschema.String, Enum: []string{"male", "female", "unknown"}}, "age": {Type: jsonschema.Integer}, "profile": {Ref: "#/$defs/Person/$defs/Profile"}, "tweets": {Type: jsonschema.Array, Items: &jsonschema.Definition{Ref: "#/$defs/Tweet"}}, }, Required: []string{"name", "gender", "age", "profile"}, Defs: map[string]jsonschema.Definition{ "Profile": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "full_name": {Type: jsonschema.String}, }, }, }, }, "Tweet": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "text": {Type: jsonschema.String}, "person": {Ref: "#/$defs/Person"}, }, }, }, }}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := jsonschema.Validate(tt.args.schema, tt.args.data); got != tt.want { t.Errorf("Validate() = %v, want %v", got, tt.want) } }) } } func TestUnmarshal(t *testing.T) { type args struct { schema jsonschema.Definition content []byte v any } tests := []struct { name string args args wantErr bool }{ {"", args{ schema: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "string": {Type: jsonschema.String}, "number": {Type: jsonschema.Number}, }, }, content: []byte(`{"string":"abc","number":123.4}`), v: &struct { String string `json:"string"` Number float64 `json:"number"` }{}, }, false}, {"", args{ schema: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "string": {Type: jsonschema.String}, "number": {Type: jsonschema.Number}, }, Required: []string{"string", "number"}, }, content: []byte(`{"string":"abc"}`), v: struct { String string `json:"string"` Number float64 `json:"number"` }{}, }, true}, {"validate integer", args{ schema: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "string": {Type: jsonschema.String}, "integer": {Type: jsonschema.Integer}, }, Required: []string{"string", "integer"}, }, content: []byte(`{"string":"abc","integer":123}`), v: &struct { String string `json:"string"` Integer int `json:"integer"` }{}, }, false}, {"validate integer failed", args{ schema: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "string": {Type: jsonschema.String}, "integer": {Type: jsonschema.Integer}, }, Required: []string{"string", "integer"}, }, content: []byte(`{"string":"abc","integer":123.4}`), v: &struct { String string `json:"string"` Integer int `json:"integer"` }{}, }, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := jsonschema.VerifySchemaAndUnmarshal(tt.args.schema, tt.args.content, tt.args.v) if (err != nil) != tt.wantErr { t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestCollectDefs(t *testing.T) { type args struct { schema jsonschema.Definition } tests := []struct { name string args args want map[string]jsonschema.Definition }{ { "test collect defs", args{ schema: jsonschema.Definition{ Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "person": {Ref: "#/$defs/Person"}, }, Required: []string{"person"}, Defs: map[string]jsonschema.Definition{ "Person": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "name": {Type: jsonschema.String}, "gender": {Type: jsonschema.String, Enum: []string{"male", "female", "unknown"}}, "age": {Type: jsonschema.Integer}, "profile": {Ref: "#/$defs/Person/$defs/Profile"}, "tweets": {Type: jsonschema.Array, Items: &jsonschema.Definition{Ref: "#/$defs/Tweet"}}, }, Required: []string{"name", "gender", "age", "profile"}, Defs: map[string]jsonschema.Definition{ "Profile": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "full_name": {Type: jsonschema.String}, }, }, }, }, "Tweet": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "text": {Type: jsonschema.String}, "person": {Ref: "#/$defs/Person"}, }, }, }, }, }, map[string]jsonschema.Definition{ "#/$defs/Person": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "name": {Type: jsonschema.String}, "gender": {Type: jsonschema.String, Enum: []string{"male", "female", "unknown"}}, "age": {Type: jsonschema.Integer}, "profile": {Ref: "#/$defs/Person/$defs/Profile"}, "tweets": {Type: jsonschema.Array, Items: &jsonschema.Definition{Ref: "#/$defs/Tweet"}}, }, Required: []string{"name", "gender", "age", "profile"}, Defs: map[string]jsonschema.Definition{ "Profile": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "full_name": {Type: jsonschema.String}, }, }, }, }, "#/$defs/Person/$defs/Profile": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "full_name": {Type: jsonschema.String}, }, }, "#/$defs/Tweet": { Type: jsonschema.Object, Properties: map[string]jsonschema.Definition{ "text": {Type: jsonschema.String}, "person": {Ref: "#/$defs/Person"}, }, }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := jsonschema.CollectDefs(tt.args.schema) if !reflect.DeepEqual(got, tt.want) { t.Errorf("CollectDefs() = %v, want %v", got, tt.want) } }) } } ================================================ FILE: messages.go ================================================ package openai import ( "context" "fmt" "net/http" "net/url" ) const ( messagesSuffix = "messages" ) type Message struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int `json:"created_at"` ThreadID string `json:"thread_id"` Role string `json:"role"` Content []MessageContent `json:"content"` FileIds []string `json:"file_ids"` //nolint:revive //backwards-compatibility AssistantID *string `json:"assistant_id,omitempty"` RunID *string `json:"run_id,omitempty"` Metadata map[string]any `json:"metadata"` httpHeader } type MessagesList struct { Messages []Message `json:"data"` Object string `json:"object"` FirstID *string `json:"first_id"` LastID *string `json:"last_id"` HasMore bool `json:"has_more"` httpHeader } type MessageContent struct { Type string `json:"type"` Text *MessageText `json:"text,omitempty"` ImageFile *ImageFile `json:"image_file,omitempty"` ImageURL *ImageURL `json:"image_url,omitempty"` } type MessageText struct { Value string `json:"value"` Annotations []any `json:"annotations"` } type ImageFile struct { FileID string `json:"file_id"` } type ImageURL struct { URL string `json:"url"` Detail string `json:"detail"` } type MessageRequest struct { Role string `json:"role"` Content string `json:"content"` FileIds []string `json:"file_ids,omitempty"` //nolint:revive // backwards-compatibility Metadata map[string]any `json:"metadata,omitempty"` Attachments []ThreadAttachment `json:"attachments,omitempty"` } type MessageFile struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int `json:"created_at"` MessageID string `json:"message_id"` httpHeader } type MessageFilesList struct { MessageFiles []MessageFile `json:"data"` httpHeader } type MessageDeletionStatus struct { ID string `json:"id"` Object string `json:"object"` Deleted bool `json:"deleted"` httpHeader } // CreateMessage creates a new message. func (c *Client) CreateMessage(ctx context.Context, threadID string, request MessageRequest) (msg Message, err error) { urlSuffix := fmt.Sprintf("/threads/%s/%s", threadID, messagesSuffix) req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &msg) return } // ListMessage fetches all messages in the thread. func (c *Client) ListMessage(ctx context.Context, threadID string, limit *int, order *string, after *string, before *string, runID *string, ) (messages MessagesList, err error) { urlValues := url.Values{} if limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *limit)) } if order != nil { urlValues.Add("order", *order) } if after != nil { urlValues.Add("after", *after) } if before != nil { urlValues.Add("before", *before) } if runID != nil { urlValues.Add("run_id", *runID) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("/threads/%s/%s%s", threadID, messagesSuffix, encodedValues) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &messages) return } // RetrieveMessage retrieves a Message. func (c *Client) RetrieveMessage( ctx context.Context, threadID, messageID string, ) (msg Message, err error) { urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &msg) return } // ModifyMessage modifies a message. func (c *Client) ModifyMessage( ctx context.Context, threadID, messageID string, metadata map[string]string, ) (msg Message, err error) { urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID) req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(map[string]any{"metadata": metadata}), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &msg) return } // RetrieveMessageFile fetches a message file. func (c *Client) RetrieveMessageFile( ctx context.Context, threadID, messageID, fileID string, ) (file MessageFile, err error) { urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files/%s", threadID, messagesSuffix, messageID, fileID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &file) return } // ListMessageFiles fetches all files attached to a message. func (c *Client) ListMessageFiles( ctx context.Context, threadID, messageID string, ) (files MessageFilesList, err error) { urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files", threadID, messagesSuffix, messageID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &files) return } // DeleteMessage deletes a message.. func (c *Client) DeleteMessage( ctx context.Context, threadID, messageID string, ) (status MessageDeletionStatus, err error) { urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID) req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &status) return } ================================================ FILE: messages_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "net/http" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test" "github.com/sashabaranov/go-openai/internal/test/checks" ) var emptyStr = "" func setupServerForTestMessage(t *testing.T, server *test.ServerTest) { threadID := "thread_abc123" messageID := "msg_abc123" fileID := "file_abc123" server.RegisterHandler( "/v1/threads/"+threadID+"/messages/"+messageID+"/files/"+fileID, func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: resBytes, _ := json.Marshal( openai.MessageFile{ ID: fileID, Object: "thread.message.file", CreatedAt: 1699061776, MessageID: messageID, }) fmt.Fprintln(w, string(resBytes)) default: t.Fatalf("unsupported messages http method: %s", r.Method) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/messages/"+messageID+"/files", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: resBytes, _ := json.Marshal( openai.MessageFilesList{MessageFiles: []openai.MessageFile{{ ID: fileID, Object: "thread.message.file", CreatedAt: 0, MessageID: messageID, }}}) fmt.Fprintln(w, string(resBytes)) default: t.Fatalf("unsupported messages http method: %s", r.Method) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/messages/"+messageID, func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: metadata := map[string]any{} err := json.NewDecoder(r.Body).Decode(&metadata) checks.NoError(t, err, "unable to decode metadata in modify message call") payload, ok := metadata["metadata"].(map[string]any) if !ok { t.Fatalf("metadata payload improperly wrapped %+v", metadata) } resBytes, _ := json.Marshal( openai.Message{ ID: messageID, Object: "thread.message", CreatedAt: 1234567890, ThreadID: threadID, Role: "user", Content: []openai.MessageContent{{ Type: "text", Text: &openai.MessageText{ Value: "How does AI work?", Annotations: nil, }, }}, FileIds: nil, AssistantID: &emptyStr, RunID: &emptyStr, Metadata: payload, }) fmt.Fprintln(w, string(resBytes)) case http.MethodGet: resBytes, _ := json.Marshal( openai.Message{ ID: messageID, Object: "thread.message", CreatedAt: 1234567890, ThreadID: threadID, Role: "user", Content: []openai.MessageContent{{ Type: "text", Text: &openai.MessageText{ Value: "How does AI work?", Annotations: nil, }, }}, FileIds: nil, AssistantID: &emptyStr, RunID: &emptyStr, Metadata: nil, }) fmt.Fprintln(w, string(resBytes)) case http.MethodDelete: resBytes, _ := json.Marshal(openai.MessageDeletionStatus{ ID: messageID, Object: "thread.message.deleted", Deleted: true, }) fmt.Fprintln(w, string(resBytes)) default: t.Fatalf("unsupported messages http method: %s", r.Method) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/messages", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: resBytes, _ := json.Marshal(openai.Message{ ID: messageID, Object: "thread.message", CreatedAt: 1234567890, ThreadID: threadID, Role: "user", Content: []openai.MessageContent{{ Type: "text", Text: &openai.MessageText{ Value: "How does AI work?", Annotations: nil, }, }}, FileIds: nil, AssistantID: &emptyStr, RunID: &emptyStr, Metadata: nil, }) fmt.Fprintln(w, string(resBytes)) case http.MethodGet: resBytes, _ := json.Marshal(openai.MessagesList{ Object: "list", Messages: []openai.Message{{ ID: messageID, Object: "thread.message", CreatedAt: 1234567890, ThreadID: threadID, Role: "user", Content: []openai.MessageContent{{ Type: "text", Text: &openai.MessageText{ Value: "How does AI work?", Annotations: nil, }, }}, FileIds: nil, AssistantID: &emptyStr, RunID: &emptyStr, Metadata: nil, }}, FirstID: &messageID, LastID: &messageID, HasMore: false, }) fmt.Fprintln(w, string(resBytes)) default: t.Fatalf("unsupported messages http method: %s", r.Method) } }, ) } // TestMessages Tests the messages endpoint of the API using the mocked server. func TestMessages(t *testing.T) { threadID := "thread_abc123" messageID := "msg_abc123" fileID := "file_abc123" client, server, teardown := setupOpenAITestServer() defer teardown() setupServerForTestMessage(t, server) ctx := context.Background() // static assertion of return type var msg openai.Message msg, err := client.CreateMessage(ctx, threadID, openai.MessageRequest{ Role: "user", Content: "How does AI work?", FileIds: nil, Metadata: nil, }) checks.NoError(t, err, "CreateMessage error") if msg.ID != messageID { t.Fatalf("unexpected message id: '%s'", msg.ID) } var msgs openai.MessagesList msgs, err = client.ListMessage(ctx, threadID, nil, nil, nil, nil, nil) checks.NoError(t, err, "ListMessages error") if len(msgs.Messages) != 1 { t.Fatalf("unexpected length of fetched messages") } // with pagination options set limit := 1 order := "desc" after := "obj_foo" before := "obj_bar" runID := "run_abc123" msgs, err = client.ListMessage(ctx, threadID, &limit, &order, &after, &before, &runID) checks.NoError(t, err, "ListMessages error") if len(msgs.Messages) != 1 { t.Fatalf("unexpected length of fetched messages") } msg, err = client.RetrieveMessage(ctx, threadID, messageID) checks.NoError(t, err, "RetrieveMessage error") if msg.ID != messageID { t.Fatalf("unexpected message id: '%s'", msg.ID) } msg, err = client.ModifyMessage(ctx, threadID, messageID, map[string]string{ "foo": "bar", }) checks.NoError(t, err, "ModifyMessage error") if msg.Metadata["foo"] != "bar" { t.Fatalf("expected message metadata to get modified") } msgDel, err := client.DeleteMessage(ctx, threadID, messageID) checks.NoError(t, err, "DeleteMessage error") if msgDel.ID != messageID { t.Fatalf("unexpected message id: '%s'", msg.ID) } if !msgDel.Deleted { t.Fatalf("expected deleted is true") } _, err = client.DeleteMessage(ctx, threadID, "not_exist_id") checks.HasError(t, err, "DeleteMessage error") // message files var msgFile openai.MessageFile msgFile, err = client.RetrieveMessageFile(ctx, threadID, messageID, fileID) checks.NoError(t, err, "RetrieveMessageFile error") if msgFile.ID != fileID { t.Fatalf("unexpected message file id: '%s'", msgFile.ID) } var msgFiles openai.MessageFilesList msgFiles, err = client.ListMessageFiles(ctx, threadID, messageID) checks.NoError(t, err, "RetrieveMessageFile error") if len(msgFiles.MessageFiles) != 1 { t.Fatalf("unexpected count of message files: %d", len(msgFiles.MessageFiles)) } if msgFiles.MessageFiles[0].ID != fileID { t.Fatalf("unexpected message file id: '%s' in list message files", msgFiles.MessageFiles[0].ID) } } ================================================ FILE: models.go ================================================ package openai import ( "context" "fmt" "net/http" ) // Model struct represents an OpenAPI model. type Model struct { CreatedAt int64 `json:"created"` ID string `json:"id"` Object string `json:"object"` OwnedBy string `json:"owned_by"` Permission []Permission `json:"permission"` Root string `json:"root"` Parent string `json:"parent"` httpHeader } // Permission struct represents an OpenAPI permission. type Permission struct { CreatedAt int64 `json:"created"` ID string `json:"id"` Object string `json:"object"` AllowCreateEngine bool `json:"allow_create_engine"` AllowSampling bool `json:"allow_sampling"` AllowLogprobs bool `json:"allow_logprobs"` AllowSearchIndices bool `json:"allow_search_indices"` AllowView bool `json:"allow_view"` AllowFineTuning bool `json:"allow_fine_tuning"` Organization string `json:"organization"` Group interface{} `json:"group"` IsBlocking bool `json:"is_blocking"` } // FineTuneModelDeleteResponse represents the deletion status of a fine-tuned model. type FineTuneModelDeleteResponse struct { ID string `json:"id"` Object string `json:"object"` Deleted bool `json:"deleted"` httpHeader } // ModelsList is a list of models, including those that belong to the user or organization. type ModelsList struct { Models []Model `json:"data"` httpHeader } // ListModels Lists the currently available models, // and provides basic information about each model such as the model id and parent. func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error) { req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/models")) if err != nil { return } err = c.sendRequest(req, &models) return } // GetModel Retrieves a model instance, providing basic information about // the model such as the owner and permissioning. func (c *Client) GetModel(ctx context.Context, modelID string) (model Model, err error) { urlSuffix := fmt.Sprintf("/models/%s", modelID) req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } err = c.sendRequest(req, &model) return } // DeleteFineTuneModel Deletes a fine-tune model. You must have the Owner // role in your organization to delete a model. func (c *Client) DeleteFineTuneModel(ctx context.Context, modelID string) ( response FineTuneModelDeleteResponse, err error) { req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/models/"+modelID)) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: models_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "net/http" "os" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) const testFineTuneModelID = "fine-tune-model-id" // TestListModels Tests the list models endpoint of the API using the mocked server. func TestListModels(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/models", handleListModelsEndpoint) _, err := client.ListModels(context.Background()) checks.NoError(t, err, "ListModels error") } func TestAzureListModels(t *testing.T) { client, server, teardown := setupAzureTestServer() defer teardown() server.RegisterHandler("/openai/models", handleListModelsEndpoint) _, err := client.ListModels(context.Background()) checks.NoError(t, err, "ListModels error") } // handleListModelsEndpoint Handles the list models endpoint by the test server. func handleListModelsEndpoint(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.ModelsList{}) fmt.Fprintln(w, string(resBytes)) } // TestGetModel Tests the retrieve model endpoint of the API using the mocked server. func TestGetModel(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/models/text-davinci-003", handleGetModelEndpoint) _, err := client.GetModel(context.Background(), "text-davinci-003") checks.NoError(t, err, "GetModel error") } // TestGetModelO3 Tests the retrieve O3 model endpoint of the API using the mocked server. func TestGetModelO3(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/models/o3", handleGetModelEndpoint) _, err := client.GetModel(context.Background(), "o3") checks.NoError(t, err, "GetModel error for O3") } // TestGetModelO4Mini Tests the retrieve O4Mini model endpoint of the API using the mocked server. func TestGetModelO4Mini(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/models/o4-mini", handleGetModelEndpoint) _, err := client.GetModel(context.Background(), "o4-mini") checks.NoError(t, err, "GetModel error for O4Mini") } func TestAzureGetModel(t *testing.T) { client, server, teardown := setupAzureTestServer() defer teardown() server.RegisterHandler("/openai/models/text-davinci-003", handleGetModelEndpoint) _, err := client.GetModel(context.Background(), "text-davinci-003") checks.NoError(t, err, "GetModel error") } // handleGetModelsEndpoint Handles the get model endpoint by the test server. func handleGetModelEndpoint(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.Model{}) fmt.Fprintln(w, string(resBytes)) } func TestGetModelReturnTimeoutError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/models/text-davinci-003", func(http.ResponseWriter, *http.Request) { time.Sleep(10 * time.Nanosecond) }) ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, time.Nanosecond) defer cancel() _, err := client.GetModel(ctx, "text-davinci-003") if err == nil { t.Fatal("Did not return error") } if !os.IsTimeout(err) { t.Fatal("Did not return timeout error") } } func TestDeleteFineTuneModel(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/models/"+testFineTuneModelID, handleDeleteFineTuneModelEndpoint) _, err := client.DeleteFineTuneModel(context.Background(), testFineTuneModelID) checks.NoError(t, err, "DeleteFineTuneModel error") } func handleDeleteFineTuneModelEndpoint(w http.ResponseWriter, _ *http.Request) { resBytes, _ := json.Marshal(openai.FineTuneModelDeleteResponse{}) fmt.Fprintln(w, string(resBytes)) } ================================================ FILE: moderation.go ================================================ package openai import ( "context" "errors" "net/http" ) // The moderation endpoint is a tool you can use to check whether content complies with OpenAI's usage policies. // Developers can thus identify content that our usage policies prohibits and take action, for instance by filtering it. // The default is text-moderation-latest which will be automatically upgraded over time. // This ensures you are always using our most accurate model. // If you use text-moderation-stable, we will provide advanced notice before updating the model. // Accuracy of text-moderation-stable may be slightly lower than for text-moderation-latest. const ( ModerationOmniLatest = "omni-moderation-latest" ModerationOmni20240926 = "omni-moderation-2024-09-26" ModerationTextStable = "text-moderation-stable" ModerationTextLatest = "text-moderation-latest" // Deprecated: use ModerationTextStable and ModerationTextLatest instead. ModerationText001 = "text-moderation-001" ) var ( ErrModerationInvalidModel = errors.New("this model is not supported with moderation, please use text-moderation-stable or text-moderation-latest instead") //nolint:lll ) var validModerationModel = map[string]struct{}{ ModerationOmniLatest: {}, ModerationOmni20240926: {}, ModerationTextStable: {}, ModerationTextLatest: {}, } // ModerationRequest represents a request structure for moderation API. type ModerationRequest struct { Input string `json:"input,omitempty"` Model string `json:"model,omitempty"` } // Result represents one of possible moderation results. type Result struct { Categories ResultCategories `json:"categories"` CategoryScores ResultCategoryScores `json:"category_scores"` Flagged bool `json:"flagged"` } // ResultCategories represents Categories of Result. type ResultCategories struct { Hate bool `json:"hate"` HateThreatening bool `json:"hate/threatening"` Harassment bool `json:"harassment"` HarassmentThreatening bool `json:"harassment/threatening"` SelfHarm bool `json:"self-harm"` SelfHarmIntent bool `json:"self-harm/intent"` SelfHarmInstructions bool `json:"self-harm/instructions"` Sexual bool `json:"sexual"` SexualMinors bool `json:"sexual/minors"` Violence bool `json:"violence"` ViolenceGraphic bool `json:"violence/graphic"` } // ResultCategoryScores represents CategoryScores of Result. type ResultCategoryScores struct { Hate float32 `json:"hate"` HateThreatening float32 `json:"hate/threatening"` Harassment float32 `json:"harassment"` HarassmentThreatening float32 `json:"harassment/threatening"` SelfHarm float32 `json:"self-harm"` SelfHarmIntent float32 `json:"self-harm/intent"` SelfHarmInstructions float32 `json:"self-harm/instructions"` Sexual float32 `json:"sexual"` SexualMinors float32 `json:"sexual/minors"` Violence float32 `json:"violence"` ViolenceGraphic float32 `json:"violence/graphic"` } // ModerationResponse represents a response structure for moderation API. type ModerationResponse struct { ID string `json:"id"` Model string `json:"model"` Results []Result `json:"results"` httpHeader } // Moderations — perform a moderation api call over a string. // Input can be an array or slice but a string will reduce the complexity. func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error) { if _, ok := validModerationModel[request.Model]; len(request.Model) > 0 && !ok { err = ErrModerationInvalidModel return } req, err := c.newRequest( ctx, http.MethodPost, c.fullURL("/moderations", withModel(request.Model)), withBody(&request), ) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: moderation_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "io" "net/http" "strconv" "strings" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) // TestModeration Tests the moderations endpoint of the API using the mocked server. func TestModerations(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/moderations", handleModerationEndpoint) _, err := client.Moderations(context.Background(), openai.ModerationRequest{ Model: openai.ModerationTextStable, Input: "I want to kill them.", }) checks.NoError(t, err, "Moderation error") } // TestModerationsWithIncorrectModel Tests passing valid and invalid models to moderations endpoint. func TestModerationsWithDifferentModelOptions(t *testing.T) { var modelOptions []struct { model string expect error } modelOptions = append(modelOptions, getModerationModelTestOption(openai.GPT3Dot5Turbo, openai.ErrModerationInvalidModel), getModerationModelTestOption(openai.ModerationTextStable, nil), getModerationModelTestOption(openai.ModerationTextLatest, nil), getModerationModelTestOption(openai.ModerationOmni20240926, nil), getModerationModelTestOption(openai.ModerationOmniLatest, nil), getModerationModelTestOption("", nil), ) client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/moderations", handleModerationEndpoint) for _, modelTest := range modelOptions { _, err := client.Moderations(context.Background(), openai.ModerationRequest{ Model: modelTest.model, Input: "I want to kill them.", }) checks.ErrorIs(t, err, modelTest.expect, fmt.Sprintf("Moderations(..) expects err: %v, actual err:%v", modelTest.expect, err)) } } func getModerationModelTestOption(model string, expect error) struct { model string expect error } { return struct { model string expect error }{model: model, expect: expect} } // handleModerationEndpoint Handles the moderation endpoint by the test server. func handleModerationEndpoint(w http.ResponseWriter, r *http.Request) { var err error var resBytes []byte // completions only accepts POST requests if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } var moderationReq openai.ModerationRequest if moderationReq, err = getModerationBody(r); err != nil { http.Error(w, "could not read request", http.StatusInternalServerError) return } resCat := openai.ResultCategories{} resCatScore := openai.ResultCategoryScores{} switch { case strings.Contains(moderationReq.Input, "hate"): resCat = openai.ResultCategories{Hate: true} resCatScore = openai.ResultCategoryScores{Hate: 1} case strings.Contains(moderationReq.Input, "hate more"): resCat = openai.ResultCategories{HateThreatening: true} resCatScore = openai.ResultCategoryScores{HateThreatening: 1} case strings.Contains(moderationReq.Input, "harass"): resCat = openai.ResultCategories{Harassment: true} resCatScore = openai.ResultCategoryScores{Harassment: 1} case strings.Contains(moderationReq.Input, "harass hard"): resCat = openai.ResultCategories{Harassment: true} resCatScore = openai.ResultCategoryScores{HarassmentThreatening: 1} case strings.Contains(moderationReq.Input, "suicide"): resCat = openai.ResultCategories{SelfHarm: true} resCatScore = openai.ResultCategoryScores{SelfHarm: 1} case strings.Contains(moderationReq.Input, "wanna suicide"): resCat = openai.ResultCategories{SelfHarmIntent: true} resCatScore = openai.ResultCategoryScores{SelfHarm: 1} case strings.Contains(moderationReq.Input, "drink bleach"): resCat = openai.ResultCategories{SelfHarmInstructions: true} resCatScore = openai.ResultCategoryScores{SelfHarmInstructions: 1} case strings.Contains(moderationReq.Input, "porn"): resCat = openai.ResultCategories{Sexual: true} resCatScore = openai.ResultCategoryScores{Sexual: 1} case strings.Contains(moderationReq.Input, "child porn"): resCat = openai.ResultCategories{SexualMinors: true} resCatScore = openai.ResultCategoryScores{SexualMinors: 1} case strings.Contains(moderationReq.Input, "kill"): resCat = openai.ResultCategories{Violence: true} resCatScore = openai.ResultCategoryScores{Violence: 1} case strings.Contains(moderationReq.Input, "corpse"): resCat = openai.ResultCategories{ViolenceGraphic: true} resCatScore = openai.ResultCategoryScores{ViolenceGraphic: 1} } result := openai.Result{Categories: resCat, CategoryScores: resCatScore, Flagged: true} res := openai.ModerationResponse{ ID: strconv.Itoa(int(time.Now().Unix())), Model: moderationReq.Model, } res.Results = append(res.Results, result) resBytes, _ = json.Marshal(res) fmt.Fprintln(w, string(resBytes)) } // getModerationBody Returns the body of the request to do a moderation. func getModerationBody(r *http.Request) (openai.ModerationRequest, error) { moderation := openai.ModerationRequest{} // read the request body reqBody, err := io.ReadAll(r.Body) if err != nil { return openai.ModerationRequest{}, err } err = json.Unmarshal(reqBody, &moderation) if err != nil { return openai.ModerationRequest{}, err } return moderation, nil } ================================================ FILE: openai_test.go ================================================ package openai_test import ( "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test" ) func setupOpenAITestServer() (client *openai.Client, server *test.ServerTest, teardown func()) { server = test.NewTestServer() ts := server.OpenAITestServer() ts.Start() teardown = ts.Close config := openai.DefaultConfig(test.GetTestToken()) config.BaseURL = ts.URL + "/v1" client = openai.NewClientWithConfig(config) return } func setupAzureTestServer() (client *openai.Client, server *test.ServerTest, teardown func()) { server = test.NewTestServer() ts := server.OpenAITestServer() ts.Start() teardown = ts.Close config := openai.DefaultAzureConfig(test.GetTestToken(), "https://dummylab.openai.azure.com/") config.BaseURL = ts.URL client = openai.NewClientWithConfig(config) return } // numTokens Returns the number of GPT-3 encoded tokens in the given text. // This function approximates based on the rule of thumb stated by OpenAI: // https://beta.openai.com/tokenizer. // // TODO: implement an actual tokenizer for GPT-3 and Codex (once available). func numTokens(s string) int { return int(float32(len(s)) / 4) } ================================================ FILE: ratelimit.go ================================================ package openai import ( "net/http" "strconv" "time" ) // RateLimitHeaders struct represents Openai rate limits headers. type RateLimitHeaders struct { LimitRequests int `json:"x-ratelimit-limit-requests"` LimitTokens int `json:"x-ratelimit-limit-tokens"` RemainingRequests int `json:"x-ratelimit-remaining-requests"` RemainingTokens int `json:"x-ratelimit-remaining-tokens"` ResetRequests ResetTime `json:"x-ratelimit-reset-requests"` ResetTokens ResetTime `json:"x-ratelimit-reset-tokens"` } type ResetTime string func (r ResetTime) String() string { return string(r) } func (r ResetTime) Time() time.Time { d, _ := time.ParseDuration(string(r)) return time.Now().Add(d) } func newRateLimitHeaders(h http.Header) RateLimitHeaders { limitReq, _ := strconv.Atoi(h.Get("x-ratelimit-limit-requests")) limitTokens, _ := strconv.Atoi(h.Get("x-ratelimit-limit-tokens")) remainingReq, _ := strconv.Atoi(h.Get("x-ratelimit-remaining-requests")) remainingTokens, _ := strconv.Atoi(h.Get("x-ratelimit-remaining-tokens")) return RateLimitHeaders{ LimitRequests: limitReq, LimitTokens: limitTokens, RemainingRequests: remainingReq, RemainingTokens: remainingTokens, ResetRequests: ResetTime(h.Get("x-ratelimit-reset-requests")), ResetTokens: ResetTime(h.Get("x-ratelimit-reset-tokens")), } } ================================================ FILE: reasoning_validator.go ================================================ package openai import ( "errors" "strings" ) var ( // Deprecated: use ErrReasoningModelMaxTokensDeprecated instead. ErrO1MaxTokensDeprecated = errors.New("this model is not supported MaxTokens, please use MaxCompletionTokens") //nolint:lll ErrCompletionUnsupportedModel = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll ErrCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateCompletionStream") //nolint:lll ErrCompletionRequestPromptTypeNotSupported = errors.New("the type of CompletionRequest.Prompt only supports string and []string") //nolint:lll ) var ( ErrO1BetaLimitationsMessageTypes = errors.New("this model has beta-limitations, user and assistant messages only, system messages are not supported") //nolint:lll ErrO1BetaLimitationsTools = errors.New("this model has beta-limitations, tools, function calling, and response format parameters are not supported") //nolint:lll // Deprecated: use ErrReasoningModelLimitations* instead. ErrO1BetaLimitationsLogprobs = errors.New("this model has beta-limitations, logprobs not supported") //nolint:lll ErrO1BetaLimitationsOther = errors.New("this model has beta-limitations, temperature, top_p and n are fixed at 1, while presence_penalty and frequency_penalty are fixed at 0") //nolint:lll ) var ( //nolint:lll ErrReasoningModelMaxTokensDeprecated = errors.New("this model is not supported MaxTokens, please use MaxCompletionTokens") ErrReasoningModelLimitationsLogprobs = errors.New("this model has beta-limitations, logprobs not supported") //nolint:lll ErrReasoningModelLimitationsOther = errors.New("this model has beta-limitations, temperature, top_p and n are fixed at 1, while presence_penalty and frequency_penalty are fixed at 0") //nolint:lll ) // ReasoningValidator handles validation for reasoning model requests. type ReasoningValidator struct{} // NewReasoningValidator creates a new validator for reasoning models. func NewReasoningValidator() *ReasoningValidator { return &ReasoningValidator{} } // Validate performs all validation checks for reasoning models. func (v *ReasoningValidator) Validate(request ChatCompletionRequest) error { o1Series := strings.HasPrefix(request.Model, "o1") o3Series := strings.HasPrefix(request.Model, "o3") o4Series := strings.HasPrefix(request.Model, "o4") gpt5Series := strings.HasPrefix(request.Model, "gpt-5") if !o1Series && !o3Series && !o4Series && !gpt5Series { return nil } if err := v.validateReasoningModelParams(request); err != nil { return err } return nil } // validateReasoningModelParams checks reasoning model parameters. func (v *ReasoningValidator) validateReasoningModelParams(request ChatCompletionRequest) error { if request.MaxTokens > 0 { return ErrReasoningModelMaxTokensDeprecated } if request.LogProbs { return ErrReasoningModelLimitationsLogprobs } if request.Temperature > 0 && request.Temperature != 1 { return ErrReasoningModelLimitationsOther } if request.TopP > 0 && request.TopP != 1 { return ErrReasoningModelLimitationsOther } if request.N > 0 && request.N != 1 { return ErrReasoningModelLimitationsOther } if request.PresencePenalty > 0 { return ErrReasoningModelLimitationsOther } if request.FrequencyPenalty > 0 { return ErrReasoningModelLimitationsOther } return nil } ================================================ FILE: run.go ================================================ package openai import ( "context" "fmt" "net/http" "net/url" ) type Run struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` ThreadID string `json:"thread_id"` AssistantID string `json:"assistant_id"` Status RunStatus `json:"status"` RequiredAction *RunRequiredAction `json:"required_action,omitempty"` LastError *RunLastError `json:"last_error,omitempty"` ExpiresAt int64 `json:"expires_at"` StartedAt *int64 `json:"started_at,omitempty"` CancelledAt *int64 `json:"cancelled_at,omitempty"` FailedAt *int64 `json:"failed_at,omitempty"` CompletedAt *int64 `json:"completed_at,omitempty"` Model string `json:"model"` Instructions string `json:"instructions,omitempty"` Tools []Tool `json:"tools"` FileIDS []string `json:"file_ids"` //nolint:revive // backwards-compatibility Metadata map[string]any `json:"metadata"` Usage Usage `json:"usage,omitempty"` Temperature *float32 `json:"temperature,omitempty"` // The maximum number of prompt tokens that may be used over the course of the run. // If the run exceeds the number of prompt tokens specified, the run will end with status 'incomplete'. MaxPromptTokens int `json:"max_prompt_tokens,omitempty"` // The maximum number of completion tokens that may be used over the course of the run. // If the run exceeds the number of completion tokens specified, the run will end with status 'incomplete'. MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` // ThreadTruncationStrategy defines the truncation strategy to use for the thread. TruncationStrategy *ThreadTruncationStrategy `json:"truncation_strategy,omitempty"` httpHeader } type RunStatus string const ( RunStatusQueued RunStatus = "queued" RunStatusInProgress RunStatus = "in_progress" RunStatusRequiresAction RunStatus = "requires_action" RunStatusCancelling RunStatus = "cancelling" RunStatusFailed RunStatus = "failed" RunStatusCompleted RunStatus = "completed" RunStatusIncomplete RunStatus = "incomplete" RunStatusExpired RunStatus = "expired" RunStatusCancelled RunStatus = "cancelled" ) type RunRequiredAction struct { Type RequiredActionType `json:"type"` SubmitToolOutputs *SubmitToolOutputs `json:"submit_tool_outputs,omitempty"` } type RequiredActionType string const ( RequiredActionTypeSubmitToolOutputs RequiredActionType = "submit_tool_outputs" ) type SubmitToolOutputs struct { ToolCalls []ToolCall `json:"tool_calls"` } type RunLastError struct { Code RunError `json:"code"` Message string `json:"message"` } type RunError string const ( RunErrorServerError RunError = "server_error" RunErrorRateLimitExceeded RunError = "rate_limit_exceeded" ) type RunRequest struct { AssistantID string `json:"assistant_id"` Model string `json:"model,omitempty"` Instructions string `json:"instructions,omitempty"` AdditionalInstructions string `json:"additional_instructions,omitempty"` AdditionalMessages []ThreadMessage `json:"additional_messages,omitempty"` Tools []Tool `json:"tools,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` // Sampling temperature between 0 and 2. Higher values like 0.8 are more random. // lower values are more focused and deterministic. Temperature *float32 `json:"temperature,omitempty"` TopP *float32 `json:"top_p,omitempty"` // The maximum number of prompt tokens that may be used over the course of the run. // If the run exceeds the number of prompt tokens specified, the run will end with status 'incomplete'. MaxPromptTokens int `json:"max_prompt_tokens,omitempty"` // The maximum number of completion tokens that may be used over the course of the run. // If the run exceeds the number of completion tokens specified, the run will end with status 'incomplete'. MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` // ThreadTruncationStrategy defines the truncation strategy to use for the thread. TruncationStrategy *ThreadTruncationStrategy `json:"truncation_strategy,omitempty"` // This can be either a string or a ToolChoice object. ToolChoice any `json:"tool_choice,omitempty"` // This can be either a string or a ResponseFormat object. ResponseFormat any `json:"response_format,omitempty"` // Disable the default behavior of parallel tool calls by setting it: false. ParallelToolCalls any `json:"parallel_tool_calls,omitempty"` } // ThreadTruncationStrategy defines the truncation strategy to use for the thread. // https://platform.openai.com/docs/assistants/how-it-works/truncation-strategy. type ThreadTruncationStrategy struct { // default 'auto'. Type TruncationStrategy `json:"type,omitempty"` // this field should be set if the truncation strategy is set to LastMessages. LastMessages *int `json:"last_messages,omitempty"` } // TruncationStrategy defines the existing truncation strategies existing for thread management in an assistant. type TruncationStrategy string const ( // TruncationStrategyAuto messages in the middle of the thread will be dropped to fit the context length of the model. TruncationStrategyAuto = TruncationStrategy("auto") // TruncationStrategyLastMessages the thread will be truncated to the n most recent messages in the thread. TruncationStrategyLastMessages = TruncationStrategy("last_messages") ) // ReponseFormat specifies the format the model must output. // https://platform.openai.com/docs/api-reference/runs/createRun#runs-createrun-response_format. // Type can either be text or json_object. type ReponseFormat struct { Type string `json:"type"` } type RunModifyRequest struct { Metadata map[string]any `json:"metadata,omitempty"` } // RunList is a list of runs. type RunList struct { Runs []Run `json:"data"` httpHeader } type SubmitToolOutputsRequest struct { ToolOutputs []ToolOutput `json:"tool_outputs"` } type ToolOutput struct { ToolCallID string `json:"tool_call_id"` Output any `json:"output"` } type CreateThreadAndRunRequest struct { RunRequest Thread ThreadRequest `json:"thread"` } type RunStep struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` AssistantID string `json:"assistant_id"` ThreadID string `json:"thread_id"` RunID string `json:"run_id"` Type RunStepType `json:"type"` Status RunStepStatus `json:"status"` StepDetails StepDetails `json:"step_details"` LastError *RunLastError `json:"last_error,omitempty"` ExpiredAt *int64 `json:"expired_at,omitempty"` CancelledAt *int64 `json:"cancelled_at,omitempty"` FailedAt *int64 `json:"failed_at,omitempty"` CompletedAt *int64 `json:"completed_at,omitempty"` Metadata map[string]any `json:"metadata"` httpHeader } type RunStepStatus string const ( RunStepStatusInProgress RunStepStatus = "in_progress" RunStepStatusCancelling RunStepStatus = "cancelled" RunStepStatusFailed RunStepStatus = "failed" RunStepStatusCompleted RunStepStatus = "completed" RunStepStatusExpired RunStepStatus = "expired" ) type RunStepType string const ( RunStepTypeMessageCreation RunStepType = "message_creation" RunStepTypeToolCalls RunStepType = "tool_calls" ) type StepDetails struct { Type RunStepType `json:"type"` MessageCreation *StepDetailsMessageCreation `json:"message_creation,omitempty"` ToolCalls []ToolCall `json:"tool_calls,omitempty"` } type StepDetailsMessageCreation struct { MessageID string `json:"message_id"` } // RunStepList is a list of steps. type RunStepList struct { RunSteps []RunStep `json:"data"` FirstID string `json:"first_id"` LastID string `json:"last_id"` HasMore bool `json:"has_more"` httpHeader } type Pagination struct { Limit *int Order *string After *string Before *string } // CreateRun creates a new run. func (c *Client) CreateRun( ctx context.Context, threadID string, request RunRequest, ) (response Run, err error) { urlSuffix := fmt.Sprintf("/threads/%s/runs", threadID) req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // RetrieveRun retrieves a run. func (c *Client) RetrieveRun( ctx context.Context, threadID string, runID string, ) (response Run, err error) { urlSuffix := fmt.Sprintf("/threads/%s/runs/%s", threadID, runID) req, err := c.newRequest( ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // ModifyRun modifies a run. func (c *Client) ModifyRun( ctx context.Context, threadID string, runID string, request RunModifyRequest, ) (response Run, err error) { urlSuffix := fmt.Sprintf("/threads/%s/runs/%s", threadID, runID) req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // ListRuns lists runs. func (c *Client) ListRuns( ctx context.Context, threadID string, pagination Pagination, ) (response RunList, err error) { urlValues := url.Values{} if pagination.Limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) } if pagination.Order != nil { urlValues.Add("order", *pagination.Order) } if pagination.After != nil { urlValues.Add("after", *pagination.After) } if pagination.Before != nil { urlValues.Add("before", *pagination.Before) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("/threads/%s/runs%s", threadID, encodedValues) req, err := c.newRequest( ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // SubmitToolOutputs submits tool outputs. func (c *Client) SubmitToolOutputs( ctx context.Context, threadID string, runID string, request SubmitToolOutputsRequest) (response Run, err error) { urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/submit_tool_outputs", threadID, runID) req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // CancelRun cancels a run. func (c *Client) CancelRun( ctx context.Context, threadID string, runID string) (response Run, err error) { urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/cancel", threadID, runID) req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // CreateThreadAndRun submits tool outputs. func (c *Client) CreateThreadAndRun( ctx context.Context, request CreateThreadAndRunRequest) (response Run, err error) { urlSuffix := "/threads/runs" req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // RetrieveRunStep retrieves a run step. func (c *Client) RetrieveRunStep( ctx context.Context, threadID string, runID string, stepID string, ) (response RunStep, err error) { urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/steps/%s", threadID, runID, stepID) req, err := c.newRequest( ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // ListRunSteps lists run steps. func (c *Client) ListRunSteps( ctx context.Context, threadID string, runID string, pagination Pagination, ) (response RunStepList, err error) { urlValues := url.Values{} if pagination.Limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) } if pagination.Order != nil { urlValues.Add("order", *pagination.Order) } if pagination.After != nil { urlValues.Add("after", *pagination.After) } if pagination.Before != nil { urlValues.Add("before", *pagination.Before) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("/threads/%s/runs/%s/steps%s", threadID, runID, encodedValues) req, err := c.newRequest( ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: run_test.go ================================================ package openai_test import ( "context" openai "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" "encoding/json" "fmt" "net/http" "testing" ) // TestAssistant Tests the assistant endpoint of the API using the mocked server. func TestRun(t *testing.T) { assistantID := "asst_abc123" threadID := "thread_abc123" runID := "run_abc123" stepID := "step_abc123" limit := 20 order := "desc" after := "asst_abc122" before := "asst_abc124" client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler( "/v1/threads/"+threadID+"/runs/"+runID+"/steps/"+stepID, func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.RunStep{ ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStepStatusCompleted, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/runs/"+runID+"/steps", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.RunStepList{ RunSteps: []openai.RunStep{ { ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStepStatusCompleted, }, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/runs/"+runID+"/cancel", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { resBytes, _ := json.Marshal(openai.Run{ ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStatusCancelling, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/runs/"+runID+"/submit_tool_outputs", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { resBytes, _ := json.Marshal(openai.Run{ ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStatusCancelling, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/runs/"+runID, func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.Run{ ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStatusQueued, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodPost { var request openai.RunModifyRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Run{ ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStatusQueued, Metadata: request.Metadata, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/threads/"+threadID+"/runs", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.RunRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Run{ ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStatusQueued, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.RunList{ Runs: []openai.Run{ { ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStatusQueued, }, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/threads/runs", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.CreateThreadAndRunRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Run{ ID: runID, Object: "run", CreatedAt: 1234567890, Status: openai.RunStatusQueued, }) fmt.Fprintln(w, string(resBytes)) } }, ) ctx := context.Background() _, err := client.CreateRun(ctx, threadID, openai.RunRequest{ AssistantID: assistantID, }) checks.NoError(t, err, "CreateRun error") _, err = client.RetrieveRun(ctx, threadID, runID) checks.NoError(t, err, "RetrieveRun error") _, err = client.ModifyRun(ctx, threadID, runID, openai.RunModifyRequest{ Metadata: map[string]any{ "key": "value", }, }) checks.NoError(t, err, "ModifyRun error") _, err = client.ListRuns( ctx, threadID, openai.Pagination{ Limit: &limit, Order: &order, After: &after, Before: &before, }, ) checks.NoError(t, err, "ListRuns error") _, err = client.SubmitToolOutputs(ctx, threadID, runID, openai.SubmitToolOutputsRequest{}) checks.NoError(t, err, "SubmitToolOutputs error") _, err = client.CancelRun(ctx, threadID, runID) checks.NoError(t, err, "CancelRun error") _, err = client.CreateThreadAndRun(ctx, openai.CreateThreadAndRunRequest{ RunRequest: openai.RunRequest{ AssistantID: assistantID, }, Thread: openai.ThreadRequest{ Messages: []openai.ThreadMessage{ { Role: openai.ThreadMessageRoleUser, Content: "Hello, World!", }, }, }, }) checks.NoError(t, err, "CreateThreadAndRun error") _, err = client.RetrieveRunStep(ctx, threadID, runID, stepID) checks.NoError(t, err, "RetrieveRunStep error") _, err = client.ListRunSteps( ctx, threadID, runID, openai.Pagination{ Limit: &limit, Order: &order, After: &after, Before: &before, }, ) checks.NoError(t, err, "ListRunSteps error") } ================================================ FILE: speech.go ================================================ package openai import ( "context" "net/http" ) type SpeechModel string const ( TTSModel1 SpeechModel = "tts-1" TTSModel1HD SpeechModel = "tts-1-hd" TTSModelCanary SpeechModel = "canary-tts" TTSModelGPT4oMini SpeechModel = "gpt-4o-mini-tts" ) type SpeechVoice string const ( VoiceAlloy SpeechVoice = "alloy" VoiceAsh SpeechVoice = "ash" VoiceBallad SpeechVoice = "ballad" VoiceCoral SpeechVoice = "coral" VoiceEcho SpeechVoice = "echo" VoiceFable SpeechVoice = "fable" VoiceOnyx SpeechVoice = "onyx" VoiceNova SpeechVoice = "nova" VoiceShimmer SpeechVoice = "shimmer" VoiceVerse SpeechVoice = "verse" ) type SpeechResponseFormat string const ( SpeechResponseFormatMp3 SpeechResponseFormat = "mp3" SpeechResponseFormatOpus SpeechResponseFormat = "opus" SpeechResponseFormatAac SpeechResponseFormat = "aac" SpeechResponseFormatFlac SpeechResponseFormat = "flac" SpeechResponseFormatWav SpeechResponseFormat = "wav" SpeechResponseFormatPcm SpeechResponseFormat = "pcm" ) type CreateSpeechRequest struct { Model SpeechModel `json:"model"` Input string `json:"input"` Voice SpeechVoice `json:"voice"` Instructions string `json:"instructions,omitempty"` // Optional, Doesnt work with tts-1 or tts-1-hd. ResponseFormat SpeechResponseFormat `json:"response_format,omitempty"` // Optional, default to mp3 Speed float64 `json:"speed,omitempty"` // Optional, default to 1.0 } func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest) (response RawResponse, err error) { req, err := c.newRequest( ctx, http.MethodPost, c.fullURL("/audio/speech", withModel(string(request.Model))), withBody(request), withContentType("application/json"), ) if err != nil { return } return c.sendRequestRaw(req) } ================================================ FILE: speech_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "io" "mime" "net/http" "os" "path/filepath" "testing" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestSpeechIntegration(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/audio/speech", func(w http.ResponseWriter, r *http.Request) { path := filepath.Join(t.TempDir(), "fake.mp3") test.CreateTestFile(t, path) // audio endpoints only accept POST requests if r.Method != "POST" { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) if err != nil { http.Error(w, "failed to parse media type", http.StatusBadRequest) return } if mediaType != "application/json" { http.Error(w, "request is not json", http.StatusBadRequest) return } // Parse the JSON body of the request var params map[string]interface{} err = json.NewDecoder(r.Body).Decode(¶ms) if err != nil { http.Error(w, "failed to parse request body", http.StatusBadRequest) return } // Check if each required field is present in the parsed JSON object reqParams := []string{"model", "input", "voice"} for _, param := range reqParams { _, ok := params[param] if !ok { http.Error(w, fmt.Sprintf("no %s in params", param), http.StatusBadRequest) return } } // read audio file content audioFile, err := os.ReadFile(path) if err != nil { http.Error(w, "failed to read audio file", http.StatusInternalServerError) return } // write audio file content to response w.Header().Set("Content-Type", "audio/mpeg") w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("Connection", "keep-alive") _, err = w.Write(audioFile) if err != nil { http.Error(w, "failed to write body", http.StatusInternalServerError) return } }) t.Run("happy path", func(t *testing.T) { res, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{ Model: openai.TTSModel1, Input: "Hello!", Voice: openai.VoiceAlloy, }) checks.NoError(t, err, "CreateSpeech error") defer res.Close() buf, err := io.ReadAll(res) checks.NoError(t, err, "ReadAll error") // save buf to file as mp3 err = os.WriteFile("test.mp3", buf, 0644) checks.NoError(t, err, "Create error") }) } ================================================ FILE: stream.go ================================================ package openai import ( "context" "errors" "net/http" ) var ( ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages") ) type CompletionStream struct { *streamReader[CompletionResponse] } // CreateCompletionStream — API call to create a completion w/ streaming // support. It sets whether to stream back partial progress. If set, tokens will be // sent as data-only server-sent events as they become available, with the // stream terminated by a data: [DONE] message. func (c *Client) CreateCompletionStream( ctx context.Context, request CompletionRequest, ) (stream *CompletionStream, err error) { urlSuffix := "/completions" if !checkEndpointSupportsModel(urlSuffix, request.Model) { err = ErrCompletionUnsupportedModel return } if !checkPromptType(request.Prompt) { err = ErrCompletionRequestPromptTypeNotSupported return } request.Stream = true req, err := c.newRequest( ctx, http.MethodPost, c.fullURL(urlSuffix, withModel(request.Model)), withBody(request), ) if err != nil { return nil, err } resp, err := sendRequestStream[CompletionResponse](c, req) if err != nil { return } stream = &CompletionStream{ streamReader: resp, } return } ================================================ FILE: stream_reader.go ================================================ package openai import ( "bufio" "bytes" "fmt" "io" "net/http" "regexp" utils "github.com/sashabaranov/go-openai/internal" ) var ( headerData = regexp.MustCompile(`^data:\s*`) errorPrefix = regexp.MustCompile(`^data:\s*{"error":`) ) type streamable interface { ChatCompletionStreamResponse | CompletionResponse } type streamReader[T streamable] struct { emptyMessagesLimit uint isFinished bool reader *bufio.Reader response *http.Response errAccumulator utils.ErrorAccumulator unmarshaler utils.Unmarshaler httpHeader } func (stream *streamReader[T]) Recv() (response T, err error) { rawLine, err := stream.RecvRaw() if err != nil { return } err = stream.unmarshaler.Unmarshal(rawLine, &response) if err != nil { return } return response, nil } func (stream *streamReader[T]) RecvRaw() ([]byte, error) { if stream.isFinished { return nil, io.EOF } return stream.processLines() } //nolint:gocognit func (stream *streamReader[T]) processLines() ([]byte, error) { var ( emptyMessagesCount uint hasErrorPrefix bool ) for { rawLine, readErr := stream.reader.ReadBytes('\n') if readErr != nil || hasErrorPrefix { respErr := stream.unmarshalError() if respErr != nil { return nil, fmt.Errorf("error, %w", respErr.Error) } return nil, readErr } noSpaceLine := bytes.TrimSpace(rawLine) if errorPrefix.Match(noSpaceLine) { hasErrorPrefix = true } if !headerData.Match(noSpaceLine) || hasErrorPrefix { if hasErrorPrefix { noSpaceLine = headerData.ReplaceAll(noSpaceLine, nil) } writeErr := stream.errAccumulator.Write(noSpaceLine) if writeErr != nil { return nil, writeErr } emptyMessagesCount++ if emptyMessagesCount > stream.emptyMessagesLimit { return nil, ErrTooManyEmptyStreamMessages } continue } noPrefixLine := headerData.ReplaceAll(noSpaceLine, nil) if string(noPrefixLine) == "[DONE]" { stream.isFinished = true return nil, io.EOF } return noPrefixLine, nil } } func (stream *streamReader[T]) unmarshalError() (errResp *ErrorResponse) { errBytes := stream.errAccumulator.Bytes() if len(errBytes) == 0 { return } err := stream.unmarshaler.Unmarshal(errBytes, &errResp) if err != nil { errResp = nil } return } func (stream *streamReader[T]) Close() error { return stream.response.Body.Close() } ================================================ FILE: stream_reader_test.go ================================================ package openai //nolint:testpackage // testing private field import ( "bufio" "bytes" "errors" "testing" utils "github.com/sashabaranov/go-openai/internal" "github.com/sashabaranov/go-openai/internal/test" "github.com/sashabaranov/go-openai/internal/test/checks" ) var errTestUnmarshalerFailed = errors.New("test unmarshaler failed") type failingUnMarshaller struct{} func (*failingUnMarshaller) Unmarshal(_ []byte, _ any) error { return errTestUnmarshalerFailed } func TestStreamReaderReturnsUnmarshalerErrors(t *testing.T) { stream := &streamReader[ChatCompletionStreamResponse]{ errAccumulator: utils.NewErrorAccumulator(), unmarshaler: &failingUnMarshaller{}, } respErr := stream.unmarshalError() if respErr != nil { t.Fatalf("Did not return nil with empty buffer: %v", respErr) } err := stream.errAccumulator.Write([]byte("{")) if err != nil { t.Fatalf("%+v", err) } respErr = stream.unmarshalError() if respErr != nil { t.Fatalf("Did not return nil when unmarshaler failed: %v", respErr) } } func TestStreamReaderReturnsErrTooManyEmptyStreamMessages(t *testing.T) { stream := &streamReader[ChatCompletionStreamResponse]{ emptyMessagesLimit: 3, reader: bufio.NewReader(bytes.NewReader([]byte("\n\n\n\n"))), errAccumulator: utils.NewErrorAccumulator(), unmarshaler: &utils.JSONUnmarshaler{}, } _, err := stream.Recv() checks.ErrorIs(t, err, ErrTooManyEmptyStreamMessages, "Did not return error when recv failed", err.Error()) } func TestStreamReaderReturnsErrTestErrorAccumulatorWriteFailed(t *testing.T) { stream := &streamReader[ChatCompletionStreamResponse]{ reader: bufio.NewReader(bytes.NewReader([]byte("\n"))), errAccumulator: &utils.DefaultErrorAccumulator{ Buffer: &test.FailingErrorBuffer{}, }, unmarshaler: &utils.JSONUnmarshaler{}, } _, err := stream.Recv() checks.ErrorIs(t, err, test.ErrTestErrorAccumulatorWriteFailed, "Did not return error when write failed", err.Error()) } func TestStreamReaderRecvRaw(t *testing.T) { stream := &streamReader[ChatCompletionStreamResponse]{ reader: bufio.NewReader(bytes.NewReader([]byte("data: {\"key\": \"value\"}\n"))), } rawLine, err := stream.RecvRaw() if err != nil { t.Fatalf("Did not return raw line: %v", err) } if !bytes.Equal(rawLine, []byte("{\"key\": \"value\"}")) { t.Fatalf("Did not return raw line: %v", string(rawLine)) } } ================================================ FILE: stream_test.go ================================================ package openai_test import ( "context" "encoding/json" "errors" "io" "net/http" "os" "testing" "time" "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) func TestCompletionsStreamWrongModel(t *testing.T) { config := openai.DefaultConfig("whatever") config.BaseURL = "http://localhost/v1" client := openai.NewClientWithConfig(config) _, err := client.CreateCompletionStream( context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: openai.GPT3Dot5Turbo, }, ) if !errors.Is(err, openai.ErrCompletionUnsupportedModel) { t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel, but returned: %v", err) } } func TestCreateCompletionStream(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data := `{"id":"1","object":"completion","created":1598069254,"model":"text-davinci-002","choices":[{"text":"response1","finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data = `{"id":"2","object":"completion","created":1598069255,"model":"text-davinci-002","choices":[{"text":"response2","finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) dataBytes = append(dataBytes, []byte("event: done\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateCompletionStream(context.Background(), openai.CompletionRequest{ Prompt: "Ex falso quodlibet", Model: "text-davinci-002", MaxTokens: 10, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() expectedResponses := []openai.CompletionResponse{ { ID: "1", Object: "completion", Created: 1598069254, Model: "text-davinci-002", Choices: []openai.CompletionChoice{{Text: "response1", FinishReason: "max_tokens"}}, }, { ID: "2", Object: "completion", Created: 1598069255, Model: "text-davinci-002", Choices: []openai.CompletionChoice{{Text: "response2", FinishReason: "max_tokens"}}, }, } for ix, expectedResponse := range expectedResponses { receivedResponse, streamErr := stream.Recv() if streamErr != nil { t.Errorf("stream.Recv() failed: %v", streamErr) } if !compareResponses(expectedResponse, receivedResponse) { t.Errorf("Stream response %v is %v, expected %v", ix, receivedResponse, expectedResponse) } } _, streamErr := stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr) } _, streamErr = stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("stream.Recv() did not return EOF when the stream is finished: %v", streamErr) } } func TestCreateCompletionStreamError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} dataStr := []string{ `{`, `"error": {`, `"message": "Incorrect API key provided: sk-***************************************",`, `"type": "invalid_request_error",`, `"param": null,`, `"code": "invalid_api_key"`, `}`, `}`, } for _, str := range dataStr { dataBytes = append(dataBytes, []byte(str+"\n")...) } _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateCompletionStream(context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: openai.GPT3TextDavinci003, Prompt: "Hello!", Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() _, streamErr := stream.Recv() checks.HasError(t, streamErr, "stream.Recv() did not return error") var apiErr *openai.APIError if !errors.As(streamErr, &apiErr) { t.Errorf("stream.Recv() did not return APIError") } t.Logf("%+v\n", apiErr) } func TestCreateCompletionStreamRateLimitError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(429) // Send test responses dataBytes := []byte(`{"error":{` + `"message": "You are sending requests too quickly.",` + `"type":"rate_limit_reached",` + `"param":null,` + `"code":"rate_limit_reached"}}`) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) var apiErr *openai.APIError _, err := client.CreateCompletionStream(context.Background(), openai.CompletionRequest{ MaxTokens: 5, Model: openai.GPT3Babbage002, Prompt: "Hello!", Stream: true, }) if !errors.As(err, &apiErr) { t.Errorf("TestCreateCompletionStreamRateLimitError did not return APIError") } t.Logf("%+v\n", apiErr) } func TestCreateCompletionStreamTooManyEmptyStreamMessagesError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data := `{"id":"1","object":"completion","created":1598069254,"model":"text-davinci-002","choices":[{"text":"response1","finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) // Totally 301 empty messages (300 is the limit) for i := 0; i < 299; i++ { dataBytes = append(dataBytes, '\n') } dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data = `{"id":"2","object":"completion","created":1598069255,"model":"text-davinci-002","choices":[{"text":"response2","finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) dataBytes = append(dataBytes, []byte("event: done\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateCompletionStream(context.Background(), openai.CompletionRequest{ Prompt: "Ex falso quodlibet", Model: "text-davinci-002", MaxTokens: 10, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() _, _ = stream.Recv() _, streamErr := stream.Recv() if !errors.Is(streamErr, openai.ErrTooManyEmptyStreamMessages) { t.Errorf("TestCreateCompletionStreamTooManyEmptyStreamMessagesError did not return ErrTooManyEmptyStreamMessages") } } func TestCreateCompletionStreamUnexpectedTerminatedError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data := `{"id":"1","object":"completion","created":1598069254,"model":"text-davinci-002","choices":[{"text":"response1","finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) // Stream is terminated without sending "done" message _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateCompletionStream(context.Background(), openai.CompletionRequest{ Prompt: "Ex falso quodlibet", Model: "text-davinci-002", MaxTokens: 10, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() _, _ = stream.Recv() _, streamErr := stream.Recv() if !errors.Is(streamErr, io.EOF) { t.Errorf("TestCreateCompletionStreamUnexpectedTerminatedError did not return io.EOF") } } func TestCreateCompletionStreamBrokenJSONError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") // Send test responses dataBytes := []byte{} dataBytes = append(dataBytes, []byte("event: message\n")...) //nolint:lll data := `{"id":"1","object":"completion","created":1598069254,"model":"text-davinci-002","choices":[{"text":"response1","finish_reason":"max_tokens"}]}` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) // Send broken json dataBytes = append(dataBytes, []byte("event: message\n")...) data = `{"id":"2","object":"completion","created":1598069255,"model":` dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) dataBytes = append(dataBytes, []byte("event: done\n")...) dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) _, err := w.Write(dataBytes) checks.NoError(t, err, "Write error") }) stream, err := client.CreateCompletionStream(context.Background(), openai.CompletionRequest{ Prompt: "Ex falso quodlibet", Model: "text-davinci-002", MaxTokens: 10, Stream: true, }) checks.NoError(t, err, "CreateCompletionStream returned error") defer stream.Close() _, _ = stream.Recv() _, streamErr := stream.Recv() var syntaxError *json.SyntaxError if !errors.As(streamErr, &syntaxError) { t.Errorf("TestCreateCompletionStreamBrokenJSONError did not return json.SyntaxError") } } func TestCreateCompletionStreamReturnTimeoutError(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/completions", func(http.ResponseWriter, *http.Request) { time.Sleep(10 * time.Nanosecond) }) ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, time.Nanosecond) defer cancel() _, err := client.CreateCompletionStream(ctx, openai.CompletionRequest{ Prompt: "Ex falso quodlibet", Model: "text-davinci-002", MaxTokens: 10, Stream: true, }) if err == nil { t.Fatal("Did not return error") } if !os.IsTimeout(err) { t.Fatal("Did not return timeout error") } } // Helper funcs. func compareResponses(r1, r2 openai.CompletionResponse) bool { if r1.ID != r2.ID || r1.Object != r2.Object || r1.Created != r2.Created || r1.Model != r2.Model { return false } if len(r1.Choices) != len(r2.Choices) { return false } for i := range r1.Choices { if !compareResponseChoices(r1.Choices[i], r2.Choices[i]) { return false } } return true } func compareResponseChoices(c1, c2 openai.CompletionChoice) bool { if c1.Text != c2.Text || c1.FinishReason != c2.FinishReason { return false } return true } ================================================ FILE: thread.go ================================================ package openai import ( "context" "net/http" ) const ( threadsSuffix = "/threads" ) type Thread struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` Metadata map[string]any `json:"metadata"` ToolResources ToolResources `json:"tool_resources,omitempty"` httpHeader } type ThreadRequest struct { Messages []ThreadMessage `json:"messages,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` ToolResources *ToolResourcesRequest `json:"tool_resources,omitempty"` } type ToolResources struct { CodeInterpreter *CodeInterpreterToolResources `json:"code_interpreter,omitempty"` FileSearch *FileSearchToolResources `json:"file_search,omitempty"` } type CodeInterpreterToolResources struct { FileIDs []string `json:"file_ids,omitempty"` } type FileSearchToolResources struct { VectorStoreIDs []string `json:"vector_store_ids,omitempty"` } type ToolResourcesRequest struct { CodeInterpreter *CodeInterpreterToolResourcesRequest `json:"code_interpreter,omitempty"` FileSearch *FileSearchToolResourcesRequest `json:"file_search,omitempty"` } type CodeInterpreterToolResourcesRequest struct { FileIDs []string `json:"file_ids,omitempty"` } type FileSearchToolResourcesRequest struct { VectorStoreIDs []string `json:"vector_store_ids,omitempty"` VectorStores []VectorStoreToolResources `json:"vector_stores,omitempty"` } type VectorStoreToolResources struct { FileIDs []string `json:"file_ids,omitempty"` ChunkingStrategy *ChunkingStrategy `json:"chunking_strategy,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` } type ChunkingStrategy struct { Type ChunkingStrategyType `json:"type"` Static *StaticChunkingStrategy `json:"static,omitempty"` } type StaticChunkingStrategy struct { MaxChunkSizeTokens int `json:"max_chunk_size_tokens"` ChunkOverlapTokens int `json:"chunk_overlap_tokens"` } type ChunkingStrategyType string const ( ChunkingStrategyTypeAuto ChunkingStrategyType = "auto" ChunkingStrategyTypeStatic ChunkingStrategyType = "static" ) type ModifyThreadRequest struct { Metadata map[string]any `json:"metadata"` ToolResources *ToolResources `json:"tool_resources,omitempty"` } type ThreadMessageRole string const ( ThreadMessageRoleAssistant ThreadMessageRole = "assistant" ThreadMessageRoleUser ThreadMessageRole = "user" ) type ThreadMessage struct { Role ThreadMessageRole `json:"role"` Content string `json:"content"` FileIDs []string `json:"file_ids,omitempty"` Attachments []ThreadAttachment `json:"attachments,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` } type ThreadAttachment struct { FileID string `json:"file_id"` Tools []ThreadAttachmentTool `json:"tools"` } type ThreadAttachmentTool struct { Type string `json:"type"` } type ThreadDeleteResponse struct { ID string `json:"id"` Object string `json:"object"` Deleted bool `json:"deleted"` httpHeader } // CreateThread creates a new thread. func (c *Client) CreateThread(ctx context.Context, request ThreadRequest) (response Thread, err error) { req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(threadsSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // RetrieveThread retrieves a thread. func (c *Client) RetrieveThread(ctx context.Context, threadID string) (response Thread, err error) { urlSuffix := threadsSuffix + "/" + threadID req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // ModifyThread modifies a thread. func (c *Client) ModifyThread( ctx context.Context, threadID string, request ModifyThreadRequest, ) (response Thread, err error) { urlSuffix := threadsSuffix + "/" + threadID req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } // DeleteThread deletes a thread. func (c *Client) DeleteThread( ctx context.Context, threadID string, ) (response ThreadDeleteResponse, err error) { urlSuffix := threadsSuffix + "/" + threadID req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) if err != nil { return } err = c.sendRequest(req, &response) return } ================================================ FILE: thread_test.go ================================================ package openai_test import ( "context" "encoding/json" "fmt" "net/http" "testing" openai "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" ) // TestThread Tests the thread endpoint of the API using the mocked server. func TestThread(t *testing.T) { threadID := "thread_abc123" client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler( "/v1/threads/"+threadID, func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: resBytes, _ := json.Marshal(openai.Thread{ ID: threadID, Object: "thread", CreatedAt: 1234567890, }) fmt.Fprintln(w, string(resBytes)) case http.MethodPost: var request openai.ThreadRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Thread{ ID: threadID, Object: "thread", CreatedAt: 1234567890, }) fmt.Fprintln(w, string(resBytes)) case http.MethodDelete: fmt.Fprintln(w, `{ "id": "thread_abc123", "object": "thread.deleted", "deleted": true }`) } }, ) server.RegisterHandler( "/v1/threads", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.ModifyThreadRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Thread{ ID: threadID, Object: "thread", CreatedAt: 1234567890, Metadata: request.Metadata, }) fmt.Fprintln(w, string(resBytes)) } }, ) ctx := context.Background() _, err := client.CreateThread(ctx, openai.ThreadRequest{ Messages: []openai.ThreadMessage{ { Role: openai.ThreadMessageRoleUser, Content: "Hello, World!", }, }, }) checks.NoError(t, err, "CreateThread error") _, err = client.RetrieveThread(ctx, threadID) checks.NoError(t, err, "RetrieveThread error") _, err = client.ModifyThread(ctx, threadID, openai.ModifyThreadRequest{ Metadata: map[string]interface{}{ "key": "value", }, }) checks.NoError(t, err, "ModifyThread error") _, err = client.DeleteThread(ctx, threadID) checks.NoError(t, err, "DeleteThread error") } // TestAzureThread Tests the thread endpoint of the API using the Azure mocked server. func TestAzureThread(t *testing.T) { threadID := "thread_abc123" client, server, teardown := setupAzureTestServer() defer teardown() server.RegisterHandler( "/openai/threads/"+threadID, func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: resBytes, _ := json.Marshal(openai.Thread{ ID: threadID, Object: "thread", CreatedAt: 1234567890, }) fmt.Fprintln(w, string(resBytes)) case http.MethodPost: var request openai.ThreadRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Thread{ ID: threadID, Object: "thread", CreatedAt: 1234567890, }) fmt.Fprintln(w, string(resBytes)) case http.MethodDelete: fmt.Fprintln(w, `{ "id": "thread_abc123", "object": "thread.deleted", "deleted": true }`) } }, ) server.RegisterHandler( "/openai/threads", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.ModifyThreadRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.Thread{ ID: threadID, Object: "thread", CreatedAt: 1234567890, Metadata: request.Metadata, }) fmt.Fprintln(w, string(resBytes)) } }, ) ctx := context.Background() _, err := client.CreateThread(ctx, openai.ThreadRequest{ Messages: []openai.ThreadMessage{ { Role: openai.ThreadMessageRoleUser, Content: "Hello, World!", }, }, }) checks.NoError(t, err, "CreateThread error") _, err = client.RetrieveThread(ctx, threadID) checks.NoError(t, err, "RetrieveThread error") _, err = client.ModifyThread(ctx, threadID, openai.ModifyThreadRequest{ Metadata: map[string]interface{}{ "key": "value", }, }) checks.NoError(t, err, "ModifyThread error") _, err = client.DeleteThread(ctx, threadID) checks.NoError(t, err, "DeleteThread error") } ================================================ FILE: vector_store.go ================================================ package openai import ( "context" "fmt" "net/http" "net/url" ) const ( vectorStoresSuffix = "/vector_stores" vectorStoresFilesSuffix = "/files" vectorStoresFileBatchesSuffix = "/file_batches" ) type VectorStoreFileCount struct { InProgress int `json:"in_progress"` Completed int `json:"completed"` Failed int `json:"failed"` Cancelled int `json:"cancelled"` Total int `json:"total"` } type VectorStore struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` Name string `json:"name"` UsageBytes int `json:"usage_bytes"` FileCounts VectorStoreFileCount `json:"file_counts"` Status string `json:"status"` ExpiresAfter *VectorStoreExpires `json:"expires_after"` ExpiresAt *int `json:"expires_at"` Metadata map[string]any `json:"metadata"` httpHeader } type VectorStoreExpires struct { Anchor string `json:"anchor"` Days int `json:"days"` } // VectorStoreRequest provides the vector store request parameters. type VectorStoreRequest struct { Name string `json:"name,omitempty"` FileIDs []string `json:"file_ids,omitempty"` ExpiresAfter *VectorStoreExpires `json:"expires_after,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` } // VectorStoresList is a list of vector store. type VectorStoresList struct { VectorStores []VectorStore `json:"data"` LastID *string `json:"last_id"` FirstID *string `json:"first_id"` HasMore bool `json:"has_more"` httpHeader } type VectorStoreDeleteResponse struct { ID string `json:"id"` Object string `json:"object"` Deleted bool `json:"deleted"` httpHeader } type VectorStoreFile struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` VectorStoreID string `json:"vector_store_id"` UsageBytes int `json:"usage_bytes"` Status string `json:"status"` httpHeader } type VectorStoreFileRequest struct { FileID string `json:"file_id"` } type VectorStoreFilesList struct { VectorStoreFiles []VectorStoreFile `json:"data"` FirstID *string `json:"first_id"` LastID *string `json:"last_id"` HasMore bool `json:"has_more"` httpHeader } type VectorStoreFileBatch struct { ID string `json:"id"` Object string `json:"object"` CreatedAt int64 `json:"created_at"` VectorStoreID string `json:"vector_store_id"` Status string `json:"status"` FileCounts VectorStoreFileCount `json:"file_counts"` httpHeader } type VectorStoreFileBatchRequest struct { FileIDs []string `json:"file_ids"` } // CreateVectorStore creates a new vector store. func (c *Client) CreateVectorStore(ctx context.Context, request VectorStoreRequest) (response VectorStore, err error) { req, _ := c.newRequest( ctx, http.MethodPost, c.fullURL(vectorStoresSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion), ) err = c.sendRequest(req, &response) return } // RetrieveVectorStore retrieves an vector store. func (c *Client) RetrieveVectorStore( ctx context.Context, vectorStoreID string, ) (response VectorStore, err error) { urlSuffix := fmt.Sprintf("%s/%s", vectorStoresSuffix, vectorStoreID) req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // ModifyVectorStore modifies a vector store. func (c *Client) ModifyVectorStore( ctx context.Context, vectorStoreID string, request VectorStoreRequest, ) (response VectorStore, err error) { urlSuffix := fmt.Sprintf("%s/%s", vectorStoresSuffix, vectorStoreID) req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // DeleteVectorStore deletes an vector store. func (c *Client) DeleteVectorStore( ctx context.Context, vectorStoreID string, ) (response VectorStoreDeleteResponse, err error) { urlSuffix := fmt.Sprintf("%s/%s", vectorStoresSuffix, vectorStoreID) req, _ := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // ListVectorStores Lists the currently available vector store. func (c *Client) ListVectorStores( ctx context.Context, pagination Pagination, ) (response VectorStoresList, err error) { urlValues := url.Values{} if pagination.After != nil { urlValues.Add("after", *pagination.After) } if pagination.Order != nil { urlValues.Add("order", *pagination.Order) } if pagination.Limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) } if pagination.Before != nil { urlValues.Add("before", *pagination.Before) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("%s%s", vectorStoresSuffix, encodedValues) req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // CreateVectorStoreFile creates a new vector store file. func (c *Client) CreateVectorStoreFile( ctx context.Context, vectorStoreID string, request VectorStoreFileRequest, ) (response VectorStoreFile, err error) { urlSuffix := fmt.Sprintf("%s/%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix) req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // RetrieveVectorStoreFile retrieves a vector store file. func (c *Client) RetrieveVectorStoreFile( ctx context.Context, vectorStoreID string, fileID string, ) (response VectorStoreFile, err error) { urlSuffix := fmt.Sprintf("%s/%s%s/%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix, fileID) req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // DeleteVectorStoreFile deletes an existing file. func (c *Client) DeleteVectorStoreFile( ctx context.Context, vectorStoreID string, fileID string, ) (err error) { urlSuffix := fmt.Sprintf("%s/%s%s/%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix, fileID) req, _ := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, nil) return } // ListVectorStoreFiles Lists the currently available files for a vector store. func (c *Client) ListVectorStoreFiles( ctx context.Context, vectorStoreID string, pagination Pagination, ) (response VectorStoreFilesList, err error) { urlValues := url.Values{} if pagination.After != nil { urlValues.Add("after", *pagination.After) } if pagination.Limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) } if pagination.Before != nil { urlValues.Add("before", *pagination.Before) } if pagination.Order != nil { urlValues.Add("order", *pagination.Order) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("%s/%s%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFilesSuffix, encodedValues) req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // CreateVectorStoreFileBatch creates a new vector store file batch. func (c *Client) CreateVectorStoreFileBatch( ctx context.Context, vectorStoreID string, request VectorStoreFileBatchRequest, ) (response VectorStoreFileBatch, err error) { urlSuffix := fmt.Sprintf("%s/%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFileBatchesSuffix) req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // RetrieveVectorStoreFileBatch retrieves a vector store file batch. func (c *Client) RetrieveVectorStoreFileBatch( ctx context.Context, vectorStoreID string, batchID string, ) (response VectorStoreFileBatch, err error) { urlSuffix := fmt.Sprintf("%s/%s%s/%s", vectorStoresSuffix, vectorStoreID, vectorStoresFileBatchesSuffix, batchID) req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // CancelVectorStoreFileBatch cancel a new vector store file batch. func (c *Client) CancelVectorStoreFileBatch( ctx context.Context, vectorStoreID string, batchID string, ) (response VectorStoreFileBatch, err error) { urlSuffix := fmt.Sprintf("%s/%s%s/%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFileBatchesSuffix, batchID, "/cancel") req, _ := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } // ListVectorStoreFiles Lists the currently available files for a vector store. func (c *Client) ListVectorStoreFilesInBatch( ctx context.Context, vectorStoreID string, batchID string, pagination Pagination, ) (response VectorStoreFilesList, err error) { urlValues := url.Values{} if pagination.After != nil { urlValues.Add("after", *pagination.After) } if pagination.Limit != nil { urlValues.Add("limit", fmt.Sprintf("%d", *pagination.Limit)) } if pagination.Before != nil { urlValues.Add("before", *pagination.Before) } if pagination.Order != nil { urlValues.Add("order", *pagination.Order) } encodedValues := "" if len(urlValues) > 0 { encodedValues = "?" + urlValues.Encode() } urlSuffix := fmt.Sprintf("%s/%s%s/%s%s%s", vectorStoresSuffix, vectorStoreID, vectorStoresFileBatchesSuffix, batchID, "/files", encodedValues) req, _ := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantVersion(c.config.AssistantVersion)) err = c.sendRequest(req, &response) return } ================================================ FILE: vector_store_test.go ================================================ package openai_test import ( "context" openai "github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai/internal/test/checks" "encoding/json" "fmt" "net/http" "testing" ) // TestVectorStore Tests the vector store endpoint of the API using the mocked server. func TestVectorStore(t *testing.T) { vectorStoreID := "vs_abc123" vectorStoreName := "TestStore" vectorStoreFileID := "file-wB6RM6wHdA49HfS2DJ9fEyrH" vectorStoreFileBatchID := "vsfb_abc123" limit := 20 order := "desc" after := "vs_abc122" before := "vs_abc123" client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler( "/v1/vector_stores/"+vectorStoreID+"/files/"+vectorStoreFileID, func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.VectorStoreFile{ ID: vectorStoreFileID, Object: "vector_store.file", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, Status: "completed", }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodDelete { fmt.Fprintln(w, `{ id: "file-wB6RM6wHdA49HfS2DJ9fEyrH", object: "vector_store.file.deleted", deleted: true }`) } }, ) server.RegisterHandler( "/v1/vector_stores/"+vectorStoreID+"/files", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.VectorStoreFilesList{ VectorStoreFiles: []openai.VectorStoreFile{ { ID: vectorStoreFileID, Object: "vector_store.file", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, }, }, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodPost { var request openai.VectorStoreFileRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.VectorStoreFile{ ID: request.FileID, Object: "vector_store.file", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/vector_stores/"+vectorStoreID+"/file_batches/"+vectorStoreFileBatchID+"/files", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.VectorStoreFilesList{ VectorStoreFiles: []openai.VectorStoreFile{ { ID: vectorStoreFileID, Object: "vector_store.file", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, }, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/vector_stores/"+vectorStoreID+"/file_batches/"+vectorStoreFileBatchID+"/cancel", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { resBytes, _ := json.Marshal(openai.VectorStoreFileBatch{ ID: vectorStoreFileBatchID, Object: "vector_store.file_batch", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, Status: "cancelling", FileCounts: openai.VectorStoreFileCount{ InProgress: 0, Completed: 1, Failed: 0, Cancelled: 0, Total: 0, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/vector_stores/"+vectorStoreID+"/file_batches/"+vectorStoreFileBatchID, func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.VectorStoreFileBatch{ ID: vectorStoreFileBatchID, Object: "vector_store.file_batch", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, Status: "completed", FileCounts: openai.VectorStoreFileCount{ Completed: 1, }, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodPost { resBytes, _ := json.Marshal(openai.VectorStoreFileBatch{ ID: vectorStoreFileBatchID, Object: "vector_store.file_batch", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, Status: "cancelling", FileCounts: openai.VectorStoreFileCount{ Completed: 1, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/vector_stores/"+vectorStoreID+"/file_batches", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.VectorStoreFileBatchRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.VectorStoreFileBatch{ ID: vectorStoreFileBatchID, Object: "vector_store.file_batch", CreatedAt: 1234567890, VectorStoreID: vectorStoreID, Status: "completed", FileCounts: openai.VectorStoreFileCount{ InProgress: 0, Completed: len(request.FileIDs), Failed: 0, Cancelled: 0, Total: 0, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) server.RegisterHandler( "/v1/vector_stores/"+vectorStoreID, func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: resBytes, _ := json.Marshal(openai.VectorStore{ ID: vectorStoreID, Object: "vector_store", CreatedAt: 1234567890, Name: vectorStoreName, }) fmt.Fprintln(w, string(resBytes)) case http.MethodPost: var request openai.VectorStore err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.VectorStore{ ID: vectorStoreID, Object: "vector_store", CreatedAt: 1234567890, Name: request.Name, }) fmt.Fprintln(w, string(resBytes)) case http.MethodDelete: fmt.Fprintln(w, `{ "id": "vectorstore_abc123", "object": "vector_store.deleted", "deleted": true }`) } }, ) server.RegisterHandler( "/v1/vector_stores", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var request openai.VectorStoreRequest err := json.NewDecoder(r.Body).Decode(&request) checks.NoError(t, err, "Decode error") resBytes, _ := json.Marshal(openai.VectorStore{ ID: vectorStoreID, Object: "vector_store", CreatedAt: 1234567890, Name: request.Name, FileCounts: openai.VectorStoreFileCount{ InProgress: 0, Completed: 0, Failed: 0, Cancelled: 0, Total: 0, }, }) fmt.Fprintln(w, string(resBytes)) } else if r.Method == http.MethodGet { resBytes, _ := json.Marshal(openai.VectorStoresList{ LastID: &vectorStoreID, FirstID: &vectorStoreID, VectorStores: []openai.VectorStore{ { ID: vectorStoreID, Object: "vector_store", CreatedAt: 1234567890, Name: vectorStoreName, }, }, }) fmt.Fprintln(w, string(resBytes)) } }, ) ctx := context.Background() t.Run("create_vector_store", func(t *testing.T) { _, err := client.CreateVectorStore(ctx, openai.VectorStoreRequest{ Name: vectorStoreName, }) checks.NoError(t, err, "CreateVectorStore error") }) t.Run("retrieve_vector_store", func(t *testing.T) { _, err := client.RetrieveVectorStore(ctx, vectorStoreID) checks.NoError(t, err, "RetrieveVectorStore error") }) t.Run("delete_vector_store", func(t *testing.T) { _, err := client.DeleteVectorStore(ctx, vectorStoreID) checks.NoError(t, err, "DeleteVectorStore error") }) t.Run("list_vector_store", func(t *testing.T) { _, err := client.ListVectorStores(context.TODO(), openai.Pagination{ Limit: &limit, Order: &order, After: &after, Before: &before, }) checks.NoError(t, err, "ListVectorStores error") }) t.Run("create_vector_store_file", func(t *testing.T) { _, err := client.CreateVectorStoreFile(context.TODO(), vectorStoreID, openai.VectorStoreFileRequest{ FileID: vectorStoreFileID, }) checks.NoError(t, err, "CreateVectorStoreFile error") }) t.Run("list_vector_store_files", func(t *testing.T) { _, err := client.ListVectorStoreFiles(ctx, vectorStoreID, openai.Pagination{ Limit: &limit, Order: &order, After: &after, Before: &before, }) checks.NoError(t, err, "ListVectorStoreFiles error") }) t.Run("retrieve_vector_store_file", func(t *testing.T) { _, err := client.RetrieveVectorStoreFile(ctx, vectorStoreID, vectorStoreFileID) checks.NoError(t, err, "RetrieveVectorStoreFile error") }) t.Run("delete_vector_store_file", func(t *testing.T) { err := client.DeleteVectorStoreFile(ctx, vectorStoreID, vectorStoreFileID) checks.NoError(t, err, "DeleteVectorStoreFile error") }) t.Run("modify_vector_store", func(t *testing.T) { _, err := client.ModifyVectorStore(ctx, vectorStoreID, openai.VectorStoreRequest{ Name: vectorStoreName, }) checks.NoError(t, err, "ModifyVectorStore error") }) t.Run("create_vector_store_file_batch", func(t *testing.T) { _, err := client.CreateVectorStoreFileBatch(ctx, vectorStoreID, openai.VectorStoreFileBatchRequest{ FileIDs: []string{vectorStoreFileID}, }) checks.NoError(t, err, "CreateVectorStoreFileBatch error") }) t.Run("retrieve_vector_store_file_batch", func(t *testing.T) { _, err := client.RetrieveVectorStoreFileBatch(ctx, vectorStoreID, vectorStoreFileBatchID) checks.NoError(t, err, "RetrieveVectorStoreFileBatch error") }) t.Run("list_vector_store_files_in_batch", func(t *testing.T) { _, err := client.ListVectorStoreFilesInBatch( ctx, vectorStoreID, vectorStoreFileBatchID, openai.Pagination{ Limit: &limit, Order: &order, After: &after, Before: &before, }) checks.NoError(t, err, "ListVectorStoreFilesInBatch error") }) t.Run("cancel_vector_store_file_batch", func(t *testing.T) { _, err := client.CancelVectorStoreFileBatch(ctx, vectorStoreID, vectorStoreFileBatchID) checks.NoError(t, err, "CancelVectorStoreFileBatch error") }) }