Skip to main content

Chat completion

Chat completion or text completion is super simple using the middlebop node client. It's a single function that you pass the wanted AI model to and your middlebop API key. Input and output data will look the same no matter the AI model.

Create a chat completion

You can create a chat completion by passing your middlebop API key a long with an array of messages.

See our supported models
See the message syntax

import { MiddlebopChatMessage, sendChat } from "@middlebop/client";

const middlebopApiKey = "mb-yourSuperSecretApiKey";

const messages: MiddlebopChatMessage[] = [
{
role: "user",
content: {
type: "text",
text: "Hello, who are you?",
},
},
];

const response = await sendChat({
messages,
model: "gpt-4", // any supported model that you'd like to chat with
middlebopApiKey,
});

console.log(response.data);

Response

The response will be an array of messages. Most times the array will contain just 1 text response but when using function calling, it might respond with multiple function calls if supported.

response.data
{
"responseMessages": [
{
"role": "assistant",
"content": {
"type": "text",
"text": "Hello! I'm AI developed by OpenAI. How can I help you today?"
}
}
]
}