openai-api
Advanced tools
Comparing version 1.2.6 to 1.3.0
const DEFAULT_ENGINE = 'davinci'; | ||
const ORIGIN = 'https://api.openai.com'; | ||
const API_VERSION = 'v1'; | ||
const OPEN_AI_URL = `${ORIGIN}/${API_VERSION}` | ||
const OPEN_AI_URL = `${ORIGIN}/${API_VERSION}`; | ||
@@ -20,10 +20,13 @@ module.exports = { | ||
classificationsUrl() { | ||
return `${OPEN_AI_URL}/classifications` | ||
return `${OPEN_AI_URL}/classifications`; | ||
}, | ||
filesUrl() { | ||
return `${OPEN_AI_URL}/files` | ||
return `${OPEN_AI_URL}/files`; | ||
}, | ||
answersUrl() { | ||
return `${OPEN_AI_URL}/answers` | ||
return `${OPEN_AI_URL}/answers`; | ||
}, | ||
embeddingsUrl(engine) { | ||
return `${OPEN_AI_URL}/engines/${engine}/embeddings`; | ||
} | ||
}; |
declare module 'openai-api' { | ||
interface CompletionOpts { | ||
export interface CompletionOpts { | ||
engine: string; | ||
@@ -17,3 +17,3 @@ prompt?: string; | ||
user?: string; | ||
logitBias?: { [ token: string ]: number } | ||
logitBias?: { [token: string]: number; }; | ||
} | ||
@@ -23,17 +23,17 @@ | ||
data: { | ||
id: string | ||
object: string | ||
created: number | ||
model: string | ||
choices: Choice[] | ||
} | ||
} | ||
id: string; | ||
object: string; | ||
created: number; | ||
model: string; | ||
choices: Choice[]; | ||
}; | ||
} | ||
export interface Choice { | ||
text: string | ||
index: number | ||
logprobs: any | ||
finish_reason: string | ||
text: string; | ||
index: number; | ||
logprobs: any; | ||
finish_reason: string; | ||
} | ||
export interface SearchOpts { | ||
@@ -43,6 +43,6 @@ engine: string; | ||
file?: string; | ||
query: string | ||
query: string; | ||
maxRerank?: number; | ||
returnMetadata?: boolean; | ||
} | ||
} | ||
@@ -53,3 +53,3 @@ export interface Search { | ||
} | ||
export interface Document { | ||
@@ -61,4 +61,15 @@ document: number; | ||
} | ||
export interface Embedding { | ||
object: string; | ||
data: EmbeddingData[]; | ||
model: string; | ||
} | ||
export interface EmbeddingData { | ||
object: string; | ||
index: number; | ||
embedding: number[]; | ||
} | ||
class OpenAI { | ||
@@ -70,4 +81,4 @@ constructor(api: string); | ||
} | ||
export default OpenAI; | ||
} |
38
index.js
@@ -17,3 +17,3 @@ "use strict"; | ||
return result.split(' ').join('_').toLowerCase(); | ||
} | ||
}; | ||
@@ -36,2 +36,27 @@ const data = {}; | ||
_check_embeddings_engine_name(engine) { | ||
const availableEngineNames = [ | ||
'text-similarity-ada-001', | ||
'text-similarity-babbage-001', | ||
'text-similarity-curie-001', | ||
'text-similarity-davinci-001', | ||
'text-search-ada-doc-001', | ||
'text-search-ada-query-001', | ||
'text-search-babbage-doc-001', | ||
'text-search-babbage-query-001', | ||
'text-search-curie-doc-001', | ||
'text-search-curie-query-001', | ||
'text-search-davinci-doc-001', | ||
'text-search-davinci-query-001', | ||
'code-search-ada-code-001', | ||
'code-search-ada-text-001', | ||
'code-search-babbage-code-001', | ||
'code-search-babbage-text-001', | ||
]; | ||
if (!availableEngineNames.includes(engine)) { | ||
throw new Error(`Unknown engine name for embeddings. Available engine names are ${availableEngineNames}`); | ||
} | ||
} | ||
complete(opts) { | ||
@@ -50,3 +75,3 @@ const url = config.completionURL(opts.engine || DEFAULT_ENGINE); | ||
search(opts) { | ||
const url = config.searchURL(opts.engine || DEFAULT_ENGINE) | ||
const url = config.searchURL(opts.engine || DEFAULT_ENGINE); | ||
delete opts.engine; | ||
@@ -68,3 +93,3 @@ return this._send_request(url, 'post', opts); | ||
const url = config.enginesUrl(); | ||
return this._send_request(url, 'get') | ||
return this._send_request(url, 'get'); | ||
} | ||
@@ -76,4 +101,11 @@ | ||
} | ||
embeddings(opts) { | ||
this._check_embeddings_engine_name(opts.engine); | ||
const url = config.embeddingsUrl(opts.engine); | ||
return this._send_request(url, 'post', opts); | ||
} | ||
} | ||
module.exports = OpenAI; |
{ | ||
"name": "openai-api", | ||
"version": "1.2.6", | ||
"version": "1.3.0", | ||
"description": "A tiny client module for the openAI API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -133,2 +133,21 @@ # openai-api | ||
### Embeddings API call | ||
Documentation: [https://beta.openai.com/docs/api-reference/embeddings](https://beta.openai.com/docs/api-reference/embeddings) | ||
```js | ||
(async () => { | ||
const gptResponse = await openai.embeddings({ | ||
"engine": "test-similarity-babbage-001", | ||
"input": [ | ||
"A happy moment", | ||
"I am sad.", | ||
"I am feeling awesome" | ||
], | ||
}); | ||
console.log(gptResponse.data); // see index.d.ts interface Embedding | ||
})(); | ||
``` | ||
### Get number of tokens for string | ||
@@ -144,1 +163,3 @@ #### Not supported as of 4/21. See issue #20 | ||
------- | ||
Powered by [Obelisk.ooo](https://obelisk.ooo) |
@@ -89,6 +89,18 @@ require('dotenv').config(); | ||
it('should return a default value from the encode function', async function () { | ||
const result = await openai.encode('this is a string') | ||
const result = await openai.encode('this is a string'); | ||
expect(result).to.be.ok; | ||
expect(result.length).to.be.eql(2047); | ||
}); | ||
it('should handle embeddings', async function () { | ||
const result = await openai.embeddings({ | ||
engine: 'text-similarity-ada-001', | ||
input: [ | ||
"A happy moment", | ||
"I am sad.", | ||
"I am feeling awesome" | ||
] | ||
}); | ||
expect(result).to.be.ok; | ||
}); | ||
}); |
13122
278
164