diff --git a/.env.example b/.env.example index dbfe67353df..fe924ba4db0 100644 --- a/.env.example +++ b/.env.example @@ -49,13 +49,24 @@ OPENAI_MODELS=gpt-3.5-turbo,gpt-3.5-turbo-16k,gpt-3.5-turbo-0301,text-davinci-00 # Note: I've noticed that the Azure API is much faster than the OpenAI API, so the streaming looks almost instantaneous. # Note "AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME" and "AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME" are optional but might be used in the future -# AZURE_OPENAI_API_KEY= +# AZURE_API_KEY= # AZURE_OPENAI_API_INSTANCE_NAME= # AZURE_OPENAI_API_DEPLOYMENT_NAME= # AZURE_OPENAI_API_VERSION= # AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME= # AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME= +# Identify the available models, separated by commas *without spaces*. +# The first will be default. +# Leave it blank to use internal settings. +AZURE_OPENAI_MODELS=gpt-3.5-turbo,gpt-4 + +# To use Azure with the Plugins endpoint, you need the variables above, and uncomment the following variable: +# NOTE: This may not work as expected and Azure OpenAI may not support OpenAI Functions yet +# Omit/leave it commented to use the default OpenAI API + +# PLUGINS_USE_AZURE="true" + ########################## # BingAI Endpoint: ########################## diff --git a/.eslintrc.js b/.eslintrc.js index 430cbe6f903..e7631e686e4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -36,6 +36,8 @@ module.exports = { } ], 'linebreak-style': 0, + 'no-trailing-spaces': 'error', + 'no-multiple-empty-lines': ['error', { 'max': 1 }], // "arrow-parens": [2, "as-needed", { requireForBlockBody: true }], // 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }], 'no-console': 'off', diff --git a/.gitignore b/.gitignore index 077b87a31a4..ba54ad5474e 100644 --- a/.gitignore +++ b/.gitignore @@ -48,10 +48,9 @@ bower_components/ # Environment .npmrc -.env -!.env.example -!.env.test.example .env* +!**/.env.example +!**/.env.test.example cache.json api/data/ owner.yml diff --git a/api/app/clients/bingai.js b/api/app/bingai.js similarity index 100% rename from api/app/clients/bingai.js rename to api/app/bingai.js diff --git a/api/app/clients/chatgpt-browser.js b/api/app/chatgpt-browser.js similarity index 100% rename from api/app/clients/chatgpt-browser.js rename to api/app/chatgpt-browser.js diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.js new file mode 100644 index 00000000000..6a306726b49 --- /dev/null +++ b/api/app/clients/BaseClient.js @@ -0,0 +1,536 @@ +const crypto = require('crypto'); +const TextStream = require('./TextStream'); +const { RecursiveCharacterTextSplitter } = require('langchain/text_splitter'); +const { ChatOpenAI } = require('langchain/chat_models/openai'); +const { loadSummarizationChain } = require('langchain/chains'); +const { refinePrompt } = require('./prompts/refinePrompt'); +const { getConvo, getMessages, saveMessage, updateMessage, saveConvo } = require('../../models'); + +class BaseClient { + constructor(apiKey, options = {}) { + this.apiKey = apiKey; + this.sender = options.sender || 'AI'; + this.contextStrategy = null; + this.currentDateString = new Date().toLocaleDateString('en-us', { + year: 'numeric', + month: 'long', + day: 'numeric' + }); + } + + setOptions() { + throw new Error("Method 'setOptions' must be implemented."); + } + + getCompletion() { + throw new Error("Method 'getCompletion' must be implemented."); + } + + getSaveOptions() { + throw new Error('Subclasses must implement getSaveOptions'); + } + + async buildMessages() { + throw new Error('Subclasses must implement buildMessages'); + } + + getBuildMessagesOptions() { + throw new Error('Subclasses must implement getBuildMessagesOptions'); + } + + async generateTextStream(text, onProgress, options = {}) { + const stream = new TextStream(text, options); + await stream.processTextStream(onProgress); + } + + async setMessageOptions(opts = {}) { + if (opts && typeof opts === 'object') { + this.setOptions(opts); + } + const user = opts.user || null; + const conversationId = opts.conversationId || crypto.randomUUID(); + const parentMessageId = opts.parentMessageId || '00000000-0000-0000-0000-000000000000'; + const userMessageId = opts.overrideParentMessageId || crypto.randomUUID(); + const responseMessageId = crypto.randomUUID(); + const saveOptions = this.getSaveOptions(); + this.abortController = opts.abortController || new AbortController(); + this.currentMessages = await this.loadHistory(conversationId, parentMessageId) ?? []; + + return { + ...opts, + user, + conversationId, + parentMessageId, + userMessageId, + responseMessageId, + saveOptions, + }; + } + + createUserMessage({ messageId, parentMessageId, conversationId, text}) { + const userMessage = { + messageId, + parentMessageId, + conversationId, + sender: 'User', + text, + isCreatedByUser: true + }; + return userMessage; + } + + async handleStartMethods(message, opts) { + const { + user, + conversationId, + parentMessageId, + userMessageId, + responseMessageId, + saveOptions, + } = await this.setMessageOptions(opts); + + const userMessage = this.createUserMessage({ + messageId: userMessageId, + parentMessageId, + conversationId, + text: message, + }); + + if (typeof opts?.getIds === 'function') { + opts.getIds({ + userMessage, + conversationId, + responseMessageId + }); + } + + if (typeof opts?.onStart === 'function') { + opts.onStart(userMessage); + } + + if (this.options.debug) { + console.debug('options'); + console.debug(this.options); + } + + return { + ...opts, + user, + conversationId, + responseMessageId, + saveOptions, + userMessage, + }; + } + + addInstructions(messages, instructions) { + const payload = []; + if (!instructions) { + return messages; + } + if (messages.length > 1) { + payload.push(...messages.slice(0, -1)); + } + + payload.push(instructions); + + if (messages.length > 0) { + payload.push(messages[messages.length - 1]); + } + + return payload; + } + + async handleTokenCountMap(tokenCountMap) { + if (this.currentMessages.length === 0) { + return; + } + + for (let i = 0; i < this.currentMessages.length; i++) { + // Skip the last message, which is the user message. + if (i === this.currentMessages.length - 1) { + break; + } + + const message = this.currentMessages[i]; + const { messageId } = message; + const update = {}; + + if (messageId === tokenCountMap.refined?.messageId) { + if (this.options.debug) { + console.debug(`Adding refined props to ${messageId}.`); + } + + update.refinedMessageText = tokenCountMap.refined.content; + update.refinedTokenCount = tokenCountMap.refined.tokenCount; + } + + if (message.tokenCount && !update.refinedTokenCount) { + if (this.options.debug) { + console.debug(`Skipping ${messageId}: already had a token count.`); + } + continue; + } + + const tokenCount = tokenCountMap[messageId]; + if (tokenCount) { + message.tokenCount = tokenCount; + update.tokenCount = tokenCount; + await this.updateMessageInDatabase({ messageId, ...update }); + } + } + } + + concatenateMessages(messages) { + return messages.reduce((acc, message) => { + const nameOrRole = message.name ?? message.role; + return acc + `${nameOrRole}:\n${message.content}\n\n`; + }, ''); + } + + async refineMessages(messagesToRefine, remainingContextTokens) { + const model = new ChatOpenAI({ temperature: 0 }); + const chain = loadSummarizationChain(model, { type: 'refine', verbose: this.options.debug, refinePrompt }); + const splitter = new RecursiveCharacterTextSplitter({ + chunkSize: 1500, + chunkOverlap: 100, + }); + const userMessages = this.concatenateMessages(messagesToRefine.filter(m => m.role === 'user')); + const assistantMessages = this.concatenateMessages(messagesToRefine.filter(m => m.role !== 'user')); + const userDocs = await splitter.createDocuments([userMessages],[],{ + chunkHeader: `DOCUMENT NAME: User Message\n\n---\n\n`, + appendChunkOverlapHeader: true, + }); + const assistantDocs = await splitter.createDocuments([assistantMessages],[],{ + chunkHeader: `DOCUMENT NAME: Assistant Message\n\n---\n\n`, + appendChunkOverlapHeader: true, + }); + // const chunkSize = Math.round(concatenatedMessages.length / 512); + const input_documents = userDocs.concat(assistantDocs); + if (this.options.debug ) { + console.debug(`Refining messages...`); + } + try { + const res = await chain.call({ + input_documents, + signal: this.abortController.signal, + }); + + const refinedMessage = { + role: 'assistant', + content: res.output_text, + tokenCount: this.getTokenCount(res.output_text), + } + + if (this.options.debug ) { + console.debug('Refined messages', refinedMessage); + console.debug(`remainingContextTokens: ${remainingContextTokens}, after refining: ${remainingContextTokens - refinedMessage.tokenCount}`); + } + + return refinedMessage; + } catch (e) { + console.error('Error refining messages'); + console.error(e); + return null; + } + } + + /** + * This method processes an array of messages and returns a context of messages that fit within a token limit. + * It iterates over the messages from newest to oldest, adding them to the context until the token limit is reached. + * If the token limit would be exceeded by adding a message, that message and possibly the previous one are added to a separate array of messages to refine. + * The method uses `push` and `pop` operations for efficient array manipulation, and reverses the arrays at the end to maintain the original order of the messages. + * The method also includes a mechanism to avoid blocking the event loop by waiting for the next tick after each iteration. + * + * @param {Array} messages - An array of messages, each with a `tokenCount` property. The messages should be ordered from oldest to newest. + * @returns {Object} An object with three properties: `context`, `remainingContextTokens`, and `messagesToRefine`. `context` is an array of messages that fit within the token limit. `remainingContextTokens` is the number of tokens remaining within the limit after adding the messages to the context. `messagesToRefine` is an array of messages that were not added to the context because they would have exceeded the token limit. + */ + async getMessagesWithinTokenLimit(messages) { + let currentTokenCount = 0; + let context = []; + let messagesToRefine = []; + let refineIndex = -1; + let remainingContextTokens = this.maxContextTokens; + + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i]; + const newTokenCount = currentTokenCount + message.tokenCount; + const exceededLimit = newTokenCount > this.maxContextTokens; + let shouldRefine = exceededLimit && this.shouldRefineContext; + let refineNextMessage = i !== 0 && i !== 1 && context.length > 0; + + if (shouldRefine) { + messagesToRefine.push(message); + + if (refineIndex === -1) { + refineIndex = i; + } + + if (refineNextMessage) { + refineIndex = i + 1; + const removedMessage = context.pop(); + messagesToRefine.push(removedMessage); + currentTokenCount -= removedMessage.tokenCount; + remainingContextTokens = this.maxContextTokens - currentTokenCount; + refineNextMessage = false; + } + + continue; + } else if (exceededLimit) { + break; + } + + context.push(message); + currentTokenCount = newTokenCount; + remainingContextTokens = this.maxContextTokens - currentTokenCount; + await new Promise(resolve => setImmediate(resolve)); + } + + return { context: context.reverse(), remainingContextTokens, messagesToRefine: messagesToRefine.reverse(), refineIndex }; + } + + async handleContextStrategy({instructions, orderedMessages, formattedMessages}) { + let payload = this.addInstructions(formattedMessages, instructions); + let orderedWithInstructions = this.addInstructions(orderedMessages, instructions); + let { context, remainingContextTokens, messagesToRefine, refineIndex } = await this.getMessagesWithinTokenLimit(payload); + + payload = context; + let refinedMessage; + + // if (messagesToRefine.length > 0) { + // refinedMessage = await this.refineMessages(messagesToRefine, remainingContextTokens); + // payload.unshift(refinedMessage); + // remainingContextTokens -= refinedMessage.tokenCount; + // } + // if (remainingContextTokens <= instructions?.tokenCount) { + // if (this.options.debug) { + // console.debug(`Remaining context (${remainingContextTokens}) is less than instructions token count: ${instructions.tokenCount}`); + // } + + // ({ context, remainingContextTokens, messagesToRefine, refineIndex } = await this.getMessagesWithinTokenLimit(payload)); + // payload = context; + // } + + // Calculate the difference in length to determine how many messages were discarded if any + let diff = orderedWithInstructions.length - payload.length; + + if (this.options.debug) { + console.debug('<---------------------------------DIFF--------------------------------->'); + console.debug(`Difference between payload (${payload.length}) and orderedWithInstructions (${orderedWithInstructions.length}): ${diff}`); + console.debug('remainingContextTokens, this.maxContextTokens (1/2)', remainingContextTokens, this.maxContextTokens); + } + + // If the difference is positive, slice the orderedWithInstructions array + if (diff > 0) { + orderedWithInstructions = orderedWithInstructions.slice(diff); + } + + if (messagesToRefine.length > 0) { + refinedMessage = await this.refineMessages(messagesToRefine, remainingContextTokens); + payload.unshift(refinedMessage); + remainingContextTokens -= refinedMessage.tokenCount; + } + + if (this.options.debug) { + console.debug('remainingContextTokens, this.maxContextTokens (2/2)', remainingContextTokens, this.maxContextTokens); + } + + let tokenCountMap = orderedWithInstructions.reduce((map, message, index) => { + if (!message.messageId) { + return map; + } + + if (index === refineIndex) { + map.refined = { ...refinedMessage, messageId: message.messageId}; + } + + map[message.messageId] = payload[index].tokenCount; + return map; + }, {}); + + const promptTokens = this.maxContextTokens - remainingContextTokens; + + if (this.options.debug) { + console.debug('<-------------------------PAYLOAD/TOKEN COUNT MAP------------------------->'); + console.debug('Payload:', payload); + console.debug('Token Count Map:', tokenCountMap); + console.debug('Prompt Tokens', promptTokens, remainingContextTokens, this.maxContextTokens); + } + + return { payload, tokenCountMap, promptTokens, messages: orderedWithInstructions }; + } + + async sendMessage(message, opts = {}) { + console.log('BaseClient: sendMessage', message, opts); + const { + user, + conversationId, + responseMessageId, + saveOptions, + userMessage, + } = await this.handleStartMethods(message, opts); + + // It's not necessary to push to currentMessages + // depending on subclass implementation of handling messages + this.currentMessages.push(userMessage); + + let { prompt: payload, tokenCountMap, promptTokens } = await this.buildMessages( + this.currentMessages, + userMessage.messageId, + this.getBuildMessagesOptions(opts), + ); + + if (this.options.debug) { + console.debug('payload'); + console.debug(payload); + } + + if (tokenCountMap) { + payload = payload.map((message, i) => { + const { tokenCount, ...messageWithoutTokenCount } = message; + // userMessage is always the last one in the payload + if (i === payload.length - 1) { + userMessage.tokenCount = message.tokenCount; + console.debug(`Token count for user message: ${tokenCount}`, `Instruction Tokens: ${tokenCountMap.instructions || 'N/A'}`); + } + return messageWithoutTokenCount; + }); + this.handleTokenCountMap(tokenCountMap); + } + + await this.saveMessageToDatabase(userMessage, saveOptions, user); + const responseMessage = { + messageId: responseMessageId, + conversationId, + parentMessageId: userMessage.messageId, + isCreatedByUser: false, + model: this.modelOptions.model, + sender: this.sender, + text: await this.sendCompletion(payload, opts), + promptTokens, + }; + + if (tokenCountMap && this.getTokenCountForResponse) { + responseMessage.tokenCount = this.getTokenCountForResponse(responseMessage); + responseMessage.completionTokens = responseMessage.tokenCount; + } + await this.saveMessageToDatabase(responseMessage, saveOptions, user); + delete responseMessage.tokenCount; + return responseMessage; + } + + async getConversation(conversationId, user = null) { + return await getConvo(user, conversationId); + } + + async loadHistory(conversationId, parentMessageId = null) { + if (this.options.debug) { + console.debug('Loading history for conversation', conversationId, parentMessageId); + } + + const messages = (await getMessages({ conversationId })) || []; + + if (messages.length === 0) { + return []; + } + + let mapMethod = null; + if (this.getMessageMapMethod) { + mapMethod = this.getMessageMapMethod(); + } + + return this.constructor.getMessagesForConversation(messages, parentMessageId, mapMethod); + } + + async saveMessageToDatabase(message, endpointOptions, user = null) { + await saveMessage({ ...message, unfinished: false }); + await saveConvo(user, { + conversationId: message.conversationId, + endpoint: this.options.endpoint, + ...endpointOptions + }); + } + + async updateMessageInDatabase(message) { + await updateMessage(message); + } + + /** + * Iterate through messages, building an array based on the parentMessageId. + * Each message has an id and a parentMessageId. The parentMessageId is the id of the message that this message is a reply to. + * @param messages + * @param parentMessageId + * @returns {*[]} An array containing the messages in the order they should be displayed, starting with the root message. + */ + static getMessagesForConversation(messages, parentMessageId, mapMethod = null) { + if (!messages || messages.length === 0) { + return []; + } + + const orderedMessages = []; + let currentMessageId = parentMessageId; + while (currentMessageId) { + const message = messages.find(msg => { + const messageId = msg.messageId ?? msg.id; + return messageId === currentMessageId; + }); + if (!message) { + break; + } + orderedMessages.unshift(message); + currentMessageId = message.parentMessageId; + } + + if (mapMethod) { + return orderedMessages.map(mapMethod); + } + + return orderedMessages; + } + + /** + * Algorithm adapted from "6. Counting tokens for chat API calls" of + * https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb + * + * An additional 2 tokens need to be added for metadata after all messages have been counted. + * + * @param {*} message + */ + getTokenCountForMessage(message) { + let tokensPerMessage; + let nameAdjustment; + if (this.modelOptions.model.startsWith('gpt-4')) { + tokensPerMessage = 3; + nameAdjustment = 1; + } else { + tokensPerMessage = 4; + nameAdjustment = -1; + } + + if (this.options.debug) { + console.debug('getTokenCountForMessage', message); + } + + // Map each property of the message to the number of tokens it contains + const propertyTokenCounts = Object.entries(message).map(([key, value]) => { + if (key === 'tokenCount' || typeof value !== 'string') { + return 0; + } + // Count the number of tokens in the property value + const numTokens = this.getTokenCount(value); + + // Adjust by `nameAdjustment` tokens if the property key is 'name' + const adjustment = (key === 'name') ? nameAdjustment : 0; + return numTokens + adjustment; + }); + + if (this.options.debug) { + console.debug('propertyTokenCounts', propertyTokenCounts); + } + + // Sum the number of tokens in all properties and add `tokensPerMessage` for metadata + return propertyTokenCounts.reduce((a, b) => a + b, tokensPerMessage); + } +} + +module.exports = BaseClient; \ No newline at end of file diff --git a/api/app/clients/ChatGPTClient.js b/api/app/clients/ChatGPTClient.js new file mode 100644 index 00000000000..81dfa2ee497 --- /dev/null +++ b/api/app/clients/ChatGPTClient.js @@ -0,0 +1,570 @@ +const crypto = require('crypto'); +const Keyv = require('keyv'); +const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('@dqbd/tiktoken'); +const { fetchEventSource } = require('@waylaidwanderer/fetch-event-source'); +const { Agent, ProxyAgent } = require('undici'); +const BaseClient = require('./BaseClient'); + +const CHATGPT_MODEL = 'gpt-3.5-turbo'; +const tokenizersCache = {}; + +class ChatGPTClient extends BaseClient { + constructor( + apiKey, + options = {}, + cacheOptions = {}, + ) { + super(apiKey, options, cacheOptions); + + cacheOptions.namespace = cacheOptions.namespace || 'chatgpt'; + this.conversationsCache = new Keyv(cacheOptions); + this.setOptions(options); + } + + setOptions(options) { + if (this.options && !this.options.replaceOptions) { + // nested options aren't spread properly, so we need to do this manually + this.options.modelOptions = { + ...this.options.modelOptions, + ...options.modelOptions, + }; + delete options.modelOptions; + // now we can merge options + this.options = { + ...this.options, + ...options, + }; + } else { + this.options = options; + } + + if (this.options.openaiApiKey) { + this.apiKey = this.options.openaiApiKey; + } + + const modelOptions = this.options.modelOptions || {}; + this.modelOptions = { + ...modelOptions, + // set some good defaults (check for undefined in some cases because they may be 0) + model: modelOptions.model || CHATGPT_MODEL, + temperature: typeof modelOptions.temperature === 'undefined' ? 0.8 : modelOptions.temperature, + top_p: typeof modelOptions.top_p === 'undefined' ? 1 : modelOptions.top_p, + presence_penalty: typeof modelOptions.presence_penalty === 'undefined' ? 1 : modelOptions.presence_penalty, + stop: modelOptions.stop, + }; + + this.isChatGptModel = this.modelOptions.model.startsWith('gpt-'); + const { isChatGptModel } = this; + this.isUnofficialChatGptModel = this.modelOptions.model.startsWith('text-chat') || this.modelOptions.model.startsWith('text-davinci-002-render'); + const { isUnofficialChatGptModel } = this; + + // Davinci models have a max context length of 4097 tokens. + this.maxContextTokens = this.options.maxContextTokens || (isChatGptModel ? 4095 : 4097); + // I decided to reserve 1024 tokens for the response. + // The max prompt tokens is determined by the max context tokens minus the max response tokens. + // Earlier messages will be dropped until the prompt is within the limit. + this.maxResponseTokens = this.modelOptions.max_tokens || 1024; + this.maxPromptTokens = this.options.maxPromptTokens || (this.maxContextTokens - this.maxResponseTokens); + + if (this.maxPromptTokens + this.maxResponseTokens > this.maxContextTokens) { + throw new Error(`maxPromptTokens + max_tokens (${this.maxPromptTokens} + ${this.maxResponseTokens} = ${this.maxPromptTokens + this.maxResponseTokens}) must be less than or equal to maxContextTokens (${this.maxContextTokens})`); + } + + this.userLabel = this.options.userLabel || 'User'; + this.chatGptLabel = this.options.chatGptLabel || 'ChatGPT'; + + if (isChatGptModel) { + // Use these faux tokens to help the AI understand the context since we are building the chat log ourselves. + // Trying to use "<|im_start|>" causes the AI to still generate "<" or "<|" at the end sometimes for some reason, + // without tripping the stop sequences, so I'm using "||>" instead. + this.startToken = '||>'; + this.endToken = ''; + this.gptEncoder = this.constructor.getTokenizer('cl100k_base'); + } else if (isUnofficialChatGptModel) { + this.startToken = '<|im_start|>'; + this.endToken = '<|im_end|>'; + this.gptEncoder = this.constructor.getTokenizer('text-davinci-003', true, { + '<|im_start|>': 100264, + '<|im_end|>': 100265, + }); + } else { + // Previously I was trying to use "<|endoftext|>" but there seems to be some bug with OpenAI's token counting + // system that causes only the first "<|endoftext|>" to be counted as 1 token, and the rest are not treated + // as a single token. So we're using this instead. + this.startToken = '||>'; + this.endToken = ''; + try { + this.gptEncoder = this.constructor.getTokenizer(this.modelOptions.model, true); + } catch { + this.gptEncoder = this.constructor.getTokenizer('text-davinci-003', true); + } + } + + if (!this.modelOptions.stop) { + const stopTokens = [this.startToken]; + if (this.endToken && this.endToken !== this.startToken) { + stopTokens.push(this.endToken); + } + stopTokens.push(`\n${this.userLabel}:`); + stopTokens.push('<|diff_marker|>'); + // I chose not to do one for `chatGptLabel` because I've never seen it happen + this.modelOptions.stop = stopTokens; + } + + if (this.options.reverseProxyUrl) { + this.completionsUrl = this.options.reverseProxyUrl; + } else if (isChatGptModel) { + this.completionsUrl = 'https://api.openai.com/v1/chat/completions'; + } else { + this.completionsUrl = 'https://api.openai.com/v1/completions'; + } + + return this; + } + + static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) { + if (tokenizersCache[encoding]) { + return tokenizersCache[encoding]; + } + let tokenizer; + if (isModelName) { + tokenizer = encodingForModel(encoding, extendSpecialTokens); + } else { + tokenizer = getEncoding(encoding, extendSpecialTokens); + } + tokenizersCache[encoding] = tokenizer; + return tokenizer; + } + + async getCompletion(input, onProgress, abortController = null) { + if (!abortController) { + abortController = new AbortController(); + } + const modelOptions = { ...this.modelOptions }; + if (typeof onProgress === 'function') { + modelOptions.stream = true; + } + if (this.isChatGptModel) { + modelOptions.messages = input; + } else { + modelOptions.prompt = input; + } + const { debug } = this.options; + const url = this.completionsUrl; + if (debug) { + console.debug(); + console.debug(url); + console.debug(modelOptions); + console.debug(); + } + const opts = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(modelOptions), + dispatcher: new Agent({ + bodyTimeout: 0, + headersTimeout: 0, + }), + }; + + if (this.apiKey && this.options.azure) { + opts.headers['api-key'] = this.apiKey; + } else if (this.apiKey) { + opts.headers.Authorization = `Bearer ${this.apiKey}`; + } + + if (this.options.headers) { + opts.headers = { ...opts.headers, ...this.options.headers }; + } + + if (this.options.proxy) { + opts.dispatcher = new ProxyAgent(this.options.proxy); + } + + if (modelOptions.stream) { + // eslint-disable-next-line no-async-promise-executor + return new Promise(async (resolve, reject) => { + try { + let done = false; + await fetchEventSource(url, { + ...opts, + signal: abortController.signal, + async onopen(response) { + if (response.status === 200) { + return; + } + if (debug) { + console.debug(response); + } + let error; + try { + const body = await response.text(); + error = new Error(`Failed to send message. HTTP ${response.status} - ${body}`); + error.status = response.status; + error.json = JSON.parse(body); + } catch { + error = error || new Error(`Failed to send message. HTTP ${response.status}`); + } + throw error; + }, + onclose() { + if (debug) { + console.debug('Server closed the connection unexpectedly, returning...'); + } + // workaround for private API not sending [DONE] event + if (!done) { + onProgress('[DONE]'); + abortController.abort(); + resolve(); + } + }, + onerror(err) { + if (debug) { + console.debug(err); + } + // rethrow to stop the operation + throw err; + }, + onmessage(message) { + if (debug) { + // console.debug(message); + } + if (!message.data || message.event === 'ping') { + return; + } + if (message.data === '[DONE]') { + onProgress('[DONE]'); + abortController.abort(); + resolve(); + done = true; + return; + } + onProgress(JSON.parse(message.data)); + }, + }); + } catch (err) { + reject(err); + } + }); + } + const response = await fetch( + url, + { + ...opts, + signal: abortController.signal, + }, + ); + if (response.status !== 200) { + const body = await response.text(); + const error = new Error(`Failed to send message. HTTP ${response.status} - ${body}`); + error.status = response.status; + try { + error.json = JSON.parse(body); + } catch { + error.body = body; + } + throw error; + } + return response.json(); + } + + async generateTitle(userMessage, botMessage) { + const instructionsPayload = { + role: 'system', + content: `Write an extremely concise subtitle for this conversation with no more than a few words. All words should be capitalized. Exclude punctuation. + +||>Message: +${userMessage.message} +||>Response: +${botMessage.message} + +||>Title:`, + }; + + const titleGenClientOptions = JSON.parse(JSON.stringify(this.options)); + titleGenClientOptions.modelOptions = { + model: 'gpt-3.5-turbo', + temperature: 0, + presence_penalty: 0, + frequency_penalty: 0, + }; + const titleGenClient = new ChatGPTClient(this.apiKey, titleGenClientOptions); + const result = await titleGenClient.getCompletion([instructionsPayload], null); + // remove any non-alphanumeric characters, replace multiple spaces with 1, and then trim + return result.choices[0].message.content + .replace(/[^a-zA-Z0-9' ]/g, '') + .replace(/\s+/g, ' ') + .trim(); + } + + async sendMessage( + message, + opts = {}, + ) { + if (opts.clientOptions && typeof opts.clientOptions === 'object') { + this.setOptions(opts.clientOptions); + } + + const conversationId = opts.conversationId || crypto.randomUUID(); + const parentMessageId = opts.parentMessageId || crypto.randomUUID(); + + let conversation = typeof opts.conversation === 'object' + ? opts.conversation + : await this.conversationsCache.get(conversationId); + + let isNewConversation = false; + if (!conversation) { + conversation = { + messages: [], + createdAt: Date.now(), + }; + isNewConversation = true; + } + + const shouldGenerateTitle = opts.shouldGenerateTitle && isNewConversation; + + const userMessage = { + id: crypto.randomUUID(), + parentMessageId, + role: 'User', + message, + }; + conversation.messages.push(userMessage); + + // Doing it this way instead of having each message be a separate element in the array seems to be more reliable, + // especially when it comes to keeping the AI in character. It also seems to improve coherency and context retention. + const { prompt: payload, context } = await this.buildPrompt( + conversation.messages, + userMessage.id, + { + isChatGptModel: this.isChatGptModel, + promptPrefix: opts.promptPrefix, + }, + ); + + if (this.options.keepNecessaryMessagesOnly) { + conversation.messages = context; + } + + let reply = ''; + let result = null; + if (typeof opts.onProgress === 'function') { + await this.getCompletion( + payload, + (progressMessage) => { + if (progressMessage === '[DONE]') { + return; + } + const token = this.isChatGptModel ? progressMessage.choices[0].delta.content : progressMessage.choices[0].text; + // first event's delta content is always undefined + if (!token) { + return; + } + if (this.options.debug) { + console.debug(token); + } + if (token === this.endToken) { + return; + } + opts.onProgress(token); + reply += token; + }, + opts.abortController || new AbortController(), + ); + } else { + result = await this.getCompletion( + payload, + null, + opts.abortController || new AbortController(), + ); + if (this.options.debug) { + console.debug(JSON.stringify(result)); + } + if (this.isChatGptModel) { + reply = result.choices[0].message.content; + } else { + reply = result.choices[0].text.replace(this.endToken, ''); + } + } + + // avoids some rendering issues when using the CLI app + if (this.options.debug) { + console.debug(); + } + + reply = reply.trim(); + + const replyMessage = { + id: crypto.randomUUID(), + parentMessageId: userMessage.id, + role: 'ChatGPT', + message: reply, + }; + conversation.messages.push(replyMessage); + + const returnData = { + response: replyMessage.message, + conversationId, + parentMessageId: replyMessage.parentMessageId, + messageId: replyMessage.id, + details: result || {}, + }; + + if (shouldGenerateTitle) { + conversation.title = await this.generateTitle(userMessage, replyMessage); + returnData.title = conversation.title; + } + + await this.conversationsCache.set(conversationId, conversation); + + if (this.options.returnConversation) { + returnData.conversation = conversation; + } + + return returnData; + } + + async buildPrompt(messages, parentMessageId, { isChatGptModel = false, promptPrefix = null }) { + const orderedMessages = this.constructor.getMessagesForConversation(messages, parentMessageId); + + promptPrefix = (promptPrefix || this.options.promptPrefix || '').trim(); + if (promptPrefix) { + // If the prompt prefix doesn't end with the end token, add it. + if (!promptPrefix.endsWith(`${this.endToken}`)) { + promptPrefix = `${promptPrefix.trim()}${this.endToken}\n\n`; + } + promptPrefix = `${this.startToken}Instructions:\n${promptPrefix}`; + } else { + const currentDateString = new Date().toLocaleDateString( + 'en-us', + { year: 'numeric', month: 'long', day: 'numeric' }, + ); + promptPrefix = `${this.startToken}Instructions:\nYou are ChatGPT, a large language model trained by OpenAI. Respond conversationally.\nCurrent date: ${currentDateString}${this.endToken}\n\n`; + } + + const promptSuffix = `${this.startToken}${this.chatGptLabel}:\n`; // Prompt ChatGPT to respond. + + const instructionsPayload = { + role: 'system', + name: 'instructions', + content: promptPrefix, + }; + + const messagePayload = { + role: 'system', + content: promptSuffix, + }; + + let currentTokenCount; + if (isChatGptModel) { + currentTokenCount = this.getTokenCountForMessage(instructionsPayload) + this.getTokenCountForMessage(messagePayload); + } else { + currentTokenCount = this.getTokenCount(`${promptPrefix}${promptSuffix}`); + } + let promptBody = ''; + const maxTokenCount = this.maxPromptTokens; + + const context = []; + + // Iterate backwards through the messages, adding them to the prompt until we reach the max token count. + // Do this within a recursive async function so that it doesn't block the event loop for too long. + const buildPromptBody = async () => { + if (currentTokenCount < maxTokenCount && orderedMessages.length > 0) { + const message = orderedMessages.pop(); + const roleLabel = message?.isCreatedByUser || message?.role?.toLowerCase() === 'user' ? this.userLabel : this.chatGptLabel; + const messageString = `${this.startToken}${roleLabel}:\n${message?.text ?? message?.message}${this.endToken}\n`; + let newPromptBody; + if (promptBody || isChatGptModel) { + newPromptBody = `${messageString}${promptBody}`; + } else { + // Always insert prompt prefix before the last user message, if not gpt-3.5-turbo. + // This makes the AI obey the prompt instructions better, which is important for custom instructions. + // After a bunch of testing, it doesn't seem to cause the AI any confusion, even if you ask it things + // like "what's the last thing I wrote?". + newPromptBody = `${promptPrefix}${messageString}${promptBody}`; + } + + context.unshift(message); + + const tokenCountForMessage = this.getTokenCount(messageString); + const newTokenCount = currentTokenCount + tokenCountForMessage; + if (newTokenCount > maxTokenCount) { + if (promptBody) { + // This message would put us over the token limit, so don't add it. + return false; + } + // This is the first message, so we can't add it. Just throw an error. + throw new Error(`Prompt is too long. Max token count is ${maxTokenCount}, but prompt is ${newTokenCount} tokens long.`); + } + promptBody = newPromptBody; + currentTokenCount = newTokenCount; + // wait for next tick to avoid blocking the event loop + await new Promise(resolve => setImmediate(resolve)); + return buildPromptBody(); + } + return true; + }; + + await buildPromptBody(); + + const prompt = `${promptBody}${promptSuffix}`; + if (isChatGptModel) { + messagePayload.content = prompt; + // Add 2 tokens for metadata after all messages have been counted. + currentTokenCount += 2; + } + + // Use up to `this.maxContextTokens` tokens (prompt + response), but try to leave `this.maxTokens` tokens for the response. + this.modelOptions.max_tokens = Math.min(this.maxContextTokens - currentTokenCount, this.maxResponseTokens); + + if (this.options.debug) { + console.debug(`Prompt : ${prompt}`); + } + + if (isChatGptModel) { + return { prompt: [instructionsPayload, messagePayload], context }; + } + return { prompt, context }; + } + + getTokenCount(text) { + return this.gptEncoder.encode(text, 'all').length; + } + + /** + * Algorithm adapted from "6. Counting tokens for chat API calls" of + * https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb + * + * An additional 2 tokens need to be added for metadata after all messages have been counted. + * + * @param {*} message + */ + getTokenCountForMessage(message) { + let tokensPerMessage; + let nameAdjustment; + if (this.modelOptions.model.startsWith('gpt-4')) { + tokensPerMessage = 3; + nameAdjustment = 1; + } else { + tokensPerMessage = 4; + nameAdjustment = -1; + } + + // Map each property of the message to the number of tokens it contains + const propertyTokenCounts = Object.entries(message).map(([key, value]) => { + // Count the number of tokens in the property value + const numTokens = this.getTokenCount(value); + + // Adjust by `nameAdjustment` tokens if the property key is 'name' + const adjustment = (key === 'name') ? nameAdjustment : 0; + return numTokens + adjustment; + }); + + // Sum the number of tokens in all properties and add `tokensPerMessage` for metadata + return propertyTokenCounts.reduce((a, b) => a + b, tokensPerMessage); + } +} + +module.exports = ChatGPTClient; \ No newline at end of file diff --git a/api/app/google/GoogleClient.js b/api/app/clients/GoogleClient.js similarity index 64% rename from api/app/google/GoogleClient.js rename to api/app/clients/GoogleClient.js index b867c2e81c9..e396581c714 100644 --- a/api/app/google/GoogleClient.js +++ b/api/app/clients/GoogleClient.js @@ -1,8 +1,6 @@ -const crypto = require('crypto'); -const TextStream = require('../stream'); +const BaseClient = require('./BaseClient'); const { google } = require('googleapis'); const { Agent, ProxyAgent } = require('undici'); -const { getMessages, saveMessage, saveConvo } = require('../../models'); const { encoding_for_model: encodingForModel, get_encoding: getEncoding @@ -10,17 +8,14 @@ const { const tokenizersCache = {}; -class GoogleAgent { +class GoogleClient extends BaseClient { constructor(credentials, options = {}) { + super('apiKey', options); this.client_email = credentials.client_email; this.project_id = credentials.project_id; this.private_key = credentials.private_key; + this.sender = 'PaLM2'; this.setOptions(options); - this.currentDateString = new Date().toLocaleDateString('en-us', { - year: 'numeric', - month: 'long', - day: 'numeric' - }); } constructUrl() { @@ -129,20 +124,6 @@ class GoogleAgent { return this; } - static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) { - if (tokenizersCache[encoding]) { - return tokenizersCache[encoding]; - } - let tokenizer; - if (isModelName) { - tokenizer = encodingForModel(encoding, extendSpecialTokens); - } else { - tokenizer = getEncoding(encoding, extendSpecialTokens); - } - tokenizersCache[encoding] = tokenizer; - return tokenizer; - } - async getClient() { const scopes = ['https://www.googleapis.com/auth/cloud-platform']; const jwtClient = new google.auth.JWT(this.client_email, null, this.private_key, scopes); @@ -157,7 +138,7 @@ class GoogleAgent { return jwtClient; } - buildPayload(input, { messages = [] }) { + buildMessages(input, { messages = [] }) { let payload = { instances: [ { @@ -184,7 +165,7 @@ class GoogleAgent { } if (this.options.debug) { - console.debug('buildPayload'); + console.debug('buildMessages'); console.dir(payload, { depth: null }); } @@ -217,83 +198,44 @@ class GoogleAgent { } const client = await this.getClient(); - const payload = this.buildPayload(input, { messages }); + const payload = this.buildMessages(input, { messages }); const res = await client.request({ url, method: 'POST', data: payload }); console.dir(res.data, { depth: null }); return res.data; } - async loadHistory(conversationId, parentMessageId = null) { - if (this.options.debug) { - console.debug('Loading history for conversation', conversationId, parentMessageId); - } - - if (!parentMessageId) { - return []; - } - - const messages = (await getMessages({ conversationId })) || []; - - if (messages.length === 0) { - this.currentMessages = []; - return []; - } - - const orderedMessages = this.constructor.getMessagesForConversation(messages, parentMessageId); - return orderedMessages.map((message) => { - return { - author: message.isCreatedByUser ? this.userLabel : this.modelLabel, - content: message.content - }; - }); + getMessageMapMethod() { + return ((message) => ({ + author: message.isCreatedByUser ? this.userLabel : this.modelLabel, + content: message?.content ?? message.text + })).bind(this); } - async saveMessageToDatabase(message, user = null) { - await saveMessage({ ...message, unfinished: false }); - await saveConvo(user, { - conversationId: message.conversationId, - endpoint: 'google', + getSaveOptions() { + return { ...this.modelOptions - }); + }; + } + + getBuildMessagesOptions() { + console.log('GoogleClient doesn\'t use getBuildMessagesOptions'); } async sendMessage(message, opts = {}) { - if (opts && typeof opts === 'object') { - this.setOptions(opts); - } - console.log('sendMessage', message, opts); - - const user = opts.user || null; - const conversationId = opts.conversationId || crypto.randomUUID(); - const parentMessageId = opts.parentMessageId || '00000000-0000-0000-0000-000000000000'; - const userMessageId = opts.overrideParentMessageId || crypto.randomUUID(); - const responseMessageId = crypto.randomUUID(); - const messages = await this.loadHistory(conversationId, this.options?.parentMessageId); - - const userMessage = { - messageId: userMessageId, - parentMessageId, + console.log('GoogleClient: sendMessage', message, opts); + const { + user, conversationId, - sender: 'User', - text: message, - isCreatedByUser: true - }; - - if (typeof opts?.getIds === 'function') { - opts.getIds({ - userMessage, - conversationId, - responseMessageId - }); - } - - console.log('userMessage', userMessage); + responseMessageId, + saveOptions, + userMessage, + } = await this.handleStartMethods(message, opts); - await this.saveMessageToDatabase(userMessage, user); + await this.saveMessageToDatabase(userMessage, saveOptions, user); let reply = ''; let blocked = false; try { - const result = await this.getCompletion(message, messages, opts.abortController); + const result = await this.getCompletion(message, this.currentMessages, opts.abortController); blocked = result?.predictions?.[0]?.safetyAttributes?.blocked; reply = result?.predictions?.[0]?.candidates?.[0]?.content || @@ -318,80 +260,40 @@ class GoogleAgent { } if (!blocked) { - const textStream = new TextStream(reply, { delay: 0.5 }); - await textStream.processTextStream(opts.onProgress); + await this.generateTextStream(reply, opts.onProgress, { delay: 0.5 }); } const responseMessage = { messageId: responseMessageId, conversationId, parentMessageId: userMessage.messageId, - sender: 'PaLM2', + sender: this.sender, text: reply, error: blocked, isCreatedByUser: false }; - await this.saveMessageToDatabase(responseMessage, user); + await this.saveMessageToDatabase(responseMessage, saveOptions, user); return responseMessage; } - getTokenCount(text) { - return this.gptEncoder.encode(text, 'all').length; - } - - /** - * Algorithm adapted from "6. Counting tokens for chat API calls" of - * https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb - * - * An additional 2 tokens need to be added for metadata after all messages have been counted. - * - * @param {*} message - */ - getTokenCountForMessage(message) { - // Map each property of the message to the number of tokens it contains - const propertyTokenCounts = Object.entries(message).map(([key, value]) => { - // Count the number of tokens in the property value - const numTokens = this.getTokenCount(value); - - // Subtract 1 token if the property key is 'name' - const adjustment = key === 'name' ? 1 : 0; - return numTokens - adjustment; - }); - - // Sum the number of tokens in all properties and add 4 for metadata - return propertyTokenCounts.reduce((a, b) => a + b, 4); - } - - /** - * Iterate through messages, building an array based on the parentMessageId. - * Each message has an id and a parentMessageId. The parentMessageId is the id of the message that this message is a reply to. - * @param messages - * @param parentMessageId - * @returns {*[]} An array containing the messages in the order they should be displayed, starting with the root message. - */ - static getMessagesForConversation(messages, parentMessageId) { - const orderedMessages = []; - let currentMessageId = parentMessageId; - while (currentMessageId) { - // eslint-disable-next-line no-loop-func - const message = messages.find((m) => m.messageId === currentMessageId); - if (!message) { - break; - } - orderedMessages.unshift(message); - currentMessageId = message.parentMessageId; + static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) { + if (tokenizersCache[encoding]) { + return tokenizersCache[encoding]; } - - if (orderedMessages.length === 0) { - return []; + let tokenizer; + if (isModelName) { + tokenizer = encodingForModel(encoding, extendSpecialTokens); + } else { + tokenizer = getEncoding(encoding, extendSpecialTokens); } + tokenizersCache[encoding] = tokenizer; + return tokenizer; + } - return orderedMessages.map((msg) => ({ - isCreatedByUser: msg.isCreatedByUser, - content: msg.text - })); + getTokenCount(text) { + return this.gptEncoder.encode(text, 'all').length; } } -module.exports = GoogleAgent; +module.exports = GoogleClient; diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js new file mode 100644 index 00000000000..44744f0f21e --- /dev/null +++ b/api/app/clients/OpenAIClient.js @@ -0,0 +1,317 @@ +const BaseClient = require('./BaseClient'); +const ChatGPTClient = require('./ChatGPTClient'); +const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('@dqbd/tiktoken'); +const { maxTokensMap, genAzureChatCompletion } = require('../../utils'); + +const tokenizersCache = {}; + +class OpenAIClient extends BaseClient { + constructor(apiKey, options = {}) { + super(apiKey, options); + this.ChatGPTClient = new ChatGPTClient(); + this.buildPrompt = this.ChatGPTClient.buildPrompt.bind(this); + this.getCompletion = this.ChatGPTClient.getCompletion.bind(this); + this.sender = options.sender ?? 'ChatGPT'; + this.contextStrategy = options.contextStrategy ? options.contextStrategy.toLowerCase() : 'discard'; + this.shouldRefineContext = this.contextStrategy === 'refine'; + this.azure = options.azure || false; + if (this.azure) { + this.azureEndpoint = genAzureChatCompletion(this.azure); + } + this.setOptions(options); + } + + setOptions(options) { + if (this.options && !this.options.replaceOptions) { + this.options.modelOptions = { + ...this.options.modelOptions, + ...options.modelOptions, + }; + delete options.modelOptions; + this.options = { + ...this.options, + ...options, + }; + } else { + this.options = options; + } + + if (this.options.openaiApiKey) { + this.apiKey = this.options.openaiApiKey; + } + + const modelOptions = this.options.modelOptions || {}; + if (!this.modelOptions) { + this.modelOptions = { + ...modelOptions, + model: modelOptions.model || 'gpt-3.5-turbo', + temperature: typeof modelOptions.temperature === 'undefined' ? 0.8 : modelOptions.temperature, + top_p: typeof modelOptions.top_p === 'undefined' ? 1 : modelOptions.top_p, + presence_penalty: typeof modelOptions.presence_penalty === 'undefined' ? 1 : modelOptions.presence_penalty, + stop: modelOptions.stop, + }; + } + + this.isChatCompletion = this.options.reverseProxyUrl || this.options.localAI || this.modelOptions.model.startsWith('gpt-'); + this.isChatGptModel = this.isChatCompletion; + if (this.modelOptions.model === 'text-davinci-003') { + this.isChatCompletion = false; + this.isChatGptModel = false; + } + const { isChatGptModel } = this; + this.isUnofficialChatGptModel = this.modelOptions.model.startsWith('text-chat') || this.modelOptions.model.startsWith('text-davinci-002-render'); + this.maxContextTokens = maxTokensMap[this.modelOptions.model] ?? 4095; // 1 less than maximum + this.maxResponseTokens = this.modelOptions.max_tokens || 1024; + this.maxPromptTokens = this.options.maxPromptTokens || (this.maxContextTokens - this.maxResponseTokens); + + if (this.maxPromptTokens + this.maxResponseTokens > this.maxContextTokens) { + throw new Error(`maxPromptTokens + max_tokens (${this.maxPromptTokens} + ${this.maxResponseTokens} = ${this.maxPromptTokens + this.maxResponseTokens}) must be less than or equal to maxContextTokens (${this.maxContextTokens})`); + } + + this.userLabel = this.options.userLabel || 'User'; + this.chatGptLabel = this.options.chatGptLabel || 'ChatGPT'; + + this.setupTokens(); + this.setupTokenizer(); + + if (!this.modelOptions.stop) { + const stopTokens = [this.startToken]; + if (this.endToken && this.endToken !== this.startToken) { + stopTokens.push(this.endToken); + } + stopTokens.push(`\n${this.userLabel}:`); + stopTokens.push('<|diff_marker|>'); + this.modelOptions.stop = stopTokens; + } + + if (this.options.reverseProxyUrl) { + this.completionsUrl = this.options.reverseProxyUrl; + } else if (isChatGptModel) { + this.completionsUrl = 'https://api.openai.com/v1/chat/completions'; + } else { + this.completionsUrl = 'https://api.openai.com/v1/completions'; + } + + if (this.azureEndpoint) { + this.completionsUrl = this.azureEndpoint; + } + + if (this.azureEndpoint && this.options.debug) { + console.debug(`Using Azure endpoint: ${this.azureEndpoint}`, this.azure); + } + + return this; + } + + setupTokens() { + if (this.isChatCompletion) { + this.startToken = '||>'; + this.endToken = ''; + } else if (this.isUnofficialChatGptModel) { + this.startToken = '<|im_start|>'; + this.endToken = '<|im_end|>'; + } else { + this.startToken = '||>'; + this.endToken = ''; + } + } + + setupTokenizer() { + this.encoding = 'text-davinci-003'; + if (this.isChatCompletion) { + this.encoding = 'cl100k_base'; + this.gptEncoder = this.constructor.getTokenizer(this.encoding); + } else if (this.isUnofficialChatGptModel) { + this.gptEncoder = this.constructor.getTokenizer(this.encoding, true, { + '<|im_start|>': 100264, + '<|im_end|>': 100265, + }); + } else { + try { + this.encoding = this.modelOptions.model; + this.gptEncoder = this.constructor.getTokenizer(this.modelOptions.model, true); + } catch { + this.gptEncoder = this.constructor.getTokenizer(this.encoding, true); + } + } + } + + static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) { + if (tokenizersCache[encoding]) { + return tokenizersCache[encoding]; + } + let tokenizer; + if (isModelName) { + tokenizer = encodingForModel(encoding, extendSpecialTokens); + } else { + tokenizer = getEncoding(encoding, extendSpecialTokens); + } + tokenizersCache[encoding] = tokenizer; + return tokenizer; + } + + freeAndResetEncoder() { + try { + if (!this.gptEncoder) { + return; + } + this.gptEncoder.free(); + delete tokenizersCache[this.encoding]; + delete tokenizersCache.count; + this.setupTokenizer(); + } catch (error) { + console.log('freeAndResetEncoder error'); + console.error(error); + } + } + + getTokenCount(text) { + try { + if (tokenizersCache.count >= 25) { + if (this.options.debug) { + console.debug('freeAndResetEncoder: reached 25 encodings, reseting...'); + } + this.freeAndResetEncoder(); + } + tokenizersCache.count = (tokenizersCache.count || 0) + 1; + return this.gptEncoder.encode(text, 'all').length; + } catch (error) { + this.freeAndResetEncoder(); + return this.gptEncoder.encode(text, 'all').length; + } + } + + getSaveOptions() { + return { + chatGptLabel: this.options.chatGptLabel, + promptPrefix: this.options.promptPrefix, + ...this.modelOptions + }; + } + + getBuildMessagesOptions(opts) { + return { + isChatCompletion: this.isChatCompletion, + promptPrefix: opts.promptPrefix, + abortController: opts.abortController, + }; + } + + async buildMessages(messages, parentMessageId, { isChatCompletion = false, promptPrefix = null }) { + if (!isChatCompletion) { + return await this.buildPrompt(messages, parentMessageId, { isChatGptModel: isChatCompletion, promptPrefix }); + } + + let payload; + let instructions; + let tokenCountMap; + let promptTokens; + let orderedMessages = this.constructor.getMessagesForConversation(messages, parentMessageId); + + promptPrefix = (promptPrefix || this.options.promptPrefix || '').trim(); + if (promptPrefix) { + promptPrefix = `Instructions:\n${promptPrefix}`; + instructions = { + role: 'system', + name: 'instructions', + content: promptPrefix + }; + + if (this.contextStrategy) { + instructions.tokenCount = this.getTokenCountForMessage(instructions); + } + } + + const formattedMessages = orderedMessages.map((message) => { + let { role: _role, sender, text } = message; + const role = _role ?? sender; + const content = text ?? ''; + const formattedMessage = { + role: role?.toLowerCase() === 'user' ? 'user' : 'assistant', + content, + }; + + if (this.options?.name && formattedMessage.role === 'user') { + formattedMessage.name = this.options.name; + } + + if (this.contextStrategy) { + formattedMessage.tokenCount = message.tokenCount ?? this.getTokenCountForMessage(formattedMessage); + } + + return formattedMessage; + }); + + // TODO: need to handle interleaving instructions better + if (this.contextStrategy) { + ({ payload, tokenCountMap, promptTokens, messages } = await this.handleContextStrategy({instructions, orderedMessages, formattedMessages})); + } + + const result = { + prompt: payload, + promptTokens, + messages, + }; + + if (tokenCountMap) { + tokenCountMap.instructions = instructions?.tokenCount; + result.tokenCountMap = tokenCountMap; + } + + return result; + } + + async sendCompletion(payload, opts = {}) { + let reply = ''; + let result = null; + if (typeof opts.onProgress === 'function') { + await this.getCompletion( + payload, + (progressMessage) => { + if (progressMessage === '[DONE]') { + return; + } + const token = this.isChatCompletion ? progressMessage.choices?.[0]?.delta?.content : progressMessage.choices?.[0]?.text; + // first event's delta content is always undefined + if (!token) { + return; + } + if (this.options.debug) { + // console.debug(token); + } + if (token === this.endToken) { + return; + } + opts.onProgress(token); + reply += token; + }, + opts.abortController || new AbortController(), + ); + } else { + result = await this.getCompletion( + payload, + null, + opts.abortController || new AbortController(), + ); + if (this.options.debug) { + console.debug(JSON.stringify(result)); + } + if (this.isChatCompletion) { + reply = result.choices[0].message.content; + } else { + reply = result.choices[0].text.replace(this.endToken, ''); + } + } + + return reply.trim(); + } + + getTokenCountForResponse(response) { + return this.getTokenCountForMessage({ + role: 'assistant', + content: response.text, + }); + } +} + +module.exports = OpenAIClient; diff --git a/api/app/clients/PluginsClient.js b/api/app/clients/PluginsClient.js new file mode 100644 index 00000000000..6b2dc93da88 --- /dev/null +++ b/api/app/clients/PluginsClient.js @@ -0,0 +1,554 @@ +const OpenAIClient = require('./OpenAIClient'); +const { ChatOpenAI } = require('langchain/chat_models/openai'); +const { CallbackManager } = require('langchain/callbacks'); +const { initializeCustomAgent, initializeFunctionsAgent } = require('./agents/'); +const { loadTools } = require('./tools/util'); +const { SelfReflectionTool } = require('./tools/'); +const { HumanChatMessage, AIChatMessage } = require('langchain/schema'); +const { + instructions, + imageInstructions, + errorInstructions, +} = require('./prompts/instructions'); + +class PluginsClient extends OpenAIClient { + constructor(apiKey, options = {}) { + super(apiKey, options); + this.sender = options.sender ?? 'Assistant'; + this.tools = []; + this.actions = []; + this.openAIApiKey = apiKey; + this.setOptions(options); + this.executor = null; + } + + getActions(input = null) { + let output = 'Internal thoughts & actions taken:\n"'; + let actions = input || this.actions; + + if (actions[0]?.action && this.functionsAgent) { + actions = actions.map((step) => ({ + log: `Action: ${step.action?.tool || ''}\nInput: ${JSON.stringify(step.action?.toolInput) || ''}\nObservation: ${step.observation}` + })); + } else if (actions[0]?.action) { + actions = actions.map((step) => ({ + log: `${step.action.log}\nObservation: ${step.observation}` + })); + } + + actions.forEach((actionObj, index) => { + output += `${actionObj.log}`; + if (index < actions.length - 1) { + output += '\n'; + } + }); + + return output + '"'; + } + + buildErrorInput(message, errorMessage) { + const log = errorMessage.includes('Could not parse LLM output:') + ? `A formatting error occurred with your response to the human's last message. You didn't follow the formatting instructions. Remember to ${instructions}` + : `You encountered an error while replying to the human's last message. Attempt to answer again or admit an answer cannot be given.\nError: ${errorMessage}`; + + return ` + ${log} + + ${this.getActions()} + + Human's last message: ${message} + `; + } + + buildPromptPrefix(result, message) { + if ((result.output && result.output.includes('N/A')) || result.output === undefined) { + return null; + } + + if ( + result?.intermediateSteps?.length === 1 && + result?.intermediateSteps[0]?.action?.toolInput === 'N/A' + ) { + return null; + } + + const internalActions = + result?.intermediateSteps?.length > 0 + ? this.getActions(result.intermediateSteps) + : 'Internal Actions Taken: None'; + + const toolBasedInstructions = internalActions.toLowerCase().includes('image') + ? imageInstructions + : ''; + + const errorMessage = result.errorMessage ? `${errorInstructions} ${result.errorMessage}\n` : ''; + + const preliminaryAnswer = + result.output?.length > 0 ? `Preliminary Answer: "${result.output.trim()}"` : ''; + const prefix = preliminaryAnswer + ? `review and improve the answer you generated using plugins in response to the User Message below. The user hasn't seen your answer or thoughts yet.` + : 'respond to the User Message below based on your preliminary thoughts & actions.'; + + return `As a helpful AI Assistant, ${prefix}${errorMessage}\n${internalActions} +${preliminaryAnswer} +Reply conversationally to the User based on your ${ + preliminaryAnswer ? 'preliminary answer, ' : '' +}internal actions, thoughts, and observations, making improvements wherever possible, but do not modify URLs. +${ + preliminaryAnswer + ? '' + : '\nIf there is an incomplete thought or action, you are expected to complete it in your response now.\n' +}You must cite sources if you are using any web links. ${toolBasedInstructions} +Only respond with your conversational reply to the following User Message: +"${message}"`; + } + + setOptions(options) { + this.agentOptions = options.agentOptions; + this.functionsAgent = this.agentOptions?.agent === 'functions'; + this.agentIsGpt3 = this.agentOptions?.model.startsWith('gpt-3'); + if (this.functionsAgent && this.agentOptions.model) { + this.agentOptions.model = this.getFunctionModelName(this.agentOptions.model); + } + + super.setOptions(options); + this.isGpt3 = this.modelOptions.model.startsWith('gpt-3'); + + if (this.reverseProxyUrl) { + this.langchainProxy = this.reverseProxyUrl.match(/.*v1/)[0]; + } + } + + getSaveOptions() { + return { + chatGptLabel: this.options.chatGptLabel, + promptPrefix: this.options.promptPrefix, + ...this.modelOptions, + agentOptions: this.agentOptions, + }; + } + + saveLatestAction(action) { + this.actions.push(action); + } + + getFunctionModelName(input) { + const prefixMap = { + 'gpt-4': 'gpt-4-0613', + 'gpt-4-32k': 'gpt-4-32k-0613', + 'gpt-3.5-turbo': 'gpt-3.5-turbo-0613' + }; + + const prefix = Object.keys(prefixMap).find(key => input.startsWith(key)); + return prefix ? prefixMap[prefix] : 'gpt-3.5-turbo-0613'; + } + + getBuildMessagesOptions(opts) { + return { + isChatCompletion: true, + promptPrefix: opts.promptPrefix, + abortController: opts.abortController, + }; + } + + createLLM(modelOptions, configOptions) { + let credentials = { openAIApiKey: this.openAIApiKey }; + if (this.azure) { + credentials = { ...this.azure }; + } + + if (this.options.debug) { + console.debug('createLLM: configOptions'); + console.debug(configOptions); + } + + return new ChatOpenAI({ credentials, ...modelOptions }, configOptions); + } + + async initialize({ user, message, onAgentAction, onChainEnd, signal }) { + const modelOptions = { + modelName: this.agentOptions.model, + temperature: this.agentOptions.temperature + }; + + const configOptions = {}; + + if (this.langchainProxy) { + configOptions.basePath = this.langchainProxy; + } + + const model = this.createLLM(modelOptions, configOptions); + + if (this.options.debug) { + console.debug(`<-----Agent Model: ${model.modelName} | Temp: ${model.temperature}----->`); + } + + this.availableTools = await loadTools({ + user, + model, + tools: this.options.tools, + functions: this.functionsAgent, + options: { + openAIApiKey: this.openAIApiKey + } + }); + // load tools + for (const tool of this.options.tools) { + const validTool = this.availableTools[tool]; + + if (tool === 'plugins') { + const plugins = await validTool(); + this.tools = [...this.tools, ...plugins]; + } else if (validTool) { + this.tools.push(await validTool()); + } + } + + if (this.options.debug) { + console.debug('Requested Tools'); + console.debug(this.options.tools); + console.debug('Loaded Tools'); + console.debug(this.tools.map((tool) => tool.name)); + } + + if (this.tools.length > 0 && !this.functionsAgent) { + this.tools.push(new SelfReflectionTool({ message, isGpt3: false })); + } else if (this.tools.length === 0) { + return; + } + + const handleAction = (action, callback = null) => { + this.saveLatestAction(action); + + if (this.options.debug) { + console.debug('Latest Agent Action ', this.actions[this.actions.length - 1]); + } + + if (typeof callback === 'function') { + callback(action); + } + }; + + // Map Messages to Langchain format + const pastMessages = this.currentMessages.map( + msg => msg?.isCreatedByUser || msg?.role?.toLowerCase() === 'user' + ? new HumanChatMessage(msg.text) + : new AIChatMessage(msg.text)); + + if (this.options.debug) { + console.debug('Current Messages'); + console.debug(this.currentMessages); + console.debug('Past Messages'); + console.debug(pastMessages); + } + + // initialize agent + const initializer = this.functionsAgent ? initializeFunctionsAgent : initializeCustomAgent; + this.executor = await initializer({ + model, + signal, + pastMessages, + tools: this.tools, + currentDateString: this.currentDateString, + verbose: this.options.debug, + returnIntermediateSteps: true, + callbackManager: CallbackManager.fromHandlers({ + async handleAgentAction(action) { + handleAction(action, onAgentAction); + }, + async handleChainEnd(action) { + if (typeof onChainEnd === 'function') { + onChainEnd(action); + } + } + }) + }); + + if (this.options.debug) { + console.debug('Loaded agent.'); + } + } + + async executorCall(message, signal) { + let errorMessage = ''; + const maxAttempts = 1; + + for (let attempts = 1; attempts <= maxAttempts; attempts++) { + const errorInput = this.buildErrorInput(message, errorMessage); + const input = attempts > 1 ? errorInput : message; + + if (this.options.debug) { + console.debug(`Attempt ${attempts} of ${maxAttempts}`); + } + + if (this.options.debug && errorMessage.length > 0) { + console.debug('Caught error, input:', input); + } + + try { + this.result = await this.executor.call({ input, signal }); + break; // Exit the loop if the function call is successful + } catch (err) { + console.error(err); + errorMessage = err.message; + if (attempts === maxAttempts) { + this.result.output = `Encountered an error while attempting to respond. Error: ${err.message}`; + this.result.intermediateSteps = this.actions; + this.result.errorMessage = errorMessage; + break; + } + } + } + } + + addImages(intermediateSteps, responseMessage) { + if (!intermediateSteps || !responseMessage) { + return; + } + + intermediateSteps.forEach(step => { + const { observation } = step; + if (!observation || !observation.includes('![')) { + return; + } + + if (!responseMessage.text.includes(observation)) { + responseMessage.text += '\n' + observation; + if (this.options.debug) { + console.debug('added image from intermediateSteps'); + } + } + }); + } + + async handleResponseMessage(responseMessage, saveOptions, user) { + responseMessage.tokenCount = this.getTokenCountForResponse(responseMessage); + responseMessage.completionTokens = responseMessage.tokenCount; + await this.saveMessageToDatabase(responseMessage, saveOptions, user); + delete responseMessage.tokenCount; + return { ...responseMessage, ...this.result }; + } + + async sendMessage(message, opts = {}) { + const completionMode = this.options.tools.length === 0; + if (completionMode) { + this.setOptions(opts); + return super.sendMessage(message, opts); + } + console.log('Plugins sendMessage', message, opts); + const { + user, + conversationId, + responseMessageId, + saveOptions, + userMessage, + onAgentAction, + onChainEnd, + } = await this.handleStartMethods(message, opts); + + let { prompt: payload, tokenCountMap, promptTokens, messages } = await this.buildMessages( + this.currentMessages, + userMessage.messageId, + this.getBuildMessagesOptions({ + promptPrefix: null, + abortController: this.abortController, + }), + ); + + if (this.options.debug) { + console.debug('buildMessages: Messages'); + console.debug(messages); + } + + if (tokenCountMap) { + payload = payload.map((message, i) => { + const { tokenCount, ...messageWithoutTokenCount } = message; + // userMessage is always the last one in the payload + if (i === payload.length - 1) { + userMessage.tokenCount = message.tokenCount; + console.debug(`Token count for user message: ${tokenCount}`, `Instruction Tokens: ${tokenCountMap.instructions || 'N/A'}`); + } + return messageWithoutTokenCount; + }); + this.handleTokenCountMap(tokenCountMap); + } + + this.result = {}; + if (messages) { + this.currentMessages = messages; + } + await this.saveMessageToDatabase(userMessage, saveOptions, user); + const responseMessage = { + messageId: responseMessageId, + conversationId, + parentMessageId: userMessage.messageId, + isCreatedByUser: false, + model: this.modelOptions.model, + sender: this.sender, + promptTokens, + }; + + await this.initialize({ + user, + message, + onAgentAction, + onChainEnd, + signal: this.abortController.signal + }); + await this.executorCall(message, this.abortController.signal); + + // If message was aborted mid-generation + if (this.result?.errorMessage?.length > 0 && this.result?.errorMessage?.includes('cancel')) { + responseMessage.text = 'Cancelled.'; + return await this.handleResponseMessage(responseMessage, saveOptions, user); + } + + if (this.agentOptions.skipCompletion && this.result.output) { + responseMessage.text = this.result.output; + this.addImages(this.result.intermediateSteps, responseMessage); + await this.generateTextStream(this.result.output, opts.onProgress); + return await this.handleResponseMessage(responseMessage, saveOptions, user); + } + + if (this.options.debug) { + console.debug('Plugins completion phase: this.result'); + console.debug(this.result); + } + + const promptPrefix = this.buildPromptPrefix(this.result, message); + + if (this.options.debug) { + console.debug('Plugins: promptPrefix'); + console.debug(promptPrefix); + } + + payload = await this.buildCompletionPrompt({ + messages: this.currentMessages, + promptPrefix, + }); + + if (this.options.debug) { + console.debug('buildCompletionPrompt Payload'); + console.debug(payload); + } + responseMessage.text = await this.sendCompletion(payload, opts); + return await this.handleResponseMessage(responseMessage, saveOptions, user); + } + + async buildCompletionPrompt({ messages, promptPrefix: _promptPrefix }) { + if (this.options.debug) { + console.debug('buildCompletionPrompt messages', messages); + } + + const orderedMessages = messages; + let promptPrefix = _promptPrefix.trim(); + // If the prompt prefix doesn't end with the end token, add it. + if (!promptPrefix.endsWith(`${this.endToken}`)) { + promptPrefix = `${promptPrefix.trim()}${this.endToken}\n\n`; + } + promptPrefix = `${this.startToken}Instructions:\n${promptPrefix}`; + const promptSuffix = `${this.startToken}${this.chatGptLabel ?? 'Assistant'}:\n`; + + const instructionsPayload = { + role: 'system', + name: 'instructions', + content: promptPrefix + }; + + const messagePayload = { + role: 'system', + content: promptSuffix + }; + + if (this.isGpt3) { + instructionsPayload.role = 'user'; + messagePayload.role = 'user'; + instructionsPayload.content += `\n${promptSuffix}`; + } + + // testing if this works with browser endpoint + if (!this.isGpt3 && this.reverseProxyUrl) { + instructionsPayload.role = 'user'; + } + + let currentTokenCount = + this.getTokenCountForMessage(instructionsPayload) + + this.getTokenCountForMessage(messagePayload); + + let promptBody = ''; + const maxTokenCount = this.maxPromptTokens; + + // Iterate backwards through the messages, adding them to the prompt until we reach the max token count. + // Do this within a recursive async function so that it doesn't block the event loop for too long. + const buildPromptBody = async () => { + if (currentTokenCount < maxTokenCount && orderedMessages.length > 0) { + const message = orderedMessages.pop(); + // const roleLabel = message.role === 'User' ? this.userLabel : this.chatGptLabel; + const roleLabel = message.role; + let messageString = `${this.startToken}${roleLabel}:\n${message.text}${this.endToken}\n`; + let newPromptBody; + if (promptBody) { + newPromptBody = `${messageString}${promptBody}`; + } else { + // Always insert prompt prefix before the last user message, if not gpt-3.5-turbo. + // This makes the AI obey the prompt instructions better, which is important for custom instructions. + // After a bunch of testing, it doesn't seem to cause the AI any confusion, even if you ask it things + // like "what's the last thing I wrote?". + newPromptBody = `${promptPrefix}${messageString}${promptBody}`; + } + + const tokenCountForMessage = this.getTokenCount(messageString); + const newTokenCount = currentTokenCount + tokenCountForMessage; + if (newTokenCount > maxTokenCount) { + if (promptBody) { + // This message would put us over the token limit, so don't add it. + return false; + } + // This is the first message, so we can't add it. Just throw an error. + throw new Error( + `Prompt is too long. Max token count is ${maxTokenCount}, but prompt is ${newTokenCount} tokens long.` + ); + } + promptBody = newPromptBody; + currentTokenCount = newTokenCount; + // wait for next tick to avoid blocking the event loop + await new Promise((resolve) => setTimeout(resolve, 0)); + return buildPromptBody(); + } + return true; + }; + + await buildPromptBody(); + const prompt = promptBody; + messagePayload.content = prompt; + // Add 2 tokens for metadata after all messages have been counted. + currentTokenCount += 2; + + if (this.isGpt3 && messagePayload.content.length > 0) { + const context = `Chat History:\n`; + messagePayload.content = `${context}${prompt}`; + currentTokenCount += this.getTokenCount(context); + } + + // Use up to `this.maxContextTokens` tokens (prompt + response), but try to leave `this.maxTokens` tokens for the response. + this.modelOptions.max_tokens = Math.min( + this.maxContextTokens - currentTokenCount, + this.maxResponseTokens + ); + + if (this.isGpt3) { + messagePayload.content += promptSuffix; + return [instructionsPayload, messagePayload]; + } + + const result = [messagePayload, instructionsPayload]; + + if (this.functionsAgent && !this.isGpt3) { + result[1].content = `${result[1].content}\nSure thing! Here is the output you requested:\n`; + } + + return result.filter((message) => message.content.length > 0); + } +} + +module.exports = PluginsClient; diff --git a/api/app/stream.js b/api/app/clients/TextStream.js similarity index 100% rename from api/app/stream.js rename to api/app/clients/TextStream.js diff --git a/api/app/langchain/agents/CustomAgent/CustomAgent.js b/api/app/clients/agents/CustomAgent/CustomAgent.js similarity index 100% rename from api/app/langchain/agents/CustomAgent/CustomAgent.js rename to api/app/clients/agents/CustomAgent/CustomAgent.js diff --git a/api/app/langchain/agents/CustomAgent/initializeCustomAgent.js b/api/app/clients/agents/CustomAgent/initializeCustomAgent.js similarity index 100% rename from api/app/langchain/agents/CustomAgent/initializeCustomAgent.js rename to api/app/clients/agents/CustomAgent/initializeCustomAgent.js diff --git a/api/app/langchain/agents/CustomAgent/instructions.js b/api/app/clients/agents/CustomAgent/instructions.js similarity index 100% rename from api/app/langchain/agents/CustomAgent/instructions.js rename to api/app/clients/agents/CustomAgent/instructions.js diff --git a/api/app/langchain/agents/CustomAgent/outputParser.js b/api/app/clients/agents/CustomAgent/outputParser.js similarity index 99% rename from api/app/langchain/agents/CustomAgent/outputParser.js rename to api/app/clients/agents/CustomAgent/outputParser.js index a4a252adfaa..d714d01db3b 100644 --- a/api/app/langchain/agents/CustomAgent/outputParser.js +++ b/api/app/clients/agents/CustomAgent/outputParser.js @@ -102,8 +102,8 @@ class CustomOutputParser extends ZeroShotAgentOutputParser { match ); selectedTool = this.getValidTool(selectedTool); - } - + } + if (match && !selectedTool) { console.log( '\n\n<----------------------HIT INVALID TOOL PARSING ERROR---------------------->\n\n', diff --git a/api/app/langchain/agents/Functions/FunctionsAgent.js b/api/app/clients/agents/Functions/FunctionsAgent.js similarity index 100% rename from api/app/langchain/agents/Functions/FunctionsAgent.js rename to api/app/clients/agents/Functions/FunctionsAgent.js diff --git a/api/app/langchain/agents/Functions/initializeFunctionsAgent.js b/api/app/clients/agents/Functions/initializeFunctionsAgent.js similarity index 99% rename from api/app/langchain/agents/Functions/initializeFunctionsAgent.js rename to api/app/clients/agents/Functions/initializeFunctionsAgent.js index dfc33c92627..04382ac959b 100644 --- a/api/app/langchain/agents/Functions/initializeFunctionsAgent.js +++ b/api/app/clients/agents/Functions/initializeFunctionsAgent.js @@ -8,7 +8,7 @@ const initializeFunctionsAgent = async ({ // currentDateString, ...rest }) => { - + const memory = new BufferMemory({ chatHistory: new ChatMessageHistory(pastMessages), memoryKey: 'chat_history', @@ -29,7 +29,6 @@ const initializeFunctionsAgent = async ({ } ); - }; module.exports = initializeFunctionsAgent; diff --git a/api/app/langchain/agents/index.js b/api/app/clients/agents/index.js similarity index 100% rename from api/app/langchain/agents/index.js rename to api/app/clients/agents/index.js diff --git a/api/app/clients/chatgpt-client.js b/api/app/clients/chatgpt-client.js deleted file mode 100644 index 984b491e680..00000000000 --- a/api/app/clients/chatgpt-client.js +++ /dev/null @@ -1,106 +0,0 @@ -require('dotenv').config(); -const { KeyvFile } = require('keyv-file'); -const { genAzureChatCompletion } = require('../../utils/genAzureEndpoints'); -const tiktoken = require('@dqbd/tiktoken'); -const tiktokenModels = require('../../utils/tiktokenModels'); -const encoding_for_model = tiktoken.encoding_for_model; - -const askClient = async ({ - text, - parentMessageId, - conversationId, - model, - oaiApiKey, - chatGptLabel, - promptPrefix, - temperature, - top_p, - presence_penalty, - frequency_penalty, - onProgress, - abortController, - userId -}) => { - const { ChatGPTClient } = await import('@waylaidwanderer/chatgpt-api'); - const store = { - store: new KeyvFile({ filename: './data/cache.json' }) - }; - - const azure = process.env.AZURE_OPENAI_API_KEY ? true : false; - let promptText = 'You are ChatGPT, a large language model trained by OpenAI.'; - if (promptPrefix) { - promptText = promptPrefix; - } - - const maxTokensMap = { - 'gpt-4': 8191, - 'gpt-4-0613': 8191, - 'gpt-4-32k': 32767, - 'gpt-4-32k-0613': 32767, - 'gpt-3.5-turbo': 4095, - 'gpt-3.5-turbo-0613': 4095, - 'gpt-3.5-turbo-0301': 4095, - 'gpt-3.5-turbo-16k': 15999, - }; - - const maxContextTokens = maxTokensMap[model] ?? 4095; // 1 less than maximum - const clientOptions = { - reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null, - azure, - maxContextTokens, - modelOptions: { - model, - temperature, - top_p, - presence_penalty, - frequency_penalty - }, - chatGptLabel, - promptPrefix, - proxy: process.env.PROXY || null - // debug: true - }; - - let apiKey = oaiApiKey ? oaiApiKey : process.env.OPENAI_API_KEY || null; - - if (azure) { - apiKey = oaiApiKey ? oaiApiKey : process.env.AZURE_OPENAI_API_KEY || null; - clientOptions.reverseProxyUrl = genAzureChatCompletion({ - azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, - azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, - azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION - }); - } - - const client = new ChatGPTClient(apiKey, clientOptions, store); - - const options = { - onProgress, - abortController, - ...(parentMessageId && conversationId ? { parentMessageId, conversationId } : {}) - }; - - let usage = {}; - let enc = null; - try { - enc = encoding_for_model(tiktokenModels.has(model) ? model : 'gpt-3.5-turbo'); - usage.prompt_tokens = (enc.encode(promptText)).length + (enc.encode(text)).length; - } catch (e) { - console.log('Error encoding prompt text', e); - } - - const res = await client.sendMessage(text, { ...options, userId }); - - try { - usage.completion_tokens = (enc.encode(res.response)).length; - enc.free(); - usage.total_tokens = usage.prompt_tokens + usage.completion_tokens; - res.usage = usage; - } catch (e) { - console.log('Error encoding response text', e); - } - - return res; -}; - -module.exports = { askClient }; diff --git a/api/app/clients/index.js b/api/app/clients/index.js new file mode 100644 index 00000000000..698a47a75ce --- /dev/null +++ b/api/app/clients/index.js @@ -0,0 +1,15 @@ +const ChatGPTClient = require('./ChatGPTClient'); +const OpenAIClient = require('./OpenAIClient'); +const PluginsClient = require('./PluginsClient'); +const GoogleClient = require('./GoogleClient'); +const TextStream = require('./TextStream'); +const toolUtils = require('./tools/util'); + +module.exports = { + ChatGPTClient, + OpenAIClient, + PluginsClient, + GoogleClient, + TextStream, + ...toolUtils +}; \ No newline at end of file diff --git a/api/app/langchain/instructions.js b/api/app/clients/prompts/instructions.js similarity index 100% rename from api/app/langchain/instructions.js rename to api/app/clients/prompts/instructions.js diff --git a/api/app/clients/prompts/refinePrompt.js b/api/app/clients/prompts/refinePrompt.js new file mode 100644 index 00000000000..ed46021f07c --- /dev/null +++ b/api/app/clients/prompts/refinePrompt.js @@ -0,0 +1,24 @@ +const { PromptTemplate } = require('langchain/prompts'); + +const refinePromptTemplate = `Your job is to produce a final summary of the following conversation. +We have provided an existing summary up to a certain point: "{existing_answer}" +We have the opportunity to refine the existing summary +(only if needed) with some more context below. +------------ +"{text}" +------------ + +Given the new context, refine the original summary of the conversation. +Do note who is speaking in the conversation to give proper context. +If the context isn't useful, return the original summary. + +REFINED CONVERSATION SUMMARY:`; + +const refinePrompt = new PromptTemplate({ + template: refinePromptTemplate, + inputVariables: ["existing_answer", "text"], +}); + +module.exports = { + refinePrompt, +}; \ No newline at end of file diff --git a/api/app/clients/specs/BaseClient.test.js b/api/app/clients/specs/BaseClient.test.js new file mode 100644 index 00000000000..dd5b517d708 --- /dev/null +++ b/api/app/clients/specs/BaseClient.test.js @@ -0,0 +1,371 @@ +const { initializeFakeClient } = require('./FakeClient'); + +jest.mock('../../../lib/db/connectDb'); +jest.mock('../../../models', () => { + return function () { + return { + save: jest.fn(), + deleteConvos: jest.fn(), + getConvo: jest.fn(), + getMessages: jest.fn(), + saveMessage: jest.fn(), + updateMessage: jest.fn(), + saveConvo: jest.fn() + }; + }; +}); + +jest.mock('langchain/text_splitter', () => { + return { + RecursiveCharacterTextSplitter: jest.fn().mockImplementation(() => { + return { createDocuments: jest.fn().mockResolvedValue([]) }; + }), + }; +}); + +jest.mock('langchain/chat_models/openai', () => { + return { + ChatOpenAI: jest.fn().mockImplementation(() => { + return {}; + }), + }; +}); + +jest.mock('langchain/chains', () => { + return { + loadSummarizationChain: jest.fn().mockReturnValue({ + call: jest.fn().mockResolvedValue({ output_text: 'Refined answer' }), + }), + }; +}); + +let parentMessageId; +let conversationId; +const fakeMessages = []; +const userMessage = 'Hello, ChatGPT!'; +const apiKey = 'fake-api-key'; + +describe('BaseClient', () => { + let TestClient; + const options = { + // debug: true, + modelOptions: { + model: 'gpt-3.5-turbo', + temperature: 0, + } + }; + + beforeEach(() => { + TestClient = initializeFakeClient(apiKey, options, fakeMessages); + }); + + test('returns the input messages without instructions when addInstructions() is called with empty instructions', () => { + const messages = [ + { content: 'Hello' }, + { content: 'How are you?' }, + { content: 'Goodbye' }, + ]; + const instructions = ''; + const result = TestClient.addInstructions(messages, instructions); + expect(result).toEqual(messages); + }); + + test('returns the input messages with instructions properly added when addInstructions() is called with non-empty instructions', () => { + const messages = [ + { content: 'Hello' }, + { content: 'How are you?' }, + { content: 'Goodbye' }, + ]; + const instructions = { content: 'Please respond to the question.' }; + const result = TestClient.addInstructions(messages, instructions); + const expected = [ + { content: 'Hello' }, + { content: 'How are you?' }, + { content: 'Please respond to the question.' }, + { content: 'Goodbye' }, + ]; + expect(result).toEqual(expected); + }); + + test('concats messages correctly in concatenateMessages()', () => { + const messages = [ + { name: 'User', content: 'Hello' }, + { name: 'Assistant', content: 'How can I help you?' }, + { name: 'User', content: 'I have a question.' }, + ]; + const result = TestClient.concatenateMessages(messages); + const expected = `User:\nHello\n\nAssistant:\nHow can I help you?\n\nUser:\nI have a question.\n\n`; + expect(result).toBe(expected); + }); + + test('refines messages correctly in refineMessages()', async () => { + const messagesToRefine = [ + { role: 'user', content: 'Hello', tokenCount: 10 }, + { role: 'assistant', content: 'How can I help you?', tokenCount: 20 } + ]; + const remainingContextTokens = 100; + const expectedRefinedMessage = { + role: 'assistant', + content: 'Refined answer', + tokenCount: 14 // 'Refined answer'.length + }; + + const result = await TestClient.refineMessages(messagesToRefine, remainingContextTokens); + expect(result).toEqual(expectedRefinedMessage); + }); + + test('gets messages within token limit (under limit) correctly in getMessagesWithinTokenLimit()', async () => { + TestClient.maxContextTokens = 100; + TestClient.shouldRefineContext = true; + TestClient.refineMessages = jest.fn().mockResolvedValue({ + role: 'assistant', + content: 'Refined answer', + tokenCount: 30 + }); + + const messages = [ + { role: 'user', content: 'Hello', tokenCount: 5 }, + { role: 'assistant', content: 'How can I help you?', tokenCount: 19 }, + { role: 'user', content: 'I have a question.', tokenCount: 18 }, + ]; + const expectedContext = [ + { role: 'user', content: 'Hello', tokenCount: 5 }, // 'Hello'.length + { role: 'assistant', content: 'How can I help you?', tokenCount: 19 }, + { role: 'user', content: 'I have a question.', tokenCount: 18 }, + ]; + const expectedRemainingContextTokens = 58; // 100 - 5 - 19 - 18 + const expectedMessagesToRefine = []; + + const result = await TestClient.getMessagesWithinTokenLimit(messages); + expect(result.context).toEqual(expectedContext); + expect(result.remainingContextTokens).toBe(expectedRemainingContextTokens); + expect(result.messagesToRefine).toEqual(expectedMessagesToRefine); + }); + + test('gets messages within token limit (over limit) correctly in getMessagesWithinTokenLimit()', async () => { + TestClient.maxContextTokens = 50; // Set a lower limit + TestClient.shouldRefineContext = true; + TestClient.refineMessages = jest.fn().mockResolvedValue({ + role: 'assistant', + content: 'Refined answer', + tokenCount: 4 + }); + + const messages = [ + { role: 'user', content: 'I need a coffee, stat!', tokenCount: 30 }, + { role: 'assistant', content: 'Sure, I can help with that.', tokenCount: 30 }, + { role: 'user', content: 'Hello', tokenCount: 5 }, + { role: 'assistant', content: 'How can I help you?', tokenCount: 19 }, + { role: 'user', content: 'I have a question.', tokenCount: 18 }, + ]; + const expectedContext = [ + { role: 'user', content: 'Hello', tokenCount: 5 }, + { role: 'assistant', content: 'How can I help you?', tokenCount: 19 }, + { role: 'user', content: 'I have a question.', tokenCount: 18 }, + ]; + const expectedRemainingContextTokens = 8; // 50 - 18 - 19 - 5 + const expectedMessagesToRefine = [ + { role: 'user', content: 'I need a coffee, stat!', tokenCount: 30 }, + { role: 'assistant', content: 'Sure, I can help with that.', tokenCount: 30 }, + ]; + + const result = await TestClient.getMessagesWithinTokenLimit(messages); + expect(result.context).toEqual(expectedContext); + expect(result.remainingContextTokens).toBe(expectedRemainingContextTokens); + expect(result.messagesToRefine).toEqual(expectedMessagesToRefine); + }); + + test('handles context strategy correctly in handleContextStrategy()', async () => { + TestClient.addInstructions = jest.fn().mockReturnValue([ + { content: 'Hello' }, + { content: 'How can I help you?' }, + { content: 'Please provide more details.' }, + { content: 'I can assist you with that.' } + ]); + TestClient.getMessagesWithinTokenLimit = jest.fn().mockReturnValue({ + context: [ + { content: 'How can I help you?' }, + { content: 'Please provide more details.' }, + { content: 'I can assist you with that.' } + ], + remainingContextTokens: 80, + messagesToRefine: [ + { content: 'Hello' }, + ], + refineIndex: 3, + }); + TestClient.refineMessages = jest.fn().mockResolvedValue({ + role: 'assistant', + content: 'Refined answer', + tokenCount: 30 + }); + TestClient.getTokenCountForResponse = jest.fn().mockReturnValue(40); + + const instructions = { content: 'Please provide more details.' }; + const orderedMessages = [ + { content: 'Hello' }, + { content: 'How can I help you?' }, + { content: 'Please provide more details.' }, + { content: 'I can assist you with that.' } + ]; + const formattedMessages = [ + { content: 'Hello' }, + { content: 'How can I help you?' }, + { content: 'Please provide more details.' }, + { content: 'I can assist you with that.' } + ]; + const expectedResult = { + payload: [ + { + content: 'Refined answer', + role: 'assistant', + tokenCount: 30 + }, + { content: 'How can I help you?' }, + { content: 'Please provide more details.' }, + { content: 'I can assist you with that.' } + ], + promptTokens: expect.any(Number), + tokenCountMap: {}, + messages: expect.any(Array), + }; + + const result = await TestClient.handleContextStrategy({ + instructions, + orderedMessages, + formattedMessages, + }); + expect(result).toEqual(expectedResult); + }); + + describe('sendMessage', () => { + test('sendMessage should return a response message', async () => { + const expectedResult = expect.objectContaining({ + sender: TestClient.sender, + text: expect.any(String), + isCreatedByUser: false, + messageId: expect.any(String), + parentMessageId: expect.any(String), + conversationId: expect.any(String) + }); + + const response = await TestClient.sendMessage(userMessage); + parentMessageId = response.messageId; + conversationId = response.conversationId; + expect(response).toEqual(expectedResult); + }); + + test('sendMessage should work with provided conversationId and parentMessageId', async () => { + const userMessage = 'Second message in the conversation'; + const opts = { + conversationId, + parentMessageId, + getIds: jest.fn(), + onStart: jest.fn() + }; + + const expectedResult = expect.objectContaining({ + sender: TestClient.sender, + text: expect.any(String), + isCreatedByUser: false, + messageId: expect.any(String), + parentMessageId: expect.any(String), + conversationId: opts.conversationId + }); + + const response = await TestClient.sendMessage(userMessage, opts); + parentMessageId = response.messageId; + expect(response.conversationId).toEqual(conversationId); + expect(response).toEqual(expectedResult); + expect(opts.getIds).toHaveBeenCalled(); + expect(opts.onStart).toHaveBeenCalled(); + expect(TestClient.getBuildMessagesOptions).toHaveBeenCalled(); + expect(TestClient.getSaveOptions).toHaveBeenCalled(); + }); + + test('should return chat history', async () => { + const chatMessages = await TestClient.loadHistory(conversationId, parentMessageId); + expect(TestClient.currentMessages).toHaveLength(4); + expect(chatMessages[0].text).toEqual(userMessage); + }); + + test('setOptions is called with the correct arguments', async () => { + TestClient.setOptions = jest.fn(); + const opts = { conversationId: '123', parentMessageId: '456' }; + await TestClient.sendMessage('Hello, world!', opts); + expect(TestClient.setOptions).toHaveBeenCalledWith(opts); + TestClient.setOptions.mockClear(); + }); + + test('loadHistory is called with the correct arguments', async () => { + const opts = { conversationId: '123', parentMessageId: '456' }; + await TestClient.sendMessage('Hello, world!', opts); + expect(TestClient.loadHistory).toHaveBeenCalledWith(opts.conversationId, opts.parentMessageId); + }); + + test('getIds is called with the correct arguments', async () => { + const getIds = jest.fn(); + const opts = { getIds }; + const response = await TestClient.sendMessage('Hello, world!', opts); + expect(getIds).toHaveBeenCalledWith({ + userMessage: expect.objectContaining({ text: 'Hello, world!' }), + conversationId: response.conversationId, + responseMessageId: response.messageId + }); + }); + + test('onStart is called with the correct arguments', async () => { + const onStart = jest.fn(); + const opts = { onStart }; + await TestClient.sendMessage('Hello, world!', opts); + expect(onStart).toHaveBeenCalledWith(expect.objectContaining({ text: 'Hello, world!' })); + }); + + test('saveMessageToDatabase is called with the correct arguments', async () => { + const saveOptions = TestClient.getSaveOptions(); + const user = {}; // Mock user + const opts = { user }; + await TestClient.sendMessage('Hello, world!', opts); + expect(TestClient.saveMessageToDatabase).toHaveBeenCalledWith( + expect.objectContaining({ + sender: expect.any(String), + text: expect.any(String), + isCreatedByUser: expect.any(Boolean), + messageId: expect.any(String), + parentMessageId: expect.any(String), + conversationId: expect.any(String) + }), + saveOptions, + user + ); + }); + + test('sendCompletion is called with the correct arguments', async () => { + const payload = {}; // Mock payload + TestClient.buildMessages.mockReturnValue({ prompt: payload, tokenCountMap: null }); + const opts = {}; + await TestClient.sendMessage('Hello, world!', opts); + expect(TestClient.sendCompletion).toHaveBeenCalledWith(payload, opts); + }); + + test('getTokenCountForResponse is called with the correct arguments', async () => { + const tokenCountMap = {}; // Mock tokenCountMap + TestClient.buildMessages.mockReturnValue({ prompt: [], tokenCountMap }); + TestClient.getTokenCountForResponse = jest.fn(); + const response = await TestClient.sendMessage('Hello, world!', {}); + expect(TestClient.getTokenCountForResponse).toHaveBeenCalledWith(response); + }); + + test('returns an object with the correct shape', async () => { + const response = await TestClient.sendMessage('Hello, world!', {}); + expect(response).toEqual(expect.objectContaining({ + sender: expect.any(String), + text: expect.any(String), + isCreatedByUser: expect.any(Boolean), + messageId: expect.any(String), + parentMessageId: expect.any(String), + conversationId: expect.any(String) + })); + }); + }); +}); diff --git a/api/app/clients/specs/FakeClient.js b/api/app/clients/specs/FakeClient.js new file mode 100644 index 00000000000..94dc90cad8a --- /dev/null +++ b/api/app/clients/specs/FakeClient.js @@ -0,0 +1,185 @@ +const crypto = require('crypto'); +const BaseClient = require('../BaseClient'); +const { maxTokensMap } = require('../../../utils'); + +class FakeClient extends BaseClient { + constructor(apiKey, options = {}) { + super(apiKey, options); + this.sender = 'AI Assistant'; + this.setOptions(options); + } + setOptions(options) { + if (this.options && !this.options.replaceOptions) { + this.options.modelOptions = { + ...this.options.modelOptions, + ...options.modelOptions, + }; + delete options.modelOptions; + this.options = { + ...this.options, + ...options, + }; + } else { + this.options = options; + } + + if (this.options.openaiApiKey) { + this.apiKey = this.options.openaiApiKey; + } + + const modelOptions = this.options.modelOptions || {}; + if (!this.modelOptions) { + this.modelOptions = { + ...modelOptions, + model: modelOptions.model || 'gpt-3.5-turbo', + temperature: typeof modelOptions.temperature === 'undefined' ? 0.8 : modelOptions.temperature, + top_p: typeof modelOptions.top_p === 'undefined' ? 1 : modelOptions.top_p, + presence_penalty: typeof modelOptions.presence_penalty === 'undefined' ? 1 : modelOptions.presence_penalty, + stop: modelOptions.stop, + }; + } + + this.maxContextTokens = maxTokensMap[this.modelOptions.model] ?? 4097; + } + getCompletion() {} + buildMessages() {} + getTokenCount(str) { + return str.length; + } + getTokenCountForMessage(message) { + return message?.content?.length || message.length; + } +} + +const initializeFakeClient = (apiKey, options, fakeMessages) => { + let TestClient = new FakeClient(apiKey); + TestClient.options = options; + TestClient.abortController = { abort: jest.fn() }; + TestClient.saveMessageToDatabase = jest.fn(); + TestClient.loadHistory = jest + .fn() + .mockImplementation((conversationId, parentMessageId = null) => { + if (!conversationId) { + TestClient.currentMessages = []; + return Promise.resolve([]); + } + + const orderedMessages = TestClient.constructor.getMessagesForConversation( + fakeMessages, + parentMessageId + ); + + TestClient.currentMessages = orderedMessages; + return Promise.resolve(orderedMessages); + }); + + TestClient.getSaveOptions = jest.fn().mockImplementation(() => { + return {}; + }); + + TestClient.getBuildMessagesOptions = jest.fn().mockImplementation(() => { + return {}; + }); + + TestClient.sendCompletion = jest.fn(async () => { + return 'Mock response text'; + }); + + TestClient.sendMessage = jest.fn().mockImplementation(async (message, opts = {}) => { + if (opts && typeof opts === 'object') { + TestClient.setOptions(opts); + } + + const user = opts.user || null; + const conversationId = opts.conversationId || crypto.randomUUID(); + const parentMessageId = opts.parentMessageId || '00000000-0000-0000-0000-000000000000'; + const userMessageId = opts.overrideParentMessageId || crypto.randomUUID(); + const saveOptions = TestClient.getSaveOptions(); + + this.pastMessages = await TestClient.loadHistory( + conversationId, + TestClient.options?.parentMessageId + ); + + const userMessage = { + text: message, + sender: TestClient.sender, + isCreatedByUser: true, + messageId: userMessageId, + parentMessageId, + conversationId + }; + + const response = { + sender: TestClient.sender, + text: 'Hello, User!', + isCreatedByUser: false, + messageId: crypto.randomUUID(), + parentMessageId: userMessage.messageId, + conversationId + }; + + fakeMessages.push(userMessage); + fakeMessages.push(response); + + if (typeof opts.getIds === 'function') { + opts.getIds({ + userMessage, + conversationId, + responseMessageId: response.messageId + }); + } + + if (typeof opts.onStart === 'function') { + opts.onStart(userMessage); + } + + let { prompt: payload, tokenCountMap } = await TestClient.buildMessages( + this.currentMessages, + userMessage.messageId, + TestClient.getBuildMessagesOptions(opts), + ); + + if (tokenCountMap) { + payload = payload.map((message, i) => { + const { tokenCount, ...messageWithoutTokenCount } = message; + // userMessage is always the last one in the payload + if (i === payload.length - 1) { + userMessage.tokenCount = message.tokenCount; + console.debug(`Token count for user message: ${tokenCount}`, `Instruction Tokens: ${tokenCountMap.instructions || 'N/A'}`); + } + return messageWithoutTokenCount; + }); + TestClient.handleTokenCountMap(tokenCountMap); + } + + await TestClient.saveMessageToDatabase(userMessage, saveOptions, user); + response.text = await TestClient.sendCompletion(payload, opts); + if (tokenCountMap && TestClient.getTokenCountForResponse) { + response.tokenCount = TestClient.getTokenCountForResponse(response); + } + await TestClient.saveMessageToDatabase(response, saveOptions, user); + return response; + }); + + TestClient.buildMessages = jest.fn(async (messages, parentMessageId) => { + const orderedMessages = TestClient.constructor.getMessagesForConversation(messages, parentMessageId); + const formattedMessages = orderedMessages.map((message) => { + let { role: _role, sender, text } = message; + const role = _role ?? sender; + const content = text ?? ''; + return { + role: role?.toLowerCase() === 'user' ? 'user' : 'assistant', + content, + }; + }); + return { + prompt: formattedMessages, + tokenCountMap: null, // Simplified for the mock + }; + }); + + return TestClient; +} + +module.exports = { FakeClient, initializeFakeClient }; \ No newline at end of file diff --git a/api/app/clients/specs/OpenAIClient.test.js b/api/app/clients/specs/OpenAIClient.test.js new file mode 100644 index 00000000000..f1c2fcff906 --- /dev/null +++ b/api/app/clients/specs/OpenAIClient.test.js @@ -0,0 +1,160 @@ +const OpenAIClient = require('../OpenAIClient'); + +describe('OpenAIClient', () => { + let client; + const model = 'gpt-4'; + const parentMessageId = '1'; + const messages = [ + { role: 'user', sender: 'User', text: 'Hello', messageId: parentMessageId}, + { role: 'assistant', sender: 'Assistant', text: 'Hi', messageId: '2' }, + ]; + + beforeEach(() => { + const options = { + // debug: true, + openaiApiKey: 'new-api-key', + modelOptions: { + model, + temperature: 0.7, + }, + }; + client = new OpenAIClient('test-api-key', options); + client.refineMessages = jest.fn().mockResolvedValue({ + role: 'assistant', + content: 'Refined answer', + tokenCount: 30 + }); + }); + + describe('setOptions', () => { + it('should set the options correctly', () => { + expect(client.apiKey).toBe('new-api-key'); + expect(client.modelOptions.model).toBe(model); + expect(client.modelOptions.temperature).toBe(0.7); + }); + }); + + describe('freeAndResetEncoder', () => { + it('should reset the encoder', () => { + client.freeAndResetEncoder(); + expect(client.gptEncoder).toBeDefined(); + }); + }); + + describe('getTokenCount', () => { + it('should return the correct token count', () => { + const count = client.getTokenCount('Hello, world!'); + expect(count).toBeGreaterThan(0); + }); + + it('should reset the encoder and count when count reaches 25', () => { + const freeAndResetEncoderSpy = jest.spyOn(client, 'freeAndResetEncoder'); + + // Call getTokenCount 25 times + for (let i = 0; i < 25; i++) { + client.getTokenCount('test text'); + } + + expect(freeAndResetEncoderSpy).toHaveBeenCalled(); + }); + + it('should not reset the encoder and count when count is less than 25', () => { + const freeAndResetEncoderSpy = jest.spyOn(client, 'freeAndResetEncoder'); + + // Call getTokenCount 24 times + for (let i = 0; i < 24; i++) { + client.getTokenCount('test text'); + } + + expect(freeAndResetEncoderSpy).not.toHaveBeenCalled(); + }); + + it('should handle errors and reset the encoder', () => { + const freeAndResetEncoderSpy = jest.spyOn(client, 'freeAndResetEncoder'); + client.gptEncoder.encode = jest.fn().mockImplementation(() => { + throw new Error('Test error'); + }); + + client.getTokenCount('test text'); + + expect(freeAndResetEncoderSpy).toHaveBeenCalled(); + }); + }); + + describe('getSaveOptions', () => { + it('should return the correct save options', () => { + const options = client.getSaveOptions(); + expect(options).toHaveProperty('chatGptLabel'); + expect(options).toHaveProperty('promptPrefix'); + }); + }); + + describe('getBuildMessagesOptions', () => { + it('should return the correct build messages options', () => { + const options = client.getBuildMessagesOptions({ promptPrefix: 'Hello' }); + expect(options).toHaveProperty('isChatCompletion'); + expect(options).toHaveProperty('promptPrefix'); + expect(options.promptPrefix).toBe('Hello'); + }); + }); + + describe('buildMessages', () => { + it('should build messages correctly for chat completion', async () => { + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true }); + expect(result).toHaveProperty('prompt'); + }); + + it('should build messages correctly for non-chat completion', async () => { + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: false }); + expect(result).toHaveProperty('prompt'); + }); + + it('should build messages correctly with a promptPrefix', async () => { + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true, promptPrefix: 'Test Prefix' }); + expect(result).toHaveProperty('prompt'); + const instructions = result.prompt.find(item => item.name === 'instructions'); + expect(instructions).toBeDefined(); + expect(instructions.content).toContain('Test Prefix'); + }); + + it('should handle context strategy correctly', async () => { + client.contextStrategy = 'refine'; + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true }); + expect(result).toHaveProperty('prompt'); + expect(result).toHaveProperty('tokenCountMap'); + }); + + it('should assign name property for user messages when options.name is set', async () => { + client.options.name = 'Test User'; + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true }); + const hasUserWithName = result.prompt.some(item => item.role === 'user' && item.name === 'Test User'); + expect(hasUserWithName).toBe(true); + }); + + it('should calculate tokenCount for each message when contextStrategy is set', async () => { + client.contextStrategy = 'refine'; + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true }); + const hasUserWithTokenCount = result.prompt.some(item => item.role === 'user' && item.tokenCount > 0); + expect(hasUserWithTokenCount).toBe(true); + }); + + it('should handle promptPrefix from options when promptPrefix argument is not provided', async () => { + client.options.promptPrefix = 'Test Prefix from options'; + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true }); + const instructions = result.prompt.find(item => item.name === 'instructions'); + expect(instructions.content).toContain('Test Prefix from options'); + }); + + it('should handle case when neither promptPrefix argument nor options.promptPrefix is set', async () => { + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true }); + const instructions = result.prompt.find(item => item.name === 'instructions'); + expect(instructions).toBeUndefined(); + }); + + it('should handle case when getMessagesForConversation returns null or an empty array', async () => { + const messages = []; + const result = await client.buildMessages(messages, parentMessageId, { isChatCompletion: true }); + expect(result.prompt).toEqual([]); + }); + }); +}); diff --git a/api/app/clients/chatgpt-client.tokens.js b/api/app/clients/specs/OpenAIClient.tokens.js similarity index 80% rename from api/app/clients/chatgpt-client.tokens.js rename to api/app/clients/specs/OpenAIClient.tokens.js index 080e2daeaa0..f4f81c1f867 100644 --- a/api/app/clients/chatgpt-client.tokens.js +++ b/api/app/clients/specs/OpenAIClient.tokens.js @@ -1,7 +1,25 @@ +/* + This is a test script to see how much memory is used by the client when encoding. + On my work machine, it was able to process 10,000 encoding requests / 48.686 seconds = approximately 205.4 RPS + I've significantly reduced the amount of encoding needed by saving token counts in the database, so these + numbers should only be hit with a large amount of concurrent users + It would take 103 concurrent users sending 1 message every 1 second to hit these numbers, which is rather unrealistic, + and at that point, out-sourcing the encoding to a separate server would be a better solution + Also, for scaling, could increase the rate at which the encoder resets; the trade-off is more resource usage on the server. + Initial memory usage: 25.93 megabytes + Peak memory usage: 55 megabytes + Final memory usage: 28.03 megabytes + Post-test (timeout of 15s): 21.91 megabytes +*/ + require('dotenv').config(); +const { OpenAIClient } = require('../'); + +function timeout(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} const run = async () => { - const { ChatGPTClient } = await import('@waylaidwanderer/chatgpt-api'); const text = ` The standard Lorem Ipsum passage, used since the 1500s @@ -37,7 +55,6 @@ const run = async () => { // Calculate initial percentage of memory used const initialMemoryUsage = process.memoryUsage().heapUsed; - function printProgressBar(percentageUsed) { const filledBlocks = Math.round(percentageUsed / 2); // Each block represents 2% @@ -46,20 +63,20 @@ const run = async () => { console.log(progressBar); } - const iterations = 16000; + const iterations = 10000; console.time('loopTime'); // Trying to catch the error doesn't help; all future calls will immediately crash for (let i = 0; i < iterations; i++) { try { console.log(`Iteration ${i}`); - const client = new ChatGPTClient(apiKey, clientOptions); + const client = new OpenAIClient(apiKey, clientOptions); client.getTokenCount(text); // const encoder = client.constructor.getTokenizer('cl100k_base'); // console.log(`Iteration ${i}: call encode()...`); // encoder.encode(text, 'all'); // encoder.free(); - + const memoryUsageDuringLoop = process.memoryUsage().heapUsed; const percentageUsed = memoryUsageDuringLoop / maxMemory * 100; printProgressBar(percentageUsed); @@ -80,10 +97,23 @@ const run = async () => { // const finalPercentageUsed = finalMemoryUsage / maxMemory * 100; console.log(`Initial memory usage: ${initialMemoryUsage / 1024 / 1024} megabytes`); console.log(`Final memory usage: ${finalMemoryUsage / 1024 / 1024} megabytes`); - setTimeout(() => { - const memoryUsageAfterTimeout = process.memoryUsage().heapUsed; - console.log(`Post timeout: ${memoryUsageAfterTimeout / 1024 / 1024} megabytes`); - } , 10000); + await timeout(15000); + const memoryUsageAfterTimeout = process.memoryUsage().heapUsed; + console.log(`Post timeout: ${memoryUsageAfterTimeout / 1024 / 1024} megabytes`); } -run(); \ No newline at end of file +run(); + +process.on('uncaughtException', (err) => { + if (!err.message.includes('fetch failed')) { + console.error('There was an uncaught error:'); + console.error(err); + } + + if (err.message.includes('fetch failed')) { + console.log('fetch failed error caught'); + // process.exit(0); + } else { + process.exit(1); + } +}); diff --git a/api/app/langchain/ChatAgent.test.js b/api/app/clients/specs/PluginsClient.test.js similarity index 90% rename from api/app/langchain/ChatAgent.test.js rename to api/app/clients/specs/PluginsClient.test.js index ce18b98ff8a..22936e1e3ae 100644 --- a/api/app/langchain/ChatAgent.test.js +++ b/api/app/clients/specs/PluginsClient.test.js @@ -1,9 +1,9 @@ const { HumanChatMessage, AIChatMessage } = require('langchain/schema'); -const ChatAgent = require('./ChatAgent'); +const PluginsClient = require('../PluginsClient'); const crypto = require('crypto'); -jest.mock('../../lib/db/connectDb'); -jest.mock('../../models/Conversation', () => { +jest.mock('../../../lib/db/connectDb'); +jest.mock('../../../models/Conversation', () => { return function () { return { save: jest.fn(), @@ -12,7 +12,7 @@ jest.mock('../../models/Conversation', () => { }; }); -describe('ChatAgent', () => { +describe('PluginsClient', () => { let TestAgent; let options = { tools: [], @@ -32,7 +32,7 @@ describe('ChatAgent', () => { const apiKey = 'fake-api-key'; beforeEach(() => { - TestAgent = new ChatAgent(apiKey, options); + TestAgent = new PluginsClient(apiKey, options); TestAgent.loadHistory = jest .fn() .mockImplementation((conversationId, parentMessageId = null) => { @@ -45,8 +45,9 @@ describe('ChatAgent', () => { fakeMessages, parentMessageId ); + const chatMessages = orderedMessages.map((msg) => - msg?.isCreatedByUser || msg?.role.toLowerCase() === 'user' + msg?.isCreatedByUser || msg?.role?.toLowerCase() === 'user' ? new HumanChatMessage(msg.text) : new AIChatMessage(msg.text) ); @@ -90,8 +91,8 @@ describe('ChatAgent', () => { }); }); - test('initializes ChatAgent without crashing', () => { - expect(TestAgent).toBeInstanceOf(ChatAgent); + test('initializes PluginsClient without crashing', () => { + expect(TestAgent).toBeInstanceOf(PluginsClient); }); test('check setOptions function', () => { diff --git a/api/app/langchain/tools/AIPluginTool.js b/api/app/clients/tools/AIPluginTool.js similarity index 99% rename from api/app/langchain/tools/AIPluginTool.js rename to api/app/clients/tools/AIPluginTool.js index 2408ae8be8f..e316c020f6f 100644 --- a/api/app/langchain/tools/AIPluginTool.js +++ b/api/app/clients/tools/AIPluginTool.js @@ -10,7 +10,6 @@ export interface AIPluginToolParams { model: BaseLanguageModel; } - export interface PathParameter { name: string; description: string; diff --git a/api/app/langchain/tools/DALL-E.js b/api/app/clients/tools/DALL-E.js similarity index 98% rename from api/app/langchain/tools/DALL-E.js rename to api/app/clients/tools/DALL-E.js index d2a4539a7d9..bfbd3a8bab3 100644 --- a/api/app/langchain/tools/DALL-E.js +++ b/api/app/clients/tools/DALL-E.js @@ -12,7 +12,7 @@ class OpenAICreateImage extends Tool { super(); let apiKey = fields.DALLE_API_KEY || this.getApiKey(); - // let azureKey = fields.AZURE_OPENAI_API_KEY || process.env.AZURE_OPENAI_API_KEY; + // let azureKey = fields.AZURE_API_KEY || process.env.AZURE_API_KEY; let config = { apiKey }; // if (azureKey) { diff --git a/api/app/langchain/tools/GoogleSearch.js b/api/app/clients/tools/GoogleSearch.js similarity index 100% rename from api/app/langchain/tools/GoogleSearch.js rename to api/app/clients/tools/GoogleSearch.js diff --git a/api/app/langchain/tools/HttpRequestTool.js b/api/app/clients/tools/HttpRequestTool.js similarity index 97% rename from api/app/langchain/tools/HttpRequestTool.js rename to api/app/clients/tools/HttpRequestTool.js index d52f73bbf72..c602817d4bb 100644 --- a/api/app/langchain/tools/HttpRequestTool.js +++ b/api/app/clients/tools/HttpRequestTool.js @@ -6,7 +6,7 @@ const { Tool } = require('langchain/tools'); // this.name = 'requests_get'; // this.headers = headers; // this.maxOutputLength = maxOutputLength || 2000; -// this.description = `A portal to the internet. Use this when you need to get specific content from a website. +// this.description = `A portal to the internet. Use this when you need to get specific content from a website. // - Input should be a url (i.e. https://www.google.com). The output will be the text response of the GET request.`; // } @@ -27,7 +27,7 @@ const { Tool } = require('langchain/tools'); // this.maxOutputLength = maxOutputLength || Infinity; // this.description = `Use this when you want to POST to a website. // - Input should be a json string with two keys: "url" and "data". -// - The value of "url" should be a string, and the value of "data" should be a dictionary of +// - The value of "url" should be a string, and the value of "data" should be a dictionary of // - key-value pairs you want to POST to the url as a JSON body. // - Be careful to always use double quotes for strings in the json string // - The output will be the text response of the POST request.`; @@ -63,23 +63,23 @@ class HttpRequestTool extends Tool { const urlPattern = /"url":\s*"([^"]*)"/; const methodPattern = /"method":\s*"([^"]*)"/; const dataPattern = /"data":\s*"([^"]*)"/; - + const url = input.match(urlPattern)[1]; const method = input.match(methodPattern)[1]; let data = input.match(dataPattern)[1]; - + // Parse 'data' back to JSON if possible try { data = JSON.parse(data); } catch (e) { // If it's not a JSON string, keep it as is } - + let options = { method: method, headers: this.headers }; - + if (['POST', 'PUT', 'PATCH'].includes(method.toUpperCase()) && data) { if (typeof data === 'object') { options.body = JSON.stringify(data); @@ -88,20 +88,20 @@ class HttpRequestTool extends Tool { } options.headers['Content-Type'] = 'application/json'; } - + const res = await fetch(url, options); - + const text = await res.text(); if (text.includes(' { try { diff --git a/api/app/langchain/tools/util/handleTools.test.js b/api/app/clients/tools/util/handleTools.test.js similarity index 99% rename from api/app/langchain/tools/util/handleTools.test.js rename to api/app/clients/tools/util/handleTools.test.js index 2c79b700bc7..64ba33c3463 100644 --- a/api/app/langchain/tools/util/handleTools.test.js +++ b/api/app/clients/tools/util/handleTools.test.js @@ -10,7 +10,6 @@ var mockPluginService = { getUserPluginAuthValue: jest.fn() }; - jest.mock('../../../../models/User', () => { return function() { return mockUser; @@ -38,7 +37,7 @@ describe('Tool Handlers', () => { beforeAll(async () => { mockUser.save.mockResolvedValue(undefined); - + const userAuthValues = {}; mockPluginService.getUserPluginAuthValue.mockImplementation((userId, authField) => { return userAuthValues[`${userId}-${authField}`]; @@ -46,7 +45,7 @@ describe('Tool Handlers', () => { mockPluginService.updateUserPluginAuth.mockImplementation((userId, authField, _pluginKey, credential) => { userAuthValues[`${userId}-${authField}`] = credential; }); - + fakeUser = new User({ name: 'Fake User', username: 'fakeuser', @@ -64,7 +63,7 @@ describe('Tool Handlers', () => { for (const authConfig of authConfigs) { await PluginService.updateUserPluginAuth(fakeUser._id, authConfig.authField, pluginKey, mockCredential); } - }); + }); afterAll(async () => { await mockUser.findByIdAndDelete(fakeUser._id); diff --git a/api/app/langchain/tools/util/index.js b/api/app/clients/tools/util/index.js similarity index 100% rename from api/app/langchain/tools/util/index.js rename to api/app/clients/tools/util/index.js diff --git a/api/app/langchain/tools/wolfram-guidelines.md b/api/app/clients/tools/wolfram-guidelines.md similarity index 100% rename from api/app/langchain/tools/wolfram-guidelines.md rename to api/app/clients/tools/wolfram-guidelines.md diff --git a/api/app/index.js b/api/app/index.js index c03b854e6d0..a5cc95fbf0f 100644 --- a/api/app/index.js +++ b/api/app/index.js @@ -1,15 +1,15 @@ -const { askClient } = require('./clients/chatgpt-client'); -const { browserClient } = require('./clients/chatgpt-browser'); -const { askBing } = require('./clients/bingai'); +const { browserClient } = require('./chatgpt-browser'); +const { askBing } = require('./bingai'); +const clients = require('./clients'); const titleConvo = require('./titleConvo'); const getCitations = require('../lib/parse/getCitations'); const citeText = require('../lib/parse/citeText'); module.exports = { - askClient, browserClient, askBing, titleConvo, getCitations, - citeText + citeText, + ...clients }; diff --git a/api/app/langchain/ChatAgent.js b/api/app/langchain/ChatAgent.js deleted file mode 100644 index 9960e85ca63..00000000000 --- a/api/app/langchain/ChatAgent.js +++ /dev/null @@ -1,959 +0,0 @@ -const crypto = require('crypto'); -const { genAzureChatCompletion } = require('../../utils/genAzureEndpoints'); -const { - encoding_for_model: encodingForModel, - get_encoding: getEncoding -} = require('@dqbd/tiktoken'); -const { fetchEventSource } = require('@waylaidwanderer/fetch-event-source'); -const { Agent, ProxyAgent } = require('undici'); -const TextStream = require('../stream'); -const { ChatOpenAI } = require('langchain/chat_models/openai'); -const { CallbackManager } = require('langchain/callbacks'); -const { HumanChatMessage, AIChatMessage } = require('langchain/schema'); -const { initializeCustomAgent, initializeFunctionsAgent } = require('./agents/'); -const { getMessages, saveMessage, saveConvo } = require('../../models'); -const { loadTools } = require('./tools/util'); -const { SelfReflectionTool } = require('./tools/'); -const { - instructions, - imageInstructions, - errorInstructions, - completionInstructions -} = require('./instructions'); - -const tokenizersCache = {}; - -class ChatAgent { - constructor(apiKey, options = {}) { - this.tools = []; - this.actions = []; - this.openAIApiKey = apiKey; - this.azure = options.azure || false; - if (this.azure) { - const { azureOpenAIApiInstanceName, azureOpenAIApiDeploymentName, azureOpenAIApiVersion } = - this.azure; - this.azureEndpoint = genAzureChatCompletion({ - azureOpenAIApiInstanceName, - azureOpenAIApiDeploymentName, - azureOpenAIApiVersion - }); - } - this.setOptions(options); - this.executor = null; - this.currentDateString = new Date().toLocaleDateString('en-us', { - year: 'numeric', - month: 'long', - day: 'numeric' - }); - } - - getActions(input = null) { - let output = 'Internal thoughts & actions taken:\n"'; - let actions = input || this.actions; - - if (actions[0]?.action && this.functionsAgent) { - actions = actions.map((step) => ({ - log: `Action: ${step.action?.tool || ''}\nInput: ${JSON.stringify(step.action?.toolInput) || ''}\nObservation: ${step.observation}` - })); - } else if (actions[0]?.action) { - actions = actions.map((step) => ({ - log: `${step.action.log}\nObservation: ${step.observation}` - })); - } - - actions.forEach((actionObj, index) => { - output += `${actionObj.log}`; - if (index < actions.length - 1) { - output += '\n'; - } - }); - - return output + '"'; - } - - buildErrorInput(message, errorMessage) { - const log = errorMessage.includes('Could not parse LLM output:') - ? `A formatting error occurred with your response to the human's last message. You didn't follow the formatting instructions. Remember to ${instructions}` - : `You encountered an error while replying to the human's last message. Attempt to answer again or admit an answer cannot be given.\nError: ${errorMessage}`; - - return ` - ${log} - - ${this.getActions()} - - Human's last message: ${message} - `; - } - - buildPromptPrefix(result, message) { - if ((result.output && result.output.includes('N/A')) || result.output === undefined) { - return null; - } - - if ( - result?.intermediateSteps?.length === 1 && - result?.intermediateSteps[0]?.action?.toolInput === 'N/A' - ) { - return null; - } - - const internalActions = - result?.intermediateSteps?.length > 0 - ? this.getActions(result.intermediateSteps) - : 'Internal Actions Taken: None'; - - const toolBasedInstructions = internalActions.toLowerCase().includes('image') - ? imageInstructions - : ''; - - const errorMessage = result.errorMessage ? `${errorInstructions} ${result.errorMessage}\n` : ''; - - const preliminaryAnswer = - result.output?.length > 0 ? `Preliminary Answer: "${result.output.trim()}"` : ''; - const prefix = preliminaryAnswer - ? `review and improve the answer you generated using plugins in response to the User Message below. The user hasn't seen your answer or thoughts yet.` - : 'respond to the User Message below based on your preliminary thoughts & actions.'; - - return `As a helpful AI Assistant, ${prefix}${errorMessage}\n${internalActions} -${preliminaryAnswer} -Reply conversationally to the User based on your ${ - preliminaryAnswer ? 'preliminary answer, ' : '' -}internal actions, thoughts, and observations, making improvements wherever possible, but do not modify URLs. -${ - preliminaryAnswer - ? '' - : '\nIf there is an incomplete thought or action, you are expected to complete it in your response now.\n' -}You must cite sources if you are using any web links. ${toolBasedInstructions} -Only respond with your conversational reply to the following User Message: -"${message}"`; - } - - setOptions(options) { - if (this.options && !this.options.replaceOptions) { - // nested options aren't spread properly, so we need to do this manually - this.options.modelOptions = { - ...this.options.modelOptions, - ...options.modelOptions - }; - this.options.agentOptions = { - ...this.options.agentOptions, - ...options.agentOptions - }; - delete options.modelOptions; - delete options.agentOptions; - // now we can merge options - this.options = { - ...this.options, - ...options - }; - } else { - this.options = options; - } - - - const modelOptions = this.options.modelOptions || {}; - this.modelOptions = { - ...modelOptions, - model: modelOptions.model || 'gpt-3.5-turbo', - temperature: typeof modelOptions.temperature === 'undefined' ? 0.8 : modelOptions.temperature, - top_p: typeof modelOptions.top_p === 'undefined' ? 1 : modelOptions.top_p, - presence_penalty: - typeof modelOptions.presence_penalty === 'undefined' ? 0 : modelOptions.presence_penalty, - frequency_penalty: - typeof modelOptions.frequency_penalty === 'undefined' ? 0 : modelOptions.frequency_penalty, - stop: modelOptions.stop - }; - - this.agentOptions = this.options.agentOptions || {}; - this.functionsAgent = this.agentOptions.agent === 'functions'; - this.agentIsGpt3 = this.agentOptions.model.startsWith('gpt-3'); - if (this.functionsAgent) { - this.agentOptions.model = this.getFunctionModelName(this.agentOptions.model); - } - - this.isChatGptModel = this.modelOptions.model.startsWith('gpt-'); - this.isGpt3 = this.modelOptions.model.startsWith('gpt-3'); - const maxTokensMap = { - 'gpt-4': 8191, - 'gpt-4-0613': 8191, - 'gpt-4-32k': 32767, - 'gpt-4-32k-0613': 32767, - 'gpt-3.5-turbo': 4095, - 'gpt-3.5-turbo-0613': 4095, - 'gpt-3.5-turbo-0301': 4095, - 'gpt-3.5-turbo-16k': 15999, - }; - - this.maxContextTokens = maxTokensMap[this.modelOptions.model] ?? 4095; // 1 less than maximum - // Reserve 1024 tokens for the response. - // The max prompt tokens is determined by the max context tokens minus the max response tokens. - // Earlier messages will be dropped until the prompt is within the limit. - this.maxResponseTokens = this.modelOptions.max_tokens || 1024; - this.maxPromptTokens = - this.options.maxPromptTokens || this.maxContextTokens - this.maxResponseTokens; - - if (this.maxPromptTokens + this.maxResponseTokens > this.maxContextTokens) { - throw new Error( - `maxPromptTokens + max_tokens (${this.maxPromptTokens} + ${this.maxResponseTokens} = ${ - this.maxPromptTokens + this.maxResponseTokens - }) must be less than or equal to maxContextTokens (${this.maxContextTokens})` - ); - } - - this.userLabel = this.options.userLabel || 'User'; - this.chatGptLabel = this.options.chatGptLabel || 'Assistant'; - - // Use these faux tokens to help the AI understand the context since we are building the chat log ourselves. - // Trying to use "<|im_start|>" causes the AI to still generate "<" or "<|" at the end sometimes for some reason, - // without tripping the stop sequences, so I'm using "||>" instead. - this.startToken = '||>'; - this.endToken = ''; - this.gptEncoder = this.constructor.getTokenizer('cl100k_base'); - this.completionsUrl = 'https://api.openai.com/v1/chat/completions'; - this.reverseProxyUrl = this.options.reverseProxyUrl || process.env.OPENAI_REVERSE_PROXY; - - if (this.reverseProxyUrl) { - this.completionsUrl = this.reverseProxyUrl; - this.langchainProxy = this.reverseProxyUrl.substring(0, this.reverseProxyUrl.indexOf('v1') + 'v1'.length) - } - - if (this.azureEndpoint) { - this.completionsUrl = this.azureEndpoint; - } - - if (this.azureEndpoint && this.options.debug) { - console.debug(`Using Azure endpoint: ${this.azureEndpoint}`, this.azure); - } - } - - static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) { - if (tokenizersCache[encoding]) { - return tokenizersCache[encoding]; - } - let tokenizer; - if (isModelName) { - tokenizer = encodingForModel(encoding, extendSpecialTokens); - } else { - tokenizer = getEncoding(encoding, extendSpecialTokens); - } - tokenizersCache[encoding] = tokenizer; - return tokenizer; - } - - async getCompletion(input, onProgress, abortController = null) { - if (!abortController) { - abortController = new AbortController(); - } - - const modelOptions = this.modelOptions; - if (typeof onProgress === 'function') { - modelOptions.stream = true; - } - if (this.isChatGptModel) { - modelOptions.messages = input; - } else { - modelOptions.prompt = input; - } - const { debug } = this.options; - const url = this.completionsUrl; - if (debug) { - console.debug(); - console.debug(url); - console.debug(modelOptions); - console.debug(); - } - const opts = { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(modelOptions), - dispatcher: new Agent({ - bodyTimeout: 0, - headersTimeout: 0 - }) - }; - - if (this.azureEndpoint) { - opts.headers['api-key'] = this.azure.azureOpenAIApiKey; - } else if (this.openAIApiKey) { - opts.headers.Authorization = `Bearer ${this.openAIApiKey}`; - } - - if (this.options.proxy) { - opts.dispatcher = new ProxyAgent(this.options.proxy); - } - - if (modelOptions.stream) { - // eslint-disable-next-line no-async-promise-executor - return new Promise(async (resolve, reject) => { - try { - let done = false; - await fetchEventSource(url, { - ...opts, - signal: abortController.signal, - async onopen(response) { - if (response.status === 200) { - return; - } - if (debug) { - // console.debug(response); - } - let error; - try { - const body = await response.text(); - error = new Error(`Failed to send message. HTTP ${response.status} - ${body}`); - error.status = response.status; - error.json = JSON.parse(body); - } catch { - error = error || new Error(`Failed to send message. HTTP ${response.status}`); - } - throw error; - }, - onclose() { - if (debug) { - console.debug('Server closed the connection unexpectedly, returning...'); - } - // workaround for private API not sending [DONE] event - if (!done) { - onProgress('[DONE]'); - abortController.abort(); - resolve(); - } - }, - onerror(err) { - if (debug) { - console.debug(err); - } - // rethrow to stop the operation - throw err; - }, - onmessage(message) { - if (debug) { - // console.debug(message); - } - if (!message.data || message.event === 'ping') { - return; - } - if (message.data === '[DONE]') { - onProgress('[DONE]'); - abortController.abort(); - resolve(); - done = true; - return; - } - onProgress(JSON.parse(message.data)); - } - }); - } catch (err) { - reject(err); - } - }); - } - const response = await fetch(url, { - ...opts, - signal: abortController.signal - }); - if (response.status !== 200) { - const body = await response.text(); - const error = new Error(`Failed to send message. HTTP ${response.status} - ${body}`); - error.status = response.status; - try { - error.json = JSON.parse(body); - } catch { - error.body = body; - } - throw error; - } - return response.json(); - } - - async loadHistory(conversationId, parentMessageId = null) { - if (this.options.debug) { - console.debug('Loading history for conversation', conversationId, parentMessageId); - } - - const messages = (await getMessages({ conversationId })) || []; - - if (messages.length === 0) { - this.currentMessages = []; - return []; - } - - const orderedMessages = this.constructor.getMessagesForConversation(messages, parentMessageId); - // Convert Message documents into appropriate ChatMessage instances - const chatMessages = orderedMessages.map((msg) => - msg?.isCreatedByUser || msg?.role.toLowerCase() === 'user' - ? new HumanChatMessage(msg.text) - : new AIChatMessage(msg.text) - ); - - this.currentMessages = orderedMessages; - - return chatMessages; - } - - async saveMessageToDatabase(message, user = null) { - await saveMessage({ ...message, unfinished: false }); - await saveConvo(user, { - conversationId: message.conversationId, - endpoint: 'gptPlugins', - chatGptLabel: this.options.chatGptLabel, - promptPrefix: this.options.promptPrefix, - ...this.modelOptions, - agentOptions: this.agentOptions - }); - } - - saveLatestAction(action) { - this.actions.push(action); - } - - getFunctionModelName(input) { - const prefixMap = { - 'gpt-4': 'gpt-4-0613', - 'gpt-4-32k': 'gpt-4-32k-0613', - 'gpt-3.5-turbo': 'gpt-3.5-turbo-0613' - }; - - const prefix = Object.keys(prefixMap).find(key => input.startsWith(key)); - return prefix ? prefixMap[prefix] : 'gpt-3.5-turbo-0613'; - } - - createLLM(modelOptions, configOptions) { - let credentials = { openAIApiKey: this.openAIApiKey }; - if (this.azure) { - credentials = { ...this.azure }; - } - - return new ChatOpenAI({ credentials, ...modelOptions }, configOptions); - } - - async initialize({ user, message, onAgentAction, onChainEnd, signal }) { - const modelOptions = { - modelName: this.agentOptions.model, - temperature: this.agentOptions.temperature - }; - - const configOptions = {}; - - if (this.langchainProxy) { - configOptions.basePath = this.langchainProxy; - } - - const model = this.createLLM(modelOptions, configOptions); - - if (this.options.debug) { - console.debug(`<-----Agent Model: ${model.modelName} | Temp: ${model.temperature}----->`); - } - - this.availableTools = await loadTools({ - user, - model, - tools: this.options.tools, - functions: this.functionsAgent, - options: { - openAIApiKey: this.openAIApiKey - } - }); - // load tools - for (const tool of this.options.tools) { - const validTool = this.availableTools[tool]; - - if (tool === 'plugins') { - const plugins = await validTool(); - this.tools = [...this.tools, ...plugins]; - } else if (validTool) { - this.tools.push(await validTool()); - } - } - - if (this.options.debug) { - console.debug('Requested Tools'); - console.debug(this.options.tools); - console.debug('Loaded Tools'); - console.debug(this.tools.map((tool) => tool.name)); - } - - if (this.tools.length > 0 && !this.functionsAgent) { - this.tools.push(new SelfReflectionTool({ message, isGpt3: false })); - } else if (this.tools.length === 0) { - return; - } - - const handleAction = (action, callback = null) => { - this.saveLatestAction(action); - - if (this.options.debug) { - console.debug('Latest Agent Action ', this.actions[this.actions.length - 1]); - } - - if (typeof callback === 'function') { - callback(action); - } - }; - - // initialize agent - const initializer = this.functionsAgent ? initializeFunctionsAgent : initializeCustomAgent; - this.executor = await initializer({ - model, - signal, - tools: this.tools, - pastMessages: this.pastMessages, - currentDateString: this.currentDateString, - verbose: this.options.debug, - returnIntermediateSteps: true, - callbackManager: CallbackManager.fromHandlers({ - async handleAgentAction(action) { - handleAction(action, onAgentAction); - }, - async handleChainEnd(action) { - if (typeof onChainEnd === 'function') { - onChainEnd(action); - } - } - }) - }); - - if (this.options.debug) { - console.debug('Loaded agent.'); - } - } - - async sendApiMessage(messages, userMessage, opts = {}) { - // Doing it this way instead of having each message be a separate element in the array seems to be more reliable, - // especially when it comes to keeping the AI in character. It also seems to improve coherency and context retention. - let payload = await this.buildPrompt({ - messages: [ - ...messages, - { - messageId: userMessage.messageId, - parentMessageId: userMessage.parentMessageId, - role: 'User', - text: userMessage.text - } - ], - ...opts - }); - - let reply = ''; - let result = {}; - if (typeof opts.onProgress === 'function') { - await this.getCompletion( - payload, - (progressMessage) => { - if (progressMessage === '[DONE]') { - return; - } - const token = this.isChatGptModel - ? progressMessage.choices?.[0]?.delta.content - : progressMessage.choices[0].text; - // first event's delta content is always undefined - if (!token) { - return; - } - - if (token === this.endToken) { - return; - } - opts.onProgress(token); - reply += token; - }, - opts.abortController || new AbortController() - ); - } else { - result = await this.getCompletion( - payload, - null, - opts.abortController || new AbortController() - ); - if (this.options.debug) { - console.debug(JSON.stringify(result)); - } - if (this.isChatGptModel) { - reply = result.choices[0].message.content; - } else { - reply = result.choices[0].text.replace(this.endToken, ''); - } - } - - if (this.options.debug) { - console.debug(); - } - - return reply.trim(); - } - - async executorCall(message, signal) { - let errorMessage = ''; - const maxAttempts = 1; - - for (let attempts = 1; attempts <= maxAttempts; attempts++) { - const errorInput = this.buildErrorInput(message, errorMessage); - const input = attempts > 1 ? errorInput : message; - - if (this.options.debug) { - console.debug(`Attempt ${attempts} of ${maxAttempts}`); - } - - if (this.options.debug && errorMessage.length > 0) { - console.debug('Caught error, input:', input); - } - - try { - this.result = await this.executor.call({ input, signal }); - break; // Exit the loop if the function call is successful - } catch (err) { - console.error(err); - errorMessage = err.message; - if (attempts === maxAttempts) { - this.result.output = `Encountered an error while attempting to respond. Error: ${err.message}`; - this.result.intermediateSteps = this.actions; - this.result.errorMessage = errorMessage; - break; - } - } - } - } - - async sendMessage(message, opts = {}) { - if (opts && typeof opts === 'object') { - this.setOptions(opts); - } - console.log('sendMessage', message, opts); - - const user = opts.user || null; - const { onAgentAction, onChainEnd } = opts; - const conversationId = opts.conversationId || crypto.randomUUID(); - const parentMessageId = opts.parentMessageId || '00000000-0000-0000-0000-000000000000'; - const userMessageId = opts.overrideParentMessageId || crypto.randomUUID(); - const responseMessageId = crypto.randomUUID(); - this.pastMessages = await this.loadHistory(conversationId, this.options?.parentMessageId); - - const userMessage = { - messageId: userMessageId, - parentMessageId, - conversationId, - sender: 'User', - text: message, - isCreatedByUser: true - }; - - if (typeof opts?.getIds === 'function') { - opts.getIds({ - userMessage, - conversationId, - responseMessageId - }); - } - - if (typeof opts?.onStart === 'function') { - opts.onStart(userMessage); - } - - await this.saveMessageToDatabase(userMessage, user); - - this.result = {}; - const responseMessage = { - messageId: responseMessageId, - conversationId, - parentMessageId: userMessage.messageId, - isCreatedByUser: false, - model: this.modelOptions.model, - sender: 'ChatGPT' - }; - - if (this.options.debug) { - console.debug('options'); - console.debug(this.options); - } - - const completionMode = this.options.tools.length === 0; - if (!completionMode) { - await this.initialize({ - user, - message, - onAgentAction, - onChainEnd, - signal: opts.abortController.signal - }); - await this.executorCall(message, opts.abortController.signal); - } - - // If message was aborted mid-generation - if (this.result?.errorMessage?.length > 0 && this.result?.errorMessage?.includes('cancel')) { - responseMessage.text = 'Cancelled.'; - await this.saveMessageToDatabase(responseMessage, user); - return { ...responseMessage, ...this.result }; - } - - if (!completionMode && this.agentOptions.skipCompletion && this.result.output) { - responseMessage.text = this.result.output; - this.addImages(this.result.intermediateSteps, responseMessage); - await this.saveMessageToDatabase(responseMessage, user); - const textStream = new TextStream(this.result.output); - await textStream.processTextStream(opts.onProgress); - return { ...responseMessage, ...this.result }; - } - - if (this.options.debug) { - console.debug('this.result', this.result); - } - - const userProvidedPrefix = completionMode && this.options?.promptPrefix?.length > 0; - const promptPrefix = userProvidedPrefix - ? this.options.promptPrefix - : this.buildPromptPrefix(this.result, message); - - if (this.options.debug) { - console.debug('promptPrefix', promptPrefix); - } - - const finalReply = await this.sendApiMessage(this.currentMessages, userMessage, { ...opts, completionMode, promptPrefix }); - responseMessage.text = finalReply; - await this.saveMessageToDatabase(responseMessage, user); - return { ...responseMessage, ...this.result }; - } - - addImages(intermediateSteps, responseMessage) { - if (!intermediateSteps || !responseMessage) { - return; - } - - intermediateSteps.forEach(step => { - const { observation } = step; - if (!observation || !observation.includes('![')) { - return; - } - - if (!responseMessage.text.includes(observation)) { - responseMessage.text += '\n' + observation; - if (this.options.debug) { - console.debug('added image from intermediateSteps'); - } - } - }); - } - - async buildPrompt({ messages, promptPrefix: _promptPrefix, completionMode = false, isChatGptModel = true }) { - if (this.options.debug) { - console.debug('buildPrompt messages', messages); - } - - const orderedMessages = messages; - let promptPrefix = _promptPrefix; - if (promptPrefix) { - promptPrefix = promptPrefix.trim(); - // If the prompt prefix doesn't end with the end token, add it. - if (!promptPrefix.endsWith(`${this.endToken}`)) { - promptPrefix = `${promptPrefix.trim()}${this.endToken}\n\n`; - } - promptPrefix = `${this.startToken}Instructions:\n${promptPrefix}`; - } else { - promptPrefix = `${this.startToken}${completionInstructions} ${this.currentDateString}${this.endToken}\n\n`; - } - - const promptSuffix = `${this.startToken}${this.chatGptLabel}:\n`; // Prompt ChatGPT to respond. - - const instructionsPayload = { - role: 'system', - name: 'instructions', - content: promptPrefix - }; - - const messagePayload = { - role: 'system', - content: promptSuffix - }; - - if (this.isGpt3) { - instructionsPayload.role = 'user'; - messagePayload.role = 'user'; - } - - if (this.isGpt3 && completionMode) { - instructionsPayload.content += `\n${promptSuffix}`; - } - - // testing if this works with browser endpoint - if (!this.isGpt3 && this.reverseProxyUrl) { - instructionsPayload.role = 'user'; - } - - let currentTokenCount; - if (isChatGptModel) { - currentTokenCount = - this.getTokenCountForMessage(instructionsPayload) + - this.getTokenCountForMessage(messagePayload); - } else { - currentTokenCount = this.getTokenCount(`${promptPrefix}${promptSuffix}`); - } - let promptBody = ''; - const maxTokenCount = this.maxPromptTokens; - - // Iterate backwards through the messages, adding them to the prompt until we reach the max token count. - // Do this within a recursive async function so that it doesn't block the event loop for too long. - const buildPromptBody = async () => { - if (currentTokenCount < maxTokenCount && orderedMessages.length > 0) { - const message = orderedMessages.pop(); - // const roleLabel = message.role === 'User' ? this.userLabel : this.chatGptLabel; - const roleLabel = message.role; - let messageString = `${this.startToken}${roleLabel}:\n${message.text}${this.endToken}\n`; - let newPromptBody; - if (promptBody || isChatGptModel) { - newPromptBody = `${messageString}${promptBody}`; - } else { - // Always insert prompt prefix before the last user message, if not gpt-3.5-turbo. - // This makes the AI obey the prompt instructions better, which is important for custom instructions. - // After a bunch of testing, it doesn't seem to cause the AI any confusion, even if you ask it things - // like "what's the last thing I wrote?". - newPromptBody = `${promptPrefix}${messageString}${promptBody}`; - } - - const tokenCountForMessage = this.getTokenCount(messageString); - const newTokenCount = currentTokenCount + tokenCountForMessage; - if (newTokenCount > maxTokenCount) { - if (promptBody) { - // This message would put us over the token limit, so don't add it. - return false; - } - // This is the first message, so we can't add it. Just throw an error. - throw new Error( - `Prompt is too long. Max token count is ${maxTokenCount}, but prompt is ${newTokenCount} tokens long.` - ); - } - promptBody = newPromptBody; - currentTokenCount = newTokenCount; - // wait for next tick to avoid blocking the event loop - await new Promise((resolve) => setTimeout(resolve, 0)); - return buildPromptBody(); - } - return true; - }; - - await buildPromptBody(); - - // const prompt = `${promptBody}${promptSuffix}`; - const prompt = promptBody; - if (isChatGptModel) { - messagePayload.content = prompt; - // Add 2 tokens for metadata after all messages have been counted. - currentTokenCount += 2; - } - - if (this.isGpt3 && messagePayload.content.length > 0) { - const context = `Chat History:\n`; - messagePayload.content = `${context}${prompt}`; - currentTokenCount += this.getTokenCount(context); - } - - // Use up to `this.maxContextTokens` tokens (prompt + response), but try to leave `this.maxTokens` tokens for the response. - this.modelOptions.max_tokens = Math.min( - this.maxContextTokens - currentTokenCount, - this.maxResponseTokens - ); - - if (this.isGpt3 && !completionMode) { - messagePayload.content += promptSuffix; - return [instructionsPayload, messagePayload]; - } - - const result = [messagePayload, instructionsPayload]; - - if (this.functionsAgent && !this.isGpt3 && !completionMode) { - result[1].content = `${result[1].content}\nSure thing! Here is the output you requested:\n`; - } - - if (isChatGptModel) { - return result.filter((message) => message.content.length > 0); - } - - this.completionPromptTokens = currentTokenCount; - return prompt; - } - - getTokenCount(text) { - return this.gptEncoder.encode(text, 'all').length; - } - - /** - * Algorithm adapted from "6. Counting tokens for chat API calls" of - * https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb - * - * An additional 2 tokens need to be added for metadata after all messages have been counted. - * - * @param {*} message - */ - getTokenCountForMessage(message) { - // Map each property of the message to the number of tokens it contains - const propertyTokenCounts = Object.entries(message).map(([key, value]) => { - // Count the number of tokens in the property value - const numTokens = this.getTokenCount(value); - - // Subtract 1 token if the property key is 'name' - const adjustment = key === 'name' ? 1 : 0; - return numTokens - adjustment; - }); - - // Sum the number of tokens in all properties and add 4 for metadata - return propertyTokenCounts.reduce((a, b) => a + b, 4); - } - - /** - * Iterate through messages, building an array based on the parentMessageId. - * Each message has an id and a parentMessageId. The parentMessageId is the id of the message that this message is a reply to. - * @param messages - * @param parentMessageId - * @returns {*[]} An array containing the messages in the order they should be displayed, starting with the root message. - */ - static getMessagesForConversation(messages, parentMessageId) { - const orderedMessages = []; - let currentMessageId = parentMessageId; - while (currentMessageId) { - // eslint-disable-next-line no-loop-func - const message = messages.find((m) => m.messageId === currentMessageId); - if (!message) { - break; - } - orderedMessages.unshift(message); - currentMessageId = message.parentMessageId; - } - - if (orderedMessages.length === 0) { - return []; - } - - return orderedMessages.map((msg) => ({ - messageId: msg.messageId, - parentMessageId: msg.parentMessageId, - role: msg.isCreatedByUser ? 'User' : 'Assistant', - text: msg.text - })); - } - - /** - * Extracts the action tool values from the intermediate steps array. - * Each step object in the array contains an action object with a tool property. - * This function returns an array of tool values. - * - * @param {Object[]} intermediateSteps - An array of intermediate step objects. - * @returns {string} An string of action tool values from each step. - */ - extractToolValues(intermediateSteps) { - const tools = intermediateSteps.map((step) => step.action.tool); - - if (tools.length === 0) { - return ''; - } - - const uniqueTools = [...new Set(tools)]; - - if (tools.length === 1) { - return tools[0] + ' plugin'; - } - - return uniqueTools.join(' plugin, '); - } -} - -module.exports = ChatAgent; diff --git a/api/app/langchain/agents/PlanAndExecute/initializePAEAgent.js b/api/app/langchain/agents/PlanAndExecute/initializePAEAgent.js deleted file mode 100644 index 9b0c907cffb..00000000000 --- a/api/app/langchain/agents/PlanAndExecute/initializePAEAgent.js +++ /dev/null @@ -1,77 +0,0 @@ -const { - ChainStepExecutor, - LLMPlanner, - PlanOutputParser, - PlanAndExecuteAgentExecutor -} = require('langchain/experimental/plan_and_execute'); -const { LLMChain } = require('langchain/chains'); -const { ChatAgent, AgentExecutor } = require('langchain/agents'); -const { BufferMemory, ChatMessageHistory } = require('langchain/memory'); -const { - ChatPromptTemplate, - SystemMessagePromptTemplate, - HumanMessagePromptTemplate -} = require('langchain/prompts'); - -const DEFAULT_STEP_EXECUTOR_HUMAN_CHAT_MESSAGE_TEMPLATE = `{chat_history} - -Previous steps: {previous_steps} -Current objective: {current_step} -{agent_scratchpad} -You may extract and combine relevant data from your previous steps when responding to me.`; - -const PLANNER_SYSTEM_PROMPT_MESSAGE_TEMPLATE = [ - `Let's first understand the problem and devise a plan to solve the problem.`, - `Please output the plan starting with the header "Plan:"`, - `and then followed by a numbered list of steps.`, - `Please make the plan the minimum number of steps required`, - `to answer the query or complete the task accurately and precisely.`, - `Your steps should be general, and should not require a specific method to solve a step. If the task is a question,`, - `the final step in the plan must be the following: "Given the above steps taken,`, - `please respond to the original query."`, - `At the end of your plan, say ""` -].join(' '); - -const PLANNER_CHAT_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromPromptMessages([ - /* #__PURE__ */ SystemMessagePromptTemplate.fromTemplate(PLANNER_SYSTEM_PROMPT_MESSAGE_TEMPLATE), - /* #__PURE__ */ HumanMessagePromptTemplate.fromTemplate(`{input}`) -]); - -const initializePAEAgent = async ({ tools: _tools, model: llm, pastMessages, ...rest }) => { - //removed currentDateString - const tools = _tools.filter((tool) => tool.name !== 'self-reflection'); - - const memory = new BufferMemory({ - chatHistory: new ChatMessageHistory(pastMessages), - // returnMessages: true, // commenting this out retains memory - memoryKey: 'chat_history', - humanPrefix: 'User', - aiPrefix: 'Assistant', - inputKey: 'input', - outputKey: 'output' - }); - - const plannerLlmChain = new LLMChain({ - llm, - prompt: PLANNER_CHAT_PROMPT, - memory - }); - const planner = new LLMPlanner(plannerLlmChain, new PlanOutputParser()); - - const agent = ChatAgent.fromLLMAndTools(llm, tools, { - humanMessageTemplate: DEFAULT_STEP_EXECUTOR_HUMAN_CHAT_MESSAGE_TEMPLATE - }); - - const stepExecutor = new ChainStepExecutor( - AgentExecutor.fromAgentAndTools({ agent, tools, memory, ...rest }) - ); - - return new PlanAndExecuteAgentExecutor({ - planner, - stepExecutor - }); -}; - -module.exports = { - initializePAEAgent -}; diff --git a/api/app/langchain/demos/demo-aiplugin.js b/api/app/langchain/demos/demo-aiplugin.js deleted file mode 100644 index 4972eecbc77..00000000000 --- a/api/app/langchain/demos/demo-aiplugin.js +++ /dev/null @@ -1,31 +0,0 @@ -require('dotenv').config(); -const { ChatOpenAI } = require( "langchain/chat_models/openai"); -const { initializeAgentExecutorWithOptions } = require( "langchain/agents"); -const HttpRequestTool = require('../tools/HttpRequestTool'); -const AIPluginTool = require('../tools/AIPluginTool'); - -const run = async () => { - const openAIApiKey = process.env.OPENAI_API_KEY; - const tools = [ - new HttpRequestTool(), - await AIPluginTool.fromPluginUrl( - "https://www.klarna.com/.well-known/ai-plugin.json", new ChatOpenAI({ temperature: 0, openAIApiKey }) - ), - ]; - const agent = await initializeAgentExecutorWithOptions( - tools, - new ChatOpenAI({ temperature: 0, openAIApiKey }), - { agentType: "chat-zero-shot-react-description", verbose: true } - ); - - const result = await agent.call({ - input: "what t shirts are available in klarna?", - }); - - console.log({ result }); -}; - -(async () => { - await run(); -})(); - diff --git a/api/app/langchain/demos/demo-yaml.js b/api/app/langchain/demos/demo-yaml.js deleted file mode 100644 index 05138abffcf..00000000000 --- a/api/app/langchain/demos/demo-yaml.js +++ /dev/null @@ -1,47 +0,0 @@ -require('dotenv').config(); - -const fs = require( "fs"); -const yaml = require( "js-yaml"); -const { OpenAI } = require( "langchain/llms/openai"); -const { JsonSpec } = require( "langchain/tools"); -const { createOpenApiAgent, OpenApiToolkit } = require( "langchain/agents"); - -const run = async () => { - let data; - try { - const yamlFile = fs.readFileSync("./app/langchain/demos/klarna.yaml", "utf8"); - data = yaml.load(yamlFile); - if (!data) { - throw new Error("Failed to load OpenAPI spec"); - } - } catch (e) { - console.error(e); - return; - } - - const headers = { - "Content-Type": "application/json", - // Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, - }; - const model = new OpenAI({ temperature: 0 }); - const toolkit = new OpenApiToolkit(new JsonSpec(data), model, headers); - const executor = createOpenApiAgent(model, toolkit, { verbose: true }); - - const input = `Find me some medium sized blue shirts`; - console.log(`Executing with input "${input}"...`); - - const result = await executor.call({ input }); - console.log(`Got output ${result.output}`); - - console.log( - `Got intermediate steps ${JSON.stringify( - result.intermediateSteps, - null, - 2 - )}` - ); -}; - -(async () => { - await run(); -})(); \ No newline at end of file diff --git a/api/app/langchain/demos/klarna.yaml b/api/app/langchain/demos/klarna.yaml deleted file mode 100644 index 827d4d8542d..00000000000 --- a/api/app/langchain/demos/klarna.yaml +++ /dev/null @@ -1,79 +0,0 @@ -openapi: 3.0.1 -servers: - - url: https://www.klarna.com/us/shopping -info: - title: Open AI Klarna product Api - version: v0 - x-apisguru-categories: - - ecommerce - x-logo: - url: https://www.klarna.com/static/img/social-prod-imagery-blinds-beauty-default.jpg - x-origin: - - format: openapi - url: https://www.klarna.com/us/shopping/public/openai/v0/api-docs/ - version: "3.0" - x-providerName: klarna.com - x-serviceName: openai -tags: - - description: Open AI Product Endpoint. Query for products. - name: open-ai-product-endpoint -paths: - /public/openai/v0/products: - get: - deprecated: false - operationId: productsUsingGET - parameters: - - description: A precise query that matches one very small category or product that needs to be searched for to find the products the user is looking for. If the user explicitly stated what they want, use that as a query. The query is as specific as possible to the product name or category mentioned by the user in its singular form, and don't contain any clarifiers like latest, newest, cheapest, budget, premium, expensive or similar. The query is always taken from the latest topic, if there is a new topic a new query is started. - in: query - name: q - required: true - schema: - type: string - - description: number of products returned - in: query - name: size - required: false - schema: - type: integer - - description: maximum price of the matching product in local currency, filters results - in: query - name: budget - required: false - schema: - type: integer - responses: - "200": - content: - application/json: - schema: - $ref: "#/components/schemas/ProductResponse" - description: Products found - "503": - description: one or more services are unavailable - summary: API for fetching Klarna product information - tags: - - open-ai-product-endpoint -components: - schemas: - Product: - properties: - attributes: - items: - type: string - type: array - name: - type: string - price: - type: string - url: - type: string - title: Product - type: object - ProductResponse: - properties: - products: - items: - $ref: "#/components/schemas/Product" - type: array - title: ProductResponse - type: object \ No newline at end of file diff --git a/api/app/langchain/demos/planExecutor.js b/api/app/langchain/demos/planExecutor.js deleted file mode 100644 index f5baf21f3df..00000000000 --- a/api/app/langchain/demos/planExecutor.js +++ /dev/null @@ -1,32 +0,0 @@ -require('dotenv').config(); -const { Calculator } = require('langchain/tools/calculator'); -const { SerpAPI } = require('langchain/tools'); -const { ChatOpenAI } = require('langchain/chat_models/openai'); -const { PlanAndExecuteAgentExecutor } = require('langchain/experimental/plan_and_execute'); - -const tools = [ - new Calculator(), - new SerpAPI(process.env.SERPAPI_API_KEY || '', { - location: 'Austin,Texas,United States', - hl: 'en', - gl: 'us' - }) -]; -const model = new ChatOpenAI({ - temperature: 0, - modelName: 'gpt-3.5-turbo', - verbose: true, - openAIApiKey: process.env.OPENAI_API_KEY -}); -const executor = PlanAndExecuteAgentExecutor.fromLLMAndTools({ - llm: model, - tools -}); - -(async () => { - const result = await executor.call({ - input: `Who is the current president of the United States? What is their current age raised to the second power?` - }); - - console.log({ result }); -})(); diff --git a/api/app/langchain/demos/spotify.yaml b/api/app/langchain/demos/spotify.yaml deleted file mode 100644 index 4d3d86f9f88..00000000000 --- a/api/app/langchain/demos/spotify.yaml +++ /dev/null @@ -1,7305 +0,0 @@ -openapi: 3.0.3 -servers: - - url: https://api.spotify.com/v1 -info: - description: | - You can use Spotify's Web API to discover music and podcasts, manage your Spotify library, control audio playback, and much more. Browse our available Web API endpoints using the sidebar at left, or via the navigation bar on top of this page on smaller screens. - - In order to make successful Web API requests your app will need a valid access token. One can be obtained through OAuth 2.0. - - The base URI for all Web API requests is `https://api.spotify.com/v1`. - - Need help? See our Web API guides for more information, or visit the Spotify for Developers community forum to ask questions and connect with other developers. - termsOfService: https://developer.spotify.com/terms/ - title: Spotify Web API - version: 1.0.0 - x-apisguru-categories: - - media - x-logo: - url: https://logo-core.clearbit.com/spotify.com - x-origin: - - format: openapi - url: https://developer.spotify.com/_data/documentation/web-api/reference/open-api-schema.yml - version: "3.0" - x-providerName: spotify.com -paths: - /albums: - get: - description: | - Get Spotify catalog information for multiple albums identified by their Spotify IDs. - operationId: get-multiple-albums - parameters: - - $ref: "#/components/parameters/QueryAlbumIds" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/ManyAlbums" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Several Albums - tags: - - Albums - x-spotify-docs-console-url: /console/get-several-albums/ - x-spotify-docs-endpoint-name: Get Multiple Albums - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Albums - x-spotify-docs-display-name: several-albums - "/albums/{id}": - get: - description: | - Get Spotify catalog information for a single album. - operationId: get-an-album - parameters: - - $ref: "#/components/parameters/PathAlbumId" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/OneAlbum" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Album - tags: - - Albums - x-spotify-docs-console-url: /console/get-album/?id=0sNOF9WDwhWunNAHPD3Baj - x-spotify-docs-endpoint-name: Get an Album - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Albums - x-spotify-docs-display-name: album - "/albums/{id}/tracks": - get: - description: | - Get Spotify catalog information about an album’s tracks. - Optional parameters can be used to limit the number of tracks returned. - operationId: get-an-albums-tracks - parameters: - - $ref: "#/components/parameters/PathAlbumId" - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSimplifiedTrackObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Album Tracks - tags: - - Albums - - Tracks - x-spotify-docs-console-url: /console/get-album-tracks/ - x-spotify-docs-endpoint-name: Get an Album's Tracks - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Albums - x-spotify-docs-display-name: album-tracks - /artists: - get: - description: | - Get Spotify catalog information for several artists based on their Spotify IDs. - operationId: get-multiple-artists - parameters: - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) for the artists. Maximum: 50 IDs. - example: 2CIMQHirSU0MQqyYHq0eOx,57dN52uHvrHOxijzpIgu3E,1vCWHaC5f2uS3yhpwWbIA6 - title: Spotify Artist IDs - type: string - responses: - "200": - $ref: "#/components/responses/ManyArtists" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Several Artists - tags: - - Artists - x-spotify-docs-console-url: /console/get-several-artists/?ids=0oSGxfWSnnOXhD2fKuz2Gy,3dBVyJ7JuOMt4GE9607Qin - x-spotify-docs-endpoint-name: Get Multiple Artists - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Artists - x-spotify-docs-display-name: several-artists - "/artists/{id}": - get: - description: | - Get Spotify catalog information for a single artist identified by their unique Spotify ID. - operationId: get-an-artist - parameters: - - $ref: "#/components/parameters/PathArtistId" - responses: - "200": - $ref: "#/components/responses/OneArtist" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Artist - tags: - - Artists - x-spotify-docs-console-url: /console/get-artist/?id=0OdUWJ0sBjDrqHygGUXeCF - x-spotify-docs-endpoint-name: Get an Artist - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Artists - x-spotify-docs-display-name: artist - "/artists/{id}/albums": - get: - description: | - Get Spotify catalog information about an artist's albums. - operationId: get-an-artists-albums - parameters: - - $ref: "#/components/parameters/PathArtistId" - - $ref: "#/components/parameters/QueryIncludeGroups" - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSimplifiedAlbumObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Artist's Albums - tags: - - Artists - - Albums - x-spotify-docs-console-url: /console/get-artist-albums/?album_type=single&limit=2&market=ES&id=1vCWHaC5f2uS3yhpwWbIA6 - x-spotify-docs-endpoint-name: Get an Artist's Albums - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Artists - x-spotify-docs-display-name: artist-albums - "/artists/{id}/related-artists": - get: - description: | - Get Spotify catalog information about artists similar to a given artist. Similarity is based on analysis of the Spotify community's [listening history](http://news.spotify.com/se/2010/02/03/related-artists/). - operationId: get-an-artists-related-artists - parameters: - - $ref: "#/components/parameters/PathArtistId" - responses: - "200": - $ref: "#/components/responses/ManyArtists" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Artist's Related Artists - tags: - - Artists - x-spotify-docs-console-url: /console/get-artist-related-artists/?id=43ZHCT0cAZBISjO8DG9PnE - x-spotify-docs-endpoint-name: Get an Artist's Related Artists - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Artists - x-spotify-docs-display-name: artist-related-artists - "/artists/{id}/top-tracks": - get: - description: | - Get Spotify catalog information about an artist's top tracks by country. - operationId: get-an-artists-top-tracks - parameters: - - $ref: "#/components/parameters/PathArtistId" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/ManyTracks" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Artist's Top Tracks - tags: - - Artists - - Tracks - x-spotify-docs-console-url: /console/get-artist-top-tracks/?country=SE&id=43ZHCT0cAZBISjO8DG9PnE - x-spotify-docs-endpoint-name: Get an Artist's Top Tracks - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Artists - x-spotify-docs-display-name: artist-top-tracks - "/audio-analysis/{id}": - get: - description: | - Get a low-level audio analysis for a track in the Spotify catalog. The audio analysis describes the track’s structure and musical content, including rhythm, pitch, and timbre. - operationId: get-audio-analysis - parameters: - - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) - for the track. - example: 11dFghVXANMlKmJXsNCbNl - title: Spotify Track ID - type: string - responses: - "200": - $ref: "#/components/responses/OneAudioAnalysis" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Track's Audio Analysis - tags: - - Tracks - x-spotify-docs-console-url: /console/get-audio-analysis-track/?id=06AKEBrKUckW0KREUWRnvT - x-spotify-docs-endpoint-name: Get Audio Analysis for a Track - x-spotify-docs-category: Tracks - x-spotify-docs-display-name: audio-analysis-track - /audio-features: - get: - description: | - Get audio features for multiple tracks based on their Spotify IDs. - operationId: get-several-audio-features - parameters: - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) - for the tracks. Maximum: 100 IDs. - example: 7ouMYWpwJ422jRcDASZB7P,4VqPOruhp5EdPBeR92t6lQ,2takcwOaAZWiXQijPHIx7B - title: Spotify Track IDs - type: string - responses: - "200": - $ref: "#/components/responses/ManyAudioFeatures" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Tracks' Audio Features - tags: - - Tracks - x-spotify-docs-console-url: /console/get-audio-features-several-tracks/?ids=4JpKVNYnVcJ8tuMKjAj50A,2NRANZE9UCmPAS5XVbXL40,24JygzOLM0EmRQeGtFcIcG - x-spotify-docs-endpoint-name: Get Audio Features for Several Tracks - x-spotify-docs-category: Tracks - x-spotify-docs-display-name: audio-features-several-tracks - "/audio-features/{id}": - get: - description: | - Get audio feature information for a single track identified by its unique - Spotify ID. - operationId: get-audio-features - parameters: - - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the track. - example: 11dFghVXANMlKmJXsNCbNl - title: Spotify Track ID - type: string - responses: - "200": - $ref: "#/components/responses/OneAudioFeatures" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Track's Audio Features - tags: - - Tracks - x-spotify-docs-console-url: /console/get-audio-features-track/?id=06AKEBrKUckW0KREUWRnvT - x-spotify-docs-endpoint-name: Get Audio Features for a Track - x-spotify-docs-category: Tracks - x-spotify-docs-display-name: audio-features-track - /audiobooks: - get: - description: | - Get Spotify catalog information for several audiobooks identified by their Spotify IDs.
- **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.** - operationId: get-multiple-audiobooks - parameters: - - $ref: "#/components/parameters/QueryAudiobookIds" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/ManyAudiobooks" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Several Audiobooks - tags: - - Audiobooks - x-spotify-docs-console-url: /console/get-several-audiobooks/?ids=5thw29eqjomhIDMY1XKsLk,2IEBhnu61ieYGFRPEJIO40 - x-spotify-docs-endpoint-name: Get Several Audiobooks - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Audiobooks - x-spotify-docs-display-name: several-audiobooks - "/audiobooks/{id}": - get: - description: | - Get Spotify catalog information for a single audiobook.
- **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.** - operationId: get-an-audiobook - parameters: - - $ref: "#/components/parameters/PathAudiobookId" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/OneAudiobook" - "400": - $ref: "#/components/responses/BadRequest" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "404": - $ref: "#/components/responses/NotFound" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get an Audiobook - tags: - - Audiobooks - x-spotify-docs-console-url: /console/get-audiobook/?id=5thw29eqjomhIDMY1XKsLk - x-spotify-docs-endpoint-name: Get an Audiobook - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Audiobooks - x-spotify-docs-display-name: audiobook - "/audiobooks/{id}/chapters": - get: - description: | - Get Spotify catalog information about an audiobook's chapters.
- **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.** - operationId: get-audiobook-chapters - parameters: - - $ref: "#/components/parameters/PathAudiobookId" - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSimplifiedChapterObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Audiobook Chapters - tags: - - Audiobooks - - Chapters - x-spotify-docs-console-url: /console/get-audiobook-chapters/?id=5thw29eqjomhIDMY1XKsLk - x-spotify-docs-endpoint-name: Get an Audiobook's Chapters - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Audiobooks - x-spotify-docs-display-name: audiobook-chapters - /browse/categories: - get: - description: | - Get a list of categories used to tag items in Spotify (on, for example, the Spotify player’s “Browse” tab). - operationId: get-categories - parameters: - - in: query - name: country - required: false - schema: - description: | - A country: an [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Provide this parameter if you want to narrow the list of returned categories to those relevant to a particular country. If omitted, the returned items will be globally relevant. - example: SE - title: Country - type: string - - in: query - name: locale - required: false - schema: - description: | - The desired language, consisting of an [ISO 639-1](http://en.wikipedia.org/wiki/ISO_639-1) language code and an [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2), joined by an underscore. For example: `es_MX`, meaning "Spanish (Mexico)". Provide this parameter if you want the category metadata returned in a particular language.
- _**Note**: if `locale` is not supplied, or if the specified language is not available, all strings will be returned in the Spotify default language (American English). The `locale` parameter, combined with the `country` parameter, may give odd results if not carefully matched. For example `country=SE&locale=de_DE` will return a list of categories relevant to Sweden but as German language strings._ - example: sv_SE - title: Locale - type: string - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagedCategories" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Several Browse Categories - tags: - - Categories - x-spotify-docs-console-url: /console/get-browse-categories/ - x-spotify-docs-endpoint-name: Get All Categories - x-spotify-docs-category: Browse - x-spotify-docs-display-name: browse-categories - "/browse/categories/{category_id}": - get: - description: | - Get a single category used to tag items in Spotify (on, for example, the Spotify player’s “Browse” tab). - operationId: get-a-category - parameters: - - in: path - name: category_id - required: true - schema: - description: | - The [Spotify category ID](/documentation/web-api/concepts/spotify-uris-ids) for the category. - example: dinner - title: Category ID - type: string - - in: query - name: country - required: false - schema: - description: | - A country: an [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Provide this parameter to ensure that the category exists for a particular country. - example: SE - title: Country - type: string - - in: query - name: locale - required: false - schema: - description: | - The desired language, consisting of an [ISO 639-1](http://en.wikipedia.org/wiki/ISO_639-1) language code and an [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2), joined by an underscore. For example: `es_MX`, meaning "Spanish (Mexico)". Provide this parameter if you want the category strings returned in a particular language.
_**Note**: if `locale` is not supplied, or if the specified language is not available, the category strings returned will be in the Spotify default language (American English)._ - example: sv_SE - title: Locale - type: string - responses: - "200": - $ref: "#/components/responses/OneCategory" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Single Browse Category - tags: - - Categories - x-spotify-docs-console-url: /console/get-browse-category/ - x-spotify-docs-endpoint-name: Get a Category - x-spotify-docs-category: Browse - x-spotify-docs-display-name: browse-category - "/browse/categories/{category_id}/playlists": - get: - description: | - Get a list of Spotify playlists tagged with a particular category. - operationId: get-a-categories-playlists - parameters: - - in: path - name: category_id - required: true - schema: - description: | - The [Spotify category ID](/documentation/web-api/concepts/spotify-uris-ids) for the category. - example: dinner - title: Category ID - type: string - - in: query - name: country - required: false - schema: - description: | - A country: an [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Provide this parameter to ensure that the category exists for a particular country. - example: SE - title: Country - type: string - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagedFeaturedPlaylists" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Category's Playlists - tags: - - Playlists - - Categories - x-spotify-docs-console-url: /console/get-category-playlists/?country=BR&category_id=party&limit=2 - x-spotify-docs-endpoint-name: Get a Category's Playlists - x-spotify-docs-category: Browse - x-spotify-docs-display-name: category-playlists - /browse/featured-playlists: - get: - description: | - Get a list of Spotify featured playlists (shown, for example, on a Spotify player's 'Browse' tab). - operationId: get-featured-playlists - parameters: - - in: query - name: country - required: false - schema: - description: | - A country: an [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Provide this parameter if you want the list of returned items to be relevant to a particular country. If omitted, the returned items will be relevant to all countries. - example: SE - title: Country - type: string - - in: query - name: locale - required: false - schema: - description: | - The desired language, consisting of a lowercase [ISO 639-1 language code](http://en.wikipedia.org/wiki/ISO_639-1) and an uppercase [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2), joined by an underscore. For example: `es_MX`, meaning "Spanish (Mexico)". Provide this parameter if you want the results returned in a particular language (where available).
- _**Note**: if `locale` is not supplied, or if the specified language is not available, all strings will be returned in the Spotify default language (American English). The `locale` parameter, combined with the `country` parameter, may give odd results if not carefully matched. For example `country=SE&locale=de_DE` will return a list of categories relevant to Sweden but as German language strings._ - example: sv_SE - title: Locale - type: string - - in: query - name: timestamp - required: false - schema: - description: | - A timestamp in [ISO 8601 format](http://en.wikipedia.org/wiki/ISO_8601): `yyyy-MM-ddTHH:mm:ss`. Use this parameter to specify the user's local time to get results tailored for that specific date and time in the day. If not provided, the response defaults to the current UTC time. Example: "2014-10-23T09:00:00" for a user whose local time is 9AM. If there were no featured playlists (or there is no data) at the specified time, the response will revert to the current UTC time. - example: 2014-10-23T09:00:00 - title: Timestamp - type: string - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagedFeaturedPlaylists" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Featured Playlists - tags: - - Playlists - x-spotify-docs-console-url: /console/get-featured-playlists/?country=SE&limit=2 - x-spotify-docs-endpoint-name: Get All Featured Playlists - x-spotify-policy-list: - - $ref: "#/components/x-spotify-policy/MultipleIntegrations" - x-spotify-docs-category: Browse - x-spotify-docs-display-name: featured-playlists - /browse/new-releases: - get: - description: | - Get a list of new album releases featured in Spotify (shown, for example, on a Spotify player’s “Browse” tab). - operationId: get-new-releases - parameters: - - in: query - name: country - required: false - schema: - description: | - A country: an [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Provide this parameter if you want the list of returned items to be relevant to a particular country. If omitted, the returned items will be relevant to all countries. - example: SE - title: Country - type: string - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagedAlbums" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get New Releases - tags: - - Albums - x-spotify-docs-console-url: /console/get-new-releases/?country=SE - x-spotify-docs-endpoint-name: Get All New Releases - x-spotify-policy-list: - - $ref: "#/components/x-spotify-policy/MultipleIntegrations" - x-spotify-docs-category: Browse - x-spotify-docs-display-name: new-releases - /chapters: - get: - description: | - Get Spotify catalog information for several chapters identified by their Spotify IDs.
- **Note: Chapters are only available for the US, UK, Ireland, New Zealand and Australia markets.** - operationId: get-several-chapters - parameters: - - $ref: "#/components/parameters/QueryChapterIds" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/ManyChapters" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Several Chapters - tags: - - Chapters - x-spotify-docs-console-url: /console/get-several-chapters/?ids=2i47HuOBSV2XaJNy0NCZXM,2GUbORsUnP1qVVlLwd9DzP - x-spotify-docs-endpoint-name: Get Several Chapters - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Chapters - x-spotify-docs-display-name: several-chapters - "/chapters/{id}": - get: - description: | - Get Spotify catalog information for a single chapter.
- **Note: Chapters are only available for the US, UK, Ireland, New Zealand and Australia markets.** - operationId: get-a-chapter - parameters: - - $ref: "#/components/parameters/PathChapterId" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/OneChapter" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get a Chapter - tags: - - Chapters - x-spotify-docs-console-url: /console/get-chapter/?id=2i47HuOBSV2XaJNy0NCZXM - x-spotify-docs-endpoint-name: Get a Chapter - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Chapters - x-spotify-docs-display-name: chapters - /episodes: - get: - description: | - Get Spotify catalog information for several episodes based on their Spotify IDs. - operationId: get-multiple-episodes - parameters: - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) for the episodes. Maximum: 50 IDs. - example: 77o6BIVlYM3msb4MMIL1jH,0Q86acNRm6V9GYx55SXKwf - title: Ids - type: string - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/ManyEpisodes" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-playback-position - summary: | - Get Several Episodes - tags: - - Episodes - x-spotify-docs-console-url: /console/get-several-episodes/?ids=77o6BIVlYM3msb4MMIL1jH,0Q86acNRm6V9GYx55SXKwf - x-spotify-docs-endpoint-name: Get Multiple Episodes - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Episodes - x-spotify-docs-display-name: several-episodes - "/episodes/{id}": - get: - description: | - Get Spotify catalog information for a single episode identified by its - unique Spotify ID. - operationId: get-an-episode - parameters: - - in: path - name: id - required: true - schema: - description: The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the episode. - example: 512ojhOuo1ktJprKbVcKyQ - title: Get an Episode - type: string - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/OneEpisode" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-playback-position - summary: | - Get Episode - tags: - - Episodes - x-spotify-docs-console-url: /console/get-episode/?id=512ojhOuo1ktJprKbVcKyQ - x-spotify-docs-endpoint-name: Get an Episode - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Episodes - x-spotify-docs-display-name: episode - /markets: - get: - description: | - Get the list of markets where Spotify is available. - operationId: get-available-markets - responses: - "200": - content: - application/json: - schema: - properties: - markets: - example: - - CA - - BR - - IT - items: - type: string - type: array - type: object - description: A markets object with an array of country codes - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Available Markets - tags: - - Markets - x-spotify-docs-console-url: /console/get-available-markets/ - x-spotify-docs-endpoint-name: Get Available Markets - x-spotify-docs-category: Markets - x-spotify-docs-display-name: available-markets - /me: - get: - description: | - Get detailed profile information about the current user (including the - current user's username). - operationId: get-current-users-profile - responses: - "200": - $ref: "#/components/responses/OnePrivateUser" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-private - - user-read-email - summary: | - Get Current User's Profile - tags: - - Users - x-spotify-docs-console-url: /console/get-current-user/ - x-spotify-docs-endpoint-name: Get Current User's Profile - x-spotify-docs-category: Users Profile - x-spotify-docs-display-name: current-user - /me/albums: - delete: - description: | - Remove one or more albums from the current user's 'Your Music' library. - operationId: remove-albums-user - parameters: - - $ref: "#/components/parameters/QueryAlbumIds" - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `["4iV5W9uYEdYUVa79Axb7Rh", "1301WleyT98MSxVHPZCA6M"]`
A maximum of 50 items can be specified in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - type: object - responses: - "200": - description: Album(s) have been removed from the library - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Remove Users' Saved Albums - tags: - - Albums - - Library - x-spotify-docs-console-url: /console/delete-current-user-saved-albums/?ids=07bYtmE3bPsLB6ZbmmFi8d%2C48JYNjh7GMie6NjqYHMmtT%2C27cZdqrQiKt3IT00338dws - x-spotify-docs-endpoint-name: Remove Albums for Current User - get: - description: | - Get a list of the albums saved in the current Spotify user's 'Your Music' library. - operationId: get-users-saved-albums - parameters: - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/PagingSavedAlbumObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Get User's Saved Albums - tags: - - Albums - - Library - x-spotify-docs-console-url: /console/get-current-user-saved-albums/?limit=1 - x-spotify-docs-endpoint-name: Get User's Saved Albums - put: - description: | - Save one or more albums to the current user's 'Your Music' library. - operationId: save-albums-user - parameters: - - $ref: "#/components/parameters/QueryAlbumIds" - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `["4iV5W9uYEdYUVa79Axb7Rh", "1301WleyT98MSxVHPZCA6M"]`
A maximum of 50 items can be specified in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - type: object - responses: - "200": - description: The album is saved - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Save Albums for Current User - tags: - - Albums - - Library - x-spotify-docs-console-url: /console/put-current-user-saved-albums/?ids=07bYtmE3bPsLB6ZbmmFi8d%2C48JYNjh7GMie6NjqYHMmtT%2C27cZdqrQiKt3IT00338dws - x-spotify-docs-endpoint-name: Save Albums for Current User - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-saved-albums - /me/albums/contains: - get: - description: | - Check if one or more albums is already saved in the current Spotify user's 'Your Music' library. - operationId: check-users-saved-albums - parameters: - - $ref: "#/components/parameters/QueryAlbumIds" - responses: - "200": - $ref: "#/components/responses/ArrayOfBooleans" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Check User's Saved Albums - tags: - - Albums - - Library - x-spotify-docs-console-url: /console/get-current-user-contains-saved-albums/?ids=0pJJgBzj26qnE1nSQUxaB0%2C5ZAKzV4ZIa5Gt7z29OYHv0 - x-spotify-docs-endpoint-name: Check User's Saved Albums - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-contains-saved-albums - /me/audiobooks: - delete: - description: | - Remove one or more audiobooks from the Spotify user's library. - operationId: remove-audiobooks-user - parameters: - - $ref: "#/components/parameters/QueryAudiobookIds" - responses: - "200": - description: Audiobook(s) have been removed from the library - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Remove User's Saved Audiobooks - tags: - - Audiobooks - - Library - x-spotify-docs-console-url: /console/delete-current-user-saved-audiobooks/?ids=07bYtmE3bPsLB6ZbmmFi8d%2C48JYNjh7GMie6NjqYHMmtT%2C27cZdqrQiKt3IT00338dws - x-spotify-docs-endpoint-name: Remove Audiobooks for Current User - get: - description: | - Get a list of the audiobooks saved in the current Spotify user's 'Your Music' library. - operationId: get-users-saved-audiobooks - parameters: - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSimplifiedAudiobookObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Get User's Saved Audiobooks - tags: - - Audiobooks - - Library - x-spotify-docs-console-url: /console/get-current-user-saved-audiobooks/?limit=1 - x-spotify-docs-endpoint-name: Get User's Saved Audiobooks - put: - description: | - Save one or more audiobooks to the current Spotify user's library. - operationId: save-audiobooks-user - parameters: - - $ref: "#/components/parameters/QueryAudiobookIds" - responses: - "200": - description: Audiobook(s) are saved to the library - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Save Audiobooks for Current User - tags: - - Audiobooks - - Library - x-spotify-docs-console-url: /console/put-current-user-saved-audiobooks/?ids=07bYtmE3bPsLB6ZbmmFi8d%2C48JYNjh7GMie6NjqYHMmtT%2C27cZdqrQiKt3IT00338dws - x-spotify-docs-endpoint-name: Save Audiobooks for Current User - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-saved-audiobooks - /me/audiobooks/contains: - get: - description: | - Check if one or more audiobooks are already saved in the current Spotify user's library. - operationId: check-users-saved-audiobooks - parameters: - - $ref: "#/components/parameters/QueryAudiobookIds" - responses: - "200": - $ref: "#/components/responses/ArrayOfBooleans" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Check User's Saved Audiobooks - tags: - - Audiobooks - - Library - x-spotify-docs-console-url: /console/get-current-user-contains-saved-audiobooks/?ids=0pJJgBzj26qnE1nSQUxaB0%2C5ZAKzV4ZIa5Gt7z29OYHv0 - x-spotify-docs-endpoint-name: Check User's Saved Audiobooks - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-contains-saved-audiobooks - /me/episodes: - delete: - description: | - Remove one or more episodes from the current user's library.
- This API endpoint is in __beta__ and could change without warning. Please share any feedback that you have, or issues that you discover, in our [developer community forum](https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer). - operationId: remove-episodes-user - parameters: - - $ref: "#/components/parameters/QueryTrackIds" - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids).
A maximum of 50 items can be specified in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - type: object - responses: - "200": - description: Episode removed - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Remove User's Saved Episodes - tags: - - Episodes - - Library - x-spotify-docs-console-url: /console/delete-current-user-saved-episodes/?ids=77o6BIVlYM3msb4MMIL1jH%2C0Q86acNRm6V9GYx55SXKwf - x-spotify-docs-endpoint-name: Remove User's Saved Episodes - get: - description: | - Get a list of the episodes saved in the current Spotify user's library.
- This API endpoint is in __beta__ and could change without warning. Please share any feedback that you have, or issues that you discover, in our [developer community forum](https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer). - operationId: get-users-saved-episodes - parameters: - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSavedEpisodeObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - - user-read-playback-position - summary: | - Get User's Saved Episodes - tags: - - Episodes - - Library - x-spotify-docs-console-url: /console/get-current-user-saved-episodes/ - x-spotify-docs-endpoint-name: Get User's Saved Episodes - put: - description: | - Save one or more episodes to the current user's library.
- This API endpoint is in __beta__ and could change without warning. Please share any feedback that you have, or issues that you discover, in our [developer community forum](https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer). - operationId: save-episodes-user - parameters: - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). Maximum: 50 IDs. - example: 77o6BIVlYM3msb4MMIL1jH,0Q86acNRm6V9GYx55SXKwf - title: Spotify Episodes IDs - type: string - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids).
A maximum of 50 items can be specified in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - required: - - uris - type: object - responses: - "200": - description: Episode saved - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Save Episodes for Current User - tags: - - Episodes - - Library - x-spotify-docs-console-url: /console/put-current-user-saved-episodes/?ids=77o6BIVlYM3msb4MMIL1jH%2C0Q86acNRm6V9GYx55SXKwf - x-spotify-docs-endpoint-name: Save Episodes for Current User - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-saved-episodes - /me/episodes/contains: - get: - description: | - Check if one or more episodes is already saved in the current Spotify user's 'Your Episodes' library.
- This API endpoint is in __beta__ and could change without warning. Please share any feedback that you have, or issues that you discover, in our [developer community forum](https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer).. - operationId: check-users-saved-episodes - parameters: - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) for the episodes. Maximum: 50 IDs. - example: 77o6BIVlYM3msb4MMIL1jH,0Q86acNRm6V9GYx55SXKwf - title: Spotify Episode IDs - type: string - responses: - "200": - $ref: "#/components/responses/ArrayOfBooleans" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Check User's Saved Episodes - tags: - - Episodes - - Library - x-spotify-docs-console-url: /console/get-current-user-contains-saved-episodes/?ids=77o6BIVlYM3msb4MMIL1jH%2C0Q86acNRm6V9GYx55SXKwf - x-spotify-docs-endpoint-name: Check User's Saved Episodes - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-contains-saved-episodes - /me/following: - delete: - description: | - Remove the current user as a follower of one or more artists or other Spotify users. - operationId: unfollow-artists-users - parameters: - - in: query - name: type - required: true - schema: - description: | - The ID type: either `artist` or `user`. - enum: - - artist - - user - example: artist - title: Item Type - type: string - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the artist or the user [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `ids=74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q`. A maximum of 50 IDs can be sent in one request. - example: 2CIMQHirSU0MQqyYHq0eOx,57dN52uHvrHOxijzpIgu3E,1vCWHaC5f2uS3yhpwWbIA6 - title: Spotify IDs - type: string - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the artist or user [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `{ids:["74ASZWbe4lXaubB36ztrGX", "08td7MxkoHQkXnWAYD8d6Q"]}`. A maximum of 50 IDs can be sent in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - type: object - responses: - "200": - description: Artist or user unfollowed - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-follow-modify - summary: | - Unfollow Artists or Users - tags: - - Users - - Artists - - Library - x-spotify-docs-console-url: /console/delete-following/?type=user&ids=exampleuser01 - x-spotify-docs-endpoint-name: Unfollow Artists or Users - get: - description: | - Get the current user's followed artists. - operationId: get-followed - parameters: - - in: query - name: type - required: true - schema: - description: | - The ID type: currently only `artist` is supported. - enum: - - artist - example: artist - title: Item Type - type: string - - in: query - name: after - required: false - schema: - description: | - The last artist ID retrieved from the previous request. - example: 0I2XqVXqHScXjHhk6AYYRe - title: After - type: string - - in: query - name: limit - required: false - schema: - default: 20 - description: | - The maximum number of items to return. Default: 20\. Minimum: 1\. Maximum: 50\. - example: 10 - maximum: 50 - minimum: 0 - title: Limit - type: integer - responses: - "200": - $ref: "#/components/responses/CursorPagedArtists" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-follow-read - summary: | - Get Followed Artists - tags: - - Users - - Library - - Artists - x-spotify-docs-console-url: /console/get-following/?type=artist&limit=20 - x-spotify-docs-endpoint-name: Get User's Followed Artists - put: - description: | - Add the current user as a follower of one or more artists or other Spotify users. - operationId: follow-artists-users - parameters: - - in: query - name: type - required: true - schema: - description: | - The ID type. - enum: - - artist - - user - example: artist - title: Item Type - type: string - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the artist or the user [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). - A maximum of 50 IDs can be sent in one request. - example: 2CIMQHirSU0MQqyYHq0eOx,57dN52uHvrHOxijzpIgu3E,1vCWHaC5f2uS3yhpwWbIA6 - title: Spotify IDs - type: string - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the artist or user [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). - For example: `{ids:["74ASZWbe4lXaubB36ztrGX", "08td7MxkoHQkXnWAYD8d6Q"]}`. A maximum of 50 IDs can be sent in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - required: - - ids - type: object - responses: - "204": - description: Artist or user followed - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-follow-modify - summary: | - Follow Artists or Users - tags: - - Users - - Artists - - Library - x-spotify-docs-console-url: /console/put-following/?type=user&ids=exampleuser01 - x-spotify-docs-endpoint-name: Follow Artists or Users - x-spotify-docs-category: Follow - x-spotify-docs-display-name: following - /me/following/contains: - get: - description: | - Check to see if the current user is following one or more artists or other Spotify users. - operationId: check-current-user-follows - parameters: - - in: query - name: type - required: true - schema: - description: | - The ID type: either `artist` or `user`. - enum: - - artist - - user - example: artist - title: Item Type - type: string - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the artist or the user [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) to check. For example: `ids=74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q`. A maximum of 50 IDs can be sent in one request. - example: 2CIMQHirSU0MQqyYHq0eOx,57dN52uHvrHOxijzpIgu3E,1vCWHaC5f2uS3yhpwWbIA6 - title: Spotify IDs - type: string - responses: - "200": - $ref: "#/components/responses/ArrayOfBooleans" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-follow-read - summary: | - Check If User Follows Artists or Users - tags: - - Users - - Artists - - Library - x-spotify-docs-console-url: /console/get-following-contains/?type=user&ids=exampleuser01 - x-spotify-docs-endpoint-name: Get Following State for Artists/Users - x-spotify-docs-category: Follow - x-spotify-docs-display-name: following-contains - /me/player: - get: - description: | - Get information about the user’s current playback state, including track or episode, progress, and active device. - operationId: get-information-about-the-users-current-playback - parameters: - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryAdditionalTypes" - responses: - "200": - $ref: "#/components/responses/OneCurrentlyPlaying" - "204": - description: Playback not available or active - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-playback-state - summary: | - Get Playback State - tags: - - Player - x-spotify-docs-console-url: /console/get-user-player/ - x-spotify-docs-endpoint-name: Get Information About The User's Current Playback - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - put: - description: | - Transfer playback to a new device and determine if it should start playing. - operationId: transfer-a-users-playback - requestBody: - content: - application/json: - schema: - additionalProperties: true - example: - device_ids: - - 74ASZWbe4lXaubB36ztrGX - properties: - device_ids: - description: | - A JSON array containing the ID of the device on which playback should be started/transferred.
For example:`{device_ids:["74ASZWbe4lXaubB36ztrGX"]}`
_**Note**: Although an array is accepted, only a single device_id is currently supported. Supplying more than one will return `400 Bad Request`_ - items: - type: string - type: array - play: - additionalProperties: true - description: | - **true**: ensure playback happens on new device.
**false** or not provided: keep the current playback state. - type: boolean - required: - - device_ids - type: object - responses: - "204": - description: Playback transferred - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Transfer Playback - tags: - - Player - x-spotify-docs-console-url: /console/put-user-player - x-spotify-docs-endpoint-name: Transfer a User's Playback - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: user-player - /me/player/currently-playing: - get: - description: | - Get the object currently being played on the user's Spotify account. - operationId: get-the-users-currently-playing-track - parameters: - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryAdditionalTypes" - responses: - "200": - $ref: "#/components/responses/OneCurrentlyPlayingTrack" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-currently-playing - summary: | - Get Currently Playing Track - tags: - - Player - x-spotify-docs-console-url: /console/get-users-currently-playing-track/ - x-spotify-docs-endpoint-name: Get the User's Currently Playing Track - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: users-currently-playing-track - /me/player/devices: - get: - description: | - Get information about a user’s available devices. - operationId: get-a-users-available-devices - responses: - "200": - $ref: "#/components/responses/ManyDevices" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-playback-state - summary: | - Get Available Devices - tags: - - Player - x-spotify-docs-console-url: /console/get-users-available-devices/ - x-spotify-docs-endpoint-name: Get a User's Available Devices - x-spotify-docs-category: Player - x-spotify-docs-display-name: users-available-devices - /me/player/next: - post: - description: | - Skips to next track in the user’s queue. - operationId: skip-users-playback-to-next-track - parameters: - - in: query - name: device_id - required: false - schema: - description: The id of the device this command is targeting. If not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Command sent - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Skip To Next - tags: - - Player - x-spotify-docs-console-url: /console/post-next/ - x-spotify-docs-endpoint-name: Skip User’s Playback To Next Track - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: next - /me/player/pause: - put: - description: | - Pause playback on the user's account. - operationId: pause-a-users-playback - parameters: - - in: query - name: device_id - required: false - schema: - description: | - The id of the device this command is targeting. If not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Playback paused - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Pause Playback - tags: - - Player - x-spotify-docs-console-url: /console/put-pause/ - x-spotify-docs-endpoint-name: Pause a User's Playback - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: pause - /me/player/play: - put: - description: | - Start a new context or resume current playback on the user's active device. - operationId: start-a-users-playback - parameters: - - in: query - name: device_id - required: false - schema: - description: The id of the device this command is targeting. If not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - requestBody: - content: - application/json: - schema: - additionalProperties: true - example: - context_uri: spotify:album:5ht7ItJgpBH7W6vJ5BqpPr - offset: - position: 5 - position_ms: 0 - properties: - context_uri: - additionalProperties: true - description: | - Optional. Spotify URI of the context to play. - Valid contexts are albums, artists & playlists. - `{context_uri:"spotify:album:1Je1IMUlBXcx1Fz0WE7oPT"}` - type: string - offset: - additionalProperties: true - description: | - Optional. Indicates from where in the context playback should start. Only available when context_uri corresponds to an album or playlist object - "position" is zero based and can’t be negative. Example: `"offset": {"position": 5}` - "uri" is a string representing the uri of the item to start at. Example: `"offset": {"uri": "spotify:track:1301WleyT98MSxVHPZCA6M"}` - type: object - position_ms: - additionalProperties: true - description: integer - type: integer - uris: - description: | - Optional. A JSON array of the Spotify track URIs to play. - For example: `{"uris": ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "spotify:track:1301WleyT98MSxVHPZCA6M"]}` - items: - type: string - type: array - type: object - responses: - "204": - description: Playback started - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Start/Resume Playback - tags: - - Player - x-spotify-docs-console-url: /console/put-play/ - x-spotify-docs-endpoint-name: Start/Resume a User's Playback - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: play - /me/player/previous: - post: - description: | - Skips to previous track in the user’s queue. - operationId: skip-users-playback-to-previous-track - parameters: - - in: query - name: device_id - required: false - schema: - description: | - The id of the device this command is targeting. If - not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Command sent - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Skip To Previous - tags: - - Player - x-spotify-docs-console-url: /console/post-previous/ - x-spotify-docs-endpoint-name: Skip User’s Playback To Previous Track - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: previous - /me/player/queue: - get: - description: | - Get the list of objects that make up the user's queue. - operationId: get-queue - responses: - "200": - $ref: "#/components/responses/Queue" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-playback-state - summary: | - Get the User's Queue - tags: - - Player - x-spotify-docs-console-url: /console/get-queue/ - x-spotify-docs-endpoint-name: Get the User's Queue - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - post: - description: | - Add an item to the end of the user's current playback queue. - operationId: add-to-queue - parameters: - - in: query - name: uri - required: true - schema: - description: | - The uri of the item to add to the queue. Must be a track or an episode uri. - example: spotify:track:4iV5W9uYEdYUVa79Axb7Rh - title: Spotify URI - type: string - - in: query - name: device_id - required: false - schema: - description: | - The id of the device this command is targeting. If - not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Command received - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Add Item to Playback Queue - tags: - - Player - x-spotify-docs-console-url: /console/post-queue/ - x-spotify-docs-endpoint-name: Add an item to queue - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: queue - /me/player/recently-played: - get: - description: | - Get tracks from the current user's recently played tracks. - _**Note**: Currently doesn't support podcast episodes._ - operationId: get-recently-played - parameters: - - in: query - name: limit - required: false - schema: - default: 20 - description: | - The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. - example: 10 - maximum: 50 - minimum: 0 - title: Limit - type: integer - - in: query - name: after - required: false - schema: - description: | - A Unix timestamp in milliseconds. Returns all items - after (but not including) this cursor position. If `after` is specified, `before` - must not be specified. - example: 1484811043508 - title: After - type: integer - - in: query - name: before - required: false - schema: - description: | - A Unix timestamp in milliseconds. Returns all items - before (but not including) this cursor position. If `before` is specified, - `after` must not be specified. - title: Before - type: integer - responses: - "200": - $ref: "#/components/responses/CursorPagedPlayHistory" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-recently-played - summary: | - Get Recently Played Tracks - tags: - - Player - x-spotify-docs-console-url: /console/get-recently-played/ - x-spotify-docs-endpoint-name: Get Current User's Recently Played Tracks - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: recently-played - /me/player/repeat: - put: - description: | - Set the repeat mode for the user's playback. Options are repeat-track, - repeat-context, and off. - operationId: set-repeat-mode-on-users-playback - parameters: - - in: query - name: state - required: true - schema: - description: | - **track**, **context** or **off**.
- **track** will repeat the current track.
- **context** will repeat the current context.
- **off** will turn repeat off. - example: context - title: State - type: string - - in: query - name: device_id - required: false - schema: - description: | - The id of the device this command is targeting. If - not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Command sent - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Set Repeat Mode - tags: - - Player - x-spotify-docs-console-url: /console/put-repeat/ - x-spotify-docs-endpoint-name: Set Repeat Mode On User’s Playback - x-spotify-docs-category: Player - x-spotify-docs-display-name: repeat - /me/player/seek: - put: - description: | - Seeks to the given position in the user’s currently playing track. - operationId: seek-to-position-in-currently-playing-track - parameters: - - in: query - name: position_ms - required: true - schema: - description: | - The position in milliseconds to seek to. Must be a - positive number. Passing in a position that is greater than the length of - the track will cause the player to start playing the next song. - example: 25000 - title: Position (ms) - type: integer - - in: query - name: device_id - required: false - schema: - description: | - The id of the device this command is targeting. If - not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Command sent - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Seek To Position - tags: - - Player - x-spotify-docs-console-url: /console/put-seek/ - x-spotify-docs-endpoint-name: Seek To Position In Currently Playing Track - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/playerPolicyList" - x-spotify-docs-category: Player - x-spotify-docs-display-name: seek - /me/player/shuffle: - put: - description: | - Toggle shuffle on or off for user’s playback. - operationId: toggle-shuffle-for-users-playback - parameters: - - in: query - name: state - required: true - schema: - description: | - **true** : Shuffle user's playback.
- **false** : Do not shuffle user's playback. - example: true - title: State - type: boolean - - in: query - name: device_id - required: false - schema: - description: | - The id of the device this command is targeting. If - not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Command sent - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Toggle Playback Shuffle - tags: - - Player - x-spotify-docs-console-url: /console/put-shuffle/?state=true - x-spotify-docs-endpoint-name: Toggle Shuffle For User’s Playback - x-spotify-docs-category: Player - x-spotify-docs-display-name: shuffle - /me/player/volume: - put: - description: | - Set the volume for the user’s current playback device. - operationId: set-volume-for-users-playback - parameters: - - in: query - name: volume_percent - required: true - schema: - description: | - The volume to set. Must be a value from 0 to 100 inclusive. - example: 50 - title: Volume % - type: integer - - in: query - name: device_id - required: false - schema: - description: | - The id of the device this command is targeting. If not supplied, the user's currently active device is the target. - example: 0d1841b0976bae2a3a310dd74c0f3df354899bc8 - title: Device ID - type: string - responses: - "204": - description: Command sent - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-modify-playback-state - summary: | - Set Playback Volume - tags: - - Player - x-spotify-docs-console-url: /console/put-volume/ - x-spotify-docs-endpoint-name: Set Volume For User's Playback - x-spotify-docs-category: Player - x-spotify-docs-display-name: volume - /me/playlists: - get: - description: | - Get a list of the playlists owned or followed by the current Spotify - user. - operationId: get-a-list-of-current-users-playlists - parameters: - - $ref: "#/components/parameters/QueryLimit" - - in: query - name: offset - required: false - schema: - default: 0 - description: | - 'The index of the first playlist to return. Default: - 0 (the first object). Maximum offset: 100.000\. Use with `limit` to get the - next set of playlists.' - example: 5 - title: Offset - type: integer - responses: - "200": - $ref: "#/components/responses/PagedPlaylists" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-read-private - summary: | - Get Current User's Playlists - tags: - - Playlists - - Library - x-spotify-docs-console-url: /console/get-current-user-playlists/ - x-spotify-docs-endpoint-name: Get a List of Current User's Playlists - x-spotify-docs-category: Playlists - x-spotify-docs-display-name: current-user-playlists - /me/shows: - delete: - description: | - Delete one or more shows from current Spotify user's library. - operationId: remove-shows-user - parameters: - - $ref: "#/components/parameters/QueryShowIds" - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - description: Show removed - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Remove User's Saved Shows - tags: - - Shows - - Library - x-spotify-docs-console-url: /console/delete-current-user-saved-shows/?ids=5AvwZVawapvyhJUIx71pdJ%2C6ups0LMt1G8n81XLlkbsPo%2C5AvwZVawapvyhJUIx71pdJ - x-spotify-docs-endpoint-name: Remove User's Saved Shows - get: - description: | - Get a list of shows saved in the current Spotify user's library. Optional parameters can be used to limit the number of shows returned. - operationId: get-users-saved-shows - parameters: - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSavedShowObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Get User's Saved Shows - tags: - - Shows - - Library - x-spotify-docs-console-url: /console/get-current-user-saved-shows/ - x-spotify-docs-endpoint-name: Get User's Saved Shows - put: - description: | - Save one or more shows to current Spotify user's library. - operationId: save-shows-user - parameters: - - $ref: "#/components/parameters/QueryShowIds" - responses: - "200": - description: Show saved - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Save Shows for Current User - tags: - - Shows - - Library - x-spotify-docs-console-url: /console/put-current-user-saved-shows/?ids=5AvwZVawapvyhJUIx71pdJ%2C6ups0LMt1G8n81XLlkbsPo%2C5AvwZVawapvyhJUIx71pdJ - x-spotify-docs-endpoint-name: Save Shows for Current User - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-saved-shows - /me/shows/contains: - get: - description: | - Check if one or more shows is already saved in the current Spotify user's library. - operationId: check-users-saved-shows - parameters: - - $ref: "#/components/parameters/QueryShowIds" - responses: - "200": - $ref: "#/components/responses/ArrayOfBooleans" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Check User's Saved Shows - tags: - - Shows - - Library - x-spotify-docs-console-url: /console/get-current-user-contains-saved-shows/?ids=5AvwZVawapvyhJUIx71pdJ%2C6ups0LMt1G8n81XLlkbsPo%2C5AvwZVawapvyhJUIx71pdJ - x-spotify-docs-endpoint-name: Check User's Saved Shows - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-contains-saved-shows - "/me/top/{type}": - get: - description: | - Get the current user's top artists or tracks based on calculated affinity. - operationId: get-users-top-artists-and-tracks - parameters: - - in: path - name: type - required: true - schema: - description: | - The type of entity to return. Valid values: `artists` or `tracks` - enum: - - artists - - tracks - title: Type - type: string - - in: query - name: time_range - required: false - schema: - default: medium_term - description: | - Over what time frame the affinities are computed. Valid values: `long_term` (calculated from several years of data and including all new data as it becomes available), `medium_term` (approximately last 6 months), `short_term` (approximately last 4 weeks). Default: `medium_term` - example: medium_term - title: Time Range - type: string - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingArtistOrTrackObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-top-read - summary: | - Get User's Top Items - tags: - - Users - - Tracks - - Library - x-spotify-docs-console-url: /console/get-current-user-top-artists-and-tracks/?type=artists - x-spotify-docs-endpoint-name: Get a User's Top Artists and Tracks - x-spotify-docs-category: Personalization - x-spotify-docs-display-name: current-user-top-artists-and-tracks - /me/tracks: - delete: - description: | - Remove one or more tracks from the current user's 'Your Music' library. - operationId: remove-tracks-user - parameters: - - $ref: "#/components/parameters/QueryTrackIds" - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `["4iV5W9uYEdYUVa79Axb7Rh", "1301WleyT98MSxVHPZCA6M"]`
A maximum of 50 items can be specified in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - type: object - responses: - "200": - description: Track removed - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Remove User's Saved Tracks - tags: - - Tracks - - Library - x-spotify-docs-console-url: /console/delete-current-user-saved-tracks/?ids=7ouMYWpwJ422jRcDASZB7P%2C4VqPOruhp5EdPBeR92t6lQ%2C2takcwOaAZWiXQijPHIx7B - x-spotify-docs-endpoint-name: Remove User's Saved Tracks - get: - description: | - Get a list of the songs saved in the current Spotify user's 'Your Music' library. - operationId: get-users-saved-tracks - parameters: - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSavedTrackObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Get User's Saved Tracks - tags: - - Tracks - - Library - x-spotify-docs-console-url: /console/get-current-user-saved-tracks/ - x-spotify-docs-endpoint-name: Get User's Saved Tracks - put: - description: | - Save one or more tracks to the current user's 'Your Music' library. - operationId: save-tracks-user - parameters: - - $ref: "#/components/parameters/QueryTrackIds" - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - ids: - description: | - A JSON array of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `["4iV5W9uYEdYUVa79Axb7Rh", "1301WleyT98MSxVHPZCA6M"]`
A maximum of 50 items can be specified in one request. _**Note**: if the `ids` parameter is present in the query string, any IDs listed here in the body will be ignored._ - items: - type: string - type: array - required: - - uris - type: object - responses: - "200": - description: Track saved - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-modify - summary: | - Save Tracks for Current User - tags: - - Tracks - - Library - x-spotify-docs-console-url: /console/put-current-user-saved-tracks/?ids=7ouMYWpwJ422jRcDASZB7P%2C4VqPOruhp5EdPBeR92t6lQ%2C2takcwOaAZWiXQijPHIx7B - x-spotify-docs-endpoint-name: Save Tracks for User - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-saved-tracks - /me/tracks/contains: - get: - description: | - Check if one or more tracks is already saved in the current Spotify user's 'Your Music' library. - operationId: check-users-saved-tracks - parameters: - - $ref: "#/components/parameters/QueryTrackIds" - responses: - "200": - $ref: "#/components/responses/ArrayOfBooleans" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-library-read - summary: | - Check User's Saved Tracks - tags: - - Tracks - - Library - x-spotify-docs-console-url: /console/get-current-user-contains-saved-tracks/?ids=0udZHhCi7p1YzMlvI4fXoK%2C3SF5puV5eb6bgRSxBeMOk9 - x-spotify-docs-endpoint-name: Check User's Saved Tracks - x-spotify-docs-category: Library - x-spotify-docs-display-name: current-user-contains-saved-tracks - "/playlists/{playlist_id}": - get: - description: | - Get a playlist owned by a Spotify user. - operationId: get-playlist - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - - $ref: "#/components/parameters/QueryMarket" - - in: query - name: fields - required: false - schema: - description: | - Filters for the query: a comma-separated list of the - fields to return. If omitted, all fields are returned. For example, to get - just the playlist''s description and URI: `fields=description,uri`. A dot - separator can be used to specify non-reoccurring fields, while parentheses - can be used to specify reoccurring fields within objects. For example, to - get just the added date and user ID of the adder: `fields=tracks.items(added_at,added_by.id)`. - Use multiple parentheses to drill down into nested objects, for example: `fields=tracks.items(track(name,href,album(name,href)))`. - Fields can be excluded by prefixing them with an exclamation mark, for example: - `fields=tracks.items(track(name,href,album(!name,href)))` - example: items(added_by.id,track(name,href,album(name,href))) - title: Fields - type: string - - $ref: "#/components/parameters/QueryAdditionalTypes" - responses: - "200": - $ref: "#/components/responses/OnePlaylist" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Playlist - tags: - - Playlists - x-spotify-docs-console-url: /console/get-playlist/?playlist_id=59ZbFPES4DQwEjBpWHzrtC&user_id=spotify - x-spotify-docs-endpoint-name: Get a Playlist - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - put: - description: | - Change a playlist's name and public/private state. (The user must, of - course, own the playlist.) - operationId: change-playlist-details - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - requestBody: - content: - application/json: - schema: - additionalProperties: true - example: - description: Updated playlist description - name: Updated Playlist Name - public: false - properties: - collaborative: - description: | - If `true`, the playlist will become collaborative and other users will be able to modify the playlist in their Spotify client.
- _**Note**: You can only set `collaborative` to `true` on non-public playlists._ - type: boolean - description: - description: | - Value for playlist description as displayed in Spotify Clients and in the Web API. - type: string - name: - description: | - The new name for the playlist, for example `"My New Playlist Title"` - type: string - public: - description: | - If `true` the playlist will be public, if `false` it will be private. - type: boolean - type: object - responses: - "200": - description: Playlist updated - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-modify-public - - playlist-modify-private - summary: | - Change Playlist Details - tags: - - Playlists - - Library - x-spotify-docs-console-url: /console/put-playlist/ - x-spotify-docs-endpoint-name: Change a Playlist's Details - x-spotify-docs-category: Playlists - x-spotify-docs-display-name: playlist - "/playlists/{playlist_id}/followers": - delete: - description: | - Remove the current user as a follower of a playlist. - operationId: unfollow-playlist - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - responses: - "200": - description: Playlist unfollowed - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-modify-public - - playlist-modify-private - summary: | - Unfollow Playlist - tags: - - Users - - Playlists - x-spotify-docs-console-url: /console/delete-playlist-followers/?playlist_id=2v3iNvBX8Ay1Gt2uXtUKUT&user_id=jmperezperez - x-spotify-docs-endpoint-name: Unfollow Playlist - put: - description: | - Add the current user as a follower of a playlist. - operationId: follow-playlist - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - requestBody: - content: - application/json: - schema: - additionalProperties: true - example: - public: false - properties: - public: - description: | - Defaults to `true`. If `true` the playlist will be included in user's public playlists, if `false` it will remain private. - type: boolean - type: object - responses: - "200": - description: Playlist followed - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-modify-public - - playlist-modify-private - summary: | - Follow Playlist - tags: - - Users - - Playlists - x-spotify-docs-console-url: /console/put-playlist-followers/?playlist_id=2v3iNvBX8Ay1Gt2uXtUKUT&body-json=%7B%0D%0A++%22public%22%3A+true%0D%0A%7D&user_id=jmperezperez - x-spotify-docs-endpoint-name: Follow a Playlist - x-spotify-docs-category: Follow - x-spotify-docs-display-name: playlist-followers - "/playlists/{playlist_id}/followers/contains": - get: - description: | - Check to see if one or more Spotify users are following a specified playlist. - operationId: check-if-user-follows-playlist - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of [Spotify User IDs](/documentation/web-api/concepts/spotify-uris-ids) ; the ids of the users that you want to check to see if they follow the playlist. Maximum: 5 ids. - example: jmperezperez,thelinmichael,wizzler - title: Spotify user IDs - type: string - responses: - "200": - $ref: "#/components/responses/ArrayOfBooleans" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Check if Users Follow Playlist - tags: - - Users - - Playlists - x-spotify-docs-console-url: /console/get-playlist-followers-contains/?ids=possan,elogain&user_id=jmperezperez&playlist_id=2v3iNvBX8Ay1Gt2uXtUKUT - x-spotify-docs-endpoint-name: Check if Users Follow a Playlist - x-spotify-docs-category: Follow - x-spotify-docs-display-name: playlist-followers-contains - "/playlists/{playlist_id}/images": - get: - description: | - Get the current image associated with a specific playlist. - operationId: get-playlist-cover - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - responses: - "200": - $ref: "#/components/responses/ArrayOfImages" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Playlist Cover Image - tags: - - Playlists - x-spotify-docs-console-url: /console/get-playlist-images?playlist_id=3cEYpjA9oz9GiPac4AsH4n - x-spotify-docs-endpoint-name: Get a Playlist Cover Image - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - put: - description: | - Replace the image used to represent a specific playlist. - operationId: upload-custom-playlist-cover - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - requestBody: - content: - image/jpeg: - schema: - description: Base64 encoded JPEG image data, maximum payload size is 256 KB. - example: /9j/2wCEABoZGSccJz4lJT5CLy8vQkc9Ozs9R0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0cBHCcnMyYzPSYmPUc9Mj1HR0dEREdHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR//dAAQAAf/uAA5BZG9iZQBkwAAAAAH/wAARCAABAAEDACIAAREBAhEB/8QASwABAQAAAAAAAAAAAAAAAAAAAAYBAQAAAAAAAAAAAAAAAAAAAAAQAQAAAAAAAAAAAAAAAAAAAAARAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwAAARECEQA/AJgAH//Z - format: byte - type: string - responses: - "202": - description: Image uploaded - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - ugc-image-upload - - playlist-modify-public - - playlist-modify-private - summary: | - Add Custom Playlist Cover Image - tags: - - Playlists - x-spotify-docs-console-url: /console/put-playlist-images?playlist_id=3cEYpjA9oz9GiPac4AsH4n - x-spotify-docs-endpoint-name: Upload a Custom Playlist Cover Image - x-spotify-docs-category: Playlists - x-spotify-docs-display-name: playlist-images - "/playlists/{playlist_id}/tracks": - delete: - description: | - Remove one or more items from a user's playlist. - operationId: remove-tracks-playlist - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - requestBody: - content: - application/json: - schema: - properties: - snapshot_id: - description: | - The playlist's snapshot ID against which you want to make the changes. - The API will validate that the specified items exist and in the specified positions and make the changes, - even if more recent changes have been made to the playlist. - type: string - tracks: - description: | - An array of objects containing [Spotify URIs](/documentation/web-api/concepts/spotify-uris-ids) of the tracks or episodes to remove. - For example: `{ "tracks": [{ "uri": "spotify:track:4iV5W9uYEdYUVa79Axb7Rh" },{ "uri": "spotify:track:1301WleyT98MSxVHPZCA6M" }] }`. A maximum of 100 objects can be sent at once. - items: - properties: - uri: - description: Spotify URI - type: string - type: object - type: array - required: - - tracks - type: object - responses: - "200": - $ref: "#/components/responses/PlaylistSnapshotId" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-modify-public - - playlist-modify-private - summary: | - Remove Playlist Items - tags: - - Playlists - - Tracks - x-spotify-docs-console-url: /console/delete-playlist-tracks/ - x-spotify-docs-endpoint-name: Remove Items from a Playlist - get: - description: | - Get full details of the items of a playlist owned by a Spotify user. - operationId: get-playlists-tracks - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - - $ref: "#/components/parameters/QueryMarket" - - in: query - name: fields - required: false - schema: - description: | - Filters for the query: a comma-separated list of the - fields to return. If omitted, all fields are returned. For example, to get - just the total number of items and the request limit:
`fields=total,limit`
A - dot separator can be used to specify non-reoccurring fields, while parentheses - can be used to specify reoccurring fields within objects. For example, to - get just the added date and user ID of the adder:
`fields=items(added_at,added_by.id)`
Use - multiple parentheses to drill down into nested objects, for example:
`fields=items(track(name,href,album(name,href)))`
Fields - can be excluded by prefixing them with an exclamation mark, for example:
`fields=items.track.album(!external_urls,images)` - example: items(added_by.id,track(name,href,album(name,href))) - title: Fields - type: string - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - - $ref: "#/components/parameters/QueryAdditionalTypes" - responses: - "200": - $ref: "#/components/responses/PagingPlaylistTrackObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-read-private - summary: | - Get Playlist Items - tags: - - Playlists - - Tracks - x-spotify-docs-console-url: /console/get-playlist-tracks/?playlist_id=21THa8j9TaSGuXYNBU5tsC&user_id=spotify_espa%C3%B1a - x-spotify-docs-endpoint-name: Get a Playlist's Items - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - post: - description: | - Add one or more items to a user's playlist. - operationId: add-tracks-to-playlist - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - - in: query - name: position - required: false - schema: - description: | - The position to insert the items, a zero-based index. For example, to insert the items in the first position: `position=0`; to insert the items in the third position: `position=2`. If omitted, the items will be appended to the playlist. Items are added in the order they are listed in the query string or request body. - example: 0 - title: Position (append by default) - type: integer - - in: query - name: uris - required: false - schema: - description: | - A comma-separated list of [Spotify URIs](/documentation/web-api/concepts/spotify-uris-ids) to add, can be track or episode URIs. For example:
`uris=spotify:track:4iV5W9uYEdYUVa79Axb7Rh, spotify:track:1301WleyT98MSxVHPZCA6M, spotify:episode:512ojhOuo1ktJprKbVcKyQ`
A maximum of 100 items can be added in one request.
- _**Note**: it is likely that passing a large number of item URIs as a query parameter will exceed the maximum length of the request URI. When adding a large number of items, it is recommended to pass them in the request body, see below._ - example: spotify:track:4iV5W9uYEdYUVa79Axb7Rh,spotify:track:1301WleyT98MSxVHPZCA6M - title: Spotify Track URIs - type: string - requestBody: - content: - application/json: - schema: - additionalProperties: true - properties: - position: - description: | - The position to insert the items, a zero-based index. For example, to insert the items in the first position: `position=0` ; to insert the items in the third position: `position=2`. If omitted, the items will be appended to the playlist. Items are added in the order they appear in the uris array. For example: `{"uris": ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh","spotify:track:1301WleyT98MSxVHPZCA6M"], "position": 3}` - type: integer - uris: - description: | - A JSON array of the [Spotify URIs](/documentation/web-api/concepts/spotify-uris-ids) to add. For example: `{"uris": ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh","spotify:track:1301WleyT98MSxVHPZCA6M", "spotify:episode:512ojhOuo1ktJprKbVcKyQ"]}`
A maximum of 100 items can be added in one request. _**Note**: if the `uris` parameter is present in the query string, any URIs listed here in the body will be ignored._ - items: - type: string - type: array - type: object - responses: - "201": - $ref: "#/components/responses/PlaylistSnapshotId" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-modify-public - - playlist-modify-private - summary: | - Add Items to Playlist - tags: - - Playlists - - Tracks - x-spotify-docs-console-url: /console/post-playlist-tracks/ - x-spotify-docs-endpoint-name: Add Items to a Playlist - put: - description: | - Either reorder or replace items in a playlist depending on the request's parameters. - To reorder items, include `range_start`, `insert_before`, `range_length` and `snapshot_id` in the request's body. - To replace items, include `uris` as either a query parameter or in the request's body. - Replacing items in a playlist will overwrite its existing items. This operation can be used for replacing or clearing items in a playlist. -
- **Note**: Replace and reorder are mutually exclusive operations which share the same endpoint, but have different parameters. - These operations can't be applied together in a single request. - operationId: reorder-or-replace-playlists-tracks - parameters: - - $ref: "#/components/parameters/PathPlaylistId" - - in: query - name: uris - required: false - schema: - description: | - A comma-separated list of [Spotify URIs](/documentation/web-api/concepts/spotify-uris-ids) to set, can be track or episode URIs. For example: `uris=spotify:track:4iV5W9uYEdYUVa79Axb7Rh,spotify:track:1301WleyT98MSxVHPZCA6M,spotify:episode:512ojhOuo1ktJprKbVcKyQ`
A maximum of 100 items can be set in one request. - title: Spotify Track URIs - type: string - requestBody: - content: - application/json: - schema: - additionalProperties: true - example: - insert_before: 3 - range_length: 2 - range_start: 1 - properties: - insert_before: - description: | - The position where the items should be inserted.
To reorder the items to the end of the playlist, simply set _insert_before_ to the position after the last item.
Examples:
To reorder the first item to the last position in a playlist with 10 items, set _range_start_ to 0, and _insert_before_ to 10.
To reorder the last item in a playlist with 10 items to the start of the playlist, set _range_start_ to 9, and _insert_before_ to 0. - type: integer - range_length: - description: | - The amount of items to be reordered. Defaults to 1 if not set.
The range of items to be reordered begins from the _range_start_ position, and includes the _range_length_ subsequent items.
Example:
To move the items at index 9-10 to the start of the playlist, _range_start_ is set to 9, and _range_length_ is set to 2. - type: integer - range_start: - description: | - The position of the first item to be reordered. - type: integer - snapshot_id: - description: | - The playlist's snapshot ID against which you want to make the changes. - type: string - uris: - items: - type: string - type: array - type: object - responses: - "200": - $ref: "#/components/responses/PlaylistSnapshotId" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-modify-public - - playlist-modify-private - summary: | - Update Playlist Items - tags: - - Playlists - - Tracks - x-spotify-docs-console-url: /console/put-playlist-tracks/ - x-spotify-docs-endpoint-name: Reorder or Replace a Playlist's Items - x-spotify-docs-category: Playlists - x-spotify-docs-display-name: playlist-tracks - /recommendations: - get: - description: | - Recommendations are generated based on the available information for a given seed entity and matched against similar artists and tracks. If there is sufficient information about the provided seeds, a list of tracks will be returned together with pool size details. - - For artists and tracks that are very new or obscure there might not be enough data to generate a list of tracks. - operationId: get-recommendations - parameters: - - in: query - name: limit - required: false - schema: - default: 20 - description: | - The target size of the list of recommended tracks. For seeds with unusually small pools or when highly restrictive filtering is applied, it may be impossible to generate the requested number of recommended tracks. Debugging information for such cases is available in the response. Default: 20\. Minimum: 1\. Maximum: 100. - example: 10 - maximum: 100 - minimum: 1 - title: Limit - type: integer - - $ref: "#/components/parameters/QueryMarket" - - in: query - name: seed_artists - required: true - schema: - description: | - A comma separated list of [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) for seed artists. Up to 5 seed values may be provided in any combination of `seed_artists`, `seed_tracks` and `seed_genres`. - example: 4NHQUGzhtTLFvgF5SZesLK - title: Spotify Artist ID Seeds - type: string - - in: query - name: seed_genres - required: true - schema: - description: | - A comma separated list of any genres in the set of [available genre seeds](#available-genre-seeds). Up to 5 seed values may be provided in any combination of `seed_artists`, `seed_tracks` and `seed_genres`. - example: classical,country - title: Genres Seeds - type: string - - in: query - name: seed_tracks - required: true - schema: - description: | - A comma separated list of [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) for a seed track. Up to 5 seed values may be provided in any combination of `seed_artists`, `seed_tracks` and `seed_genres`. - example: 0c6xIDDpzE81m2q797ordA - title: Spotify Track ID Seeds - type: string - - in: query - name: min_acousticness - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Acousticness - type: number - - in: query - name: max_acousticness - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Acousticness - type: number - - in: query - name: target_acousticness - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Acousticness - type: number - - in: query - name: min_danceability - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Danceability - type: number - - in: query - name: max_danceability - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Danceability - type: number - - in: query - name: target_danceability - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Danceability - type: number - - in: query - name: min_duration_ms - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - title: Min. Duration (ms) - type: integer - - in: query - name: max_duration_ms - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - title: Max. Duration (ms) - type: integer - - in: query - name: target_duration_ms - required: false - schema: - description: Target duration of the track (ms) - title: Target Duration (ms) - type: integer - - in: query - name: min_energy - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Energy - type: number - - in: query - name: max_energy - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Energy - type: number - - in: query - name: target_energy - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Energy - type: number - - in: query - name: min_instrumentalness - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Instrumentalness - type: number - - in: query - name: max_instrumentalness - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Instrumentalness - type: number - - in: query - name: target_instrumentalness - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Instrumentalness - type: number - - in: query - name: min_key - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 11 - minimum: 0 - title: Min. Key - type: integer - - in: query - name: max_key - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 11 - minimum: 0 - title: Max. Key - type: integer - - in: query - name: target_key - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 11 - minimum: 0 - title: Target Key - type: integer - - in: query - name: min_liveness - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Liveness - type: number - - in: query - name: max_liveness - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Liveness - type: number - - in: query - name: target_liveness - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Liveness - type: number - - in: query - name: min_loudness - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - title: Min. Loudness - type: number - - in: query - name: max_loudness - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - title: Max. Loudness - type: number - - in: query - name: target_loudness - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - title: Target Loudness - type: number - - in: query - name: min_mode - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Mode - type: integer - - in: query - name: max_mode - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Mode - type: integer - - in: query - name: target_mode - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Mode - type: integer - - in: query - name: min_popularity - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 100 - minimum: 0 - title: Min. Popularity - type: integer - - in: query - name: max_popularity - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 100 - minimum: 0 - title: Max. Popularity - type: integer - - in: query - name: target_popularity - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 100 - minimum: 0 - title: Target Popularity - type: integer - - in: query - name: min_speechiness - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Speechiness - type: number - - in: query - name: max_speechiness - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Speechiness - type: number - - in: query - name: target_speechiness - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Speechiness - type: number - - in: query - name: min_tempo - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - title: Min. Tempo - type: number - - in: query - name: max_tempo - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - title: Max. Tempo - type: number - - in: query - name: target_tempo - required: false - schema: - description: Target tempo (BPM) - title: Target Tempo - type: number - - in: query - name: min_time_signature - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 11 - title: Min. Time Signature - type: integer - - in: query - name: max_time_signature - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - title: Max. Time Signature - type: integer - - in: query - name: target_time_signature - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - title: Target Time Signature - type: integer - - in: query - name: min_valence - required: false - schema: - description: | - For each tunable track attribute, a hard floor on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `min_tempo=140` would restrict results to only those tracks with a tempo of greater than 140 beats per minute. - maximum: 1 - minimum: 0 - title: Min. Valence - type: number - - in: query - name: max_valence - required: false - schema: - description: | - For each tunable track attribute, a hard ceiling on the selected track attribute’s value can be provided. See tunable track attributes below for the list of available options. For example, `max_instrumentalness=0.35` would filter out most tracks that are likely to be instrumental. - maximum: 1 - minimum: 0 - title: Max. Valence - type: number - - in: query - name: target_valence - required: false - schema: - description: | - For each of the tunable track attributes (below) a target value may be provided. Tracks with the attribute values nearest to the target values will be preferred. For example, you might request `target_energy=0.6` and `target_danceability=0.8`. All target values will be weighed equally in ranking results. - maximum: 1 - minimum: 0 - title: Target Valence - type: number - responses: - "200": - $ref: "#/components/responses/OneRecommendations" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Recommendations - tags: - - Tracks - x-spotify-docs-console-url: /console/get-recommendations/?seed_artists=4NHQUGzhtTLFvgF5SZesLK&seed_tracks=0c6xIDDpzE81m2q797ordA&min_energy=0.4&min_popularity=50&market=US - x-spotify-docs-endpoint-name: Get Recommendations - x-spotify-docs-category: Browse - x-spotify-docs-display-name: recommendations - /recommendations/available-genre-seeds: - get: - description: | - Retrieve a list of available genres seed parameter values for [recommendations](/documentation/web-api/reference/get-recommendations). - operationId: get-recommendation-genres - responses: - "200": - $ref: "#/components/responses/ManyGenres" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Available Genre Seeds - tags: - - Genres - x-spotify-docs-console-url: /console/get-available-genre-seeds/ - x-spotify-docs-endpoint-name: Get Recommendation Genres - x-spotify-docs-category: Browse - x-spotify-docs-display-name: available-genre-seeds - /search: - get: - description: | - Get Spotify catalog information about albums, artists, playlists, tracks, shows, episodes or audiobooks - that match a keyword string.
- **Note: Audiobooks are only available for the US, UK, Ireland, New Zealand and Australia markets.** - operationId: search - parameters: - - in: query - name: q - required: true - schema: - description: | - Your search query. - - You can narrow down your search using field filters. The available filters are `album`, `artist`, `track`, `year`, `upc`, `tag:hipster`, `tag:new`, `isrc`, and `genre`. Each field filter only applies to certain result types. - - The `artist` and `year` filters can be used while searching albums, artists and tracks. You can filter on a single `year` or a range (e.g. 1955-1960).
- The `album` filter can be used while searching albums and tracks.
- The `genre` filter can be used while searching artists and tracks.
- The `isrc` and `track` filters can be used while searching tracks.
- The `upc`, `tag:new` and `tag:hipster` filters can only be used while searching albums. The `tag:new` filter will return albums released in the past two weeks and `tag:hipster` can be used to return only albums with the lowest 10% popularity.
- example: remaster%20track:Doxy%20artist:Miles%20Davis - title: Query - type: string - - explode: false - in: query - name: type - required: true - schema: - description: | - A comma-separated list of item types to search across. Search results include hits - from all the specified item types. For example: `q=abacab&type=album,track` returns - both albums and tracks matching "abacab". - items: - enum: - - album - - artist - - playlist - - track - - show - - episode - - audiobook - type: string - title: Item type - type: array - - $ref: "#/components/parameters/QueryMarket" - - in: query - name: limit - required: false - schema: - default: 20 - description: | - The maximum number of results to return in each item type. - example: 10 - maximum: 50 - minimum: 0 - title: Limit - type: integer - - in: query - name: offset - required: false - schema: - default: 0 - description: | - The index of the first result to return. Use - with limit to get the next page of search results. - example: 5 - maximum: 1000 - minimum: 0 - title: Offset - type: integer - - in: query - name: include_external - required: false - schema: - description: | - If `include_external=audio` is specified it signals that the client can play externally hosted audio content, and marks - the content as playable in the response. By default externally hosted audio content is marked as unplayable in the response. - enum: - - audio - title: Include External - type: string - responses: - "200": - $ref: "#/components/responses/SearchItems" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Search for Item - tags: - - Search - x-spotify-docs-console-url: /console/get-search-item/?q=tania+bowra&type=artist - x-spotify-docs-endpoint-name: Search for an Item - x-spotify-docs-category: Search - x-spotify-docs-display-name: search-item - /shows: - get: - description: | - Get Spotify catalog information for several shows based on their Spotify IDs. - operationId: get-multiple-shows - parameters: - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryShowIds" - responses: - "200": - $ref: "#/components/responses/ManySimplifiedShows" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Several Shows - tags: - - Shows - x-spotify-docs-console-url: /console/get-several-shows/?ids=5CfCWKI5pZ28U0uOzXkDHe,5as3aKmN2k11yfDDDSrvaZ - x-spotify-docs-endpoint-name: Get Multiple Shows - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Shows - x-spotify-docs-display-name: several-shows - "/shows/{id}": - get: - description: | - Get Spotify catalog information for a single show identified by its - unique Spotify ID. - operationId: get-a-show - parameters: - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/PathShowId" - responses: - "200": - $ref: "#/components/responses/OneShow" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-playback-position - summary: | - Get Show - tags: - - Shows - x-spotify-docs-console-url: /console/get-show/?id=38bS44xjbVVZ3No3ByF1dJ - x-spotify-docs-endpoint-name: Get a Show - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Shows - x-spotify-docs-display-name: show - "/shows/{id}/episodes": - get: - description: | - Get Spotify catalog information about an show’s episodes. Optional parameters can be used to limit the number of episodes returned. - operationId: get-a-shows-episodes - parameters: - - $ref: "#/components/parameters/PathShowId" - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryLimit" - - $ref: "#/components/parameters/QueryOffset" - responses: - "200": - $ref: "#/components/responses/PagingSimplifiedEpisodeObject" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - user-read-playback-position - summary: | - Get Show Episodes - tags: - - Shows - - Episodes - x-spotify-docs-console-url: /console/get-show-episodes/ - x-spotify-docs-endpoint-name: Get a Show's Episodes - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Shows - x-spotify-docs-display-name: show-episodes - /tracks: - get: - description: | - Get Spotify catalog information for multiple tracks based on their Spotify IDs. - operationId: get-several-tracks - parameters: - - $ref: "#/components/parameters/QueryMarket" - - $ref: "#/components/parameters/QueryTrackIds" - responses: - "200": - $ref: "#/components/responses/ManyTracks" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Several Tracks - tags: - - Tracks - x-spotify-docs-console-url: /console/get-several-tracks/?ids=3n3Ppam7vgaVa1iaRUc9Lp,3twNvmDtFQtAd5gMKedhLD - x-spotify-docs-endpoint-name: Get Several Tracks - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Tracks - x-spotify-docs-display-name: several-tracks - "/tracks/{id}": - get: - description: | - Get Spotify catalog information for a single track identified by its - unique Spotify ID. - operationId: get-track - parameters: - - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) - for the track. - example: 11dFghVXANMlKmJXsNCbNl - title: Spotify Track ID - type: string - - $ref: "#/components/parameters/QueryMarket" - responses: - "200": - $ref: "#/components/responses/OneTrack" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get Track - tags: - - Tracks - x-spotify-docs-console-url: /console/get-track/?id=3n3Ppam7vgaVa1iaRUc9Lp - x-spotify-docs-endpoint-name: Get a Track - x-spotify-policy-list: - $ref: "#/components/x-spotify-policy/metadataPolicyList" - x-spotify-docs-category: Tracks - x-spotify-docs-display-name: track - "/users/{user_id}": - get: - description: | - Get public profile information about a Spotify user. - operationId: get-users-profile - parameters: - - $ref: "#/components/parameters/PathUserId" - responses: - "200": - $ref: "#/components/responses/OnePublicUser" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: [] - summary: | - Get User's Profile - tags: - - Users - x-spotify-docs-console-url: /console/get-users-profile/?user_id=wizzler - x-spotify-docs-endpoint-name: Get a User's Profile - x-spotify-docs-category: Users Profile - x-spotify-docs-display-name: users-profile - "/users/{user_id}/playlists": - get: - description: | - Get a list of the playlists owned or followed by a Spotify user. - operationId: get-list-users-playlists - parameters: - - $ref: "#/components/parameters/PathUserId" - - $ref: "#/components/parameters/QueryLimit" - - in: query - name: offset - required: false - schema: - default: 0 - description: | - The index of the first playlist to return. Default: - 0 (the first object). Maximum offset: 100.000\. Use with `limit` to get the - next set of playlists. - example: 5 - title: Offset - type: integer - responses: - "200": - $ref: "#/components/responses/PagedPlaylists" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-read-private - - playlist-read-collaborative - summary: | - Get User's Playlists - tags: - - Playlists - - Users - x-spotify-docs-console-url: /console/get-playlists/?user_id=wizzler - x-spotify-docs-endpoint-name: Get a List of a User's Playlists - post: - description: | - Create a playlist for a Spotify user. (The playlist will be empty until - you [add tracks](/documentation/web-api/reference/add-tracks-to-playlist).) - operationId: create-playlist - parameters: - - $ref: "#/components/parameters/PathUserId" - requestBody: - content: - application/json: - schema: - additionalProperties: true - example: - description: New playlist description - name: New Playlist - public: false - properties: - collaborative: - description: | - Defaults to `false`. If `true` the playlist will be collaborative. _**Note**: to create a collaborative playlist you must also set `public` to `false`. To create collaborative playlists you must have granted `playlist-modify-private` and `playlist-modify-public` [scopes](/documentation/web-api/concepts/scopes/#list-of-scopes)._ - type: boolean - description: - description: | - value for playlist description as displayed in Spotify Clients and in the Web API. - type: string - name: - description: | - The name for the new playlist, for example `"Your Coolest Playlist"`. This name does not need to be unique; a user may have several playlists with the same name. - type: string - public: - description: | - Defaults to `true`. If `true` the playlist will be public, if `false` it will be private. To be able to create private playlists, the user must have granted the `playlist-modify-private` [scope](/documentation/web-api/concepts/scopes/#list-of-scopes) - type: boolean - required: - - name - type: object - responses: - "201": - $ref: "#/components/responses/OnePlaylist" - "401": - $ref: "#/components/responses/Unauthorized" - "403": - $ref: "#/components/responses/Forbidden" - "429": - $ref: "#/components/responses/TooManyRequests" - security: - - oauth_2_0: - - playlist-modify-public - - playlist-modify-private - summary: | - Create Playlist - tags: - - Playlists - - Library - x-spotify-docs-console-url: /console/post-playlists/ - x-spotify-docs-endpoint-name: Create a Playlist - x-spotify-docs-category: Playlists - x-spotify-docs-display-name: playlists -components: - parameters: - PathAlbumId: - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) of the album. - example: 4aawyAB9vmqN3uQ7FjRGTy - title: Spotify Album ID - type: string - PathArtistId: - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) of the artist. - example: 0TnOYISbd1XYRBk9myaseg - title: Spotify Artist ID - type: string - PathAudiobookId: - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) - for the audiobook. - example: 7iHfbu1YPACw6oZPAFJtqe - title: Spotify Audiobook ID - type: string - PathChapterId: - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) - for the chapter. - example: 0D5wENdkdwbqlrHoaJ9g29 - title: Spotify Chapter ID - type: string - PathPlaylistId: - in: path - name: playlist_id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) of the playlist. - example: 3cEYpjA9oz9GiPac4AsH4n - title: Playlist ID - type: string - PathShowId: - in: path - name: id - required: true - schema: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) - for the show. - example: 38bS44xjbVVZ3No3ByF1dJ - title: Spotify Show ID - type: string - PathUserId: - in: path - name: user_id - required: true - schema: - description: | - The user's [Spotify user ID](/documentation/web-api/concepts/spotify-uris-ids). - example: smedjan - title: User ID - type: string - QueryAdditionalTypes: - in: query - name: additional_types - required: false - schema: - description: | - A comma-separated list of item types that your client supports besides the default `track` type. Valid types are: `track` and `episode`.
- _**Note**: This parameter was introduced to allow existing clients to maintain their current behaviour and might be deprecated in the future._
- In addition to providing this parameter, make sure that your client properly handles cases of new types in the future by checking against the `type` field of each object. - title: Additional Types - type: string - QueryAlbumIds: - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) for the albums. Maximum: 20 IDs. - example: 382ObEPsp2rxGrnsizN5TX,1A2GTWGtFfWp7KSQTwWOyo,2noRn2Aes5aoNVsU6iWThc - title: Spotify Album IDs - type: string - QueryAudiobookIds: - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `ids=18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ`. Maximum: 50 IDs. - example: 18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ,7iHfbu1YPACw6oZPAFJtqe - title: Spotify Audiobook IDs - type: string - QueryChapterIds: - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `ids=0IsXVP0JmcB2adSE338GkK,3ZXb8FKZGU0EHALYX6uCzU`. Maximum: 50 IDs. - example: 0IsXVP0JmcB2adSE338GkK,3ZXb8FKZGU0EHALYX6uCzU,0D5wENdkdwbqlrHoaJ9g29 - title: Spotify Chapter IDs - type: string - QueryIncludeGroups: - in: query - name: include_groups - required: false - schema: - description: | - A comma-separated list of keywords that will be used to filter the response. If not supplied, all album types will be returned.
- Valid values are:
- `album`
- `single`
- `appears_on`
- `compilation`
For example: `include_groups=album,single`. - example: single,appears_on - title: Groups to include (single, album, appears_on, compilation) - type: string - QueryLimit: - in: query - name: limit - required: false - schema: - default: 20 - description: | - The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. - example: 10 - maximum: 50 - minimum: 0 - title: Limit - type: integer - QueryMarket: - in: query - name: market - required: false - schema: - description: | - An [ISO 3166-1 alpha-2 country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). - If a country code is specified, only content that is available in that market will be returned.
- If a valid user access token is specified in the request header, the country associated with - the user account will take priority over this parameter.
- _**Note**: If neither market or user country are provided, the content is considered unavailable for the client._
- Users can view the country that is associated with their account in the [account settings](https://www.spotify.com/se/account/overview/). - example: ES - title: Market - type: string - QueryOffset: - in: query - name: offset - required: false - schema: - default: 0 - description: | - The index of the first item to return. Default: 0 (the first item). Use with limit to get the next set of items. - example: 5 - title: Offset - type: integer - QueryShowIds: - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids) for the shows. Maximum: 50 IDs. - example: 5CfCWKI5pZ28U0uOzXkDHe,5as3aKmN2k11yfDDDSrvaZ - title: Ids - type: string - QueryTrackIds: - in: query - name: ids - required: true - schema: - description: | - A comma-separated list of the [Spotify IDs](/documentation/web-api/concepts/spotify-uris-ids). For example: `ids=4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M`. Maximum: 50 IDs. - example: 7ouMYWpwJ422jRcDASZB7P,4VqPOruhp5EdPBeR92t6lQ,2takcwOaAZWiXQijPHIx7B - title: Spotify Track IDs - type: string - responses: - ArrayOfBooleans: - content: - application/json: - schema: - example: - - false - - true - items: - type: boolean - type: array - description: Array of booleans - ArrayOfImages: - content: - application/json: - schema: - items: - $ref: "#/components/schemas/ImageObject" - type: array - description: A set of images - BadRequest: - content: - application/json: - schema: - properties: - error: - $ref: "#/components/schemas/ErrorObject" - required: - - error - type: object - description: | - The request contains malformed data in path, query parameters, or body. - CursorPagedArtists: - content: - application/json: - schema: - properties: - artists: - $ref: "#/components/schemas/CursorPagingSimplifiedArtistObject" - required: - - artists - type: object - description: A paged set of artists - CursorPagedPlayHistory: - content: - application/json: - schema: - $ref: "#/components/schemas/CursorPagingPlayHistoryObject" - description: A paged set of tracks - Forbidden: - content: - application/json: - schema: - properties: - error: - $ref: "#/components/schemas/ErrorObject" - required: - - error - type: object - description: | - Bad OAuth request (wrong consumer key, bad nonce, expired - timestamp...). Unfortunately, re-authenticating the user won't help here. - ManyAlbums: - content: - application/json: - schema: - properties: - albums: - items: - $ref: "#/components/schemas/AlbumObject" - type: array - required: - - albums - type: object - description: A set of albums - ManyArtists: - content: - application/json: - schema: - properties: - artists: - items: - $ref: "#/components/schemas/ArtistObject" - type: array - required: - - artists - type: object - description: A set of artists - ManyAudioFeatures: - content: - application/json: - schema: - properties: - audio_features: - items: - $ref: "#/components/schemas/AudioFeaturesObject" - type: array - required: - - audio_features - type: object - description: A set of audio features - ManyAudiobooks: - content: - application/json: - schema: - properties: - audiobooks: - items: - $ref: "#/components/schemas/AudiobookObject" - type: array - required: - - audiobooks - type: object - description: A set of audiobooks - ManyChapters: - content: - application/json: - schema: - properties: - chapters: - items: - $ref: "#/components/schemas/ChapterObject" - type: array - required: - - chapters - type: object - description: A set of chapters - ManyDevices: - content: - application/json: - schema: - properties: - devices: - items: - $ref: "#/components/schemas/DeviceObject" - type: array - required: - - devices - type: object - description: A set of devices - ManyEpisodes: - content: - application/json: - schema: - properties: - episodes: - items: - $ref: "#/components/schemas/EpisodeObject" - type: array - required: - - episodes - type: object - description: A set of episodes - ManyGenres: - content: - application/json: - schema: - properties: - genres: - example: - - alternative - - samba - items: - type: string - type: array - required: - - genres - type: object - description: A set of genres - ManySimplifiedShows: - content: - application/json: - schema: - properties: - shows: - items: - $ref: "#/components/schemas/SimplifiedShowObject" - type: array - required: - - shows - type: object - description: A set of shows - ManyTracks: - content: - application/json: - schema: - properties: - tracks: - items: - $ref: "#/components/schemas/TrackObject" - type: array - required: - - tracks - type: object - description: A set of tracks - NotFound: - content: - application/json: - schema: - properties: - error: - $ref: "#/components/schemas/ErrorObject" - required: - - error - type: object - description: | - The requested resource cannot be found. - OneAlbum: - content: - application/json: - schema: - $ref: "#/components/schemas/AlbumObject" - description: An album - OneArtist: - content: - application/json: - schema: - $ref: "#/components/schemas/ArtistObject" - description: An artist - OneAudioAnalysis: - content: - application/json: - schema: - $ref: "#/components/schemas/AudioAnalysisObject" - description: Audio analysis for one track - OneAudioFeatures: - content: - application/json: - schema: - $ref: "#/components/schemas/AudioFeaturesObject" - description: Audio features for one track - OneAudiobook: - content: - application/json: - schema: - $ref: "#/components/schemas/AudiobookObject" - description: An Audiobook - OneCategory: - content: - application/json: - schema: - $ref: "#/components/schemas/CategoryObject" - description: A category - OneChapter: - content: - application/json: - schema: - $ref: "#/components/schemas/ChapterObject" - description: A Chapter - OneCurrentlyPlaying: - content: - application/json: - schema: - $ref: "#/components/schemas/CurrentlyPlayingContextObject" - description: Information about playback - OneCurrentlyPlayingTrack: - content: - application/json: - schema: - $ref: "#/components/schemas/CurrentlyPlayingContextObject" - description: Information about the currently playing track - OneEpisode: - content: - application/json: - schema: - $ref: "#/components/schemas/EpisodeObject" - description: An episode - OnePlaylist: - content: - application/json: - schema: - $ref: "#/components/schemas/PlaylistObject" - description: A playlist - OnePrivateUser: - content: - application/json: - schema: - $ref: "#/components/schemas/PrivateUserObject" - description: A user - OnePublicUser: - content: - application/json: - schema: - $ref: "#/components/schemas/PublicUserObject" - description: A user - OneRecommendations: - content: - application/json: - schema: - $ref: "#/components/schemas/RecommendationsObject" - description: A set of recommendations - OneShow: - content: - application/json: - schema: - $ref: "#/components/schemas/ShowObject" - description: A show - OneTrack: - content: - application/json: - schema: - $ref: "#/components/schemas/TrackObject" - description: A track - PagedAlbums: - content: - application/json: - schema: - properties: - albums: - $ref: "#/components/schemas/PagingSimplifiedAlbumObject" - required: - - albums - type: object - description: A paged set of albums - PagedCategories: - content: - application/json: - schema: - properties: - categories: - $ref: "#/components/schemas/PagingObject" - required: - - categories - type: object - description: A paged set of categories - PagedFeaturedPlaylists: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingFeaturedPlaylistObject" - description: A paged set of playlists - PagedPlaylists: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingPlaylistObject" - description: A paged set of playlists - PagingArtistOrTrackObject: - content: - application/json: - schema: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - discriminator: - propertyName: type - oneOf: - - $ref: "#/components/schemas/ArtistObject" - - $ref: "#/components/schemas/TrackObject" - type: object - type: array - type: object - type: object - description: Pages of artists or tracks - PagingPlaylistTrackObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingPlaylistTrackObject" - description: Pages of tracks - PagingSavedAlbumObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSavedAlbumObject" - description: Pages of albums - PagingSavedEpisodeObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSavedEpisodeObject" - description: Pages of episodes - PagingSavedShowObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSavedShowObject" - description: Pages of shows - PagingSavedTrackObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSavedTrackObject" - description: Pages of tracks - PagingSimplifiedAlbumObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSimplifiedAlbumObject" - description: Pages of albums - PagingSimplifiedArtistObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSimplifiedArtistObject" - description: Pages of artists - PagingSimplifiedAudiobookObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSimplifiedAudiobookObject" - description: Pages of audiobooks - PagingSimplifiedChapterObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSimplifiedChapterObject" - description: Pages of chapters - PagingSimplifiedEpisodeObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSimplifiedEpisodeObject" - description: Pages of episodes - PagingSimplifiedShowObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSimplifiedShowObject" - description: Pages of shows - PagingSimplifiedTrackObject: - content: - application/json: - schema: - $ref: "#/components/schemas/PagingSimplifiedTrackObject" - description: Pages of tracks - PlaylistSnapshotId: - content: - application/json: - schema: - properties: - snapshot_id: - example: abc - type: string - type: object - description: A snapshot ID for the playlist - Queue: - content: - application/json: - schema: - $ref: "#/components/schemas/QueueObject" - description: Information about the queue - SearchItems: - content: - application/json: - schema: - properties: - albums: - $ref: "#/components/schemas/PagingSimplifiedAlbumObject" - artists: - $ref: "#/components/schemas/PagingArtistObject" - audiobooks: - $ref: "#/components/schemas/PagingSimplifiedAudiobookObject" - episodes: - $ref: "#/components/schemas/PagingSimplifiedEpisodeObject" - playlists: - $ref: "#/components/schemas/PagingPlaylistObject" - shows: - $ref: "#/components/schemas/PagingSimplifiedShowObject" - tracks: - $ref: "#/components/schemas/PagingTrackObject" - type: object - description: Search response - TooManyRequests: - content: - application/json: - schema: - properties: - error: - $ref: "#/components/schemas/ErrorObject" - required: - - error - type: object - description: | - The app has exceeded its rate limits. - Unauthorized: - content: - application/json: - schema: - properties: - error: - $ref: "#/components/schemas/ErrorObject" - required: - - error - type: object - description: | - Bad or expired token. This can happen if the user revoked a token or - the access token has expired. You should re-authenticate the user. - schemas: - AlbumBase: - properties: - album_type: - description: | - The type of the album. - enum: - - album - - single - - compilation - example: compilation - type: string - available_markets: - description: | - The markets in which the album is available: [ISO 3166-1 alpha-2 country codes](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). _**NOTE**: an album is considered available in a market when at least 1 of its tracks is available in that market._ - example: - - CA - - BR - - IT - items: - type: string - type: array - copyrights: - description: | - The copyright statements of the album. - items: - $ref: "#/components/schemas/CopyrightObject" - type: array - external_ids: - allOf: - - $ref: "#/components/schemas/ExternalIdObject" - description: | - Known external IDs for the album. - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known external URLs for this album. - genres: - description: | - A list of the genres the album is associated with. If not yet classified, the array is empty. - example: - - Egg punk - - Noise rock - items: - type: string - type: array - href: - description: | - A link to the Web API endpoint providing full details of the album. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the album. - example: 2up3OPMp9Tb4dAKM2erWXQ - type: string - images: - description: | - The cover art for the album in various sizes, widest first. - items: - $ref: "#/components/schemas/ImageObject" - type: array - label: - description: | - The label associated with the album. - type: string - name: - description: | - The name of the album. In case of an album takedown, the value may be an empty string. - type: string - popularity: - description: | - The popularity of the album. The value will be between 0 and 100, with 100 being the most popular. - type: integer - release_date: - description: | - The date the album was first released. - example: 1981-12 - type: string - release_date_precision: - description: | - The precision with which `release_date` value is known. - enum: - - year - - month - - day - example: year - type: string - restrictions: - allOf: - - $ref: "#/components/schemas/AlbumRestrictionObject" - description: | - Included in the response when a content restriction is applied. - total_tracks: - description: The number of tracks in the album. - example: 9 - type: integer - type: - description: | - The object type. - enum: - - album - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the album. - example: spotify:album:2up3OPMp9Tb4dAKM2erWXQ - type: string - required: - - album_type - - total_tracks - - available_markets - - external_urls - - href - - id - - images - - name - - release_date - - release_date_precision - - type - - uri - type: object - AlbumObject: - allOf: - - $ref: "#/components/schemas/AlbumBase" - - properties: - artists: - description: | - The artists of the album. Each artist object includes a link in `href` to more detailed information about the artist. - items: - $ref: "#/components/schemas/ArtistObject" - type: array - tracks: - $ref: "#/components/schemas/PagingSimplifiedTrackObject" - description: | - The tracks of the album. - type: object - x-spotify-docs-type: AlbumObject - AlbumRestrictionObject: - properties: - reason: - description: | - The reason for the restriction. Albums may be restricted if the content is not available in a given market, to the user's subscription type, or when the user's account is set to not play explicit content. - Additional reasons may be added in the future. - enum: - - market - - product - - explicit - type: string - type: object - x-spotify-docs-type: AlbumRestrictionObject - ArtistObject: - properties: - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known external URLs for this artist. - followers: - allOf: - - $ref: "#/components/schemas/FollowersObject" - description: | - Information about the followers of the artist. - genres: - description: | - A list of the genres the artist is associated with. If not yet classified, the array is empty. - example: - - Prog rock - - Grunge - items: - type: string - type: array - href: - description: | - A link to the Web API endpoint providing full details of the artist. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the artist. - type: string - images: - description: | - Images of the artist in various sizes, widest first. - items: - $ref: "#/components/schemas/ImageObject" - type: array - name: - description: | - The name of the artist. - type: string - popularity: - description: | - The popularity of the artist. The value will be between 0 and 100, with 100 being the most popular. The artist's popularity is calculated from the popularity of all the artist's tracks. - type: integer - type: - description: | - The object type. - enum: - - artist - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the artist. - type: string - type: object - x-spotify-docs-type: ArtistObject - AudioAnalysisObject: - properties: - bars: - description: The time intervals of the bars throughout the track. A bar (or measure) is a segment of time defined as a given number of beats. - items: - $ref: "#/components/schemas/TimeIntervalObject" - type: array - beats: - description: The time intervals of beats throughout the track. A beat is the basic time unit of a piece of music; for example, each tick of a metronome. Beats are typically multiples of tatums. - items: - $ref: "#/components/schemas/TimeIntervalObject" - type: array - meta: - properties: - analysis_time: - description: The amount of time taken to analyze this track. - example: 6.93906 - type: number - analyzer_version: - description: The version of the Analyzer used to analyze this track. - example: 4.0.0 - type: string - detailed_status: - description: A detailed status code for this track. If analysis data is missing, this code may explain why. - example: OK - type: string - input_process: - description: The method used to read the track's audio data. - example: libvorbisfile L+R 44100->22050 - type: string - platform: - description: The platform used to read the track's audio data. - example: Linux - type: string - status_code: - description: The return code of the analyzer process. 0 if successful, 1 if any errors occurred. - example: 0 - type: integer - timestamp: - description: The Unix timestamp (in seconds) at which this track was analyzed. - example: 1495193577 - type: integer - type: object - sections: - description: Sections are defined by large variations in rhythm or timbre, e.g. chorus, verse, bridge, guitar solo, etc. Each section contains its own descriptions of tempo, key, mode, time_signature, and loudness. - items: - $ref: "#/components/schemas/SectionObject" - type: array - segments: - description: Each segment contains a roughly conisistent sound throughout its duration. - items: - $ref: "#/components/schemas/SegmentObject" - type: array - tatums: - description: A tatum represents the lowest regular pulse train that a listener intuitively infers from the timing of perceived musical events (segments). - items: - $ref: "#/components/schemas/TimeIntervalObject" - type: array - track: - properties: - analysis_channels: - description: The number of channels used for analysis. If 1, all channels are summed together to mono before analysis. - example: 1 - type: integer - analysis_sample_rate: - description: The sample rate used to decode and analyze this track. May differ from the actual sample rate of this track available on Spotify. - example: 22050 - type: integer - code_version: - description: A version number for the Echo Nest Musical Fingerprint format used in the codestring field. - example: 3.15 - type: number - codestring: - description: An [Echo Nest Musical Fingerprint (ENMFP)](https://academiccommons.columbia.edu/doi/10.7916/D8Q248M4) codestring for this track. - type: string - duration: - description: Length of the track in seconds. - example: 207.95985 - type: number - echoprint_version: - description: A version number for the EchoPrint format used in the echoprintstring field. - example: 4.15 - type: number - echoprintstring: - description: An [EchoPrint](https://github.com/spotify/echoprint-codegen) codestring for this track. - type: string - end_of_fade_in: - description: The time, in seconds, at which the track's fade-in period ends. If the track has no fade-in, this will be 0.0. - example: 0 - type: number - key: - $ref: "#/components/schemas/Key" - key_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the `key`. - example: 0.408 - maximum: 1 - minimum: 0 - type: number - loudness: - $ref: "#/components/schemas/Loudness" - mode: - $ref: "#/components/schemas/Mode" - mode_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the `mode`. - example: 0.485 - maximum: 1 - minimum: 0 - type: number - num_samples: - description: The exact number of audio samples analyzed from this track. See also `analysis_sample_rate`. - example: 4585515 - type: integer - offset_seconds: - description: An offset to the start of the region of the track that was analyzed. (As the entire track is analyzed, this should always be 0.) - example: 0 - type: integer - rhythm_version: - description: A version number for the Rhythmstring used in the rhythmstring field. - example: 1 - type: number - rhythmstring: - description: A Rhythmstring for this track. The format of this string is similar to the Synchstring. - type: string - sample_md5: - description: This field will always contain the empty string. - type: string - start_of_fade_out: - description: The time, in seconds, at which the track's fade-out period starts. If the track has no fade-out, this should match the track's length. - example: 201.13705 - type: number - synch_version: - description: A version number for the Synchstring used in the synchstring field. - example: 1 - type: number - synchstring: - description: A [Synchstring](https://github.com/echonest/synchdata) for this track. - type: string - tempo: - $ref: "#/components/schemas/Tempo" - tempo_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the `tempo`. - example: 0.73 - maximum: 1 - minimum: 0 - type: number - time_signature: - $ref: "#/components/schemas/TimeSignature" - time_signature_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the `time_signature`. - example: 0.994 - maximum: 1 - minimum: 0 - type: number - window_seconds: - description: The length of the region of the track was analyzed, if a subset of the track was analyzed. (As the entire track is analyzed, this should always be 0.) - example: 0 - type: integer - type: object - type: object - x-spotify-docs-type: AudioAnalysisObject - AudioFeaturesObject: - properties: - acousticness: - description: | - A confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic. - example: 0.00242 - format: float - maximum: 1 - minimum: 0 - type: number - x-spotify-docs-type: Float - analysis_url: - description: | - A URL to access the full audio analysis of this track. An access token is required to access this data. - example: | - https://api.spotify.com/v1/audio-analysis/2takcwOaAZWiXQijPHIx7B - type: string - danceability: - description: | - Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable. - example: 0.585 - format: float - type: number - x-spotify-docs-type: Float - duration_ms: - description: | - The duration of the track in milliseconds. - example: 237040 - type: integer - energy: - description: | - Energy is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy. - example: 0.842 - format: float - type: number - x-spotify-docs-type: Float - id: - description: | - The Spotify ID for the track. - example: 2takcwOaAZWiXQijPHIx7B - type: string - instrumentalness: - description: | - Predicts whether a track contains no vocals. "Ooh" and "aah" sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly "vocal". The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0. - example: 0.00686 - format: float - type: number - x-spotify-docs-type: Float - key: - $ref: "#/components/schemas/Key" - liveness: - description: | - Detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live. - example: 0.0866 - format: float - type: number - x-spotify-docs-type: Float - loudness: - $ref: "#/components/schemas/Loudness" - mode: - $ref: "#/components/schemas/Mode" - speechiness: - description: | - Speechiness detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks. - example: 0.0556 - format: float - type: number - x-spotify-docs-type: Float - tempo: - $ref: "#/components/schemas/Tempo" - time_signature: - $ref: "#/components/schemas/TimeSignature" - track_href: - description: | - A link to the Web API endpoint providing full details of the track. - example: | - https://api.spotify.com/v1/tracks/2takcwOaAZWiXQijPHIx7B - type: string - type: - description: | - The object type. - enum: - - audio_features - type: string - uri: - description: | - The Spotify URI for the track. - example: spotify:track:2takcwOaAZWiXQijPHIx7B - type: string - valence: - description: | - A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry). - example: 0.428 - format: float - maximum: 1 - minimum: 0 - type: number - x-spotify-docs-type: Float - type: object - x-spotify-docs-type: AudioFeaturesObject - AudiobookBase: - properties: - authors: - description: | - The author(s) for the audiobook. - items: - $ref: "#/components/schemas/AuthorObject" - type: array - available_markets: - description: | - A list of the countries in which the audiobook can be played, identified by their [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code. - items: - type: string - type: array - copyrights: - description: | - The copyright statements of the audiobook. - items: - $ref: "#/components/schemas/CopyrightObject" - type: array - description: - description: | - A description of the audiobook. HTML tags are stripped away from this field, use `html_description` field in case HTML tags are needed. - type: string - edition: - description: | - The edition of the audiobook. - example: Unabridged - type: string - explicit: - description: | - Whether or not the audiobook has explicit content (true = yes it does; false = no it does not OR unknown). - type: boolean - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - External URLs for this audiobook. - href: - description: | - A link to the Web API endpoint providing full details of the audiobook. - type: string - html_description: - description: | - A description of the audiobook. This field may contain HTML tags. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the audiobook. - type: string - images: - description: | - The cover art for the audiobook in various sizes, widest first. - items: - $ref: "#/components/schemas/ImageObject" - type: array - languages: - description: | - A list of the languages used in the audiobook, identified by their [ISO 639](https://en.wikipedia.org/wiki/ISO_639) code. - items: - type: string - type: array - media_type: - description: | - The media type of the audiobook. - type: string - name: - description: | - The name of the audiobook. - type: string - narrators: - description: | - The narrator(s) for the audiobook. - items: - $ref: "#/components/schemas/NarratorObject" - type: array - publisher: - description: | - The publisher of the audiobook. - type: string - total_chapters: - description: | - The number of chapters in this audiobook. - type: integer - type: - description: | - The object type. - enum: - - audiobook - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the audiobook. - type: string - required: - - authors - - available_markets - - copyrights - - description - - explicit - - external_urls - - href - - html_description - - id - - images - - languages - - media_type - - name - - narrators - - publisher - - total_chapters - - type - - uri - type: object - AudiobookObject: - allOf: - - $ref: "#/components/schemas/AudiobookBase" - - properties: - chapters: - allOf: - - $ref: "#/components/schemas/PagingSimplifiedChapterObject" - description: | - The chapters of the audiobook. - type: object - required: - - chapters - type: object - x-spotify-docs-type: AudiobookObject - AuthorObject: - properties: - name: - description: | - The name of the author. - type: string - type: object - x-spotify-docs-type: AuthorObject - CategoryObject: - properties: - href: - description: | - A link to the Web API endpoint returning full details of the category. - type: string - icons: - description: | - The category icon, in various sizes. - items: - $ref: "#/components/schemas/ImageObject" - type: array - id: - description: | - The [Spotify category ID](/documentation/web-api/concepts/spotify-uris-ids) of the category. - example: equal - type: string - name: - description: | - The name of the category. - example: EQUAL - type: string - required: - - href - - icons - - id - - name - type: object - x-spotify-docs-type: CategoryObject - ChapterBase: - properties: - audio_preview_url: - description: | - A URL to a 30 second preview (MP3 format) of the episode. `null` if not available. - example: https://p.scdn.co/mp3-preview/2f37da1d4221f40b9d1a98cd191f4d6f1646ad17 - type: string - x-spotify-policy-list: - - $ref: "#/components/x-spotify-policy/StandalonePreview" - available_markets: - description: | - A list of the countries in which the chapter can be played, identified by their [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code. - items: - type: string - type: array - chapter_number: - description: | - The number of the chapter - example: 1 - type: integer - description: - description: | - A description of the episode. HTML tags are stripped away from this field, use `html_description` field in case HTML tags are needed. - example: | - A Spotify podcast sharing fresh insights on important topics of the moment—in a way only Spotify can. You’ll hear from experts in the music, podcast and tech industries as we discover and uncover stories about our work and the world around us. - type: string - duration_ms: - description: | - The episode length in milliseconds. - example: 1686230 - type: integer - explicit: - description: | - Whether or not the episode has explicit content (true = yes it does; false = no it does not OR unknown). - type: boolean - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - External URLs for this episode. - href: - description: | - A link to the Web API endpoint providing full details of the episode. - example: https://api.spotify.com/v1/episodes/5Xt5DXGzch68nYYamXrNxZ - type: string - html_description: - description: | - A description of the episode. This field may contain HTML tags. - example: | -

A Spotify podcast sharing fresh insights on important topics of the moment—in a way only Spotify can. You’ll hear from experts in the music, podcast and tech industries as we discover and uncover stories about our work and the world around us.

- type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the episode. - example: 5Xt5DXGzch68nYYamXrNxZ - type: string - images: - description: | - The cover art for the episode in various sizes, widest first. - items: - $ref: "#/components/schemas/ImageObject" - type: array - is_playable: - description: | - True if the episode is playable in the given market. Otherwise false. - type: boolean - languages: - description: | - A list of the languages used in the episode, identified by their [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639) code. - example: - - fr - - en - items: - type: string - type: array - name: - description: | - The name of the episode. - example: | - Starting Your Own Podcast: Tips, Tricks, and Advice From Anchor Creators - type: string - release_date: - description: | - The date the episode was first released, for example `"1981-12-15"`. Depending on the precision, it might be shown as `"1981"` or `"1981-12"`. - example: 1981-12-15 - type: string - release_date_precision: - description: | - The precision with which `release_date` value is known. - enum: - - year - - month - - day - example: day - type: string - restrictions: - allOf: - - $ref: "#/components/schemas/ChapterRestrictionObject" - description: | - Included in the response when a content restriction is applied. - resume_point: - allOf: - - $ref: "#/components/schemas/ResumePointObject" - description: | - The user's most recent position in the episode. Set if the supplied access token is a user token and has the scope 'user-read-playback-position'. - type: - description: | - The object type. - enum: - - episode - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the episode. - example: spotify:episode:0zLhl3WsOCQHbe1BPTiHgr - type: string - required: - - audio_preview_url - - chapter_number - - description - - html_description - - duration_ms - - explicit - - external_urls - - href - - id - - images - - is_playable - - languages - - name - - release_date - - release_date_precision - - resume_point - - type - - uri - type: object - ChapterObject: - allOf: - - $ref: "#/components/schemas/ChapterBase" - - properties: - audiobook: - $ref: "#/components/schemas/SimplifiedAudiobookObject" - description: | - The audiobook for which the chapter belongs. - required: - - audiobook - type: object - type: object - x-spotify-docs-type: ChapterObject - ChapterRestrictionObject: - properties: - reason: - description: | - The reason for the restriction. Supported values: - - `market` - The content item is not available in the given market. - - `product` - The content item is not available for the user's subscription type. - - `explicit` - The content item is explicit and the user's account is set to not play explicit content. - - `payment_required` - Payment is required to play the content item. - - Additional reasons may be added in the future. - **Note**: If you use this field, make sure that your application safely handles unknown values. - type: string - type: object - x-spotify-docs-type: ChapterRestrictionObject - ContextObject: - properties: - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: External URLs for this context. - href: - description: A link to the Web API endpoint providing full details of the track. - type: string - type: - description: | - The object type, e.g. "artist", "playlist", "album", "show". - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the context. - type: string - type: object - x-spotify-docs-type: ContextObject - CopyrightObject: - properties: - text: - description: | - The copyright text for this content. - type: string - type: - description: | - The type of copyright: `C` = the copyright, `P` = the sound recording (performance) copyright. - type: string - type: object - x-spotify-docs-type: CopyrightObject - CurrentlyPlayingContextObject: - properties: - actions: - allOf: - - $ref: "#/components/schemas/DisallowsObject" - description: | - Allows to update the user interface based on which playback actions are available within the current context. - context: - allOf: - - $ref: "#/components/schemas/ContextObject" - description: A Context Object. Can be `null`. - currently_playing_type: - description: | - The object type of the currently playing item. Can be one of `track`, `episode`, `ad` or `unknown`. - type: string - device: - allOf: - - $ref: "#/components/schemas/DeviceObject" - description: | - The device that is currently active. - is_playing: - description: If something is currently playing, return `true`. - type: boolean - item: - description: The currently playing track or episode. Can be `null`. - discriminator: - propertyName: type - oneOf: - - $ref: "#/components/schemas/TrackObject" - - $ref: "#/components/schemas/EpisodeObject" - x-spotify-docs-type: TrackObject | EpisodeObject - progress_ms: - description: Progress into the currently playing track or episode. Can be `null`. - type: integer - repeat_state: - description: off, track, context - type: string - shuffle_state: - description: If shuffle is on or off. - type: boolean - timestamp: - description: Unix Millisecond Timestamp when data was fetched. - type: integer - type: object - x-spotify-docs-type: CurrentlyPlayingContextObject - CurrentlyPlayingObject: - properties: - context: - allOf: - - $ref: "#/components/schemas/ContextObject" - description: A Context Object. Can be `null`. - currently_playing_type: - description: | - The object type of the currently playing item. Can be one of `track`, `episode`, `ad` or `unknown`. - type: string - is_playing: - description: If something is currently playing, return `true`. - type: boolean - item: - description: The currently playing track or episode. Can be `null`. - discriminator: - propertyName: type - oneOf: - - $ref: "#/components/schemas/TrackObject" - - $ref: "#/components/schemas/EpisodeObject" - x-spotify-docs-type: TrackObject | EpisodeObject - progress_ms: - description: Progress into the currently playing track or episode. Can be `null`. - type: integer - timestamp: - description: Unix Millisecond Timestamp when data was fetched - type: integer - type: object - x-spotify-docs-type: CurrentlyPlayingObject - CursorObject: - properties: - after: - description: The cursor to use as key to find the next page of items. - type: string - before: - description: The cursor to use as key to find the previous page of items. - type: string - type: object - x-spotify-docs-type: CursorObject - CursorPagingObject: - properties: - cursors: - allOf: - - $ref: "#/components/schemas/CursorObject" - description: The cursors used to find the next set of items. - href: - description: A link to the Web API endpoint returning the full result of the request. - type: string - limit: - description: The maximum number of items in the response (as set in the query or by default). - type: integer - next: - description: URL to the next page of items. ( `null` if none) - type: string - total: - description: The total number of items available to return. - type: integer - type: object - x-spotify-docs-type: CursorPagingObject - CursorPagingPlayHistoryObject: - allOf: - - $ref: "#/components/schemas/CursorPagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/PlayHistoryObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingTrackObject - CursorPagingSimplifiedArtistObject: - allOf: - - $ref: "#/components/schemas/CursorPagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/ArtistObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingArtistObject - DeviceObject: - properties: - id: - description: The device ID. - nullable: true - type: string - is_active: - description: If this device is the currently active device. - type: boolean - is_private_session: - description: If this device is currently in a private session. - type: boolean - is_restricted: - description: Whether controlling this device is restricted. At present if this is "true" then no Web API commands will be accepted by this device. - type: boolean - name: - description: A human-readable name for the device. Some devices have a name that the user can configure (e.g. \"Loudest speaker\") and some devices have a generic name associated with the manufacturer or device model. - example: Kitchen speaker - type: string - type: - description: Device type, such as "computer", "smartphone" or "speaker". - example: computer - type: string - volume_percent: - description: The current volume in percent. - example: 59 - maximum: 100 - minimum: 0 - nullable: true - type: integer - type: object - x-spotify-docs-type: DeviceObject - DevicesObject: - properties: - devices: - description: A list of 0..n Device objects - items: - $ref: "#/components/schemas/DeviceObject" - type: array - type: object - x-spotify-docs-type: DevicesObject - DisallowsObject: - properties: - interrupting_playback: - description: Interrupting playback. Optional field. - type: boolean - pausing: - description: Pausing. Optional field. - type: boolean - resuming: - description: Resuming. Optional field. - type: boolean - seeking: - description: Seeking playback location. Optional field. - type: boolean - skipping_next: - description: Skipping to the next context. Optional field. - type: boolean - skipping_prev: - description: Skipping to the previous context. Optional field. - type: boolean - toggling_repeat_context: - description: Toggling repeat context flag. Optional field. - type: boolean - toggling_repeat_track: - description: Toggling repeat track flag. Optional field. - type: boolean - toggling_shuffle: - description: Toggling shuffle flag. Optional field. - type: boolean - transferring_playback: - description: Transfering playback between devices. Optional field. - type: boolean - type: object - x-spotify-docs-type: DisallowsObject - EpisodeBase: - properties: - audio_preview_url: - description: | - A URL to a 30 second preview (MP3 format) of the episode. `null` if not available. - example: https://p.scdn.co/mp3-preview/2f37da1d4221f40b9d1a98cd191f4d6f1646ad17 - type: string - x-spotify-policy-list: - - $ref: "#/components/x-spotify-policy/StandalonePreview" - description: - description: | - A description of the episode. HTML tags are stripped away from this field, use `html_description` field in case HTML tags are needed. - example: | - A Spotify podcast sharing fresh insights on important topics of the moment—in a way only Spotify can. You’ll hear from experts in the music, podcast and tech industries as we discover and uncover stories about our work and the world around us. - type: string - duration_ms: - description: | - The episode length in milliseconds. - example: 1686230 - type: integer - explicit: - description: | - Whether or not the episode has explicit content (true = yes it does; false = no it does not OR unknown). - type: boolean - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - External URLs for this episode. - href: - description: | - A link to the Web API endpoint providing full details of the episode. - example: https://api.spotify.com/v1/episodes/5Xt5DXGzch68nYYamXrNxZ - type: string - html_description: - description: | - A description of the episode. This field may contain HTML tags. - example: | -

A Spotify podcast sharing fresh insights on important topics of the moment—in a way only Spotify can. You’ll hear from experts in the music, podcast and tech industries as we discover and uncover stories about our work and the world around us.

- type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the episode. - example: 5Xt5DXGzch68nYYamXrNxZ - type: string - images: - description: | - The cover art for the episode in various sizes, widest first. - items: - $ref: "#/components/schemas/ImageObject" - type: array - is_externally_hosted: - description: | - True if the episode is hosted outside of Spotify's CDN. - type: boolean - is_playable: - description: | - True if the episode is playable in the given market. Otherwise false. - type: boolean - language: - deprecated: true - description: | - The language used in the episode, identified by a [ISO 639](https://en.wikipedia.org/wiki/ISO_639) code. This field is deprecated and might be removed in the future. Please use the `languages` field instead. - example: en - type: string - languages: - description: | - A list of the languages used in the episode, identified by their [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639) code. - example: - - fr - - en - items: - type: string - type: array - name: - description: | - The name of the episode. - example: | - Starting Your Own Podcast: Tips, Tricks, and Advice From Anchor Creators - type: string - release_date: - description: | - The date the episode was first released, for example `"1981-12-15"`. Depending on the precision, it might be shown as `"1981"` or `"1981-12"`. - example: 1981-12-15 - type: string - release_date_precision: - description: | - The precision with which `release_date` value is known. - enum: - - year - - month - - day - example: day - type: string - restrictions: - allOf: - - $ref: "#/components/schemas/EpisodeRestrictionObject" - description: | - Included in the response when a content restriction is applied. - resume_point: - allOf: - - $ref: "#/components/schemas/ResumePointObject" - description: | - The user's most recent position in the episode. Set if the supplied access token is a user token and has the scope 'user-read-playback-position'. - type: - description: | - The object type. - enum: - - episode - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the episode. - example: spotify:episode:0zLhl3WsOCQHbe1BPTiHgr - type: string - required: - - audio_preview_url - - description - - html_description - - duration_ms - - explicit - - external_urls - - href - - id - - images - - is_externally_hosted - - is_playable - - languages - - name - - release_date - - release_date_precision - - resume_point - - type - - uri - type: object - EpisodeObject: - allOf: - - $ref: "#/components/schemas/EpisodeBase" - - properties: - show: - $ref: "#/components/schemas/SimplifiedShowObject" - description: | - The show on which the episode belongs. - required: - - show - type: object - type: object - x-spotify-docs-type: EpisodeObject - EpisodeRestrictionObject: - properties: - reason: - description: | - The reason for the restriction. Supported values: - - `market` - The content item is not available in the given market. - - `product` - The content item is not available for the user's subscription type. - - `explicit` - The content item is explicit and the user's account is set to not play explicit content. - - Additional reasons may be added in the future. - **Note**: If you use this field, make sure that your application safely handles unknown values. - type: string - type: object - x-spotify-docs-type: EpisodeRestrictionObject - ErrorObject: - properties: - message: - description: | - A short description of the cause of the error. - type: string - status: - description: | - The HTTP status code (also returned in the response header; see [Response Status Codes](/documentation/web-api/concepts/api-calls#response-status-codes) for more information). - maximum: 599 - minimum: 400 - type: integer - required: - - status - - message - type: object - x-spotify-docs-type: ErrorObject - ExplicitContentSettingsObject: - properties: - filter_enabled: - description: | - When `true`, indicates that explicit content should not be played. - type: boolean - filter_locked: - description: | - When `true`, indicates that the explicit content setting is locked and can't be changed by the user. - type: boolean - type: object - x-spotify-docs-type: ExplicitContentSettingsObject - ExternalIdObject: - properties: - ean: - description: | - [International Article Number](http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29) - type: string - isrc: - description: | - [International Standard Recording Code](http://en.wikipedia.org/wiki/International_Standard_Recording_Code) - type: string - upc: - description: | - [Universal Product Code](http://en.wikipedia.org/wiki/Universal_Product_Code) - type: string - type: object - x-spotify-docs-type: ExternalIdObject - ExternalUrlObject: - properties: - spotify: - description: | - The [Spotify URL](/documentation/web-api/concepts/spotify-uris-ids) for the object. - type: string - type: object - x-spotify-docs-type: ExternalUrlObject - FollowersObject: - properties: - href: - description: | - This will always be set to null, as the Web API does not support it at the moment. - nullable: true - type: string - total: - description: | - The total number of followers. - type: integer - type: object - x-spotify-docs-type: FollowersObject - ImageObject: - properties: - height: - description: | - The image height in pixels. - example: 300 - nullable: true - type: integer - url: - description: | - The source URL of the image. - example: | - https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228 - type: string - width: - description: | - The image width in pixels. - example: 300 - nullable: true - type: integer - required: - - url - - height - - width - type: object - x-spotify-docs-type: ImageObject - Key: - description: | - The key the track is in. Integers map to pitches using standard [Pitch Class notation](https://en.wikipedia.org/wiki/Pitch_class). E.g. 0 = C, 1 = C♯/D♭, 2 = D, and so on. If no key was detected, the value is -1. - example: 9 - maximum: 11 - minimum: -1 - type: integer - LinkedTrackObject: - properties: - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known external URLs for this track. - href: - description: | - A link to the Web API endpoint providing full details of the track. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the track. - type: string - type: - description: | - The object type: "track". - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the track. - type: string - type: object - x-spotify-docs-type: LinkedTrackObject - Loudness: - description: | - The overall loudness of a track in decibels (dB). Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Loudness is the quality of a sound that is the primary psychological correlate of physical strength (amplitude). Values typically range between -60 and 0 db. - example: -5.883 - format: float - type: number - x-spotify-docs-type: Float - Mode: - description: | - Mode indicates the modality (major or minor) of a track, the type of scale from which its melodic content is derived. Major is represented by 1 and minor is 0. - example: 0 - type: integer - NarratorObject: - properties: - name: - description: | - The name of the Narrator. - type: string - type: object - x-spotify-docs-type: NarratorObject - PagingArtistObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/ArtistObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingArtistObject - PagingFeaturedPlaylistObject: - properties: - message: - type: string - playlists: - $ref: "#/components/schemas/PagingPlaylistObject" - type: object - x-spotify-docs-type: PagingFeaturedPlaylistObject - PagingObject: - properties: - href: - description: | - A link to the Web API endpoint returning the full result of the request - example: | - https://api.spotify.com/v1/me/shows?offset=0&limit=20 - type: string - limit: - description: | - The maximum number of items in the response (as set in the query or by default). - example: 20 - type: integer - next: - description: | - URL to the next page of items. ( `null` if none) - example: https://api.spotify.com/v1/me/shows?offset=1&limit=1 - nullable: true - type: string - offset: - description: | - The offset of the items returned (as set in the query or by default) - example: 0 - type: integer - previous: - description: | - URL to the previous page of items. ( `null` if none) - example: https://api.spotify.com/v1/me/shows?offset=1&limit=1 - nullable: true - type: string - total: - description: | - The total number of items available to return. - example: 4 - type: integer - required: - - href - - items - - limit - - next - - offset - - previous - - total - type: object - x-spotify-docs-type: PagingObject - PagingPlaylistObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedPlaylistObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingPlaylistObject - PagingPlaylistTrackObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/PlaylistTrackObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingPlaylistTrackObject - PagingSavedAlbumObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SavedAlbumObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingSavedAlbumObject - PagingSavedEpisodeObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SavedEpisodeObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingEpisodeObject - PagingSavedShowObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SavedShowObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingShowObject - PagingSavedTrackObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SavedTrackObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingTrackObject - PagingSimplifiedAlbumObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedAlbumObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingAlbumObject - PagingSimplifiedArtistObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedArtistObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingArtistObject - PagingSimplifiedAudiobookObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedAudiobookObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingAudiobookObject - PagingSimplifiedChapterObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedChapterObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingSimplifiedChapterObject - PagingSimplifiedEpisodeObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedEpisodeObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingEpisodeObject - PagingSimplifiedShowObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedShowObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingShowObject - PagingSimplifiedTrackObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/SimplifiedTrackObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingTrackObject - PagingTrackObject: - allOf: - - $ref: "#/components/schemas/PagingObject" - - properties: - items: - items: - $ref: "#/components/schemas/TrackObject" - type: array - type: object - type: object - x-spotify-docs-type: PagingTrackObject - PlayHistoryObject: - properties: - context: - allOf: - - $ref: "#/components/schemas/ContextObject" - description: The context the track was played from. - played_at: - description: The date and time the track was played. - format: date-time - type: string - x-spotify-docs-type: Timestamp - track: - allOf: - - $ref: "#/components/schemas/TrackObject" - description: The track the user listened to. - type: object - x-spotify-docs-type: PlayHistoryObject - PlayerErrorObject: - properties: - message: - description: | - A short description of the cause of the error. - type: string - reason: - allOf: - - $ref: "#/components/schemas/PlayerErrorReasons" - status: - description: | - The HTTP status code. Either `404 NOT FOUND` or `403 FORBIDDEN`. Also returned in the response header. - type: integer - type: object - x-spotify-docs-type: PlayerErrorObject - PlayerErrorReasons: - description: | - * `NO_PREV_TRACK` - The command requires a previous track, but there is none in the context. - * `NO_NEXT_TRACK` - The command requires a next track, but there is none in the context. - * `NO_SPECIFIC_TRACK` - The requested track does not exist. - * `ALREADY_PAUSED` - The command requires playback to not be paused. - * `NOT_PAUSED` - The command requires playback to be paused. - * `NOT_PLAYING_LOCALLY` - The command requires playback on the local device. - * `NOT_PLAYING_TRACK` - The command requires that a track is currently playing. - * `NOT_PLAYING_CONTEXT` - The command requires that a context is currently playing. - * `ENDLESS_CONTEXT` - The shuffle command cannot be applied on an endless context. - * `CONTEXT_DISALLOW` - The command could not be performed on the context. - * `ALREADY_PLAYING` - The track should not be restarted if the same track and context is already playing, and there is a resume point. - * `RATE_LIMITED` - The user is rate limited due to too frequent track play, also known as cat-on-the-keyboard spamming. - * `REMOTE_CONTROL_DISALLOW` - The context cannot be remote-controlled. - * `DEVICE_NOT_CONTROLLABLE` - Not possible to remote control the device. - * `VOLUME_CONTROL_DISALLOW` - Not possible to remote control the device's volume. - * `NO_ACTIVE_DEVICE` - Requires an active device and the user has none. - * `PREMIUM_REQUIRED` - The request is prohibited for non-premium users. - * `UNKNOWN` - Certain actions are restricted because of unknown reasons. - enum: - - NO_PREV_TRACK - - NO_NEXT_TRACK - - NO_SPECIFIC_TRACK - - ALREADY_PAUSED - - NOT_PAUSED - - NOT_PLAYING_LOCALLY - - NOT_PLAYING_TRACK - - NOT_PLAYING_CONTEXT - - ENDLESS_CONTEXT - - CONTEXT_DISALLOW - - ALREADY_PLAYING - - RATE_LIMITED - - REMOTE_CONTROL_DISALLOW - - DEVICE_NOT_CONTROLLABLE - - VOLUME_CONTROL_DISALLOW - - NO_ACTIVE_DEVICE - - PREMIUM_REQUIRED - - UNKNOWN - type: string - PlaylistObject: - properties: - collaborative: - description: | - `true` if the owner allows other users to modify the playlist. - type: boolean - description: - description: | - The playlist description. _Only returned for modified, verified playlists, otherwise_ `null`. - nullable: true - type: string - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known external URLs for this playlist. - followers: - allOf: - - $ref: "#/components/schemas/FollowersObject" - description: Information about the followers of the playlist. - href: - description: | - A link to the Web API endpoint providing full details of the playlist. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the playlist. - type: string - images: - description: | - Images for the playlist. The array may be empty or contain up to three images. The images are returned by size in descending order. See [Working with Playlists](/documentation/web-api/concepts/playlists). _**Note**: If returned, the source URL for the image (`url`) is temporary and will expire in less than a day._ - items: - $ref: "#/components/schemas/ImageObject" - type: array - name: - description: | - The name of the playlist. - type: string - owner: - allOf: - - $ref: "#/components/schemas/PlaylistOwnerObject" - description: | - The user who owns the playlist - public: - description: | - The playlist's public/private status: `true` the playlist is public, `false` the playlist is private, `null` the playlist status is not relevant. For more about public/private status, see [Working with Playlists](/documentation/web-api/concepts/playlists) - type: boolean - snapshot_id: - description: | - The version identifier for the current playlist. Can be supplied in other requests to target a specific playlist version - type: string - tracks: - allOf: - - $ref: "#/components/schemas/PagingPlaylistTrackObject" - description: | - The tracks of the playlist. - type: object - type: - description: | - The object type: "playlist" - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the playlist. - type: string - type: object - x-spotify-docs-type: PlaylistObject - PlaylistOwnerObject: - allOf: - - $ref: "#/components/schemas/PlaylistUserObject" - - properties: - display_name: - description: | - The name displayed on the user's profile. `null` if not available. - nullable: true - type: string - type: object - PlaylistTrackObject: - properties: - added_at: - description: | - The date and time the track or episode was added. _**Note**: some very old playlists may return `null` in this field._ - format: date-time - type: string - x-spotify-docs-type: Timestamp - added_by: - allOf: - - $ref: "#/components/schemas/PlaylistUserObject" - description: | - The Spotify user who added the track or episode. _**Note**: some very old playlists may return `null` in this field._ - is_local: - description: | - Whether this track or episode is a [local file](/documentation/web-api/concepts/playlists/#local-files) or not. - type: boolean - track: - description: Information about the track or episode. - discriminator: - propertyName: type - oneOf: - - $ref: "#/components/schemas/TrackObject" - - $ref: "#/components/schemas/EpisodeObject" - x-spotify-docs-type: TrackObject | EpisodeObject - type: object - x-spotify-docs-type: PlaylistTrackObject - PlaylistTracksRefObject: - properties: - href: - description: | - A link to the Web API endpoint where full details of the playlist's tracks can be retrieved. - type: string - total: - description: | - Number of tracks in the playlist. - type: integer - type: object - x-spotify-docs-type: PlaylistTracksRefObject - PlaylistUserObject: - properties: - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known public external URLs for this user. - followers: - allOf: - - $ref: "#/components/schemas/FollowersObject" - description: | - Information about the followers of this user. - href: - description: | - A link to the Web API endpoint for this user. - type: string - id: - description: | - The [Spotify user ID](/documentation/web-api/concepts/spotify-uris-ids) for this user. - type: string - type: - description: | - The object type. - enum: - - user - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for this user. - type: string - type: object - x-spotify-docs-type: PlaylistUserObject - PrivateUserObject: - properties: - country: - description: | - The country of the user, as set in the user's account profile. An [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). _This field is only available when the current user has granted access to the [user-read-private](/documentation/web-api/concepts/scopes/#list-of-scopes) scope._ - type: string - display_name: - description: | - The name displayed on the user's profile. `null` if not available. - type: string - email: - description: | - The user's email address, as entered by the user when creating their account. _**Important!** This email address is unverified; there is no proof that it actually belongs to the user._ _This field is only available when the current user has granted access to the [user-read-email](/documentation/web-api/concepts/scopes/#list-of-scopes) scope._ - type: string - explicit_content: - allOf: - - $ref: "#/components/schemas/ExplicitContentSettingsObject" - description: | - The user's explicit content settings. _This field is only available when the current user has granted access to the [user-read-private](/documentation/web-api/concepts/scopes/#list-of-scopes) scope._ - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: Known external URLs for this user. - followers: - allOf: - - $ref: "#/components/schemas/FollowersObject" - description: Information about the followers of the user. - href: - description: | - A link to the Web API endpoint for this user. - type: string - id: - description: | - The [Spotify user ID](/documentation/web-api/concepts/spotify-uris-ids) for the user. - type: string - images: - description: The user's profile image. - items: - $ref: "#/components/schemas/ImageObject" - type: array - product: - description: | - The user's Spotify subscription level: "premium", "free", etc. (The subscription level "open" can be considered the same as "free".) _This field is only available when the current user has granted access to the [user-read-private](/documentation/web-api/concepts/scopes/#list-of-scopes) scope._ - type: string - type: - description: | - The object type: "user" - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the user. - type: string - type: object - x-spotify-docs-type: PrivateUserObject - PublicUserObject: - properties: - display_name: - description: | - The name displayed on the user's profile. `null` if not available. - nullable: true - type: string - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known public external URLs for this user. - followers: - allOf: - - $ref: "#/components/schemas/FollowersObject" - description: | - Information about the followers of this user. - href: - description: | - A link to the Web API endpoint for this user. - type: string - id: - description: | - The [Spotify user ID](/documentation/web-api/concepts/spotify-uris-ids) for this user. - type: string - images: - description: | - The user's profile image. - items: - $ref: "#/components/schemas/ImageObject" - type: array - type: - description: | - The object type. - enum: - - user - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for this user. - type: string - type: object - x-spotify-docs-type: PublicUserObject - QueueObject: - properties: - currently_playing: - description: The currently playing track or episode. Can be `null`. - discriminator: - propertyName: type - oneOf: - - $ref: "#/components/schemas/TrackObject" - - $ref: "#/components/schemas/EpisodeObject" - x-spotify-docs-type: TrackObject | EpisodeObject - queue: - description: The tracks or episodes in the queue. Can be empty. - items: - discriminator: - propertyName: type - oneOf: - - $ref: "#/components/schemas/TrackObject" - - $ref: "#/components/schemas/EpisodeObject" - x-spotify-docs-type: TrackObject | EpisodeObject - type: array - type: object - x-spotify-docs-type: QueueObject - RecommendationSeedObject: - properties: - afterFilteringSize: - description: | - The number of tracks available after min\_\* and max\_\* filters have been applied. - type: integer - afterRelinkingSize: - description: | - The number of tracks available after relinking for regional availability. - type: integer - href: - description: | - A link to the full track or artist data for this seed. For tracks this will be a link to a Track Object. For artists a link to an Artist Object. For genre seeds, this value will be `null`. - type: string - id: - description: | - The id used to select this seed. This will be the same as the string used in the `seed_artists`, `seed_tracks` or `seed_genres` parameter. - type: string - initialPoolSize: - description: | - The number of recommended tracks available for this seed. - type: integer - type: - description: | - The entity type of this seed. One of `artist`, `track` or `genre`. - type: string - type: object - x-spotify-docs-type: RecommendationSeedObject - RecommendationsObject: - properties: - seeds: - description: | - An array of recommendation seed objects. - items: - $ref: "#/components/schemas/RecommendationSeedObject" - type: array - tracks: - description: | - An array of track object (simplified) ordered according to the parameters supplied. - items: - $ref: "#/components/schemas/TrackObject" - type: array - required: - - seeds - - tracks - type: object - x-spotify-docs-type: RecommendationsObject - ResumePointObject: - properties: - fully_played: - description: | - Whether or not the episode has been fully played by the user. - type: boolean - resume_position_ms: - description: | - The user's most recent position in the episode in milliseconds. - type: integer - type: object - x-spotify-docs-type: ResumePointObject - SavedAlbumObject: - properties: - added_at: - description: | - The date and time the album was saved - Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. - If the time is imprecise (for example, the date/time of an album release), an additional field indicates the precision; see for example, release_date in an album object. - format: date-time - type: string - x-spotify-docs-type: Timestamp - album: - allOf: - - $ref: "#/components/schemas/AlbumObject" - description: Information about the album. - type: object - x-spotify-docs-type: SavedAlbumObject - SavedEpisodeObject: - properties: - added_at: - description: | - The date and time the episode was saved. - Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. - format: date-time - type: string - x-spotify-docs-type: Timestamp - episode: - allOf: - - $ref: "#/components/schemas/EpisodeObject" - description: Information about the episode. - type: object - x-spotify-docs-type: SavedEpisodeObject - SavedShowObject: - properties: - added_at: - description: | - The date and time the show was saved. - Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. - If the time is imprecise (for example, the date/time of an album release), an additional field indicates the precision; see for example, release_date in an album object. - format: date-time - type: string - x-spotify-docs-type: Timestamp - show: - allOf: - - $ref: "#/components/schemas/SimplifiedShowObject" - description: Information about the show. - type: object - x-spotify-docs-type: SavedShowObject - SavedTrackObject: - properties: - added_at: - description: | - The date and time the track was saved. - Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ. - If the time is imprecise (for example, the date/time of an album release), an additional field indicates the precision; see for example, release_date in an album object. - format: date-time - type: string - x-spotify-docs-type: Timestamp - track: - allOf: - - $ref: "#/components/schemas/TrackObject" - description: Information about the track. - type: object - x-spotify-docs-type: SavedTrackObject - SectionObject: - properties: - confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the section's "designation". - example: 1 - maximum: 1 - minimum: 0 - type: number - duration: - description: The duration (in seconds) of the section. - example: 6.97092 - type: number - key: - description: The estimated overall key of the section. The values in this field ranging from 0 to 11 mapping to pitches using standard Pitch Class notation (E.g. 0 = C, 1 = C♯/D♭, 2 = D, and so on). If no key was detected, the value is -1. - example: 9 - type: integer - key_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the key. Songs with many key changes may correspond to low values in this field. - example: 0.297 - maximum: 1 - minimum: 0 - type: number - loudness: - description: The overall loudness of the section in decibels (dB). Loudness values are useful for comparing relative loudness of sections within tracks. - example: -14.938 - type: number - mode: - description: Indicates the modality (major or minor) of a section, the type of scale from which its melodic content is derived. This field will contain a 0 for "minor", a 1 for "major", or a -1 for no result. Note that the major key (e.g. C major) could more likely be confused with the minor key at 3 semitones lower (e.g. A minor) as both keys carry the same pitches. - enum: - - -1 - - 0 - - 1 - type: number - mode_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the `mode`. - example: 0.471 - maximum: 1 - minimum: 0 - type: number - start: - description: The starting point (in seconds) of the section. - example: 0 - type: number - tempo: - description: The overall estimated tempo of the section in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration. - example: 113.178 - type: number - tempo_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the tempo. Some tracks contain tempo changes or sounds which don't contain tempo (like pure speech) which would correspond to a low value in this field. - example: 0.647 - maximum: 1 - minimum: 0 - type: number - time_signature: - $ref: "#/components/schemas/TimeSignature" - time_signature_confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the `time_signature`. Sections with time signature changes may correspond to low values in this field. - example: 1 - maximum: 1 - minimum: 0 - type: number - type: object - SegmentObject: - properties: - confidence: - description: | - The confidence, from 0.0 to 1.0, of the reliability of the segmentation. Segments of the song which are difficult to logically segment (e.g: noise) may correspond to low values in this field. - example: 0.435 - maximum: 1 - minimum: 0 - type: number - duration: - description: The duration (in seconds) of the segment. - example: 0.19891 - type: number - loudness_end: - description: The offset loudness of the segment in decibels (dB). This value should be equivalent to the loudness_start of the following segment. - example: 0 - type: number - loudness_max: - description: The peak loudness of the segment in decibels (dB). Combined with `loudness_start` and `loudness_max_time`, these components can be used to describe the "attack" of the segment. - example: -14.25 - type: number - loudness_max_time: - description: The segment-relative offset of the segment peak loudness in seconds. Combined with `loudness_start` and `loudness_max`, these components can be used to desctibe the "attack" of the segment. - example: 0.07305 - type: number - loudness_start: - description: The onset loudness of the segment in decibels (dB). Combined with `loudness_max` and `loudness_max_time`, these components can be used to describe the "attack" of the segment. - example: -23.053 - type: number - pitches: - description: | - Pitch content is given by a “chroma” vector, corresponding to the 12 pitch classes C, C#, D to B, with values ranging from 0 to 1 that describe the relative dominance of every pitch in the chromatic scale. For example a C Major chord would likely be represented by large values of C, E and G (i.e. classes 0, 4, and 7). - - Vectors are normalized to 1 by their strongest dimension, therefore noisy sounds are likely represented by values that are all close to 1, while pure tones are described by one value at 1 (the pitch) and others near 0. - As can be seen below, the 12 vector indices are a combination of low-power spectrum values at their respective pitch frequencies. - ![pitch vector](https://developer.spotify.com/assets/audio/Pitch_vector.png) - example: - - 0.212 - - 0.141 - - 0.294 - items: - maximum: 1 - minimum: 0 - type: number - type: array - start: - description: The starting point (in seconds) of the segment. - example: 0.70154 - type: number - timbre: - description: | - Timbre is the quality of a musical note or sound that distinguishes different types of musical instruments, or voices. It is a complex notion also referred to as sound color, texture, or tone quality, and is derived from the shape of a segment’s spectro-temporal surface, independently of pitch and loudness. The timbre feature is a vector that includes 12 unbounded values roughly centered around 0. Those values are high level abstractions of the spectral surface, ordered by degree of importance. - - For completeness however, the first dimension represents the average loudness of the segment; second emphasizes brightness; third is more closely correlated to the flatness of a sound; fourth to sounds with a stronger attack; etc. See an image below representing the 12 basis functions (i.e. template segments). - ![timbre basis functions](https://developer.spotify.com/assets/audio/Timbre_basis_functions.png) - - The actual timbre of the segment is best described as a linear combination of these 12 basis functions weighted by the coefficient values: timbre = c1 x b1 + c2 x b2 + ... + c12 x b12, where c1 to c12 represent the 12 coefficients and b1 to b12 the 12 basis functions as displayed below. Timbre vectors are best used in comparison with each other. - example: - - 42.115 - - 64.373 - - -0.233 - items: - type: number - type: array - type: object - ShowBase: - properties: - available_markets: - description: | - A list of the countries in which the show can be played, identified by their [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code. - items: - type: string - type: array - copyrights: - description: | - The copyright statements of the show. - items: - $ref: "#/components/schemas/CopyrightObject" - type: array - description: - description: | - A description of the show. HTML tags are stripped away from this field, use `html_description` field in case HTML tags are needed. - type: string - explicit: - description: | - Whether or not the show has explicit content (true = yes it does; false = no it does not OR unknown). - type: boolean - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - External URLs for this show. - href: - description: | - A link to the Web API endpoint providing full details of the show. - type: string - html_description: - description: | - A description of the show. This field may contain HTML tags. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the show. - type: string - images: - description: | - The cover art for the show in various sizes, widest first. - items: - $ref: "#/components/schemas/ImageObject" - type: array - is_externally_hosted: - description: | - True if all of the shows episodes are hosted outside of Spotify's CDN. This field might be `null` in some cases. - type: boolean - languages: - description: | - A list of the languages used in the show, identified by their [ISO 639](https://en.wikipedia.org/wiki/ISO_639) code. - items: - type: string - type: array - media_type: - description: | - The media type of the show. - type: string - name: - description: | - The name of the episode. - type: string - publisher: - description: | - The publisher of the show. - type: string - total_episodes: - description: | - The total number of episodes in the show. - type: integer - type: - description: | - The object type. - enum: - - show - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the show. - type: string - required: - - available_markets - - copyrights - - description - - explicit - - external_urls - - href - - html_description - - id - - images - - is_externally_hosted - - languages - - media_type - - name - - publisher - - total_episodes - - type - - uri - type: object - ShowObject: - allOf: - - $ref: "#/components/schemas/ShowBase" - - properties: - episodes: - allOf: - - $ref: "#/components/schemas/PagingSimplifiedEpisodeObject" - description: | - The episodes of the show. - type: object - required: - - episodes - type: object - x-spotify-docs-type: ShowObject - SimplifiedAlbumObject: - allOf: - - $ref: "#/components/schemas/AlbumBase" - - properties: - album_group: - description: | - The field is present when getting an artist's albums. Compare to album_type this field represents relationship between the artist and the album. - enum: - - album - - single - - compilation - - appears_on - example: compilation - type: string - artists: - description: | - The artists of the album. Each artist object includes a link in `href` to more detailed information about the artist. - items: - $ref: "#/components/schemas/SimplifiedArtistObject" - type: array - required: - - artists - type: object - x-spotify-docs-type: SimplifiedAlbumObject - SimplifiedArtistObject: - properties: - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known external URLs for this artist. - href: - description: | - A link to the Web API endpoint providing full details of the artist. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the artist. - type: string - name: - description: | - The name of the artist. - type: string - type: - description: | - The object type. - enum: - - artist - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the artist. - type: string - type: object - x-spotify-docs-type: SimplifiedArtistObject - SimplifiedAudiobookObject: - allOf: - - $ref: "#/components/schemas/AudiobookBase" - - type: object - x-spotify-docs-type: SimplifiedAudiobookObject - SimplifiedChapterObject: - allOf: - - $ref: "#/components/schemas/ChapterBase" - - type: object - type: object - x-spotify-docs-type: SimplifiedChapterObject - SimplifiedEpisodeObject: - allOf: - - $ref: "#/components/schemas/EpisodeBase" - - type: object - type: object - x-spotify-docs-type: SimplifiedEpisodeObject - SimplifiedPlaylistObject: - properties: - collaborative: - description: | - `true` if the owner allows other users to modify the playlist. - type: boolean - description: - description: | - The playlist description. _Only returned for modified, verified playlists, otherwise_ `null`. - type: string - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known external URLs for this playlist. - href: - description: | - A link to the Web API endpoint providing full details of the playlist. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the playlist. - type: string - images: - description: | - Images for the playlist. The array may be empty or contain up to three images. The images are returned by size in descending order. See [Working with Playlists](/documentation/web-api/concepts/playlists). _**Note**: If returned, the source URL for the image (`url`) is temporary and will expire in less than a day._ - items: - $ref: "#/components/schemas/ImageObject" - type: array - name: - description: | - The name of the playlist. - type: string - owner: - allOf: - - $ref: "#/components/schemas/PlaylistOwnerObject" - description: | - The user who owns the playlist - public: - description: | - The playlist's public/private status: `true` the playlist is public, `false` the playlist is private, `null` the playlist status is not relevant. For more about public/private status, see [Working with Playlists](/documentation/web-api/concepts/playlists) - type: boolean - snapshot_id: - description: | - The version identifier for the current playlist. Can be supplied in other requests to target a specific playlist version - type: string - tracks: - allOf: - - $ref: "#/components/schemas/PlaylistTracksRefObject" - description: | - A collection containing a link ( `href` ) to the Web API endpoint where full details of the playlist's tracks can be retrieved, along with the `total` number of tracks in the playlist. Note, a track object may be `null`. This can happen if a track is no longer available. - type: - description: | - The object type: "playlist" - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the playlist. - type: string - type: object - x-spotify-docs-type: SimplifiedPlaylistObject - SimplifiedShowObject: - allOf: - - $ref: "#/components/schemas/ShowBase" - - type: object - x-spotify-docs-type: SimplifiedShowObject - SimplifiedTrackObject: - properties: - artists: - description: The artists who performed the track. Each artist object includes a link in `href` to more detailed information about the artist. - items: - $ref: "#/components/schemas/SimplifiedArtistObject" - type: array - available_markets: - description: | - A list of the countries in which the track can be played, identified by their [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code. - items: - type: string - type: array - disc_number: - description: The disc number (usually `1` unless the album consists of more than one disc). - type: integer - duration_ms: - description: The track length in milliseconds. - type: integer - explicit: - description: Whether or not the track has explicit lyrics ( `true` = yes it does; `false` = no it does not OR unknown). - type: boolean - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - External URLs for this track. - href: - description: A link to the Web API endpoint providing full details of the track. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the track. - type: string - is_local: - description: | - Whether or not the track is from a local file. - type: boolean - is_playable: - description: | - Part of the response when [Track Relinking](/documentation/web-api/concepts/track-relinking/) is applied. If `true`, the track is playable in the given market. Otherwise `false`. - type: boolean - linked_from: - allOf: - - $ref: "#/components/schemas/LinkedTrackObject" - description: Part of the response when [Track Relinking](/documentation/web-api/concepts/track-relinking/) is applied and is only part of the response if the track linking, in fact, exists. The requested track has been replaced with a different track. The track in the `linked_from` object contains information about the originally requested track. - name: - description: The name of the track. - type: string - preview_url: - description: | - A URL to a 30 second preview (MP3 format) of the track. - type: string - x-spotify-policy-list: - - $ref: "#/components/x-spotify-policy/StandalonePreview" - restrictions: - allOf: - - $ref: "#/components/schemas/TrackRestrictionObject" - description: | - Included in the response when a content restriction is applied. - track_number: - description: | - The number of the track. If an album has several discs, the track number is the number on the specified disc. - type: integer - type: - description: | - The object type: "track". - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the track. - type: string - type: object - x-spotify-docs-type: SimplifiedTrackObject - Tempo: - description: | - The overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration. - example: 118.211 - format: float - type: number - x-spotify-docs-type: Float - TimeIntervalObject: - properties: - confidence: - description: The confidence, from 0.0 to 1.0, of the reliability of the interval. - example: 0.925 - maximum: 1 - minimum: 0 - type: number - duration: - description: The duration (in seconds) of the time interval. - example: 2.18749 - type: number - start: - description: The starting point (in seconds) of the time interval. - example: 0.49567 - type: number - type: object - TimeSignature: - description: An estimated time signature. The time signature (meter) is a notational convention to specify how many beats are in each bar (or measure). The time signature ranges from 3 to 7 indicating time signatures of "3/4", to "7/4". - example: 4 - maximum: 7 - minimum: 3 - type: integer - TrackObject: - properties: - album: - allOf: - - $ref: "#/components/schemas/SimplifiedAlbumObject" - description: | - The album on which the track appears. The album object includes a link in `href` to full information about the album. - artists: - description: | - The artists who performed the track. Each artist object includes a link in `href` to more detailed information about the artist. - items: - $ref: "#/components/schemas/ArtistObject" - type: array - available_markets: - description: | - A list of the countries in which the track can be played, identified by their [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code. - items: - type: string - type: array - disc_number: - description: | - The disc number (usually `1` unless the album consists of more than one disc). - type: integer - duration_ms: - description: | - The track length in milliseconds. - type: integer - explicit: - description: | - Whether or not the track has explicit lyrics ( `true` = yes it does; `false` = no it does not OR unknown). - type: boolean - external_ids: - allOf: - - $ref: "#/components/schemas/ExternalIdObject" - description: | - Known external IDs for the track. - external_urls: - allOf: - - $ref: "#/components/schemas/ExternalUrlObject" - description: | - Known external URLs for this track. - href: - description: | - A link to the Web API endpoint providing full details of the track. - type: string - id: - description: | - The [Spotify ID](/documentation/web-api/concepts/spotify-uris-ids) for the track. - type: string - is_local: - description: | - Whether or not the track is from a local file. - type: boolean - is_playable: - description: | - Part of the response when [Track Relinking](/documentation/web-api/concepts/track-relinking) is applied. If `true`, the track is playable in the given market. Otherwise `false`. - type: boolean - linked_from: - description: | - Part of the response when [Track Relinking](/documentation/web-api/concepts/track-relinking) is applied, and the requested track has been replaced with different track. The track in the `linked_from` object contains information about the originally requested track. - type: object - name: - description: | - The name of the track. - type: string - popularity: - description: | - The popularity of the track. The value will be between 0 and 100, with 100 being the most popular.
The popularity of a track is a value between 0 and 100, with 100 being the most popular. The popularity is calculated by algorithm and is based, in the most part, on the total number of plays the track has had and how recent those plays are.
Generally speaking, songs that are being played a lot now will have a higher popularity than songs that were played a lot in the past. Duplicate tracks (e.g. the same track from a single and an album) are rated independently. Artist and album popularity is derived mathematically from track popularity. _**Note**: the popularity value may lag actual popularity by a few days: the value is not updated in real time._ - type: integer - preview_url: - description: | - A link to a 30 second preview (MP3 format) of the track. Can be `null` - type: string - x-spotify-policy-list: - - $ref: "#/components/x-spotify-policy/StandalonePreview" - restrictions: - allOf: - - $ref: "#/components/schemas/TrackRestrictionObject" - description: | - Included in the response when a content restriction is applied. - track_number: - description: | - The number of the track. If an album has several discs, the track number is the number on the specified disc. - type: integer - type: - description: | - The object type: "track". - enum: - - track - type: string - uri: - description: | - The [Spotify URI](/documentation/web-api/concepts/spotify-uris-ids) for the track. - type: string - type: object - x-spotify-docs-type: TrackObject - TrackRestrictionObject: - properties: - reason: - description: | - The reason for the restriction. Supported values: - - `market` - The content item is not available in the given market. - - `product` - The content item is not available for the user's subscription type. - - `explicit` - The content item is explicit and the user's account is set to not play explicit content. - - Additional reasons may be added in the future. - **Note**: If you use this field, make sure that your application safely handles unknown values. - type: string - type: object - x-spotify-docs-type: TrackRestrictionObject - TuneableTrackObject: - properties: - acousticness: - description: | - A confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic. - format: float - type: number - x-spotify-docs-type: Float - danceability: - description: | - Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable. - format: float - type: number - x-spotify-docs-type: Float - duration_ms: - description: | - The duration of the track in milliseconds. - type: integer - energy: - description: | - Energy is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy. - format: float - type: number - x-spotify-docs-type: Float - instrumentalness: - description: | - Predicts whether a track contains no vocals. "Ooh" and "aah" sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly "vocal". The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0. - format: float - type: number - x-spotify-docs-type: Float - key: - $ref: "#/components/schemas/Key" - liveness: - description: | - Detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live. - format: float - type: number - x-spotify-docs-type: Float - loudness: - $ref: "#/components/schemas/Loudness" - mode: - $ref: "#/components/schemas/Mode" - popularity: - description: | - The popularity of the track. The value will be between 0 and 100, with 100 being the most popular. The popularity is calculated by algorithm and is based, in the most part, on the total number of plays the track has had and how recent those plays are. _**Note**: When applying track relinking via the `market` parameter, it is expected to find relinked tracks with popularities that do not match `min_*`, `max_*`and `target_*` popularities. These relinked tracks are accurate replacements for unplayable tracks with the expected popularity scores. Original, non-relinked tracks are available via the `linked_from` attribute of the [relinked track response](/documentation/web-api/concepts/track-relinking)._ - format: float - type: number - x-spotify-docs-type: Float - speechiness: - description: | - Speechiness detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks. - format: float - type: number - x-spotify-docs-type: Float - tempo: - $ref: "#/components/schemas/Tempo" - time_signature: - $ref: "#/components/schemas/TimeSignature" - valence: - description: | - A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry). - format: float - type: number - x-spotify-docs-type: Float - type: object - x-spotify-docs-type: TuneableTrackObject - securitySchemes: - oauth_2_0: - description: Spotify supports OAuth 2.0 for authenticating all API requests. - flows: - authorizationCode: - authorizationUrl: https://accounts.spotify.com/authorize - scopes: - app-remote-control: | - Communicate with the Spotify app on your device. - playlist-modify-private: | - Manage your private playlists. - playlist-modify-public: | - Manage your public playlists. - playlist-read-collaborative: | - Access your collaborative playlists. - playlist-read-private: | - Access your private playlists. - streaming: | - Play content and control playback on your other devices. - ugc-image-upload: | - Upload images to Spotify on your behalf. - user-follow-modify: | - Manage your saved content. - user-follow-read: | - Access your followers and who you are following. - user-library-modify: | - Manage your saved content. - user-library-read: | - Access your saved content. - user-modify-playback-state: | - Control playback on your Spotify clients and Spotify Connect devices. - user-read-currently-playing: | - Read your currently playing content. - user-read-email: | - Get your real email address. - user-read-playback-position: | - Read your position in content you have played. - user-read-playback-state: | - Read your currently playing content and Spotify Connect devices information. - user-read-private: | - Access your subscription details. - user-read-recently-played: | - Access your recently played items. - user-top-read: | - Read your top artists and content. - tokenUrl: https://accounts.spotify.com/api/token - type: oauth2 - x-spotify-policy: - $ref: ../policies.yaml - Attribution: {} - Broadcasting: {} - CommercialStreaming: {} - ContentAlteration: {} - Downloading: {} - MultipleIntegrations: {} - StandalonePreview: {} - Synchronization: {} - VisualAlteration: {} - metadataPolicyList: - - $ref: "#/components/x-spotify-policy/Downloading" - - $ref: "#/components/x-spotify-policy/VisualAlteration" - - $ref: "#/components/x-spotify-policy/Attribution" - playerPolicyList: - - $ref: "#/components/x-spotify-policy/CommercialStreaming" - - $ref: "#/components/x-spotify-policy/ContentAlteration" - - $ref: "#/components/x-spotify-policy/Synchronization" - - $ref: "#/components/x-spotify-policy/Broadcasting" diff --git a/api/app/titleConvo.js b/api/app/titleConvo.js index 06a62a0c9b8..ffb1d75638e 100644 --- a/api/app/titleConvo.js +++ b/api/app/titleConvo.js @@ -1,21 +1,6 @@ -// const { Configuration, OpenAIApi } = require('openai'); -const _ = require('lodash'); -const { genAzureChatCompletion } = require('../utils/genAzureEndpoints'); - -// const proxyEnvToAxiosProxy = (proxyString) => { -// if (!proxyString) return null; -// const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/; -// const [, protocol, username, password, host, port] = proxyString.match(regex); -// const proxyConfig = { -// protocol, -// host, -// port: port ? parseInt(port) : undefined, -// auth: username && password ? { username, password } : undefined -// }; - -// return proxyConfig; -// }; +const _ = require('lodash'); +const { genAzureChatCompletion, getAzureCredentials } = require('../utils/'); const titleConvo = async ({ text, response, oaiApiKey }) => { let title = 'New Chat'; @@ -34,7 +19,7 @@ const titleConvo = async ({ text, response, oaiApiKey }) => { ||>Title:` }; - const azure = process.env.AZURE_OPENAI_API_KEY ? true : false; + const azure = process.env.AZURE_API_KEY ? true : false; const options = { azure, reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null, @@ -53,12 +38,8 @@ const titleConvo = async ({ text, response, oaiApiKey }) => { let apiKey = oaiApiKey || process.env.OPENAI_API_KEY; if (azure) { - apiKey = process.env.AZURE_OPENAI_API_KEY; - titleGenClientOptions.reverseProxyUrl = genAzureChatCompletion({ - azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, - azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, - azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION - }); + apiKey = process.env.AZURE_API_KEY; + titleGenClientOptions.reverseProxyUrl = genAzureChatCompletion(getAzureCredentials()); } const titleGenClient = new ChatGPTClient(apiKey, titleGenClientOptions); diff --git a/api/models/Message.js b/api/models/Message.js index 1dc351ccea4..676682c23bf 100644 --- a/api/models/Message.js +++ b/api/models/Message.js @@ -14,6 +14,7 @@ module.exports = { error, unfinished, cancelled, + tokenCount = null, plugin = null, model = null, }) { @@ -31,6 +32,7 @@ module.exports = { error, unfinished, cancelled, + tokenCount, plugin, model }, @@ -43,14 +45,41 @@ module.exports = { parentMessageId, sender, text, - isCreatedByUser + isCreatedByUser, + tokenCount, }; } catch (err) { console.error(`Error saving message: ${err}`); throw new Error('Failed to save message.'); } }, + async updateMessage(message) { + try { + const { messageId, ...update } = message; + const updatedMessage = await Message.findOneAndUpdate( + { messageId }, + update, + { new: true } + ); + + if (!updatedMessage) { + throw new Error('Message not found.'); + } + return { + messageId: updatedMessage.messageId, + conversationId: updatedMessage.conversationId, + parentMessageId: updatedMessage.parentMessageId, + sender: updatedMessage.sender, + text: updatedMessage.text, + isCreatedByUser: updatedMessage.isCreatedByUser, + tokenCount: updatedMessage.tokenCount, + }; + } catch (err) { + console.error(`Error updating message: ${err}`); + throw new Error('Failed to update message.'); + } + }, async deleteMessagesSince({ messageId, conversationId }) { try { const message = await Message.findOne({ messageId }).exec(); diff --git a/api/models/index.js b/api/models/index.js index bda6239d7bd..62790f95fdb 100644 --- a/api/models/index.js +++ b/api/models/index.js @@ -1,10 +1,11 @@ -const { getMessages, saveMessage, deleteMessagesSince, deleteMessages } = require('./Message'); +const { getMessages, saveMessage, updateMessage, deleteMessagesSince, deleteMessages } = require('./Message'); const { getConvoTitle, getConvo, saveConvo } = require('./Conversation'); const { getPreset, getPresets, savePreset, deletePresets } = require('./Preset'); module.exports = { getMessages, saveMessage, + updateMessage, deleteMessagesSince, deleteMessages, diff --git a/api/models/schema/messageSchema.js b/api/models/schema/messageSchema.js index 608602d2133..2e367dc8614 100644 --- a/api/models/schema/messageSchema.js +++ b/api/models/schema/messageSchema.js @@ -31,6 +31,12 @@ const messageSchema = mongoose.Schema( type: String // required: true }, + tokenCount: { + type: Number + }, + refinedTokenCount: { + type: Number + }, sender: { type: String, required: true, @@ -41,6 +47,9 @@ const messageSchema = mongoose.Schema( required: true, meiliIndex: true }, + refinedMessageText: { + type: String + }, isCreatedByUser: { type: Boolean, required: true, diff --git a/api/server/controllers/PluginController.js b/api/server/controllers/PluginController.js index 758e364fd97..eca94d29cd8 100644 --- a/api/server/controllers/PluginController.js +++ b/api/server/controllers/PluginController.js @@ -25,7 +25,7 @@ const isPluginAuthenticated = (plugin) => { const getAvailablePluginsController = async (req, res) => { try { fs.readFile( - path.join(__dirname, '..', '..', 'app', 'langchain', 'tools', 'manifest.json'), + path.join(__dirname, '..', '..', 'app', 'clients', 'tools', 'manifest.json'), 'utf8', (err, data) => { if (err) { diff --git a/api/server/index.js b/api/server/index.js index a982aab1798..ec203703e45 100644 --- a/api/server/index.js +++ b/api/server/index.js @@ -42,7 +42,9 @@ config.validate(); // Validate the config if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) { require('../strategies/facebookStrategy'); } - if (process.env.OPENID_CLIENT_ID && process.env.OPENID_CLIENT_SECRET && process.env.OPENID_ISSUER && process.env.OPENID_SCOPE && process.env.OPENID_SESSION_SECRET) { + if (process.env.OPENID_CLIENT_ID && process.env.OPENID_CLIENT_SECRET && + process.env.OPENID_ISSUER && process.env.OPENID_SCOPE && + process.env.OPENID_SESSION_SECRET) { app.use(session({ secret: process.env.OPENID_SESSION_SECRET, resave: false, diff --git a/api/server/routes/ask/askOpenAI.js b/api/server/routes/ask/askOpenAI.js deleted file mode 100644 index 4a37cff9734..00000000000 --- a/api/server/routes/ask/askOpenAI.js +++ /dev/null @@ -1,286 +0,0 @@ -const express = require('express'); -const crypto = require('crypto'); -const router = express.Router(); -const addToCache = require('./addToCache'); -// const { getOpenAIModels } = require('../endpoints'); -const { titleConvo, askClient } = require('../../../app/'); -const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../../models'); -const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers'); -const requireJwtAuth = require('../../../middleware/requireJwtAuth'); - -const abortControllers = new Map(); - -router.post('/abort', requireJwtAuth, async (req, res) => { - const { abortKey } = req.body; - console.log(`req.body`, req.body); - if (!abortControllers.has(abortKey)) { - return res.status(404).send('Request not found'); - } - - const { abortController } = abortControllers.get(abortKey); - - abortControllers.delete(abortKey); - const ret = await abortController.abortAsk(); - console.log('Aborted request', abortKey); - console.log('Aborted message:', ret); - - res.send(JSON.stringify(ret)); -}); - -router.post('/', requireJwtAuth, async (req, res) => { - const { - endpoint, - text, - overrideParentMessageId = null, - parentMessageId, - conversationId: oldConversationId - } = req.body; - if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' }); - if (endpoint !== 'openAI') return handleError(res, { text: 'Illegal request' }); - - // build user message - const conversationId = oldConversationId || crypto.randomUUID(); - const isNewConversation = !oldConversationId; - const userMessageId = crypto.randomUUID(); - const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000'; - const userMessage = { - messageId: userMessageId, - sender: 'User', - text, - parentMessageId: userParentMessageId, - conversationId, - isCreatedByUser: true - }; - - // build endpoint option - const endpointOption = { - model: req.body?.model ?? 'gpt-3.5-turbo', - chatGptLabel: req.body?.chatGptLabel ?? null, - promptPrefix: req.body?.promptPrefix ?? null, - temperature: req.body?.temperature ?? 1, - top_p: req.body?.top_p ?? 1, - presence_penalty: req.body?.presence_penalty ?? 0, - frequency_penalty: req.body?.frequency_penalty ?? 0 - }; - - // const availableModels = getOpenAIModels(); - // if (availableModels.find((model) => model === endpointOption.model) === undefined) - // return handleError(res, { text: 'Illegal request: model' }); - - console.log('ask log', { - userMessage, - endpointOption, - conversationId - }); - - if (!overrideParentMessageId) { - await saveMessage(userMessage); - await saveConvo(req.user.id, { - ...userMessage, - ...endpointOption, - conversationId, - endpoint - }); - } - - // eslint-disable-next-line no-use-before-define - return await ask({ - isNewConversation, - userMessage, - endpointOption, - conversationId, - preSendRequest: true, - overrideParentMessageId, - req, - res - }); -}); - -const ask = async ({ - isNewConversation, - userMessage, - endpointOption, - conversationId, - preSendRequest = true, - overrideParentMessageId = null, - req, - res -}) => { - let { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage; - const userId = req.user.id; - let responseMessageId = crypto.randomUUID(); - - res.writeHead(200, { - Connection: 'keep-alive', - 'Content-Type': 'text/event-stream', - 'Cache-Control': 'no-cache, no-transform', - 'Access-Control-Allow-Origin': '*', - 'X-Accel-Buffering': 'no' - }); - - if (preSendRequest) sendMessage(res, { message: userMessage, created: true }); - - try { - let lastSavedTimestamp = 0; - const { onProgress: progressCallback, getPartialText } = createOnProgress({ - onProgress: ({ text }) => { - const currentTimestamp = Date.now(); - if (currentTimestamp - lastSavedTimestamp > 500) { - lastSavedTimestamp = currentTimestamp; - saveMessage({ - messageId: responseMessageId, - sender: endpointOption?.chatGptLabel || 'ChatGPT', - conversationId, - parentMessageId: overrideParentMessageId || userMessageId, - text: text, - unfinished: true, - cancelled: false, - error: false - }); - } - } - }); - - let abortController = new AbortController(); - abortController.abortAsk = async function () { - this.abort(); - - const responseMessage = { - messageId: responseMessageId, - sender: endpointOption?.chatGptLabel || 'ChatGPT', - conversationId, - parentMessageId: overrideParentMessageId || userMessageId, - text: getPartialText(), - unfinished: false, - cancelled: true, - error: false - }; - - saveMessage(responseMessage); - await addToCache({ endpoint: 'openAI', endpointOption, userMessage, responseMessage }); - - return { - title: await getConvoTitle(req.user.id, conversationId), - final: true, - conversation: await getConvo(req.user.id, conversationId), - requestMessage: userMessage, - responseMessage: responseMessage - }; - }; - const abortKey = conversationId; - abortControllers.set(abortKey, { abortController, ...endpointOption }); - const oaiApiKey = req.body?.token ?? null; - - let response = await askClient({ - text, - parentMessageId: userParentMessageId, - conversationId, - oaiApiKey, - ...endpointOption, - onProgress: progressCallback.call(null, { - res, - text, - parentMessageId: overrideParentMessageId || userMessageId - }), - abortController, - userId - }); - - abortControllers.delete(abortKey); - console.log('CLIENT RESPONSE', response); - - const newConversationId = response.conversationId || conversationId; - const newUserMessageId = response.parentMessageId || userMessageId; - const newResponseMessageId = response.messageId; - - // STEP1 generate response message - response.text = response.response || '**ChatGPT refused to answer.**'; - - let responseMessage = { - conversationId: newConversationId, - messageId: responseMessageId, - newMessageId: newResponseMessageId, - parentMessageId: overrideParentMessageId || newUserMessageId, - text: await handleText(response), - sender: endpointOption?.chatGptLabel || 'ChatGPT', - unfinished: false, - cancelled: false, - error: false - }; - - await saveMessage(responseMessage); - responseMessage.messageId = newResponseMessageId; - - // STEP2 update the conversation - let conversationUpdate = { conversationId: newConversationId, endpoint: 'openAI' }; - if (conversationId != newConversationId) - if (isNewConversation) { - // change the conversationId to new one - conversationUpdate = { - ...conversationUpdate, - conversationId: conversationId, - newConversationId: newConversationId - }; - } else { - // create new conversation - conversationUpdate = { - ...conversationUpdate, - ...endpointOption - }; - } - - await saveConvo(req.user.id, conversationUpdate); - conversationId = newConversationId; - - // STEP3 update the user message - userMessage.conversationId = newConversationId; - userMessage.messageId = newUserMessageId; - - // If response has parentMessageId, the fake userMessage.messageId should be updated to the real one. - if (!overrideParentMessageId) - await saveMessage({ - ...userMessage, - messageId: userMessageId, - newMessageId: newUserMessageId - }); - userMessageId = newUserMessageId; - - sendMessage(res, { - title: await getConvoTitle(req.user.id, conversationId), - final: true, - conversation: await getConvo(req.user.id, conversationId), - requestMessage: userMessage, - responseMessage: responseMessage - }); - res.end(); - - if (userParentMessageId == '00000000-0000-0000-0000-000000000000') { - const title = await titleConvo({ - endpoint: endpointOption?.endpoint, - text, - response: responseMessage, - oaiApiKey - }); - await saveConvo(req.user.id, { - conversationId: conversationId, - title - }); - } - } catch (error) { - console.error(error); - const errorMessage = { - messageId: responseMessageId, - sender: endpointOption?.chatGptLabel || 'ChatGPT', - conversationId, - parentMessageId: overrideParentMessageId || userMessageId, - unfinished: false, - cancelled: false, - error: true, - text: error.message - }; - await saveMessage(errorMessage); - handleError(res, errorMessage); - } -}; - -module.exports = router; diff --git a/api/server/routes/ask/askGoogle.js b/api/server/routes/ask/google.js similarity index 97% rename from api/server/routes/ask/askGoogle.js rename to api/server/routes/ask/google.js index f9d41cb8bec..471bee295eb 100644 --- a/api/server/routes/ask/askGoogle.js +++ b/api/server/routes/ask/google.js @@ -1,8 +1,8 @@ const express = require('express'); const router = express.Router(); const crypto = require('crypto'); -const { titleConvo } = require('../../../app/'); -const GoogleClient = require('../../../app/google/GoogleClient'); +const { titleConvo, GoogleClient } = require('../../../app'); +// const GoogleClient = require('../../../app/google/GoogleClient'); const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../../models'); const { handleError, sendMessage, createOnProgress } = require('./handlers'); const requireJwtAuth = require('../../../middleware/requireJwtAuth'); diff --git a/api/server/routes/ask/askGPTPlugins.js b/api/server/routes/ask/gptPlugins.js similarity index 84% rename from api/server/routes/ask/askGPTPlugins.js rename to api/server/routes/ask/gptPlugins.js index 10465e71ce5..f9a9b5bbcb4 100644 --- a/api/server/routes/ask/askGPTPlugins.js +++ b/api/server/routes/ask/gptPlugins.js @@ -1,9 +1,7 @@ const express = require('express'); const router = express.Router(); -const { titleConvo } = require('../../../app/'); -// const { getOpenAIModels } = require('../endpoints'); -const ChatAgent = require('../../../app/langchain/ChatAgent'); -const { validateTools } = require('../../../app/langchain/tools/util'); +const { titleConvo, validateTools, PluginsClient } = require('../../../app'); +const { abortMessage, getAzureCredentials } = require('../../../utils'); const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../../models'); const { handleError, @@ -17,20 +15,7 @@ const requireJwtAuth = require('../../../middleware/requireJwtAuth'); const abortControllers = new Map(); router.post('/abort', requireJwtAuth, async (req, res) => { - const { abortKey } = req.body; - console.log(`req.body`, req.body); - if (!abortControllers.has(abortKey)) { - return res.status(404).send('Request not found'); - } - - const { abortController } = abortControllers.get(abortKey); - - abortControllers.delete(abortKey); - const ret = await abortController.abortAsk(); - console.log('Aborted request', abortKey); - console.log('Aborted message:', ret); - - res.send(JSON.stringify(ret)); + return await abortMessage(req, res, abortControllers); }); router.post('/', requireJwtAuth, async (req, res) => { @@ -47,7 +32,7 @@ router.post('/', requireJwtAuth, async (req, res) => { // presence_penalty: 0, // frequency_penalty: 0 }; - + const tools = req.body?.tools.map((tool) => tool.pluginKey) ?? []; // build endpoint option const endpointOption = { @@ -73,6 +58,7 @@ router.post('/', requireJwtAuth, async (req, res) => { // eslint-disable-next-line no-use-before-define return await ask({ text, + endpoint, endpointOption, conversationId, parentMessageId, @@ -81,7 +67,7 @@ router.post('/', requireJwtAuth, async (req, res) => { }); }); -const ask = async ({ text, endpointOption, parentMessageId = null, conversationId, req, res }) => { +const ask = async ({ text, endpoint, endpointOption, parentMessageId = null, conversationId, req, res }) => { res.writeHead(200, { Connection: 'keep-alive', 'Content-Type': 'text/event-stream', @@ -175,21 +161,18 @@ const ask = async ({ text, endpointOption, parentMessageId = null, conversationI endpointOption.tools = await validateTools(user, endpointOption.tools); const clientOptions = { debug: true, + endpoint, reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null, proxy: process.env.PROXY || null, ...endpointOption }; - if (process.env.AZURE_OPENAI_API_KEY) { - clientOptions.azure = { - azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY, - azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, - azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, - azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION - }; + if (process.env.PLUGINS_USE_AZURE === 'true') { + clientOptions.azure = getAzureCredentials(); } - const chatAgent = new ChatAgent(process.env.OPENAI_API_KEY, clientOptions); + const oaiApiKey = req.body?.token ?? process.env.OPENAI_API_KEY; + const chatAgent = new PluginsClient(oaiApiKey, clientOptions); const onAgentAction = (action) => { const formattedAction = formatAction(action); @@ -232,8 +215,8 @@ const ask = async ({ text, endpointOption, parentMessageId = null, conversationI response.parentMessageId = overrideParentMessageId; } - // console.log('CLIENT RESPONSE'); - // console.dir(response, { depth: null }); + console.log('CLIENT RESPONSE'); + console.dir(response, { depth: null }); response.plugin = { ...plugin, loading: false }; await saveMessage(response); diff --git a/api/server/routes/ask/handlers.js b/api/server/routes/ask/handlers.js index f415ac0d061..d709512c249 100644 --- a/api/server/routes/ask/handlers.js +++ b/api/server/routes/ask/handlers.js @@ -148,7 +148,7 @@ function formatAction(action) { formattedAction.inputStr = formattedAction.inputStr.replace('N/A - ', ''); } else { const hasThought = formattedAction.thought.length > 0; - const thought = hasThought ? `\n\tthought: ${formattedAction.thought}` : ''; + const thought = hasThought ? `\n\tthought: ${formattedAction.thought}` : ''; formattedAction.inputStr = `{\n\tplugin: ${formattedAction.plugin}\n\tinput: ${formattedAction.input}\n${thought}}`; } diff --git a/api/server/routes/ask/index.js b/api/server/routes/ask/index.js index 48381d27ef9..9ba81b8a124 100644 --- a/api/server/routes/ask/index.js +++ b/api/server/routes/ask/index.js @@ -1,17 +1,18 @@ const express = require('express'); const router = express.Router(); // const askAzureOpenAI = require('./askAzureOpenAI';) -const askOpenAI = require('./askOpenAI'); -const askGoogle = require('./askGoogle'); +// const askOpenAI = require('./askOpenAI'); +const openAI = require('./openAI'); +const google = require('./google'); const askBingAI = require('./askBingAI'); +const gptPlugins = require('./gptPlugins'); const askChatGPTBrowser = require('./askChatGPTBrowser'); -const askGPTPlugins = require('./askGPTPlugins'); // router.use('/azureOpenAI', askAzureOpenAI); -router.use('/openAI', askOpenAI); -router.use('/google', askGoogle); +router.use(['/azureOpenAI', '/openAI'], openAI); +router.use('/google', google); router.use('/bingAI', askBingAI); router.use('/chatGPTBrowser', askChatGPTBrowser); -router.use('/gptPlugins', askGPTPlugins); +router.use('/gptPlugins', gptPlugins); module.exports = router; diff --git a/api/server/routes/ask/openAI.js b/api/server/routes/ask/openAI.js new file mode 100644 index 00000000000..ae597a7bbe6 --- /dev/null +++ b/api/server/routes/ask/openAI.js @@ -0,0 +1,211 @@ +const express = require('express'); +const router = express.Router(); +const { titleConvo, OpenAIClient } = require('../../../app'); +const { getAzureCredentials, abortMessage } = require('../../../utils'); +const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../../models'); +const { + handleError, + sendMessage, + createOnProgress, +} = require('./handlers'); +const requireJwtAuth = require('../../../middleware/requireJwtAuth'); + +const abortControllers = new Map(); + +router.post('/abort', requireJwtAuth, async (req, res) => { + return await abortMessage(req, res, abortControllers); +}); + +router.post('/', requireJwtAuth, async (req, res) => { + const { endpoint, text, parentMessageId, conversationId } = req.body; + if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' }); + const isOpenAI = endpoint === 'openAI' || endpoint === 'azureOpenAI'; + if (!isOpenAI) return handleError(res, { text: 'Illegal request' }); + + // build endpoint option + const endpointOption = { + chatGptLabel: req.body?.chatGptLabel ?? null, + promptPrefix: req.body?.promptPrefix ?? null, + modelOptions: { + model: req.body?.model ?? 'gpt-3.5-turbo', + temperature: req.body?.temperature ?? 1, + top_p: req.body?.top_p ?? 1, + presence_penalty: req.body?.presence_penalty ?? 0, + frequency_penalty: req.body?.frequency_penalty ?? 0 + } + }; + + console.log('ask log'); + console.dir({ text, conversationId, endpointOption }, { depth: null }); + + // eslint-disable-next-line no-use-before-define + return await ask({ + text, + endpointOption, + conversationId, + parentMessageId, + endpoint, + req, + res + }); +}); + +const ask = async ({ text, endpointOption, parentMessageId = null, endpoint, conversationId, req, res }) => { + res.writeHead(200, { + Connection: 'keep-alive', + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'no-cache, no-transform', + 'Access-Control-Allow-Origin': '*', + 'X-Accel-Buffering': 'no' + }); + let userMessage; + let userMessageId; + let responseMessageId; + let lastSavedTimestamp = 0; + const newConvo = !conversationId; + const { overrideParentMessageId = null } = req.body; + const user = req.user.id; + + const getIds = (data) => { + userMessage = data.userMessage; + userMessageId = userMessage.messageId; + responseMessageId = data.responseMessageId; + if (!conversationId) { + conversationId = data.conversationId; + } + }; + + const { onProgress: progressCallback, getPartialText } = createOnProgress({ + onProgress: ({ text: partialText }) => { + const currentTimestamp = Date.now(); + + if (currentTimestamp - lastSavedTimestamp > 500) { + lastSavedTimestamp = currentTimestamp; + saveMessage({ + messageId: responseMessageId, + sender: 'ChatGPT', + conversationId, + parentMessageId: overrideParentMessageId || userMessageId, + text: partialText, + model: endpointOption.modelOptions.model, + unfinished: false, + cancelled: true, + error: false + }); + } + } + }); + + const abortController = new AbortController(); + abortController.abortAsk = async function () { + this.abort(); + + const responseMessage = { + messageId: responseMessageId, + sender: endpointOption?.chatGptLabel || 'ChatGPT', + conversationId, + parentMessageId: overrideParentMessageId || userMessageId, + text: getPartialText(), + model: endpointOption.modelOptions.model, + unfinished: false, + cancelled: true, + error: false, + }; + + saveMessage(responseMessage); + + return { + title: await getConvoTitle(req.user.id, conversationId), + final: true, + conversation: await getConvo(req.user.id, conversationId), + requestMessage: userMessage, + responseMessage: responseMessage + }; + }; + + const onStart = (userMessage) => { + sendMessage(res, { message: userMessage, created: true }); + abortControllers.set(userMessage.conversationId, { abortController, ...endpointOption }); + }; + + try { + const clientOptions = { + // debug: true, + // contextStrategy: 'refine', + reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null, + proxy: process.env.PROXY || null, + endpoint, + ...endpointOption + }; + + let oaiApiKey = req.body?.token ?? process.env.OPENAI_API_KEY; + + if (process.env.AZURE_API_KEY && endpoint === 'azureOpenAI') { + clientOptions.azure = getAzureCredentials(); + // clientOptions.reverseProxyUrl = process.env.AZURE_REVERSE_PROXY ?? genAzureChatCompletion({ ...clientOptions.azure }); + oaiApiKey = clientOptions.azure.azureOpenAIApiKey; + } + + const client = new OpenAIClient(oaiApiKey, clientOptions); + + let response = await client.sendMessage(text, { + user, + parentMessageId, + conversationId, + overrideParentMessageId, + getIds, + onStart, + onProgress: progressCallback.call(null, { + res, + text, + parentMessageId: overrideParentMessageId || userMessageId + }), + abortController + }); + + if (overrideParentMessageId) { + response.parentMessageId = overrideParentMessageId; + } + + console.log('promptTokens, completionTokens:', response.promptTokens, response.completionTokens); + await saveMessage(response); + + sendMessage(res, { + title: await getConvoTitle(req.user.id, conversationId), + final: true, + conversation: await getConvo(req.user.id, conversationId), + requestMessage: userMessage, + responseMessage: response + }); + res.end(); + + if (parentMessageId == '00000000-0000-0000-0000-000000000000' && newConvo) { + const title = await titleConvo({ text, response }); + await saveConvo(req.user.id, { + conversationId, + title + }); + } + } catch (error) { + console.error(error); + const partialText = getPartialText(); + if (partialText?.length > 2) { + return await abortMessage(req, res, abortControllers); + } else { + const errorMessage = { + messageId: responseMessageId, + sender: 'ChatGPT', + conversationId, + parentMessageId: userMessageId, + unfinished: false, + cancelled: false, + error: true, + text: error.message + }; + await saveMessage(errorMessage); + handleError(res, errorMessage); + } + } +}; + +module.exports = router; diff --git a/api/server/routes/endpoints.js b/api/server/routes/endpoints.js index 2e54c0b42b5..5d97194989a 100644 --- a/api/server/routes/endpoints.js +++ b/api/server/routes/endpoints.js @@ -1,10 +1,11 @@ const express = require('express'); const router = express.Router(); -const { availableTools } = require('../../app/langchain/tools'); +const { availableTools } = require('../../app/clients/tools'); -const getOpenAIModels = () => { +const getOpenAIModels = (opts = { azure: false }) => { let models = ['gpt-4', 'gpt-4-0613', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-0301', 'text-davinci-003' ]; - if (process.env.OPENAI_MODELS) models = String(process.env.OPENAI_MODELS).split(','); + const key = opts.azure ? 'AZURE_OPENAI_MODELS' : 'OPENAI_MODELS'; + if (process.env[key]) models = String(process.env[key]).split(','); return models; }; @@ -47,12 +48,15 @@ router.get('/', async function (req, res) { key || palmUser ? { userProvide: palmUser, availableModels: ['chat-bison', 'text-bison', 'codechat-bison'] } : false; - const azureOpenAI = !!process.env.AZURE_OPENAI_API_KEY; - const apiKey = process.env.OPENAI_API_KEY || process.env.AZURE_OPENAI_API_KEY; - const openAI = apiKey - ? { availableModels: getOpenAIModels(), userProvide: apiKey === 'user_provided' } + const openAIApiKey = process.env.OPENAI_API_KEY; + const azureOpenAIApiKey = process.env.AZURE_API_KEY; + const openAI = openAIApiKey + ? { availableModels: getOpenAIModels(), userProvide: openAIApiKey === 'user_provided' } : false; - const gptPlugins = apiKey + const azureOpenAI = azureOpenAIApiKey + ? { availableModels: getOpenAIModels({ azure: true}), userProvide: azureOpenAIApiKey === 'user_provided' } + : false; + const gptPlugins = openAIApiKey || azureOpenAIApiKey ? { availableModels: getPluginModels(), availableTools, availableAgents: ['classic', 'functions'] } : false; const bingAI = process.env.BINGAI_TOKEN diff --git a/api/server/services/PluginService.js b/api/server/services/PluginService.js index 114b6590776..7e49ad0ebf5 100644 --- a/api/server/services/PluginService.js +++ b/api/server/services/PluginService.js @@ -1,5 +1,5 @@ const PluginAuth = require('../../models/schema/pluginAuthSchema'); -const { encrypt, decrypt } = require('../../utils/crypto'); +const { encrypt, decrypt } = require('../../utils/'); const getUserPluginAuthValue = async (user, authField) => { try { diff --git a/api/server/services/auth.service.js b/api/server/services/auth.service.js index eb4a5dcc0d3..e570e052a43 100644 --- a/api/server/services/auth.service.js +++ b/api/server/services/auth.service.js @@ -1,10 +1,9 @@ const User = require('../../models/User'); const Token = require('../../models/schema/tokenSchema'); -const sendEmail = require('../../utils/sendEmail'); const crypto = require('crypto'); const bcrypt = require('bcryptjs'); const { registerSchema } = require('../../strategies/validators'); -const migrateDataToFirstUser = require('../../utils/migrateDataToFirstUser'); +const { migrateDataToFirstUser, sendEmail } = require('../../utils'); const config = require('../../../config/loader'); const domains = config.domains; @@ -63,7 +62,7 @@ const registerUser = async (user) => { { name: 'Request params:', value: user }, { name: 'Existing user:', value: existingUser } ); - + // Sleep for 1 second await new Promise((resolve) => setTimeout(resolve, 1000)); @@ -100,7 +99,7 @@ const registerUser = async (user) => { return { status: 500, message: err?.message || 'Something went wrong' }; } }; - + /** * Request password reset * diff --git a/e2e/.env.test.example b/api/test/.env.test.example similarity index 100% rename from e2e/.env.test.example rename to api/test/.env.test.example diff --git a/api/utils/abortMessage.js b/api/utils/abortMessage.js new file mode 100644 index 00000000000..fea2e7d31a6 --- /dev/null +++ b/api/utils/abortMessage.js @@ -0,0 +1,18 @@ +async function abortMessage(req, res, abortControllers) { + const { abortKey } = req.body; + console.log(`req.body`, req.body); + if (!abortControllers.has(abortKey)) { + return res.status(404).send('Request not found'); + } + + const { abortController } = abortControllers.get(abortKey); + + abortControllers.delete(abortKey); + const ret = await abortController.abortAsk(); + console.log('Aborted request', abortKey); + console.log('Aborted message:', ret); + + res.send(JSON.stringify(ret)); +} + +module.exports = abortMessage; \ No newline at end of file diff --git a/api/utils/azureUtils.js b/api/utils/azureUtils.js new file mode 100644 index 00000000000..13687e4763c --- /dev/null +++ b/api/utils/azureUtils.js @@ -0,0 +1,22 @@ +const genAzureEndpoint = ({ azureOpenAIApiInstanceName, azureOpenAIApiDeploymentName }) => { + return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}`; +} + +const genAzureChatCompletion = ({ + azureOpenAIApiInstanceName, + azureOpenAIApiDeploymentName, + azureOpenAIApiVersion +}) => { + return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}/chat/completions?api-version=${azureOpenAIApiVersion}`; +} + +const getAzureCredentials = () => { + return { + azureOpenAIApiKey: process.env.AZURE_API_KEY, + azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, + azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, + azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION + } +} + +module.exports = { genAzureEndpoint, genAzureChatCompletion, getAzureCredentials }; diff --git a/api/utils/genAzureEndpoints.js b/api/utils/genAzureEndpoints.js deleted file mode 100644 index e89308a2e26..00000000000 --- a/api/utils/genAzureEndpoints.js +++ /dev/null @@ -1,13 +0,0 @@ -function genAzureEndpoint({ azureOpenAIApiInstanceName, azureOpenAIApiDeploymentName }) { - return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}`; -} - -function genAzureChatCompletion({ - azureOpenAIApiInstanceName, - azureOpenAIApiDeploymentName, - azureOpenAIApiVersion -}) { - return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}/chat/completions?api-version=${azureOpenAIApiVersion}`; -} - -module.exports = { genAzureEndpoint, genAzureChatCompletion }; diff --git a/api/utils/index.js b/api/utils/index.js new file mode 100644 index 00000000000..96bf09e1249 --- /dev/null +++ b/api/utils/index.js @@ -0,0 +1,16 @@ +const azureUtils = require('./azureUtils'); +const cryptoUtils = require('./crypto'); +const { tiktokenModels, maxTokensMap } = require('./tokens'); +const migrateConversations = require('./migrateDataToFirstUser'); +const sendEmail = require('./sendEmail'); +const abortMessage = require('./abortMessage'); + +module.exports = { + ...cryptoUtils, + ...azureUtils, + maxTokensMap, + tiktokenModels, + migrateConversations, + sendEmail, + abortMessage +} \ No newline at end of file diff --git a/api/utils/tiktokenModels.js b/api/utils/tokens.js similarity index 73% rename from api/utils/tiktokenModels.js rename to api/utils/tokens.js index 3107cfff0c8..39d1b182c90 100644 --- a/api/utils/tiktokenModels.js +++ b/api/utils/tokens.js @@ -37,4 +37,15 @@ const models = [ 'gpt-3.5-turbo-0301' ]; -module.exports = new Set(models); +const maxTokensMap = { + 'gpt-4': 8191, + 'gpt-4-0613': 8191, + 'gpt-4-32k': 32767, + 'gpt-4-32k-0613': 32767, + 'gpt-3.5-turbo': 4095, + 'gpt-3.5-turbo-0613': 4095, + 'gpt-3.5-turbo-0301': 4095, + 'gpt-3.5-turbo-16k': 15999, +}; + +module.exports = { tiktokenModels: new Set(models), maxTokensMap }; diff --git a/client/src/components/Endpoints/OpenAI/Settings.jsx b/client/src/components/Endpoints/OpenAI/Settings.jsx index 956ec3f8ae6..87b0ac26f40 100644 --- a/client/src/components/Endpoints/OpenAI/Settings.jsx +++ b/client/src/components/Endpoints/OpenAI/Settings.jsx @@ -29,6 +29,7 @@ function Settings(props) { setOption } = props; const endpoint = props.endpoint || 'openAI'; + const isOpenAI = endpoint === 'openAI' || endpoint === 'azureOpenAI'; const endpointsConfig = useRecoilValue(store.endpointsConfig); @@ -59,7 +60,7 @@ function Settings(props) { containerClassName="flex w-full resize-none" /> - {endpoint === 'openAI' && ( + {isOpenAI && ( <>
); - } else if (!isTokenProvided && endpoint !== 'openAI') { + } else if (!isTokenProvided && (endpoint !== 'openAI' || endpoint !== 'azureOpenAI' )) { return ( <>