Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Last active June 4, 2025 16:10
Show Gist options
  • Save Kerollmops/4bf96d20a3c53027b3df5299f9a22129 to your computer and use it in GitHub Desktop.
Save Kerollmops/4bf96d20a3c53027b3df5299f9a22129 to your computer and use it in GitHub Desktop.
An example usage of the Meilisearch chat completions route with the official OpenAI JS SDK
import OpenAI from "https://deno.land/x/[email protected]/mod.ts";
const client = new OpenAI({
apiKey: process.env['MEILI_API_KEY'],
baseURL: 'http://localhost:7700/chats/main',
});
// Can be called by Meilisearch to inform the front-end about its progression
const meili_search_progress = {
"type": "function",
"function": {
"name": "_meiliSearchProgress",
"description": "Give context about what Meilisearch is doing",
"parameters": {
"type": "object",
"properties": {
"function_name": {
"type": "string",
"description": "The name of the function we are executing"
},
"function_parameters": {
"type": "string",
"description": "The parameters of the function we are executing, encoded in JSON"
}
},
"required": ["functionName", "functionParameters"],
"additionalProperties": false
},
"strict": true
}
};
// Will be called whenever a new message must be appended to the conversation
let meili_append_conversation_message = {
"type": "function",
"function": {
"name": "_meiliAppendConversationMessage",
"description": "Append a new message to the conversation based on what happened internally",
"parameters": {
"type": "object",
"properties": {
"role": {
"type": "string",
"description": "The role of the messages author, either `role` or `assistant`"
},
"content": {
"type": "string",
"description": "The contents of the `assistant` or `tool` message. Required unless `tool_calls` is specified."
},
"tool_calls": {
"type": ["array", "null"],
"description": "The tool calls generated by the model, such as function calls",
"items": {
"type": "object",
"properties": {
"function": {
"type": "object",
"description": "The function that the model called",
"properties": {
"name": {
"type": "string",
"description": "The name of the function to call"
},
"arguments": {
"type": "string",
"description": "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."
}
}
},
"id": {
"type": "string",
"description": "The ID of the tool call"
},
"type": {
"type": "string",
"description": "The type of the tool. Currently, only function is supported"
}
}
}
},
"tool_call_id": {
"type": ["string", "null"],
"description": "Tool call that this message is responding to",
}
},
"required": ["role", "content", "tool_calls", "tool_call_id"],
"additionalProperties": false
},
"strict": true
}
};
let messages = [
// { role: 'developer', content: 'Talk like a pirate.' },
];
while (true) {
const input = prompt('> ');
if (input === null) { break }
messages.push({ role: 'user', content: input });
const stream = await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: messages,
tools: [meili_search_progress, meili_append_conversation_message],
stream: true,
});
let assistant_message = new String;
for await (const chunk of stream) {
const delta = chunk.choices[0].delta;
if (delta.content) {
assistant_message += delta.content;
const encoder = new TextEncoder();
const data = encoder.encode(delta.content);
let bytesWritten = 0;
while (bytesWritten < data.length) {
const result = Deno.stdout.writeSync(data.subarray(bytesWritten));
bytesWritten += result;
}
}
if (delta.tool_calls) {
// console.log(delta.tool_calls);
for (const call of delta.tool_calls) {
const fun = call.function;
if (fun.name === '_meiliSearchProgress') {
const prog = JSON.parse(fun.arguments);
console.log(`Doing progress on ${prog.function_name} with ${prog.function_arguments}`);
}
if (fun.name === '_meiliAppendConversationMessage') {
// Extract, parse and append the message to the conversation
messages.push(JSON.parse(fun.arguments));
}
}
}
}
// Don't forget to push the assistant message to the conversation
messages.push({ role: 'assistant', content: assistant_message });
}
console.log("Here is the conversation history:");
console.log(messages);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment