sourcegraph/internal
Beatrix f2590cbb36
Cody Gateway: Add Gemini models to PLG and Enterprise users (#63053)
CLOSE https://github.com/sourcegraph/cody-issues/issues/211 &
https://github.com/sourcegraph/cody-issues/issues/412 &
https://github.com/sourcegraph/cody-issues/issues/412
UNBLOCK https://github.com/sourcegraph/cody/pull/4360

* Add support for Google Gemini AI models as chat completions provider
* Add new `google` package to handle Google Generative AI client
* Update `client.go` and `codygateway.go` to handle the new Google
provider
* Set default models for chat, fast chat, and completions when Google is
the configured provider
* Add gemini-pro to the allowed list

<!-- 💡 To write a useful PR description, make sure that your description
covers:
- WHAT this PR is changing:
    - How was it PREVIOUSLY.
    - How it will be from NOW on.
- WHY this PR is needed.
- CONTEXT, i.e. to which initiative, project or RFC it belongs.

The structure of the description doesn't matter as much as covering
these points, so use
your best judgement based on your context.
Learn how to write good pull request description:
https://www.notion.so/sourcegraph/Write-a-good-pull-request-description-610a7fd3e613496eb76f450db5a49b6e?pvs=4
-->


## Test plan

<!-- All pull requests REQUIRE a test plan:
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles
-->

For Enterprise instances using google as provider:

1. In your Soucegraph local instance's Site Config, add the following:

```
    "accessToken": "REDACTED",
    "chatModel": "gemini-1.5-pro-latest",
    "provider": "google",
```

Note: You can get the accessToken for Gemini API in 1Password.

2. After saving the site config with the above change, run the following
curl command:

```
curl 'https://sourcegraph.test:3443/.api/completions/stream' -i \
-X POST \
-H 'authorization: token $LOCAL_INSTANCE_TOKEN' \
--data-raw '{"messages":[{"speaker":"human","text":"Who are you?"}],"maxTokensToSample":30,"temperature":0,"stopSequences":[],"timeoutMs":5000,"stream":true,"model":"gemini-1.5-pro-latest"}'
```

3. Expected Output:

```
❯ curl 'https://sourcegraph.test:3443/.api/completions/stream' -i \
-X POST \
-H 'authorization: token <REDACTED>' \
--data-raw '{"messages":[{"speaker":"human","text":"Who are you?"}],"maxTokensToSample":30,"temperature":0,"stopSequences":[],"timeoutMs":5000,"stream":true,"model":"gemini-1.5-pro-latest"}'

HTTP/2 200
access-control-allow-credentials: true
access-control-allow-origin:
alt-svc: h3=":3443"; ma=2592000
cache-control: no-cache
content-type: text/event-stream
date: Tue, 04 Jun 2024 05:45:33 GMT
server: Caddy
server: Caddy
vary: Accept-Encoding, Authorization, Cookie, Authorization, X-Requested-With, Cookie
x-accel-buffering: no
x-content-type-options: nosniff
x-frame-options: DENY
x-powered-by: Express
x-trace: d4b1f02a3e2882a3d52331335d217b03
x-trace-span: 728ec33860d3b5e6
x-trace-url: https://sourcegraph.test:3443/-/debug/jaeger/trace/d4b1f02a3e2882a3d52331335d217b03
x-xss-protection: 1; mode=block

event: completion
data: {"completion":"I","stopReason":"STOP"}

event: completion
data: {"completion":"I am a large language model, trained by Google. \n\nThink of me as","stopReason":"STOP"}

event: completion
data: {"completion":"I am a large language model, trained by Google. \n\nThink of me as a computer program that can understand and generate human-like text.","stopReason":"MAX_TOKENS"}

event: done
data: {}
```

Verified locally:


![image](https://github.com/sourcegraph/sourcegraph/assets/68532117/2e6c914d-7a77-4484-b693-16bbc394518c)

#### Before

Cody Gateway returns `no client known for upstream provider google`

```sh
curl -X 'POST' -d '{"messages":[{"speaker":"human","text":"Who are you?"}],"maxTokensToSample":30,"temperature":0,"stopSequences":[],"timeoutMs":5000,"stream":true,"model":"google/gemini-1.5-pro-latest"}' -H 'Accept: application/json' -H 'Authorization: token $YOUR_DOTCOM_TOKEN' -H 'Content-Type: application/json' 'https://sourcegraph.com/.api/completions/stream'

event: error
data: {"error":"no client known for upstream provider google"}

event: done
data: {
```

## Changelog

<!--
1. Ensure your pull request title is formatted as: $type($domain): $what
2. Add bullet list items for each additional detail you want to cover
(see example below)
5. You can edit this after the pull request was merged, as long as
release shipping it hasn't been promoted to the public.
6. For more information, please see this how-to
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c?

Audience: TS/CSE > Customers > Teammates (in that order).

Cheat sheet: $type = chore|fix|feat $domain:
source|search|ci|release|plg|cody|local|...
-->

<!--
Example:

Title: fix(search): parse quotes with the appropriate context
Changelog section:

## Changelog

- When a quote is used with regexp pattern type, then ...
- Refactored underlying code.
-->

Added support for Google as an LLM provider for Cody, with the following
models available through Cody Gateway: Gemini Pro (`gemini-pro-latest`),
Gemini 1.5 Flash (`gemini-1.5-flash-latest`), and Gemini 1.5 Pro
(`gemini-1.5-pro-latest`).
2024-06-04 23:46:36 +00:00
..
accesstoken bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
actor bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
adminanalytics bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
api chore: Use new cmp package to simplify comparisons (#63029) 2024-06-03 19:44:48 +08:00
appliance feat(appliance): deploy codeinsights-db (#63042) 2024-06-04 10:23:29 +00:00
audit bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
auth privateApp to set first_source_url contact property on HubSpot (#62920) 2024-05-30 16:16:03 -04:00
authbearer bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
authz authz: Fix panic when auth provider is missing (#62759) 2024-05-17 16:09:39 +02:00
batches lib/background: upgrade Routine interface with context and errors (#62136) 2024-05-24 10:04:55 -04:00
binary
bytesize bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
byteutils bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
cloud bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
cmd chore: Remove client construction from SignUp/In funcs (#62789) 2024-05-21 15:18:58 +02:00
codeintel chore(codeintel): usagesForSymbol - make start/end non-optional for now (#63059) 2024-06-04 18:38:49 +08:00
codemonitors bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
codygateway bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
collections chore: Rename Contains to IsSupersetOf (#63062) 2024-06-04 09:14:19 +00:00
comby bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
completions Cody Gateway: Add Gemini models to PLG and Enterprise users (#63053) 2024-06-04 23:46:36 +00:00
compute bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
conf Cody Gateway: Add Gemini models to PLG and Enterprise users (#63053) 2024-06-04 23:46:36 +00:00
cookie bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
ctags_config bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
database Syntactic indexing: enqueuer and scheduler (#62485) 2024-05-31 10:25:30 +01:00
debugserver feat/enterprise-portal: ConnectRPC layer for {Get/List}CodyGatewayAccess (#62771) 2024-05-27 13:39:57 -07:00
deviceid
diskcache all: use observation.TestContextTB instead of TestContext (#61751) 2024-04-10 14:07:39 +02:00
diskusage bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
dotcom dotcom: MockSourcegraphDotComMode requires a T for cleanup (#61172) 2024-03-14 20:27:21 +00:00
download
embeddings bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
encryption bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
endpoint bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
env Cleanup internalapi after gRPC migration (#60572) 2024-02-19 14:13:26 +01:00
errcode chore: Replace errors.As with generic As and AsInterface (#63047) 2024-06-04 01:56:55 +00:00
eventlogger bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
executor worker: Reduce frequency of very frequently run jobs (#62864) 2024-05-23 18:31:20 +02:00
extsvc syncer: Fix issues causing excessive fetches (#62837) 2024-05-23 20:52:21 +02:00
featureflag Feature flags: relax some constraints (#61343) 2024-03-25 10:39:01 -06:00
fileutil gitserver: Implement RefHash in backend (#62612) 2024-05-13 16:05:16 +02:00
github_apps bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
gitserver gitserver: grpc: relax BlameAuthor name field to allow arbitrary byte sequences (#62917) 2024-05-29 02:18:42 -07:00
goroutine lib/background: upgrade Routine interface with context and errors (#62136) 2024-05-24 10:04:55 -04:00
gosyntect bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
gqltestutil chore: Remove client construction from SignUp/In funcs (#62789) 2024-05-21 15:18:58 +02:00
gqlutil
grpc lib/background: upgrade Routine interface with context and errors (#62136) 2024-05-24 10:04:55 -04:00
guardrails bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
hashutil
highlight fix: Make most syntaxHighlighting config fields optional (#62817) 2024-05-22 06:25:33 +00:00
honey honey: add read locking to event.Fields call for NonSendingReader (#61886) 2024-04-15 15:19:38 +00:00
hostmatcher gomod: update or vendor buildkit, docker, hostmatcher and saml to resolve CVEs (#60130) 2024-02-05 13:14:15 +02:00
hostname
htmlutil Web: add mermaid diagram rendering (#62678) 2024-05-16 14:54:43 -04:00
httpcli httpcli: Remove useless err check (#62321) 2024-05-02 19:22:43 +02:00
httpserver lib/background: upgrade Routine interface with context and errors (#62136) 2024-05-24 10:04:55 -04:00
httptestutil Remove GitHub proxy service (#56485) 2023-09-14 19:43:40 +02:00
insights insights: Fix overview page (#62986) 2024-05-30 20:50:14 +08:00
instrumentation chore: upgrade otel SDK packages (#59564) 2024-01-15 20:08:54 +00:00
ipynb Render Jupyter notebooks (#62583) 2024-05-10 12:21:10 -04:00
jsonc
k8s/resource appliance: deploy cadvisor (#62942) 2024-05-29 10:04:33 +01:00
lazyregexp
license bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
licensing Cody Gateway: Add Gemini models to PLG and Enterprise users (#63053) 2024-06-04 23:46:36 +00:00
limiter bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
logging fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
luasandbox bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
mapfs bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
maps Revert "Remove unused internal/k8s package" (#61835) 2024-04-12 09:35:40 -04:00
markdown Render Jupyter notebooks (#62583) 2024-05-10 12:21:10 -04:00
memo
metrics bazel: native go-mockgen in Bazel (#60386) 2024-02-16 13:26:48 +00:00
notebooks bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
oauthtoken bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
oauthutil bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
observation chore: Simplify RequestState/PositionalRequestArgs o11y code (#63040) 2024-06-04 18:08:48 +08:00
oobmigration lib/background: upgrade Routine interface with context and errors (#62136) 2024-05-24 10:04:55 -04:00
opencodegraph fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
otlpenv
own gitserver: Add CommitLog API to replace client-side Commits (#62606) 2024-05-21 15:21:58 +02:00
packagefilters bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
paths Replace all traditional for-loops (#60988) 2024-03-11 16:05:47 +02:00
perforce bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
productsubscription bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
profiler bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
pubsub lib/background: upgrade Routine interface with context and errors (#62136) 2024-05-24 10:04:55 -04:00
randstring Replace all traditional for-loops (#60988) 2024-03-11 16:05:47 +02:00
ratelimit bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
rbac bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
rcache Fixing cache methods to use the right method to retrieve key value stores (#62087) 2024-04-22 21:51:21 +02:00
redislock bump to Go 1.22.1 (#60902) 2024-03-06 17:38:43 -07:00
redispool chore: Remove dead code (#61899) 2024-04-16 10:17:47 +02:00
repos repoupdater(scheduler): Prevent race condition of schedule preloading (#63086) 2024-06-04 22:55:10 +02:00
repoupdater bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
requestclient Replace all traditional for-loops (#60988) 2024-03-11 16:05:47 +02:00
requestinteraction requestinteraction: add X-Sourcegraph-Interaction-ID propagation (#58016) 2023-11-22 20:09:39 +00:00
rockskip rockskip: Replace second long-running process with gRPC API (#62734) 2024-05-21 17:45:27 +02:00
sams bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
sanitycheck
search Context: detect symbol names in query (#62976) 2024-05-30 11:52:01 -07:00
searcher bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
security bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
service httpcli: Move init function from conf package into httpcli (#62318) 2024-05-02 18:30:44 +02:00
session bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
settings Replace all traditional for-loops (#60988) 2024-03-11 16:05:47 +02:00
siteid bazel: first pass at moving moving logging linting into nogo (#58910) 2024-01-02 10:07:25 -08:00
slack
slices appliance: deploy prometheus (#62876) 2024-05-24 10:26:30 +01:00
sourcegraphoperator bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
src-cli Bump minimum src-cli version required (#62700) 2024-05-16 09:52:46 +00:00
src-prometheus fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
suspiciousnames bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
symbols bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
sysreq
telemetry fix typo on feature name (#62971) 2024-05-29 20:47:47 +00:00
telemetrygateway bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
temporarysettings
testutil build-tracker: fix convenience urls in env (#62340) 2024-05-01 14:26:34 +00:00
timeutil
trace chore: Break dependency of internal/trace on conf (#62177) 2024-04-30 21:12:39 +02:00
tracer chore: Break dependency of internal/trace on conf (#62177) 2024-04-30 21:12:39 +02:00
ttlcache bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
txemail bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
types Remove clone progress DB experiment (#61615) 2024-04-10 21:00:22 +02:00
unpack
updatecheck bug(release): update version strings in upgradeReadiness (#62990) 2024-05-31 12:19:31 -07:00
uploadhandler chore: Enable traces for uploads (#63025) 2024-06-03 04:17:50 -07:00
uploadstore bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
usagestats fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
users logger: update log lib and remove use of description (#57690) 2023-10-18 17:29:08 +02:00
vcs bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
version bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
webhooks/outbound bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
workerutil lib/background: upgrade Routine interface with context and errors (#62136) 2024-05-24 10:04:55 -04:00
wrexec wrexec: Don't panic when process state is nil (#57610) 2023-10-16 23:40:16 +02:00
buf.yaml
BUILD.bazel