sourcegraph/dev
Ólafur Páll Geirsson 1b1229c867
feat/API: implement /models and /models/{modelId} using TypeSpec (#64421)
Fixes CODY-3085
Fixes CODY-3086

Previously, there was no way for OpenAI clients to list the available
models on Sourcegraph or query metadata about a given model ID ("model
ref" using our internal terminology). This PR fixes that problem AND
additionally adds infrastructure to auto-generate Go models from a
TypeSpec specification.

[TypeSpec](https://typespec.io/) is an IDL to document REST APIs,
created by Microsoft. Historically, the Go code in this repository has
been the single source of truth about what exact JSON structures are
expected in HTTP request/response pairs in our REST endpoints. This new
TypeSpec infrastructure allows us to document these shapes at a higher
abstraction level, which has several benefits including automatic
OpenAPI generation, which we can use to generate docs on
sourcegraph.com/docs or automatically generate client bindings in
TypeScript (among many other use-cases).

I am planning to write an RFC to propose we start using TypeSpec for new
REST endpoints going forward. If the RFC is not approved then we can
just delete the new `tools/typespec_codegen` directory and keep the
generated code in the repo. It won't be a big difference in the end
compared our current manual approach of writing Go structs for HTTP
APIs.

<!-- PR description tips:
https://www.notion.so/sourcegraph/Write-a-good-pull-request-description-610a7fd3e613496eb76f450db5a49b6e
-->

## Test plan
See test cases. I additionally wrote a basic python script with the
official OpenAI client to test that it works with this endpoint. First,
I ran `sg start minimal`. Then I wrote this script
```py
import os
from openai import OpenAI
from dotenv import load_dotenv
import httpx

load_dotenv()

openai = OpenAI(
    # base_url="https://api.openai.com/v1",
    # api_key=os.getenv("OPENAI_API_KEY"),
    base_url="https://sourcegraph.test:3443/api/v1",
    api_key=os.getenv("SRC_ACCESS_TOKEN"),
    http_client=httpx.Client(verify=False)
)

def main():
    response = openai.models.list()
    for model in response.data:
        print(model.id)
if __name__ == "__main__":
    main()

```
Finally, I ran 
```
❯ python3 models.py
anthropic::unknown::claude-3-haiku-20240307
anthropic::unknown::claude-3-sonnet-20240229
fireworks::unknown::starcoder
```
<!-- REQUIRED; info at
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles
-->

## Changelog

* New `GET /.api/llm/models` and `GET /.api/llm/models/{modelId}` REST
API endpoints to list available LLM models on the instance and to get
information about a given model. This endpoints is compatible with the
`/models` and `/models/{modelId}` endpoints from OpenAI.

<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
2024-08-14 10:47:00 +00:00
..
auth-provider remove more ENTERPRISE=1 remnants (#57232) 2023-10-02 10:43:11 -07:00
authtest chore: Remove client construction from SignUp/In funcs (#62789) 2024-05-21 15:18:58 +02:00
backcompat migrations: use COMMIT AND CHAIN for tenant_id (#64431) 2024-08-13 11:59:59 +00:00
bkstats fix(build-tracker): Failed back-compat doesn't count towards branch-locking quota (#63911) 2024-07-25 06:45:09 -05:00
build-tracker Revert "chore(ci): rework build-tracker to use redis instead of in-memory store of build results" (#64436) 2024-08-13 13:22:41 +02:00
buildchecker fix(build-tracker): Failed back-compat doesn't count towards branch-locking quota (#63911) 2024-07-25 06:45:09 -05:00
check Update the outdated link to #dev-experience to #discuss-dev-infra (#64018) 2024-07-25 06:58:22 -05:00
ci ci: set go mod tidy step timeout (#64449) 2024-08-13 17:45:30 +02:00
clustering Dev tool: python script for text clustering based on local embeddings (#58691) 2023-12-04 09:27:14 -05:00
codehost_testing bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
codeintel-qa chore: Remove client construction from SignUp/In funcs (#62789) 2024-05-21 15:18:58 +02:00
db scripts: dont run subshell in for loop (#60611) 2024-02-19 14:48:33 +00:00
depgraph depgraph: Fix spurious error when no subcommand name is passed (#61871) 2024-04-15 16:10:44 +02:00
deployment-lag-notifier bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
deployment-notifier fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
gitserverintegration gitserver: Framework to support integration testing against gitserver (#62801) 2024-06-07 17:01:12 +02:00
go-mockgen-gazelle bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
go-mockgen-transformer bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
gqltest Enable p4-fusion by default for Perforce code host connections (#64101) 2024-08-02 11:15:16 +02:00
grafana chore: remove unused dev/sg/internal/loki (#61923) 2024-04-16 12:19:32 +00:00
insight-data-gen bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
internal/cmd feat(search): remove smart search logic (#64215) 2024-08-01 18:02:35 +03:00
linearhooks Revert "Revert "refactor: upgrade to rules_oci 2.0 (2nd attempt)"" (#64354) 2024-08-08 09:00:08 +00:00
linters linters: update go-critic to latest (#64419) 2024-08-12 21:30:49 +02:00
managedservicesplatform sg/msp: enable alerting by default for production projects (#63912) 2024-07-18 20:57:38 +01:00
nix fix(nix/p4-fusion): update p4-fusion helix-api hashes (#63805) 2024-07-15 10:20:33 +02:00
perforce bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
phabricator add shellcheck & shfmt to pre-commit's pre-push hook (#59485) 2024-01-11 14:16:22 +00:00
pr-auditor Removed pr-auditor and left redirect notice (#59497) 2024-01-10 15:15:24 -08:00
prometheus telemetry-gateway: migrate to MSP runtime (#58814) 2023-12-08 12:14:34 -08:00
scaletesting bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
sg fix(sg): make sg gen output more readable (#64406) 2024-08-13 06:22:21 -05:00
src-expose chore: Change errors.HasType to respect multi-errors (#63024) 2024-06-06 13:02:14 +00:00
team bazel: native go-mockgen in Bazel (#60386) 2024-02-16 13:26:48 +00:00
tilt
tools chore(ci): instrument push_all.sh commands in honeycomb (#63350) 2024-06-19 18:16:21 +01:00
update-embeddings-list reapply "switch from jest to vitest for faster, simpler tests (#57886)" (#58145) 2023-11-07 12:00:18 +02:00
zoekt gomod: update zoekt to fix several CVEs in dependencies (#60093) 2024-02-02 15:36:04 +00:00
.gitignore
add_https_domain_to_hosts.sh
bazel_buildkite_stamp_vars.sh bazel: test volatile variables with Percy (#51693) 2023-05-11 09:20:42 +00:00
bazel_stamp_vars.sh Reapply "bazel: migrate dind dockerfile to rules_oci" (#61790) (#61824) 2024-04-12 15:52:44 +00:00
BUILD.bazel Revert "chore(ci): rework build-tracker to use redis instead of in-memory store of build results" (#64436) 2024-08-13 13:22:41 +02:00
caddy.sh fix(sg): start Caddy admin server before caddy trust (#58928) 2023-12-20 10:03:45 +02:00
Caddyfile Add an option for disabling HTTP/2 when communicating with Cody Gateway (#59668) 2024-01-17 15:36:40 +01:00
Caddyfile-sveltekit refactor(svelte): Improve integration with sg (#64333) 2024-08-08 11:39:26 +02:00
check-test-ownership.sh feat(ci): add GHA to report when Bazel test ownership drops below 70% threshold (#62985) 2024-05-31 14:46:01 +01:00
check-tokens.sh chore(local): add FORBIDCOMMIT pragma to prevent accidental commits (#63581) 2024-07-01 18:27:26 +00:00
CLA.txt
codeinsights-db.sh
comby-install-or-upgrade.sh add shellcheck & shfmt to pre-commit's pre-push hook (#59485) 2024-01-11 14:16:22 +00:00
defs.bzl Removed unused bazel macro arg (#60996) 2024-03-11 06:36:15 -07:00
dev-sourcegraph-server.sh
docsite.sh [docsite]: bump version (#56013) 2023-08-18 12:40:44 -06:00
drop-entire-local-database-and-redis.sh
esbuild.bzl chore(bazel): enable rules_esbuild sandbox with object-inspect workaround (#61969) 2024-06-05 15:34:29 +01:00
eslint-report-test.sh Revert "revert "bazel: improve ESLint rule" (#52853)" (#52878) 2023-06-04 19:06:39 -07:00
eslint.bzl chore(bazel): fixup custom eslint test rule after bump to rules_js 2 (#63143) 2024-06-06 23:32:34 +01:00
export-http-recording-tokens.sh feat/API: add publicly available /api/v1/chat/completions REST endpoint (#64239) 2024-08-09 09:36:27 +00:00
forbidcommit.sh chore(local): add FORBIDCOMMIT pragma to prevent accidental commits (#63581) 2024-07-01 18:27:26 +00:00
foreach-non-web-client-project.sh release: drop legacy release tooling (#61220) 2024-04-09 14:29:35 -05:00
generate-openapi-models.sh feat/API: implement /models and /models/{modelId} using TypeSpec (#64421) 2024-08-14 10:47:00 +00:00
git-stats
global-settings.json Chore: remove search console (#63322) 2024-06-19 11:05:03 -06:00
go_defs.bzl build: add buildifier check to Aspect Workflows (#58566) 2023-11-27 14:58:01 +02:00
go_mockgen_rule.bzl bump to Go 1.22.1 (#60902) 2024-03-06 17:38:43 -07:00
go_mockgen.bzl bazel: native go-mockgen in Bazel (#60386) 2024-02-16 13:26:48 +00:00
go_stringer.bzl build: upgrade to Bazel 7 pre-release (#57970) 2023-10-31 15:29:14 +02:00
go-mod-update.sh
licenses.sh fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
migrations.bzl bazel: add utility macro for wrapping single-file tools (#62930) 2024-05-27 16:53:51 +00:00
mocha.bzl chore(ci): remove Percy visual tests (#63515) 2024-06-27 16:20:06 +02:00
msp_delivery.bzl msp_delivery: infer pipeline id from service_id and region (#61395) 2024-03-26 15:30:27 +00:00
oci_defs.bzl Revert "Revert "refactor: upgrade to rules_oci 2.0 (2nd attempt)"" (#64354) 2024-08-08 09:00:08 +00:00
oci_deps.bzl Update wolfi hashes (#64289) 2024-08-06 15:54:02 +02:00
p4-fusion-dev bazel: provide p4-fusion via bazel (#58893) 2023-12-11 16:06:06 +00:00
proto.bzl fix: gazelle and proto generation (continuation of #49547) (#49713) 2023-04-11 11:36:45 +02:00
prune-pick.sh
redis-postgres.yml fix: update links for dev docs (#62758) 2024-05-17 13:47:34 +02:00
redis.conf
remote_cache_local_env.sh chore(sg): sg bazel uses remote cache by default (unless in CI) (#62245) 2024-04-29 18:56:18 +02:00
run-server-image.sh Remove HTTP for inter-service RPC (#59093) 2024-01-11 19:46:32 +01:00
sass.bzl Buildifier fixes (#48691) 2023-03-07 10:15:26 +01:00
schema_migrations.bzl chore(rel): bump minor for stitch graph + add support invalidating migrations repo rule (#62511) 2024-05-07 22:04:59 +00:00
scip-ctags-dev local: fix scip-ctags install script (#57252) 2023-10-02 14:58:14 +00:00
scip-ctags-install.sh syntax-highlighter: initial ctags work (w/ musl scip-ctags) (#52643) 2023-05-30 17:19:39 -04:00
scip-syntax-dev highlighter: Rename scip-treesitter-cli -> scip-syntax (#60077) 2024-02-05 13:15:09 +01:00
scip-syntax-install.sh Syntactic indexing: add database schema and dbworker (#60055) 2024-02-08 18:00:21 +00:00
site-config.json Remove broken federation feature (#55161) 2023-07-20 17:30:32 +00:00
src-prof-services.json Add embeddings to server behind env var (#50288) 2023-04-04 16:45:50 +02:00
src-search-meta.sh
tool_deps.bzl chore(security) : upgrade src-cli version to address CVE (#63750) 2024-07-10 09:57:16 -04:00
tools.go dev/linearhooks: add POC (#62367) 2024-05-07 00:14:05 -07:00
universal-ctags-dev bzl: do not show full commmand line for ctags in dev (#58261) 2023-11-10 10:05:25 -05:00
write_generated_to_source_files.bzl bazel: native go-mockgen in Bazel (#60386) 2024-02-16 13:26:48 +00:00