├── .github └── workflows │ ├── ci.yml │ └── release.yml ├── .gitignore ├── .goreleaser.yaml ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── assets └── images │ ├── aoai-proxy.jpg │ ├── chatgpt-next-web.png │ ├── chatgpt-web.png │ └── endpoint.png ├── azure ├── init.go ├── model.go └── proxy.go ├── build.sh ├── cmd ├── main.go └── router.go ├── config ├── .gitkeep └── config.example.yaml ├── constant └── env.go ├── examples └── simple-langchain-examples.ipynb ├── go.mod ├── go.sum └── util ├── http_proxy.go ├── http_proxy_test.go ├── path.go ├── response_err.go └── types.go /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | paths-ignore: 7 | - 'docs/**' 8 | - 'assets/**' 9 | - '**/*.gitignore' 10 | - '**/*.md' 11 | pull_request: 12 | branches: [ "master" ] 13 | paths-ignore: 14 | - 'docs/**' 15 | - 'assets/**' 16 | - '**/*.gitignore' 17 | - '**/*.md' 18 | 19 | jobs: 20 | build: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - name: Checkout 24 | uses: actions/checkout@v3 25 | with: 26 | fetch-depth: '0' 27 | - name: Set up Go 28 | uses: actions/setup-go@v3 29 | with: 30 | cache: false 31 | go-version-file: go.mod 32 | 33 | - name: Verify gofmt 34 | run: | 35 | make fmt && git add azure util cmd constant && 36 | git diff --cached --exit-code || (echo 'Please run "make fmt" to verify gofmt' && exit 1); 37 | - name: Verify govet 38 | run: | 39 | make vet && git add azure util cmd constant && 40 | git diff --cached --exit-code || (echo 'Please run "make vet" to verify govet' && exit 1); 41 | 42 | - name: Build 43 | run: make build -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | tag: 7 | default: 'latest' 8 | required: true 9 | description: 'Docker image tag' 10 | push: 11 | tags: 12 | - 'v*' 13 | 14 | jobs: 15 | build-image: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: Set up QEMU 19 | uses: docker/setup-qemu-action@v2 20 | - name: Set up Docker Buildx 21 | uses: docker/setup-buildx-action@v2 22 | 23 | - name: Login to Docker Hub 24 | uses: docker/login-action@v2 25 | with: 26 | username: ${{ secrets.DOCKERHUB_USERNAME }} 27 | password: ${{ secrets.DOCKERHUB_TOKEN }} 28 | 29 | - name: Login to the GPR 30 | uses: docker/login-action@v2 31 | with: 32 | registry: ghcr.io 33 | username: ${{ github.repository_owner }} 34 | password: ${{ secrets.GITHUB_TOKEN }} 35 | 36 | - name: Parse Tag Name 37 | run: | 38 | if [ x${{ github.event.inputs.tag }} == x"" ]; then 39 | echo "TAG_NAME=${{ github.ref_name }}" >> $GITHUB_ENV 40 | else 41 | echo "TAG_NAME=${{ github.event.inputs.tag }}" >> $GITHUB_ENV 42 | fi 43 | 44 | - name: Build and push 45 | uses: docker/build-push-action@v4 46 | env: 47 | BUILDX_NO_DEFAULT_ATTESTATIONS: 1 # https://github.com/orgs/community/discussions/45969 48 | with: 49 | platforms: linux/amd64,linux/arm64 50 | push: true 51 | pull: true 52 | labels: | 53 | org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} 54 | org.opencontainers.image.licenses=Apache-2.0 55 | tags: | 56 | ${{ github.repository }}:${{ env.TAG_NAME }} 57 | ghcr.io/${{ github.repository }}:${{ env.TAG_NAME }} 58 | cache-from: type=gha # https://docs.docker.com/build/cache/backends/gha/ 59 | cache-to: type=gha,mode=max 60 | 61 | goreleaser: 62 | runs-on: ubuntu-latest 63 | if: ${{ github.event.inputs.tag == '' }} 64 | steps: 65 | - name: Checkout 66 | uses: actions/checkout@v3 67 | with: 68 | fetch-depth: 0 69 | - name: Fetch all tags 70 | run: git fetch --force --tags 71 | - name: Set up Go 72 | uses: actions/setup-go@v4 73 | with: 74 | cache: false 75 | go-version-file: go.mod 76 | - name: Run GoReleaser 77 | uses: goreleaser/goreleaser-action@v4 78 | with: 79 | distribution: goreleaser 80 | version: latest 81 | args: release --rm-dist 82 | env: 83 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | 17 | .idea/ 18 | bin/ 19 | release/ 20 | docker-compose.yml 21 | dist/ 22 | config/config.yaml -------------------------------------------------------------------------------- /.goreleaser.yaml: -------------------------------------------------------------------------------- 1 | # Make sure to check the documentation at https://goreleaser.com 2 | env: 3 | - GIT_URL=https://github.com/stulzq/azure-openai-proxy 4 | before: 5 | hooks: 6 | - go mod tidy 7 | builds: 8 | - id: azure-openai-proxy 9 | env: 10 | - CGO_ENABLED=0 11 | goos: 12 | - linux 13 | - windows 14 | - darwin 15 | goarch: 16 | - amd64 17 | main: ./cmd 18 | binary: azure-openai-proxy 19 | flags: 20 | - -trimpath 21 | ldflags: 22 | - -s -w 23 | - -X main.version={{ .Version }} 24 | - -X main.buildDate={{ .Date }} 25 | - -X main.gitCommit={{ .Commit }} 26 | 27 | archives: 28 | - format: tar.gz 29 | # this name template makes the OS and Arch compatible with the results of uname. 30 | name_template: >- 31 | {{ .ProjectName }}_ 32 | {{- .Version }}_ 33 | {{- .Os }}_ 34 | {{- if eq .Arch "amd64" }}x86_64 35 | {{- else if eq .Arch "386" }}i386 36 | {{- else }}{{ .Arch }}{{ end }} 37 | {{- if .Arm }}v{{ .Arm }}{{ end }} 38 | # use zip for windows archives 39 | format_overrides: 40 | - goos: windows 41 | format: zip 42 | checksum: 43 | name_template: 'checksums.txt' 44 | snapshot: 45 | name_template: "{{ incpatch .Version }}-next" 46 | 47 | # https://goreleaser.com/customization/changelog/ 48 | changelog: 49 | sort: asc 50 | use: github 51 | filters: 52 | exclude: 53 | - '^build:' 54 | - '^ci:' 55 | # - '^docs:' 56 | - '^test:' 57 | - '^chore:' 58 | - '^feat(deps):' 59 | - 'merge conflict' 60 | - Merge pull request 61 | - Merge remote-tracking branch 62 | - Merge branch 63 | - go mod tidy 64 | - '^Update' 65 | groups: 66 | - title: Dependency updates 67 | regexp: '^.*?(feat|fix)\(deps\)!?:.+$' 68 | order: 300 69 | - title: 'New Features' 70 | regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$' 71 | order: 100 72 | - title: 'Security updates' 73 | regexp: '^.*?sec(\([[:word:]]+\))??!?:.+$' 74 | order: 150 75 | - title: 'Bug fixes' 76 | regexp: '^.*?fix(\([[:word:]]+\))??!?:.+$' 77 | order: 200 78 | - title: 'Documentation updates' 79 | regexp: '^.*?doc(\([[:word:]]+\))??!?:.+$' 80 | order: 400 81 | # - title: 'Build process updates' 82 | # regexp: '^.*?build(\([[:word:]]+\))??!?:.+$' 83 | # order: 400 84 | - title: Other work 85 | order: 9999 86 | release: 87 | footer: | 88 | **Full Changelog**: https://github.com/stulzq/azure-openai-proxy/compare/{{ .PreviousTag }}...{{ .Tag }} 89 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to azure-openai-proxy 2 | 3 | Git commit conventions: https://www.conventionalcommits.org/en/v1.0.0/ -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.19 AS builder 2 | 3 | COPY . /builder 4 | WORKDIR /builder 5 | 6 | RUN make build 7 | 8 | FROM alpine:3 9 | 10 | WORKDIR /app 11 | 12 | EXPOSE 8080 13 | COPY --from=builder /builder/bin . 14 | 15 | ENTRYPOINT ["/app/azure-openai-proxy"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | LDFLAGS := -s -w 2 | BIN_NAME := "azure-openai-proxy" 3 | 4 | build: 5 | @env CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o bin/$(BIN_NAME) ./cmd 6 | 7 | fmt: 8 | go fmt ./... 9 | 10 | vet: 11 | go vet ./... 12 | 13 | .PHONY: build fmt vet -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # azure-openai-proxy 2 | 3 | [![License](https://img.shields.io/github/license/koordinator-sh/koordinator.svg?color=4EB1BA&style=flat-square)](https://opensource.org/licenses/Apache-2.0) 4 | [![GitHub release](https://img.shields.io/github/v/release/stulzq/azure-openai-proxy.svg?style=flat-square)](https://github.com/stulzq/azure-openai-proxy/releases/latest) 5 | [![Go Report Card](https://goreportcard.com/badge/github.com/stulzq/azure-openai-proxy)](https://goreportcard.com/badge/github.com/stulzq/azure-openai-proxy) 6 | [![CI](https://img.shields.io/github/actions/workflow/status/stulzq/azure-openai-proxy/ci.yml?label=CI&logo=github&style=flat-square&branch=master)](https://github.com/stulzq/azure-openai-proxy/actions/workflows/ci.yml) 7 | [![Release](https://img.shields.io/github/actions/workflow/status/stulzq/azure-openai-proxy/release.yml?label=Release&logo=github&style=flat-square&branch=master)](https://github.com/stulzq/azure-openai-proxy/actions/workflows/release.yml) 8 | [![PRs Welcome](https://badgen.net/badge/PRs/welcome/green?icon=https://api.iconify.design/octicon:git-pull-request.svg?color=white&style=flat-square)](CONTRIBUTING.md) 9 | [![Docker Pulls](https://img.shields.io/docker/pulls/stulzq/azure-openai-proxy.svg?style=flat-square)]([https://hub.docker.com/u/stulzq](https://hub.docker.com/r/stulzq/azure-openai-proxy/tags)) 10 | 11 | English|[中文](https://www.cnblogs.com/stulzq/p/17271937.html) 12 | 13 | Azure OpenAI Service Proxy, convert OpenAI official API request to Azure OpenAI API request, support all models, support GPT-4,Embeddings. 14 | >Eliminate the differences between OpenAI and Azure OpenAI, acting as a bridge connecting them, OpenAI ecosystem accesses Azure OpenAI at zero cost. 15 | 16 | ![aoai-proxy.jpg](assets/images/aoai-proxy.jpg) 17 | 18 | Verified support projects: 19 | 20 | | Name | Status | 21 | | -------------------------------------------------------- | ------ | 22 | | [chatgpt-web](https://github.com/Chanzhaoyu/chatgpt-web) | √ | 23 | | [chatbox](https://github.com/Bin-Huang/chatbox) | √ | 24 | | [langchain](https://python.langchain.com/en/latest/) | √ | 25 | | [ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web) | √ | 26 | 27 | ## Get Start 28 | 29 | ### Retrieve key and endpoint 30 | 31 | To successfully make a call against Azure OpenAI, you'll need the following: 32 | 33 | | Name | Desc | Default | 34 | | --------------------- | ------------------------------------------------------------ | ----------------------------- | 35 | | AZURE_OPENAI_ENDPOINT | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://docs-test-001.openai.azure.com/`. | N | 36 | | AZURE_OPENAI_API_VER | [See here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart?tabs=command-line&pivots=rest-api) or Azure OpenAI Studio | 2024-02-01 | 37 | | AZURE_OPENAI_MODEL_MAPPER | This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under **Resource Management** > **Deployments** in the Azure portal or alternatively under **Management** > **Deployments** in Azure OpenAI Studio. | N | 38 | 39 | `AZURE_OPENAI_MODEL_MAPPER` is a mapping from Azure OpenAI deployed model names to official OpenAI model names. You can use commas to separate multiple mappings. 40 | 41 | **Format:** 42 | 43 | `AZURE_OPENAI_MODEL_MAPPER`: \=\ 44 | 45 | OpenAI Model Names: https://platform.openai.com/docs/models 46 | 47 | Azure Deployment Names: **Resource Management** > **Deployments** 48 | 49 | **Example:** 50 | 51 | ````yaml 52 | AZURE_OPENAI_MODEL_MAPPER: gpt-3.5-turbo=gpt-35-turbo 53 | ```` 54 | 55 | ![Screenshot of the overview UI for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red.](assets/images/endpoint.png) 56 | 57 | API Key: This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`. 58 | 59 | ### Proxy 60 | 61 | **HTTP Proxy** 62 | 63 | Env: 64 | 65 | ````shell 66 | AZURE_OPENAI_HTTP_PROXY=http://127.0.0.1:1087 67 | ```` 68 | 69 | 70 | 71 | **Socks5 Proxy** 72 | 73 | Env: 74 | 75 | ````shell 76 | AZURE_OPENAI_SOCKS_PROXY=socks5://127.0.0.1:1080 77 | ```` 78 | 79 | 80 | 81 | ### Use Docker 82 | 83 | ````shell 84 | # config by environment 85 | docker run -d -p 8080:8080 --name=azure-openai-proxy \ 86 | --env AZURE_OPENAI_ENDPOINT=your_azure_endpoint \ 87 | --env AZURE_OPENAI_API_VER=your_azure_api_ver \ 88 | --env AZURE_OPENAI_MODEL_MAPPER=your_azure_deploy_mapper \ 89 | stulzq/azure-openai-proxy:latest 90 | 91 | # config by file 92 | docker run -d -p 8080:8080 --name=azure-openai-proxy \ 93 | -v /path/to/config.yaml:/app/config.yaml \ 94 | stulzq/azure-openai-proxy:latest 95 | ```` 96 | 97 | Call API: 98 | 99 | ````shell 100 | curl --location --request POST 'localhost:8080/v1/chat/completions' \ 101 | -H 'Authorization: Bearer ' \ 102 | -H 'Content-Type: application/json' \ 103 | -d '{ 104 | "max_tokens": 1000, 105 | "model": "gpt-3.5-turbo", 106 | "temperature": 0.8, 107 | "top_p": 1, 108 | "presence_penalty": 1, 109 | "messages": [ 110 | { 111 | "role": "user", 112 | "content": "Hello" 113 | } 114 | ], 115 | "stream": true 116 | }' 117 | ```` 118 | 119 | ### Use ChatGPT-Next-Web 120 | 121 | ![chatgpt-web](assets/images/chatgpt-next-web.png) 122 | 123 | docker-compose.yml 124 | 125 | ````yaml 126 | version: '3' 127 | 128 | services: 129 | chatgpt-web: 130 | image: yidadaa/chatgpt-next-web 131 | ports: 132 | - 3000:3000 133 | environment: 134 | OPENAI_API_KEY: 135 | BASE_URL: http://azure-openai:8080 136 | CODE: "" 137 | HIDE_USER_API_KEY: 1 138 | HIDE_BALANCE_QUERY: 1 139 | depends_on: 140 | - azure-openai 141 | links: 142 | - azure-openai 143 | networks: 144 | - chatgpt-ns 145 | 146 | azure-openai: 147 | image: stulzq/azure-openai-proxy 148 | ports: 149 | - 8080:8080 150 | environment: 151 | AZURE_OPENAI_ENDPOINT: 152 | AZURE_OPENAI_MODEL_MAPPER: 153 | # AZURE_OPENAI_MODEL_MAPPER: gpt-4=gpt-4,gpt-3.5-turbo=gpt-35-turbo 154 | AZURE_OPENAI_API_VER: "2024-02-01" 155 | networks: 156 | - chatgpt-ns 157 | 158 | networks: 159 | chatgpt-ns: 160 | driver: bridge 161 | ```` 162 | 163 | ### Use ChatGPT-Web 164 | 165 | ChatGPT Web: https://github.com/Chanzhaoyu/chatgpt-web 166 | 167 | ![chatgpt-web](assets/images/chatgpt-web.png) 168 | 169 | Envs: 170 | 171 | - `OPENAI_API_KEY` Azure OpenAI API Key 172 | - `AZURE_OPENAI_ENDPOINT` Azure OpenAI API Endpoint 173 | - `AZURE_OPENAI_MODEL_MAPPER` Azure OpenAI API Deployment Name Mappings 174 | 175 | docker-compose.yml: 176 | 177 | ````yaml 178 | version: '3' 179 | 180 | services: 181 | chatgpt-web: 182 | image: chenzhaoyu94/chatgpt-web 183 | ports: 184 | - 3002:3002 185 | environment: 186 | OPENAI_API_KEY: 187 | OPENAI_API_BASE_URL: http://azure-openai:8080 188 | # OPENAI_API_MODEL: gpt-4 189 | AUTH_SECRET_KEY: "" 190 | MAX_REQUEST_PER_HOUR: 1000 191 | TIMEOUT_MS: 60000 192 | depends_on: 193 | - azure-openai 194 | links: 195 | - azure-openai 196 | networks: 197 | - chatgpt-ns 198 | 199 | azure-openai: 200 | image: stulzq/azure-openai-proxy 201 | ports: 202 | - 8080:8080 203 | environment: 204 | AZURE_OPENAI_ENDPOINT: 205 | AZURE_OPENAI_MODEL_MAPPER: 206 | AZURE_OPENAI_API_VER: "2024-02-01" 207 | networks: 208 | - chatgpt-ns 209 | 210 | networks: 211 | chatgpt-ns: 212 | driver: bridge 213 | ```` 214 | 215 | Run: 216 | 217 | ````shell 218 | docker compose up -d 219 | ```` 220 | 221 | ### Use Config File 222 | 223 | The configuration file supports different endpoints and API keys for each model. 224 | 225 | config.yaml 226 | 227 | ````yaml 228 | api_base: "/v1" 229 | deployment_config: 230 | - deployment_name: "xxx" 231 | model_name: "text-davinci-003" 232 | endpoint: "https://xxx-east-us.openai.azure.com/" 233 | api_key: "11111111111" 234 | api_version: "2024-02-01" 235 | - deployment_name: "yyy" 236 | model_name: "gpt-3.5-turbo" 237 | endpoint: "https://yyy.openai.azure.com/" 238 | api_key: "11111111111" 239 | api_version: "2024-02-01" 240 | - deployment_name: "zzzz" 241 | model_name: "text-embedding-ada-002" 242 | endpoint: "https://zzzz.openai.azure.com/" 243 | api_key: "11111111111" 244 | api_version: "2024-02-01" 245 | ```` 246 | 247 | By default, it reads `/config.yaml`, and you can pass the path through the parameter `-c config.yaml`. 248 | 249 | docker-compose: 250 | 251 | ````yaml 252 | azure-openai: 253 | image: stulzq/azure-openai-proxy 254 | ports: 255 | - 8080:8080 256 | volumes: 257 | - /path/to/config.yaml:/app/config.yaml 258 | networks: 259 | - chatgpt-ns 260 | ```` 261 | 262 | 263 | 264 | -------------------------------------------------------------------------------- /assets/images/aoai-proxy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stulzq/azure-openai-proxy/fda64266a9ed19de631401a2866ae08f289556e5/assets/images/aoai-proxy.jpg -------------------------------------------------------------------------------- /assets/images/chatgpt-next-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stulzq/azure-openai-proxy/fda64266a9ed19de631401a2866ae08f289556e5/assets/images/chatgpt-next-web.png -------------------------------------------------------------------------------- /assets/images/chatgpt-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stulzq/azure-openai-proxy/fda64266a9ed19de631401a2866ae08f289556e5/assets/images/chatgpt-web.png -------------------------------------------------------------------------------- /assets/images/endpoint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stulzq/azure-openai-proxy/fda64266a9ed19de631401a2866ae08f289556e5/assets/images/endpoint.png -------------------------------------------------------------------------------- /azure/init.go: -------------------------------------------------------------------------------- 1 | package azure 2 | 3 | import ( 4 | "fmt" 5 | "github.com/spf13/viper" 6 | "github.com/stulzq/azure-openai-proxy/constant" 7 | "github.com/stulzq/azure-openai-proxy/util" 8 | "log" 9 | "net/url" 10 | "path/filepath" 11 | "strings" 12 | ) 13 | 14 | const ( 15 | AuthHeaderKey = "api-key" 16 | ) 17 | 18 | var ( 19 | C Config 20 | ModelDeploymentConfig = map[string]DeploymentConfig{} 21 | ) 22 | 23 | func Init() error { 24 | var ( 25 | apiVersion string 26 | endpoint string 27 | openaiModelMapper string 28 | err error 29 | ) 30 | 31 | apiVersion = viper.GetString(constant.ENV_AZURE_OPENAI_API_VER) 32 | endpoint = viper.GetString(constant.ENV_AZURE_OPENAI_ENDPOINT) 33 | openaiModelMapper = viper.GetString(constant.ENV_AZURE_OPENAI_MODEL_MAPPER) 34 | if endpoint != "" { 35 | if apiVersion == "" { 36 | apiVersion = "2024-02-01" 37 | } 38 | InitFromEnvironmentVariables(apiVersion, endpoint, openaiModelMapper) 39 | } else { 40 | if err = InitFromConfigFile(); err != nil { 41 | return err 42 | } 43 | } 44 | 45 | // ensure apiBase likes /v1 46 | viper.SetDefault("api_base", "/v1") 47 | apiBase := viper.GetString("api_base") 48 | if !strings.HasPrefix(apiBase, "/") { 49 | apiBase = "/" + apiBase 50 | } 51 | if strings.HasSuffix(apiBase, "/") { 52 | apiBase = apiBase[:len(apiBase)-1] 53 | } 54 | viper.Set("api_base", apiBase) 55 | log.Printf("apiBase is: %s", apiBase) 56 | for _, itemConfig := range C.DeploymentConfig { 57 | u, err := url.Parse(itemConfig.Endpoint) 58 | if err != nil { 59 | return fmt.Errorf("parse endpoint error: %w", err) 60 | } 61 | itemConfig.EndpointUrl = u 62 | ModelDeploymentConfig[itemConfig.ModelName] = itemConfig 63 | } 64 | return err 65 | } 66 | 67 | func InitFromEnvironmentVariables(apiVersion, endpoint, openaiModelMapper string) { 68 | log.Println("Init from environment variables") 69 | if openaiModelMapper != "" { 70 | // openaiModelMapper example: 71 | // gpt-3.5-turbo=deployment_name_for_gpt_model,text-davinci-003=deployment_name_for_davinci_model 72 | for _, pair := range strings.Split(openaiModelMapper, ",") { 73 | info := strings.Split(pair, "=") 74 | if len(info) != 2 { 75 | log.Fatalf("error parsing %s, invalid value %s", constant.ENV_AZURE_OPENAI_MODEL_MAPPER, pair) 76 | } 77 | modelName, deploymentName := info[0], info[1] 78 | u, err := url.Parse(endpoint) 79 | if err != nil { 80 | log.Fatalf("parse endpoint error: %s", err.Error()) 81 | } 82 | ModelDeploymentConfig[modelName] = DeploymentConfig{ 83 | DeploymentName: deploymentName, 84 | ModelName: modelName, 85 | Endpoint: endpoint, 86 | EndpointUrl: u, 87 | ApiKey: "", 88 | ApiVersion: apiVersion, 89 | } 90 | } 91 | } 92 | } 93 | 94 | func InitFromConfigFile() error { 95 | log.Println("Init from config file") 96 | 97 | configFile := viper.GetString("configFile") 98 | if configFile == "" { 99 | configFile = filepath.Join(util.GetWorkdir(), "config.yaml") 100 | } else if !filepath.IsAbs(configFile) { 101 | configFile = filepath.Join(util.GetWorkdir(), configFile) 102 | } 103 | 104 | viper.SetConfigType("yaml") 105 | viper.SetConfigFile(configFile) 106 | if err := viper.ReadInConfig(); err != nil { 107 | log.Printf("read config file error: %+v\n", err) 108 | return err 109 | } 110 | 111 | if err := viper.Unmarshal(&C); err != nil { 112 | log.Printf("unmarshal config file error: %+v\n", err) 113 | return err 114 | } 115 | for _, configItem := range C.DeploymentConfig { 116 | ModelDeploymentConfig[configItem.ModelName] = configItem 117 | } 118 | 119 | log.Println("read config file success") 120 | return nil 121 | } 122 | -------------------------------------------------------------------------------- /azure/model.go: -------------------------------------------------------------------------------- 1 | package azure 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "github.com/pkg/errors" 7 | "log" 8 | "net/http" 9 | "net/url" 10 | "path" 11 | "strings" 12 | "text/template" 13 | ) 14 | 15 | type DeploymentConfig struct { 16 | DeploymentName string `yaml:"deployment_name" json:"deployment_name" mapstructure:"deployment_name"` // azure openai deployment name 17 | ModelName string `yaml:"model_name" json:"model_name" mapstructure:"model_name"` // corresponding model name in openai 18 | Endpoint string `yaml:"endpoint" json:"endpoint" mapstructure:"endpoint"` // deployment endpoint 19 | ApiKey string `yaml:"api_key" json:"api_key" mapstructure:"api_key"` // secrect key1 or 2 20 | ApiVersion string `yaml:"api_version" json:"api_version" mapstructure:"api_version"` // deployment version, not required 21 | EndpointUrl *url.URL // url.URL form deployment endpoint 22 | } 23 | 24 | type Config struct { 25 | ApiBase string `yaml:"api_base" mapstructure:"api_base"` // if you use openai、langchain as sdk, it will be useful 26 | DeploymentConfig []DeploymentConfig `yaml:"deployment_config" mapstructure:"deployment_config"` // deployment config 27 | } 28 | 29 | type RequestConverter interface { 30 | Name() string 31 | Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error) 32 | } 33 | 34 | type StripPrefixConverter struct { 35 | Prefix string 36 | } 37 | 38 | func (c *StripPrefixConverter) Name() string { 39 | return "StripPrefix" 40 | } 41 | func (c *StripPrefixConverter) Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error) { 42 | req.Host = config.EndpointUrl.Host 43 | req.URL.Scheme = config.EndpointUrl.Scheme 44 | req.URL.Host = config.EndpointUrl.Host 45 | req.URL.Path = path.Join(fmt.Sprintf("/openai/deployments/%s", config.DeploymentName), strings.Replace(req.URL.Path, c.Prefix+"/", "/", 1)) 46 | req.URL.RawPath = req.URL.EscapedPath() 47 | 48 | query := req.URL.Query() 49 | query.Add("api-version", config.ApiVersion) 50 | req.URL.RawQuery = query.Encode() 51 | return req, nil 52 | } 53 | func NewStripPrefixConverter(prefix string) *StripPrefixConverter { 54 | return &StripPrefixConverter{ 55 | Prefix: prefix, 56 | } 57 | } 58 | 59 | type TemplateConverter struct { 60 | Tpl string 61 | Tempalte *template.Template 62 | } 63 | 64 | func (c *TemplateConverter) Name() string { 65 | return "Template" 66 | } 67 | func (c *TemplateConverter) Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error) { 68 | data := map[string]interface{}{ 69 | "DeploymentName": config.DeploymentName, 70 | "ModelName": config.ModelName, 71 | "Endpoint": config.Endpoint, 72 | "ApiKey": config.ApiKey, 73 | "ApiVersion": config.ApiVersion, 74 | } 75 | buff := new(bytes.Buffer) 76 | if err := c.Tempalte.Execute(buff, data); err != nil { 77 | return req, errors.Wrap(err, "template execute error") 78 | } 79 | 80 | req.Host = config.EndpointUrl.Host 81 | req.URL.Scheme = config.EndpointUrl.Scheme 82 | req.URL.Host = config.EndpointUrl.Host 83 | req.URL.Path = buff.String() 84 | req.URL.RawPath = req.URL.EscapedPath() 85 | 86 | query := req.URL.Query() 87 | query.Add("api-version", config.ApiVersion) 88 | req.URL.RawQuery = query.Encode() 89 | return req, nil 90 | } 91 | func NewTemplateConverter(tpl string) *TemplateConverter { 92 | _template, err := template.New("template").Parse(tpl) 93 | if err != nil { 94 | log.Fatalf("template parse error: %s", err.Error()) 95 | } 96 | return &TemplateConverter{ 97 | Tpl: tpl, 98 | Tempalte: _template, 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /azure/proxy.go: -------------------------------------------------------------------------------- 1 | package azure 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "github.com/stulzq/azure-openai-proxy/util" 8 | "io" 9 | "log" 10 | "net/http" 11 | "net/http/httputil" 12 | "strings" 13 | 14 | "github.com/bytedance/sonic" 15 | "github.com/gin-gonic/gin" 16 | "github.com/pkg/errors" 17 | ) 18 | 19 | func ProxyWithConverter(requestConverter RequestConverter) gin.HandlerFunc { 20 | return func(c *gin.Context) { 21 | Proxy(c, requestConverter) 22 | } 23 | } 24 | 25 | type DeploymentInfo struct { 26 | Data []map[string]interface{} `json:"data"` 27 | Object string `json:"object"` 28 | } 29 | 30 | func ModelProxy(c *gin.Context) { 31 | // Create a channel to receive the results of each request 32 | results := make(chan []map[string]interface{}, len(ModelDeploymentConfig)) 33 | 34 | // Send a request for each deployment in the map 35 | for _, deployment := range ModelDeploymentConfig { 36 | go func(deployment DeploymentConfig) { 37 | // Create the request 38 | req, err := http.NewRequest(http.MethodGet, deployment.Endpoint+"/openai/deployments?api-version=2022-12-01", nil) 39 | if err != nil { 40 | log.Printf("error parsing response body for deployment %s: %v", deployment.DeploymentName, err) 41 | results <- nil 42 | return 43 | } 44 | 45 | // Set the auth header 46 | req.Header.Set(AuthHeaderKey, deployment.ApiKey) 47 | 48 | // Send the request 49 | client := &http.Client{} 50 | resp, err := client.Do(req) 51 | if err != nil { 52 | log.Printf("error sending request for deployment %s: %v", deployment.DeploymentName, err) 53 | results <- nil 54 | return 55 | } 56 | defer resp.Body.Close() 57 | if resp.StatusCode != http.StatusOK { 58 | log.Printf("unexpected status code %d for deployment %s", resp.StatusCode, deployment.DeploymentName) 59 | results <- nil 60 | return 61 | } 62 | 63 | // Read the response body 64 | body, err := io.ReadAll(resp.Body) 65 | if err != nil { 66 | log.Printf("error reading response body for deployment %s: %v", deployment.DeploymentName, err) 67 | results <- nil 68 | return 69 | } 70 | 71 | // Parse the response body as JSON 72 | var deplotmentInfo DeploymentInfo 73 | err = json.Unmarshal(body, &deplotmentInfo) 74 | if err != nil { 75 | log.Printf("error parsing response body for deployment %s: %v", deployment.DeploymentName, err) 76 | results <- nil 77 | return 78 | } 79 | results <- deplotmentInfo.Data 80 | }(deployment) 81 | } 82 | 83 | // Wait for all requests to finish and collect the results 84 | var allResults []map[string]interface{} 85 | for i := 0; i < len(ModelDeploymentConfig); i++ { 86 | result := <-results 87 | if result != nil { 88 | allResults = append(allResults, result...) 89 | } 90 | } 91 | var info = DeploymentInfo{Data: allResults, Object: "list"} 92 | combinedResults, err := json.Marshal(info) 93 | if err != nil { 94 | log.Printf("error marshalling results: %v", err) 95 | util.SendError(c, err) 96 | return 97 | } 98 | 99 | // Set the response headers and body 100 | c.Header("Content-Type", "application/json") 101 | c.String(http.StatusOK, string(combinedResults)) 102 | } 103 | 104 | // Proxy Azure OpenAI 105 | func Proxy(c *gin.Context, requestConverter RequestConverter) { 106 | if c.Request.Method == http.MethodOptions { 107 | c.Header("Access-Control-Allow-Origin", "*") 108 | c.Header("Access-Control-Allow-Methods", "GET, OPTIONS, POST") 109 | c.Header("Access-Control-Allow-Headers", "Authorization, Content-Type") 110 | c.Status(200) 111 | return 112 | } 113 | 114 | // preserve request body for error logging 115 | var buf bytes.Buffer 116 | tee := io.TeeReader(c.Request.Body, &buf) 117 | bodyBytes, err := io.ReadAll(tee) 118 | if err != nil { 119 | log.Printf("Error reading request body: %v", err) 120 | return 121 | } 122 | c.Request.Body = io.NopCloser(&buf) 123 | 124 | director := func(req *http.Request) { 125 | if req.Body == nil { 126 | util.SendError(c, errors.New("request body is empty")) 127 | return 128 | } 129 | body, _ := io.ReadAll(req.Body) 130 | req.Body = io.NopCloser(bytes.NewBuffer(body)) 131 | 132 | // get model from url params or body 133 | model := c.Param("model") 134 | if model == "" { 135 | _model, err := sonic.Get(body, "model") 136 | if err != nil { 137 | util.SendError(c, errors.Wrap(err, "get model error")) 138 | return 139 | } 140 | _modelStr, err := _model.String() 141 | if err != nil { 142 | util.SendError(c, errors.Wrap(err, "get model name error")) 143 | return 144 | } 145 | model = _modelStr 146 | } 147 | 148 | // get deployment from request 149 | deployment, err := GetDeploymentByModel(model) 150 | if err != nil { 151 | util.SendError(c, err) 152 | return 153 | } 154 | 155 | // get auth token from header or deployemnt config 156 | token := deployment.ApiKey 157 | if token == "" { 158 | rawToken := req.Header.Get("Authorization") 159 | token = strings.TrimPrefix(rawToken, "Bearer ") 160 | } 161 | if token == "" { 162 | util.SendError(c, errors.New("token is empty")) 163 | return 164 | } 165 | req.Header.Set(AuthHeaderKey, token) 166 | req.Header.Del("Authorization") 167 | 168 | originURL := req.URL.String() 169 | req, err = requestConverter.Convert(req, deployment) 170 | if err != nil { 171 | util.SendError(c, errors.Wrap(err, "convert request error")) 172 | return 173 | } 174 | log.Printf("proxying request [%s] %s -> %s", model, originURL, req.URL.String()) 175 | } 176 | 177 | proxy := &httputil.ReverseProxy{Director: director} 178 | transport, err := util.NewProxyFromEnv() 179 | if err != nil { 180 | util.SendError(c, errors.Wrap(err, "get proxy error")) 181 | return 182 | } 183 | if transport != nil { 184 | proxy.Transport = transport 185 | } 186 | 187 | proxy.ServeHTTP(c.Writer, c.Request) 188 | 189 | // issue: https://github.com/Chanzhaoyu/chatgpt-web/issues/831 190 | if c.Writer.Header().Get("Content-Type") == "text/event-stream" { 191 | if _, err := c.Writer.Write([]byte{'\n'}); err != nil { 192 | log.Printf("rewrite response error: %v", err) 193 | } 194 | } 195 | 196 | if c.Writer.Status() != 200 { 197 | log.Printf("encountering error with body: %s", string(bodyBytes)) 198 | } 199 | } 200 | 201 | func GetDeploymentByModel(model string) (*DeploymentConfig, error) { 202 | deploymentConfig, exist := ModelDeploymentConfig[model] 203 | if !exist { 204 | return nil, errors.New(fmt.Sprintf("deployment config for %s not found", model)) 205 | } 206 | return &deploymentConfig, nil 207 | } 208 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | VERSION=v1.1.0 6 | 7 | rm -rf bin 8 | 9 | export GOOS=linux 10 | export GOARCH=amd64 11 | go build -trimpath -ldflags "-s -w" -o bin/azure-openai-proxy ./cmd 12 | 13 | docker build -t stulzq/azure-openai-proxy:$VERSION . -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "github.com/spf13/pflag" 7 | "github.com/spf13/viper" 8 | "github.com/stulzq/azure-openai-proxy/azure" 9 | "log" 10 | "net/http" 11 | "os" 12 | "os/signal" 13 | "syscall" 14 | 15 | "github.com/gin-gonic/gin" 16 | "github.com/pkg/errors" 17 | ) 18 | 19 | var ( 20 | version = "" 21 | buildDate = "" 22 | gitCommit = "" 23 | ) 24 | 25 | func main() { 26 | viper.AutomaticEnv() 27 | parseFlag() 28 | 29 | err := azure.Init() 30 | if err != nil { 31 | panic(err) 32 | } 33 | 34 | gin.SetMode(gin.ReleaseMode) 35 | r := gin.Default() 36 | 37 | // if viper get cors is true, then apply corsMiddleware 38 | if viper.GetBool("cors") { 39 | log.Printf("CORS supported! \n") 40 | r.Use(corsMiddleware()) 41 | } 42 | 43 | registerRoute(r) 44 | 45 | srv := &http.Server{ 46 | Addr: viper.GetString("listen"), 47 | Handler: r, 48 | } 49 | 50 | runServer(srv) 51 | } 52 | 53 | // corsMiddleware sets up the CORS headers for all responses 54 | func corsMiddleware() gin.HandlerFunc { 55 | return func(c *gin.Context) { 56 | // Clear any previously set headers 57 | if c.Request.Method != "POST" { 58 | c.Writer.Header().Set("Access-Control-Allow-Origin", "*") 59 | } 60 | c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") 61 | c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Stainless-OS, X-STAINLESS-LANG, X-STAINLESS-PACKAGE-VERSION, X-STAINLESS-RUNTIME, X-STAINLESS-RUNTIME-VERSION, X-STAINLESS-ARCH") 62 | c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") 63 | 64 | // Handle preflight requests 65 | if c.Request.Method == "OPTIONS" { 66 | c.AbortWithStatus(200) 67 | return 68 | } 69 | c.Next() 70 | } 71 | } 72 | 73 | func runServer(srv *http.Server) { 74 | go func() { 75 | log.Printf("Server listening at %s\n", srv.Addr) 76 | if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { 77 | panic(errors.Errorf("listen: %s\n", err)) 78 | } 79 | }() 80 | 81 | quit := make(chan os.Signal, 1) 82 | signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) 83 | <-quit 84 | 85 | log.Println("Server Shutdown...") 86 | if err := srv.Shutdown(context.Background()); err != nil { 87 | log.Fatal("Server Shutdown:", err) 88 | } 89 | log.Println("Server exiting") 90 | } 91 | 92 | func parseFlag() { 93 | pflag.StringP("configFile", "c", "config.yaml", "config file") 94 | pflag.StringP("listen", "l", ":8080", "listen address") 95 | pflag.BoolP("version", "v", false, "version information") 96 | pflag.BoolP("cors", "s", false, "cors support") 97 | pflag.Parse() 98 | if err := viper.BindPFlags(pflag.CommandLine); err != nil { 99 | panic(err) 100 | } 101 | if viper.GetBool("version") { 102 | fmt.Println("version:", version) 103 | fmt.Println("buildDate:", buildDate) 104 | fmt.Println("gitCommit:", gitCommit) 105 | os.Exit(0) 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /cmd/router.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | "github.com/spf13/viper" 6 | "github.com/stulzq/azure-openai-proxy/azure" 7 | ) 8 | 9 | // registerRoute registers all routes 10 | func registerRoute(r *gin.Engine) { 11 | // https://platform.openai.com/docs/api-reference 12 | r.HEAD("/", func(c *gin.Context) { 13 | c.Status(200) 14 | }) 15 | r.Any("/health", func(c *gin.Context) { 16 | c.Status(200) 17 | }) 18 | apiBase := viper.GetString("api_base") 19 | stripPrefixConverter := azure.NewStripPrefixConverter(apiBase) 20 | r.GET(stripPrefixConverter.Prefix+"/models", azure.ModelProxy) 21 | templateConverter := azure.NewTemplateConverter("/openai/deployments/{{.DeploymentName}}/embeddings") 22 | apiBasedRouter := r.Group(apiBase) 23 | { 24 | apiBasedRouter.Any("/engines/:model/embeddings", azure.ProxyWithConverter(templateConverter)) 25 | apiBasedRouter.Any("/completions", azure.ProxyWithConverter(stripPrefixConverter)) 26 | apiBasedRouter.Any("/chat/completions", azure.ProxyWithConverter(stripPrefixConverter)) 27 | apiBasedRouter.Any("/embeddings", azure.ProxyWithConverter(stripPrefixConverter)) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /config/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stulzq/azure-openai-proxy/fda64266a9ed19de631401a2866ae08f289556e5/config/.gitkeep -------------------------------------------------------------------------------- /config/config.example.yaml: -------------------------------------------------------------------------------- 1 | api_base: "/v1" 2 | deployment_config: 3 | - deployment_name: "xxx" 4 | model_name: "text-davinci-003" 5 | endpoint: "https://xxx-east-us.openai.azure.com/" 6 | api_key: "11111111111" 7 | api_version: "2024-02-01" 8 | - deployment_name: "yyy" 9 | model_name: "gpt-3.5-turbo" 10 | endpoint: "https://yyy.openai.azure.com/" 11 | api_key: "11111111111" 12 | api_version: "2024-02-01" 13 | - deployment_name: "zzzz" 14 | model_name: "text-embedding-ada-002" 15 | endpoint: "https://zzzz.openai.azure.com/" 16 | api_key: "11111111111" 17 | api_version: "2024-02-01" -------------------------------------------------------------------------------- /constant/env.go: -------------------------------------------------------------------------------- 1 | package constant 2 | 3 | const ( 4 | ENV_AZURE_OPENAI_ENDPOINT = "AZURE_OPENAI_ENDPOINT" 5 | ENV_AZURE_OPENAI_API_VER = "AZURE_OPENAI_API_VER" 6 | ENV_AZURE_OPENAI_MODEL_MAPPER = "AZURE_OPENAI_MODEL_MAPPER" 7 | 8 | ENV_AZURE_OPENAI_HTTP_PROXY = "AZURE_OPENAI_HTTP_PROXY" 9 | ENV_AZURE_OPENAI_SOCKS_PROXY = "AZURE_OPENAI_SOCKS_PROXY" 10 | ) 11 | -------------------------------------------------------------------------------- /examples/simple-langchain-examples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true, 8 | "ExecuteTime": { 9 | "end_time": "2023-06-16T08:35:26.876853Z", 10 | "start_time": "2023-06-16T08:35:24.737223Z" 11 | } 12 | }, 13 | "outputs": [ 14 | { 15 | "name": "stdout", 16 | "output_type": "stream", 17 | "text": [ 18 | "Looking in indexes: https://pypi.douban.com/simple/\r\n", 19 | "Requirement already satisfied: langchain in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (0.0.200)\r\n", 20 | "Requirement already satisfied: PyYAML>=5.4.1 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (6.0)\r\n", 21 | "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (2.0.15)\r\n", 22 | "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (3.8.4)\r\n", 23 | "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (4.0.2)\r\n", 24 | "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (0.5.7)\r\n", 25 | "Requirement already satisfied: langchainplus-sdk>=0.0.9 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (0.0.10)\r\n", 26 | "Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (2.8.4)\r\n", 27 | "Requirement already satisfied: numpy<2,>=1 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (1.23.5)\r\n", 28 | "Requirement already satisfied: openapi-schema-pydantic<2.0,>=1.2 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (1.2.4)\r\n", 29 | "Requirement already satisfied: pydantic<2,>=1 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (1.10.7)\r\n", 30 | "Requirement already satisfied: requests<3,>=2 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (2.28.2)\r\n", 31 | "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from langchain) (8.2.2)\r\n", 32 | "Requirement already satisfied: attrs>=17.3.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.1.0)\r\n", 33 | "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (3.1.0)\r\n", 34 | "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\r\n", 35 | "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)\r\n", 36 | "Requirement already satisfied: frozenlist>=1.1.1 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\r\n", 37 | "Requirement already satisfied: aiosignal>=1.1.2 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\r\n", 38 | "Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)\r\n", 39 | "Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)\r\n", 40 | "Requirement already satisfied: typing-inspect>=0.4.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.8.0)\r\n", 41 | "Requirement already satisfied: typing-extensions>=4.2.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from pydantic<2,>=1->langchain) (4.5.0)\r\n", 42 | "Requirement already satisfied: idna<4,>=2.5 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from requests<3,>=2->langchain) (3.4)\r\n", 43 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from requests<3,>=2->langchain) (1.26.15)\r\n", 44 | "Requirement already satisfied: certifi>=2017.4.17 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from requests<3,>=2->langchain) (2022.12.7)\r\n", 45 | "Requirement already satisfied: packaging>=17.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (23.1)\r\n", 46 | "Requirement already satisfied: mypy-extensions>=0.3.0 in /Users/dingwenjiang/.pyenv/versions/3.10.11/envs/openai310/lib/python3.10/site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (1.0.0)\r\n" 47 | ] 48 | } 49 | ], 50 | "source": [ 51 | "!pip install langchain --upgrade" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "source": [ 57 | "# LLM Example\n", 58 | "In LLM example, we will use `text-davinci-003` model, and use Azure as the proxy server." 59 | ], 60 | "metadata": { 61 | "collapsed": false 62 | } 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 2, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | " I have always had a love for colorful socks. I have seen a lot of sock companies around, but I want to create one that is fun and colorful.\n", 73 | "\n", 74 | "How about \"Funky Footwear\" or \"Sock It To Me\". I'm sure there are many more clever names that will be suggested. When you come up with a name, be sure to let us know the name of your company.\n", 75 | "\n", 76 | "By Cricket [205 Posts, 895 Comments] September 28, 2011 0 found this helpful\n", 77 | "\n", 78 | "How about \"Colorful Toes\" or \"Sock Hop\" or \"Sock Opera\"?\n", 79 | "\n", 80 | "Ad\n", 81 | "\n", 82 | "Or you could use a play on words and use your name. Such as if your name is Jones, you could call it \"Jonesing for Socks\". Or if your name is Green, you could call it \"Green Sock\" (sounds like Grinch, so you could even play off that!).\n", 83 | "\n", 84 | "Reply Was this helpful? Yes\n", 85 | "\n", 86 | "By Donna [413 Posts, 402 Comments] September 28, 2011 0 found this helpful\n", 87 | "\n", 88 | "Sock It To Me is a great name! How about \"Feet Treats\" or \"Colorful Soles\" or \"Sock It Up?\" I love the idea of a new sock company and\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "from langchain.llms import OpenAI\n", 94 | "llm = OpenAI(\n", 95 | " openai_api_key=\"ANYTHING or None, if set None, plz ensure you have set azure apikey in the proxy server\",\n", 96 | " openai_api_base=\"http://127.0.0.1:8080/v1\"\n", 97 | ")\n", 98 | "text = \"What would be a good company name for a company that makes colorful socks?\"\n", 99 | "print(llm(text))" 100 | ], 101 | "metadata": { 102 | "collapsed": false, 103 | "ExecuteTime": { 104 | "end_time": "2023-06-16T08:35:40.753272Z", 105 | "start_time": "2023-06-16T08:35:30.493555Z" 106 | } 107 | } 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "source": [ 112 | "# Chat Example\n", 113 | "In Chat example, we will use `gpt-3.5-turbo` model." 114 | ], 115 | "metadata": { 116 | "collapsed": false 117 | } 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 3, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "content=\"J'aime programmer.\" additional_kwargs={} example=False\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "from langchain.chat_models import ChatOpenAI\n", 133 | "from langchain.schema import (\n", 134 | " AIMessage,\n", 135 | " HumanMessage,\n", 136 | " SystemMessage\n", 137 | ")\n", 138 | "chat = ChatOpenAI(\n", 139 | " openai_api_key=\"ANYTHING or None, if set None, plz ensure you have set azure apikey in the proxy server\",\n", 140 | " openai_api_base=\"http://127.0.0.1:8080/v1\",\n", 141 | " temperature=0\n", 142 | ")\n", 143 | "print(chat([HumanMessage(content=\"Translate this sentence from English to French. I love programming.\")]))" 144 | ], 145 | "metadata": { 146 | "collapsed": false, 147 | "ExecuteTime": { 148 | "end_time": "2023-06-16T08:35:56.013685Z", 149 | "start_time": "2023-06-16T08:35:55.380708Z" 150 | } 151 | } 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "source": [ 156 | "# Embedding Example\n", 157 | "In Embedding example, we will use `text-embedding-ada-002` model. After embedding, we always get 1536 dimension vector." 158 | ], 159 | "metadata": { 160 | "collapsed": false 161 | } 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 4, 166 | "outputs": [ 167 | { 168 | "name": "stdout", 169 | "output_type": "stream", 170 | "text": [ 171 | "[-0.003158408682793379, 0.011094409972429276, -0.0040013170801103115, -0.011747414246201515, -0.0010153218172490597, 0.010781234130263329, -0.010368109680712223, -0.005297331139445305, -0.009881687350571156, -0.02616015262901783, 0.020376399159431458, 0.022575292736291885, -0.007522876374423504, 0.01728462427854538, -0.006003641523420811, 0.01912369765341282, 0.02125595696270466, -0.015645449981093407, 0.007669468875974417, -0.018364081159234047, -0.0006909018848091364, -0.006416766904294491, -0.01098113413900137, 0.017950955778360367, -0.02213551290333271, -0.003076783148571849, 0.01437942124903202, -0.029984891414642334, 0.01852400042116642, -0.007916011847555637, 0.010068260133266449, -0.019456863403320312, -0.003688141703605652, -0.024241119623184204, -0.005530546884983778, 0.003824739484116435, -0.005653818137943745, -0.029451826587319374, 0.018603960052132607, -0.016618292778730392, 0.00752953952178359, 0.012420408427715302, -0.0009903343161568046, -0.010721264407038689, 0.01000162772834301, -0.012560337781906128, 0.033529773354530334, -0.02699972875416279, -0.00792933814227581, 0.029025375843048096, 0.019696742296218872, -0.002077286597341299, -0.02739952877163887, -0.008775578811764717, -0.006213536020368338, 0.0019939953926950693, -0.00738294655457139, 0.02798589877784252, 0.006829892285168171, -0.009315306320786476, -0.02248200587928295, 0.010468059219419956, -0.013546507805585861, -0.011094409972429276, -0.004887537565082312, -0.008275830186903477, -0.005823732353746891, 0.011114399880170822, 0.006276837550103664, 0.004434432368725538, 0.01056800875812769, 0.012480378150939941, 0.005337310954928398, 0.005393948871642351, 0.008988804183900356, 0.004261186346411705, -0.022681904956698418, -0.013233331963419914, 0.011201023124158382, -0.005753767676651478, 0.00982838124036789, -0.024401038885116577, -0.00858234241604805, 0.007829388603568077, 0.0025037385057657957, 0.007549529429525137, 0.005747104529291391, 0.02817247249186039, -0.02290845662355423, -0.015525510534644127, -0.008182544261217117, 0.02005656063556671, 0.003568202257156372, 0.012786890380084515, -0.01645837351679802, 0.03155743330717087, 0.013506527990102768, 0.030437996610999107, -0.006240189075469971, -0.049681633710861206, -0.003728121519088745, -0.0013001782353967428, -0.007063108030706644, 0.003524890635162592, -0.034835781902074814, -0.009062101133167744, 0.021975593641400337, -0.007236354053020477, 0.015485530719161034, -0.008295820094645023, -0.01644504815340042, 0.005060783587396145, -0.006390113849192858, -0.0361151359975338, 0.015618797391653061, 0.005653818137943745, 0.006503389682620764, -0.02189563401043415, -0.010248169302940369, 0.0010503041557967663, 0.0006350966868922114, 0.016938133165240288, 0.02651997096836567, -0.027506140992045403, 0.010548018850386143, 0.013273311778903008, -0.00882222130894661, -0.041872236877679825, -0.0064933947287499905, -0.005567194893956184, 0.019137024879455566, 0.02148250862956047, 0.014192848466336727, 0.01751117780804634, -0.023348236456513405, 0.03734118491411209, -0.04040630906820297, 0.024774184450507164, -0.0432048961520195, -0.00840909592807293, 0.008309146389365196, 0.027372874319553375, -0.019150350242853165, -0.016964785754680634, 0.010288150049746037, 0.01399294938892126, 0.008262503892183304, -0.024840816855430603, 0.012493705376982689, -0.021056057885289192, 0.0031051021069288254, 0.0017724402714520693, 0.0177643820643425, 0.0025603766553103924, 0.01556549035012722, 0.03147747367620468, 0.003858056152239442, -0.009548522531986237, -0.01800426095724106, -0.019616782665252686, 0.0031034364365041256, 0.022162167355418205, 0.009568512439727783, 0.0018540658056735992, 0.023454848676919937, 0.03467586264014244, 0.004004648886620998, 0.007669468875974417, -0.0016133537283167243, -0.01541889738291502, -0.02502739056944847, 0.024760857224464417, -0.02538720890879631, 0.00764947896823287, -0.02017650008201599, 0.014299461618065834, 0.00718971062451601, 0.003481579013168812, -0.004424437414854765, -0.004054623655974865, -0.03390291705727577, 0.016844846308231354, 0.0147925466299057, 0.03185061737895012, -0.017004765570163727, -0.0069365049712359905, 0.024241119623184204, -0.009541858918964863, 0.001388467033393681, -0.0046343314461410046, 0.02314833737909794, 0.021935613825917244, -0.006290163844823837, -0.014805872924625874, -0.7023661136627197, -0.016351761296391487, 0.004261186346411705, -0.017311276867985725, 0.022175492718815804, 0.031370859593153, 0.024640917778015137, 0.0020939449314028025, -0.005986983422189951, 0.021855654194951057, -0.02266857773065567, -0.012700267136096954, -0.00920203048735857, -0.013206679373979568, -0.014139542356133461, -0.02314833737909794, -0.016951458528637886, -0.003324991324916482, -0.017604462802410126, 0.013106729835271835, 0.014979119412600994, 0.01547220442444086, -0.015712084248661995, -0.016711579635739326, 0.0005126583855599165, 0.00893549807369709, 0.013100066222250462, -0.006123581435531378, 0.017484523355960846, 0.016884826123714447, -0.026240112259984016, -0.0035215590614825487, -0.0009320303797721863, 0.008535698987543583, 0.033236585557460785, -0.0020672916434705257, -0.014472708106040955, -0.02504071593284607, 0.016604967415332794, 0.0009203695808537304, -0.01680486649274826, -0.00034961552591994405, 0.004830899182707071, -0.001729128765873611, -0.004571030382066965, -0.0024737536441534758, 0.01883051171898842, 0.0003673149331007153, 0.027452833950519562, 0.017684422433376312, -0.0165783129632473, -0.0005447255098260939, 0.006663309410214424, -0.003518227254971862, 0.0010453066788613796, -0.01042141579091549, 0.02160244807600975, 0.007756092119961977, 0.01083454117178917, 0.016604967415332794, -0.0011510866461321712, 0.011860691010951996, -0.005657149478793144, -0.0168714988976717, -0.02894541621208191, -0.0074962228536605835, -0.02077619917690754, 0.022535312920808792, 0.0010594661580398679, -0.01899043098092079, -0.003758106380701065, 0.012520357966423035, -0.010261496528983116, -0.019723394885659218, 0.020749544724822044, 0.015485530719161034, 0.009761747904121876, -0.00994832068681717, -0.00935528613626957, 0.016778212040662766, 0.0037014682311564684, 0.003621508600190282, -0.014366094954311848, -0.01541889738291502, 0.010847867466509342, -0.020283114165067673, -0.04611010104417801, -0.012960136868059635, -0.0066999574191868305, 0.013386588543653488, 0.02160244807600975, 0.0014384419191628695, -0.00795599166303873, -0.00046601518988609314, 0.00890884455293417, -0.0028002557810395956, 0.001188567839562893, 0.003934684209525585, 0.030091505497694016, -0.0019306938629597425, 0.0015000775456428528, 0.002447100356221199, 0.0006804904551245272, 0.011740750633180141, 0.02112269029021263, 0.005224034655839205, 0.0036481618881225586, 0.024467671290040016, 0.018270794302225113, -0.00849571917206049, -0.007169720716774464, -0.00899546779692173, -0.009535195305943489, 0.0015650447458028793, -0.016071902588009834, -0.03416945040225983, 0.00798930786550045, 0.011241002939641476, 0.002340487437322736, 0.0033899585250765085, 0.014059582725167274, 0.017724402248859406, 0.013299965299665928, -0.0016316778492182493, 0.004584356676787138, -0.0024887460749596357, 0.0039746640250086784, -0.019430210813879967, -0.017204664647579193, -0.009701778180897236, -0.019456863403320312, -0.0017857669154182076, 0.0337163470685482, -0.014166195876896381, 0.0077027855440974236, 0.0023038391955196857, 0.003831402864307165, -0.005277340766042471, 0.021349243819713593, -0.009748421609401703, -0.013020106591284275, 0.0133132915943861, -0.006963158026337624, -0.009068763814866543, -0.011953976936638355, -0.02230875939130783, -0.018017588183283806, -0.0030268083792179823, 0.0009944989578798413, 0.009941657073795795, -0.02206888049840927, 0.0014101228443905711, -0.010128229856491089, -0.0058470540679991245, -0.008095921017229557, -0.011081083677709103, -0.01704474538564682, -0.026293419301509857, 0.0010411420371383429, -0.0014725913060829043, -0.008648975752294064, 0.01757781021296978, -0.006223530974239111, 0.0011360942153260112, -0.026533298194408417, -0.018310774117708206, -0.03302336111664772, 0.03318328037858009, -0.009988300502300262, -0.033876266330480576, -0.0183507539331913, 0.005207376088947058, -0.013473211787641048, -0.0020423042587935925, 0.005070778541266918, -0.010587998665869236, -0.0156854297965765, 0.007862704806029797, 0.01032812986522913, -0.021349243819713593, -0.004967497196048498, 0.0034749158658087254, 0.012227172963321209, 0.0020706234499812126, 0.011760740540921688, 0.01505907904356718, 0.013479874469339848, 0.007049781270325184, -0.012600317597389221, 0.002543718321248889, 0.008002634160220623, 0.003303335513919592, -0.014659280888736248, 0.00932196993380785, -0.0077027855440974236, 0.019856661558151245, -0.02971835993230343, 0.01042141579091549, 0.012886839918792248, 0.02384132146835327, 0.022588618099689484, 0.008655638433992863, 0.003888041013851762, -0.022748537361621857, 0.00016585394041612744, -0.035422153770923615, -0.013126719743013382, -0.002710300963371992, 0.013533181510865688, 0.006286832503974438, 0.012886839918792248, -0.01757781021296978, -0.02468089759349823, 0.017364583909511566, 0.02154914289712906, 0.005447255447506905, -0.010128229856491089, -0.0028219115920364857, -0.0068032387644052505, -0.002025645924732089, 0.006739937234669924, 0.004847557749599218, 0.012520357966423035, -0.0072763338685035706, -0.016258474439382553, 0.011507535353302956, 0.028598923236131668, 0.03262356296181679, 0.023001743480563164, -0.02414783276617527, -0.018390733748674393, 0.002598690567538142, 0.0008878859807737172, 0.0012443730374798179, 0.003168403636664152, 0.010474721901118755, 0.022402046248316765, -0.01941688358783722, 0.02875884249806404, 0.0003606516111176461, 0.011480881832540035, 0.018857166171073914, 0.028625577688217163, -0.00849571917206049, 0.022921783849596977, -0.00012087659706594422, 0.02817247249186039, 0.008575678803026676, -0.004051291849464178, 0.03153077885508537, -0.010707938112318516, 0.0018573974957689643, 0.0009944989578798413, 0.004204547964036465, 0.032463643699884415, -0.026853136718273163, -0.013399914838373661, 0.0019190331222489476, 0.014326115138828754, 0.031157635152339935, 0.022215472534298897, 0.005020803771913052, -0.002633672906085849, 0.0012277147034183145, 0.023188317194581032, -0.004537713713943958, -0.013226669281721115, 0.003406616859138012, -0.019976601004600525, -0.013040096499025822, -0.005637159571051598, -0.0014459381345659494, 0.0180575679987669, 0.0024820826947689056, 0.024640917778015137, 0.010581335052847862, 0.025906946510076523, 0.012020610272884369, 0.01645837351679802, 0.0014925813302397728, -0.009102080948650837, -0.02242869883775711, 0.0006725777639076114, 0.012207183055579662, 0.012280479073524475, -0.011687444522976875, -0.010228179395198822, 0.004657653160393238, -0.007462906651198864, 0.006500058341771364, 0.0040013170801103115, -1.1660791642498225e-05, 0.01709805242717266, -0.023161662742495537, -5.312427310855128e-05, 0.0159786157310009, 0.008835548534989357, -0.005557199940085411, -0.010148219764232635, -0.007816062308847904, 0.011201023124158382, -0.00757618248462677, 0.008795568719506264, 0.006203541066497564, 0.020149847492575645, 0.0002575785620138049, -0.002617014804854989, -0.012953473255038261, -0.013466548174619675, -0.01030147634446621, 0.018017588183283806, 0.0038114129565656185, -0.014006276614964008, -0.0059803202748298645, 0.01556549035012722, -0.0056971292942762375, 0.0015317281940951943, 0.013319955207407475, 0.01681819185614586, 0.012826870195567608, -0.015352264977991581, -0.0068498821929097176, -0.018097547814249992, 0.010721264407038689, 0.04882873222231865, 0.04315159097313881, 0.0025637082289904356, -0.010941154323518276, 0.003691473277285695, -0.01936357654631138, -0.026959748938679695, -0.013499864377081394, 0.006393445190042257, -0.0021755704656243324, -0.007856042124330997, -0.004321156069636345, 0.016285128891468048, 0.0013526517432183027, 0.016618292778730392, 0.00711641414090991, -0.0014392748707905412, -0.0024820826947689056, -0.009448572993278503, -0.012260489165782928, 0.0029385194648057222, 0.0077027855440974236, 0.0076294890604913235, 0.0033932903315871954, 0.012073916383087635, -0.021349243819713593, 0.00772277545183897, 0.001691647688858211, 0.0002557045081630349, -0.02036307379603386, -0.014152868650853634, 0.01923030987381935, 0.010101577267050743, 0.018097547814249992, -0.0006234358879737556, 0.03286344185471535, -0.0044477591291069984, -0.01573873683810234, 0.007789408788084984, 0.004840894136577845, 0.009222020395100117, 0.004571030382066965, -0.003391624428331852, -0.008269166573882103, -0.00712974090129137, 0.00024820826365612447, -0.016604967415332794, 0.003399953478947282, -0.008662302047014236, -0.007556192576885223, 0.012080579996109009, -0.017737729474902153, -0.0012060590088367462, -0.01544555090367794, 0.028732189908623695, 0.02326827682554722, 0.004401115700602531, 0.006843218579888344, -0.0021672414150089025, -0.017564482986927032, -0.0298782791942358, -0.006749932188540697, -0.01136760599911213, -0.009301980026066303, -0.011380932293832302, -0.03041134402155876, -0.02349482849240303, -0.013333282433450222, -0.02017650008201599, 0.0037381164729595184, 0.010987796820700169, -0.017591137439012527, -0.024760857224464417, -0.016231821849942207, 0.0289987213909626, -0.013952969573438168, 0.0005209874943830073, 0.013106729835271835, 0.001535892835818231, 0.022588618099689484, -0.010861193761229515, -0.0224953331053257, 0.005863712169229984, -0.02414783276617527, -0.005843722261488438, 0.0168581735342741, -0.0039713322184979916, 0.006753263995051384, -0.002751946682110429, 0.01042141579091549, -0.006043621338903904, -0.0024654243607074022, -0.004880873952060938, -0.029371866956353188, 0.016351761296391487, -0.00872893538326025, 0.0019939953926950693, 0.007442916743457317, -0.00866896566003561, -0.004154573194682598, -0.015538837760686874, -0.001521733240224421, -0.009128733538091183, 0.001610854989849031, 0.020749544724822044, -0.019030410796403885, -0.013373262248933315, 0.016911478713154793, -0.023294929414987564, -0.00770944869145751, -0.010488049127161503, -0.018137527629733086, 0.009601828642189503, -0.02000325545668602, 0.0023937937803566456, 0.017417890951037407, -0.007442916743457317, 0.02194894105195999, -0.01030147634446621, -0.030198117718100548, 7.584720151498914e-05, -0.009808391332626343, 0.011094409972429276, 0.03563537821173668, 0.02206888049840927, 0.029211947694420815, -0.00876225158572197, -0.0030484639573842287, -0.014206175692379475, 0.007909348234534264, -0.005074109882116318, 0.0011727424571290612, -0.00779607193544507, -0.004184558056294918, -0.018843838945031166, -0.011447565630078316, -0.001529229455627501, 0.013646457344293594, -0.005264014471322298, -0.010661294683814049, -0.01505907904356718, 0.013673110865056515, 0.011394258588552475, -0.009168713353574276, -0.025720374658703804, -0.02982497215270996, -0.02165575511753559, 0.010108239948749542, -0.02060295268893242, 0.0008845542906783521, 0.021096037700772285, 0.0003396205429453403, -0.011647464707493782, -0.004904195666313171, 0.015818696469068527, -0.029371866956353188, -0.004824236035346985, 0.006163561251014471, 0.016231821849942207, 0.027119668200612068, 0.022681904956698418, -0.01163413841277361, 0.011707434430718422, 0.019869988784193993, -0.0034249410964548588, 0.009635144844651222, -0.00944190938025713, -0.013779724016785622, -0.016378413885831833, 0.0025637082289904356, 0.010008290410041809, 0.00675992714241147, 0.014486034400761127, -0.00941525585949421, 0.015485530719161034, 0.00900213047862053, -0.0016783210448920727, 0.0010244838194921613, -0.004670979920774698, -0.023508155718445778, -0.011907333508133888, 0.017417890951037407, -0.008628985844552517, 0.016551660373806953, -0.013639794662594795, -0.020389726385474205, 0.024547630921006203, 0.004644326400011778, 0.015698757022619247, 0.015045752748847008, 0.01859063282608986, -0.005986983422189951, 0.0007916844333522022, 0.0011752411955967546, 0.005747104529291391, -0.012340448796749115, 0.017471197992563248, -0.022215472534298897, -0.01505907904356718, 0.025080695748329163, 0.0010053267469629645, 0.011334288865327835, -0.00848905649036169, 0.014699260704219341, 0.011847363784909248, 0.004257854539901018, 0.010148219764232635, 0.005030798725783825, -0.010767907835543156, 0.006743269041180611, -0.0236814022064209, -0.031504128128290176, -0.0027719365898519754, -0.00801596138626337, 0.0159786157310009, 0.012300468981266022, -0.012140549719333649, 0.012200519442558289, -0.02113601751625538, -0.013393252156674862, 0.004514391999691725, -0.0007025626837275922, 0.049628328531980515, 0.007536202669143677, 0.005323984194546938, 0.01751117780804634, 0.0005526382010430098, -0.024707550182938576, 0.01723131723701954, -0.018364081159234047, 0.0014717584708705544, 0.04037965461611748, 0.025893619284033775, -0.01704474538564682, -0.013606477528810501, -0.00773610221222043, 0.024534305557608604, 0.013326618820428848, -0.017950955778360367, -0.01781768910586834, 0.019443536177277565, -0.002728625200688839, -0.005277340766042471, -0.007569519337266684, -0.04011312127113342, 0.015085732564330101, -0.0007675299420952797, -0.003831402864307165, -0.02206888049840927, -0.0016899817856028676, -0.022588618099689484, 0.00732297683134675, -0.036195095628499985, 0.01993662118911743, 0.011894007213413715, -0.0289987213909626, -0.007876032032072544, 0.0018640607595443726, -0.03286344185471535, 0.0014317785389721394, -0.0001593988563399762, 0.02846565656363964, -0.010747917927801609, -0.0015908650821074843, 0.011227676644921303, 0.008529036305844784, -0.017244644463062286, 0.007029791362583637, -0.0024487662594765425, 0.026293419301509857, -0.01399294938892126, -0.011021113954484463, -0.00037626875564455986, 0.006300158798694611, 0.00846240296959877, -0.0037414482794702053, -0.002617014804854989, -0.002217216184362769, -0.008595668710768223, 0.0039146943017840385, -0.0022521985229104757, 0.018750552088022232, -0.0013176694046705961, -0.022855151444673538, -0.011407585814595222, -0.006263510789722204, -0.013979623094201088, -0.008742261677980423, -0.0147925466299057, -0.026240112259984016, -0.014246155507862568, 0.005870375316590071, 0.001669991877861321, 0.021709062159061432, 0.00997497420758009, -0.014406074769794941, 0.02154914289712906, 0.027852632105350494, -0.023694727569818497, -0.0009420253336429596, -0.006356797181069851, -5.944921213085763e-05, -0.007616162765771151, 0.002901871223002672, 0.006876535248011351, -0.009801727719604969, 0.027452833950519562, -0.014152868650853634, -0.024174485355615616, -0.036728162318468094, -0.005860380362719297, 0.015512184239923954, -0.012793553993105888, -0.0024937435518950224, -0.0054305968806147575, 0.0006709119770675898, 0.01869724690914154, 0.02497408352792263, -0.012200519442558289, 0.019390230998396873, -0.004587688483297825, -0.0018057568231597543, 0.005710456054657698, -0.024001240730285645, -0.0072030373848974705, 0.008988804183900356, -0.0016566652338951826, 0.00902878399938345, -0.017964281141757965, -0.017950955778360367, 0.007063108030706644, 0.02273521199822426, 0.0026203463785350323, -0.015232325531542301, -0.03035803698003292, -0.0004576860519591719, -0.0049475072883069515, -0.001862394972704351, 0.00757618248462677, -0.008589006029069424, -0.021042730659246445, 0.0004306163755245507, -0.011854027397930622, 0.005647154524922371, -0.008628985844552517, 0.0052140397019684315, -0.016831519082188606, -0.011880680918693542, -0.033476464450359344, -0.020962771028280258, 0.0015800371766090393, 0.007689458783715963, 0.015458877198398113, -0.01517901849001646, -0.027199627831578255, -0.007422926370054483, -0.04048626869916916, -0.020043235272169113, -0.01721799187362194, -0.001014488865621388, 0.016938133165240288, 0.01645837351679802, -0.012160539627075195, 0.03203719109296799, 0.006903188303112984, 0.03331654518842697, -0.020522993057966232, -0.019430210813879967, 0.024534305557608604, -0.01059466227889061, 0.00726300710812211, 0.004221206530928612, -0.010461395606398582, -0.021349243819713593, 0.022468678653240204, 0.0023488164879381657, 0.00923534668982029, 0.021042730659246445, 0.004810909274965525, -0.02822577767074108, -0.008682291954755783, 0.01859063282608986, 0.0011410916922613978, 0.0033066673204302788, -0.0005917851813137531, -0.007123077753931284, 0.003501569153741002, 0.0036548252683132887, 0.000372104172129184, -0.0023554798681288958, -0.004674311727285385, 0.004730949643999338, -0.0043877894058823586, 0.0022005578503012657, -0.033822957426309586, -0.006120249629020691, -0.01000162772834301, -0.01645837351679802, 0.009895014576613903, -0.014139542356133461, -0.005670476239174604, 0.03355642408132553, 0.0039013675414025784, -0.012473715469241142, -0.011887343600392342, 0.0201231949031353, -0.013873009942471981, -0.005813737399876118, 0.0035348855890333652, -0.011414248496294022, 0.0016133537283167243, -0.0008570681675337255, -0.0008274997235275805, -0.015112385153770447, 0.002357145771384239, -0.004654321353882551, -0.006696626078337431, -0.01829744689166546, 0.0022871808614581823, 0.014486034400761127, -0.025733700022101402, -0.012666950933635235, -0.020629605278372765, 0.02017650008201599, 0.0005101596470922232, -0.005104094743728638, -0.009881687350571156, -0.010221516713500023, 0.014779220335185528, 0.02390795387327671, -0.013513191603124142, 0.0008241680916398764, 0.013566497713327408, 0.014419401064515114, -0.0036481618881225586, 0.008382443338632584, 0.2319897711277008, 0.005717119202017784, 0.01733793132007122, 0.03243698924779892, -0.006416766904294491, 0.03366303816437721, 0.0075428662821650505, -0.010101577267050743, -0.008415759541094303, 0.021389223635196686, 0.016245149075984955, 0.014565994031727314, -0.005157401319593191, 0.005254019517451525, -0.002037306781858206, -0.0019356913398951292, -0.02314833737909794, -0.021709062159061432, -0.03520892560482025, -0.016711579635739326, 0.0011485879076644778, -0.022002248093485832, 0.019137024879455566, -0.010961144231259823, 0.014605973847210407, -0.016205167397856712, -0.019989928230643272, -0.0016983109526336193, 0.021615775302052498, 0.02088281139731407, -0.013593151234090328, 0.0012893503298982978, 0.012740247882902622, -0.0039713322184979916, -0.03131755441427231, -0.005627164617180824, 0.030517956241965294, -0.003758106380701065, 0.02557378076016903, -0.00842908676713705, 0.006250184029340744, -0.024334406480193138, 0.006446751765906811, -0.014326115138828754, 0.004461085423827171, 0.026946423575282097, 0.0011027776636183262, -0.015485530719161034, 0.0011869019363075495, 0.001221884391270578, -0.020629605278372765, -0.001829078420996666, 0.019256964325904846, 0.0014559330884367228, 0.017724402248859406, -0.015338937751948833, 0.018430713564157486, -0.0014534343499690294, 0.011234339326620102, 0.0039713322184979916, -0.00842908676713705, 0.009868361055850983, -0.023867974057793617, 0.026013558730483055, -0.021295936778187752, 0.008262503892183304, -0.029211947694420815, -0.012380428612232208, -0.001425115275196731, -0.01050803903490305, 0.003055127337574959, -0.0064367568120360374, 0.01871057227253914, 0.0061135864816606045, -0.02124262973666191, -0.009608492255210876, 0.020669585093855858, 0.02409452572464943, 0.02698640339076519, 0.015658777207136154, -0.01965676248073578, -0.000673827133141458, -0.015485530719161034, -0.03265021741390228, -0.021215977147221565, -0.03363638371229172, -0.010607988573610783, -0.0035415489692240953, 0.0010269825579598546, -0.023174989968538284, -0.019216984510421753, -0.016884826123714447, -0.007063108030706644, -0.014672607183456421, 0.01184070110321045, 0.016658272594213486, -0.00860899593681097, 0.026266764849424362, 0.007049781270325184, 0.012373764999210835, -0.03544880449771881, 0.04533715546131134, 0.01857730560004711, 0.006006973329931498, 0.006929841823875904, 0.005157401319593191, -0.015045752748847008, -0.008988804183900356, -0.0008270832477137446, -0.0008987138280645013, 0.006133576389402151, -0.004727617837488651, 0.010554681532084942, -0.020989423617720604, 0.0014151203213259578, 0.01632510870695114, 0.0036548252683132887, -0.011667454615235329, 0.011047766543924809, -0.014899159781634808, -0.00724968034774065, 0.0019823345355689526, -0.013779724016785622, 0.0042078797705471516, -0.010794561356306076, -0.008682291954755783, -0.01699143834412098, -0.008415759541094303, 0.016525007784366608, -0.019909968599677086, 0.009608492255210876, -0.0006575853331014514, 0.017684422433376312, 0.00045102275907993317, -0.01529895793646574, -0.0018340758979320526, 0.006020300090312958, 0.01723131723701954, -0.02763940766453743, 0.011314298957586288, 0.01723131723701954, 0.001929028076119721, -0.00789602193981409, -0.0028718863613903522, -0.00982838124036789, -0.0147925466299057, 0.0355021134018898, -0.018217487260699272, -0.0039713322184979916, -0.01633843407034874, -0.022575292736291885, -0.009541858918964863, 0.01411288883537054, 0.016138534992933273, 0.0367015078663826, -0.012753574177622795, -0.011001124046742916, -0.03656824305653572, 0.024014566093683243, -0.007302986923605204, -0.038913726806640625, 0.005124084651470184, 0.03504900634288788, -0.02141587622463703, -0.0069365049712359905, -0.009108743630349636, -0.1745253950357437, 0.014486034400761127, 0.0027152984403073788, -0.013626467436552048, 0.023188317194581032, -0.005583852995187044, 0.03981993719935417, 0.0023454849142581224, 0.011347616091370583, 0.005723782815039158, -0.0018307442078366876, -0.01764444261789322, -0.02006988786160946, -0.006383450236171484, -0.005627164617180824, 0.0028602255042642355, -0.021455856040120125, -0.007995971478521824, 0.014152868650853634, 0.012800217606127262, 0.009881687350571156, -0.014272809028625488, 0.026120172813534737, -0.018217487260699272, 0.0029451826121658087, -0.004944175481796265, -0.010827877558767796, 0.021269284188747406, -0.001584201818332076, 0.0069365049712359905, 0.008115910924971104, 0.011407585814595222, 0.015072405338287354, 0.011907333508133888, 0.012860187329351902, 0.004557703621685505, 0.029265254735946655, -0.00046559874317608774, -0.008202534168958664, 0.011287646368145943, -0.0017641111044213176, 0.012680277228355408, -0.0012718591606244445, -0.020029908046126366, 0.004171231761574745, 0.019443536177277565, 0.036488283425569534, 9.620152559364215e-05, 0.014592647552490234, -0.01669825240969658, 0.013093402609229088, -0.027119668200612068, -0.01157416868954897, -0.005993646569550037, 0.016538333147764206, 0.008842211216688156, -0.021282609552145004, 0.010248169302940369, -0.003131755394861102, -0.005177391227334738, 0.010607988573610783, -0.01651168055832386, 0.002858559601008892, 0.0013060086639598012, -0.0039713322184979916, -0.018017588183283806, -0.031983885914087296, 0.007356293499469757, -0.00017574478988535702, 0.006196877453476191, 0.0003977162705268711, -0.020149847492575645, -0.0037747647147625685, 0.006606671027839184, 0.004297834355384111, 0.004631000105291605, -0.012726920656859875, 0.020376399159431458, -0.012507031671702862, 0.0017091388581320643, -0.0036381669342517853, 0.017324604094028473, 0.002372138202190399, 0.00934862345457077, -0.0008299984619952738, 0.005387285724282265, -0.009835044853389263, -2.6783380235428922e-05, 0.002413783920928836, -0.010474721901118755, 0.02593359909951687, -0.02971835993230343, -0.013333282433450222, -0.01752450317144394, -1.2246433470863849e-05, 0.022295432165265083, 0.0011894006747752428, 0.0005942839197814465, 0.0010269825579598546, 0.003498237347230315, -0.0034149461425840855, -0.011320962570607662, -0.03001154586672783, 0.018084222450852394, 0.04786921292543411, 0.001147755072452128, -0.00973509531468153, 0.0147925466299057, 0.04472413286566734, 0.0029784992802888155, -0.03699469193816185, 1.1153234481753316e-05, 0.006396776996552944, 0.017857668921351433, 0.024894123896956444, 0.03350311890244484, 0.005673808045685291, -0.016205167397856712, -0.0027602759655565023, 0.011127726174890995, 0.04523054510354996, 0.00016856090223882347, -0.024867471307516098, 0.021402548998594284, 0.0013751405058428645, -0.009328632615506649, -0.08976810425519943, -0.030144810676574707, 0.02533390186727047, 0.049868207424879074, -0.005463913548737764, 0.03496904671192169, 0.00042332836892455816, 0.022122187539935112, -0.0028968737460672855, 0.0006134409341029823, -0.009128733538091183, -0.009388603270053864, -0.028199125081300735, -0.010994460433721542, 0.0073363035917282104, 0.005360632203519344, 0.00956184882670641, -0.0053473059087991714, -0.017964281141757965, 0.03078448958694935, 0.00048517220420762897, -0.005257350858300924, 0.00711641414090991, -0.024827491492033005, -0.007682795636355877, -0.007482896558940411, -0.02851896360516548, 0.026773177087306976, 0.004311161115765572, -0.0029568434692919254, -0.023588115349411964, -0.016738232225179672, 0.012280479073524475, -0.015072405338287354, -0.0021122691687196493, 0.013133382424712181, -0.03632836416363716, -0.012280479073524475, 0.03416945040225983, -0.011094409972429276, 0.008322473615407944, 0.02894541621208191, -0.007289660628885031, -0.05237361043691635, 0.0014192848466336727, -0.010021617636084557, 0.004877542611211538, 0.013146709650754929, -0.007476232945919037, -0.02390795387327671, -0.02296176366508007, 0.011027776636183262, -0.023641422390937805, 0.005807074252516031, 0.010528028942644596, 0.000825833878479898, 0.015458877198398113, 0.023228297010064125, -0.011287646368145943, 0.00032421163632534444, -0.022348739206790924, -0.0031150970607995987, -0.008855538442730904, 0.01847069337964058, -0.018803859129548073, 0.01006159745156765, -0.01615186221897602, 0.003584860358387232, 0.008029287680983543, -0.011061093769967556, 0.004644326400011778, 0.0006188548286445439, -0.010454731993377209, 0.0004158321535214782, -0.017604462802410126, -0.0006542537012137473, -0.03480912744998932, -0.034062836319208145, 0.01977670192718506, -0.003868051106110215, -0.016058575361967087, -0.015285631641745567, 4.776239438797347e-05, -0.011374268680810928, 0.014899159781634808, 0.016844846308231354, 0.0038480611983686686, 0.0021156007423996925, 0.02982497215270996, -0.040273040533065796, 0.010587998665869236, 0.003958005923777819, 0.012533685192465782, -0.029211947694420815, -0.0222021471709013, 0.008662302047014236, 0.01911037042737007, -0.0048142410814762115, 0.020203154534101486, -0.002661992097273469, -0.04256521910429001, -0.020736219361424446, -0.06119583174586296, 0.020256459712982178, -0.0033299888018518686, -0.038860421627759933, -0.00705644441768527, 0.006856545340269804, -0.011700770817697048, -0.017671097069978714, -0.012293805368244648, 0.010581335052847862, -0.02272188477218151, 0.006190214306116104, 0.0024870801717042923, -0.00944190938025713, -0.010341456159949303, -0.012880177237093449, 0.03766102343797684, 0.006183551158756018, 0.012447061948478222, 0.016085227951407433, 0.007003138307482004, -0.025840314105153084, 0.005870375316590071, 0.00816255435347557, -0.01721799187362194, 0.014752566814422607, -0.014859179966151714, -0.0014051253674551845, -0.03379630669951439, -0.005170728079974651, -0.002537054941058159, -0.03747445344924927, -0.001440107706002891, 0.0017074730712920427, -0.008688955567777157, 0.012207183055579662, -0.01423282828181982, 0.03574199229478836, 0.013173362240195274, 0.016245149075984955, -0.006516716443002224, -0.045603688806295395, 0.0144993606954813, -0.02592027373611927, -0.015392244793474674, 0.03531553968787193, -0.03118428774178028, 0.011594158597290516, 0.041099291294813156, 0.006296827457845211, 0.026146825402975082, 0.008395769633352757, -0.030971061438322067, -0.010961144231259823, 0.019043738022446632, -0.004354472737759352, 0.011580831371247768, 0.005224034655839205, 0.004754271358251572, -0.010554681532084942, 0.034915741533041, -0.002883547218516469, 0.0165783129632473, 0.010821213945746422, 0.0014667609939351678, 0.021469183266162872, -0.011747414246201515, -0.0006238523637875915, 0.00778274517506361, -0.02492077648639679, -0.018364081159234047, -0.012733584269881248, 0.0015558827435597777, 0.017204664647579193, 0.031237594783306122, 0.020336419343948364, 0.006026963237673044, 0.008575678803026676, -0.0010453066788613796, 0.02556045539677143, 0.0331566259264946, 0.0059803202748298645, -0.03289009630680084, 0.014712586998939514, 0.014992445707321167, 0.009835044853389263, -0.005297331139445305, 0.009941657073795795, -0.012487041763961315, 0.0008349959389306605, -0.01989664137363434, 0.013473211787641048, 0.009895014576613903, 0.003784759668633342, -0.018750552088022232, -0.02337488904595375, 0.005827064160257578, -0.0014367761323228478, 0.03243698924779892, 0.023801341652870178, 0.013026770204305649, 0.023748034611344337, -0.001625014585442841, -0.014392748475074768, -0.010727928020060062, 0.009268662892282009, -0.019563475623726845, -0.041339170187711716, -0.0067466008476912975, 0.012420408427715302, -0.0002298841718584299, -0.010647968389093876, 0.006073606666177511, 0.012233835645020008, -0.00997497420758009, -0.000403546669986099, -0.00825584027916193, 0.011314298957586288, -0.031983885914087296, 0.026240112259984016, -0.01036144606769085, 0.013126719743013382, 0.034409329295158386, -0.0036348351277410984, 0.01959013007581234, -0.0005884534912183881, 0.020203154534101486, -0.012053926475346088, 0.0023321581538766623, 0.011054430156946182, -0.0011027776636183262, 0.018617285415530205, -0.024347731843590736, -0.018097547814249992, -0.0037780962884426117, -0.006323480512946844, -0.018137527629733086, 0.020549645647406578, -0.0008420757367275655, 0.055012281984090805, 0.008069267496466637, -0.03006485104560852, 0.011001124046742916, 0.007096424233168364, 0.021642427891492844, 0.017417890951037407, 0.011347616091370583, -0.011967303231358528, 0.00724968034774065, 0.03683477267622948, 0.00964847207069397, -0.0020456360653042793, -0.03177065774798393, -0.03989989683032036, 0.0149391395971179, -0.0055072251707315445, 0.007442916743457317, -0.005913686938583851, 0.004404447507113218, 0.012973463162779808, 0.019043738022446632, 0.008055941201746464, 0.006423430051654577, -0.023095030337572098, -0.0024087862111628056, 0.03201053664088249, -0.029131988063454628, -0.007742765359580517, -0.011860691010951996, 0.021335916593670845, 0.01071460172533989, -0.01899043098092079, -0.02841235138475895, 0.015218998305499554, 0.003225041786208749, -0.006193546112626791, -0.010428079403936863, 0.00739627331495285, -0.006166892591863871, 0.022868476808071136, 0.039206910878419876, -0.019216984510421753, 0.0018607291858643293, -0.01036144606769085, -0.014406074769794941, -0.020336419343948364, 0.012780227698385715, -0.019083717837929726]\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "from langchain.embeddings import OpenAIEmbeddings\n", 177 | "embeddings = OpenAIEmbeddings(\n", 178 | " openai_api_key=\"ANYTHING or None, if set None, plz ensure you have set azure apikey in the proxy server\",\n", 179 | " openai_api_base=\"http://127.0.0.1:8080/v1\",\n", 180 | ")\n", 181 | "text = \"This is a test document.\"\n", 182 | "query_result = embeddings.embed_query(text)\n", 183 | "print(query_result)" 184 | ], 185 | "metadata": { 186 | "collapsed": false, 187 | "ExecuteTime": { 188 | "end_time": "2023-06-16T08:36:03.640759Z", 189 | "start_time": "2023-06-16T08:36:01.950382Z" 190 | } 191 | } 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "source": [ 196 | "Above shows the usage of azure-openai-proxy with langchain, based on the examples, it's very esay to implement a qna system based on your private docuements.✈️" 197 | ], 198 | "metadata": { 199 | "collapsed": false 200 | } 201 | } 202 | ], 203 | "metadata": { 204 | "kernelspec": { 205 | "display_name": "Python 3", 206 | "language": "python", 207 | "name": "python3" 208 | }, 209 | "language_info": { 210 | "codemirror_mode": { 211 | "name": "ipython", 212 | "version": 2 213 | }, 214 | "file_extension": ".py", 215 | "mimetype": "text/x-python", 216 | "name": "python", 217 | "nbconvert_exporter": "python", 218 | "pygments_lexer": "ipython2", 219 | "version": "2.7.6" 220 | } 221 | }, 222 | "nbformat": 4, 223 | "nbformat_minor": 0 224 | } 225 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/stulzq/azure-openai-proxy 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/bytedance/sonic v1.10.2 7 | github.com/gin-gonic/gin v1.9.1 8 | github.com/pkg/errors v0.9.1 9 | github.com/spf13/pflag v1.0.5 10 | github.com/spf13/viper v1.18.2 11 | github.com/stretchr/testify v1.8.4 12 | golang.org/x/net v0.19.0 13 | ) 14 | 15 | require ( 16 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect 17 | github.com/chenzhuoyu/iasm v0.9.1 // indirect 18 | github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect 19 | github.com/fsnotify/fsnotify v1.7.0 // indirect 20 | github.com/gabriel-vasile/mimetype v1.4.3 // indirect 21 | github.com/gin-contrib/sse v0.1.0 // indirect 22 | github.com/go-playground/locales v0.14.1 // indirect 23 | github.com/go-playground/universal-translator v0.18.1 // indirect 24 | github.com/go-playground/validator/v10 v10.16.0 // indirect 25 | github.com/goccy/go-json v0.10.2 // indirect 26 | github.com/hashicorp/hcl v1.0.0 // indirect 27 | github.com/json-iterator/go v1.1.12 // indirect 28 | github.com/klauspost/cpuid/v2 v2.2.6 // indirect 29 | github.com/leodido/go-urn v1.2.4 // indirect 30 | github.com/magiconair/properties v1.8.7 // indirect 31 | github.com/mattn/go-isatty v0.0.20 // indirect 32 | github.com/mitchellh/mapstructure v1.5.0 // indirect 33 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 34 | github.com/modern-go/reflect2 v1.0.2 // indirect 35 | github.com/pelletier/go-toml/v2 v2.1.1 // indirect 36 | github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect 37 | github.com/sagikazarmark/locafero v0.4.0 // indirect 38 | github.com/sagikazarmark/slog-shim v0.1.0 // indirect 39 | github.com/sourcegraph/conc v0.3.0 // indirect 40 | github.com/spf13/afero v1.11.0 // indirect 41 | github.com/spf13/cast v1.6.0 // indirect 42 | github.com/subosito/gotenv v1.6.0 // indirect 43 | github.com/twitchyliquid64/golang-asm v0.15.1 // indirect 44 | github.com/ugorji/go/codec v1.2.12 // indirect 45 | go.uber.org/multierr v1.11.0 // indirect 46 | golang.org/x/arch v0.6.0 // indirect 47 | golang.org/x/crypto v0.17.0 // indirect 48 | golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect 49 | golang.org/x/sys v0.15.0 // indirect 50 | golang.org/x/text v0.14.0 // indirect 51 | google.golang.org/protobuf v1.31.0 // indirect 52 | gopkg.in/ini.v1 v1.67.0 // indirect 53 | gopkg.in/yaml.v3 v3.0.1 // indirect 54 | ) 55 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= 2 | github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= 3 | github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= 4 | github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= 5 | github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= 6 | github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= 7 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= 8 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= 9 | github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= 10 | github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= 11 | github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= 12 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 13 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 14 | github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= 15 | github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 16 | github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= 17 | github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= 18 | github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= 19 | github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= 20 | github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= 21 | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= 22 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 23 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 24 | github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= 25 | github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= 26 | github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= 27 | github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 28 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= 29 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= 30 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= 31 | github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= 32 | github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= 33 | github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= 34 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= 35 | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= 36 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 37 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 38 | github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= 39 | github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 40 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 41 | github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= 42 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 43 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 44 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 45 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= 46 | github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= 47 | github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= 48 | github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= 49 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 50 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 51 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 52 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 53 | github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= 54 | github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= 55 | github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= 56 | github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= 57 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 58 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 59 | github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= 60 | github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 61 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 62 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 63 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 64 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 65 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 66 | github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= 67 | github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= 68 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 69 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 70 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 71 | github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= 72 | github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 73 | github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= 74 | github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= 75 | github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= 76 | github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= 77 | github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= 78 | github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= 79 | github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= 80 | github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= 81 | github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= 82 | github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= 83 | github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= 84 | github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= 85 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 86 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 87 | github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= 88 | github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= 89 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 90 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 91 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 92 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 93 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 94 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 95 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 96 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 97 | github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 98 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 99 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 100 | github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= 101 | github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= 102 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= 103 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= 104 | github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= 105 | github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= 106 | go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= 107 | go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= 108 | golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= 109 | golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= 110 | golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= 111 | golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= 112 | golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= 113 | golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4= 114 | golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= 115 | golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= 116 | golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= 117 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 118 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 119 | golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= 120 | golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 121 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= 122 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 123 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 124 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 125 | google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= 126 | google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= 127 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 128 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 129 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 130 | gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= 131 | gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= 132 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 133 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 134 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 135 | nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= 136 | rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= 137 | -------------------------------------------------------------------------------- /util/http_proxy.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "context" 5 | "encoding/base64" 6 | "fmt" 7 | "github.com/stulzq/azure-openai-proxy/constant" 8 | "net" 9 | "net/http" 10 | "net/url" 11 | "os" 12 | 13 | "golang.org/x/net/proxy" 14 | ) 15 | 16 | func NewProxyFromEnv() (*http.Transport, error) { 17 | socksProxy := os.Getenv(constant.ENV_AZURE_OPENAI_SOCKS_PROXY) 18 | if socksProxy != "" { 19 | return NewSocksProxy(socksProxy) 20 | } 21 | 22 | httpProxy := os.Getenv(constant.ENV_AZURE_OPENAI_HTTP_PROXY) 23 | if httpProxy != "" { 24 | return NewHttpProxy(httpProxy) 25 | } 26 | 27 | return nil, nil 28 | } 29 | 30 | func NewHttpProxy(proxyAddress string) (*http.Transport, error) { 31 | proxyURL, err := url.Parse(proxyAddress) 32 | if err != nil { 33 | return nil, fmt.Errorf("error parsing proxy URL: %v", err) 34 | } 35 | 36 | transport := &http.Transport{ 37 | Proxy: http.ProxyURL(proxyURL), 38 | } 39 | 40 | if proxyURL.User != nil { 41 | proxyAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(proxyURL.User.String())) 42 | 43 | transport.ProxyConnectHeader = http.Header{ 44 | "Proxy-Authorization": []string{proxyAuth}, 45 | } 46 | } 47 | 48 | return transport, nil 49 | } 50 | 51 | func NewSocksProxy(proxyAddress string) (*http.Transport, error) { 52 | // proxyAddress: socks5://user:password@127.0.0.1:1080 53 | proxyURL, err := url.Parse(proxyAddress) 54 | if err != nil { 55 | return nil, fmt.Errorf("error parsing proxy URL: %v", err) 56 | } 57 | 58 | dialer, err := proxy.FromURL(proxyURL, proxy.Direct) 59 | if err != nil { 60 | return nil, fmt.Errorf("error creating proxy dialer: %v", err) 61 | } 62 | 63 | transport := &http.Transport{ 64 | DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { 65 | return dialer.Dial(network, address) 66 | }, 67 | } 68 | 69 | return transport, nil 70 | } 71 | -------------------------------------------------------------------------------- /util/http_proxy_test.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "net/http" 6 | "testing" 7 | ) 8 | 9 | func TestHttpProxy(t *testing.T) { 10 | proxyAddress := "http://127.0.0.1:1087" 11 | transport, err := NewHttpProxy(proxyAddress) 12 | 13 | assert.NoError(t, err) 14 | assert.NotNil(t, transport) 15 | 16 | client := &http.Client{ 17 | Transport: transport, 18 | } 19 | 20 | resp, err := client.Get("https://www.google.com") 21 | assert.NoError(t, err) 22 | assert.NotNil(t, resp) 23 | assert.Equal(t, 200, resp.StatusCode) 24 | } 25 | 26 | func TestSocksProxy(t *testing.T) { 27 | proxyAddress := "socks5://127.0.0.1:1080" 28 | transport, err := NewSocksProxy(proxyAddress) 29 | 30 | assert.NoError(t, err) 31 | assert.NotNil(t, transport) 32 | 33 | client := &http.Client{ 34 | Transport: transport, 35 | } 36 | 37 | resp, err := client.Get("https://www.google.com") 38 | assert.NoError(t, err) 39 | assert.NotNil(t, resp) 40 | assert.Equal(t, 200, resp.StatusCode) 41 | } 42 | -------------------------------------------------------------------------------- /util/path.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "log" 5 | "os" 6 | "path" 7 | "path/filepath" 8 | "runtime" 9 | "strings" 10 | ) 11 | 12 | func GetCurrentAbsPath() (string, bool) { 13 | // Work in dev and prod runtime 14 | isDebug := false 15 | absDir := getCurrentAbsPathByExecutable() 16 | tmpDir, _ := filepath.EvalSymlinks(os.TempDir()) 17 | // If it is a temporary directory or the directory contains 'GoLand' keyword, 18 | // then retrieve it using the `caller` method. 19 | if strings.Contains(absDir, tmpDir) || strings.Contains(absDir, "GoLand") { 20 | isDebug = true 21 | return getCurrentAbsPathByCaller(), isDebug 22 | } 23 | return absDir, isDebug 24 | } 25 | 26 | func GetWorkdir() string { 27 | workDir, isDebug := GetCurrentAbsPath() 28 | if isDebug { 29 | workDir = filepath.Join(workDir, "..") 30 | } 31 | return workDir 32 | } 33 | 34 | // getCurrentAbsPathByExecutable Retrieve the absolute path to the currently executing file. 35 | func getCurrentAbsPathByExecutable() string { 36 | exePath, err := os.Executable() 37 | log.Printf("executable path: %s", exePath) 38 | if err != nil { 39 | panic(err) 40 | } 41 | res, _ := filepath.EvalSymlinks(exePath) 42 | return path.Dir(res) 43 | } 44 | 45 | // getCurrentAbsPathByCaller Retrieve the absolute path to the currently executing file.(by `go run`) 46 | func getCurrentAbsPathByCaller() string { 47 | var abPath string 48 | _, filename, _, ok := runtime.Caller(0) 49 | if ok { 50 | abPath = path.Dir(filename) 51 | } 52 | return abPath 53 | } 54 | -------------------------------------------------------------------------------- /util/response_err.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | ) 6 | 7 | func SendError(c *gin.Context, err error) { 8 | c.JSON(500, ApiResponse{ 9 | Error: ErrorDescription{ 10 | Code: "500", 11 | Message: err.Error(), 12 | }, 13 | }) 14 | } 15 | -------------------------------------------------------------------------------- /util/types.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | type ApiResponse struct { 4 | Error ErrorDescription `json:"error"` 5 | } 6 | 7 | type ErrorDescription struct { 8 | Code string `json:"code"` 9 | Message string `json:"message"` 10 | } 11 | --------------------------------------------------------------------------------