├── .github └── workflows │ └── go.yml ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── cmd └── proxy-to-gemini │ └── main.go ├── docs └── quickstart.md ├── go.mod ├── go.sum ├── internal └── internal.go ├── ollama └── ollama.go └── openai ├── chat.go ├── chat_test.go ├── embeddings.go ├── openai.go └── streaming.go /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Go 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v4 18 | 19 | - name: Set up Go 20 | uses: actions/setup-go@v4 21 | with: 22 | go-version: '1.22' 23 | 24 | - name: Build 25 | run: go build -v ./... 26 | 27 | - name: Test 28 | run: go test -v ./... 29 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We would love to accept your patches and contributions to this project. 4 | 5 | ## Before you begin 6 | 7 | ### Sign our Contributor License Agreement 8 | 9 | Contributions to this project must be accompanied by a 10 | [Contributor License Agreement](https://cla.developers.google.com/about) (CLA). 11 | You (or your employer) retain the copyright to your contribution; this simply 12 | gives us permission to use and redistribute your contributions as part of the 13 | project. 14 | 15 | If you or your current employer have already signed the Google CLA (even if it 16 | was for a different project), you probably don't need to do it again. 17 | 18 | Visit to see your current agreements or to 19 | sign a new one. 20 | 21 | ### Review our Community Guidelines 22 | 23 | This project follows [Google's Open Source Community 24 | Guidelines](https://opensource.google/conduct/). 25 | 26 | ## Contribution process 27 | 28 | ### Code Reviews 29 | 30 | All submissions, including submissions by project members, require review. We 31 | use [GitHub pull requests](https://docs.github.com/articles/about-pull-requests) 32 | for this purpose. 33 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang AS builder 2 | WORKDIR /go/code 3 | ADD . /go/code 4 | RUN CGO_ENABLED=0 go build -o /proxy ./cmd/proxy-to-gemini 5 | 6 | FROM alpine:latest 7 | COPY --from=builder /proxy /proxy-to-gemini 8 | RUN apk --no-cache add ca-certificates \ 9 | && update-ca-certificates 10 | ENTRYPOINT ["/proxy-to-gemini"] 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | DOCKER_IMAGE = "googlegemini/proxy-to-gemini" 2 | 3 | build: 4 | docker build -t $(DOCKER_IMAGE) . 5 | 6 | publish: build 7 | docker push $(DOCKER_IMAGE) 8 | 9 | run: build 10 | docker run -p 5555:5555 -e GEMINI_API_KEY=${GEMINI_API_KEY} $(DOCKER_IMAGE) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # proxy-to-gemini 2 | 3 | [![Go](https://github.com/google-gemini/proxy-to-gemini/actions/workflows/go.yml/badge.svg)](https://github.com/google-gemini/proxy-to-gemini/actions/workflows/go.yml) 4 | 5 | > [!IMPORTANT] 6 | > Gemini API officially supports OpenAI API compatibility and this sidecar is no longer needed. 7 | > See the [blog post](https://developers.googleblog.com/en/gemini-is-now-accessible-from-the-openai-library/) for more details! 8 | 9 | 10 | A simple proxy server to access Gemini models by using other well-known APIs like OpenAI and Ollama. 11 | 12 | ## Setup 13 | 14 | Obtain a Gemini API key from the [AI Studio](https://aistudio.google.com/). 15 | Then set the following environmental variable to the key. 16 | 17 | ```sh 18 | $ export GEMINI_API_KEY= 19 | ``` 20 | 21 | ## Usage with OpenAI API 22 | 23 | Run the binary: 24 | 25 | ```sh 26 | $ docker run -p 5555:5555 -e GEMINI_API_KEY=$GEMINI_API_KEY googlegemini/proxy-to-gemini 27 | 2024/07/20 19:35:21 Starting server on :5555 28 | ``` 29 | 30 | Once server starts, you can access Gemini models through the proxy server 31 | by using OpenAI API and client libraries. 32 | 33 | ``` sh 34 | $ curl http://127.0.0.1:5555/v1/chat/completions \ 35 | -H "Content-Type: application/json" \ 36 | -d '{ 37 | "model": "gemini-1.5-pro", 38 | "messages": [{"role": "user", "content": "Hello, world!"}] 39 | }' 40 | { 41 | "object": "chat.completion", 42 | "created": 1721535029, 43 | "model": "gemini-1.5-pro", 44 | "choices": [ 45 | { 46 | "index": 0, 47 | "message": { 48 | "role": "model", 49 | "content": "Hello back to you! \n\nIt's great to hear from you. What can I do for you today? \n" 50 | }, 51 | "finish_reason": "stop" 52 | } 53 | ], 54 | "usage": { 55 | "prompt_tokens": 5, 56 | "total_tokens": 29, 57 | "completion_tokens": 24 58 | } 59 | } 60 | ``` 61 | 62 | You can stream the chat responses: 63 | 64 | ```sh 65 | $ curl http://127.0.0.1:5555/v1/chat/completions \ 66 | -H "Content-Type: application/json" \ 67 | -d '{ 68 | "model": "gemini-1.5-pro", 69 | "messages": [{"role": "user", "content": "Hello, world!"}], 70 | "stream": true 71 | }' 72 | data: {"object":"chat.completion.chunk","created":1725852986,"model":"gemini-1.5-pro","choices":[{"index":0,"message":{"role":"model","content":"Hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"total_tokens":6,"completion_tokens":1}} 73 | data: {"object":"chat.completion.chunk","created":1725852986,"model":"gemini-1.5-pro","choices":[{"index":0,"message":{"role":"model","content":" back! \n\nIt's nice to be greeted by the classic \""},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"total_tokens":21,"completion_tokens":16}} 74 | data: {"object":"chat.completion.chunk","created":1725852987,"model":"gemini-1.5-pro","choices":[{"index":0,"message":{"role":"model","content":"Hello, world!\" What can I help you with today? \n"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"total_tokens":35,"completion_tokens":30}} 75 | data: [DONE] 76 | ``` 77 | 78 | You can create embeddings: 79 | 80 | ```sh 81 | $ curl http://127.0.0.1:5555/v1/embeddings \ 82 | -H "Content-Type: application/json" \ 83 | -d '{ 84 | "model": "text-embedding-004", 85 | "input": ["hello"] 86 | }' 87 | { 88 | "object": "list", 89 | "data": [ 90 | { 91 | "object": "embedding", 92 | "embedding": [ 93 | 0.04824496, 94 | 0.0117766075, 95 | -0.011552069, 96 | -0.018164534, 97 | -0.0026110192, 98 | 0.05092675, 99 | ... 100 | 0.0002852207, 101 | 0.046413545 102 | ], 103 | "index": 0 104 | } 105 | ], 106 | "model": "text-embedding-004", 107 | } 108 | ``` 109 | 110 | ### Known OpenAI Limitations 111 | 112 | * Only [chat completions](https://platform.openai.com/docs/api-reference/chat) and [embeddings](https://platform.openai.com/docs/api-reference/embeddings/create) are planned to be supported. 113 | * Tool support is work in progress. 114 | * Only text input and output is supported for now. 115 | * response_format is not supported yet. 116 | 117 | ## Usage with Ollama API 118 | 119 | ``` sh 120 | $ docker run -p 5555:5555 -e GEMINI_API_KEY=$GEMINI_API_KEY googlegemini/proxy-to-gemini -api=ollama 121 | 2024/07/20 19:35:21 Starting server on :5555 122 | ``` 123 | Once server starts, you can access Gemini models through the proxy server 124 | by using Ollama API and client libraries. 125 | 126 | ``` sh 127 | $ curl http://127.0.0.1:5555/api/generate \ 128 | -H "Content-Type: application/json" \ 129 | -d '{ 130 | "model": "gemini-1.5-pro", 131 | "prompt": "Hello, how are you?" 132 | }' 133 | {"model":"gemini-1.5-pro","response":"I'm doing well, thank you! As an AI, I don't have feelings, but I'm here and ready to assist you. \n\nHow can I help you today? \n","created_at":"2024-07-28T14:57:36.25261-07:00","prompt_eval_count":7,"eval_count":47,"done":true} 134 | ``` 135 | 136 | Create embeddings: 137 | 138 | ```sh 139 | $ curl http://127.0.0.1:5555/api/embed \ 140 | -H "Content-Type: application/json" \ 141 | -d '{ 142 | "model": "text-embedding-004", 143 | "input": ["hello"] 144 | }' 145 | {"model":"text-embedding-004","embeddings":[[0.04824496,0.0117766075,-0.011552069,-0.018164534,-0.0026110192,0.05092675,0.08172899,0.007869772,0.054475933,0.026131334,-0.06593486,-0.002256868,0.038781915,...]]} 146 | ``` 147 | 148 | ### Known Ollama Limitations 149 | * Streaming is not yet supported. 150 | * Images are not supported. 151 | * Response format is not supported. 152 | * Model parameters not supported by Gemini are ignored. 153 | 154 | ## Notes 155 | 156 | The list of available models are listed at [Gemini API docs](https://ai.google.dev/gemini-api/docs/models/gemini). 157 | 158 | This proxy is aiming users to try out the Gemini models easily. Hence, 159 | it mainly supports text based use cases. Please refer to the Gemini SDKs 160 | for media support. 161 | -------------------------------------------------------------------------------- /cmd/proxy-to-gemini/main.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "context" 19 | "flag" 20 | "fmt" 21 | "log" 22 | "net/http" 23 | "os" 24 | 25 | "github.com/google-gemini/proxy-to-gemini/ollama" 26 | "github.com/google-gemini/proxy-to-gemini/openai" 27 | "github.com/google/generative-ai-go/genai" 28 | "github.com/gorilla/mux" 29 | "google.golang.org/api/option" 30 | ) 31 | 32 | var ( 33 | apikey string 34 | hostport string 35 | api string 36 | ) 37 | 38 | func main() { 39 | ctx := context.Background() 40 | 41 | flag.StringVar(&hostport, "listen", ":5555", "host and port to listen on") 42 | flag.StringVar(&api, "api", "openai", "API proxocol; openai or ollama") 43 | flag.Parse() 44 | 45 | apikey = os.Getenv("GEMINI_API_KEY") 46 | if apikey == "" { 47 | log.Fatal("GEMINI_API_KEY environment variable not set") 48 | } 49 | client, err := genai.NewClient(ctx, option.WithAPIKey(apikey)) 50 | if err != nil { 51 | log.Fatal(err) 52 | } 53 | defer client.Close() 54 | 55 | r := mux.NewRouter() 56 | r.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { 57 | fmt.Fprint(w, "ok") 58 | }) 59 | switch api { 60 | case "openai": 61 | openai.RegisterHandlers(r, client) 62 | case "ollama": 63 | ollama.RegisterHandlers(r, client) 64 | } 65 | r.HandleFunc("/", indexHandler) 66 | 67 | log.Printf("Starting server on %v", hostport) 68 | if err := http.ListenAndServe(hostport, r); err != nil { 69 | log.Printf("Error starting server: %v", err) 70 | } 71 | } 72 | 73 | func indexHandler(w http.ResponseWriter, r *http.Request) { 74 | fmt.Fprintf(w, "You are running proxy-to-gemini at %q; api = %q", hostport, api) 75 | } 76 | -------------------------------------------------------------------------------- /docs/quickstart.md: -------------------------------------------------------------------------------- 1 | # Quickstart 2 | 3 | ## OpenAI API 4 | 5 | Obtain a Gemini API key from the [AI Studio](https://aistudio.google.com/). 6 | Then set the following environmental variable to the key and run the proxy: 7 | 8 | ```sh 9 | $ export GEMINI_API_KEY= 10 | $ docker run -p 5555:5555 -e GEMINI_API_KEY=$GEMINI_API_KEY googlegemini/proxy-to-gemini 11 | ``` 12 | 13 | Set the following environment variable to the proxy: 14 | 15 | ```sh 16 | $ export OPENAI_BASE_URL="http://127.0.0.1:5555/v1" 17 | ``` 18 | 19 | Then the OpenAI Python client library will use the proxy. 20 | Save the following code in test.py: 21 | 22 | ```python 23 | from openai import OpenAI 24 | 25 | client = OpenAI() 26 | chat_completion = client.chat.completions.create( 27 | model = "gemini-1.5-pro", 28 | messages=[ 29 | { 30 | "role": "user", 31 | "content": "Say this is a test", 32 | } 33 | ], 34 | ) 35 | print(chat_completion.chat_completion.choices) 36 | ``` 37 | 38 | Run the Python file: 39 | 40 | ```sh 41 | $ python test.py 42 | [Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='This is a test. \n', refusal=None, role='model', function_call=None, tool_calls=None))] 43 | ``` -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/google-gemini/proxy-to-gemini 2 | 3 | go 1.21 4 | 5 | toolchain go1.22.5 6 | 7 | require ( 8 | github.com/google/generative-ai-go v0.17.0 9 | github.com/gorilla/mux v1.8.1 10 | google.golang.org/api v0.188.0 11 | ) 12 | 13 | require ( 14 | cloud.google.com/go v0.115.0 // indirect 15 | cloud.google.com/go/ai v0.8.0 // indirect 16 | cloud.google.com/go/auth v0.7.0 // indirect 17 | cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect 18 | cloud.google.com/go/compute/metadata v0.4.0 // indirect 19 | cloud.google.com/go/longrunning v0.5.7 // indirect 20 | github.com/felixge/httpsnoop v1.0.4 // indirect 21 | github.com/go-logr/logr v1.4.1 // indirect 22 | github.com/go-logr/stdr v1.2.2 // indirect 23 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect 24 | github.com/golang/protobuf v1.5.4 // indirect 25 | github.com/google/s2a-go v0.1.7 // indirect 26 | github.com/google/uuid v1.6.0 // indirect 27 | github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect 28 | github.com/googleapis/gax-go/v2 v2.12.5 // indirect 29 | go.opencensus.io v0.24.0 // indirect 30 | go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect 31 | go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect 32 | go.opentelemetry.io/otel v1.26.0 // indirect 33 | go.opentelemetry.io/otel/metric v1.26.0 // indirect 34 | go.opentelemetry.io/otel/trace v1.26.0 // indirect 35 | golang.org/x/crypto v0.31.0 // indirect 36 | golang.org/x/net v0.27.0 // indirect 37 | golang.org/x/oauth2 v0.21.0 // indirect 38 | golang.org/x/sync v0.10.0 // indirect 39 | golang.org/x/sys v0.28.0 // indirect 40 | golang.org/x/text v0.21.0 // indirect 41 | golang.org/x/time v0.5.0 // indirect 42 | google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 // indirect 43 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect 44 | google.golang.org/grpc v1.64.1 // indirect 45 | google.golang.org/protobuf v1.34.2 // indirect 46 | ) 47 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | cloud.google.com/go v0.115.0 h1:CnFSK6Xo3lDYRoBKEcAtia6VSC837/ZkJuRduSFnr14= 3 | cloud.google.com/go v0.115.0/go.mod h1:8jIM5vVgoAEoiVxQ/O4BFTfHqulPZgs/ufEzMcFMdWU= 4 | cloud.google.com/go/ai v0.8.0 h1:rXUEz8Wp2OlrM8r1bfmpF2+VKqc1VJpafE3HgzRnD/w= 5 | cloud.google.com/go/ai v0.8.0/go.mod h1:t3Dfk4cM61sytiggo2UyGsDVW3RF1qGZaUKDrZFyqkE= 6 | cloud.google.com/go/auth v0.7.0 h1:kf/x9B3WTbBUHkC+1VS8wwwli9TzhSt0vSTVBmMR8Ts= 7 | cloud.google.com/go/auth v0.7.0/go.mod h1:D+WqdrpcjmiCgWrXmLLxOVq1GACoE36chW6KXoEvuIw= 8 | cloud.google.com/go/auth/oauth2adapt v0.2.2 h1:+TTV8aXpjeChS9M+aTtN/TjdQnzJvmzKFt//oWu7HX4= 9 | cloud.google.com/go/auth/oauth2adapt v0.2.2/go.mod h1:wcYjgpZI9+Yu7LyYBg4pqSiaRkfEK3GQcpb7C/uyF1Q= 10 | cloud.google.com/go/compute/metadata v0.4.0 h1:vHzJCWaM4g8XIcm8kopr3XmDA4Gy/lblD3EhhSux05c= 11 | cloud.google.com/go/compute/metadata v0.4.0/go.mod h1:SIQh1Kkb4ZJ8zJ874fqVkslA29PRXuleyj6vOzlbK7M= 12 | cloud.google.com/go/longrunning v0.5.7 h1:WLbHekDbjK1fVFD3ibpFFVoyizlLRl73I7YKuAKilhU= 13 | cloud.google.com/go/longrunning v0.5.7/go.mod h1:8GClkudohy1Fxm3owmBGid8W0pSgodEMwEAztp38Xng= 14 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 15 | github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= 16 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 17 | github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= 18 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 19 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 20 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 21 | github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 22 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 23 | github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= 24 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 25 | github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= 26 | github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= 27 | github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= 28 | github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= 29 | github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= 30 | github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= 31 | github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= 32 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 33 | github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 34 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= 35 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 36 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 37 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 38 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 39 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 40 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 41 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 42 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 43 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 44 | github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= 45 | github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 46 | github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= 47 | github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= 48 | github.com/google/generative-ai-go v0.17.0 h1:kUmCXUIwJouD7I7ev3OmxzzQVICyhIWAxaXk2yblCMY= 49 | github.com/google/generative-ai-go v0.17.0/go.mod h1:JYolL13VG7j79kM5BtHz4qwONHkeJQzOCkKXnpqtS/E= 50 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 51 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 52 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 53 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 54 | github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 55 | github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 56 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 57 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 58 | github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= 59 | github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= 60 | github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 61 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= 62 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 63 | github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= 64 | github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= 65 | github.com/googleapis/gax-go/v2 v2.12.5 h1:8gw9KZK8TiVKB6q3zHY3SBzLnrGp6HQjyfYBYGmXdxA= 66 | github.com/googleapis/gax-go/v2 v2.12.5/go.mod h1:BUDKcWo+RaKq5SC9vVYL0wLADa3VcfswbOMMRmB9H3E= 67 | github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= 68 | github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= 69 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 70 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 71 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 72 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 73 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 74 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 75 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 76 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 77 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 78 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 79 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 80 | go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= 81 | go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= 82 | go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 h1:A3SayB3rNyt+1S6qpI9mHPkeHTZbD7XILEqWnYZb2l0= 83 | go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0/go.mod h1:27iA5uvhuRNmalO+iEUdVn5ZMj2qy10Mm+XRIpRmyuU= 84 | go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 h1:Xs2Ncz0gNihqu9iosIZ5SkBbWo5T8JhhLJFMQL1qmLI= 85 | go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0/go.mod h1:vy+2G/6NvVMpwGX/NyLqcC41fxepnuKHk16E6IZUcJc= 86 | go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs= 87 | go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4= 88 | go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30= 89 | go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4= 90 | go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA= 91 | go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0= 92 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 93 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 94 | golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= 95 | golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= 96 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 97 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 98 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 99 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 100 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 101 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 102 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 103 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 104 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 105 | golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= 106 | golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= 107 | golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= 108 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 109 | golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= 110 | golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= 111 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 112 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 113 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 114 | golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= 115 | golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 116 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 117 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 118 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 119 | golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 120 | golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= 121 | golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 122 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 123 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 124 | golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= 125 | golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= 126 | golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= 127 | golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= 128 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 129 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 130 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 131 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 132 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 133 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 134 | google.golang.org/api v0.188.0 h1:51y8fJ/b1AaaBRJr4yWm96fPcuxSo0JcegXE3DaHQHw= 135 | google.golang.org/api v0.188.0/go.mod h1:VR0d+2SIiWOYG3r/jdm7adPW9hI2aRv9ETOSCQ9Beag= 136 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 137 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 138 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 139 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= 140 | google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= 141 | google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 h1:MuYw1wJzT+ZkybKfaOXKp5hJiZDn2iHaXRw0mRYdHSc= 142 | google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4/go.mod h1:px9SlOOZBg1wM1zdnr8jEL4CNGUBZ+ZKYtNPApNQc4c= 143 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b h1:04+jVzTs2XBnOZcPsLnmrTGqltqJbZQ1Ey26hjYdQQ0= 144 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= 145 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 146 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 147 | google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= 148 | google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 149 | google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= 150 | google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= 151 | google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= 152 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 153 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 154 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 155 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 156 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 157 | google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 158 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 159 | google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 160 | google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= 161 | google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= 162 | google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= 163 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 164 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 165 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 166 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 167 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 168 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 169 | -------------------------------------------------------------------------------- /internal/internal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package internal 16 | 17 | import ( 18 | "fmt" 19 | "log" 20 | "net/http" 21 | ) 22 | 23 | func ErrorHandler(w http.ResponseWriter, r *http.Request, code int, msg string, arg ...interface{}) { 24 | if len(arg) > 0 { 25 | msg = fmt.Sprintf(msg, arg...) 26 | } 27 | log.Printf("Error responding: %v", msg) 28 | http.Error(w, msg, code) 29 | } 30 | -------------------------------------------------------------------------------- /ollama/ollama.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package ollama provies handlers that proxies 16 | // ollama API calls to Gemini models. 17 | package ollama 18 | 19 | import ( 20 | "encoding/json" 21 | "io" 22 | "net/http" 23 | "strings" 24 | "time" 25 | 26 | "github.com/google-gemini/proxy-to-gemini/internal" 27 | "github.com/google/generative-ai-go/genai" 28 | "github.com/gorilla/mux" 29 | ) 30 | 31 | type handlers struct { 32 | client *genai.Client 33 | } 34 | 35 | func RegisterHandlers(r *mux.Router, client *genai.Client) { 36 | handlers := &handlers{client: client} 37 | r.HandleFunc("/api/generate", handlers.generateHandler) 38 | r.HandleFunc("/api/embed", handlers.embedHandler) 39 | } 40 | 41 | func (h *handlers) generateHandler(w http.ResponseWriter, r *http.Request) { 42 | body, err := io.ReadAll(r.Body) 43 | if err != nil { 44 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to read request body: %v", err) 45 | return 46 | } 47 | defer r.Body.Close() 48 | 49 | var req GenerateRequest 50 | if err := json.Unmarshal(body, &req); err != nil { 51 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to unmarshal request body: %v", err) 52 | return 53 | } 54 | 55 | model := h.client.GenerativeModel(req.Model) 56 | model.GenerationConfig = genai.GenerationConfig{ 57 | Temperature: req.Options.Temperature, 58 | MaxOutputTokens: req.Options.NumPredict, 59 | TopK: req.Options.TopK, 60 | TopP: req.Options.TopP, 61 | } 62 | if req.Options.Stop != nil { 63 | model.GenerationConfig.StopSequences = []string{*req.Options.Stop} 64 | } 65 | if req.System != "" { 66 | model.SystemInstruction = &genai.Content{ 67 | Role: "system", 68 | Parts: []genai.Part{genai.Text(req.System)}, 69 | } 70 | } 71 | parts := []genai.Part{genai.Text(req.Prompt)} 72 | gresp, err := model.GenerateContent(r.Context(), parts...) 73 | if err != nil { 74 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to generate content: %v", err) 75 | return 76 | } 77 | if len(gresp.Candidates) == 0 { 78 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "no candidates returned") 79 | return 80 | } 81 | 82 | responseBuilder := &strings.Builder{} 83 | for _, part := range gresp.Candidates[0].Content.Parts { 84 | switch v := part.(type) { 85 | case genai.Text: 86 | responseBuilder.WriteString(string(v)) 87 | default: 88 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "unsupported part type: %T", v) 89 | return 90 | } 91 | } 92 | if err := json.NewEncoder(w).Encode(&GenerateResponse{ 93 | Model: req.Model, 94 | Response: responseBuilder.String(), 95 | CreatedAt: time.Now(), 96 | PromptEvalCount: gresp.UsageMetadata.PromptTokenCount, 97 | EvalCount: gresp.UsageMetadata.TotalTokenCount, 98 | Done: true, 99 | }); err != nil { 100 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to encode generate response: %v", err) 101 | return 102 | } 103 | } 104 | 105 | func (h *handlers) embedHandler(w http.ResponseWriter, r *http.Request) { 106 | body, err := io.ReadAll(r.Body) 107 | if err != nil { 108 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to read request body: %v", err) 109 | return 110 | } 111 | defer r.Body.Close() 112 | 113 | var req EmbedRequest 114 | if err := json.Unmarshal(body, &req); err != nil { 115 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to unmarshal request body: %v", err) 116 | return 117 | } 118 | 119 | model := h.client.EmbeddingModel(req.Model) 120 | batch := model.NewBatch() 121 | for _, input := range req.Input { 122 | batch.AddContent(genai.Text(input)) 123 | } 124 | 125 | gresp, err := model.BatchEmbedContents(r.Context(), batch) 126 | if err != nil { 127 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to create embedding: %v", err) 128 | return 129 | } 130 | 131 | embeddings := make([][]float32, 0, len(gresp.Embeddings)) 132 | for _, embedding := range gresp.Embeddings { 133 | embeddings = append(embeddings, embedding.Values) 134 | } 135 | 136 | if err := json.NewEncoder(w).Encode(&EmbedResponse{ 137 | Model: req.Model, 138 | Embeddings: embeddings, 139 | }); err != nil { 140 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to encode embeddings response: %v", err) 141 | return 142 | } 143 | } 144 | 145 | type GenerateRequest struct { 146 | Model string `json:"model,omitempty"` 147 | Prompt string `json:"prompt,omitempty"` 148 | Suffix string `json:"suffix,omitempty"` 149 | Options Options `json:"options,omitempty"` 150 | System string `json:"system,omitempty"` 151 | 152 | // TODO: Support images. 153 | // TODO: Support format. 154 | // TODO: Support streaming. 155 | } 156 | 157 | type GenerateResponse struct { 158 | Model string `json:"model,omitempty"` 159 | Response string `json:"response,omitempty"` 160 | CreatedAt time.Time `json:"created_at,omitempty"` 161 | 162 | PromptEvalCount int32 `json:"prompt_eval_count,omitempty"` 163 | EvalCount int32 `json:"eval_count,omitempty"` 164 | 165 | Done bool `json:"done,omitempty"` 166 | } 167 | 168 | type Options struct { 169 | Temperature *float32 `json:"temperature,omitempty"` 170 | Stop *string `json:"stop,omitempty"` 171 | NumPredict *int32 `json:"num_predict,omitempty"` 172 | TopK *int32 `json:"top_k,omitempty"` 173 | TopP *float32 `json:"top_p,omitempty"` 174 | 175 | // TODO: Anything else to support? 176 | } 177 | 178 | type EmbedRequest struct { 179 | Model string `json:"model,omitempty"` 180 | Input []string `json:"input,omitempty"` 181 | } 182 | 183 | type EmbedResponse struct { 184 | Model string `json:"model,omitempty"` 185 | Embeddings [][]float32 `json:"embeddings,omitempty"` 186 | } 187 | -------------------------------------------------------------------------------- /openai/chat.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package openai 16 | 17 | import ( 18 | "encoding/json" 19 | "io" 20 | "log" 21 | "net/http" 22 | "reflect" 23 | "strings" 24 | "time" 25 | 26 | "github.com/google-gemini/proxy-to-gemini/internal" 27 | "github.com/google/generative-ai-go/genai" 28 | ) 29 | 30 | func (h *handlers) ChatCompletionsHandler(w http.ResponseWriter, r *http.Request) { 31 | if r.Method != http.MethodPost { 32 | internal.ErrorHandler(w, r, http.StatusMethodNotAllowed, "method not allowed") 33 | return 34 | } 35 | body, err := io.ReadAll(r.Body) 36 | if err != nil { 37 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to read request body: %v", err) 38 | return 39 | } 40 | defer r.Body.Close() 41 | 42 | var chatReq ChatCompletionRequest 43 | if err := json.Unmarshal(body, &chatReq); err != nil { 44 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to parse chat completions body: %v", err) 45 | return 46 | } 47 | 48 | model := h.geminiClient.GenerativeModel(chatReq.Model) 49 | model.GenerationConfig = genai.GenerationConfig{ 50 | CandidateCount: chatReq.N, 51 | StopSequences: chatReq.Stop, 52 | ResponseMIMEType: "text/plain", 53 | MaxOutputTokens: chatReq.MaxTokens, 54 | Temperature: chatReq.Temperature, 55 | TopP: chatReq.TopP, 56 | } 57 | 58 | chat := model.StartChat() 59 | var lastPart genai.Part 60 | for i, r := range chatReq.Messages { 61 | if r.Role == "system" { 62 | model.SystemInstruction = &genai.Content{ 63 | Role: r.Role, 64 | Parts: []genai.Part{genai.Text(r.Content)}, 65 | } 66 | continue 67 | } 68 | if i == len(chatReq.Messages)-1 { // the last message 69 | // TODO(jbd): This hack strips away the role of the last message. 70 | // But Gemini API Go SDK doesn't give flexibility to call SendMessage 71 | // with a list of contents. 72 | lastPart = genai.Text(r.Content) 73 | break 74 | } 75 | chat.History = append(chat.History, &genai.Content{ 76 | Role: r.Role, 77 | Parts: []genai.Part{genai.Text(r.Content)}, 78 | }) 79 | } 80 | 81 | if chatReq.Stream { 82 | streamingChatCompletionsHandler(w, r, chatReq.Model, chat, lastPart) 83 | return 84 | } 85 | 86 | geminiResp, err := chat.SendMessage(r.Context(), lastPart) 87 | if err != nil { 88 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to generate content: %v", err) 89 | return 90 | } 91 | 92 | resp := toOpenAIResponse(geminiResp, "chat.completion", chatReq.Model) 93 | if err := json.NewEncoder(w).Encode(resp); err != nil { 94 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to encode chat completions response: %v", err) 95 | return 96 | } 97 | } 98 | 99 | func toOpenAIResponse(from *genai.GenerateContentResponse, object, model string) (to ChatCompletionResponse) { 100 | to.Object = object 101 | to.Created = time.Now().Unix() 102 | to.Model = model 103 | if from.UsageMetadata != nil { 104 | to.Usage = Usage{ 105 | PromptTokens: from.UsageMetadata.PromptTokenCount, 106 | CompletionTokens: from.UsageMetadata.CandidatesTokenCount, 107 | TotalTokens: from.UsageMetadata.TotalTokenCount, 108 | } 109 | } 110 | 111 | to.Choices = make([]ChatCompletionChoice, 0, len(from.Candidates)) 112 | for i, c := range from.Candidates { 113 | var builder strings.Builder 114 | for _, p := range c.Content.Parts { 115 | content, ok := p.(genai.Text) 116 | if !ok { 117 | log.Printf("failed to process content part; type = %v", reflect.TypeOf(p)) 118 | continue 119 | } 120 | builder.WriteString(string(content)) 121 | } 122 | choice := ChatCompletionChoice{ 123 | Index: i, 124 | Message: ChatMessage{ 125 | Role: c.Content.Role, 126 | Content: builder.String(), 127 | }, 128 | } 129 | 130 | finishReason := toGeminiFinishReason(c.FinishReason) 131 | if finishReason != "" { 132 | choice.FinishReason = finishReason 133 | } 134 | to.Choices = append(to.Choices, choice) 135 | } 136 | return to 137 | } 138 | 139 | func toGeminiFinishReason(code genai.FinishReason) string { 140 | switch code { 141 | case genai.FinishReasonStop: 142 | return "stop" 143 | case genai.FinishReasonMaxTokens: 144 | return "length" 145 | case genai.FinishReasonRecitation: 146 | return "content_filter" 147 | case genai.FinishReasonSafety: 148 | return "content_filter" 149 | case genai.FinishReasonOther: 150 | return "other" 151 | default: 152 | return "" 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /openai/chat_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package openai 16 | 17 | import ( 18 | "reflect" 19 | "testing" 20 | 21 | "github.com/google/generative-ai-go/genai" 22 | ) 23 | 24 | func Test_geminiToOpenAIResponse(t *testing.T) { 25 | tests := []struct { 26 | name string 27 | from *genai.GenerateContentResponse 28 | object string 29 | model string 30 | want ChatCompletionResponse 31 | }{ 32 | { 33 | name: "basic", 34 | from: &genai.GenerateContentResponse{ 35 | Candidates: []*genai.Candidate{ 36 | { 37 | Index: 0, 38 | Content: &genai.Content{ 39 | Parts: []genai.Part{ 40 | genai.Text("I'm good, how are you?"), 41 | }, 42 | Role: "model", 43 | }, 44 | }, 45 | { 46 | Index: 1, 47 | Content: &genai.Content{ 48 | Parts: []genai.Part{ 49 | genai.Text("Is there anything I can help with?"), 50 | }, 51 | Role: "model", 52 | }, 53 | FinishReason: genai.FinishReasonMaxTokens, 54 | }, 55 | }, 56 | UsageMetadata: &genai.UsageMetadata{ 57 | PromptTokenCount: 123, 58 | CandidatesTokenCount: 456, 59 | TotalTokenCount: 789, 60 | }, 61 | }, 62 | object: "chat.completion", 63 | model: "gemini1.5", 64 | want: ChatCompletionResponse{ 65 | Object: "chat.completion", 66 | Model: "gemini1.5", 67 | Choices: []ChatCompletionChoice{ 68 | { 69 | Index: 0, 70 | Message: ChatMessage{ 71 | Role: "model", 72 | Content: "I'm good, how are you?", 73 | }, 74 | FinishReason: "", 75 | }, 76 | { 77 | Index: 1, 78 | Message: ChatMessage{ 79 | Role: "model", 80 | Content: "Is there anything I can help with?", 81 | }, 82 | FinishReason: "length", 83 | }, 84 | }, 85 | Usage: Usage{ 86 | PromptTokens: 123, 87 | CompletionTokens: 456, 88 | TotalTokens: 789, 89 | }, 90 | }, 91 | }, 92 | { 93 | name: "no parts", 94 | from: &genai.GenerateContentResponse{ 95 | Candidates: []*genai.Candidate{ 96 | { 97 | Index: 0, 98 | Content: &genai.Content{ 99 | Parts: []genai.Part{}, 100 | Role: "model", 101 | }, 102 | }, 103 | }, 104 | }, 105 | object: "chat.completion", 106 | model: "gemini1.5", 107 | want: ChatCompletionResponse{ 108 | Object: "chat.completion", 109 | Model: "gemini1.5", 110 | Choices: []ChatCompletionChoice{ 111 | { 112 | Index: 0, 113 | Message: ChatMessage{ 114 | Role: "model", 115 | Content: "", 116 | }, 117 | }, 118 | }, 119 | }, 120 | }, 121 | } 122 | for _, tt := range tests { 123 | t.Run(tt.name, func(t *testing.T) { 124 | got := toOpenAIResponse(tt.from, tt.object, tt.model) 125 | got.Created = tt.want.Created 126 | if !reflect.DeepEqual(got, tt.want) { 127 | t.Errorf("geminiToOpenAIResponse() = %v, want %v", got, tt.want) 128 | } 129 | }) 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /openai/embeddings.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package openai 16 | 17 | import ( 18 | "encoding/json" 19 | "io" 20 | "net/http" 21 | 22 | "github.com/google-gemini/proxy-to-gemini/internal" 23 | "github.com/google/generative-ai-go/genai" 24 | ) 25 | 26 | func (h *handlers) EmbeddingsHandler(w http.ResponseWriter, r *http.Request) { 27 | if r.Method != http.MethodPost { 28 | internal.ErrorHandler(w, r, http.StatusMethodNotAllowed, "method not allowed") 29 | return 30 | } 31 | 32 | body, err := io.ReadAll(r.Body) 33 | if err != nil { 34 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to read request body: %v", err) 35 | return 36 | } 37 | defer r.Body.Close() 38 | 39 | var embeddingsReq EmbeddingsRequest 40 | if err := json.Unmarshal(body, &embeddingsReq); err != nil { 41 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to unmarshal request body: %v", err) 42 | return 43 | } 44 | 45 | model := h.geminiClient.EmbeddingModel(embeddingsReq.Model) 46 | batch := model.NewBatch() 47 | for _, content := range embeddingsReq.Input { 48 | batch.AddContent(genai.Text(content)) 49 | } 50 | 51 | geminiResp, err := model.BatchEmbedContents(r.Context(), batch) 52 | if err != nil { 53 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to make embeddings request: %v", err) 54 | return 55 | } 56 | 57 | embeddingsResp := &EmbeddingsResponse{ 58 | Object: "list", 59 | Model: embeddingsReq.Model, 60 | Data: make([]EmbeddingData, 0, len(geminiResp.Embeddings)), 61 | } 62 | for i, contentEmbedding := range geminiResp.Embeddings { 63 | embeddingsResp.Data = append(embeddingsResp.Data, EmbeddingData{ 64 | Index: i, 65 | Object: "embedding", 66 | Embedding: contentEmbedding.Values, 67 | }) 68 | } 69 | if err := json.NewEncoder(w).Encode(embeddingsResp); err != nil { 70 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to encode embeddings response: %v", err) 71 | return 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /openai/openai.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package openai implements HTTP handlers that implements 16 | // the OpenAI API and make calls to Gemini models. 17 | package openai 18 | 19 | import ( 20 | "github.com/google/generative-ai-go/genai" 21 | "github.com/gorilla/mux" 22 | ) 23 | 24 | // handlers provides various HTTP handlers 25 | // to transform OpenAI protocol to Gemini calls. 26 | type handlers struct { 27 | geminiClient *genai.Client 28 | } 29 | 30 | // RegisterHandlers registers the HTTP handlers on the mux. 31 | func RegisterHandlers(r *mux.Router, geminiClient *genai.Client) { 32 | handlers := &handlers{geminiClient: geminiClient} 33 | r.HandleFunc("/v1/embeddings", handlers.EmbeddingsHandler) 34 | r.HandleFunc("/v1/chat/completions", handlers.ChatCompletionsHandler) 35 | } 36 | 37 | type EmbeddingsRequest struct { 38 | Model string `json:"model"` 39 | Input []string `json:"input"` 40 | User string `json:"user,omitempty"` 41 | } 42 | 43 | type EmbeddingsResponse struct { 44 | Object string `json:"object"` 45 | Data []EmbeddingData `json:"data"` 46 | Model string `json:"model"` 47 | Usage Usage `json:"usage"` 48 | Error interface{} `json:"error,omitempty"` 49 | } 50 | 51 | type EmbeddingData struct { 52 | Object string `json:"object"` 53 | Embedding []float32 `json:"embedding"` 54 | Index int `json:"index"` 55 | } 56 | 57 | type Usage struct { 58 | PromptTokens int32 `json:"prompt_tokens,omitempty"` 59 | TotalTokens int32 `json:"total_tokens,omitempty"` 60 | CompletionTokens int32 `json:"completion_tokens,omitempty"` 61 | } 62 | 63 | type ChatCompletionRequest struct { 64 | // TODO: Support response_format 65 | // TODO: Add logit bias and logprobs/top_logprobs 66 | // TODO: Support tools 67 | // TODO: Support Stop to be string only 68 | Model string `json:"model"` 69 | Messages []ChatMessage `json:"messages"` 70 | 71 | Stream bool `json:"stream,omitempty"` 72 | StreamOptions StreamOptions `json:"stream_options,omitempty"` 73 | 74 | N *int32 `json:"n,omitempty"` 75 | Stop []string `json:"stop,omitempty"` 76 | MaxTokens *int32 `json:"max_tokens,omitempty"` 77 | FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"` 78 | PresencePenalty *float32 `json:"presence_penalty,omitempty"` 79 | Temperature *float32 `json:"temperature,omitempty"` 80 | TopP *float32 `json:"top_p,omitempty"` 81 | 82 | User string `json:"user,omitempty"` 83 | } 84 | 85 | type ChatMessage struct { 86 | Role string `json:"role"` 87 | Content string `json:"content"` 88 | } 89 | 90 | type ChatCompletionResponse struct { 91 | ID string `json:"id,omitempty"` 92 | Object string `json:"object,omitempty"` 93 | Created int64 `json:"created,omitempty"` 94 | Model string `json:"model,omitempty"` 95 | Choices []ChatCompletionChoice `json:"choices,omitempty"` 96 | Usage Usage `json:"usage,omitempty"` 97 | } 98 | 99 | type ChatCompletionChoice struct { 100 | Index int `json:"index"` 101 | Message ChatMessage `json:"message"` 102 | FinishReason string `json:"finish_reason"` 103 | } 104 | 105 | type StreamOptions struct { 106 | IncludeUsage bool `json:"include_usage,omitempty"` 107 | } 108 | -------------------------------------------------------------------------------- /openai/streaming.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Google LLC 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package openai 16 | 17 | import ( 18 | "encoding/json" 19 | "fmt" 20 | "net/http" 21 | 22 | "github.com/google-gemini/proxy-to-gemini/internal" 23 | "github.com/google/generative-ai-go/genai" 24 | "google.golang.org/api/iterator" 25 | ) 26 | 27 | func streamingChatCompletionsHandler(w http.ResponseWriter, r *http.Request, model string, chat *genai.ChatSession, lastPart genai.Part) { 28 | iter := chat.SendMessageStream(r.Context(), lastPart) 29 | 30 | for { 31 | gresp, err := iter.Next() 32 | if err == iterator.Done { 33 | break 34 | } 35 | if err != nil { 36 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to stream response: %v", err) 37 | return 38 | } 39 | chunk, err := json.Marshal(toOpenAIResponse(gresp, "chat.completion.chunk", model)) 40 | if err != nil { 41 | internal.ErrorHandler(w, r, http.StatusInternalServerError, "failed to marshal chunk: %v", err) 42 | return 43 | } 44 | fmt.Fprintf(w, "data: %s\n", chunk) 45 | } 46 | fmt.Fprint(w, "data: [DONE]\n") 47 | } 48 | --------------------------------------------------------------------------------