forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-embeddings.ts
More file actions
47 lines (38 loc) · 1.09 KB
/
create-embeddings.ts
File metadata and controls
47 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { UpstreamTrace } from "~/lib/upstream-trace"
import { copilotHeaders, copilotBaseUrl } from "~/lib/api-config"
import { HTTPError } from "~/lib/error"
import { state } from "~/lib/state"
export const createEmbeddings = async (
payload: EmbeddingRequest,
trace?: UpstreamTrace,
) => {
if (!state.copilotToken) throw new Error("Copilot token not found")
const response = await fetch(`${copilotBaseUrl(state)}/embeddings`, {
method: "POST",
headers: copilotHeaders(state, false, trace?.requestId),
body: JSON.stringify(payload),
})
if (trace) {
trace.upstreamStatus = response.status
}
if (!response.ok) throw new HTTPError("Failed to create embeddings", response)
return (await response.json()) as EmbeddingResponse
}
export interface EmbeddingRequest {
input: string | Array<string>
model: string
}
export interface Embedding {
object: string
embedding: Array<number>
index: number
}
export interface EmbeddingResponse {
object: string
data: Array<Embedding>
model: string
usage: {
prompt_tokens: number
total_tokens: number
}
}