Comparing version 0.0.7 to 0.0.8
/// <reference types="node" /> | ||
import { Answer, AnswerRequest, Classification, ClassificationRequest, Completion, CompletionRequest, ContentLabel, Engine, EngineId, File, FilePurpose, FineTune, FineTuneEvent, FineTuneRequest, JsonLines, SearchDocument, SearchRequest } from './types'; | ||
import { Readable } from 'stream'; | ||
import type { Answer, AnswerRequest, Classification, ClassificationRequest, Completion, CompletionRequest, Engine, EngineId, File, FilePurpose, FineTune, FineTuneEvent, FineTuneRequest, JsonLines, SearchDocument, SearchRequest } from './types'; | ||
export declare class OpenAI { | ||
@@ -12,2 +12,3 @@ private readonly url; | ||
completionTextStream(engine: EngineId, options: CompletionRequest): Promise<Readable>; | ||
contentFilter(content: string, user?: string): Promise<ContentLabel>; | ||
search(engine: EngineId, options: SearchRequest): Promise<SearchDocument[]>; | ||
@@ -14,0 +15,0 @@ classify(options: ClassificationRequest): Promise<Classification>; |
@@ -61,2 +61,33 @@ (function (factory) { | ||
} | ||
async contentFilter(content, user) { | ||
const completion = await this.complete('content-filter-alpha-c4', { | ||
prompt: `<|endoftext|>${content}\n--\nLabel:`, | ||
temperature: 0, | ||
max_tokens: 1, | ||
top_p: 1, | ||
frequency_penalty: 0, | ||
presence_penalty: 0, | ||
logprobs: 10, | ||
user, | ||
}); | ||
let label = Number(completion.choices[0].text); | ||
if (label === 2) { | ||
const logprobs = completion.choices[0].logprobs.top_logprobs[0]; | ||
if (logprobs['2'] < -0.355) { | ||
if (logprobs['0'] && logprobs['1']) { | ||
label = logprobs['0'] >= logprobs['1'] ? 0 : 1; | ||
} | ||
else if (logprobs['0']) { | ||
label = 0; | ||
} | ||
else if (logprobs['1']) { | ||
label = 1; | ||
} | ||
} | ||
} | ||
if (![0, 1, 2].includes(label)) { | ||
label = 2; | ||
} | ||
return label; | ||
} | ||
search(engine, options) { | ||
@@ -63,0 +94,0 @@ return this.request(`/engines/${engine}/search`, 'POST', options).then((r) => r.data); |
@@ -32,6 +32,12 @@ export declare type EngineId = 'davinci' | 'curie' | 'babbage' | 'ada' | string; | ||
} | ||
export interface LogProbs { | ||
tokens: string[]; | ||
token_logprobs: number[]; | ||
top_logprobs: Array<Record<string, number>>; | ||
text_offset: number[]; | ||
} | ||
export interface Choice { | ||
text: string; | ||
index: number; | ||
logprobes: number | null; | ||
logprobs: LogProbs; | ||
finish_reason: string | null; | ||
@@ -166,1 +172,6 @@ } | ||
} | ||
export declare const enum ContentLabel { | ||
Safe = 0, | ||
Sensitive = 1, | ||
Unsafe = 2 | ||
} |
{ | ||
"name": "openai", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Tiny OpenAI API wrapper", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -65,2 +65,3 @@ # OpenAI | ||
// Very experimental! Don't use on production!!! | ||
// This API may change at any time | ||
const stream = await openai.completionTextStream('curie', { | ||
@@ -74,2 +75,8 @@ prompt: 'Q: Hello\nA:', | ||
Make a content filter: | ||
```js | ||
const isSafe = (await openai.contentFilter('hi I am cool')) === 0; | ||
``` | ||
### Search | ||
@@ -76,0 +83,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19005
387
194