@celljs/ai-openai 3.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/lib/common/api/api-protocol.d.ts +47 -0
- package/lib/common/api/api-protocol.d.ts.map +1 -0
- package/lib/common/api/api-protocol.js +6 -0
- package/lib/common/api/api-protocol.js.map +1 -0
- package/lib/common/api/chat-request.d.ts +198 -0
- package/lib/common/api/chat-request.d.ts.map +1 -0
- package/lib/common/api/chat-request.js +145 -0
- package/lib/common/api/chat-request.js.map +1 -0
- package/lib/common/api/chat-response.d.ts +193 -0
- package/lib/common/api/chat-response.d.ts.map +1 -0
- package/lib/common/api/chat-response.js +124 -0
- package/lib/common/api/chat-response.js.map +1 -0
- package/lib/common/api/embeddings-request.d.ts +59 -0
- package/lib/common/api/embeddings-request.d.ts.map +1 -0
- package/lib/common/api/embeddings-request.js +49 -0
- package/lib/common/api/embeddings-request.js.map +1 -0
- package/lib/common/api/embeddings-response.d.ts +27 -0
- package/lib/common/api/embeddings-response.d.ts.map +1 -0
- package/lib/common/api/embeddings-response.js +30 -0
- package/lib/common/api/embeddings-response.js.map +1 -0
- package/lib/common/api/index.d.ts +10 -0
- package/lib/common/api/index.d.ts.map +1 -0
- package/lib/common/api/index.js +13 -0
- package/lib/common/api/index.js.map +1 -0
- package/lib/common/api/message.d.ts +216 -0
- package/lib/common/api/message.d.ts.map +1 -0
- package/lib/common/api/message.js +179 -0
- package/lib/common/api/message.js.map +1 -0
- package/lib/common/api/openai-api.d.ts +18 -0
- package/lib/common/api/openai-api.d.ts.map +1 -0
- package/lib/common/api/openai-api.js +99 -0
- package/lib/common/api/openai-api.js.map +1 -0
- package/lib/common/api/openai-api.spec.d.ts +2 -0
- package/lib/common/api/openai-api.spec.d.ts.map +1 -0
- package/lib/common/api/openai-api.spec.js +162 -0
- package/lib/common/api/openai-api.spec.js.map +1 -0
- package/lib/common/api/openai-model.d.ts +117 -0
- package/lib/common/api/openai-model.d.ts.map +1 -0
- package/lib/common/api/openai-model.js +121 -0
- package/lib/common/api/openai-model.js.map +1 -0
- package/lib/common/api/response-format.d.ts +60 -0
- package/lib/common/api/response-format.d.ts.map +1 -0
- package/lib/common/api/response-format.js +79 -0
- package/lib/common/api/response-format.js.map +1 -0
- package/lib/common/api/usage.d.ts +79 -0
- package/lib/common/api/usage.d.ts.map +1 -0
- package/lib/common/api/usage.js +95 -0
- package/lib/common/api/usage.js.map +1 -0
- package/lib/common/index.d.ts +4 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +7 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/model/index.d.ts +5 -0
- package/lib/common/model/index.d.ts.map +1 -0
- package/lib/common/model/index.js +8 -0
- package/lib/common/model/index.js.map +1 -0
- package/lib/common/model/openai-chat-model.d.ts +37 -0
- package/lib/common/model/openai-chat-model.d.ts.map +1 -0
- package/lib/common/model/openai-chat-model.js +357 -0
- package/lib/common/model/openai-chat-model.js.map +1 -0
- package/lib/common/model/openai-chat-model.spec.d.ts +2 -0
- package/lib/common/model/openai-chat-model.spec.d.ts.map +1 -0
- package/lib/common/model/openai-chat-model.spec.js +82 -0
- package/lib/common/model/openai-chat-model.spec.js.map +1 -0
- package/lib/common/model/openai-chat-options.d.ts +159 -0
- package/lib/common/model/openai-chat-options.d.ts.map +1 -0
- package/lib/common/model/openai-chat-options.js +231 -0
- package/lib/common/model/openai-chat-options.js.map +1 -0
- package/lib/common/model/openai-embedding-model.d.ts +15 -0
- package/lib/common/model/openai-embedding-model.d.ts.map +1 -0
- package/lib/common/model/openai-embedding-model.js +64 -0
- package/lib/common/model/openai-embedding-model.js.map +1 -0
- package/lib/common/model/openai-embedding-model.spec.d.ts +2 -0
- package/lib/common/model/openai-embedding-model.spec.d.ts.map +1 -0
- package/lib/common/model/openai-embedding-model.spec.js +100 -0
- package/lib/common/model/openai-embedding-model.spec.js.map +1 -0
- package/lib/common/model/openai-embedding-options.d.ts +35 -0
- package/lib/common/model/openai-embedding-options.d.ts.map +1 -0
- package/lib/common/model/openai-embedding-options.js +56 -0
- package/lib/common/model/openai-embedding-options.js.map +1 -0
- package/lib/common/module.d.ts +4 -0
- package/lib/common/module.d.ts.map +1 -0
- package/lib/common/module.js +6 -0
- package/lib/common/module.js.map +1 -0
- package/lib/common/support/index.d.ts +3 -0
- package/lib/common/support/index.d.ts.map +1 -0
- package/lib/common/support/index.js +6 -0
- package/lib/common/support/index.js.map +1 -0
- package/lib/common/support/openai-api-response-headers.d.ts +34 -0
- package/lib/common/support/openai-api-response-headers.d.ts.map +1 -0
- package/lib/common/support/openai-api-response-headers.js +45 -0
- package/lib/common/support/openai-api-response-headers.js.map +1 -0
- package/lib/common/support/openai-response-header-extractor.d.ts +12 -0
- package/lib/common/support/openai-response-header-extractor.d.ts.map +1 -0
- package/lib/common/support/openai-response-header-extractor.js +131 -0
- package/lib/common/support/openai-response-header-extractor.js.map +1 -0
- package/lib/common/test/mock-openai-api.d.ts +13 -0
- package/lib/common/test/mock-openai-api.d.ts.map +1 -0
- package/lib/common/test/mock-openai-api.js +57 -0
- package/lib/common/test/mock-openai-api.js.map +1 -0
- package/lib/common/test/test-container.d.ts +3 -0
- package/lib/common/test/test-container.d.ts.map +1 -0
- package/lib/common/test/test-container.js +16 -0
- package/lib/common/test/test-container.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +4 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +46 -0
- package/src/common/api/api-protocol.ts +51 -0
- package/src/common/api/chat-request.ts +258 -0
- package/src/common/api/chat-response.ts +218 -0
- package/src/common/api/embeddings-request.ts +92 -0
- package/src/common/api/embeddings-response.ts +44 -0
- package/src/common/api/index.ts +9 -0
- package/src/common/api/message.ts +273 -0
- package/src/common/api/openai-api.spec.ts +175 -0
- package/src/common/api/openai-api.ts +111 -0
- package/src/common/api/openai-model.ts +137 -0
- package/src/common/api/response-format.ts +109 -0
- package/src/common/api/usage.ts +135 -0
- package/src/common/index.ts +3 -0
- package/src/common/model/index.ts +4 -0
- package/src/common/model/openai-chat-model.spec.ts +85 -0
- package/src/common/model/openai-chat-model.ts +399 -0
- package/src/common/model/openai-chat-options.ts +326 -0
- package/src/common/model/openai-embedding-model.spec.ts +112 -0
- package/src/common/model/openai-embedding-model.ts +83 -0
- package/src/common/model/openai-embedding-options.ts +72 -0
- package/src/common/module.ts +4 -0
- package/src/common/support/index.ts +2 -0
- package/src/common/support/openai-api-response-headers.ts +46 -0
- package/src/common/support/openai-response-header-extractor.ts +141 -0
- package/src/common/test/mock-openai-api.ts +70 -0
- package/src/common/test/test-container.ts +13 -0
- package/src/package.spec.ts +4 -0
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@celljs/ai-openai",
|
|
3
|
+
"version": "3.4.4",
|
|
4
|
+
"description": "OpenAI models support",
|
|
5
|
+
"main": "lib/common/index.js",
|
|
6
|
+
"typings": "lib/common/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@celljs/ai-core": "3.4.4",
|
|
9
|
+
"@celljs/core": "3.4.4",
|
|
10
|
+
"@celljs/http": "3.4.4",
|
|
11
|
+
"class-transformer": "^0.5.1",
|
|
12
|
+
"rxjs": "^6.6.0",
|
|
13
|
+
"tslib": "^2.8.0"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"cell-component"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/cellbang/cell/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/cellbang/cell",
|
|
30
|
+
"files": [
|
|
31
|
+
"lib",
|
|
32
|
+
"src",
|
|
33
|
+
"cell.yml"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"lint": "cell-component lint",
|
|
37
|
+
"build": "cell-component build",
|
|
38
|
+
"watch": "cell-component watch",
|
|
39
|
+
"clean": "cell-component clean",
|
|
40
|
+
"test": "cell-component test:js"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@celljs/component": "3.4.4"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "5d4a2384da3a67b3c16de24d4692bf754acfeda1"
|
|
46
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { ChatCompletionRequest } from './chat-request';
|
|
3
|
+
import { ChatCompletion, ChatCompletionChunk } from './chat-response';
|
|
4
|
+
import { EmbeddingResponse } from './embeddings-response';
|
|
5
|
+
import { EmbeddingRequest } from './embeddings-request';
|
|
6
|
+
import { ResponseEntity } from '@celljs/http';
|
|
7
|
+
|
|
8
|
+
export const OpenAIAPIOptions = Symbol('OpenAIAPIOptions');
|
|
9
|
+
export const OpenAIAPI = Symbol('OpenAIAPI');
|
|
10
|
+
|
|
11
|
+
export interface OpenAIAPIOptions {
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
apiKey: string;
|
|
14
|
+
completionsPath?: string;
|
|
15
|
+
embeddingsPath?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Interface representing the OpenAI API.
|
|
20
|
+
*/
|
|
21
|
+
export interface OpenAIAPI {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a model response for the given chat conversation.
|
|
24
|
+
* @param chatRequest - The chat completion request.
|
|
25
|
+
* @param additionalHttpHeader - Optional, additional HTTP headers to be added to the request.
|
|
26
|
+
* @returns A promise that resolves to the chat completion response.
|
|
27
|
+
*/
|
|
28
|
+
chat(chatRequest: ChatCompletionRequest, additionalHttpHeader?: Record<string, string>): Promise<ResponseEntity<ChatCompletion>>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates a streaming chat response for the given chat conversation.
|
|
32
|
+
* @param chatRequest - The chat completion request. Must have the stream property set to true.
|
|
33
|
+
* @param additionalHttpHeader - Optional, additional HTTP headers to be added to the request.
|
|
34
|
+
* @returns A promise that resolves to an observable of chat completion chunks.
|
|
35
|
+
*/
|
|
36
|
+
streamingChat(chatRequest: ChatCompletionRequest, additionalHttpHeader?: Record<string, string>): Promise<Observable<ResponseEntity<ChatCompletionChunk>>>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates an embedding vector representing the input text or token array.
|
|
40
|
+
* @param embeddingsRequest - The embedding request.
|
|
41
|
+
* @typeParam T - Type of the entity in the data list. Can be a string or list of tokens (e.g., integers).
|
|
42
|
+
* For embedding multiple inputs in a single request, you can pass a list of strings or a list of lists of tokens.
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* embed(List.of("text1", "text2", "text3"));
|
|
46
|
+
* embed(List.of(List.of(1, 2, 3), List.of(3, 4, 5)));
|
|
47
|
+
* ```
|
|
48
|
+
* @returns A promise that resolves to an embedding response containing a list of embeddings.
|
|
49
|
+
*/
|
|
50
|
+
embed<T>(embeddingsRequest: EmbeddingRequest<T>): Promise<ResponseEntity<EmbeddingResponse>>;
|
|
51
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { Expose, Type } from 'class-transformer';
|
|
2
|
+
import { ChatCompletionMessage } from './message';
|
|
3
|
+
import { ResponseFormat } from './response-format';
|
|
4
|
+
|
|
5
|
+
export class StreamOptions {
|
|
6
|
+
static INCLUDE_USAGE = new StreamOptions(true);
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
/**
|
|
10
|
+
* includeUsage If set, an additional chunk will be streamed
|
|
11
|
+
* before the data: [DONE] message. The usage field on this chunk
|
|
12
|
+
* shows the token usage statistics for the entire request, and
|
|
13
|
+
* the choices field will always be an empty array. All other chunks
|
|
14
|
+
* will also include a usage field, but with a null value.
|
|
15
|
+
*/
|
|
16
|
+
public includeUsage: boolean
|
|
17
|
+
) {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Function definition.
|
|
23
|
+
*/
|
|
24
|
+
export class Function {
|
|
25
|
+
description: string;
|
|
26
|
+
name: string;
|
|
27
|
+
parameters: Record<string, any>;
|
|
28
|
+
strict?: boolean;
|
|
29
|
+
|
|
30
|
+
static create(description: string, name: string, parameters: Record<string, any>, strict?: boolean): Function {
|
|
31
|
+
const func = new Function();
|
|
32
|
+
func.description = description;
|
|
33
|
+
func.name = name;
|
|
34
|
+
func.parameters = parameters;
|
|
35
|
+
func.strict = strict;
|
|
36
|
+
return func;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export enum ToolType {
|
|
41
|
+
/**
|
|
42
|
+
* Function tool type.
|
|
43
|
+
*/
|
|
44
|
+
FUNCTION = 'function'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class FunctionTool {
|
|
48
|
+
/**
|
|
49
|
+
* The type of the tool. Currently, only 'function' is supported.
|
|
50
|
+
*/
|
|
51
|
+
type: ToolType;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function definition.
|
|
55
|
+
*/
|
|
56
|
+
function: Function;
|
|
57
|
+
|
|
58
|
+
static create(func: Function): FunctionTool {
|
|
59
|
+
const tool = new FunctionTool();
|
|
60
|
+
tool.type = ToolType.FUNCTION;
|
|
61
|
+
tool.function = func;
|
|
62
|
+
return tool;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The type of modality for the model completion.
|
|
68
|
+
*/
|
|
69
|
+
export enum OutputModality {
|
|
70
|
+
AUDIO = 'audio',
|
|
71
|
+
TEXT = 'text',
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export enum Voice {
|
|
75
|
+
ALLOY = 'alloy',
|
|
76
|
+
ECHO = 'echo',
|
|
77
|
+
FABLE = 'fable',
|
|
78
|
+
ONYX = 'onyx',
|
|
79
|
+
NOVA = 'nova',
|
|
80
|
+
SHIMMER = 'shimmer'
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export enum AudioResponseFormat {
|
|
84
|
+
MP3 = 'mp3',
|
|
85
|
+
FLAC = 'flac',
|
|
86
|
+
OPUS = 'opus',
|
|
87
|
+
PCM16 = 'pcm16',
|
|
88
|
+
WAV = 'wav'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class AudioParameters {
|
|
92
|
+
/**
|
|
93
|
+
* Specifies the voice type.
|
|
94
|
+
*/
|
|
95
|
+
voice?: Voice;
|
|
96
|
+
/**
|
|
97
|
+
* Specifies the output audio format.
|
|
98
|
+
*/
|
|
99
|
+
format?: AudioResponseFormat;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates a model response for the given chat conversation.
|
|
104
|
+
*/
|
|
105
|
+
export class ChatCompletionRequest {
|
|
106
|
+
/**
|
|
107
|
+
* A list of messages comprising the conversation so far.
|
|
108
|
+
*/
|
|
109
|
+
messages: ChatCompletionMessage[];
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* ID of the model to use.
|
|
113
|
+
*/
|
|
114
|
+
model: string;
|
|
115
|
+
/**
|
|
116
|
+
* Whether to store the output of this chat completion request for use in OpenAI's model distillation or evals products.
|
|
117
|
+
*/
|
|
118
|
+
store?: boolean;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Developer-defined tags and values used for filtering completions in the OpenAI's dashboard.
|
|
122
|
+
*/
|
|
123
|
+
metadata?: Record<string, any>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far,
|
|
127
|
+
* decreasing the model's likelihood to repeat the same line verbatim.
|
|
128
|
+
*/
|
|
129
|
+
@Expose({ name: 'frequency_penalty' })
|
|
130
|
+
frequencyPenalty?: number;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
|
134
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100.
|
|
135
|
+
*/
|
|
136
|
+
@Expose({ name: 'logit_bias' })
|
|
137
|
+
logitBias?: Record<string, number>;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Whether to return log probabilities of the output tokens or not.
|
|
141
|
+
*/
|
|
142
|
+
logprobs?: boolean;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* An integer between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability.
|
|
146
|
+
*/
|
|
147
|
+
@Expose({ name: 'top_logprobs' })
|
|
148
|
+
topLogprobs?: number;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @deprecated The maximum number of tokens that can be generated in the chat completion. This value can be used to control costs for text generated via API.
|
|
152
|
+
*/
|
|
153
|
+
maxTokens?: number;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.
|
|
157
|
+
*/
|
|
158
|
+
@Expose({ name: 'max_completion_tokens' })
|
|
159
|
+
maxCompletionTokens?: number;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* How many chat completion choices to generate for each input message.
|
|
163
|
+
*/
|
|
164
|
+
n?: number;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Output types that you would like the model to generate for this request.
|
|
168
|
+
*/
|
|
169
|
+
@Expose({ name: 'output_modalities' })
|
|
170
|
+
outputModalities?: OutputModality[];
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Parameters for audio output. Required when audio output is requested with outputModalities: ["audio"].
|
|
174
|
+
*/
|
|
175
|
+
@Expose({ name: 'audio_parameters' })
|
|
176
|
+
@Type(() => AudioParameters)
|
|
177
|
+
audioParameters?: AudioParameters;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far,
|
|
181
|
+
* increasing the model's likelihood to talk about new topics.
|
|
182
|
+
*/
|
|
183
|
+
@Expose({ name: 'presence_penalty' })
|
|
184
|
+
presencePenalty?: number;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* An object specifying the format that the model must output.
|
|
188
|
+
*/
|
|
189
|
+
@Expose({ name: 'response_format' })
|
|
190
|
+
@Type(() => ResponseFormat)
|
|
191
|
+
responseFormat?: ResponseFormat;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* If specified, our system will make a best effort to sample deterministically,
|
|
195
|
+
* such that repeated requests with the same seed and parameters should return the same result.
|
|
196
|
+
*/
|
|
197
|
+
seed?: number;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Specifies the latency tier to use for processing the request.
|
|
201
|
+
*/
|
|
202
|
+
@Expose({ name: 'service_tier' })
|
|
203
|
+
serviceTier?: string;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Up to 4 sequences where the API will stop generating further tokens.
|
|
207
|
+
*/
|
|
208
|
+
stop?: string[];
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Options for streaming response.
|
|
212
|
+
*/
|
|
213
|
+
stream?: boolean;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The format to return the response in. Currently, the only accepted value is "json".
|
|
217
|
+
*/
|
|
218
|
+
@Expose({ name: 'stream_options' })
|
|
219
|
+
@Type(() => StreamOptions)
|
|
220
|
+
streamOptions?: StreamOptions;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* What sampling temperature to use, between 0 and 1. Higher values like 0.8 will make the output more random,
|
|
224
|
+
* while lower values like 0.2 will make it more focused and deterministic.
|
|
225
|
+
*/
|
|
226
|
+
temperature?: number;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* An alternative to sampling with temperature, called nucleus sampling,
|
|
230
|
+
* where the model considers the results of the tokens with top_p probability mass.
|
|
231
|
+
*/
|
|
232
|
+
@Expose({ name: 'top_p' })
|
|
233
|
+
topP?: number;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A list of tools the model may call. Currently, only functions are supported as a tool.
|
|
237
|
+
*/
|
|
238
|
+
@Type(() => FunctionTool)
|
|
239
|
+
tools?: FunctionTool[];
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Controls which (if any) function is called by the model.
|
|
243
|
+
*/
|
|
244
|
+
@Expose({ name: 'tool_choice' })
|
|
245
|
+
toolChoice?: Record<string, any>;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* If set to true, the model will call all functions in the tools list in parallel. Otherwise,
|
|
249
|
+
* the model will call the functions in the tools list in the order they are provided.
|
|
250
|
+
*/
|
|
251
|
+
@Expose({ name: 'parallel_tool_calls' })
|
|
252
|
+
parallelToolCalls?: boolean;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
|
|
256
|
+
*/
|
|
257
|
+
user?: string;
|
|
258
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { Expose, Type } from 'class-transformer';
|
|
2
|
+
import { ChatCompletionFinishReason, ChatCompletionMessage } from './message';
|
|
3
|
+
import { Usage } from './usage';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The most likely tokens and their log probability, at this token position.
|
|
7
|
+
*/
|
|
8
|
+
export class TopLogProbs {
|
|
9
|
+
/**
|
|
10
|
+
* The token.
|
|
11
|
+
*/
|
|
12
|
+
token: string;
|
|
13
|
+
/**
|
|
14
|
+
* The log probability of the token.
|
|
15
|
+
*/
|
|
16
|
+
logprob: number;
|
|
17
|
+
/**
|
|
18
|
+
* A list of integers representing the UTF-8 bytes
|
|
19
|
+
* representation of the token. Useful in instances where characters are represented by multiple
|
|
20
|
+
* tokens and their byte representations must be combined to generate the correct
|
|
21
|
+
* text representation. Can be null if there is no bytes representation for the
|
|
22
|
+
* token.
|
|
23
|
+
*/
|
|
24
|
+
@Expose({ name: 'bytes' })
|
|
25
|
+
probBytes: number[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Message content tokens with log probability information.
|
|
30
|
+
*/
|
|
31
|
+
export class Content {
|
|
32
|
+
/**
|
|
33
|
+
* The token.
|
|
34
|
+
*/
|
|
35
|
+
token: string;
|
|
36
|
+
/**
|
|
37
|
+
* The log probability of the token.
|
|
38
|
+
*/
|
|
39
|
+
logprob: number;
|
|
40
|
+
/**
|
|
41
|
+
* A list of integers representing the UTF-8 bytes representation
|
|
42
|
+
* of the token. Useful in instances where characters are represented by multiple
|
|
43
|
+
* tokens and their byte representations must be combined to generate the correct
|
|
44
|
+
* text representation. Can be null if there is no bytes representation for the
|
|
45
|
+
* token.
|
|
46
|
+
*/
|
|
47
|
+
@Expose({ name: 'bytes' })
|
|
48
|
+
probBytes: number[];
|
|
49
|
+
/**
|
|
50
|
+
* List of the most likely tokens and their log probability, at
|
|
51
|
+
* this token position. In rare cases, there may be fewer than the number of
|
|
52
|
+
* requested top_logprobs returned.
|
|
53
|
+
*/
|
|
54
|
+
@Expose({ name: 'top_logprobs' })
|
|
55
|
+
topLogprobs: TopLogProbs[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Log probability information for the choice.
|
|
60
|
+
*/
|
|
61
|
+
export class LogProbs {
|
|
62
|
+
/**
|
|
63
|
+
* Message content tokens with log probability information.
|
|
64
|
+
*/
|
|
65
|
+
@Type(() => Content)
|
|
66
|
+
content: Content[];
|
|
67
|
+
/**
|
|
68
|
+
* Message refusal tokens with log probability information.
|
|
69
|
+
*/
|
|
70
|
+
@Type(() => Content)
|
|
71
|
+
refusal: Content[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Chat completion choice.
|
|
76
|
+
*/
|
|
77
|
+
export class Choice {
|
|
78
|
+
/**
|
|
79
|
+
* The reason the model stopped generating tokens.
|
|
80
|
+
*/
|
|
81
|
+
@Expose({ name: 'finish_reason' })
|
|
82
|
+
finishReason: ChatCompletionFinishReason;
|
|
83
|
+
/**
|
|
84
|
+
* The index of the choice in the list of choices.
|
|
85
|
+
*/
|
|
86
|
+
index: number;
|
|
87
|
+
/**
|
|
88
|
+
* A chat completion message generated by the model.
|
|
89
|
+
*/
|
|
90
|
+
@Type(() => ChatCompletionMessage)
|
|
91
|
+
message: ChatCompletionMessage;
|
|
92
|
+
/**
|
|
93
|
+
* Log probability information for the choice.
|
|
94
|
+
*/
|
|
95
|
+
logprobs: LogProbs;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Represents a chat completion response returned by model, based on the provided
|
|
100
|
+
* input.
|
|
101
|
+
*/
|
|
102
|
+
export class ChatCompletion {
|
|
103
|
+
/**
|
|
104
|
+
* A unique identifier for the chat completion.
|
|
105
|
+
*/
|
|
106
|
+
id: string;
|
|
107
|
+
/**
|
|
108
|
+
* A list of chat completion choices. Can be more than one if n is
|
|
109
|
+
* greater than 1.
|
|
110
|
+
*/
|
|
111
|
+
@Type(() => Choice)
|
|
112
|
+
choices: Choice[];
|
|
113
|
+
/**
|
|
114
|
+
* The Unix timestamp (in seconds) of when the chat completion was
|
|
115
|
+
* created.
|
|
116
|
+
*/
|
|
117
|
+
created: number;
|
|
118
|
+
/**
|
|
119
|
+
* The model used for the chat completion.
|
|
120
|
+
*/
|
|
121
|
+
model: string;
|
|
122
|
+
/**
|
|
123
|
+
* The service tier used for processing the request. This field is
|
|
124
|
+
* only included if the service_tier parameter is specified in the request.
|
|
125
|
+
*/
|
|
126
|
+
@Expose({ name: 'service_tier' })
|
|
127
|
+
serviceTier: string;
|
|
128
|
+
/**
|
|
129
|
+
* This fingerprint represents the backend configuration that
|
|
130
|
+
* the model runs with. Can be used in conjunction with the seed request parameter to
|
|
131
|
+
* understand when backend changes have been made that might impact determinism.
|
|
132
|
+
*/
|
|
133
|
+
@Expose({ name: 'system_fingerprint' })
|
|
134
|
+
systemFingerprint: string;
|
|
135
|
+
/**
|
|
136
|
+
* The object type, which is always chat.completion.
|
|
137
|
+
*/
|
|
138
|
+
object: string;
|
|
139
|
+
/**
|
|
140
|
+
* Usage statistics for the completion request.
|
|
141
|
+
*/
|
|
142
|
+
@Type(() => Usage)
|
|
143
|
+
usage: Usage;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Chat completion choice.
|
|
148
|
+
*/
|
|
149
|
+
export class ChunkChoice {
|
|
150
|
+
/**
|
|
151
|
+
* The reason the model stopped generating tokens.
|
|
152
|
+
*/
|
|
153
|
+
@Expose({ name: 'finish_reason' })
|
|
154
|
+
finishReason: ChatCompletionFinishReason;
|
|
155
|
+
/**
|
|
156
|
+
* The index of the choice in the list of choices.
|
|
157
|
+
*/
|
|
158
|
+
index: number;
|
|
159
|
+
/**
|
|
160
|
+
* A chat completion delta generated by streamed model responses.
|
|
161
|
+
*/
|
|
162
|
+
@Type(() => ChatCompletionMessage)
|
|
163
|
+
delta: ChatCompletionMessage;
|
|
164
|
+
/**
|
|
165
|
+
* Log probability information for the choice.
|
|
166
|
+
*/
|
|
167
|
+
@Type(() => LogProbs)
|
|
168
|
+
logprobs: LogProbs;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Represents a streamed chunk of a chat completion response returned by model, based
|
|
173
|
+
* on the provided input.
|
|
174
|
+
*/
|
|
175
|
+
export class ChatCompletionChunk {
|
|
176
|
+
/**
|
|
177
|
+
* A unique identifier for the chat completion. Each chunk has the same ID.
|
|
178
|
+
*/
|
|
179
|
+
id: string;
|
|
180
|
+
/**
|
|
181
|
+
* A list of chat completion choices. Can be more than one if n is
|
|
182
|
+
* greater than 1.
|
|
183
|
+
*/
|
|
184
|
+
@Type(() => ChunkChoice)
|
|
185
|
+
choices: ChunkChoice[];
|
|
186
|
+
/**
|
|
187
|
+
* The Unix timestamp (in seconds) of when the chat completion was
|
|
188
|
+
* created. Each chunk has the same timestamp.
|
|
189
|
+
*/
|
|
190
|
+
created: number;
|
|
191
|
+
/**
|
|
192
|
+
* The model used for the chat completion.
|
|
193
|
+
*/
|
|
194
|
+
model: string;
|
|
195
|
+
/**
|
|
196
|
+
* The service tier used for processing the request. This field is
|
|
197
|
+
* only included if the service_tier parameter is specified in the request.
|
|
198
|
+
*/
|
|
199
|
+
@Expose({ name: 'service_tier' })
|
|
200
|
+
serviceTier: string;
|
|
201
|
+
/**
|
|
202
|
+
* This fingerprint represents the backend configuration that
|
|
203
|
+
* the model runs with. Can be used in conjunction with the seed request parameter to
|
|
204
|
+
* understand when backend changes have been made that might impact determinism.
|
|
205
|
+
*/
|
|
206
|
+
@Expose({ name: 'system_fingerprint' })
|
|
207
|
+
systemFingerprint: string;
|
|
208
|
+
/**
|
|
209
|
+
* The object type, which is always 'chat.completion.chunk'.
|
|
210
|
+
*/
|
|
211
|
+
object: string;
|
|
212
|
+
/**
|
|
213
|
+
* Usage statistics for the completion request. Present in the last chunk
|
|
214
|
+
* only if the StreamOptions.includeUsage is set to true.
|
|
215
|
+
*/
|
|
216
|
+
@Type(() => Usage)
|
|
217
|
+
usage: Usage;
|
|
218
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Expose } from 'class-transformer';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents an embedding vector returned by embedding endpoint.
|
|
5
|
+
*/
|
|
6
|
+
export class Embedding {
|
|
7
|
+
/**
|
|
8
|
+
* The index of the embedding in the list of embeddings.
|
|
9
|
+
*/
|
|
10
|
+
index: number;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The embedding vector, which is a list of floats. The length of vector depends on the model.
|
|
14
|
+
*/
|
|
15
|
+
embedding: number[];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The object type, which is always 'embedding'.
|
|
19
|
+
*/
|
|
20
|
+
object: string;
|
|
21
|
+
|
|
22
|
+
constructor(index: number, embedding: number[], object: string = 'embedding') {
|
|
23
|
+
this.index = index;
|
|
24
|
+
this.embedding = embedding;
|
|
25
|
+
this.object = object;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Creates an embedding vector representing the input text.
|
|
31
|
+
*/
|
|
32
|
+
export class EmbeddingRequest<T> {
|
|
33
|
+
/**
|
|
34
|
+
* Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request,
|
|
35
|
+
* pass an array of strings or array of token arrays.
|
|
36
|
+
* The input must not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002),
|
|
37
|
+
* cannot be an empty string, and any array must be 2048 dimensions or less.
|
|
38
|
+
*/
|
|
39
|
+
input: T;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* ID of the model to use.
|
|
43
|
+
*/
|
|
44
|
+
model: string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The format to return the embeddings in. Can be either float or base64.
|
|
48
|
+
*/
|
|
49
|
+
@Expose({ name: 'encoding_format' })
|
|
50
|
+
encodingFormat: string;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
|
|
54
|
+
*/
|
|
55
|
+
dimensions?: number;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
|
|
59
|
+
*/
|
|
60
|
+
user?: string;
|
|
61
|
+
|
|
62
|
+
constructor(
|
|
63
|
+
input: T,
|
|
64
|
+
model: string,
|
|
65
|
+
encodingFormat: string = 'float',
|
|
66
|
+
dimensions?: number,
|
|
67
|
+
user?: string
|
|
68
|
+
) {
|
|
69
|
+
this.input = input;
|
|
70
|
+
this.model = model;
|
|
71
|
+
this.encodingFormat = encodingFormat;
|
|
72
|
+
this.dimensions = dimensions;
|
|
73
|
+
this.user = user;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Create an embedding request with the given input, model and encoding format set to float.
|
|
78
|
+
* @param input Input text to embed.
|
|
79
|
+
* @param model ID of the model to use.
|
|
80
|
+
*/
|
|
81
|
+
static fromInputAndModel<T>(input: T, model: string): EmbeddingRequest<T> {
|
|
82
|
+
return new EmbeddingRequest(input, model);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create an embedding request with the given input. Encoding format is set to float and user is null and the model is set to 'text-embedding-ada-002'.
|
|
87
|
+
* @param input Input text to embed.
|
|
88
|
+
*/
|
|
89
|
+
static fromInput<T>(input: T): EmbeddingRequest<T> {
|
|
90
|
+
return new EmbeddingRequest(input, 'text-embedding-ada-002');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Type } from 'class-transformer';
|
|
2
|
+
import { Embedding } from './embeddings-request';
|
|
3
|
+
import { Usage } from './usage';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* List of multiple embedding responses.
|
|
7
|
+
*
|
|
8
|
+
* @typeparam T Type of the entities in the data list.
|
|
9
|
+
*/
|
|
10
|
+
export class EmbeddingResponse {
|
|
11
|
+
/**
|
|
12
|
+
* Must have value "list".
|
|
13
|
+
*/
|
|
14
|
+
object: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* List of entities.
|
|
18
|
+
*/
|
|
19
|
+
@Type(() => Embedding)
|
|
20
|
+
data: Embedding[];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* ID of the model to use.
|
|
24
|
+
*/
|
|
25
|
+
model: string;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Usage statistics for the completion request.
|
|
29
|
+
*/
|
|
30
|
+
@Type(() => Usage)
|
|
31
|
+
usage: Usage;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
object: string,
|
|
35
|
+
data: Embedding[],
|
|
36
|
+
model: string,
|
|
37
|
+
usage: Usage
|
|
38
|
+
) {
|
|
39
|
+
this.object = object;
|
|
40
|
+
this.data = data;
|
|
41
|
+
this.model = model;
|
|
42
|
+
this.usage = usage;
|
|
43
|
+
}
|
|
44
|
+
}
|