Home / Market / go-cerebras

go-cerebras

Writes Go code with go-cerebras (package cerebras, github.com/psyb0t/go-cerebras), an unofficial client for the Cerebras Inference API generated with oapi-codegen. `cerebras.NewClientWithResponses("https://api.cerebras.ai", cerebras.WithRequestEditorFn(...))` gives typed methods for every path of the spec, including chat completions (`ChatV1ChatCompletionsPost`), text completions (`CompletionsV1CompletionsPost`), model listing and lookup, and TCP warming. Union schemas come with `As...`/`From...

skill★ 00 dlclawhubMIT-0Install in CosmosUpstream Source

Documentation & Preview

go-cerebras: setup and reference

Requirements

Install

go get github.com/psyb0t/go-cerebras

The package name is cerebras:

import "github.com/psyb0t/go-cerebras"

Auth and base URL

The spec's security scheme is HTTP Bearer. Send the API key as Authorization: Bearer <key> through a request editor:

auth := cerebras.WithRequestEditorFn(func(_ context.Context, req *http.Request) error {
	req.Header.Set("Authorization", "Bearer "+os.Getenv("CEREBRAS_API_KEY"))

	return nil
})

client, err := cerebras.NewClientWithResponses("https://api.cerebras.ai", auth)

The spec lists no server, so the base URL is always passed in. WithHTTPClient swaps the *http.Client, for timeouts or a custom transport.

Methods

| Path | Method on ClientWithResponses | Auth | |---|---|---| | POST /v1/chat/completions | ChatV1ChatCompletionsPostWithResponse | Bearer | | POST /v1/completions | CompletionsV1CompletionsPostWithResponse | Bearer | | GET /v1/models | ListModelsV1ModelsGetWithResponse | Bearer | | GET /v1/models/{model_id} | GetModelV1ModelsModelIdGetWithResponse | Bearer | | GET /public/v1/models | ListPublicModelsEndpointPublicV1ModelsGetWithResponse | none | | GET /public/v1/models/{model_id} | GetPublicModelPublicV1ModelsModelIdGetWithResponse | none | | GET /v1/tcp_warming | TcpWarmingV1TcpWarmingGetWithResponse | none |

Each POST also has a ...WithBodyWithResponse(ctx, params, contentType, io.Reader) form for a raw body. The plain Client has the same methods returning *http.Response, which is the way to read a streamed body.

Most methods take a params struct with optional headers from the spec (X-Amz-Cf-Id, CF-RAY, X-delay-time). Pass an empty struct when you don't need them.

Unions

Several schemas are anyOf unions. oapi-codegen makes each one a struct with From<Variant>, As<Variant> and Merge<Variant> methods:

Responses and errors

Number fields

Every plain number in the spec is float64.