MCPcopy
hub / github.com/continuedev/continue / CohereApi

Class CohereApi

packages/openai-adapters/src/apis/Cohere.ts:26–212  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24} from "./base.js";
25
26export class CohereApi implements BaseLlmApi {
27 apiBase: string = "https://api.cohere.com/v1";
28
29 static maxStopSequences = 5;
30
31 constructor(protected config: CohereConfig) {
32 this.apiBase = config.apiBase ?? this.apiBase;
33 }
34
35 private _convertMessages(
36 msgs: OpenAI.Chat.Completions.ChatCompletionMessageParam[],
37 ): any[] {
38 return msgs.map((m) => ({
39 role: m.role === "assistant" ? "CHATBOT" : "USER",
40 message: m.content,
41 }));
42 }
43
44 private _convertBody(oaiBody: ChatCompletionCreateParams) {
45 return {
46 message: oaiBody.messages.pop()?.content,
47 chat_history: this._convertMessages(
48 oaiBody.messages.filter((msg) => msg.role !== "system"),
49 ),
50 preamble: oaiBody.messages.find((msg) => msg.role === "system")?.content,
51 model: oaiBody.model,
52 stream: oaiBody.stream,
53 temperature: oaiBody.temperature,
54 max_tokens: oaiBody.max_tokens,
55 p: oaiBody.top_p,
56 stop_sequences: oaiBody.stop?.slice(0, CohereApi.maxStopSequences),
57 frequency_penalty: oaiBody.frequency_penalty,
58 presence_penalty: oaiBody.presence_penalty,
59 };
60 }
61
62 async chatCompletionNonStream(
63 body: ChatCompletionCreateParamsNonStreaming,
64 signal: AbortSignal,
65 ): Promise<ChatCompletion> {
66 const headers = {
67 "Content-Type": "application/json",
68 Authorization: `Bearer ${this.config.apiKey}`,
69 };
70
71 const resp = await customFetch(this.config.requestOptions)(
72 new URL("chat", this.apiBase),
73 {
74 method: "POST",
75 headers,
76 body: JSON.stringify(this._convertBody(body)),
77 signal,
78 },
79 );
80
81 if (resp.status === 499) {
82 return EMPTY_CHAT_COMPLETION;
83 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected