sourcegraph/client
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
..
branded Svelte: Add telemetry v2 to svelte client (#63041) 2024-06-04 11:12:32 +02:00
browser v2t: add v2 telemetry to the client/shared folder (#62586) 2024-06-03 16:34:28 -07:00
build-config web: Update rxjs to v7 (#61122) 2024-03-18 14:02:57 +01:00
client-api v2t: add v2 telemetry to the client/shared folder (#62586) 2024-06-03 16:34:28 -07:00
codeintellify Migrate deprecated rxjs functions/methods (#61222) 2024-04-08 11:23:34 +02:00
cody-context-filters-test-dataset Create a shared Cody Ignore dataset (#61968) 2024-05-09 13:18:35 +00:00
cody-shared Rename smartContext to smartContextWindow (#62948) 2024-05-28 10:00:34 -07:00
cody-ui Cody web: Bring back old packages from git history (#61376) 2024-04-08 14:21:41 +02:00
common feat: popover to configure editor (#62452) 2024-05-23 16:36:58 +02:00
eslint-plugin-wildcard chore: upgrade to Aspect CLI 5.8.5 (#57961) 2023-10-30 17:01:58 +02:00
extension-api Docs: update links to point to new site (#60381) 2024-02-13 00:23:47 +00:00
extension-api-types use @typescript-eslint projectService for faster eslint (#57851) 2023-10-24 01:40:40 +00:00
http-client reapply "switch from jest to vitest for faster, simpler tests (#57886)" (#58145) 2023-11-07 12:00:18 +02:00
jetbrains bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
observability-client reapply "switch from jest to vitest for faster, simpler tests (#57886)" (#58145) 2023-11-07 12:00:18 +02:00
observability-server reapply "switch from jest to vitest for faster, simpler tests (#57886)" (#58145) 2023-11-07 12:00:18 +02:00
shared Svelte: Add telemetry v2 to svelte client (#63041) 2024-06-04 11:12:32 +02:00
storybook fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
template-parser reapply "switch from jest to vitest for faster, simpler tests (#57886)" (#58145) 2023-11-07 12:00:18 +02:00
testing code intel: Don't rely on URL polyfill to correctly parse git: URIs (#58258) 2023-11-17 19:32:46 +01:00
vscode v2t: add v2t to client/branded (#62364) 2024-05-08 10:25:09 -07:00
web Cody Gateway: Add Gemini models to PLG and Enterprise users (#63053) 2024-06-04 23:46:36 +00:00
web-sveltekit Svelte: fix editor button styling (#63080) 2024-06-04 12:49:40 -06:00
wildcard fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
BUILD.bazel Added ts_projects for storybook files in client/* (#59400) 2024-01-09 10:37:53 -08:00
README.md use esbuild for client/web builds (#57365) 2023-10-23 10:59:06 -07:00

Frontend packages

List

  • web: The web application deployed to http://sourcegraph.com/
  • browser: The Sourcegraph browser extension adds tooltips to code on different code hosts.
  • vscode: The Sourcegraph VS Code extension.
  • extension-api: The Sourcegraph extension API types for the Sourcegraph extensions. Published as sourcegraph.
  • extension-api-types: The Sourcegraph extension API types for client applications that embed Sourcegraph extensions and need to communicate with them. Published as @sourcegraph/extension-api-types.
  • sandboxes: All demos-mvp (minimum viable product) for the Sourcegraph web application.
  • shared: Contains common TypeScript/React/SCSS client code shared between the browser extension and the web app. Everything in this package is code-host agnostic.
  • branded: Contains React components and implements the visual design language we use across our web app and e.g. in the options menu of the browser extension. Over time, components from shared and branded packages should be moved into the wildcard package.
  • wildcard: Package that encapsulates storybook configuration and contains our Wildcard design system components. If we're using a component in two or more different areas (e.g. web-app and browser-extension) then it should live in the wildcard package. Otherwise the components should be better colocated with the code where they're actually used.
  • search: Search-related code that may be shared between all clients, both branded (e.g. web, VS Code extension) and unbranded (e.g. browser extension)
  • storybook: Storybook configuration.

Further migration plan

  1. Fix circular dependency in TS project-references graph wildcard package should not rely on web and probably shared, branded too. Ideally it should be an independent self-contained package.

  2. Decide on package naming and update existing package names. Especially it should be done for a shared package because we have multiple shared folders inside of other packages. It's hard to understand from where dependency is coming from and it's not possible to refactor import paths using find-and-replace.

  3. Investigate if we can painlessly switch to npm workspaces.

  4. Content of packages shared and branded should be moved to wildcard and refactored using the latest FE rules and conventions. Having different packages clearly communicates the migration plan. Developers first should look for components in the wildcard package and then fall-back to legacy packages if wildcard doesn't have the solution to their problem yet.

  5. shared contains utility functions, types, polyfills, etc which is not a part of the Wildcard component library. These modules should be moved into utils package and other new packages: e.g. api for GraphQL client and type generators, etc.

  6. Packages should use package name (e.g. @sourcegraph/wildcard) for imports instead of the relative paths (e.g. ../../../../wildcard/src/components/Markdown) to avoid long relative-paths and make dependency graph between packages clear. (Typescript will warn if packages have circular dependencies). It's easy to refactor such isolated packages, extract functionality into new ones, or even into new repositories.