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!
| Parameter | Type (check TS Interface) | Required | Description |
|---|---|---|---|
model | string | Required if no personality | The model you would like to use. Refer to the Listing Available Models page for more information. |
personality | string | Required if no model | Specifies the personality for the conversation. Refer to the Personalities page for more details. |
disableErrorSummarization | boolean | Optional | AI-powered error summarizer: most error messages will be summarized according to the error records (if all providers errored). |
primaryProvider | string | Optional | The identifier of a specific upstream provider to try first. If it returns with any error(s), the API will try other providers. |
messages | array | Required | A list of messages comprising the conversation so far. |
messages.role | 'system' | 'user' | 'assistant' | 'function' | Required | The role of the message’s author. One of system, user, assistant, or function. |
messages.content | string | null | Required | The contents of the message. content is required for all messages, and may be null for assistant messages with function calls. |
messages.name | string | Optional | The 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_call | object | Optional | The name and arguments of a function that should be called, as generated by the model. |
messages.function_call.name | string | Required | The name of the function to call. |
messages.function_call.arguments | string | Required | The 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. |
functions | array | Optional | A list of functions the model may generate JSON inputs for. |
functions.name | string | Required | The 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.description | string | Optional | A description of what the function does, used by the model to choose when and how to call the function. |
functions.parameters | object | Required | The parameters the functions accept, described as a JSON Schema object. See the OpenAI guide . |
function_call | string | object | Optional | Controls 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. |
temperature | number | null | Optional | Defaults 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_p | number | null | Optional | Defaults 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. |
n | integer | null | Optional | Defaults to 1. How many chat completion choices to generate for each input message. |
stream | boolean | null | Optional | Defaults 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. |
stop | string | array | null | Optional | Defaults to null. Up to 4 sequences where the API will stop generating further tokens. |
max_tokens | integer | Optional | Defaults to inf. The maximum number of tokens to generate in the chat completion. |
presence_penalty | number | null | Optional | Defaults 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_penalty | number | null | Optional | Defaults 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_bias | map | Optional | Defaults 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. |
user | string | Optional | A 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
| Property | Type | Description |
|---|---|---|
id | string | The ID of the chat completion response. |
object | 'chat.completion' | The object type of the response. |
created | number | Timestamp of when the response was created. |
model | string | The model used for generating the response. |
choices | array (of objects) | An array of choices containing generated message, index, and finish reason. |
choices.index | number | Index of the choice in the array. |
choices.message | object | The generated message by the assistant. |
choices.message.role | 'assistant' | The role of the generated message (always ‘assistant’). |
choices.message.content | string | The content of the generated message. |
choices.finish_reason | any | The reason why the choice was finished. |
usage | object | Usage information about tokens. |
usage.prompt_tokens | number | Number of tokens in the prompt. |
usage.completion_tokens | number | Number of tokens in the completion. |
usage.total_tokens | number | Total number of tokens in the response. |
overwritten | boolean | Indicates if the response was overwritten. |
provider | string | The provider used for generating the response. |
cache | object | (Optional) Cache information. |
cache.status | 500 | Cache status. |
cache.error | object | Cache error details. |
cache.error.message | string | Cache error message. |
cache.error.records | array (of objects) | Records of providers and their error responses. |
cache.error.records.provider | string | Provider that returned an error. |
cache.error.records.response | object | Response details of the provider error. |
cache.error.records.response.status | number | Status code of the provider error response. |
cache.error.records.response.data | any | (Optional) Data associated with the provider error. |
calledFunctions | array (of objects) | Array of called function details. |
calledFunctions[].name | string | Name of the called function. |
calledFunctions[].arguments | string | Arguments of the called function. |
calledFunctions[].role | 'function' | (Optional) Role of the called function (if applicable). |
calledFunctions[].content | string | (Optional) Content of the called function (if applicable). |
status | 500 | Status code indicating an error in the response. |
message | string | Error message. |
records | array (of objects) | Records of providers and their error responses. |
records.provider | string | Provider that returned an error. |
records.response | object | Response details of the provider error. |
records.response.status | number | Status code of the provider error response. |
records.response.data | any | (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