├── .github
├── dockerhub.png
└── workflows
│ ├── ci.yml
│ └── release.yml
├── .gitignore
├── .goreleaser.yaml
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── build.sh
├── define
└── env.go
├── docker-compose.example.yml
├── example
└── simple-langchain-examples.ipynb
├── go.mod
├── go.sum
├── main.go
├── util
├── http_proxy.go
├── http_proxy_test.go
├── path.go
├── response_err.go
└── types.go
└── yi
├── cache.go
├── define.go
├── fn.go
└── proxy.go
/.github/dockerhub.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/soulteary/yi-openai-proxy/11abf30d7f5fd59b85b14b224273164e344fbe83/.github/dockerhub.png
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | push:
5 | branches: [ "main" ]
6 | paths-ignore:
7 | - 'example/**'
8 | - '**/*.gitignore'
9 | - '**/*.md'
10 | pull_request:
11 | branches: [ "main" ]
12 | paths-ignore:
13 | - 'example/**'
14 | - '**/*.gitignore'
15 | - '**/*.md'
16 |
17 | jobs:
18 | build:
19 | runs-on: ubuntu-latest
20 | steps:
21 | - name: Checkout
22 | uses: actions/checkout@v3
23 | with:
24 | fetch-depth: '0'
25 | - name: Set up Go
26 | uses: actions/setup-go@v3
27 | with:
28 | cache: false
29 | go-version-file: go.mod
30 |
31 | - name: Verify gofmt
32 | run: |
33 | make fmt && git add define util yi main.go &&
34 | git diff --cached --exit-code || (echo 'Please run "make fmt" to verify gofmt' && exit 1);
35 | - name: Verify govet
36 | run: |
37 | make vet && git add define util yi main.go &&
38 | git diff --cached --exit-code || (echo 'Please run "make vet" to verify govet' && exit 1);
39 |
40 | - name: Build
41 | 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 | permissions:
15 | contents: read
16 | packages: write
17 |
18 | jobs:
19 | build-image:
20 | runs-on: ubuntu-latest
21 | steps:
22 | - name: Set up QEMU
23 | uses: docker/setup-qemu-action@v2
24 | - name: Set up Docker Buildx
25 | uses: docker/setup-buildx-action@v2
26 |
27 | - name: Login to Docker Hub
28 | uses: docker/login-action@v2
29 | with:
30 | username: ${{ secrets.DOCKERHUB_USERNAME }}
31 | password: ${{ secrets.DOCKERHUB_TOKEN }}
32 |
33 | - name: Login to the GPR
34 | uses: docker/login-action@v2
35 | with:
36 | registry: ghcr.io
37 | username: ${{ github.repository_owner }}
38 | password: ${{ secrets.GITHUB_TOKEN }}
39 |
40 | - name: Parse Tag Name
41 | run: |
42 | if [ x${{ github.event.inputs.tag }} == x"" ]; then
43 | echo "TAG_NAME=${{ github.ref_name }}" >> $GITHUB_ENV
44 | else
45 | echo "TAG_NAME=${{ github.event.inputs.tag }}" >> $GITHUB_ENV
46 | fi
47 |
48 | - name: Build and push
49 | uses: docker/build-push-action@v4
50 | env:
51 | BUILDX_NO_DEFAULT_ATTESTATIONS: 1 # https://github.com/orgs/community/discussions/45969
52 | with:
53 | platforms: linux/amd64,linux/arm64
54 | push: true
55 | pull: true
56 | labels: |
57 | org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
58 | org.opencontainers.image.licenses=Apache-2.0
59 | tags: |
60 | ${{ github.repository }}:${{ env.TAG_NAME }}
61 | ghcr.io/${{ github.repository }}:${{ env.TAG_NAME }}
62 | cache-from: type=gha # https://docs.docker.com/build/cache/backends/gha/
63 | cache-to: type=gha,mode=max
64 |
65 | goreleaser:
66 | runs-on: ubuntu-latest
67 | if: ${{ github.event.inputs.tag == '' }}
68 | steps:
69 | - name: Checkout
70 | uses: actions/checkout@v3
71 | with:
72 | fetch-depth: 0
73 | - name: Fetch all tags
74 | run: git fetch --force --tags
75 | - name: Set up Go
76 | uses: actions/setup-go@v4
77 | with:
78 | cache: false
79 | go-version-file: go.mod
80 | - name: Run GoReleaser
81 | uses: goreleaser/goreleaser-action@v4
82 | with:
83 | distribution: goreleaser
84 | version: latest
85 | args: release --clean
86 | env:
87 | 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 | .DS_Store
17 | .idea/
18 | bin/
19 | release/
20 | docker-compose.yml
21 | dist/
--------------------------------------------------------------------------------
/.goreleaser.yaml:
--------------------------------------------------------------------------------
1 | # Make sure to check the documentation at https://goreleaser.com
2 | env:
3 | - GIT_URL=https://github.com/soulteary/yi-openai-proxy
4 | before:
5 | hooks:
6 | - go mod tidy
7 | builds:
8 | - id: yi-openai-proxy
9 | env:
10 | - CGO_ENABLED=0
11 | goos:
12 | - linux
13 | - windows
14 | - darwin
15 | goarch:
16 | - amd64
17 | main: ./main.go
18 | binary: yi-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/soulteary/yi-openai-proxy/compare/{{ .PreviousTag }}...{{ .Tag }}
89 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM golang:1.21.0-alpine AS builder
2 | RUN apk update && apk upgrade \
3 | && apk add --no-cache ca-certificates tzdata \
4 | && update-ca-certificates 2>/dev/null || true
5 | RUN apk add --no-cache make git gcc g++ libc-dev
6 | ENV GO111MODULE=on
7 | ENV CGO_ENABLED=1
8 | ENV GOOS=linux
9 | WORKDIR /build
10 | ADD go.mod go.sum ./
11 | RUN go mod download
12 | COPY . .
13 | RUN make build
14 |
15 | FROM alpine:3.18.0
16 | RUN apk update && apk upgrade \
17 | && apk add --no-cache ca-certificates tzdata \
18 | && update-ca-certificates 2>/dev/null || true
19 | WORKDIR /app
20 | EXPOSE 8080
21 | COPY --from=builder /build/bin .
22 | ENTRYPOINT ["/app/yi-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 := "yi-openai-proxy"
3 |
4 | build:
5 | @env CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o bin/$(BIN_NAME) .
6 |
7 | fmt:
8 | go fmt ./...
9 |
10 | vet:
11 | go vet ./...
12 |
13 | .PHONY: build fmt vet
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # YI OpenAI Proxy
2 |
3 | **推荐使用新项目,包含更多的模型 [https://github.com/soulteary/amazing-openai-api](https://github.com/soulteary/amazing-openai-api)**
4 |
5 | Yi OpenAI Proxy,将零一万物 YI-34B 模型 API 转换为各种使用 OpenAI API 的开源软件支持的格式,无需修改开源软件配置或代码。
6 |
7 | 10 来 MB 的小工具,让你几乎无成本的使用各种 AI 应用。
8 |
9 | *只需要配置两个环境变量,一键启动,开始畅玩 AI Native 应用🚀。*
10 |
11 |
12 |
13 | [请前往新项目](https://github.com/soulteary/amazing-openai-api),同时支持多种模型 🎉
14 |
15 | ## 当前验证可玩开源项目(持更,欢迎 PR)
16 |
17 | | 开源项目 | 适配状态 |
18 | | -------------------------------------------------------- | ------ |
19 | | [chatgpt-web](https://github.com/Chanzhaoyu/chatgpt-web) | ✅ |
20 | | [chatbox](https://github.com/Bin-Huang/chatbox) | ✅ |
21 | | [langchain](https://python.langchain.com/en/latest/) | ✅ |
22 | | [ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web) | ✅ |
23 |
24 | ## 软件下载
25 |
26 | 你可以在 GitHub Release 页面下载构建好的二进制可执行文件,在你的机器直接运行。
27 |
28 | 也可以使用 Docker Pull,下载 GitHub CI 自动构建好的小巧的容器镜像。
29 |
30 | ```bash
31 | docker pull soulteary/yi-openai-proxy:v1.0.0
32 | ```
33 |
34 | ## 快速上手
35 |
36 | 如果你直接下载和使用二进制可执行,那么使用命令启动即可:
37 |
38 | ```bash
39 | YI_ENDPOINT=https://你申请到的API地址 YI_API_KEY=你申请到的API_KEY ./yi-openai-proxy
40 |
41 | ```
42 |
43 | 如果你使用容器,可以使用下面的命令一键启动:
44 |
45 | ```bash
46 | docker run --rm -e YI_ENDPOINT=https://你申请到的API地址 YI_API_KEY=你申请到的API_KEY soulteary/yi-openai-proxy:v1.0.0
47 | ```
48 |
49 | 当然,如果你喜欢声明式的配置,使用下面的 `docker-compose.yml` 配置会更直观:
50 |
51 | ```yaml
52 | version: "3"
53 |
54 | services:
55 | yi-openai-proxy:
56 | image: soulteary/yi-openai-proxy:v1.0.0
57 | restart: always
58 | ports:
59 | - "8080:8080"
60 | environment:
61 | # 使用以 `https://` 开头的 YI-34B API 地址
62 | - YI_ENDPOINT=https://
63 | # 如果你定义了这个参数,那么你的程序在调用 “API” 的时候,就不需要携带 KEY 的内容了,或者随意写都行,这样就不会把 KEY 泄漏给应用了。更方便,也更安全。
64 | - YI_API_KEY=
65 | ```
66 |
67 | ## 调用验证
68 |
69 | 我们可以使用 `curl` 来方便的模拟 SDK 或者应用访问,比如执行:
70 |
71 | ```bash
72 | curl --location --request POST 'localhost:8080/v1/chat/completions' \
73 | -H 'Authorization: Bearer <你的 API Key>' \
74 | -H 'Content-Type: application/json' \
75 | -d '{
76 | "max_tokens": 1000,
77 | "model": "gpt-3.5-turbo",
78 | "temperature": 0.8,
79 | "top_p": 1,
80 | "presence_penalty": 1,
81 | "messages": [
82 | {
83 | "role": "user",
84 | "content": "Hello"
85 | }
86 | ],
87 | "stream": true
88 | }'
89 | ```
90 |
91 | 虽然上面我们写的模型是 `gpt-3.5-turbo`,甚至我们没有正确填写 API Key,如果你在启动服务的时候,已经配置好了 `YI_API_KEY` 环境变量,因为通过了这个服务的处理,我们想用的开源软件就能够实现不修改代码或者逻辑,直接复用 YI。
92 |
93 | ### 使用 ChatGPT-Next-Web
94 |
95 | 
96 |
97 | docker-compose.yml
98 |
99 | ````yaml
100 | version: '3'
101 |
102 | services:
103 | chatgpt-web:
104 | image: yidadaa/chatgpt-next-web
105 | ports:
106 | - 3000:3000
107 | environment:
108 | API_KEY:
109 | BASE_URL: http://yi-openai-proxy:8080
110 | CODE: ""
111 | HIDE_USER_API_KEY: 1
112 | HIDE_BALANCE_QUERY: 1
113 | depends_on:
114 | - yi-openai-proxy
115 | links:
116 | - yi-openai-proxy
117 | networks:
118 | - chatgpt-ns
119 |
120 | yi-openai-proxy:
121 | image: soulteary/yi-openai-proxy:v1.0.0
122 | restart: always
123 | ports:
124 | - "8080:8080"
125 | environment:
126 | # 使用以 `https://` 开头的 YI-34B API 地址
127 | - YI_ENDPOINT=https://
128 | # 如果你定义了这个参数,那么你的程序在调用 “API” 的时候,就不需要携带 KEY 的内容了,或者随意写都行,这样就不会把 KEY 泄漏给应用了。更方便,也更安全。
129 | - YI_API_KEY=
130 | networks:
131 | - chatgpt-ns
132 |
133 | networks:
134 | chatgpt-ns:
135 | driver: bridge
136 | ````
137 |
138 | ### 使用 ChatGPT-Web
139 |
140 | ChatGPT Web: https://github.com/Chanzhaoyu/chatgpt-web
141 |
142 | docker-compose.yml:
143 |
144 | ````yaml
145 | version: '3'
146 |
147 | services:
148 | chatgpt-web:
149 | image: chenzhaoyu94/chatgpt-web
150 | ports:
151 | - 3002:3002
152 | environment:
153 | API_KEY:
154 | API_BASE_URL: http://yi-openai-proxy:8080
155 | # API_MODEL: gpt-4
156 | AUTH_SECRET_KEY: ""
157 | MAX_REQUEST_PER_HOUR: 1000
158 | TIMEOUT_MS: 60000
159 | depends_on:
160 | - yi-openai-proxy
161 | links:
162 | - yi-openai-proxy
163 | networks:
164 | - chatgpt-ns
165 |
166 | yi-openai-proxy:
167 | image: soulteary/yi-openai-proxy:v1.0.0
168 | restart: always
169 | ports:
170 | - "8080:8080"
171 | environment:
172 | # 使用以 `https://` 开头的 YI-34B API 地址
173 | - YI_ENDPOINT=https://
174 | # 如果你定义了这个参数,那么你的程序在调用 “API” 的时候,就不需要携带 KEY 的内容了,或者随意写都行,这样就不会把 KEY 泄漏给应用了。更方便,也更安全。
175 | - YI_API_KEY=
176 | networks:
177 | - chatgpt-ns
178 |
179 | networks:
180 | chatgpt-ns:
181 | driver: bridge
182 | ````
183 |
184 | ## 感谢
185 |
186 | 项目借鉴和修改自下面两个项目,感谢 `stulzq` 的创意 ❤️:
187 |
188 | - [stulzq/azure-openai-proxy](https://github.com/stulzq/azure-openai-proxy)。
189 | - [soulteary/azure-openai-proxy](https://github.com/soulteary/azure-openai-proxy)。
190 |
--------------------------------------------------------------------------------
/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/yi-openai-proxy .
12 |
13 | docker build -t soulteary/yi-openai-proxy:$VERSION .
--------------------------------------------------------------------------------
/define/env.go:
--------------------------------------------------------------------------------
1 | package define
2 |
3 | const (
4 | ENV_YI_ENDPOINT = "YI_ENDPOINT"
5 | ENV_YI_API_VER = "YI_API_VER"
6 | ENV_YI_API_KEY = "YI_API_KEY"
7 | ENV_YI_MODEL_NAME = "yi-34b-chat"
8 |
9 | ENV_YI_HTTP_PROXY = "YI_HTTP_PROXY"
10 | ENV_YI_SOCKS_PROXY = "YI_SOCKS_PROXY"
11 | )
12 |
13 | const (
14 | REST_API_VERSION = "/v1"
15 | )
16 |
--------------------------------------------------------------------------------
/docker-compose.example.yml:
--------------------------------------------------------------------------------
1 | version: "3.4"
2 |
3 | services:
4 | yi-openai-proxy:
5 | image: soulteary/yi-openai-proxy:v1.0.0
6 | restart: always
7 | ports:
8 | - "8080:8080"
9 | environment:
10 | # 推荐使用“无痛”的变量来配置你的程序
11 | - TZ=Asia/Shanghai
12 | # 使用 Yi 社区提供的 API URL
13 | - YI_ENDPOINT=https://YI Endpoint>/
14 | # 使用 Yi 社区提供的 API Key
15 | - YI_API_KEY=
16 | # 使用 Yi 社区提供的 Model Name。可选,默认为 "yi-34b-chat"
17 | - YI_MODEL_NAME=
18 |
--------------------------------------------------------------------------------
/example/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/soulteary/yi-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 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "log"
7 | "net/http"
8 | "os"
9 | "os/signal"
10 | "syscall"
11 |
12 | "github.com/gin-gonic/gin"
13 | "github.com/pkg/errors"
14 | "github.com/soulteary/yi-openai-proxy/define"
15 | "github.com/soulteary/yi-openai-proxy/yi"
16 | "github.com/spf13/pflag"
17 | "github.com/spf13/viper"
18 | )
19 |
20 | var (
21 | version = ""
22 | buildDate = ""
23 | gitCommit = ""
24 | )
25 |
26 | func main() {
27 | viper.AutomaticEnv()
28 | parseFlag()
29 |
30 | err := yi.GetInstance()
31 | if err != nil {
32 | panic(err)
33 | }
34 |
35 | gin.SetMode(gin.ReleaseMode)
36 | r := gin.Default()
37 | registerRoute(r)
38 |
39 | srv := &http.Server{
40 | Addr: viper.GetString("listen"),
41 | Handler: r,
42 | }
43 |
44 | runServer(srv)
45 | }
46 |
47 | // registerRoute registers all routes
48 | func registerRoute(r *gin.Engine) {
49 | // https://platform.openai.com/docs/api-reference
50 | r.HEAD("/", func(c *gin.Context) {
51 | c.Status(200)
52 | })
53 |
54 | r.Any("/health", func(c *gin.Context) {
55 | c.Status(200)
56 | })
57 |
58 | stripPrefixConverter := yi.NewStripPrefixConverter(define.REST_API_VERSION)
59 | apiRouter := r.Group(define.REST_API_VERSION)
60 | {
61 | apiRouter.Any("/completions", yi.ProxyWithConverter(stripPrefixConverter))
62 | apiRouter.Any("/chat/completions", yi.ProxyWithConverter(stripPrefixConverter))
63 | }
64 | }
65 |
66 | func runServer(srv *http.Server) {
67 | go func() {
68 | log.Printf("Server listening at %s\n", srv.Addr)
69 | if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
70 | panic(errors.Errorf("listen: %s\n", err))
71 | }
72 | }()
73 |
74 | quit := make(chan os.Signal, 1)
75 | signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
76 | <-quit
77 |
78 | log.Println("Server Shutdown...")
79 | if err := srv.Shutdown(context.Background()); err != nil {
80 | log.Fatal("Server Shutdown:", err)
81 | }
82 | log.Println("Server exiting")
83 | }
84 |
85 | func parseFlag() {
86 | pflag.StringP("listen", "l", ":8080", "listen address")
87 | pflag.BoolP("version", "v", false, "version information")
88 | pflag.Parse()
89 | if err := viper.BindPFlags(pflag.CommandLine); err != nil {
90 | panic(err)
91 | }
92 | if viper.GetBool("v") {
93 | fmt.Println("version:", version)
94 | fmt.Println("buildDate:", buildDate)
95 | fmt.Println("gitCommit:", gitCommit)
96 | os.Exit(0)
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/util/http_proxy.go:
--------------------------------------------------------------------------------
1 | package util
2 |
3 | import (
4 | "context"
5 | "encoding/base64"
6 | "fmt"
7 | "net"
8 | "net/http"
9 | "net/url"
10 | "os"
11 |
12 | "github.com/soulteary/yi-openai-proxy/define"
13 | "golang.org/x/net/proxy"
14 | )
15 |
16 | func NewProxyFromEnv() (*http.Transport, error) {
17 | socksProxy := os.Getenv(define.ENV_YI_SOCKS_PROXY)
18 | if socksProxy != "" {
19 | return NewSocksProxy(socksProxy)
20 | }
21 |
22 | httpProxy := os.Getenv(define.ENV_YI_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 | "net/http"
5 | "testing"
6 |
7 | "github.com/stretchr/testify/assert"
8 | )
9 |
10 | func TestHttpProxy(t *testing.T) {
11 | proxyAddress := "http://127.0.0.1:1087"
12 | transport, err := NewHttpProxy(proxyAddress)
13 |
14 | assert.NoError(t, err)
15 | assert.NotNil(t, transport)
16 |
17 | client := &http.Client{
18 | Transport: transport,
19 | }
20 |
21 | resp, err := client.Get("https://www.google.com")
22 | assert.NoError(t, err)
23 | assert.NotNil(t, resp)
24 | assert.Equal(t, 200, resp.StatusCode)
25 | }
26 |
27 | func TestSocksProxy(t *testing.T) {
28 | proxyAddress := "socks5://127.0.0.1:1080"
29 | transport, err := NewSocksProxy(proxyAddress)
30 |
31 | assert.NoError(t, err)
32 | assert.NotNil(t, transport)
33 |
34 | client := &http.Client{
35 | Transport: transport,
36 | }
37 |
38 | resp, err := client.Get("https://www.google.com")
39 | assert.NoError(t, err)
40 | assert.NotNil(t, resp)
41 | assert.Equal(t, 200, resp.StatusCode)
42 | }
43 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/yi/cache.go:
--------------------------------------------------------------------------------
1 | package yi
2 |
3 | var (
4 | ModelDeploymentConfig = map[string]DeploymentConfig{}
5 | )
6 |
--------------------------------------------------------------------------------
/yi/define.go:
--------------------------------------------------------------------------------
1 | package yi
2 |
3 | import (
4 | "net/http"
5 | "net/url"
6 | )
7 |
8 | type DeploymentConfig struct {
9 | ModelName string `yaml:"model_name" json:"model_name" mapstructure:"model_name"` // corresponding model name in openai
10 | Endpoint string `yaml:"endpoint" json:"endpoint" mapstructure:"endpoint"` // deployment endpoint
11 | ApiKey string `yaml:"api_key" json:"api_key" mapstructure:"api_key"` // secrect key1 or 2
12 | ApiVersion string `yaml:"api_version" json:"api_version" mapstructure:"api_version"` // deployment version, not required
13 | EndpointUrl *url.URL // url.URL form deployment endpoint
14 | }
15 |
16 | type RequestConverter interface {
17 | Name() string
18 | Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error)
19 | }
20 |
21 | type StripPrefixConverter struct {
22 | Prefix string
23 | }
24 |
--------------------------------------------------------------------------------
/yi/fn.go:
--------------------------------------------------------------------------------
1 | package yi
2 |
3 | import (
4 | "errors"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "strings"
9 |
10 | "github.com/soulteary/yi-openai-proxy/define"
11 | "github.com/spf13/viper"
12 | )
13 |
14 | func (c *StripPrefixConverter) Name() string {
15 | return "StripPrefix"
16 | }
17 |
18 | func (c *StripPrefixConverter) Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error) {
19 | req.Host = config.EndpointUrl.Host
20 | req.URL.Scheme = config.EndpointUrl.Scheme
21 | req.URL.Host = config.EndpointUrl.Host
22 | req.URL.RawPath = req.URL.EscapedPath()
23 |
24 | query := req.URL.Query()
25 | query.Add("api-version", config.ApiVersion)
26 | req.URL.RawQuery = query.Encode()
27 | return req, nil
28 | }
29 |
30 | func NewStripPrefixConverter(prefix string) *StripPrefixConverter {
31 | return &StripPrefixConverter{
32 | Prefix: prefix,
33 | }
34 | }
35 |
36 | func GetOptionFromEnv(key string) string {
37 | return strings.TrimSpace(viper.GetString(key))
38 | }
39 |
40 | func GetInstance() (err error) {
41 | var config DeploymentConfig
42 | apiVersion := GetOptionFromEnv(define.ENV_YI_API_VER)
43 | endpoint := GetOptionFromEnv(define.ENV_YI_ENDPOINT)
44 | apikey := GetOptionFromEnv(define.ENV_YI_API_KEY)
45 | modelName := GetOptionFromEnv(define.ENV_YI_MODEL_NAME)
46 |
47 | if endpoint == "" {
48 | return errors.New("endpoint should not be empty")
49 | }
50 |
51 | if modelName == "" {
52 | modelName = "yi-34b-chat"
53 | }
54 |
55 | if apiVersion == "" {
56 | apiVersion = "2023-07-01-preview"
57 | }
58 |
59 | config.ApiKey = apikey
60 | config.Endpoint = endpoint
61 | config.ApiVersion = apiVersion
62 | config.ModelName = modelName
63 |
64 | u, err := url.Parse(endpoint)
65 | if err != nil {
66 | return fmt.Errorf("parse endpoint error: %w", err)
67 | }
68 | config.EndpointUrl = u
69 |
70 | ModelDeploymentConfig[modelName] = config
71 |
72 | return nil
73 | }
74 |
--------------------------------------------------------------------------------
/yi/proxy.go:
--------------------------------------------------------------------------------
1 | package yi
2 |
3 | import (
4 | "bytes"
5 | "fmt"
6 | "io"
7 | "log"
8 | "net/http"
9 | "net/http/httputil"
10 | "regexp"
11 | "strings"
12 |
13 | "github.com/bytedance/sonic"
14 | "github.com/gin-gonic/gin"
15 | "github.com/pkg/errors"
16 | "github.com/soulteary/yi-openai-proxy/util"
17 | )
18 |
19 | func ProxyWithConverter(requestConverter RequestConverter) gin.HandlerFunc {
20 | return func(c *gin.Context) {
21 | Proxy(c, requestConverter)
22 | }
23 | }
24 |
25 | var maskURL = regexp.MustCompile(`https?:\/\/.+\/v1\/`)
26 |
27 | // Proxy Azure OpenAI
28 | func Proxy(c *gin.Context, requestConverter RequestConverter) {
29 | if c.Request.Method == http.MethodOptions {
30 | c.Header("Access-Control-Allow-Origin", "*")
31 | c.Header("Access-Control-Allow-Methods", "GET, OPTIONS, POST")
32 | c.Header("Access-Control-Allow-Headers", "Authorization, Content-Type, x-requested-with")
33 | c.Status(200)
34 | return
35 | }
36 |
37 | // preserve request body for error logging
38 | var buf bytes.Buffer
39 | tee := io.TeeReader(c.Request.Body, &buf)
40 | bodyBytes, err := io.ReadAll(tee)
41 | if err != nil {
42 | log.Printf("Error reading request body: %v", err)
43 | return
44 | }
45 | c.Request.Body = io.NopCloser(&buf)
46 |
47 | director := func(req *http.Request) {
48 | if req.Body == nil {
49 | util.SendError(c, errors.New("request body is empty"))
50 | return
51 | }
52 | body, _ := io.ReadAll(req.Body)
53 | req.Body = io.NopCloser(bytes.NewBuffer(body))
54 |
55 | // get model from url params or body
56 | model := c.Param("model")
57 | if model == "" {
58 | _model, err := sonic.Get(body, "model")
59 | if err != nil {
60 | util.SendError(c, errors.Wrap(err, "get model error"))
61 | return
62 | }
63 | _modelStr, err := _model.String()
64 | if err != nil {
65 | util.SendError(c, errors.Wrap(err, "get model name error"))
66 | return
67 | }
68 | model = _modelStr
69 | }
70 |
71 | // get deployment from request
72 | deployment, err := GetDeploymentByModel(model)
73 | if err != nil {
74 | util.SendError(c, err)
75 | return
76 | }
77 |
78 | // get auth token from header or deployemnt config
79 | token := deployment.ApiKey
80 | if token == "" {
81 | rawToken := req.Header.Get("Authorization")
82 | token = strings.TrimPrefix(rawToken, "Bearer ")
83 | }
84 | if token == "" {
85 | util.SendError(c, errors.New("token is empty"))
86 | return
87 | }
88 | req.Header.Set("Authorization", token)
89 |
90 | originURL := req.URL.String()
91 | req, err = requestConverter.Convert(req, deployment)
92 | if err != nil {
93 | util.SendError(c, errors.Wrap(err, "convert request error"))
94 | return
95 | }
96 |
97 | log.Printf("proxying request [%s] %s -> %s", model, originURL, maskURL.ReplaceAllString(req.URL.String(), "${YI-API-SERVER}/v1/"))
98 | }
99 |
100 | proxy := &httputil.ReverseProxy{Director: director}
101 | transport, err := util.NewProxyFromEnv()
102 | if err != nil {
103 | util.SendError(c, errors.Wrap(err, "get proxy error"))
104 | return
105 | }
106 | if transport != nil {
107 | proxy.Transport = transport
108 | }
109 |
110 | proxy.ServeHTTP(c.Writer, c.Request)
111 |
112 | // issue: https://github.com/Chanzhaoyu/chatgpt-web/issues/831
113 | if c.Writer.Header().Get("Content-Type") == "text/event-stream" {
114 | if _, err := c.Writer.Write([]byte{'\n'}); err != nil {
115 | log.Printf("rewrite response error: %v", err)
116 | }
117 | }
118 |
119 | if c.Writer.Status() != 200 {
120 | log.Printf("encountering error with body: %s", string(bodyBytes))
121 | }
122 | }
123 |
124 | func GetDeploymentByModel(model string) (*DeploymentConfig, error) {
125 | deploymentConfig, exist := ModelDeploymentConfig[model]
126 | if !exist {
127 | return nil, errors.New(fmt.Sprintf("deployment config for %s not found", model))
128 | }
129 | return &deploymentConfig, nil
130 | }
131 |
--------------------------------------------------------------------------------