Skip to Content
PurAI is SHUT DOWN. This website is purely for archival purposes. No such services exist anymore.

Using OpenAI Chat Completion

Request

URL: https://ai.purlabs.xyz/openai/chat/completions

Request Method: POST

TS Request Interface

⚠️

One of the model and personality fields must be set!

interface Request { model?: string; personality?: string; disableErrorSummarization?: true; primaryProvider?: string; messages: { role: 'system' | 'user' | 'assistant' | 'function'; content: string; name?: string; function_call?: object; }[]; functions?: { name: string; description?: string; parameters: { type: 'object'; properties: { [name: string]: { type: string; description: string; }; }; required?: string[]; }; }[]; function_call?: 'none' | 'auto' | { name: string; }; temperature?: number; top_p?: number; n?: number; stream?: boolean; stop?: string | any[] | null; max_tokens?: number; presence_penalty?: number; frequency_penalty?: number; logit_bias?: object | null; user?: string; };

Request API Reference

⚠️

Refer to OpenAI’s docs  to learn more about all the paramters, and functions!

ParameterType (check TS Interface)RequiredDescription
modelstringRequired if no personalityThe model you would like to use. Refer to the Listing Available Models page for more information.
personalitystringRequired if no modelSpecifies the personality for the conversation. Refer to the Personalities page for more details.
disableErrorSummarizationbooleanOptionalAI-powered error summarizer: most error messages will be summarized according to the error records (if all providers errored).
primaryProviderstringOptionalThe identifier of a specific upstream provider to try first. If it returns with any error(s), the API will try other providers.
messagesarrayRequiredA list of messages comprising the conversation so far.
messages.role'system' | 'user' | 'assistant' | 'function'RequiredThe role of the message’s author. One of system, user, assistant, or function.
messages.contentstring | nullRequiredThe contents of the message. content is required for all messages, and may be null for assistant messages with function calls.
messages.namestringOptionalThe name of the author of this message. name is required if role is function, and it should be the name of the function whose response is in the content. May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
messages.function_callobjectOptionalThe name and arguments of a function that should be called, as generated by the model.
messages.function_call.namestringRequiredThe name of the function to call.
messages.function_call.argumentsstringRequiredThe arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
functionsarrayOptionalA list of functions the model may generate JSON inputs for.
functions.namestringRequiredThe name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
functions.descriptionstringOptionalA description of what the function does, used by the model to choose when and how to call the function.
functions.parametersobjectRequiredThe parameters the functions accept, described as a JSON Schema object. See the OpenAI guide .
function_callstring | objectOptionalControls how the model responds to function calls. “none” means the model does not call a function, and responds to the end-user. “auto” means the model can pick between an end-user or calling a function. Specifying a particular function via {"name":"my_function"} forces the model to call that function. “none” is the default when no functions are present. “auto” is the default if functions are present.
temperaturenumber | nullOptionalDefaults to 1. What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.
top_pnumber | nullOptionalDefaults to 1. An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.
ninteger | nullOptionalDefaults to 1. How many chat completion choices to generate for each input message.
streamboolean | nullOptionalDefaults to false. If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
stopstring | array | nullOptionalDefaults to null. Up to 4 sequences where the API will stop generating further tokens.
max_tokensintegerOptionalDefaults to inf. The maximum number of tokens to generate in the chat completion.
presence_penaltynumber | nullOptionalDefaults to 0. Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
frequency_penaltynumber | nullOptionalDefaults to 0. Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
logit_biasmapOptionalDefaults to null. Modify the likelihood of specified tokens appearing in the completion. Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase the likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
userstringOptionalA unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more .

Example JSON Request Body

{ "model": "gpt-3.5-turbo", "messages": [ { "role": "user", "content": "Hello! How are you?" } ] }

Response

TS Response Interface

interface Response { id: string; object: 'chat.completion'; created: number; model: string; choices: { index: number; message: { role: 'assistant'; content: string; }; finish_reason: any; }[]; usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; }; overwritten: boolean; provider: string; cache?: { status: 500, error: { message: 'Some of our providers returned with errors. Errors are automatically reported to our developers.'; records: { provider: string; response: { status: number; data?: any; }; }[]; }; }; calledFunctions: [ { name: string; arguments: string; }, { role: 'function'; name: string; content: string; } ][]; } | { status: 500, message: string; records: { provider: string; response: { status: number; data?: any; }; }[]; };

Response API Reference

PropertyTypeDescription
idstringThe ID of the chat completion response.
object'chat.completion'The object type of the response.
creatednumberTimestamp of when the response was created.
modelstringThe model used for generating the response.
choicesarray (of objects)An array of choices containing generated message, index, and finish reason.
choices.indexnumberIndex of the choice in the array.
choices.messageobjectThe generated message by the assistant.
choices.message.role'assistant'The role of the generated message (always ‘assistant’).
choices.message.contentstringThe content of the generated message.
choices.finish_reasonanyThe reason why the choice was finished.
usageobjectUsage information about tokens.
usage.prompt_tokensnumberNumber of tokens in the prompt.
usage.completion_tokensnumberNumber of tokens in the completion.
usage.total_tokensnumberTotal number of tokens in the response.
overwrittenbooleanIndicates if the response was overwritten.
providerstringThe provider used for generating the response.
cacheobject(Optional) Cache information.
cache.status500Cache status.
cache.errorobjectCache error details.
cache.error.messagestringCache error message.
cache.error.recordsarray (of objects)Records of providers and their error responses.
cache.error.records.providerstringProvider that returned an error.
cache.error.records.responseobjectResponse details of the provider error.
cache.error.records.response.statusnumberStatus code of the provider error response.
cache.error.records.response.dataany(Optional) Data associated with the provider error.
calledFunctionsarray (of objects)Array of called function details.
calledFunctions[].namestringName of the called function.
calledFunctions[].argumentsstringArguments of the called function.
calledFunctions[].role'function'(Optional) Role of the called function (if applicable).
calledFunctions[].contentstring(Optional) Content of the called function (if applicable).
status500Status code indicating an error in the response.
messagestringError message.
recordsarray (of objects)Records of providers and their error responses.
records.providerstringProvider that returned an error.
records.responseobjectResponse details of the provider error.
records.response.statusnumberStatus code of the provider error response.
records.response.dataany(Optional) Data associated with the provider error.

Example JSON Response

{ "id": "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK", "object": "chat.completion", "created": 0, "model": "gpt-3.5-turbo-0301", "usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0 }, "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello! As an AI language model, I don't have feelings, but I'm here and ready to assist you. How can I help you today?" }, "finish_reason": null } ], "provider": "provider-1", "overwritten": false, "cache": { "status": 500, "error": { "message": "Some of our providers returned with errors. Errors are automatically reported to our developers.", "records": [ { "provider": "provider-2", "response": { "status": 404, "data": "404 Not Found" } }, { "provider": "provider-3", "response": { "status": 403, "data": "403 Forbidden" } }, { "provider": "provider-4", "response": { "status": 504, "data": "timeout of 60000ms exceeded" } } ] } } }
Last updated on