openai-free
Version:
Free OpenAI-compatible API client by JrDev06 - no API keys needed
89 lines (78 loc) • 1.96 kB
JavaScript
const axios = require('axios')
class OpenAI {
constructor(config = {}) {
this.baseURL = config.baseURL || 'https://gpt35.chenjinshui.top/api/openai/v1'
this.chat = {
completions: {
create: this.createChatCompletion.bind(this)
}
}
this.models = {
list: this.listModels.bind(this),
retrieve: this.retrieveModel.bind(this)
}
}
async createChatCompletion(params = {}) {
const payload = {
messages: params.messages || [],
model: params.model || 'gpt-4o',
temperature: params.temperature || 0.5,
presence_penalty: params.presence_penalty || 0,
frequency_penalty: params.frequency_penalty || 0,
top_p: params.top_p || 1,
max_tokens: params.max_tokens || 4000,
stream: params.stream || false
}
try {
const response = await axios.post(`${this.baseURL}/chat/completions`, payload, {
headers: {
'Content-Type': 'application/json',
'Accept': params.stream ? 'text/event-stream' : 'application/json'
}
})
return response.data
} catch (error) {
throw error
}
}
async listModels() {
return {
object: 'list',
data: [
{
id: 'gpt-4o',
object: 'model',
created: 1687882410,
owned_by: 'openai'
},
{
id: 'gpt-4o-mini',
object: 'model',
created: 1687882410,
owned_by: 'openai'
}
]
}
}
async retrieveModel(modelId) {
const models = {
'gpt-4o': {
id: 'gpt-4o',
object: 'model',
created: 1687882410,
owned_by: 'openai'
},
'gpt-4o-mini': {
id: 'gpt-4o-mini',
object: 'model',
created: 1687882410,
owned_by: 'openai'
}
}
if (!models[modelId]) {
throw new Error('Not found')
}
return models[modelId]
}
}
module.exports = OpenAI